diff --git a/build.sh b/build.sh index 3565b82..6d6994a 100755 --- a/build.sh +++ b/build.sh @@ -68,6 +68,7 @@ ports=( "lsb_vsx" "heatshrink" "smolrtsp" + "wamr" ) diff --git a/wamr/build.sh b/wamr/build.sh new file mode 100755 index 0000000..ae1aa4d --- /dev/null +++ b/wamr/build.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +set -e + +version="${PORTS_WAMR_VERSION:-2.4.2}" +archive_filename="WAMR-${version}.tar.gz" +PREFIX_PORT_SRC="${PREFIX_PORT_BUILD}/${version}" +PRODUCT_MINI_DIR=${PREFIX_PORT_SRC}/product-mini/platforms/phoenix/build +PLATFORMS_BUILD_PATH="${PREFIX_PORT_SRC}/core/shared/platform" +DEBUG=0 + +if [ $DEBUG = 1 ]; then + DEBUG_FLAG="-DCMAKE_BUILD_TYPE=Debug" +fi + +if ! [[ "${TARGET_FAMILY}" =~ "armv7m" || "${TARGET_FAMILY}" =~ "armv8m" ]]; then + TLS_FLAG="-DTARGET_HAS_TLS=1" +fi + +if [[ "${TARGET_FAMILY}" =~ "arm" ]]; then + TARGET_FLAG="-DWAMR_BUILD_TARGET=THUMB" + INVOKE_GENERAL_FLAG="-DWAMR_BUILD_INVOKE_NATIVE_GENERAL=1" +elif [[ "${TARGET_FAMILY}" =~ "aarch" ]]; then + TARGET_FLAG="-DWAMR_BUILD_TARGET=AARCH64" +elif [[ "${TARGET_FAMILY}" =~ "riscv" ]]; then + TARGET_FLAG="-DWAMR_BUILD_TARGET=RISCV64" +fi + +b_port_download "https://github.com/bytecodealliance/wasm-micro-runtime/archive/refs/tags/" "${archive_filename}" + +if [ ! -d "${PREFIX_PORT_SRC}" ]; then + echo "Extracting sources from ${archive_filename}" + mkdir -p "${PREFIX_PORT_SRC}" + tar -axf "${PREFIX_PORT}/${archive_filename}" --strip-components 1 -C "${PREFIX_PORT_SRC}" +fi + +b_port_apply_patches "${PREFIX_PORT_SRC}" "${version}" + +mkdir -p "${PLATFORMS_BUILD_PATH}/phoenix" +cp "${PLATFORMS_BUILD_PATH}/linux/platform_internal.h" "${PLATFORMS_BUILD_PATH}/phoenix/platform_internal.h" +cp "${PLATFORMS_BUILD_PATH}/linux/platform_init.c" "${PLATFORMS_BUILD_PATH}/phoenix/platform_init.c" +cp "${PLATFORMS_BUILD_PATH}/linux/shared_platform.cmake" "${PLATFORMS_BUILD_PATH}/phoenix/shared_platform.cmake" + +mkdir -p "${PREFIX_PORT_SRC}/product-mini/platforms/phoenix" +cp "${PREFIX_PORT_SRC}/product-mini/platforms/posix/main.c" "${PREFIX_PORT_SRC}/product-mini/platforms/phoenix/" +cp "${PREFIX_PORT_SRC}/product-mini/platforms/linux/CMakeLists.txt" "${PREFIX_PORT_SRC}/product-mini/platforms/phoenix/" +rm -rf "${PREFIX_PORT_SRC}/product-mini/platforms/phoenix/native_libs" +cp -r "${PREFIX_PROJECT}/${WAMR_NATIVE_LIBS_DIR:-phoenix-rtos-ports/wamr/platform_files/native_libs}" "${PREFIX_PORT_SRC}/product-mini/platforms/phoenix/native_libs" + +WAMR_FLAGS="-DWAMR_BUILD_PLATFORM=phoenix $TARGET_FLAG \ + -DWAMR_BUILD_AOT=0 \ + -DWAMR_DISABLE_HW_BOUND_CHECK=1 ${INVOKE_GENERAL_FLAG} -DWAMR_BUILD_MULTI_MODULE=1 \ + -DWAMR_BUILD_SHRUNK_MEMORY=1 -DWAMR_BUILD_EXTENDED_CONST_EXPR=1 -DWAMR_BUILD_TAIL_CALL=1 \ + ${DEBUG_FLAG} ${TLS_FLAG}" + +if [[ ${WAMR_LOW_MEMORY} = 1 ]]; then + echo "Compiling in low memory mode" + WAMR_FLAGS="${WAMR_FLAGS} -DWAMR_BUILD_LIBC_WASI=0 -DWAMR_APP_THREAD_STACK_SIZE_MAX=131072 \ + -DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_SIMD=0 -DWAMR_BUILD_LIB_WASI_THREADS=0" +else + WAMR_FLAGS="${WAMR_FLAGS} -DWAMR_BUILD_LIB_WASI_THREADS=1 -DWAMR_BUILD_BULK_MEMORY=1 \ + -DWAMR_BUILD_LIB_SIMDE=1 -DWAMR_BUILD_SIMD=1" +fi + +mkdir -p "${PRODUCT_MINI_DIR}" +(cd "${PRODUCT_MINI_DIR}" && cmake ${WAMR_FLAGS} .. && make) + +mkdir -p "${PREFIX_ROOTFS}"usr/bin + +if [ ${DEBUG} = 0 ]; then + $STRIP -o "${PRODUCT_MINI_DIR}/iwasm" "${PRODUCT_MINI_DIR}/iwasm-${version}" + b_install "${PRODUCT_MINI_DIR}/iwasm" /usr/bin/ +else + mv "${PRODUCT_MINI_DIR}/iwasm-${version}" "${PREFIX_ROOTFS}usr/bin/iwasm" +fi + diff --git a/wamr/checksums/WAMR-2.4.2.tar.gz.sha256 b/wamr/checksums/WAMR-2.4.2.tar.gz.sha256 new file mode 100644 index 0000000..04c0e40 --- /dev/null +++ b/wamr/checksums/WAMR-2.4.2.tar.gz.sha256 @@ -0,0 +1 @@ +73380561a01f4863506e855c2c265cf03c5b6efb17bbb8c9bbafe80745fd00ef WAMR-2.4.2.tar.gz diff --git a/wamr/native_libs_example/example/example.c b/wamr/native_libs_example/example/example.c new file mode 100644 index 0000000..ba73f52 --- /dev/null +++ b/wamr/native_libs_example/example/example.c @@ -0,0 +1,14 @@ +#include "wasm_export.h" +#include "example.h" +#include +#include + +int foo1(wasm_exec_env_t exec_env, int a, int b) +{ + return a * b + 3; +} + +void foo2(wasm_exec_env_t exec_env, unsigned char *msg, uint8_t *buffer, int buf_len) +{ + strncpy(msg, buffer, buf_len); +} diff --git a/wamr/native_libs_example/example/example.h b/wamr/native_libs_example/example/example.h new file mode 100644 index 0000000..451965c --- /dev/null +++ b/wamr/native_libs_example/example/example.h @@ -0,0 +1,4 @@ +#include "wasm_export.h" + +int foo1(wasm_exec_env_t exec_env, int a, int b); +void foo2(wasm_exec_env_t exec_env, unsigned char *msg, uint8_t *buffer, int buf_len); diff --git a/wamr/native_libs_example/native_libs.c b/wamr/native_libs_example/native_libs.c new file mode 100644 index 0000000..b7a9ba3 --- /dev/null +++ b/wamr/native_libs_example/native_libs.c @@ -0,0 +1,16 @@ +#include +#include "wasm_export.h" +#include "example/example.h" + +NativeSymbol native_symbols[] = { + { "foo1", // the name of WASM function name + foo1, // the native function pointer + "(ii)i", // the function prototype signature + NULL }, + { "foo2", // the name of WASM function name + foo2, // the native function pointer + "($*~)", // the function prototype signature + NULL } +}; + +int n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol); diff --git a/wamr/patches/2.4.2/CMakeLists.patch b/wamr/patches/2.4.2/CMakeLists.patch new file mode 100644 index 0000000..32acce1 --- /dev/null +++ b/wamr/patches/2.4.2/CMakeLists.patch @@ -0,0 +1,103 @@ +--- a/product-mini/platforms/linux/CMakeLists.txt 2025-09-15 11:11:18.000000000 +0200 ++++ b/product-mini/platforms/linux/CMakeLists.txt 2025-09-25 15:53:48.579867600 +0200 +@@ -3,15 +3,17 @@ + + cmake_minimum_required (VERSION 3.14) + ++SET(CMAKE_FIND_ROOT_PATH $ENV{PHOENIX_SYSROOT}) ++ + include(CheckPIESupported) + +-project (iwasm) ++project (iwasm C) + + option(BUILD_SHARED_LIBS "Build using shared libraries" OFF) + + set (CMAKE_VERBOSE_MAKEFILE OFF) + +-set (WAMR_BUILD_PLATFORM "linux") ++set (WAMR_BUILD_PLATFORM "phoenix") + + # Reset default linker flags + set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") +@@ -49,14 +51,19 @@ + set(CMAKE_BUILD_TYPE Release) + endif () + ++if (DEFINED TARGET_HAS_TLS) ++ set(TARGET_HAS_TLS 1) ++ message(STATUS "Target supports thread local storage") ++endif () ++ + if (NOT DEFINED WAMR_BUILD_INTERP) + # Enable Interpreter by default + set (WAMR_BUILD_INTERP 1) + endif () + + if (NOT DEFINED WAMR_BUILD_AOT) +- # Enable AOT by default. +- set (WAMR_BUILD_AOT 1) ++ # Disable AOT by default. ++ set (WAMR_BUILD_AOT 0) + endif () + + if (NOT DEFINED WAMR_BUILD_JIT) +@@ -95,8 +102,8 @@ + endif () + + if (NOT DEFINED WAMR_BUILD_LIB_WASI_THREADS) +- # Disable wasi threads library by default +- set (WAMR_BUILD_LIB_WASI_THREADS 0) ++ # Enable wasi threads library by default ++ set (WAMR_BUILD_LIB_WASI_THREADS 1) + endif() + + if (NOT DEFINED WAMR_BUILD_MINI_LOADER) +@@ -125,12 +132,6 @@ + set (WAMR_BUILD_SIMD 0) + endif () + +-# if enable wasi-nn, both wasi-nn-backends and iwasm +-# need to use same WAMR (dynamic) libraries +-if (WAMR_BUILD_WASI_NN EQUAL 1) +- set (BUILD_SHARED_LIBS ON) +-endif () +- + set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..) + + include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) +@@ -139,10 +140,10 @@ + + set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + +-set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow") ++set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow -Os") + # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion") + +-set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused") ++set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused -Os") + + if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") + if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang")) +@@ -162,7 +163,11 @@ + + include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) + +-add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE}) ++file(GLOB_RECURSE NATIVE_LIBRARIES_SOURCE ++ "${CMAKE_CURRENT_SOURCE_DIR}/native_libs/*.c" ++) ++ ++add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE} ${NATIVE_LIBRARIES_SOURCE}) + + set_version_info (iwasm) + +@@ -192,7 +197,7 @@ + POSITION_INDEPENDENT_CODE ON + ) + +-target_link_libraries (vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread) ++target_link_libraries (vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -lpthread) # Removed -ldl because we don't support dynamic linking + + install (TARGETS vmlib + EXPORT iwasmTargets diff --git a/wamr/patches/2.4.2/main.patch b/wamr/patches/2.4.2/main.patch new file mode 100644 index 0000000..9c30322 --- /dev/null +++ b/wamr/patches/2.4.2/main.patch @@ -0,0 +1,698 @@ +--- a/product-mini/platforms/posix/main.c 2025-09-15 11:11:18.000000000 +0200 ++++ b/product-mini/platforms/posix/main.c 2025-09-30 10:31:24.988088154 +0200 +@@ -3,9 +3,6 @@ + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +-#ifndef _GNU_SOURCE +-#define _GNU_SOURCE +-#endif + #include + #include + #include +@@ -20,9 +17,6 @@ + + #include "../common/wasm_proposal.c" + +-#if BH_HAS_DLFCN +-#include +-#endif + + static int app_argc; + static char **app_argv; +@@ -42,44 +36,16 @@ + #if WASM_ENABLE_INTERP != 0 + printf(" --interp Run the wasm app with interpreter mode\n"); + #endif +-#if WASM_ENABLE_FAST_JIT != 0 +- printf(" --fast-jit Run the wasm app with fast jit mode\n"); +-#endif +-#if WASM_ENABLE_JIT != 0 +- printf(" --llvm-jit Run the wasm app with llvm jit mode\n"); +-#endif +-#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0 +- printf(" --multi-tier-jit Run the wasm app with multi-tier jit mode\n"); +-#endif + printf(" --stack-size=n Set maximum stack size in bytes, default is 64 KB\n"); + #if WASM_ENABLE_LIBC_WASI !=0 + printf(" --heap-size=n Set maximum heap size in bytes, default is 0 KB when libc wasi is enabled\n"); + #else +- printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB when libc wasi is diabled\n"); +-#endif +-#if WASM_ENABLE_FAST_JIT != 0 +- printf(" --jit-codecache-size=n Set fast jit maximum code cache size in bytes,\n"); +- printf(" default is %u KB\n", FAST_JIT_DEFAULT_CODE_CACHE_SIZE / 1024); ++ printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB when libc wasi is disabled\n"); + #endif + #if WASM_ENABLE_GC != 0 + printf(" --gc-heap-size=n Set maximum gc heap size in bytes,\n"); + printf(" default is %u KB\n", GC_HEAP_SIZE_DEFAULT / 1024); + #endif +-#if WASM_ENABLE_JIT != 0 +- printf(" --llvm-jit-size-level=n Set LLVM JIT size level, default is 3\n"); +- printf(" --llvm-jit-opt-level=n Set LLVM JIT optimization level, default is 3\n"); +-#if defined(os_writegsbase) +- printf(" --enable-segue[=] Enable using segment register GS as the base address of\n"); +- printf(" linear memory, which may improve performance, flags can be:\n"); +- printf(" i32.load, i64.load, f32.load, f64.load, v128.load,\n"); +- printf(" i32.store, i64.store, f32.store, f64.store, v128.store\n"); +- printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n"); +- printf(" and --enable-segue means all flags are added.\n"); +-#endif +-#endif /* WASM_ENABLE_JIT != 0*/ +-#if WASM_ENABLE_LINUX_PERF != 0 +- printf(" --enable-linux-perf Enable linux perf support. It works in aot and llvm-jit.\n"); +-#endif + printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n" + " that runs commands in the form of \"FUNC ARG...\"\n"); + #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0 +@@ -88,11 +54,6 @@ + #if WASM_ENABLE_LIBC_WASI != 0 + libc_wasi_print_help(); + #endif +-#if BH_HAS_DLFCN +- printf(" --native-lib= Register native libraries to the WASM module, which\n"); +- printf(" are shared object (.so) files, for example:\n"); +- printf(" --native-lib=test1.so --native-lib=test2.so\n"); +-#endif + #if WASM_ENABLE_MULTI_MODULE != 0 + printf(" --module-path= Indicate a module search path. default is current\n" + " directory('./')\n"); +@@ -105,13 +66,6 @@ + printf(" If it expires, the runtime aborts the execution\n"); + printf(" with a trap.\n"); + #endif +-#if WASM_ENABLE_DEBUG_INTERP != 0 +- printf(" -g=ip:port Set the debug sever address, default is debug disabled\n"); +- printf(" if port is 0, then a random port will be used\n"); +-#endif +-#if WASM_ENABLE_STATIC_PGO != 0 +- printf(" --gen-prof-file= Generate LLVM PGO (Profile-Guided Optimization) profile file\n"); +-#endif + printf(" --version Show version information\n"); + return 1; + } +@@ -219,183 +173,6 @@ + return NULL; + } + +-#if WASM_ENABLE_JIT != 0 +-static uint32 +-resolve_segue_flags(char *str_flags) +-{ +- uint32 segue_flags = 0; +- int32 flag_count, i; +- char **flag_list; +- +- flag_list = split_string(str_flags, &flag_count, ","); +- if (flag_list) { +- for (i = 0; i < flag_count; i++) { +- if (!strcmp(flag_list[i], "i32.load")) { +- segue_flags |= 1 << 0; +- } +- else if (!strcmp(flag_list[i], "i64.load")) { +- segue_flags |= 1 << 1; +- } +- else if (!strcmp(flag_list[i], "f32.load")) { +- segue_flags |= 1 << 2; +- } +- else if (!strcmp(flag_list[i], "f64.load")) { +- segue_flags |= 1 << 3; +- } +- else if (!strcmp(flag_list[i], "v128.load")) { +- segue_flags |= 1 << 4; +- } +- else if (!strcmp(flag_list[i], "i32.store")) { +- segue_flags |= 1 << 8; +- } +- else if (!strcmp(flag_list[i], "i64.store")) { +- segue_flags |= 1 << 9; +- } +- else if (!strcmp(flag_list[i], "f32.store")) { +- segue_flags |= 1 << 10; +- } +- else if (!strcmp(flag_list[i], "f64.store")) { +- segue_flags |= 1 << 11; +- } +- else if (!strcmp(flag_list[i], "v128.store")) { +- segue_flags |= 1 << 12; +- } +- else { +- /* invalid flag */ +- segue_flags = (uint32)-1; +- break; +- } +- } +- free(flag_list); +- } +- return segue_flags; +-} +-#endif /* end of WASM_ENABLE_JIT != 0 */ +- +-#if BH_HAS_DLFCN +-struct native_lib { +- void *handle; +- +- uint32 (*get_native_lib)(char **p_module_name, +- NativeSymbol **p_native_symbols); +- int (*init_native_lib)(void); +- void (*deinit_native_lib)(void); +- +- char *module_name; +- NativeSymbol *native_symbols; +- uint32 n_native_symbols; +-}; +- +-struct native_lib * +-load_native_lib(const char *name) +-{ +- struct native_lib *lib = wasm_runtime_malloc(sizeof(*lib)); +- if (lib == NULL) { +- LOG_WARNING("warning: failed to load native library %s because of " +- "allocation failure", +- name); +- goto fail; +- } +- memset(lib, 0, sizeof(*lib)); +- +- /* open the native library */ +- if (!(lib->handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL)) +- && !(lib->handle = dlopen(name, RTLD_LAZY))) { +- LOG_WARNING("warning: failed to load native library %s. %s", name, +- dlerror()); +- goto fail; +- } +- +- lib->init_native_lib = dlsym(lib->handle, "init_native_lib"); +- lib->get_native_lib = dlsym(lib->handle, "get_native_lib"); +- lib->deinit_native_lib = dlsym(lib->handle, "deinit_native_lib"); +- +- if (!lib->get_native_lib) { +- LOG_WARNING("warning: failed to lookup `get_native_lib` function " +- "from native lib %s", +- name); +- goto fail; +- } +- +- if (lib->init_native_lib) { +- int ret = lib->init_native_lib(); +- if (ret != 0) { +- LOG_WARNING("warning: `init_native_lib` function from native " +- "lib %s failed with %d", +- name, ret); +- goto fail; +- } +- } +- +- lib->n_native_symbols = +- lib->get_native_lib(&lib->module_name, &lib->native_symbols); +- +- /* register native symbols */ +- if (!(lib->n_native_symbols > 0 && lib->module_name && lib->native_symbols +- && wasm_runtime_register_natives( +- lib->module_name, lib->native_symbols, lib->n_native_symbols))) { +- LOG_WARNING("warning: failed to register native lib %s", name); +- if (lib->deinit_native_lib) { +- lib->deinit_native_lib(); +- } +- goto fail; +- } +- return lib; +-fail: +- if (lib != NULL) { +- if (lib->handle != NULL) { +- dlclose(lib->handle); +- } +- wasm_runtime_free(lib); +- } +- return NULL; +-} +- +-static uint32 +-load_and_register_native_libs(const char **native_lib_list, +- uint32 native_lib_count, +- struct native_lib **native_lib_loaded_list) +-{ +- uint32 i, native_lib_loaded_count = 0; +- +- for (i = 0; i < native_lib_count; i++) { +- struct native_lib *lib = load_native_lib(native_lib_list[i]); +- if (lib == NULL) { +- continue; +- } +- native_lib_loaded_list[native_lib_loaded_count++] = lib; +- } +- +- return native_lib_loaded_count; +-} +- +-static void +-unregister_and_unload_native_libs(uint32 native_lib_count, +- struct native_lib **native_lib_loaded_list) +-{ +- uint32 i; +- +- for (i = 0; i < native_lib_count; i++) { +- struct native_lib *lib = native_lib_loaded_list[i]; +- +- /* unregister native symbols */ +- if (!wasm_runtime_unregister_natives(lib->module_name, +- lib->native_symbols)) { +- LOG_WARNING("warning: failed to unregister native lib %p", +- lib->handle); +- continue; +- } +- +- if (lib->deinit_native_lib) { +- lib->deinit_native_lib(); +- } +- +- dlclose(lib->handle); +- wasm_runtime_free(lib); +- } +-} +-#endif /* BH_HAS_DLFCN */ +- + #if WASM_ENABLE_MULTI_MODULE != 0 + static char * + handle_module_path(const char *module_path) +@@ -415,10 +192,6 @@ + if (module_type == Wasm_Module_Bytecode) + file_format = ".wasm"; + #endif +-#if WASM_ENABLE_AOT != 0 +- if (module_type == Wasm_Module_AoT) +- file_format = ".aot"; +-#endif + bh_assert(file_format); + const char *format = "%s/%s%s"; + int sz = strlen(module_search_path) + strlen("/") + strlen(module_name) +@@ -490,44 +263,6 @@ + } + #endif /* end of WASM_ENABLE_GLOBAL_HEAP_POOL */ + +-#if WASM_ENABLE_STATIC_PGO != 0 +-static void +-dump_pgo_prof_data(wasm_module_inst_t module_inst, const char *path) +-{ +- char *buf; +- uint32 len; +- FILE *file; +- +- if (!(len = wasm_runtime_get_pgo_prof_data_size(module_inst))) { +- printf("failed to get LLVM PGO profile data size\n"); +- return; +- } +- +- if (!(buf = wasm_runtime_malloc(len))) { +- printf("allocate memory failed\n"); +- return; +- } +- +- if (len != wasm_runtime_dump_pgo_prof_data_to_buf(module_inst, buf, len)) { +- printf("failed to dump LLVM PGO profile data\n"); +- wasm_runtime_free(buf); +- return; +- } +- +- if (!(file = fopen(path, "wb"))) { +- printf("failed to create file %s", path); +- wasm_runtime_free(buf); +- return; +- } +- fwrite(buf, len, 1, file); +- fclose(file); +- +- wasm_runtime_free(buf); +- +- printf("LLVM raw profile file %s was generated.\n", path); +-} +-#endif +- + #if WASM_ENABLE_THREAD_MGR != 0 + struct timeout_arg { + uint32 timeout_ms; +@@ -564,6 +299,9 @@ + } + #endif + ++extern NativeSymbol native_symbols[]; ++extern int n_native_symbols; ++ + int + main(int argc, char *argv[]) + { +@@ -578,25 +316,13 @@ + #else + uint32 heap_size = 16 * 1024; + #endif +-#if WASM_ENABLE_FAST_JIT != 0 +- uint32 jit_code_cache_size = FAST_JIT_DEFAULT_CODE_CACHE_SIZE; +-#endif + #if WASM_ENABLE_GC != 0 + uint32 gc_heap_size = GC_HEAP_SIZE_DEFAULT; + #endif +-#if WASM_ENABLE_JIT != 0 +- uint32 llvm_jit_size_level = 3; +- uint32 llvm_jit_opt_level = 3; +- uint32 segue_flags = 0; +-#endif +-#if WASM_ENABLE_LINUX_PERF != 0 +- bool enable_linux_perf = false; +-#endif + wasm_module_t wasm_module = NULL; + wasm_module_inst_t wasm_module_inst = NULL; + RunningMode running_mode = 0; + RuntimeInitArgs init_args; +- struct InstantiationArgs2 *inst_args; + char error_buf[128] = { 0 }; + #if WASM_ENABLE_LOG != 0 + int log_verbose_level = 2; +@@ -609,19 +335,6 @@ + #if WASM_ENABLE_LIBC_WASI != 0 + libc_wasi_parse_context_t wasi_parse_ctx; + #endif +-#if BH_HAS_DLFCN +- const char *native_lib_list[8] = { NULL }; +- uint32 native_lib_count = 0; +- struct native_lib *native_lib_loaded_list[8]; +- uint32 native_lib_loaded_count = 0; +-#endif +-#if WASM_ENABLE_DEBUG_INTERP != 0 +- char *ip_addr = NULL; +- int instance_port = 0; +-#endif +-#if WASM_ENABLE_STATIC_PGO != 0 +- const char *gen_prof_file = NULL; +-#endif + #if WASM_ENABLE_THREAD_MGR != 0 + int timeout_ms = -1; + #endif +@@ -644,22 +357,6 @@ + running_mode = Mode_Interp; + } + #endif +-#if WASM_ENABLE_FAST_JIT != 0 +- else if (!strcmp(argv[0], "--fast-jit")) { +- running_mode = Mode_Fast_JIT; +- } +-#endif +-#if WASM_ENABLE_JIT != 0 +- else if (!strcmp(argv[0], "--llvm-jit")) { +- running_mode = Mode_LLVM_JIT; +- } +-#endif +-#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \ +- && WASM_ENABLE_LAZY_JIT != 0 +- else if (!strcmp(argv[0], "--multi-tier-jit")) { +- running_mode = Mode_Multi_Tier_JIT; +- } +-#endif + #if WASM_ENABLE_LOG != 0 + else if (!strncmp(argv[0], "-v=", 3)) { + log_verbose_level = atoi(argv[0] + 3); +@@ -685,13 +382,6 @@ + return print_help(); + heap_size = atoi(argv[0] + 12); + } +-#if WASM_ENABLE_FAST_JIT != 0 +- else if (!strncmp(argv[0], "--jit-codecache-size=", 21)) { +- if (argv[0][21] == '\0') +- return print_help(); +- jit_code_cache_size = atoi(argv[0] + 21); +- } +-#endif + #if WASM_ENABLE_GC != 0 + else if (!strncmp(argv[0], "--gc-heap-size=", 15)) { + if (argv[0][15] == '\0') +@@ -699,64 +389,6 @@ + gc_heap_size = atoi(argv[0] + 15); + } + #endif +-#if WASM_ENABLE_JIT != 0 +- else if (!strncmp(argv[0], "--llvm-jit-size-level=", 22)) { +- if (argv[0][22] == '\0') +- return print_help(); +- llvm_jit_size_level = atoi(argv[0] + 22); +- if (llvm_jit_size_level < 1) { +- printf("LLVM JIT size level shouldn't be smaller than 1, " +- "setting it to 1\n"); +- llvm_jit_size_level = 1; +- } +- else if (llvm_jit_size_level > 3) { +- printf("LLVM JIT size level shouldn't be greater than 3, " +- "setting it to 3\n"); +- llvm_jit_size_level = 3; +- } +- } +- else if (!strncmp(argv[0], "--llvm-jit-opt-level=", 21)) { +- if (argv[0][21] == '\0') +- return print_help(); +- llvm_jit_opt_level = atoi(argv[0] + 21); +- if (llvm_jit_opt_level < 1) { +- printf("LLVM JIT opt level shouldn't be smaller than 1, " +- "setting it to 1\n"); +- llvm_jit_opt_level = 1; +- } +- else if (llvm_jit_opt_level > 3) { +- printf("LLVM JIT opt level shouldn't be greater than 3, " +- "setting it to 3\n"); +- llvm_jit_opt_level = 3; +- } +- } +- else if (!strcmp(argv[0], "--enable-segue")) { +- /* all flags are enabled */ +- segue_flags = 0x1F1F; +- } +- else if (!strncmp(argv[0], "--enable-segue=", 15)) { +- segue_flags = resolve_segue_flags(argv[0] + 15); +- if (segue_flags == (uint32)-1) +- return print_help(); +- } +-#endif /* end of WASM_ENABLE_JIT != 0 */ +-#if BH_HAS_DLFCN +- else if (!strncmp(argv[0], "--native-lib=", 13)) { +- if (argv[0][13] == '\0') +- return print_help(); +- if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) { +- printf("Only allow max native lib number %d\n", +- (int)(sizeof(native_lib_list) / sizeof(char *))); +- return 1; +- } +- native_lib_list[native_lib_count++] = argv[0] + 13; +- } +-#endif +-#if WASM_ENABLE_LINUX_PERF != 0 +- else if (!strncmp(argv[0], "--enable-linux-perf", 19)) { +- enable_linux_perf = true; +- } +-#endif + #if WASM_ENABLE_MULTI_MODULE != 0 + else if (!strncmp(argv[0], + "--module-path=", strlen("--module-path="))) { +@@ -780,26 +412,6 @@ + timeout_ms = atoi(argv[0] + 10); + } + #endif +-#if WASM_ENABLE_DEBUG_INTERP != 0 +- else if (!strncmp(argv[0], "-g=", 3)) { +- char *port_str = strchr(argv[0] + 3, ':'); +- char *port_end; +- if (port_str == NULL) +- return print_help(); +- *port_str = '\0'; +- instance_port = strtoul(port_str + 1, &port_end, 10); +- if (port_str[1] == '\0' || *port_end != '\0') +- return print_help(); +- ip_addr = argv[0] + 3; +- } +-#endif +-#if WASM_ENABLE_STATIC_PGO != 0 +- else if (!strncmp(argv[0], "--gen-prof-file=", 16)) { +- if (argv[0][16] == '\0') +- return print_help(); +- gen_prof_file = argv[0] + 16; +- } +-#endif + else if (!strcmp(argv[0], "--version")) { + uint32 major, minor, patch; + wasm_runtime_get_version(&major, &minor, &patch); +@@ -852,30 +464,10 @@ + init_args.mem_alloc_option.allocator.free_func = free_func; + #endif + +-#if WASM_ENABLE_FAST_JIT != 0 +- init_args.fast_jit_code_cache_size = jit_code_cache_size; +-#endif +- + #if WASM_ENABLE_GC != 0 + init_args.gc_heap_size = gc_heap_size; + #endif + +-#if WASM_ENABLE_JIT != 0 +- init_args.llvm_jit_size_level = llvm_jit_size_level; +- init_args.llvm_jit_opt_level = llvm_jit_opt_level; +- init_args.segue_flags = segue_flags; +-#endif +-#if WASM_ENABLE_LINUX_PERF != 0 +- init_args.enable_linux_perf = enable_linux_perf; +-#endif +- +-#if WASM_ENABLE_DEBUG_INTERP != 0 +- init_args.instance_port = instance_port; +- if (ip_addr) +- /* ensure that init_args.ip_addr is null terminated */ +- strncpy(init_args.ip_addr, ip_addr, sizeof(init_args.ip_addr) - 1); +-#endif +- + /* initialize runtime environment */ + if (!wasm_runtime_full_init(&init_args)) { + printf("Init runtime environment failed.\n"); +@@ -886,45 +478,18 @@ + bh_log_set_verbose_level(log_verbose_level); + #endif + +-#if BH_HAS_DLFCN +- native_lib_loaded_count = load_and_register_native_libs( +- native_lib_list, native_lib_count, native_lib_loaded_list); +-#endif ++ ++ if (!wasm_runtime_register_natives("env", ++ native_symbols, ++ n_native_symbols)) { ++ goto fail1; ++ } + + /* load WASM byte buffer from WASM bin file */ + if (!(wasm_file_buf = + (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size))) + goto fail1; + +-#if WASM_ENABLE_AOT != 0 +- if (wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size)) { +- uint8 *wasm_file_mapped; +- uint8 *daddr; +- int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC; +- int map_flags = MMAP_MAP_32BIT; +- +- if (!(wasm_file_mapped = os_mmap(NULL, (uint32)wasm_file_size, map_prot, +- map_flags, os_get_invalid_handle()))) { +- printf("mmap memory failed\n"); +- wasm_runtime_free(wasm_file_buf); +- goto fail1; +- } +- +-#if (WASM_MEM_DUAL_BUS_MIRROR != 0) +- daddr = os_get_dbus_mirror(wasm_file_mapped); +-#else +- daddr = wasm_file_mapped; +-#endif +- bh_memcpy_s(daddr, wasm_file_size, wasm_file_buf, wasm_file_size); +-#if (WASM_MEM_DUAL_BUS_MIRROR != 0) +- os_dcache_flush(); +-#endif +- wasm_runtime_free(wasm_file_buf); +- wasm_file_buf = wasm_file_mapped; +- is_xip_file = true; +- } +-#endif +- + #if WASM_ENABLE_MULTI_MODULE != 0 + wasm_runtime_set_module_reader(module_reader_callback, + module_destroyer_callback); +@@ -937,33 +502,14 @@ + goto fail2; + } + +-#if WASM_ENABLE_DYNAMIC_AOT_DEBUG != 0 +- if (!wasm_runtime_set_module_name(wasm_module, wasm_file, error_buf, +- sizeof(error_buf))) { +- printf("set aot module name failed in dynamic aot debug mode, %s\n", +- error_buf); +- goto fail3; +- } +-#endif +- + #if WASM_ENABLE_LIBC_WASI != 0 + libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx); + #endif + +- if (!wasm_runtime_instantiation_args_create(&inst_args)) { +- printf("failed to create instantiate args\n"); +- goto fail3; +- } +- wasm_runtime_instantiation_args_set_default_stack_size(inst_args, +- stack_size); +- wasm_runtime_instantiation_args_set_host_managed_heap_size(inst_args, +- heap_size); +- + /* instantiate the module */ +- wasm_module_inst = wasm_runtime_instantiate_ex2( +- wasm_module, inst_args, error_buf, sizeof(error_buf)); +- wasm_runtime_instantiation_args_destroy(inst_args); +- if (!wasm_module_inst) { ++ if (!(wasm_module_inst = ++ wasm_runtime_instantiate(wasm_module, stack_size, heap_size, ++ error_buf, sizeof(error_buf)))) { + printf("%s\n", error_buf); + goto fail3; + } +@@ -974,23 +520,6 @@ + } + #endif + +-#if WASM_ENABLE_DEBUG_INTERP != 0 +- if (ip_addr != NULL) { +- wasm_exec_env_t exec_env = +- wasm_runtime_get_exec_env_singleton(wasm_module_inst); +- uint32_t debug_port; +- if (exec_env == NULL) { +- printf("%s\n", wasm_runtime_get_exception(wasm_module_inst)); +- goto fail4; +- } +- debug_port = wasm_runtime_start_debug_instance(exec_env); +- if (debug_port == 0) { +- printf("Failed to start debug instance\n"); +- goto fail4; +- } +- } +-#endif +- + #if WASM_ENABLE_THREAD_MGR != 0 + struct timeout_arg timeout_arg; + korp_tid timeout_tid; +@@ -1037,12 +566,6 @@ + if (exception) + printf("%s\n", exception); + +-#if WASM_ENABLE_STATIC_PGO != 0 && WASM_ENABLE_AOT != 0 +- if (get_package_type(wasm_file_buf, wasm_file_size) == Wasm_Module_AoT +- && gen_prof_file) +- dump_pgo_prof_data(wasm_module_inst, gen_prof_file); +-#endif +- + #if WASM_ENABLE_THREAD_MGR != 0 + if (timeout_ms >= 0) { + timeout_arg.cancel = true; +@@ -1053,9 +576,6 @@ + #if WASM_ENABLE_THREAD_MGR != 0 + fail5: + #endif +-#if WASM_ENABLE_DEBUG_INTERP != 0 +-fail4: +-#endif + /* destroy the module instance */ + wasm_runtime_deinstantiate(wasm_module_inst); + +@@ -1071,11 +591,6 @@ + os_munmap(wasm_file_buf, wasm_file_size); + + fail1: +-#if BH_HAS_DLFCN +- /* unload the native libraries */ +- unregister_and_unload_native_libs(native_lib_loaded_count, +- native_lib_loaded_list); +-#endif + + /* destroy runtime environment */ + wasm_runtime_destroy(); diff --git a/wamr/patches/2.4.2/math.patch b/wamr/patches/2.4.2/math.patch new file mode 100644 index 0000000..dd4be63 --- /dev/null +++ b/wamr/patches/2.4.2/math.patch @@ -0,0 +1,881 @@ +--- a/core/shared/platform/common/math/math.c ++++ b/core/shared/platform/common/math/math.c +@@ -419,40 +419,8 @@ + #else + static double + #endif +-bp[] = {1.0, 1.5,}, +-dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */ +-dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */ +-two = 2.0, +-two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */ + two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ +-twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ +- /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ +-L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */ +-L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */ +-L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */ +-L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */ +-L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */ +-L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */ +-P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ +-P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ +-P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ +-P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ +-P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */ +-lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */ +-lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */ +-lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */ +-ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */ +-cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */ +-cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */ +-cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/ +-ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */ +-ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ +-ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ +- +-static double +-freebsd_floor(double x); +-static double +-freebsd_ceil(double x); ++twom54 = 5.55111512312578270212e-17; /* 0x3C900000, 0x00000000 */ + static double + freebsd_fabs(double x); + static double +@@ -618,311 +586,6 @@ + default: /* case 3 */ + return (z - pi_lo) - pi; /* atan(-,-) */ + } +-} +- +-#ifndef BH_HAS_SQRTF +-static float +-freebsd_sqrtf(float x) +-{ +- float z; +- int32_t sign = (int)0x80000000; +- int32_t ix, s, q, m, t, i; +- u_int32_t r; +- +- GET_FLOAT_WORD(ix, x); +- +- /* take care of Inf and NaN */ +- if ((ix & 0x7f800000) == 0x7f800000) { +- return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf +- sqrt(-inf)=sNaN */ +- } +- /* take care of zero */ +- if (ix <= 0) { +- if ((ix & (~sign)) == 0) +- return x; /* sqrt(+-0) = +-0 */ +- else if (ix < 0) +- return (x - x) / (x - x); /* sqrt(-ve) = sNaN */ +- } +- /* normalize x */ +- m = (ix >> 23); +- if (m == 0) { /* subnormal x */ +- for (i = 0; (ix & 0x00800000) == 0; i++) +- ix <<= 1; +- m -= i - 1; +- } +- m -= 127; /* unbias exponent */ +- ix = (ix & 0x007fffff) | 0x00800000; +- if (m & 1) /* odd m, double x to make it even */ +- ix += ix; +- m >>= 1; /* m = [m/2] */ +- +- /* generate sqrt(x) bit by bit */ +- ix += ix; +- q = s = 0; /* q = sqrt(x) */ +- r = 0x01000000; /* r = moving bit from right to left */ +- +- while (r != 0) { +- t = s + r; +- if (t <= ix) { +- s = t + r; +- ix -= t; +- q += r; +- } +- ix += ix; +- r >>= 1; +- } +- +- /* use floating add to find out rounding direction */ +- if (ix != 0) { +- z = one - tiny; /* trigger inexact flag */ +- if (z >= one) { +- z = one + tiny; +- if (z > one) +- q += 2; +- else +- q += (q & 1); +- } +- } +- ix = (q >> 1) + 0x3f000000; +- ix += (m << 23); +- SET_FLOAT_WORD(z, ix); +- return z; +-} +-#endif /* end of BH_HAS_SQRTF */ +- +-#ifndef BH_HAS_SQRT +-static double +-freebsd_sqrt(double x) /* wrapper sqrt */ +-{ +- double z; +- int32_t sign = (int)0x80000000; +- int32_t ix0, s0, q, m, t, i; +- u_int32_t r, t1, s1, ix1, q1; +- +- EXTRACT_WORDS(ix0, ix1, x); +- +- /* take care of Inf and NaN */ +- if ((ix0 & 0x7ff00000) == 0x7ff00000) { +- return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf +- sqrt(-inf)=sNaN */ +- } +- /* take care of zero */ +- if (ix0 <= 0) { +- if (((ix0 & (~sign)) | ix1) == 0) +- return x; /* sqrt(+-0) = +-0 */ +- else if (ix0 < 0) +- return (x - x) / (x - x); /* sqrt(-ve) = sNaN */ +- } +- /* normalize x */ +- m = (ix0 >> 20); +- if (m == 0) { /* subnormal x */ +- while (ix0 == 0) { +- m -= 21; +- ix0 |= (ix1 >> 11); +- ix1 <<= 21; +- } +- for (i = 0; (ix0 & 0x00100000) == 0; i++) +- ix0 <<= 1; +- m -= i - 1; +- ix0 |= (ix1 >> (32 - i)); +- ix1 <<= i; +- } +- m -= 1023; /* unbias exponent */ +- ix0 = (ix0 & 0x000fffff) | 0x00100000; +- if (m & 1) { /* odd m, double x to make it even */ +- ix0 += ix0 + ((ix1 & sign) >> 31); +- ix1 += ix1; +- } +- m >>= 1; /* m = [m/2] */ +- +- /* generate sqrt(x) bit by bit */ +- ix0 += ix0 + ((ix1 & sign) >> 31); +- ix1 += ix1; +- q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ +- r = 0x00200000; /* r = moving bit from right to left */ +- +- while (r != 0) { +- t = s0 + r; +- if (t <= ix0) { +- s0 = t + r; +- ix0 -= t; +- q += r; +- } +- ix0 += ix0 + ((ix1 & sign) >> 31); +- ix1 += ix1; +- r >>= 1; +- } +- +- r = sign; +- while (r != 0) { +- t1 = s1 + r; +- t = s0; +- if ((t < ix0) || ((t == ix0) && (t1 <= ix1))) { +- s1 = t1 + r; +- if (((t1 & sign) == sign) && (s1 & sign) == 0) +- s0 += 1; +- ix0 -= t; +- if (ix1 < t1) +- ix0 -= 1; +- ix1 -= t1; +- q1 += r; +- } +- ix0 += ix0 + ((ix1 & sign) >> 31); +- ix1 += ix1; +- r >>= 1; +- } +- +- /* use floating add to find out rounding direction */ +- if ((ix0 | ix1) != 0) { +- z = one - tiny; /* trigger inexact flag */ +- if (z >= one) { +- z = one + tiny; +- if (q1 == (u_int32_t)0xffffffff) { +- q1 = 0; +- q += 1; +- } +- else if (z > one) { +- if (q1 == (u_int32_t)0xfffffffe) +- q += 1; +- q1 += 2; +- } +- else +- q1 += (q1 & 1); +- } +- } +- ix0 = (q >> 1) + 0x3fe00000; +- ix1 = q1 >> 1; +- if ((q & 1) == 1) +- ix1 |= sign; +- ix0 += (m << 20); +- +- INSERT_WORDS(z, ix0, ix1); +- +- return z; +-} +-#endif /* end of BH_HAS_SQRT */ +- +-static double +-freebsd_floor(double x) +-{ +- int32_t i0, i1, j0; +- u_int32_t i, j; +- +- EXTRACT_WORDS(i0, i1, x); +- +- j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; +- if (j0 < 20) { +- if (j0 < 0) { /* raise inexact if x != 0 */ +- if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */ +- if (i0 >= 0) { +- i0 = i1 = 0; +- } +- else if (((i0 & 0x7fffffff) | i1) != 0) { +- i0 = 0xbff00000; +- i1 = 0; +- } +- } +- } +- else { +- i = (0x000fffff) >> j0; +- if (((i0 & i) | i1) == 0) +- return x; /* x is integral */ +- if (huge + x > 0.0) { /* raise inexact flag */ +- if (i0 < 0) +- i0 += (0x00100000) >> j0; +- i0 &= (~i); +- i1 = 0; +- } +- } +- } +- else if (j0 > 51) { +- if (j0 == 0x400) +- return x + x; /* inf or NaN */ +- else +- return x; /* x is integral */ +- } +- else { +- i = ((u_int32_t)(0xffffffff)) >> (j0 - 20); +- if ((i1 & i) == 0) +- return x; /* x is integral */ +- if (huge + x > 0.0) { /* raise inexact flag */ +- if (i0 < 0) { +- if (j0 == 20) +- i0 += 1; +- else { +- j = i1 + (1 << (52 - j0)); +- if (j < i1) +- i0 += 1; /* got a carry */ +- i1 = j; +- } +- } +- i1 &= (~i); +- } +- } +- +- INSERT_WORDS(x, i0, i1); +- +- return x; +-} +- +-static double +-freebsd_ceil(double x) +-{ +- int32_t i0, i1, j0; +- u_int32_t i, j; +- EXTRACT_WORDS(i0, i1, x); +- j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; +- if (j0 < 20) { +- if (j0 < 0) { /* raise inexact if x != 0 */ +- if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */ +- if (i0 < 0) { +- i0 = 0x80000000; +- i1 = 0; +- } +- else if ((i0 | i1) != 0) { +- i0 = 0x3ff00000; +- i1 = 0; +- } +- } +- } +- else { +- i = (0x000fffff) >> j0; +- if (((i0 & i) | i1) == 0) +- return x; /* x is integral */ +- if (huge + x > 0.0) { /* raise inexact flag */ +- if (i0 > 0) +- i0 += (0x00100000) >> j0; +- i0 &= (~i); +- i1 = 0; +- } +- } +- } +- else if (j0 > 51) { +- if (j0 == 0x400) +- return x + x; /* inf or NaN */ +- else +- return x; /* x is integral */ +- } +- else { +- i = ((u_int32_t)(0xffffffff)) >> (j0 - 20); +- if ((i1 & i) == 0) +- return x; /* x is integral */ +- if (huge + x > 0.0) { /* raise inexact flag */ +- if (i0 > 0) { +- if (j0 == 20) +- i0 += 1; +- else { +- j = i1 + (1 << (52 - j0)); +- if (j < i1) +- i0 += 1; /* got a carry */ +- i1 = j; +- } +- } +- i1 &= (~i); +- } +- } +- INSERT_WORDS(x, i0, i1); +- return x; + } + + static double +@@ -1005,15 +668,6 @@ + } + } + +-static float +-freebsd_fabsf(float x) +-{ +- u_int32_t ix; +- GET_FLOAT_WORD(ix, x); +- SET_FLOAT_WORD(x, ix & 0x7fffffff); +- return x; +-} +- + static double + freebsd_fabs(double x) + { +@@ -1023,42 +677,10 @@ + return x; + } + +-static const float huge_f = 1.0e30F; +- + static const float TWO23[2] = { + 8.3886080000e+06, /* 0x4b000000 */ + -8.3886080000e+06, /* 0xcb000000 */ + }; +- +-static float +-freebsd_truncf(float x) +-{ +- int32_t i0, j0; +- u_int32_t i; +- GET_FLOAT_WORD(i0, x); +- j0 = ((i0 >> 23) & 0xff) - 0x7f; +- if (j0 < 23) { +- if (j0 < 0) { /* raise inexact if x != 0 */ +- if (huge_f + x > 0.0F) /* |x|<1, so return 0*sign(x) */ +- i0 &= 0x80000000; +- } +- else { +- i = (0x007fffff) >> j0; +- if ((i0 & i) == 0) +- return x; /* x is integral */ +- if (huge_f + x > 0.0F) /* raise inexact flag */ +- i0 &= (~i); +- } +- } +- else { +- if (j0 == 0x80) +- return x + x; /* inf or NaN */ +- else +- return x; /* x is integral */ +- } +- SET_FLOAT_WORD(x, i0); +- return x; +-} + + static float + freebsd_rintf(float x) +@@ -1088,85 +710,6 @@ + } + + static float +-freebsd_ceilf(float x) +-{ +- int32_t i0, j0; +- u_int32_t i; +- +- GET_FLOAT_WORD(i0, x); +- j0 = ((i0 >> 23) & 0xff) - 0x7f; +- if (j0 < 23) { +- if (j0 < 0) { /* raise inexact if x != 0 */ +- if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */ +- if (i0 < 0) { +- i0 = 0x80000000; +- } +- else if (i0 != 0) { +- i0 = 0x3f800000; +- } +- } +- } +- else { +- i = (0x007fffff) >> j0; +- if ((i0 & i) == 0) +- return x; /* x is integral */ +- if (huge_f + x > (float)0.0) { /* raise inexact flag */ +- if (i0 > 0) +- i0 += (0x00800000) >> j0; +- i0 &= (~i); +- } +- } +- } +- else { +- if (j0 == 0x80) +- return x + x; /* inf or NaN */ +- else +- return x; /* x is integral */ +- } +- SET_FLOAT_WORD(x, i0); +- return x; +-} +- +-static float +-freebsd_floorf(float x) +-{ +- int32_t i0, j0; +- u_int32_t i; +- GET_FLOAT_WORD(i0, x); +- j0 = ((i0 >> 23) & 0xff) - 0x7f; +- if (j0 < 23) { +- if (j0 < 0) { /* raise inexact if x != 0 */ +- if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */ +- if (i0 >= 0) { +- i0 = 0; +- } +- else if ((i0 & 0x7fffffff) != 0) { +- i0 = 0xbf800000; +- } +- } +- } +- else { +- i = (0x007fffff) >> j0; +- if ((i0 & i) == 0) +- return x; /* x is integral */ +- if (huge_f + x > (float)0.0) { /* raise inexact flag */ +- if (i0 < 0) +- i0 += (0x00800000) >> j0; +- i0 &= (~i); +- } +- } +- } +- else { +- if (j0 == 0x80) +- return x + x; /* inf or NaN */ +- else +- return x; /* x is integral */ +- } +- SET_FLOAT_WORD(x, i0); +- return x; +-} +- +-static float + freebsd_fminf(float x, float y) + { + if (is_little_endian()) { +@@ -1290,260 +833,6 @@ + return x * twom54; + } + +-static double +-freebsd_pow(double x, double y) +-{ +- double z, ax, z_h, z_l, p_h, p_l; +- double y1, t1, t2, r, s, t, u, v, w; +- int32_t i, j, k, yisint, n; +- int32_t hx, hy, ix, iy; +- u_int32_t lx, ly; +- +- EXTRACT_WORDS(hx, lx, x); +- EXTRACT_WORDS(hy, ly, y); +- ix = hx & 0x7fffffff; +- iy = hy & 0x7fffffff; +- +- /* y==zero: x**0 = 1 */ +- if ((iy | ly) == 0) +- return one; +- +- /* x==1: 1**y = 1, even if y is NaN */ +- if (hx == 0x3ff00000 && lx == 0) +- return one; +- +- /* y!=zero: result is NaN if either arg is NaN */ +- if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000 +- || ((iy == 0x7ff00000) && (ly != 0))) +- return (x + 0.0) + (y + 0.0); +- +- /* determine if y is an odd int when x < 0 +- * yisint = 0 ... y is not an integer +- * yisint = 1 ... y is an odd int +- * yisint = 2 ... y is an even int +- */ +- yisint = 0; +- if (hx < 0) { +- if (iy >= 0x43400000) +- yisint = 2; /* even integer y */ +- else if (iy >= 0x3ff00000) { +- k = (iy >> 20) - 0x3ff; /* exponent */ +- if (k > 20) { +- j = ly >> (52 - k); +- if ((j << (52 - k)) == ly) +- yisint = 2 - (j & 1); +- } +- else if (ly == 0) { +- j = iy >> (20 - k); +- if ((j << (20 - k)) == iy) +- yisint = 2 - (j & 1); +- } +- } +- } +- +- /* special value of y */ +- if (ly == 0) { +- if (iy == 0x7ff00000) { /* y is +-inf */ +- if (((ix - 0x3ff00000) | lx) == 0) +- return one; /* (-1)**+-inf is NaN */ +- else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */ +- return (hy >= 0) ? y : zero; +- else /* (|x|<1)**-,+inf = inf,0 */ +- return (hy < 0) ? -y : zero; +- } +- if (iy == 0x3ff00000) { /* y is +-1 */ +- if (hy < 0) +- return one / x; +- else +- return x; +- } +- if (hy == 0x40000000) +- return x * x; /* y is 2 */ +- if (hy == 0x40080000) +- return x * x * x; /* y is 3 */ +- if (hy == 0x40100000) { /* y is 4 */ +- u = x * x; +- return u * u; +- } +- if (hy == 0x3fe00000) { /* y is 0.5 */ +- if (hx >= 0) /* x >= +0 */ +- return sqrt(x); +- } +- } +- +- ax = fabs(x); +- /* special value of x */ +- if (lx == 0) { +- if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) { +- z = ax; /*x is +-0,+-inf,+-1*/ +- if (hy < 0) +- z = one / z; /* z = (1/|x|) */ +- if (hx < 0) { +- if (((ix - 0x3ff00000) | yisint) == 0) { +- z = (z - z) / (z - z); /* (-1)**non-int is NaN */ +- } +- else if (yisint == 1) +- z = -z; /* (x<0)**odd = -(|x|**odd) */ +- } +- return z; +- } +- } +- +- /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be +- n = (hx>>31)+1; +- but ANSI C says a right shift of a signed negative quantity is +- implementation defined. */ +- n = ((u_int32_t)hx >> 31) - 1; +- +- /* (x<0)**(non-int) is NaN */ +- if ((n | yisint) == 0) +- return (x - x) / (x - x); +- +- s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ +- if ((n | (yisint - 1)) == 0) +- s = -one; /* (-ve)**(odd int) */ +- +- /* |y| is huge */ +- if (iy > 0x41e00000) { /* if |y| > 2**31 */ +- if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */ +- if (ix <= 0x3fefffff) +- return (hy < 0) ? huge * huge : tiny * tiny; +- if (ix >= 0x3ff00000) +- return (hy > 0) ? huge * huge : tiny * tiny; +- } +- /* over/underflow if x is not close to one */ +- if (ix < 0x3fefffff) +- return (hy < 0) ? s * huge * huge : s * tiny * tiny; +- if (ix > 0x3ff00000) +- return (hy > 0) ? s * huge * huge : s * tiny * tiny; +- /* now |1-x| is tiny <= 2**-20, suffice to compute +- log(x) by x-x^2/2+x^3/3-x^4/4 */ +- t = ax - one; /* t has 20 trailing zeros */ +- w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); +- u = ivln2_h * t; /* ivln2_h has 21 sig. bits */ +- v = t * ivln2_l - w * ivln2; +- t1 = u + v; +- SET_LOW_WORD(t1, 0); +- t2 = v - (t1 - u); +- } +- else { +- double ss, s2, s_h, s_l, t_h, t_l; +- n = 0; +- /* take care subnormal number */ +- if (ix < 0x00100000) { +- ax *= two53; +- n -= 53; +- GET_HIGH_WORD(ix, ax); +- } +- n += ((ix) >> 20) - 0x3ff; +- j = ix & 0x000fffff; +- /* determine interval */ +- ix = j | 0x3ff00000; /* normalize ix */ +- if (j <= 0x3988E) +- k = 0; /* |x|> 1) | 0x20000000) + 0x00080000 + (k << 18)); +- t_l = ax - (t_h - bp[k]); +- s_l = v * ((u - s_h * t_h) - s_h * t_l); +- /* compute log(ax) */ +- s2 = ss * ss; +- r = s2 * s2 +- * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6))))); +- r += s_l * (s_h + ss); +- s2 = s_h * s_h; +- t_h = 3.0 + s2 + r; +- SET_LOW_WORD(t_h, 0); +- t_l = r - ((t_h - 3.0) - s2); +- /* u+v = ss*(1+...) */ +- u = s_h * t_h; +- v = s_l * t_h + t_l * ss; +- /* 2/(3log2)*(ss+...) */ +- p_h = u + v; +- SET_LOW_WORD(p_h, 0); +- p_l = v - (p_h - u); +- z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */ +- z_l = cp_l * p_h + p_l * cp + dp_l[k]; +- /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ +- t = (double)n; +- t1 = (((z_h + z_l) + dp_h[k]) + t); +- SET_LOW_WORD(t1, 0); +- t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); +- } +- +- /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ +- y1 = y; +- SET_LOW_WORD(y1, 0); +- p_l = (y - y1) * t1 + y * t2; +- p_h = y1 * t1; +- z = p_l + p_h; +- EXTRACT_WORDS(j, i, z); +- if (j >= 0x40900000) { /* z >= 1024 */ +- if (((j - 0x40900000) | i) != 0) /* if z > 1024 */ +- return s * huge * huge; /* overflow */ +- else { +- if (p_l + ovt > z - p_h) +- return s * huge * huge; /* overflow */ +- } +- } +- else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */ +- if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */ +- return s * tiny * tiny; /* underflow */ +- else { +- if (p_l <= z - p_h) +- return s * tiny * tiny; /* underflow */ +- } +- } +- /* +- * compute 2**(p_h+p_l) +- */ +- i = j & 0x7fffffff; +- k = (i >> 20) - 0x3ff; +- n = 0; +- if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ +- n = j + (0x00100000 >> (k + 1)); +- k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */ +- t = zero; +- SET_HIGH_WORD(t, n & ~(0x000fffff >> k)); +- n = ((n & 0x000fffff) | 0x00100000) >> (20 - k); +- if (j < 0) +- n = -n; +- p_h -= t; +- } +- t = p_l + p_h; +- SET_LOW_WORD(t, 0); +- u = t * lg2_h; +- v = (p_l - (t - p_h)) * lg2 + t * lg2_l; +- z = u + v; +- w = v - (z - u); +- t = z * z; +- t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); +- r = (z * t1) / (t1 - two) - (w + z * w); +- z = one - (r - z); +- GET_HIGH_WORD(j, z); +- j += (n << 20); +- if ((j >> 20) <= 0) +- z = freebsd_scalbn(z, n); /* subnormal output */ +- else +- SET_HIGH_WORD(z, j); +- return s * z; +-} +- + double + atan(double x) + { +@@ -1556,26 +845,6 @@ + return freebsd_atan2(y, x); + } + +-#ifndef BH_HAS_SQRT +-double +-sqrt(double x) +-{ +- return freebsd_sqrt(x); +-} +-#endif +- +-double +-floor(double x) +-{ +- return freebsd_floor(x); +-} +- +-double +-ceil(double x) +-{ +- return freebsd_ceil(x); +-} +- + double + fmin(double x, double y) + { +@@ -1594,24 +863,12 @@ + return freebsd_rint(x); + } + +-double +-fabs(double x) +-{ +- return freebsd_fabs(x); +-} +- + int + isnan(double x) + { + return freebsd_isnan(x); + } + +-double +-trunc(double x) +-{ +- return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x); +-} +- + int + signbit(double x) + { +@@ -1619,33 +876,9 @@ + } + + float +-fabsf(float x) +-{ +- return freebsd_fabsf(x); +-} +- +-float +-truncf(float x) +-{ +- return freebsd_truncf(x); +-} +- +-float + rintf(float x) + { + return freebsd_rintf(x); +-} +- +-float +-ceilf(float x) +-{ +- return freebsd_ceilf(x); +-} +- +-float +-floorf(float x) +-{ +- return freebsd_floorf(x); + } + + float +@@ -1660,22 +893,17 @@ + return freebsd_fmaxf(x, y); + } + +-#ifndef BH_HAS_SQRTF +-float +-sqrtf(float x) +-{ +- return freebsd_sqrtf(x); +-} +-#endif +- +-double +-pow(double x, double y) +-{ +- return freebsd_pow(x, y); +-} +- + double + scalbn(double x, int n) + { + return freebsd_scalbn(x, n); + } ++ ++double nearbyint(double x) ++{ ++ return round(x); ++} ++float nearbyintf(float x) ++{ ++ return round(x); ++} + diff --git a/wamr/patches/2.4.2/platform_internal.patch b/wamr/patches/2.4.2/platform_internal.patch new file mode 100644 index 0000000..70df341 --- /dev/null +++ b/wamr/patches/2.4.2/platform_internal.patch @@ -0,0 +1,111 @@ +--- a/core/shared/platform/linux/platform_internal.h ++++ b/core/shared/platform/linux/platform_internal.h +@@ -18,7 +18,7 @@ + #include + #include + #include +-#include ++// #include + #include + #include + #include +@@ -31,20 +31,74 @@ + #include + #include + #include +-#include ++// #include + #include + #include + #include + #include ++#include // Header file from libphoenix ++#include // Header file from libphoenix + + #ifdef __cplusplus + extern "C" { + #endif + +-#ifndef BH_PLATFORM_LINUX +-#define BH_PLATFORM_LINUX ++#ifndef BH_PLATFORM_PHOENIX ++#define BH_PLATFORM_PHOENIX + #endif + ++/* Config definitions */ ++#define DISABLE_CLOCK_NANOSLEEP ++ ++/* Missing definitions */ ++double nearbyint(double x); ++float nearbyintf(float x); ++#define MAX_PATH_LEN PATH_MAX ++ ++/* Definitions from lwip */ ++#define IP_MULTICAST_LOOP 7 ++#define IP_ADD_MEMBERSHIP 3 ++#define IP_DROP_MEMBERSHIP 4 ++ ++typedef struct ip_mreq { ++ struct in_addr imr_multiaddr; /* IP multicast address of group */ ++ struct in_addr imr_interface; /* local IP address of interface */ ++} ip_mreq; ++ ++/* Additional math includes */ ++ ++double atan(double x); ++double atan2(double y, double x); ++double sqrt(double x); ++double floor(double x); ++double ceil(double x); ++double fmin(double x, double y); ++double fmax(double x, double y); ++double rint(double x); ++double fabs(double x); ++double trunc(double x); ++float sqrtf(float x); ++float floorf(float x); ++float ceilf(float x); ++float fminf(float x, float y); ++float fmaxf(float x, float y); ++float rintf(float x); ++float fabsf(float x); ++float truncf(float x); ++int isnan(double x); ++double pow(double x, double y); ++double scalbn(double x, int n); ++ ++#ifndef BH_HAS_SIGNBIT ++int signbit(double x); ++#endif ++ ++unsigned long long int strtoull(const char *nptr, char **endptr, int base); ++double strtod(const char *nptr, char **endptr); ++float strtof(const char *nptr, char **endptr); ++ ++#define BH_HAS_SQRT ++ + /* Stack size of applet threads's native part. */ + #define BH_APPLET_PRESERVED_STACK_SIZE (32 * 1024) + +@@ -55,12 +109,19 @@ + typedef pthread_mutex_t korp_mutex; + typedef pthread_cond_t korp_cond; + typedef pthread_t korp_thread; +-typedef pthread_rwlock_t korp_rwlock; +-typedef sem_t korp_sem; ++typedef struct { ++ char writing; ++ int readers; ++ korp_mutex mx; ++ korp_cond cond; ++} korp_rwlock; ++typedef semaphore_t korp_sem; + + #define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER + ++#ifdef TARGET_HAS_TLS + #define os_thread_local_attribute __thread ++#endif + + #define bh_socket_t int + diff --git a/wamr/patches/2.4.2/posix_clock.patch b/wamr/patches/2.4.2/posix_clock.patch new file mode 100644 index 0000000..a04df2b --- /dev/null +++ b/wamr/patches/2.4.2/posix_clock.patch @@ -0,0 +1,35 @@ +--- a/core/shared/platform/common/posix/posix_clock.c ++++ b/core/shared/platform/common/posix/posix_clock.c +@@ -40,8 +40,6 @@ + static __wasi_timestamp_t + timespec_to_nanoseconds(const struct timespec *ts) + { +- if (ts->tv_sec < 0) +- return 0; + if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / NANOSECONDS_PER_SECOND) + return UINT64_MAX; + return (__wasi_timestamp_t)ts->tv_sec * NANOSECONDS_PER_SECOND +@@ -51,19 +49,9 @@ + __wasi_errno_t + os_clock_res_get(__wasi_clockid_t clock_id, __wasi_timestamp_t *resolution) + { +- clockid_t nclock_id; +- __wasi_errno_t error = wasi_clockid_to_clockid(clock_id, &nclock_id); +- +- if (error != __WASI_ESUCCESS) +- return error; +- +- struct timespec ts; +- if (clock_getres(nclock_id, &ts) < 0) +- return convert_errno(errno); +- +- *resolution = timespec_to_nanoseconds(&ts); +- +- return error; ++ /* Phoenix has microsecond precision */ ++ *resolution = 1000; ++ return convert_errno(EOK); + } + + __wasi_errno_t + diff --git a/wamr/patches/2.4.2/posix_file.patch b/wamr/patches/2.4.2/posix_file.patch new file mode 100644 index 0000000..8f91dc7 --- /dev/null +++ b/wamr/patches/2.4.2/posix_file.patch @@ -0,0 +1,508 @@ +--- a/core/shared/platform/common/posix/posix_file.c 2025-09-15 11:11:18.000000000 +0200 ++++ b/core/shared/platform/common/posix/posix_file.c 2025-09-30 10:40:35.862576656 +0200 +@@ -7,7 +7,7 @@ + #include "libc_errno.h" + #include + +-#if !defined(__APPLE__) && !defined(ESP_PLATFORM) ++#if !defined(__APPLE__) && !defined(ESP_PLATFORM) && !defined(phoenix) // Phoenix has no preadv/pwritev + #define CONFIG_HAS_PWRITEV 1 + #define CONFIG_HAS_PREADV 1 + #else +@@ -15,7 +15,7 @@ + #define CONFIG_HAS_PREADV 0 + #endif + +-#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(ESP_PLATFORM) ++#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(ESP_PLATFORM) && !defined(phoenix) // Phoenix has no fdatasync + #define CONFIG_HAS_FDATASYNC 1 + #else + #define CONFIG_HAS_FDATASYNC 0 +@@ -34,7 +34,7 @@ + #endif + #endif + +-#if !defined(__APPLE__) && !defined(ESP_PLATFORM) && !defined(__COSMOPOLITAN__) ++#if !defined(__APPLE__) && !defined(ESP_PLATFORM) && !defined(__COSMOPOLITAN__) && !defined(phoenix) // Phoenix has no posix_fallocate + #define CONFIG_HAS_POSIX_FALLOCATE 1 + #else + #define CONFIG_HAS_POSIX_FALLOCATE 0 +@@ -66,12 +66,105 @@ + #define STDERR_FILENO 2 + #endif + ++#define ATIM 3 ++#define MTIM 12 ++ ++ ++size_t dirpaths_length = 0; ++char** dirpaths = NULL; ++ ++ ++void __attribute__((destructor)) clear_paths() ++{ ++ free(dirpaths); ++} ++ ++void remove_path(int fd) ++{ ++ if(fd < (int)dirpaths_length) { ++ free(dirpaths[fd]); ++ dirpaths[fd] = NULL; ++ } ++} ++ ++char* get_path(int fd) ++{ ++ if(fd >= (int)dirpaths_length) { ++ return NULL; ++ } ++ return dirpaths[fd]; ++} ++ ++int set_path(int fd, const char* path) ++{ ++ int previous_length = dirpaths_length; ++ char* cwd; ++ if(fd >= (int)dirpaths_length) { ++ dirpaths = realloc(dirpaths, fd * 2 * sizeof(char*)); ++ if(!dirpaths) ++ return -ENOMEM; ++ memset(dirpaths + previous_length, 0, fd * 2 - previous_length); ++ dirpaths_length = fd * 2; ++ } ++ if(path[0] == '/') ++ dirpaths[fd] = strdup(path); ++ else { ++ cwd = getcwd(NULL, 0); ++ if(asprintf(&dirpaths[fd], "%s%s%s", cwd, cwd[strlen(cwd) - 1] != '/' ? "/" : "", path) <= 0) ++ { ++ free(cwd); ++ return -errno; ++ } ++ free(cwd); ++ } ++ return 0; ++} ++ ++char* resolve_fd_path_ex(int fd, const char* relative_path, int resolve_last_symlink, int allow_missing_leaf) ++{ ++ char *fd_path; ++ char* buffer; ++ int fd_len, rel_len = strlen(relative_path); ++ buffer = malloc(MAX_PATH_LEN); ++ if(!buffer) ++ return NULL; ++ if(relative_path[0] == '/') ++ memcpy(buffer, relative_path, rel_len); ++ else { ++ fd_path = get_path(fd); ++ if(!fd_path) { ++ return NULL; ++ } ++ fd_len = strlen(fd_path); ++ if(fd_len + rel_len > MAX_PATH_LEN) { ++ errno = ENAMETOOLONG; ++ return NULL; ++ } ++ memcpy(buffer, fd_path, fd_len); ++ if(buffer[fd_len - 1] != '/') ++ buffer[fd_len++] = '/'; ++ memcpy(buffer + fd_len, relative_path, rel_len); ++ } ++ resolve_path(buffer, buffer, resolve_last_symlink, allow_missing_leaf); ++ return buffer; ++} ++ ++char* resolve_fd_path(int fd, const char* relative_path, int resolve_last_symlink) ++{ ++ char *res = resolve_fd_path_ex(fd, relative_path, resolve_last_symlink, 0); ++ return res; ++} ++ ++char* resolve_nonexisting_fd_path(int fd, const char* relative_path) ++{ ++ char *res = resolve_fd_path_ex(fd, relative_path, 0, 1); ++ return res; ++} ++ + // Converts a POSIX timespec to a WASI timestamp. + static __wasi_timestamp_t + convert_timespec(const struct timespec *ts) + { +- if (ts->tv_sec < 0) +- return 0; + if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / 1000000000) + return UINT64_MAX; + return (__wasi_timestamp_t)ts->tv_sec * 1000000000 +@@ -145,45 +238,43 @@ + } + + static void +-convert_timestamp(__wasi_timestamp_t in, struct timespec *out) +-{ +- // Store sub-second remainder. +-#if defined(__SYSCALL_SLONG_TYPE) +- out->tv_nsec = (__SYSCALL_SLONG_TYPE)(in % 1000000000); +-#else +- out->tv_nsec = (long)(in % 1000000000); +-#endif +- in /= 1000000000; +- +- // Clamp to the maximum in case it would overflow our system's time_t. +- out->tv_sec = (time_t)in < BH_TIME_T_MAX ? (time_t)in : BH_TIME_T_MAX; +-} +- +-// Converts the provided timestamps and flags to a set of arguments for +-// futimens() and utimensat(). +-static void +-convert_utimens_arguments(__wasi_timestamp_t st_atim, ++convert_utimes_arguments(__wasi_timestamp_t st_atim, + __wasi_timestamp_t st_mtim, +- __wasi_fstflags_t fstflags, struct timespec *ts) ++ __wasi_fstflags_t fstflags, ++ struct stat *st, struct timeval *tv) + { ++ uint64_t now; ++ if ((fstflags & __WASI_FILESTAT_SET_ATIM_NOW) || (fstflags & __WASI_FILESTAT_SET_MTIM_NOW)) { ++ os_clock_time_get(__WASI_CLOCK_REALTIME, 0, &now); ++ now /= 1000; ++ } ++ /* Missing UTIME_NOW and UTIME_OMIT flags in phoenix */ + if ((fstflags & __WASI_FILESTAT_SET_ATIM_NOW) != 0) { +- ts[0].tv_nsec = UTIME_NOW; ++ tv[0].tv_sec = now / 1000000; ++ tv[0].tv_usec = now % 1000000; + } + else if ((fstflags & __WASI_FILESTAT_SET_ATIM) != 0) { +- convert_timestamp(st_atim, &ts[0]); ++ st_atim /= 1000; ++ tv[0].tv_sec = st_atim / 1000000; ++ tv[0].tv_usec = st_atim % 1000000; + } + else { +- ts[0].tv_nsec = UTIME_OMIT; ++ tv[0].tv_sec = st->st_atim.tv_sec; ++ tv[0].tv_usec = st->st_atim.tv_nsec / 1000; + } + + if ((fstflags & __WASI_FILESTAT_SET_MTIM_NOW) != 0) { +- ts[1].tv_nsec = UTIME_NOW; ++ tv[1].tv_sec = now / 1000000; ++ tv[1].tv_usec = now % 1000000; + } + else if ((fstflags & __WASI_FILESTAT_SET_MTIM) != 0) { +- convert_timestamp(st_mtim, &ts[1]); ++ st_mtim /= 1000; ++ tv[0].tv_sec = st_mtim / 1000000; ++ tv[0].tv_usec = st_mtim % 1000000; + } + else { +- ts[1].tv_nsec = UTIME_OMIT; ++ tv[1].tv_sec = st->st_mtim.tv_sec; ++ tv[1].tv_usec = st->st_mtim.tv_nsec / 1000; + } + } + +@@ -205,11 +296,12 @@ + os_fstatat(os_file_handle handle, const char *path, + struct __wasi_filestat_t *buf, __wasi_lookupflags_t lookup_flags) + { ++ char* resolved_path = resolve_fd_path(handle, path, lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW); + struct stat stat_buf; +- int ret = fstatat(handle, path, &stat_buf, +- (lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) +- ? AT_SYMLINK_FOLLOW +- : AT_SYMLINK_NOFOLLOW); ++ ++ int ret = lstat(resolved_path, &stat_buf); ++ ++ free(resolved_path); + + if (ret < 0) + return convert_errno(errno); +@@ -314,14 +406,23 @@ + __wasi_errno_t + os_open_preopendir(const char *path, os_file_handle *out) + { ++ struct stat buf; ++ ++ stat(path, &buf); ++ if(!S_ISDIR(buf.st_mode)) { ++ errno = ENOTDIR; ++ return convert_errno(errno); ++ } + +- int fd = open(path, O_RDONLY | O_DIRECTORY, 0); ++ int fd = open(path, O_RDONLY, 0); + + if (fd < 0) + return convert_errno(errno); + + *out = fd; + ++ set_path(fd, path); ++ + return __WASI_ESUCCESS; + } + +@@ -330,14 +431,14 @@ + __wasi_fdflags_t fs_flags, __wasi_lookupflags_t lookup_flags, + wasi_libc_file_access_mode read_write_mode, os_file_handle *out) + { ++ struct stat statbuf; ++ char* resolved_path; + int open_flags = 0; + + // Convert open flags. + if ((oflags & __WASI_O_CREAT) != 0) { + open_flags |= O_CREAT; + } +- if ((oflags & __WASI_O_DIRECTORY) != 0) +- open_flags |= O_DIRECTORY; + if ((oflags & __WASI_O_EXCL) != 0) + open_flags |= O_EXCL; + if ((oflags & __WASI_O_TRUNC) != 0) { +@@ -371,8 +472,17 @@ + #endif + } + +- if ((lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) == 0) { +- open_flags |= O_NOFOLLOW; ++ resolved_path = resolve_fd_path(handle, path, 0); ++ lstat(resolved_path, &statbuf); ++ if((lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) && S_ISLNK(statbuf.st_mode)) ++ { ++ errno = ELOOP; ++ return convert_errno(errno); ++ } ++ ++ if ((oflags & __WASI_O_DIRECTORY) != 0 && !S_ISDIR(statbuf.st_mode)) { ++ errno = ENOTDIR; ++ return convert_errno(errno); + } + + switch (read_write_mode) { +@@ -389,40 +499,19 @@ + return __WASI_EINVAL; + } + +- int fd = openat(handle, path, open_flags, 0666); ++ int fd = open(resolved_path, open_flags, 0666); + + if (fd < 0) { + int openat_errno = errno; +- // Linux returns ENXIO instead of EOPNOTSUPP when opening a socket. +- if (openat_errno == ENXIO) { +- struct stat sb; +- int ret = fstatat(handle, path, &sb, +- (lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) +- ? 0 +- : AT_SYMLINK_NOFOLLOW); +- return ret == 0 && S_ISSOCK(sb.st_mode) ? __WASI_ENOTSUP +- : __WASI_ENXIO; +- } +- // Linux returns ENOTDIR instead of ELOOP when using +- // O_NOFOLLOW|O_DIRECTORY on a symlink. +- if (openat_errno == ENOTDIR +- && (open_flags & (O_NOFOLLOW | O_DIRECTORY)) != 0) { +- struct stat sb; +- int ret = fstatat(handle, path, &sb, AT_SYMLINK_NOFOLLOW); +- if (S_ISLNK(sb.st_mode)) { +- return __WASI_ELOOP; +- } +- (void)ret; +- } +- // FreeBSD returns EMLINK instead of ELOOP when using O_NOFOLLOW on +- // a symlink. +- if ((lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) == 0 +- && openat_errno == EMLINK) +- return __WASI_ELOOP; + ++ free(resolved_path); + return convert_errno(openat_errno); + } + ++ if(S_ISDIR(statbuf.st_mode)) ++ set_path(fd, resolved_path); ++ free(resolved_path); ++ + *out = fd; + + return __WASI_ESUCCESS; +@@ -465,6 +554,8 @@ + if (ret < 0) + return convert_errno(errno); + ++ remove_path(handle); ++ + return __WASI_ESUCCESS; + } + +@@ -638,10 +729,16 @@ + os_futimens(os_file_handle handle, __wasi_timestamp_t access_time, + __wasi_timestamp_t modification_time, __wasi_fstflags_t fstflags) + { +- struct timespec ts[2]; +- convert_utimens_arguments(access_time, modification_time, fstflags, ts); ++ struct timeval tv[2]; ++ struct stat st; ++ ++ if (!(fstflags & ATIM) || !(fstflags & MTIM)) { ++ fstat(handle, &st); ++ } + +- int ret = futimens(handle, ts); ++ convert_utimes_arguments(access_time, modification_time, fstflags, &st, tv); ++ ++ int ret = futimes(handle, tv); + + if (ret < 0) + return convert_errno(errno); +@@ -655,18 +752,24 @@ + __wasi_timestamp_t modification_time, __wasi_fstflags_t fstflags, + __wasi_lookupflags_t lookup_flags) + { +- struct timespec ts[2]; +- convert_utimens_arguments(access_time, modification_time, fstflags, ts); ++ char* resolved_path; ++ if(lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) ++ resolved_path = resolve_fd_path(handle, path, 1); ++ else ++ resolved_path = resolve_fd_path(handle, path, 0); ++ struct timeval tv[2]; ++ struct stat st; ++ ++ if (!(fstflags & ATIM) || !(fstflags & MTIM)) { ++ fstat(handle, &st); ++ } + +- int ret = utimensat(handle, path, ts, +- (lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) +- ? 0 +- : AT_SYMLINK_NOFOLLOW); ++ convert_utimes_arguments(access_time, modification_time, fstflags, &st, tv); + +- if (ret < 0) +- return convert_errno(errno); ++ __wasi_errno_t ret = utimes(resolved_path, tv); + +- return __WASI_ESUCCESS; ++ free(resolved_path); ++ return ret; + } + + __wasi_errno_t +@@ -675,9 +778,15 @@ + { + // Linux requires that the buffer size is positive. whereas POSIX does + // not. Use a fake buffer to store the results if the size is zero. ++ ++ char* resolved_path = resolve_fd_path(handle, path, 0); ++ + char fakebuf[1]; +- ssize_t len = readlinkat(handle, path, bufsize == 0 ? fakebuf : buf, +- bufsize == 0 ? sizeof(fakebuf) : bufsize); ++ ++ ssize_t len = readlink(resolved_path, bufsize == 0 ? fakebuf : buf, ++ bufsize == 0 ? sizeof(fakebuf) : bufsize); ++ ++ free(resolved_path); + + if (len < 0) + return convert_errno(errno); +@@ -692,9 +801,13 @@ + os_file_handle to_handle, const char *to_path, + __wasi_lookupflags_t lookup_flags) + { +- int ret = linkat( +- from_handle, from_path, to_handle, to_path, +- (lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) ? AT_SYMLINK_FOLLOW : 0); ++ char* resolved_from = resolve_fd_path(from_handle, from_path, (lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW)); ++ char* resolved_to = resolve_nonexisting_fd_path(to_handle, to_path); ++ ++ int ret = link(resolved_from, resolved_to); ++ ++ free(resolved_from); ++ free(resolved_to); + + if (ret < 0) + return convert_errno(errno); +@@ -705,7 +818,11 @@ + __wasi_errno_t + os_symlinkat(const char *old_path, os_file_handle handle, const char *new_path) + { +- int ret = symlinkat(old_path, handle, new_path); ++ char* resolved_path = resolve_fd_path(handle, new_path, 0); ++ ++ int ret = symlink(old_path, resolved_path); ++ ++ free(resolved_path); + + if (ret < 0) + return convert_errno(errno); +@@ -716,7 +833,11 @@ + __wasi_errno_t + os_mkdirat(os_file_handle handle, const char *path) + { +- int ret = mkdirat(handle, path, 0777); ++ char* resolved_path = resolve_fd_path(handle, path, 0); ++ ++ int ret = mkdir(resolved_path, 0777); ++ ++ free(resolved_path); + + if (ret < 0) + return convert_errno(errno); +@@ -729,7 +850,13 @@ + os_file_handle new_handle, const char *new_path) + { + +- int ret = renameat(old_handle, old_path, new_handle, new_path); ++ char* resolved_old = resolve_fd_path(old_handle, old_path, 0); ++ char* resolved_new = resolve_nonexisting_fd_path(new_handle, new_path); ++ ++ int ret = rename(resolved_old, resolved_new); ++ ++ free(resolved_old); ++ free(resolved_new); + + if (ret < 0) + return convert_errno(errno); +@@ -740,33 +867,11 @@ + __wasi_errno_t + os_unlinkat(os_file_handle handle, const char *path, bool is_dir) + { +- int ret = unlinkat(handle, path, is_dir ? AT_REMOVEDIR : 0); ++ char* resolved_path = resolve_fd_path(handle, path, 0); + +-#ifndef __linux__ +- if (ret < 0) { +- // Non-Linux implementations may return EPERM when attempting to remove +- // a directory without REMOVEDIR. While that's what POSIX specifies, +- // it's less useful. Adjust this to EISDIR. It doesn't matter that this +- // is not atomic with the unlinkat, because if the file is removed and a +- // directory is created before fstatat sees it, we're racing with that +- // change anyway and unlinkat could have legitimately seen the directory +- // if the race had turned out differently. +- if (errno == EPERM) { +- struct stat statbuf; +- if (fstatat(handle, path, &statbuf, AT_SYMLINK_NOFOLLOW) == 0 +- && S_ISDIR(statbuf.st_mode)) { +- errno = EISDIR; +- } +- } +- // POSIX permits either EEXIST or ENOTEMPTY when the directory is not +- // empty. Map it to ENOTEMPTY. +- else if (errno == EEXIST) { +- errno = ENOTEMPTY; +- } ++ int ret = is_dir ? rmdir(resolved_path) : unlink(resolved_path); + +- return convert_errno(errno); +- } +-#endif ++ free(resolved_path); + + if (ret < 0) + return convert_errno(errno); diff --git a/wamr/patches/2.4.2/posix_thread.patch b/wamr/patches/2.4.2/posix_thread.patch new file mode 100644 index 0000000..70150db --- /dev/null +++ b/wamr/patches/2.4.2/posix_thread.patch @@ -0,0 +1,320 @@ +--- a/core/shared/platform/common/posix/posix_thread.c 2025-09-15 11:11:18.000000000 +0200 ++++ b/core/shared/platform/common/posix/posix_thread.c 2025-09-30 10:39:24.418864763 +0200 +@@ -28,6 +28,13 @@ + static os_thread_local_attribute os_signal_handler signal_handler; + #endif + ++#define SEM_MAP_LEN 128 ++typedef struct sem_entry { ++ korp_sem sem; ++ char* name; ++ struct sem_entry* next; ++} sem_entry_t; ++ + static void * + os_thread_wrapper(void *arg) + { +@@ -52,8 +59,6 @@ + #if BH_DEBUG != 0 + #if defined __APPLE__ + pthread_setname_np("wamr"); +-#else +- pthread_setname_np(pthread_self(), "wamr"); + #endif + #endif + start_func(thread_arg); +@@ -212,56 +217,142 @@ + return BHT_OK; + } + ++static sem_entry_t *sem_hash_map[SEM_MAP_LEN] = { NULL }; ++static korp_mutex hash_map_guard; ++ ++static void __attribute__((constructor)) initialize_guard() ++{ ++ os_mutex_init(&hash_map_guard); ++} ++ ++static void __attribute__((destructor)) close_guard() ++{ ++ os_mutex_destroy(&hash_map_guard); ++} ++ ++static unsigned long sem_djb2_hash(const char* str) ++{ ++ unsigned long hash = 5381; ++ int c; ++ while ((c = *str++)) { ++ hash = ((hash << 5) + hash) + c; ++ } ++ return hash % SEM_MAP_LEN; ++} ++ + korp_sem * + os_sem_open(const char *name, int oflags, int mode, int val) + { +- return sem_open(name, oflags, mode, val); ++ (void)oflags; ++ (void)mode; ++ int hash = sem_djb2_hash(name); ++ os_mutex_lock(&hash_map_guard); ++ sem_entry_t *entry = sem_hash_map[hash]; ++ while (NULL != entry && strcmp(entry->name, name) != 0) ++ { ++ entry = entry->next; ++ } ++ if (entry) { ++ os_mutex_unlock(&hash_map_guard); ++ return &(entry->sem); ++ } ++ entry = (sem_entry_t*)malloc(sizeof(sem_entry_t)); ++ if(!entry) { ++ os_mutex_unlock(&hash_map_guard); ++ return NULL; ++ } ++ entry->name = strdup(name); ++ if(!(entry->name)) ++ { ++ free(entry); ++ os_mutex_unlock(&hash_map_guard); ++ return NULL; ++ } ++ if(semaphoreCreate(&entry->sem, val) != EOK) ++ { ++ os_mutex_unlock(&hash_map_guard); ++ free(entry); ++ return NULL; ++ } ++ entry->next = sem_hash_map[hash]; ++ sem_hash_map[hash] = entry; ++ os_mutex_unlock(&hash_map_guard); ++ return &entry->sem; + } + + int + os_sem_close(korp_sem *sem) + { +- return sem_close(sem); ++ return BHT_OK; + } + + int + os_sem_wait(korp_sem *sem) + { +- return sem_wait(sem); ++ assert(sem); ++ return semaphoreDown(sem, 0); + } + + int + os_sem_trywait(korp_sem *sem) + { +- return sem_trywait(sem); ++ assert(sem); ++ int val; ++ mutexLock(sem->mutex); ++ val = sem->v; ++ if(val > 0) ++ --(sem->v); ++ mutexUnlock(sem->mutex); ++ if(val > 0) ++ return 0; ++ errno = EAGAIN; ++ return -1; + } + + int + os_sem_post(korp_sem *sem) + { +- return sem_post(sem); ++ assert(sem); ++ return semaphoreUp(sem); + } + + int + os_sem_getvalue(korp_sem *sem, int *sval) + { +-#if defined(__APPLE__) +- /* +- * macOS doesn't have working sem_getvalue. +- * It's marked as deprecated in the system header. +- * Mock it up here to avoid compile-time deprecation warnings. +- */ +- errno = ENOSYS; +- return -1; +-#else +- return sem_getvalue(sem, sval); +-#endif ++ assert(sem); ++ int ret; ++ mutexLock(sem->mutex); ++ ret = sem->v; ++ mutexUnlock(sem->mutex); ++ return ret; + } + + int + os_sem_unlink(const char *name) + { +- return sem_unlink(name); ++ int hash = sem_djb2_hash(name); ++ sem_entry_t **entry = &(sem_hash_map[hash]); ++ sem_entry_t *tmp; ++ ++ os_mutex_lock(&hash_map_guard); ++ ++ while (*entry != NULL && strcmp((*entry)->name, name) != 0) ++ { ++ entry = &((*entry)->next); ++ } ++ if(entry == NULL) ++ { ++ os_mutex_unlock(&hash_map_guard); ++ errno = ENOENT; ++ return BHT_ERROR; ++ } ++ tmp = *entry; ++ *entry = (*entry)->next; ++ free(tmp->name); ++ os_mutex_unlock(&hash_map_guard); ++ semaphoreDone(&tmp->sem); ++ free(tmp); ++ return BHT_OK; + } + + static void +@@ -304,6 +395,8 @@ + int + os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds) + { ++ assert(cond); ++ assert(mutex); + int ret; + struct timespec abstime; + +@@ -342,14 +435,17 @@ + return BHT_OK; + } + ++/* Phoenix has no rwlock implementation, using custom */ ++ + int + os_rwlock_init(korp_rwlock *lock) + { + assert(lock); +- +- if (pthread_rwlock_init(lock, NULL) != BHT_OK) ++ lock->readers = 0; ++ lock->writing = 0; ++ if (os_mutex_init(&lock->mx) || os_cond_init(&lock->cond)) { + return BHT_ERROR; +- ++ } + return BHT_OK; + } + +@@ -357,43 +453,65 @@ + os_rwlock_rdlock(korp_rwlock *lock) + { + assert(lock); +- +- if (pthread_rwlock_rdlock(lock) != BHT_OK) ++ if (os_mutex_lock(&lock->mx)) { + return BHT_ERROR; +- +- return BHT_OK; ++ } ++ while (lock->writing) { ++ if (os_cond_wait(&lock->cond, &lock->mx)) { ++ return BHT_ERROR; ++ } ++ } ++ lock->readers++; ++ return os_mutex_unlock(&lock->mx); + } + + int + os_rwlock_wrlock(korp_rwlock *lock) + { + assert(lock); +- +- if (pthread_rwlock_wrlock(lock) != BHT_OK) ++ if (os_mutex_lock(&lock->mx)) { + return BHT_ERROR; +- +- return BHT_OK; ++ } ++ while (lock->writing || lock->readers > 0) { ++ if (os_cond_wait(&lock->cond, &lock->mx)) { ++ return BHT_ERROR; ++ } ++ } ++ lock->writing = 1; ++ return os_mutex_unlock(&lock->mx); + } + + int + os_rwlock_unlock(korp_rwlock *lock) + { + assert(lock); +- +- if (pthread_rwlock_unlock(lock) != BHT_OK) ++ if (os_mutex_lock(&lock->mx)) { + return BHT_ERROR; +- +- return BHT_OK; ++ } ++ if (lock->writing) { ++ lock->writing = 0; ++ if (os_cond_broadcast(&lock->cond)) { ++ return BHT_ERROR; ++ } ++ } ++ else { ++ lock->readers--; ++ if (lock->readers == 0) { ++ if (os_cond_broadcast(&lock->cond)) { ++ return BHT_ERROR; ++ } ++ } ++ } ++ return os_mutex_unlock(&lock->mx); + } + + int + os_rwlock_destroy(korp_rwlock *lock) + { + assert(lock); +- +- if (pthread_rwlock_destroy(lock) != BHT_OK) ++ if (os_mutex_destroy(&lock->mx) || os_cond_destroy(&lock->cond)) { + return BHT_ERROR; +- ++ } + return BHT_OK; + } + +@@ -425,13 +543,12 @@ + uint8 * + os_thread_get_stack_boundary() + { +- pthread_t self; + #ifdef __linux__ + pthread_attr_t attr; + size_t guard_size; + #endif + uint8 *addr = NULL; +- size_t stack_size, max_stack_size; ++ size_t max_stack_size; + int page_size; + + #if defined(os_thread_local_attribute) +@@ -440,7 +557,7 @@ + #endif + + page_size = getpagesize(); +- self = pthread_self(); ++ // self = pthread_self(); + max_stack_size = + (size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1) & ~(page_size - 1); + diff --git a/wamr/patches/2.4.2/posix_time.patch b/wamr/patches/2.4.2/posix_time.patch new file mode 100644 index 0000000..29a3ac2 --- /dev/null +++ b/wamr/patches/2.4.2/posix_time.patch @@ -0,0 +1,17 @@ +--- a/core/shared/platform/common/posix/posix_time.c ++++ b/core/shared/platform/common/posix/posix_time.c +@@ -19,10 +19,6 @@ + uint64 + os_time_thread_cputime_us() + { +- struct timespec ts; +- if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) { +- return 0; +- } +- +- return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000; +-} +\ No newline at end of file ++ /* Phoenix does not support CLOCK_THREAD_CPUTIME_ID flag */ ++ return 0; ++} diff --git a/wamr/patches/2.4.2/shared_platform.patch b/wamr/patches/2.4.2/shared_platform.patch new file mode 100644 index 0000000..c30ce4e --- /dev/null +++ b/wamr/patches/2.4.2/shared_platform.patch @@ -0,0 +1,32 @@ +--- a/core/shared/platform/linux/shared_platform.cmake ++++ b/core/shared/platform/linux/shared_platform.cmake +@@ -1,18 +1,26 @@ + # Copyright (C) 2019 Intel Corporation. All rights reserved. + # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + ++# Static compilation ++set(BUILD_SHARED_LIBS OFF) ++set(CMAKE_EXE_LINKER_FLAGS "-static") ++ + set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR}) + +-add_definitions(-DBH_PLATFORM_LINUX) ++add_definitions(-DBH_PLATFORM_PHOENIX) ++add_definitions (-DWASM_HAVE_MREMAP=0) # Missing mremap + + include_directories(${PLATFORM_SHARED_DIR}) + include_directories(${PLATFORM_SHARED_DIR}/../include) + + include (${CMAKE_CURRENT_LIST_DIR}/../common/posix/platform_api_posix.cmake) ++include (${CMAKE_CURRENT_LIST_DIR}/../common/math/platform_api_math.cmake) # Included math api with missing functions from math.h ++include (${CMAKE_CURRENT_LIST_DIR}/../common/memory/platform_api_memory.cmake) # Included memory api which allows us not using mremap + + file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c) + +-set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_POSIX_SOURCE}) ++set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_POSIX_SOURCE} ${PLATFORM_COMMON_MATH_SOURCE} ${PLATFORM_COMMON_MEMORY_SOURCE}) # Added math and memory source ++ + + file (GLOB header ${PLATFORM_SHARED_DIR}/../include/*.h) + LIST (APPEND RUNTIME_LIB_HEADER_LIST ${header}) + diff --git a/wamr/patches/2.4.2/wasm_memory.patch b/wamr/patches/2.4.2/wasm_memory.patch new file mode 100644 index 0000000..955a7ac --- /dev/null +++ b/wamr/patches/2.4.2/wasm_memory.patch @@ -0,0 +1,34 @@ +--- a/core/iwasm/common/wasm_memory.c ++++ b/core/iwasm/common/wasm_memory.c +@@ -1501,8 +1501,9 @@ + new_mem = os_mremap(mapped_mem, old_size, new_size); + } + else { +- new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE, +- os_get_invalid_handle()); ++ /* mprotect fix */ ++ new_mem = os_mmap(NULL, new_size, MMAP_PROT_READ | MMAP_PROT_WRITE, MMAP_MAP_NONE, ++ os_get_invalid_handle()); + } + if (!new_mem) { + return NULL; +@@ -1512,13 +1513,15 @@ + if (commit_size > 0 + && !os_mem_commit(new_mem, commit_size, + MMAP_PROT_READ | MMAP_PROT_WRITE)) { +- os_munmap(new_mem, new_size); ++ os_munmap(new_mem, new_size); + return NULL; + } + #endif + +- if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE) +- != 0) { ++ /* mprotect fix */ ++ ++ if (os_mprotect(new_mem, new_size, MMAP_PROT_NONE) != 0 || ++ os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) { + wasm_munmap_linear_memory(new_mem, new_size, new_size); + return NULL; + } + diff --git a/wamr/platform_files/native_libs/native_libs.c b/wamr/platform_files/native_libs/native_libs.c new file mode 100644 index 0000000..07adf89 --- /dev/null +++ b/wamr/platform_files/native_libs/native_libs.c @@ -0,0 +1,5 @@ +#include "wasm_export.h" + +NativeSymbol native_symbols[] = {}; + +int n_native_symbols = 0; \ No newline at end of file