From 63012f0222dc0372959a540e67bc28020dc79549 Mon Sep 17 00:00:00 2001 From: Maks Litskevich Date: Sat, 13 Jan 2024 00:35:49 +0000 Subject: [PATCH 01/53] Fix build errors when initializing wasm_val_t values with macros (#3007) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Errors were reported when initializing wasm_val_t values with WASM_I32_VAL like macros. ``` error: missing initializer for member ‘wasm_val_t::__paddings’ [-Werror=missing-field-initializers] 64 | wasm_val_t res = {WASM_INIT_VAL}; ``` And rename DEPRECATED to WASM_API_DEPRECATED to avoid using defines with generic names. --- core/iwasm/include/wasm_c_api.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core/iwasm/include/wasm_c_api.h b/core/iwasm/include/wasm_c_api.h index 1d49376e0..304b3a4ee 100644 --- a/core/iwasm/include/wasm_c_api.h +++ b/core/iwasm/include/wasm_c_api.h @@ -22,12 +22,12 @@ #endif #if defined(__GNUC__) || defined(__clang__) -#define DEPRECATED __attribute__((deprecated)) +#define WASM_API_DEPRECATED __attribute__((deprecated)) #elif defined(_MSC_VER) -#define DEPRECATED __declspec(deprecated) +#define WASM_API_DEPRECATED __declspec(deprecated) #else #pragma message("WARNING: You need to implement DEPRECATED for this compiler") -#define DEPRECATED +#define WASM_API_DEPRECATED #endif #ifdef __cplusplus @@ -231,7 +231,7 @@ WASM_DECLARE_OWN(engine) */ WASM_API_EXTERN own wasm_engine_t* wasm_engine_new(void); WASM_API_EXTERN own wasm_engine_t* wasm_engine_new_with_config(wasm_config_t*); -DEPRECATED WASM_API_EXTERN own wasm_engine_t * +WASM_API_DEPRECATED WASM_API_EXTERN own wasm_engine_t * wasm_engine_new_with_args(mem_alloc_type_t type, const MemAllocOption *opts); // Store @@ -828,12 +828,12 @@ static inline void* wasm_val_ptr(const wasm_val_t* val) { #endif } -#define WASM_I32_VAL(i) {.kind = WASM_I32, .of = {.i32 = i}} -#define WASM_I64_VAL(i) {.kind = WASM_I64, .of = {.i64 = i}} -#define WASM_F32_VAL(z) {.kind = WASM_F32, .of = {.f32 = z}} -#define WASM_F64_VAL(z) {.kind = WASM_F64, .of = {.f64 = z}} -#define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .of = {.ref = r}} -#define WASM_INIT_VAL {.kind = WASM_ANYREF, .of = {.ref = NULL}} +#define WASM_I32_VAL(i) {.kind = WASM_I32, .__paddings = {0}, .of = {.i32 = i}} +#define WASM_I64_VAL(i) {.kind = WASM_I64, .__paddings = {0}, .of = {.i64 = i}} +#define WASM_F32_VAL(z) {.kind = WASM_F32, .__paddings = {0}, .of = {.f32 = z}} +#define WASM_F64_VAL(z) {.kind = WASM_F64, .__paddings = {0}, .of = {.f64 = z}} +#define WASM_REF_VAL(r) {.kind = WASM_ANYREF, .__paddings = {0}, .of = {.ref = r}} +#define WASM_INIT_VAL {.kind = WASM_ANYREF, .__paddings = {0}, .of = {.ref = NULL}} #define KILOBYTE(n) ((n) * 1024) From 23d2e0627cefa0732b21ff6722e750125bb6a2aa Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 13 Jan 2024 08:56:39 +0800 Subject: [PATCH 02/53] Clear compilation warning and dead code (#3002) --- core/iwasm/compilation/aot_emit_aot_file.c | 6 ++++++ core/iwasm/interpreter/wasm_loader.c | 9 --------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index 66304339e..9b5fa47ed 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -25,6 +25,7 @@ } \ } while (0) +#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 static bool check_utf8_str(const uint8 *str, uint32 len) { @@ -89,6 +90,7 @@ check_utf8_str(const uint8 *str, uint32 len) } return (p == p_end); } +#endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */ /* Internal function in object file */ typedef struct AOTObjectFunc { @@ -1111,6 +1113,7 @@ static union { EMIT_BUF(s, str_len); \ } while (0) +#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 static bool read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign, uint64 *p_result) @@ -1309,6 +1312,7 @@ get_name_section_size(AOTCompData *comp_data) fail: return 0; } +#endif /* end of WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 */ static uint32 get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data) @@ -2065,6 +2069,7 @@ aot_emit_native_symbol(uint8 *buf, uint8 *buf_end, uint32 *p_offset, return true; } +#if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 static bool aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, AOTCompData *comp_data, AOTCompContext *comp_ctx) @@ -2090,6 +2095,7 @@ aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, LOG_DEBUG("emit name section"); return true; } +#endif static bool aot_emit_custom_sections(uint8 *buf, uint8 *buf_end, uint32 *p_offset, diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 1fc9805bd..f02f23d83 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -4046,15 +4046,6 @@ check_wasi_abi_compatibility(const WASMModule *module, * according to the assumption, they should be all wasi compatiable */ - /* always can not have both at the same time */ - if (start && initialize) { - set_error_buf( - error_buf, error_buf_size, - "neither a command nor a reactor can both have _start function " - "and _initialize function at the same time"); - return false; - } - #if WASM_ENABLE_MULTI_MODULE != 0 /* filter out commands (with `_start`) cases */ if (start && !main_module) { From 837b9904f580d73a5cf1016d35c37525b7246f11 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 15 Jan 2024 16:02:12 +0900 Subject: [PATCH 03/53] aot debug: Try to use a bit more appropriate file names (#3000) When the original wasm contains multiple compilation units, the current logic uses the first one for everything. This commit tries to use a bit more appropriate ones. --- .../compilation/debug/dwarf_extractor.cpp | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/core/iwasm/compilation/debug/dwarf_extractor.cpp b/core/iwasm/compilation/debug/dwarf_extractor.cpp index ee32c2f75..d322aefe5 100644 --- a/core/iwasm/compilation/debug/dwarf_extractor.cpp +++ b/core/iwasm/compilation/debug/dwarf_extractor.cpp @@ -133,9 +133,10 @@ dwarf_gen_file_info(const AOTCompContext *comp_ctx) file_name = filespec.GetFilename(); dir_name = filespec.GetDirectory(); if (file_name || dir_name) { - file_info = LLVMDIBuilderCreateFile(comp_ctx->debug_builder, - file_name, strlen(file_name), - dir_name, strlen(dir_name)); + file_info = LLVMDIBuilderCreateFile( + comp_ctx->debug_builder, file_name, + file_name ? strlen(file_name) : 0, dir_name, + dir_name ? strlen(dir_name) : 0); } } return file_info; @@ -298,7 +299,7 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx, return NULL; LLVMDIBuilderRef DIB = comp_ctx->debug_builder; - LLVMMetadataRef File = comp_ctx->debug_file; + LLVMMetadataRef File = comp_ctx->debug_file; /* a fallback */ LLVMMetadataRef ParamTypes[num_function_args + 1]; @@ -315,6 +316,21 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx, } } + auto compile_unit = sc.GetCompileUnit(); + auto file_spec = compile_unit.GetFileSpec(); + const char *file_name = file_spec.GetFilename(); + const char *dir_name = file_spec.GetDirectory(); + LLVMMetadataRef file_info = NULL; + if (file_name || dir_name) { + file_info = + LLVMDIBuilderCreateFile(comp_ctx->debug_builder, file_name, + file_name ? strlen(file_name) : 0, dir_name, + dir_name ? strlen(dir_name) : 0); + } + if (file_info) { + File = file_info; + } + LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType( DIB, File, ParamTypes, num_function_args + 1, LLVMDIFlagZero); From 892a94fd05f2e95fc68535d763bf67ab1b8e6263 Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Mon, 15 Jan 2024 11:18:37 +0100 Subject: [PATCH 04/53] fix(wasm-c-api): Do not clone stack frames if there's no trap (#3008) When running the wasi-threads no_pthread sample, the assert was failing on `src->num_elems != 0` in debug mode, it is because that the exception is `proc_exit`, there is no trap (the execution didn't fail, no stack frames): https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/samples/wasi-threads/wasm-apps/no_pthread.c --- core/iwasm/common/wasm_c_api.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 27b1e0405..bdc5315eb 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -1924,14 +1924,16 @@ wasm_frame_func_offset(const wasm_frame_t *frame) void wasm_frame_vec_clone_internal(Vector *src, Vector *out) { - bh_assert(src->num_elems != 0 && src->data); - - bh_vector_destroy(out); - if (!bh_vector_init(out, src->num_elems, sizeof(WASMCApiFrame), false)) { + if (src->num_elems == 0) { bh_vector_destroy(out); return; } + if (!bh_vector_destroy(out) + || !bh_vector_init(out, src->num_elems, sizeof(WASMCApiFrame), false)) { + return; + } + bh_memcpy_s(out->data, src->num_elems * sizeof(WASMCApiFrame), src->data, src->num_elems * sizeof(WASMCApiFrame)); out->num_elems = src->num_elems; From 54bc9f2053dc8f105147b46c199a1cf655a89b3b Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Mon, 15 Jan 2024 12:05:23 +0100 Subject: [PATCH 05/53] Increase default app thread stack size (#3010) --- core/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/config.h b/core/config.h index e067ea271..440272676 100644 --- a/core/config.h +++ b/core/config.h @@ -397,7 +397,7 @@ #define APP_THREAD_STACK_SIZE_DEFAULT (64 * 1024) #define APP_THREAD_STACK_SIZE_MIN (48 * 1024) #else -#define APP_THREAD_STACK_SIZE_DEFAULT (64 * 1024) +#define APP_THREAD_STACK_SIZE_DEFAULT (128 * 1024) #define APP_THREAD_STACK_SIZE_MIN (24 * 1024) #endif #endif /* end of !(defined(APP_THREAD_STACK_SIZE_DEFAULT) \ From 884c1abc11df644a39210fb330060a020e605679 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 16 Jan 2024 08:44:35 +0800 Subject: [PATCH 06/53] Fix nightly-run CI failure (#3014) Not know how to make `__attribute__((no_sanitize_address))` take effect on gcc in Ubuntu-20.04, just disable quick AOT entry for wasm-c-api sample in nightly-run CI. --- .github/workflows/nightly_run.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/nightly_run.yml b/.github/workflows/nightly_run.yml index 1bd0c4856..69cfb6f1b 100644 --- a/.github/workflows/nightly_run.yml +++ b/.github/workflows/nightly_run.yml @@ -383,7 +383,9 @@ jobs: - name: Build Sample [wasm-c-api] run: | VERBOSE=1 - cmake -S . -B build ${{ matrix.make_options }} -D WAMR_BUILD_SANITIZER="${{matrix.sanitizer}}" + cmake -S . -B build ${{ matrix.make_options }} \ + -D WAMR_BUILD_SANITIZER="${{matrix.sanitizer}}" \ + -D WAMR_BUILD_QUICK_AOT_ENTRY=0 cmake --build build --config Release --parallel 4 ctest --test-dir build --output-on-failure working-directory: samples/wasm-c-api From 915adc433db184f59df5553393c420978ac86d60 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Tue, 16 Jan 2024 08:53:18 +0800 Subject: [PATCH 07/53] Rename rwlock_init to avoid conflict (#3016) This symbol is too simple to cause conflict with others, such as the RW spinlock in Linux kernel and NuttX, so rename it to avoid the conflict. --- .../libc-wasi/sandboxed-system-primitives/src/locking.h | 2 +- .../libc-wasi/sandboxed-system-primitives/src/posix.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h index 5e0778c3f..0ad40ecfd 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/locking.h @@ -88,7 +88,7 @@ struct LOCKABLE rwlock { }; static inline bool -rwlock_init(struct rwlock *lock) REQUIRES_UNLOCKED(*lock) +rwlock_initialize(struct rwlock *lock) REQUIRES_UNLOCKED(*lock) { return os_rwlock_init(&lock->object) == 0 ? true : false; } diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index 9819c92fd..d24d49393 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -204,7 +204,7 @@ struct fd_prestat { bool fd_prestats_init(struct fd_prestats *pt) { - if (!rwlock_init(&pt->lock)) + if (!rwlock_initialize(&pt->lock)) return false; pt->prestats = NULL; pt->size = 0; @@ -340,7 +340,7 @@ struct fd_entry { bool fd_table_init(struct fd_table *ft) { - if (!rwlock_init(&ft->lock)) + if (!rwlock_initialize(&ft->lock)) return false; ft->entries = NULL; ft->size = 0; From 685d55d2e749ee56bdb69984a151799d8c7e960e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 16 Jan 2024 10:17:58 +0900 Subject: [PATCH 08/53] nuttx: Use larger alignment for os_mmap and comment why (#3017) Other platforms with malloc-based os_mmap might need similar changes too, depending on their target cpu arch. --- core/shared/platform/nuttx/nuttx_platform.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/shared/platform/nuttx/nuttx_platform.c b/core/shared/platform/nuttx/nuttx_platform.c index 38e70076d..d105924ae 100644 --- a/core/shared/platform/nuttx/nuttx_platform.c +++ b/core/shared/platform/nuttx/nuttx_platform.c @@ -87,6 +87,7 @@ os_dumps_proc_mem_info(char *out, unsigned int size) void * os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) { + void *p; #if (WASM_MEM_DUAL_BUS_MIRROR != 0) void *i_addr, *d_addr; #endif @@ -110,7 +111,21 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) return in_ibus_ext(i_addr) ? i_addr : d_addr; } #endif - return malloc((uint32)size); + /* Note: aot_loader.c assumes that os_mmap provides large enough + * alignment for any data sections. Some sections like rodata.cst32 + * actually require alignment larger than the natural alignment + * provided by malloc. + * + * Probably it's cleaner to add an explicit alignment argument to + * os_mmap. However, it only makes sense if we change our aot format + * to keep the necessary alignment. + * + * For now, let's assume 32 byte alignment is enough. + */ + if (posix_memalign(&p, 32, size)) { + return NULL; + } + return p; } void From c9fd44e8b8d61a71c6b8714f34cd1a9b2b97b75c Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Tue, 16 Jan 2024 02:36:05 +0100 Subject: [PATCH 09/53] Build samples in debug mode (#3019) Follow-up on #3008. Compiling samples in Debug mode allows us to take advantage of asserts and would have prevented the fix in #3008. --- .../workflows/compilation_on_android_ubuntu.yml | 14 +++++++------- .github/workflows/compilation_on_macos.yml | 12 ++++++------ .github/workflows/compilation_on_sgx.yml | 2 +- samples/basic/build.sh | 2 +- samples/shared-module/build.sh | 2 +- samples/simple/build.sh | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/workflows/compilation_on_android_ubuntu.yml b/.github/workflows/compilation_on_android_ubuntu.yml index 98d346f9e..8c27465d3 100644 --- a/.github/workflows/compilation_on_android_ubuntu.yml +++ b/.github/workflows/compilation_on_android_ubuntu.yml @@ -322,7 +322,7 @@ jobs: run: | VERBOSE=1 cmake -S . -B build ${{ matrix.make_options }} - cmake --build build --config Release --parallel 4 + cmake --build build --config Debug --parallel 4 ctest --test-dir build --output-on-failure working-directory: samples/wasm-c-api @@ -393,7 +393,7 @@ jobs: cd samples/file mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./src/iwasm -f wasm-app/file.wasm -d . - name: Build Sample [multi-thread] @@ -401,7 +401,7 @@ jobs: cd samples/multi-thread mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./iwasm wasm-apps/test.wasm - name: Build Sample [multi-module] @@ -409,7 +409,7 @@ jobs: cd samples/multi-module mkdir build && cd build cmake .. -DWAMR_BUILD_AOT=1 - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./multi_module mC.wasm ./multi_module mC.aot @@ -418,7 +418,7 @@ jobs: cd samples/spawn-thread mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./spawn_thread - name: Build Sample [ref-types] @@ -426,7 +426,7 @@ jobs: cd samples/ref-types mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./hello - name: Build Sample [simple] @@ -441,7 +441,7 @@ jobs: cd samples/wasi-threads mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./iwasm wasm-apps/no_pthread.wasm - name: Build Sample [shared-module] diff --git a/.github/workflows/compilation_on_macos.yml b/.github/workflows/compilation_on_macos.yml index 12f1d73cb..92da67861 100644 --- a/.github/workflows/compilation_on_macos.yml +++ b/.github/workflows/compilation_on_macos.yml @@ -285,7 +285,7 @@ jobs: cd samples/file mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./src/iwasm -f wasm-app/file.wasm -d . - name: Build Sample [multi-thread] @@ -293,7 +293,7 @@ jobs: cd samples/multi-thread mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./iwasm wasm-apps/test.wasm - name: Build Sample [multi-module] @@ -301,7 +301,7 @@ jobs: cd samples/multi-module mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./multi_module mC.wasm - name: Build Sample [spawn-thread] @@ -309,7 +309,7 @@ jobs: cd samples/spawn-thread mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./spawn_thread - name: Build Sample [ref-types] @@ -317,7 +317,7 @@ jobs: cd samples/ref-types mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./hello - name: Build Sample [wasi-threads] @@ -325,7 +325,7 @@ jobs: cd samples/wasi-threads mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 ./iwasm wasm-apps/no_pthread.wasm - name: Build Sample [shared-module] diff --git a/.github/workflows/compilation_on_sgx.yml b/.github/workflows/compilation_on_sgx.yml index 0d19d83ad..2cb37bdc6 100644 --- a/.github/workflows/compilation_on_sgx.yml +++ b/.github/workflows/compilation_on_sgx.yml @@ -246,7 +246,7 @@ jobs: cd samples/file mkdir build && cd build cmake .. - cmake --build . --config Release --parallel 4 + cmake --build . --config Debug --parallel 4 cp wasm-app/file.wasm `pwd`/../../../product-mini/platforms/${{ matrix.platform }}/enclave-sample - name: Test Sample [file] in non-aot mode diff --git a/samples/basic/build.sh b/samples/basic/build.sh index a0be7e1c7..bc75ac783 100755 --- a/samples/basic/build.sh +++ b/samples/basic/build.sh @@ -21,7 +21,7 @@ echo "#####################build basic project" cd ${CURR_DIR} mkdir -p cmake_build cd cmake_build -cmake .. +cmake .. -DCMAKE_BUILD_TYPE=Debug make -j ${nproc} if [ $? != 0 ];then echo "BUILD_FAIL basic exit as $?\n" diff --git a/samples/shared-module/build.sh b/samples/shared-module/build.sh index 9af5b3edd..e6c104745 100755 --- a/samples/shared-module/build.sh +++ b/samples/shared-module/build.sh @@ -21,7 +21,7 @@ echo "##################### build shared-module project" cd ${CURR_DIR} mkdir -p cmake_build cd cmake_build -cmake .. +cmake .. -DCMAKE_BUILD_TYPE=Debug make -j ${nproc} if [ $? != 0 ];then echo "BUILD_FAIL shared-module exit as $?\n" diff --git a/samples/simple/build.sh b/samples/simple/build.sh index 4e8155156..9d9d1874d 100755 --- a/samples/simple/build.sh +++ b/samples/simple/build.sh @@ -17,7 +17,7 @@ APP_LIB_SRC="${APP_FRAMEWORK_DIR}/base/app/*.c ${APP_FRAMEWORK_DIR}/sensor/app/* ${APP_FRAMEWORK_DIR}/connection/app/*.c ${NATIVE_LIBS}/*.c" WASM_APPS=${PWD}/wasm-apps CLEAN= -CM_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Release" +CM_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Debug" CM_TOOLCHAIN="" usage () From 705bf807b2e7cb9c07b4ada9347a976995aa45b5 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 16 Jan 2024 10:15:56 +0800 Subject: [PATCH 10/53] Remove deprecated tests in language-bindings python (#3018) After PR #2922, the caller of wasm-c-api wasm_func_call should ensure the params and results are correctly created, runtime doesn't check their format again. --- .../python/wasm-c-api/tests/test_advanced.py | 32 ------------------- 1 file changed, 32 deletions(-) diff --git a/language-bindings/python/wasm-c-api/tests/test_advanced.py b/language-bindings/python/wasm-c-api/tests/test_advanced.py index 2e1c285ea..706447ab5 100644 --- a/language-bindings/python/wasm-c-api/tests/test_advanced.py +++ b/language-bindings/python/wasm-c-api/tests/test_advanced.py @@ -188,18 +188,6 @@ class AdvancedTestSuite(unittest.TestCase): ffi.wasm_func_call(func, params, results) self.assertEqual(params.data[0].of.f32 * 2, results.data[0].of.f64) - def test_wasm_func_call_wrong_params(self): - export_list = ffi.wasm_vec_to_list(self.exports) - func = ffi.wasm_extern_as_func(export_list[0]) - # make a call - params = ffi.wasm_val_vec_t() - ffi.wasm_val_vec_new_empty(params) - results = ffi.wasm_val_vec_t() - ffi.wasm_val_vec_new_empty(results) - trap = ffi.wasm_func_call(func, params, results) - - self.assertIsNotNullPointer(trap) - def test_wasm_func_call_unlinked(self): ft = ffi.wasm_functype_new_0_0() func = ffi.wasm_func_new(self._wasm_store, ft, callback) @@ -453,26 +441,6 @@ class AdvancedTestSuite(unittest.TestCase): ffi.wasm_memory_data_size(mem) ffi.wasm_memory_delete(mem) - def test_wasm_trap(self): - export_list = ffi.wasm_vec_to_list(self.exports) - func = ffi.wasm_extern_as_func(export_list[0]) - # make a call - params = ffi.wasm_val_vec_t() - ffi.wasm_val_vec_new_empty(params) - results = ffi.wasm_val_vec_t() - ffi.wasm_val_vec_new_empty(results) - - trap = ffi.wasm_func_call(func, params, results) - self.assertIsNotNullPointer(trap) - - message = ffi.wasm_message_t() - ffi.wasm_trap_message(trap, message) - self.assertIsNotNullPointer(c.pointer(message)) - - # not a function internal exception - frame = ffi.wasm_trap_origin(trap) - self.assertIsNullPointer(frame) - @unittest.skipUnless( TEST_WITH_WAMR_BUILD_DUMP_CALL_STACK, "need to enable WAMR_BUILD_DUMP_CALL_STACK", From bb053e3a2de24daaabdbbea7dfa7a3dd1ab8cf87 Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Tue, 16 Jan 2024 05:14:43 +0100 Subject: [PATCH 11/53] Do not use pagezero size option if osx version >= 13 (#3025) Reported in https://github.com/bytecodealliance/wasm-micro-runtime/issues/3009. --- build-scripts/config_common.cmake | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 212141d5e..62c5163ac 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -464,7 +464,13 @@ else () endif () if (APPLE) + EXEC_PROGRAM(sw_vers ARGS -productVersion OUTPUT_VARIABLE OS_X_VERSION) + STRING(REGEX MATCH "([0-9]+)" OS_X_VERSION ${OS_X_VERSION}) + if (OS_X_VERSION LESS 13) + set(PAGE_ZERO_SIZE_OPTION "-Wl,-pagezero_size,0x4000") + endif () + # On recent macOS versions, by default, the size of page zero is 4GB. # Shrink it to make MAP_32BIT mmap can work. - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-pagezero_size,0x4000") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PAGE_ZERO_SIZE_OPTION}") endif () From b3aaf2abc0964bbc3555c6fd272b5638885a5b0f Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 16 Jan 2024 21:55:10 +0800 Subject: [PATCH 12/53] Check passive mode in fast-jit table.init (#3031) Reported in https://github.com/bytecodealliance/wasm-micro-runtime/issues/3021. --- core/iwasm/fast-jit/fe/jit_emit_table.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/iwasm/fast-jit/fe/jit_emit_table.c b/core/iwasm/fast-jit/fe/jit_emit_table.c index b8ed6a1d5..a2f5ce513 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_table.c +++ b/core/iwasm/fast-jit/fe/jit_emit_table.c @@ -112,6 +112,9 @@ wasm_init_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 elem_idx, if (bh_bitmap_get_bit(inst->e->common.elem_dropped, elem_idx)) goto out_of_bounds; + if (!wasm_elem_is_passive(inst->module->table_segments[elem_idx].mode)) + goto out_of_bounds; + bh_memcpy_s((uint8 *)tbl + offsetof(WASMTableInstance, elems) + dst_offset * sizeof(uint32), (uint32)((tbl_sz - dst_offset) * sizeof(uint32)), From ffa131b5accd8575933cfc054b0b5f5176c7339a Mon Sep 17 00:00:00 2001 From: Marcin Kolny Date: Tue, 16 Jan 2024 14:15:55 +0000 Subject: [PATCH 13/53] Allow using mmap for shared memory if hw bound check is disabled (#3029) For shared memory, the max memory size must be defined in advanced. Re-allocation for growing memory can't be used as it might change the base address, therefore when OS_ENABLE_HW_BOUND_CHECK is enabled the memory is mmaped, and if the flag is disabled, the memory is allocated. This change introduces a flag that allows users to use mmap for reserving memory address space even if the OS_ENABLE_HW_BOUND_CHECK is disabled. --- .../compilation_on_android_ubuntu.yml | 1 + build-scripts/config_common.cmake | 6 ++ core/iwasm/aot/aot_runtime.c | 93 +++++++++--------- core/iwasm/common/wasm_runtime_common.c | 64 +++++++++++++ core/iwasm/common/wasm_runtime_common.h | 13 +++ core/iwasm/interpreter/wasm_runtime.c | 94 +++++++++---------- .../platform/android/platform_internal.h | 4 +- .../platform/cosmopolitan/platform_internal.h | 4 +- .../platform/darwin/platform_internal.h | 4 +- .../platform/freebsd/platform_internal.h | 4 +- .../shared/platform/linux/platform_internal.h | 4 +- .../platform/vxworks/platform_internal.h | 4 +- 12 files changed, 183 insertions(+), 112 deletions(-) diff --git a/.github/workflows/compilation_on_android_ubuntu.yml b/.github/workflows/compilation_on_android_ubuntu.yml index 8c27465d3..d842159f5 100644 --- a/.github/workflows/compilation_on_android_ubuntu.yml +++ b/.github/workflows/compilation_on_android_ubuntu.yml @@ -145,6 +145,7 @@ jobs: "-DWAMR_BUILD_SIMD=1", "-DWAMR_BUILD_TAIL_CALL=1", "-DWAMR_DISABLE_HW_BOUND_CHECK=1", + "-DWAMR_ENABLE_SHARED_MEMORY_MMAP=1", ] os: [ubuntu-22.04] platform: [android, linux] diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 62c5163ac..b9d5a0413 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -248,6 +248,12 @@ if (WAMR_BUILD_SHARED_MEMORY EQUAL 1) else () add_definitions (-DWASM_ENABLE_SHARED_MEMORY=0) endif () +if (WAMR_ENABLE_SHARED_MEMORY_MMAP EQUAL 1) + add_definitions (-DWASM_ENABLE_SHARED_MEMORY_MMAP=1) + message (" Shared memory allocated using mmap enabled") +else () + add_definitions (-DWASM_ENABLE_SHARED_MEMORY_MMAP=0) +endif () if (WAMR_BUILD_THREAD_MGR EQUAL 1) message (" Thread manager enabled") endif () diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 0fa4f7939..4f9635a7c 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -340,6 +340,9 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, static void memories_deinstantiate(AOTModuleInstance *module_inst) { +#ifdef WASM_LINEAR_MEMORY_MMAP + uint64 map_size; +#endif uint32 i; AOTMemoryInstance *memory_inst; @@ -362,14 +365,21 @@ memories_deinstantiate(AOTModuleInstance *module_inst) if (memory_inst->memory_data) { #ifndef OS_ENABLE_HW_BOUND_CHECK - wasm_runtime_free(memory_inst->memory_data); -#else -#ifdef BH_PLATFORM_WINDOWS - os_mem_decommit(memory_inst->memory_data, - memory_inst->num_bytes_per_page - * memory_inst->cur_page_count); +#ifdef WASM_LINEAR_MEMORY_MMAP + if (shared_memory_is_shared(memory_inst)) { + map_size = (uint64)memory_inst->num_bytes_per_page + * memory_inst->max_page_count; + wasm_munmap_linear_memory(memory_inst->memory_data, + map_size, map_size); + } + else #endif - os_munmap(memory_inst->memory_data, 8 * (uint64)BH_GB); + wasm_runtime_free(memory_inst->memory_data); +#else + map_size = (uint64)memory_inst->num_bytes_per_page + * memory_inst->cur_page_count; + wasm_munmap_linear_memory(memory_inst->memory_data, map_size, + 8 * (uint64)BH_GB); #endif } } @@ -392,10 +402,9 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent, uint32 heap_offset = num_bytes_per_page * init_page_count; uint64 memory_data_size, max_memory_data_size; uint8 *p = NULL, *global_addr; -#ifdef OS_ENABLE_HW_BOUND_CHECK - uint8 *mapped_mem; - uint64 map_size = 8 * (uint64)BH_GB; - uint64 page_size = os_getpagesize(); +#ifdef WASM_LINEAR_MEMORY_MMAP + uint8 *mapped_mem = NULL; + uint64 map_size; #endif #if WASM_ENABLE_SHARED_MEMORY != 0 @@ -519,15 +528,25 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent, #ifndef OS_ENABLE_HW_BOUND_CHECK #if WASM_ENABLE_SHARED_MEMORY != 0 if (is_shared_memory) { +#if WASM_ENABLE_SHARED_MEMORY_MMAP != 0 + map_size = max_memory_data_size; + if (max_memory_data_size > 0 + && !(p = mapped_mem = + wasm_mmap_linear_memory(map_size, &max_memory_data_size, + error_buf, error_buf_size))) { + return NULL; + } +#else /* Allocate maximum memory size when memory is shared */ if (max_memory_data_size > 0 && !(p = runtime_malloc(max_memory_data_size, error_buf, error_buf_size))) { return NULL; } +#endif } else -#endif +#endif /* end of WASM_ENABLE_SHARED_MEMORY != 0 */ { /* Allocate initial memory size when memory is not shared */ if (memory_data_size > 0 @@ -536,43 +555,18 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent, return NULL; } } -#else /* else of OS_ENABLE_HW_BOUND_CHECK */ - memory_data_size = (memory_data_size + page_size - 1) & ~(page_size - 1); - +#else /* else of OS_ENABLE_HW_BOUND_CHECK */ /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G: * ea = i + memarg.offset * both i and memarg.offset are u32 in range 0 to 4G * so the range of ea is 0 to 8G */ - if (!(p = mapped_mem = os_mmap(NULL, map_size, MMAP_PROT_NONE, - MMAP_MAP_NONE, os_get_invalid_handle()))) { + map_size = 8 * (uint64)BH_GB; + if (!(p = mapped_mem = wasm_mmap_linear_memory( + map_size, &memory_data_size, error_buf, error_buf_size))) { set_error_buf(error_buf, error_buf_size, "mmap memory failed"); return NULL; } - -#ifdef BH_PLATFORM_WINDOWS - if (!os_mem_commit(p, memory_data_size, MMAP_PROT_READ | MMAP_PROT_WRITE)) { - set_error_buf(error_buf, error_buf_size, "commit memory failed"); - os_munmap(mapped_mem, map_size); - return NULL; - } -#endif - - if (os_mprotect(p, memory_data_size, MMAP_PROT_READ | MMAP_PROT_WRITE) - != 0) { - set_error_buf(error_buf, error_buf_size, "mprotect memory failed"); -#ifdef BH_PLATFORM_WINDOWS - os_mem_decommit(p, memory_data_size); -#endif - os_munmap(mapped_mem, map_size); - return NULL; - } - - /* Newly allocated pages are filled with zero by the OS, we don't fill it - * again here */ - - if (memory_data_size > UINT32_MAX) - memory_data_size = UINT32_MAX; #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ memory_inst->module_type = Wasm_Module_AoT; @@ -623,16 +617,15 @@ fail2: if (heap_size > 0) wasm_runtime_free(memory_inst->heap_handle); fail1: -#ifndef OS_ENABLE_HW_BOUND_CHECK - if (memory_inst->memory_data) - wasm_runtime_free(memory_inst->memory_data); -#else -#ifdef BH_PLATFORM_WINDOWS - if (memory_inst->memory_data) - os_mem_decommit(p, memory_data_size); -#endif - os_munmap(mapped_mem, map_size); +#ifdef WASM_LINEAR_MEMORY_MMAP + if (mapped_mem) + wasm_munmap_linear_memory(mapped_mem, memory_data_size, map_size); + else #endif + { + if (memory_inst->memory_data) + wasm_runtime_free(memory_inst->memory_data); + } memory_inst->memory_data = NULL; return NULL; } diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index e245cf50b..5c75d82af 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -6229,3 +6229,67 @@ wasm_runtime_set_linux_perf(bool flag) enable_linux_perf = flag; } #endif + +#ifdef WASM_LINEAR_MEMORY_MMAP +void +wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size) +{ +#ifdef BH_PLATFORM_WINDOWS + os_mem_decommit(mapped_mem, commit_size); +#else + (void)commit_size; +#endif + os_munmap(mapped_mem, map_size); +} + +void * +wasm_mmap_linear_memory(uint64_t map_size, uint64 *io_memory_data_size, + char *error_buf, uint32 error_buf_size) +{ + uint64 page_size = os_getpagesize(); + void *mapped_mem = NULL; + uint64 memory_data_size; + + bh_assert(io_memory_data_size); + + memory_data_size = + (*io_memory_data_size + page_size - 1) & ~(page_size - 1); + + if (memory_data_size > UINT32_MAX) + memory_data_size = UINT32_MAX; + + if (!(mapped_mem = os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE, + os_get_invalid_handle()))) { + set_error_buf(error_buf, error_buf_size, "mmap memory failed"); + goto fail1; + } + +#ifdef BH_PLATFORM_WINDOWS + if (memory_data_size > 0 + && !os_mem_commit(mapped_mem, memory_data_size, + MMAP_PROT_READ | MMAP_PROT_WRITE)) { + set_error_buf(error_buf, error_buf_size, "commit memory failed"); + os_munmap(mapped_mem, map_size); + goto fail1; + } +#endif + + if (os_mprotect(mapped_mem, memory_data_size, + MMAP_PROT_READ | MMAP_PROT_WRITE) + != 0) { + set_error_buf(error_buf, error_buf_size, "mprotect memory failed"); + goto fail2; + } + + /* Newly allocated pages are filled with zero by the OS, we don't fill it + * again here */ + + *io_memory_data_size = memory_data_size; + + return mapped_mem; +fail2: + wasm_munmap_linear_memory(mapped_mem, memory_data_size, map_size); +fail1: + return NULL; +} +#endif diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 606eea019..039244ff6 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -320,6 +320,11 @@ LOAD_I16(void *addr) #define SHARED_MEMORY_UNLOCK(memory) (void)0 #endif +#if defined(OS_ENABLE_HW_BOUND_CHECK) \ + || (WASM_ENABLE_SHARED_MEMORY != 0 && WASM_ENABLE_SHARED_MEMORY_MMAP != 0) +#define WASM_LINEAR_MEMORY_MMAP +#endif + typedef struct WASMModuleCommon { /* Module type, for module loaded from WASM bytecode binary, this field is Wasm_Module_Bytecode, and this structure should @@ -1093,6 +1098,14 @@ wasm_runtime_quick_invoke_c_api_native(WASMModuleInstanceCommon *module_inst, void wasm_runtime_show_app_heap_corrupted_prompt(); +void +wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, + uint64 map_size); + +void * +wasm_mmap_linear_memory(uint64_t map_size, uint64 *io_memory_data_size, + char *error_buf, uint32 error_buf_size); + #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0 void wasm_runtime_destroy_custom_sections(WASMCustomSection *section_list); diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 7cda841e5..267e3d59d 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -115,6 +115,9 @@ static void memories_deinstantiate(WASMModuleInstance *module_inst, WASMMemoryInstance **memories, uint32 count) { +#ifdef WASM_LINEAR_MEMORY_MMAP + uint64 map_size; +#endif uint32 i; if (memories) { for (i = 0; i < count; i++) { @@ -142,15 +145,21 @@ memories_deinstantiate(WASMModuleInstance *module_inst, } if (memories[i]->memory_data) { #ifndef OS_ENABLE_HW_BOUND_CHECK - wasm_runtime_free(memories[i]->memory_data); -#else -#ifdef BH_PLATFORM_WINDOWS - os_mem_decommit(memories[i]->memory_data, - memories[i]->num_bytes_per_page - * memories[i]->cur_page_count); +#ifdef WASM_LINEAR_MEMORY_MMAP + if (shared_memory_is_shared(memories[i])) { + map_size = (uint64)memories[i]->num_bytes_per_page + * memories[i]->max_page_count; + wasm_munmap_linear_memory(memories[i]->memory_data, + map_size, map_size); + } + else #endif - os_munmap((uint8 *)memories[i]->memory_data, - 8 * (uint64)BH_GB); + wasm_runtime_free(memories[i]->memory_data); +#else + map_size = (uint64)memories[i]->num_bytes_per_page + * memories[i]->cur_page_count; + wasm_munmap_linear_memory(memories[i]->memory_data, + map_size, 8 * (uint64)BH_GB); #endif } } @@ -173,10 +182,9 @@ memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent, uint32 inc_page_count, aux_heap_base, global_idx; uint32 bytes_of_last_page, bytes_to_page_end; uint8 *global_addr; -#ifdef OS_ENABLE_HW_BOUND_CHECK - uint8 *mapped_mem; - uint64 map_size = 8 * (uint64)BH_GB; - uint64 page_size = os_getpagesize(); +#ifdef WASM_LINEAR_MEMORY_MMAP + uint8 *mapped_mem = NULL; + uint64 map_size; #endif #if WASM_ENABLE_SHARED_MEMORY != 0 @@ -295,18 +303,29 @@ memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent, (void)max_memory_data_size; bh_assert(memory != NULL); + #ifndef OS_ENABLE_HW_BOUND_CHECK #if WASM_ENABLE_SHARED_MEMORY != 0 if (is_shared_memory) { /* Allocate maximum memory size when memory is shared */ +#if WASM_ENABLE_SHARED_MEMORY_MMAP != 0 + map_size = max_memory_data_size; + if (max_memory_data_size > 0 + && !(memory->memory_data = mapped_mem = + wasm_mmap_linear_memory(map_size, &max_memory_data_size, + error_buf, error_buf_size))) { + goto fail1; + } +#else if (max_memory_data_size > 0 && !(memory->memory_data = runtime_malloc( max_memory_data_size, error_buf, error_buf_size))) { goto fail1; } +#endif } else -#endif +#endif /* end of WASM_ENABLE_SHARED_MEMORY != 0 */ { /* Allocate initial memory size when memory is not shared */ if (memory_data_size > 0 @@ -315,43 +334,18 @@ memory_instantiate(WASMModuleInstance *module_inst, WASMModuleInstance *parent, goto fail1; } } -#else /* else of OS_ENABLE_HW_BOUND_CHECK */ - memory_data_size = (memory_data_size + page_size - 1) & ~(page_size - 1); - +#else /* else of OS_ENABLE_HW_BOUND_CHECK */ /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G: * ea = i + memarg.offset * both i and memarg.offset are u32 in range 0 to 4G * so the range of ea is 0 to 8G */ - if (!(memory->memory_data = mapped_mem = - os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE, - os_get_invalid_handle()))) { + map_size = 8 * (uint64)BH_GB; + if (!(memory->memory_data = mapped_mem = wasm_mmap_linear_memory( + map_size, &memory_data_size, error_buf, error_buf_size))) { set_error_buf(error_buf, error_buf_size, "mmap memory failed"); goto fail1; } - -#ifdef BH_PLATFORM_WINDOWS - if (memory_data_size > 0 - && !os_mem_commit(mapped_mem, memory_data_size, - MMAP_PROT_READ | MMAP_PROT_WRITE)) { - set_error_buf(error_buf, error_buf_size, "commit memory failed"); - os_munmap(mapped_mem, map_size); - goto fail1; - } -#endif - - if (os_mprotect(mapped_mem, memory_data_size, - MMAP_PROT_READ | MMAP_PROT_WRITE) - != 0) { - set_error_buf(error_buf, error_buf_size, "mprotect memory failed"); - goto fail2; - } - - /* Newly allocated pages are filled with zero by the OS, we don't fill it - * again here */ - - if (memory_data_size > UINT32_MAX) - memory_data_size = UINT32_MAX; #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ memory->module_type = Wasm_Module_Bytecode; @@ -398,15 +392,15 @@ fail3: if (heap_size > 0) wasm_runtime_free(memory->heap_handle); fail2: -#ifndef OS_ENABLE_HW_BOUND_CHECK - if (memory->memory_data) - wasm_runtime_free(memory->memory_data); -#else -#ifdef BH_PLATFORM_WINDOWS - os_mem_decommit(mapped_mem, memory_data_size); -#endif - os_munmap(mapped_mem, map_size); +#ifdef WASM_LINEAR_MEMORY_MMAP + if (mapped_mem) + wasm_munmap_linear_memory(mapped_mem, memory_data_size, map_size); + else #endif + { + if (memory->memory_data) + wasm_runtime_free(memory->memory_data); + } fail1: return NULL; } diff --git a/core/shared/platform/android/platform_internal.h b/core/shared/platform/android/platform_internal.h index 42e4e726c..4449f21e8 100644 --- a/core/shared/platform/android/platform_internal.h +++ b/core/shared/platform/android/platform_internal.h @@ -80,8 +80,6 @@ typedef jmp_buf korp_jmpbuf; #define os_longjmp longjmp #define os_alloca alloca -#define os_getpagesize getpagesize - typedef void (*os_signal_handler)(void *sig_addr); int @@ -101,6 +99,8 @@ os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#define os_getpagesize getpagesize + typedef long int __syscall_slong_t; #if __ANDROID_API__ < 19 diff --git a/core/shared/platform/cosmopolitan/platform_internal.h b/core/shared/platform/cosmopolitan/platform_internal.h index 02cd78be4..66515e305 100644 --- a/core/shared/platform/cosmopolitan/platform_internal.h +++ b/core/shared/platform/cosmopolitan/platform_internal.h @@ -98,8 +98,6 @@ typedef jmp_buf korp_jmpbuf; #define os_longjmp longjmp #define os_alloca alloca -#define os_getpagesize getpagesize - typedef void (*os_signal_handler)(void *sig_addr); int @@ -119,6 +117,8 @@ os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#define os_getpagesize getpagesize + #ifdef __cplusplus } #endif diff --git a/core/shared/platform/darwin/platform_internal.h b/core/shared/platform/darwin/platform_internal.h index 30b89624e..1cbecdc20 100644 --- a/core/shared/platform/darwin/platform_internal.h +++ b/core/shared/platform/darwin/platform_internal.h @@ -82,8 +82,6 @@ typedef jmp_buf korp_jmpbuf; #define os_longjmp longjmp #define os_alloca alloca -#define os_getpagesize getpagesize - typedef void (*os_signal_handler)(void *sig_addr); int @@ -103,6 +101,8 @@ os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#define os_getpagesize getpagesize + #if WASM_DISABLE_WAKEUP_BLOCKING_OP == 0 #define OS_ENABLE_WAKEUP_BLOCKING_OP #endif diff --git a/core/shared/platform/freebsd/platform_internal.h b/core/shared/platform/freebsd/platform_internal.h index 5241c6456..bfdfe1493 100644 --- a/core/shared/platform/freebsd/platform_internal.h +++ b/core/shared/platform/freebsd/platform_internal.h @@ -85,8 +85,6 @@ typedef jmp_buf korp_jmpbuf; #define os_longjmp longjmp #define os_alloca alloca -#define os_getpagesize getpagesize - typedef void (*os_signal_handler)(void *sig_addr); int @@ -106,6 +104,8 @@ os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#define os_getpagesize getpagesize + #if WASM_DISABLE_WAKEUP_BLOCKING_OP == 0 #define OS_ENABLE_WAKEUP_BLOCKING_OP #endif diff --git a/core/shared/platform/linux/platform_internal.h b/core/shared/platform/linux/platform_internal.h index 335070bf8..aeddc4ccf 100644 --- a/core/shared/platform/linux/platform_internal.h +++ b/core/shared/platform/linux/platform_internal.h @@ -95,8 +95,6 @@ typedef jmp_buf korp_jmpbuf; #define os_longjmp longjmp #define os_alloca alloca -#define os_getpagesize getpagesize - typedef void (*os_signal_handler)(void *sig_addr); int @@ -116,6 +114,8 @@ os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#define os_getpagesize getpagesize + #if WASM_DISABLE_WAKEUP_BLOCKING_OP == 0 #define OS_ENABLE_WAKEUP_BLOCKING_OP #endif diff --git a/core/shared/platform/vxworks/platform_internal.h b/core/shared/platform/vxworks/platform_internal.h index 930ff7777..1b870c70e 100644 --- a/core/shared/platform/vxworks/platform_internal.h +++ b/core/shared/platform/vxworks/platform_internal.h @@ -79,8 +79,6 @@ typedef jmp_buf korp_jmpbuf; #define os_longjmp longjmp #define os_alloca alloca -#define os_getpagesize getpagesize - typedef void (*os_signal_handler)(void *sig_addr); int @@ -100,6 +98,8 @@ os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64 */ #endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#define os_getpagesize getpagesize + static inline os_file_handle os_get_invalid_handle() { From 7b6d0a5d45da556573e24182dacaf6882132d550 Mon Sep 17 00:00:00 2001 From: Maks Litskevich Date: Tue, 16 Jan 2024 14:28:06 +0000 Subject: [PATCH 14/53] Don't redefine D_INO if already defined (#3036) --- core/shared/platform/common/posix/posix_file.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/shared/platform/common/posix/posix_file.c b/core/shared/platform/common/posix/posix_file.c index 8c4f7aa9d..ad5589f73 100644 --- a/core/shared/platform/common/posix/posix_file.c +++ b/core/shared/platform/common/posix/posix_file.c @@ -25,12 +25,14 @@ * For NuttX, CONFIG_HAS_ISATTY is provided by its platform header. * (platform_internal.h) */ +#if !defined(CONFIG_HAS_D_INO) #if !defined(__NuttX__) #define CONFIG_HAS_D_INO 1 #define CONFIG_HAS_ISATTY 1 #else #define CONFIG_HAS_D_INO 0 #endif +#endif #if !defined(__APPLE__) && !defined(ESP_PLATFORM) && !defined(__COSMOPOLITAN__) #define CONFIG_HAS_POSIX_FALLOCATE 1 From 25ccc9f2d51a048d664bbceaedf2877660c75a79 Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Wed, 17 Jan 2024 01:06:02 +0100 Subject: [PATCH 15/53] Avoid unused thread_id warning and recompile multi-module sample (#3033) - Unused variable warning (when building in release mode): ```bash wasm-micro-runtime/samples/wasi-threads/wasm-apps/no_pthread.c:44:9: warning: variable 'thread_id' set but not used [-Wunused-but-set-variable] int thread_id; ``` - When using the multi-module example and changing the files in `wasm-apps`, they don't get recompiled when the sample is rebuilt. Adding `BUILD_ALWAYS` as it's done in other samples. --- samples/multi-module/CMakeLists.txt | 1 + samples/wasi-threads/wasm-apps/wasi_thread_start.h | 1 + 2 files changed, 2 insertions(+) diff --git a/samples/multi-module/CMakeLists.txt b/samples/multi-module/CMakeLists.txt index ee6c98df4..17f4e1bcc 100644 --- a/samples/multi-module/CMakeLists.txt +++ b/samples/multi-module/CMakeLists.txt @@ -132,6 +132,7 @@ endif() # .c -> .wasm ExternalProject_Add(WASM_MODULE SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps + BUILD_ALWAYS TRUE UPDATE_COMMAND "" PATCH_COMMAND "" CONFIGURE_COMMAND ${CMAKE_COMMAND} diff --git a/samples/wasi-threads/wasm-apps/wasi_thread_start.h b/samples/wasi-threads/wasm-apps/wasi_thread_start.h index 2427fd2b9..5eae2938d 100644 --- a/samples/wasi-threads/wasm-apps/wasi_thread_start.h +++ b/samples/wasi-threads/wasm-apps/wasi_thread_start.h @@ -9,6 +9,7 @@ /* See https://github.com/WebAssembly/wasi-threads#design-choice-thread-ids */ #define ASSERT_VALID_TID(TID) \ + (void)TID; \ assert(TID >= 1 && TID <= 0x1FFFFFFF && "Invalid thread ID") typedef struct { From 5c8b8a17a63b709bd4a47ca8e755d447ce772ce2 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Wed, 17 Jan 2024 09:51:54 +0800 Subject: [PATCH 16/53] Enhancements on wasm function execution time statistic (#2985) Enhance the statistic of wasm function execution time, or the performance profiling feature: - Add os_time_thread_cputime_us() to get the cputime of a thread, and use it to calculate the execution time of a wasm function - Support the statistic of the children execution time of a function, and dump it in wasm_runtime_dump_perf_profiling - Expose two APIs: wasm_runtime_sum_wasm_exec_time wasm_runtime_get_wasm_func_exec_time And rename os_time_get_boot_microsecond to os_time_get_boot_us. --- core/iwasm/aot/aot_runtime.c | 60 +++++++- core/iwasm/aot/aot_runtime.h | 9 ++ core/iwasm/common/wasm_runtime_common.c | 35 +++++ core/iwasm/fast-jit/fe/jit_emit_control.c | 5 +- core/iwasm/fast-jit/jit_frontend.c | 5 +- core/iwasm/include/wasm_export.h | 20 +++ core/iwasm/interpreter/wasm_interp_classic.c | 9 +- core/iwasm/interpreter/wasm_interp_fast.c | 10 +- core/iwasm/interpreter/wasm_runtime.c | 139 +++++++++++------- core/iwasm/interpreter/wasm_runtime.h | 9 ++ .../libc-builtin/libc_builtin_wrapper.c | 4 +- .../libraries/thread-mgr/thread_manager.c | 6 + core/shared/platform/alios/alios_time.c | 9 +- .../platform/common/freertos/freertos_time.c | 9 +- .../shared/platform/common/posix/posix_time.c | 13 +- .../shared/platform/esp-idf/espidf_platform.c | 9 +- .../platform/include/platform_api_vmcore.h | 8 +- core/shared/platform/linux-sgx/sgx_time.c | 17 ++- core/shared/platform/riot/riot_time.c | 15 +- core/shared/platform/rt-thread/rtt_platform.c | 9 +- core/shared/platform/windows/win_time.c | 9 +- core/shared/platform/zephyr/zephyr_time.c | 9 +- core/shared/utils/bh_log.c | 2 +- core/shared/utils/runtime_timer.c | 2 +- 24 files changed, 336 insertions(+), 86 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 4f9635a7c..8f7032c43 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -2807,7 +2807,7 @@ aot_alloc_frame(WASMExecEnv *exec_env, uint32 func_index) } #if WASM_ENABLE_PERF_PROFILING != 0 - frame->time_started = os_time_get_boot_microsecond(); + frame->time_started = os_time_thread_cputime_us(); frame->func_perf_prof_info = func_perf_prof; #endif @@ -2826,8 +2826,13 @@ aot_free_frame(WASMExecEnv *exec_env) #if WASM_ENABLE_PERF_PROFILING != 0 cur_frame->func_perf_prof_info->total_exec_time += - os_time_get_boot_microsecond() - cur_frame->time_started; + os_time_thread_cputime_us() - cur_frame->time_started; cur_frame->func_perf_prof_info->total_exec_cnt++; + + /* parent function */ + if (prev_frame) + prev_frame->func_perf_prof_info->children_exec_time = + cur_frame->func_perf_prof_info->total_exec_time; #endif wasm_exec_env_free_wasm_frame(exec_env, cur_frame); @@ -2964,22 +2969,65 @@ aot_dump_perf_profiling(const AOTModuleInstance *module_inst) os_printf("Performance profiler data:\n"); for (i = 0; i < total_func_count; i++, perf_prof++) { + if (perf_prof->total_exec_cnt == 0) + continue; + func_name = get_func_name_from_index(module_inst, i); if (func_name) os_printf( " func %s, execution time: %.3f ms, execution count: %" PRIu32 - " times\n", + " times, children execution time: %.3f ms\n", func_name, perf_prof->total_exec_time / 1000.0f, - perf_prof->total_exec_cnt); + perf_prof->total_exec_cnt, + perf_prof->children_exec_time / 1000.0f); else os_printf(" func %" PRIu32 ", execution time: %.3f ms, execution count: %" PRIu32 - " times\n", + " times, children execution time: %.3f ms\n", i, perf_prof->total_exec_time / 1000.0f, - perf_prof->total_exec_cnt); + perf_prof->total_exec_cnt, + perf_prof->children_exec_time / 1000.0f); } } + +double +aot_summarize_wasm_execute_time(const AOTModuleInstance *inst) +{ + double ret = 0; + + AOTModule *module = (AOTModule *)inst->module; + uint32 total_func_count = module->import_func_count + module->func_count, i; + + for (i = 0; i < total_func_count; i++) { + AOTFuncPerfProfInfo *perf_prof = + (AOTFuncPerfProfInfo *)inst->func_perf_profilings + i; + ret += (perf_prof->total_exec_time - perf_prof->children_exec_time) + / 1000.0f; + } + + return ret; +} + +double +aot_get_wasm_func_exec_time(const AOTModuleInstance *inst, + const char *func_name) +{ + AOTModule *module = (AOTModule *)inst->module; + uint32 total_func_count = module->import_func_count + module->func_count, i; + + for (i = 0; i < total_func_count; i++) { + const char *name_in_wasm = get_func_name_from_index(inst, i); + if (name_in_wasm && strcmp(func_name, name_in_wasm) == 0) { + AOTFuncPerfProfInfo *perf_prof = + (AOTFuncPerfProfInfo *)inst->func_perf_profilings + i; + return (perf_prof->total_exec_time - perf_prof->children_exec_time) + / 1000.0f; + } + } + + return -1.0; +} #endif /* end of WASM_ENABLE_PERF_PROFILING */ #if WASM_ENABLE_STATIC_PGO != 0 diff --git a/core/iwasm/aot/aot_runtime.h b/core/iwasm/aot/aot_runtime.h index 35c78bfa0..47d8a2021 100644 --- a/core/iwasm/aot/aot_runtime.h +++ b/core/iwasm/aot/aot_runtime.h @@ -286,6 +286,8 @@ typedef struct AOTFuncPerfProfInfo { uint64 total_exec_time; /* total execution count */ uint32 total_exec_cnt; + /* children execution time */ + uint64 children_exec_time; } AOTFuncPerfProfInfo; /* AOT auxiliary call stack */ @@ -613,6 +615,13 @@ aot_dump_call_stack(WASMExecEnv *exec_env, bool print, char *buf, uint32 len); void aot_dump_perf_profiling(const AOTModuleInstance *module_inst); +double +aot_summarize_wasm_execute_time(const AOTModuleInstance *inst); + +double +aot_get_wasm_func_exec_time(const AOTModuleInstance *inst, + const char *func_name); + const uint8 * aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len); diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 5c75d82af..2bd29205e 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -1690,8 +1690,43 @@ wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst) } #endif } + +double +wasm_runtime_sum_wasm_exec_time(WASMModuleInstanceCommon *inst) +{ +#if WASM_ENABLE_INTERP != 0 + if (inst->module_type == Wasm_Module_Bytecode) + return wasm_summarize_wasm_execute_time((WASMModuleInstance *)inst); #endif +#if WASM_ENABLE_AOT != 0 + if (inst->module_type == Wasm_Module_AoT) + return aot_summarize_wasm_execute_time((AOTModuleInstance *)inst); +#endif + + return 0.0; +} + +double +wasm_runtime_get_wasm_func_exec_time(WASMModuleInstanceCommon *inst, + const char *func_name) +{ +#if WASM_ENABLE_INTERP != 0 + if (inst->module_type == Wasm_Module_Bytecode) + return wasm_get_wasm_func_exec_time((WASMModuleInstance *)inst, + func_name); +#endif + +#if WASM_ENABLE_AOT != 0 + if (inst->module_type == Wasm_Module_AoT) + return aot_get_wasm_func_exec_time((AOTModuleInstance *)inst, + func_name); +#endif + + return 0.0; +} +#endif /* WASM_ENABLE_PERF_PROFILING != 0 */ + WASMModuleInstanceCommon * wasm_runtime_get_module_inst(WASMExecEnv *exec_env) { diff --git a/core/iwasm/fast-jit/fe/jit_emit_control.c b/core/iwasm/fast-jit/fe/jit_emit_control.c index 5438bc62c..47ab1d51e 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_control.c +++ b/core/iwasm/fast-jit/fe/jit_emit_control.c @@ -396,9 +396,8 @@ handle_func_return(JitCompContext *cc, JitBlock *block) #endif #if WASM_ENABLE_PERF_PROFILING != 0 - /* time_end = os_time_get_boot_microsecond() */ - if (!jit_emit_callnative(cc, os_time_get_boot_microsecond, time_end, NULL, - 0)) { + /* time_end = os_time_get_boot_us() */ + if (!jit_emit_callnative(cc, os_time_get_boot_us, time_end, NULL, 0)) { return false; } /* time_start = cur_frame->time_started */ diff --git a/core/iwasm/fast-jit/jit_frontend.c b/core/iwasm/fast-jit/jit_frontend.c index c9c22e0ad..e0b204ce9 100644 --- a/core/iwasm/fast-jit/jit_frontend.c +++ b/core/iwasm/fast-jit/jit_frontend.c @@ -1157,11 +1157,10 @@ init_func_translation(JitCompContext *cc) func_inst = jit_cc_new_reg_ptr(cc); #if WASM_ENABLE_PERF_PROFILING != 0 time_started = jit_cc_new_reg_I64(cc); - /* Call os_time_get_boot_microsecond() to get time_started firstly + /* Call os_time_get_boot_us() to get time_started firstly as there is stack frame switching below, calling native in them may cause register spilling work inproperly */ - if (!jit_emit_callnative(cc, os_time_get_boot_microsecond, time_started, - NULL, 0)) { + if (!jit_emit_callnative(cc, os_time_get_boot_us, time_started, NULL, 0)) { return NULL; } #endif diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index c27b52339..4a23b8d19 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -1274,6 +1274,26 @@ wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env); WASM_RUNTIME_API_EXTERN void wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst); +/** + * Return total wasm functions' execution time in ms + * + * @param module_inst the WASM module instance to profile + */ +WASM_RUNTIME_API_EXTERN double +wasm_runtime_sum_wasm_exec_time(wasm_module_inst_t module_inst); + +/** + * Return execution time in ms of a given wasm funciton with +* func_name. If the function is not found, return 0. + * + * @param module_inst the WASM module instance to profile + * @param func_name could be an export name or a name in the + * name section + */ +WASM_RUNTIME_API_EXTERN double +wasm_runtime_get_wasm_func_exec_time(wasm_module_inst_t inst, + const char *func_name); + /* wasm thread callback function type */ typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *); /* wasm thread type */ diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 937f8525e..c5e8879ee 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -850,7 +850,7 @@ ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) if (frame) { frame->prev_frame = prev_frame; #if WASM_ENABLE_PERF_PROFILING != 0 - frame->time_started = os_time_get_boot_microsecond(); + frame->time_started = os_time_thread_cputime_us(); #endif } else { @@ -866,9 +866,14 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame) { #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { + WASMInterpFrame *prev_frame = frame->prev_frame; frame->function->total_exec_time += - os_time_get_boot_microsecond() - frame->time_started; + os_time_thread_cputime_us() - frame->time_started; frame->function->total_exec_cnt++; + + if (prev_frame && prev_frame->function) + prev_frame->function->children_exec_time += + frame->function->total_exec_time; } #endif wasm_exec_env_free_wasm_frame(exec_env, frame); diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 099c18fc5..86aaa5e6d 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -884,7 +884,7 @@ ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) if (frame) { frame->prev_frame = prev_frame; #if WASM_ENABLE_PERF_PROFILING != 0 - frame->time_started = os_time_get_boot_microsecond(); + frame->time_started = os_time_thread_cputime_us(); #endif } else { @@ -900,9 +900,15 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame) { #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { + WASMInterpFrame *prev_frame = frame->prev_frame; + frame->function->total_exec_time += - os_time_get_boot_microsecond() - frame->time_started; + os_time_thread_cputime_us() - frame->time_started; frame->function->total_exec_cnt++; + + if (prev_frame && prev_frame->function) + prev_frame->function->children_exec_time += + frame->function->total_exec_time; } #endif wasm_exec_env_free_wasm_frame(exec_env, frame); diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 267e3d59d..560a14c12 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2392,53 +2392,103 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function, return !wasm_copy_exception(module_inst, NULL); } -#if WASM_ENABLE_PERF_PROFILING != 0 -void -wasm_dump_perf_profiling(const WASMModuleInstance *module_inst) +#if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 +/* look for the function name */ +static char * +get_func_name_from_index(const WASMModuleInstance *inst, uint32 func_index) { - WASMExportFuncInstance *export_func; - WASMFunctionInstance *func_inst; - char *func_name; - uint32 i, j; + char *func_name = NULL; + WASMFunctionInstance *func_inst = inst->e->functions + func_index; - os_printf("Performance profiler data:\n"); - for (i = 0; i < module_inst->e->function_count; i++) { - func_inst = module_inst->e->functions + i; - if (func_inst->is_import_func) { - func_name = func_inst->u.func_import->field_name; - } + if (func_inst->is_import_func) { + func_name = func_inst->u.func_import->field_name; + } + else { #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 - else if (func_inst->u.func->field_name) { - func_name = func_inst->u.func->field_name; - } + func_name = func_inst->u.func->field_name; #endif - else { - func_name = NULL; - for (j = 0; j < module_inst->export_func_count; j++) { - export_func = module_inst->export_functions + j; + /* if custom name section is not generated, + search symbols from export table */ + if (!func_name) { + unsigned j; + for (j = 0; j < inst->export_func_count; j++) { + WASMExportFuncInstance *export_func = + inst->export_functions + j; if (export_func->function == func_inst) { func_name = export_func->name; break; } } } + } + return func_name; +} +#endif /*WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0*/ + +#if WASM_ENABLE_PERF_PROFILING != 0 +void +wasm_dump_perf_profiling(const WASMModuleInstance *module_inst) +{ + WASMFunctionInstance *func_inst; + char *func_name; + uint32 i; + + os_printf("Performance profiler data:\n"); + for (i = 0; i < module_inst->e->function_count; i++) { + func_inst = module_inst->e->functions + i; + + if (func_inst->total_exec_cnt == 0) + continue; + + func_name = get_func_name_from_index(module_inst, i); if (func_name) os_printf( " func %s, execution time: %.3f ms, execution count: %" PRIu32 - " times\n", - func_name, - module_inst->e->functions[i].total_exec_time / 1000.0f, - module_inst->e->functions[i].total_exec_cnt); + " times, children execution time: %.3f ms\n", + func_name, func_inst->total_exec_time / 1000.0f, + func_inst->total_exec_cnt, + func_inst->children_exec_time / 1000.0f); else os_printf(" func %" PRIu32 ", execution time: %.3f ms, execution count: %" PRIu32 - " times\n", - i, module_inst->e->functions[i].total_exec_time / 1000.0f, - module_inst->e->functions[i].total_exec_cnt); + " times, children execution time: %.3f ms\n", + i, func_inst->total_exec_time / 1000.0f, + func_inst->total_exec_cnt, + func_inst->children_exec_time / 1000.0f); } } -#endif + +double +wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst) +{ + double ret = 0; + + unsigned i; + for (i = 0; i < inst->e->function_count; i++) { + WASMFunctionInstance *func = inst->e->functions + i; + ret += (func->total_exec_time - func->children_exec_time) / 1000.0f; + } + + return ret; +} + +double +wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst, + const char *func_name) +{ + unsigned i; + for (i = 0; i < inst->e->function_count; i++) { + char *name_in_wasm = get_func_name_from_index(inst, i); + if (name_in_wasm && strcmp(name_in_wasm, func_name) == 0) { + WASMFunctionInstance *func = inst->e->functions + i; + return (func->total_exec_time - func->children_exec_time) / 1000.0f; + } + } + + return -1.0; +} +#endif /*WASM_ENABLE_PERF_PROFILING != 0*/ uint32 wasm_module_malloc_internal(WASMModuleInstance *module_inst, @@ -2933,29 +2983,7 @@ wasm_interp_create_call_stack(struct WASMExecEnv *exec_env) frame.func_offset = (uint32)(cur_frame->ip - func_code_base); } - /* look for the function name */ - if (func_inst->is_import_func) { - func_name = func_inst->u.func_import->field_name; - } - else { -#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 - func_name = func_inst->u.func->field_name; -#endif - /* if custom name section is not generated, - search symbols from export table */ - if (!func_name) { - uint32 i; - for (i = 0; i < module_inst->export_func_count; i++) { - WASMExportFuncInstance *export_func = - module_inst->export_functions + i; - if (export_func->function == func_inst) { - func_name = export_func->name; - break; - } - } - } - } - + func_name = get_func_name_from_index(module_inst, frame.func_index); frame.func_name_wp = func_name; if (!bh_vector_append(module_inst->frames, &frame)) { @@ -3402,7 +3430,7 @@ llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index) frame->ip = NULL; frame->sp = frame->lp; #if WASM_ENABLE_PERF_PROFILING != 0 - frame->time_started = os_time_get_boot_microsecond(); + frame->time_started = os_time_thread_cputime_us(); #endif frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env); wasm_exec_env_set_cur_frame(exec_env, frame); @@ -3424,8 +3452,13 @@ llvm_jit_free_frame(WASMExecEnv *exec_env) #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { frame->function->total_exec_time += - os_time_get_boot_microsecond() - frame->time_started; + os_time_thread_cputime_us() - frame->time_started; frame->function->total_exec_cnt++; + + /* parent function */ + if (prev_frame) + prev_frame->function->children_exec_time = + frame->function->total_exec_time; } #endif wasm_exec_env_free_wasm_frame(exec_env, frame); diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 89b92b658..80ffde349 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -186,6 +186,8 @@ struct WASMFunctionInstance { uint64 total_exec_time; /* total execution count */ uint32 total_exec_cnt; + /* children execution time */ + uint64 children_exec_time; #endif }; @@ -432,6 +434,13 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, void wasm_dump_perf_profiling(const WASMModuleInstance *module_inst); +double +wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst); + +double +wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst, + const char *func_name); + void wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst); diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c index 55916deb4..fec8fcc97 100644 --- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c +++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c @@ -927,7 +927,7 @@ clock_gettime_wrapper(wasm_exec_env_t exec_env, uint32 clk_id, if (!validate_native_addr(ts_app, sizeof(struct timespec_app))) return (uint32)-1; - time = os_time_get_boot_microsecond(); + time = os_time_get_boot_us(); ts_app->tv_sec = time / 1000000; ts_app->tv_nsec = (time % 1000000) * 1000; @@ -939,7 +939,7 @@ clock_wrapper(wasm_exec_env_t exec_env) { /* Convert to nano seconds as CLOCKS_PER_SEC in wasi-sdk */ - return os_time_get_boot_microsecond() * 1000; + return os_time_get_boot_us() * 1000; } #if WASM_ENABLE_SPEC_TEST != 0 diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index eafec6680..a3fc6fe92 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -667,6 +667,12 @@ thread_manager_start_routine(void *arg) since we will exit soon */ } +#if WASM_ENABLE_PERF_PROFILING != 0 + os_printf("============= Spawned thread ===========\n"); + wasm_runtime_dump_perf_profiling(module_inst); + os_printf("========================================\n"); +#endif + /* Free aux stack space */ free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom); /* Remove exec_env */ diff --git a/core/shared/platform/alios/alios_time.c b/core/shared/platform/alios/alios_time.c index 549252738..fb09623a6 100644 --- a/core/shared/platform/alios/alios_time.c +++ b/core/shared/platform/alios/alios_time.c @@ -6,7 +6,14 @@ #include "platform_api_vmcore.h" uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { return (uint64)aos_now_ms() * 1000; } + +uint64 +os_time_thread_cputime_us(void) +{ + /* FIXME if u know the right api */ + return os_time_get_boot_us(); +} \ No newline at end of file diff --git a/core/shared/platform/common/freertos/freertos_time.c b/core/shared/platform/common/freertos/freertos_time.c index 4497d8c6c..e8249fec1 100644 --- a/core/shared/platform/common/freertos/freertos_time.c +++ b/core/shared/platform/common/freertos/freertos_time.c @@ -6,8 +6,15 @@ #include "platform_api_vmcore.h" uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { TickType_t ticks = xTaskGetTickCount(); return (uint64)1000 * 1000 / configTICK_RATE_HZ * ticks; } + +uint64 +os_time_thread_cputime_us(void) +{ + /* FIXME if u know the right api */ + return os_time_get_boot_us(); +} \ No newline at end of file diff --git a/core/shared/platform/common/posix/posix_time.c b/core/shared/platform/common/posix/posix_time.c index bcf5ca3ce..8c339aba2 100644 --- a/core/shared/platform/common/posix/posix_time.c +++ b/core/shared/platform/common/posix/posix_time.c @@ -6,7 +6,7 @@ #include "platform_api_vmcore.h" uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { @@ -15,3 +15,14 @@ os_time_get_boot_microsecond() return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000; } + +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 diff --git a/core/shared/platform/esp-idf/espidf_platform.c b/core/shared/platform/esp-idf/espidf_platform.c index bbc18f69b..0a1dd3c9d 100644 --- a/core/shared/platform/esp-idf/espidf_platform.c +++ b/core/shared/platform/esp-idf/espidf_platform.c @@ -36,11 +36,18 @@ os_vprintf(const char *format, va_list ap) } uint64 -os_time_get_boot_microsecond(void) +os_time_get_boot_us(void) { return (uint64)esp_timer_get_time(); } +uint64 +os_time_thread_cputime_us(void) +{ + /* FIXME if u know the right api */ + return os_time_get_boot_us(); +} + uint8 * os_thread_get_stack_boundary(void) { diff --git a/core/shared/platform/include/platform_api_vmcore.h b/core/shared/platform/include/platform_api_vmcore.h index 559141f75..4a9d29cc0 100644 --- a/core/shared/platform/include/platform_api_vmcore.h +++ b/core/shared/platform/include/platform_api_vmcore.h @@ -64,7 +64,13 @@ os_vprintf(const char *format, va_list ap); * Get microseconds after boot. */ uint64 -os_time_get_boot_microsecond(void); +os_time_get_boot_us(void); + +/** + * Get thread-specific CPU-time clock in microseconds + */ +uint64 +os_time_thread_cputime_us(void); /** * Get current thread id. diff --git a/core/shared/platform/linux-sgx/sgx_time.c b/core/shared/platform/linux-sgx/sgx_time.c index d090083ef..d39db22fb 100644 --- a/core/shared/platform/linux-sgx/sgx_time.c +++ b/core/shared/platform/linux-sgx/sgx_time.c @@ -26,7 +26,7 @@ ocall_clock_nanosleep(int *p_ret, unsigned clock_id, int flags, const void *rem_buf, unsigned int rem_buf_size); uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { #ifndef SGX_DISABLE_WASI struct timespec ts; @@ -40,6 +40,21 @@ os_time_get_boot_microsecond() #endif } +uint64 +os_time_thread_cputime_us(void) +{ +#ifndef SGX_DISABLE_WASI + 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; +#else + return 0; +#endif +} + #ifndef SGX_DISABLE_WASI int diff --git a/core/shared/platform/riot/riot_time.c b/core/shared/platform/riot/riot_time.c index 1503495c1..ce73777c9 100644 --- a/core/shared/platform/riot/riot_time.c +++ b/core/shared/platform/riot/riot_time.c @@ -10,25 +10,32 @@ #if IS_USED(MODULE_ZTIMER64_USEC) uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { return ztimer64_now(ZTIMER64_USEC); } #elif IS_USED(MODULE_ZTIMER64_MSEC) uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { return ztimer64_now(ZTIMER64_MSEC) * 1000; } #else #ifdef __GNUC__ __attribute__((weak)) uint64 -os_time_get_boot_microsecond(); +os_time_get_boot_us(); #endif uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { static uint64_t times; return ++times; } #endif + +uint64 +os_time_thread_cputime_us(void) +{ + /* FIXME if u know the right api */ + return os_time_get_boot_us(); +} diff --git a/core/shared/platform/rt-thread/rtt_platform.c b/core/shared/platform/rt-thread/rtt_platform.c index 37b247d35..31fb31622 100644 --- a/core/shared/platform/rt-thread/rtt_platform.c +++ b/core/shared/platform/rt-thread/rtt_platform.c @@ -120,13 +120,20 @@ os_vprintf(const char *format, va_list ap) } uint64 -os_time_get_boot_microsecond(void) +os_time_get_boot_us(void) { uint64 ret = rt_tick_get() * 1000; ret /= RT_TICK_PER_SECOND; return ret; } +uint64 +os_time_thread_cputime_us(void) +{ + /* FIXME if u know the right api */ + return os_time_get_boot_us(); +} + korp_tid os_self_thread(void) { diff --git a/core/shared/platform/windows/win_time.c b/core/shared/platform/windows/win_time.c index 20e90d5eb..7b2cd4fff 100644 --- a/core/shared/platform/windows/win_time.c +++ b/core/shared/platform/windows/win_time.c @@ -6,7 +6,7 @@ #include "platform_api_vmcore.h" uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { struct timespec ts; #if defined(__MINGW32__) @@ -18,3 +18,10 @@ os_time_get_boot_microsecond() return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000; } + +uint64 +os_time_thread_cputime_us(void) +{ + /* FIXME if u know the right api */ + return os_time_get_boot_us(); +} \ No newline at end of file diff --git a/core/shared/platform/zephyr/zephyr_time.c b/core/shared/platform/zephyr/zephyr_time.c index 99eb3b354..78bc3e076 100644 --- a/core/shared/platform/zephyr/zephyr_time.c +++ b/core/shared/platform/zephyr/zephyr_time.c @@ -6,7 +6,14 @@ #include "platform_api_vmcore.h" uint64 -os_time_get_boot_microsecond() +os_time_get_boot_us() { return k_uptime_get() * 1000; } + +uint64 +os_time_thread_cputime_us(void) +{ + /* FIXME if u know the right api */ + return os_time_get_boot_us(); +} diff --git a/core/shared/utils/bh_log.c b/core/shared/utils/bh_log.c index 7bb3d3363..7a9465e0c 100644 --- a/core/shared/utils/bh_log.c +++ b/core/shared/utils/bh_log.c @@ -31,7 +31,7 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...) self = os_self_thread(); - usec = os_time_get_boot_microsecond(); + usec = os_time_get_boot_us(); t = (uint32)(usec / 1000000) % (24 * 60 * 60); h = t / (60 * 60); t = t % (60 * 60); diff --git a/core/shared/utils/runtime_timer.c b/core/shared/utils/runtime_timer.c index 8fccf4c2f..b9ace567f 100644 --- a/core/shared/utils/runtime_timer.c +++ b/core/shared/utils/runtime_timer.c @@ -38,7 +38,7 @@ struct _timer_ctx { uint64 bh_get_tick_ms() { - return os_time_get_boot_microsecond() / 1000; + return os_time_get_boot_us() / 1000; } uint32 From ade97ac3e1cc8fd57e6e8b58924ef6f191222254 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo <90845888+midokura-xavi92@users.noreply.github.com> Date: Wed, 17 Jan 2024 07:56:04 +0000 Subject: [PATCH 17/53] wamr-compiler: Fix non-x86{_64} host builds (#3037) The build system assumed sizeof (void *) == 8 as a host system running on a x86_64 architecture, x86 otherwise. However, this assumption is invalid for most other architectures out there, such as aarch64 or armv7l. This PR refers to the CMAKE_HOST_SYSTEM_PROCESSOR variable which, according to the documentation, typically refers to the output from "uname -m" for Unix-like systems. --- wamr-compiler/CMakeLists.txt | 37 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/wamr-compiler/CMakeLists.txt b/wamr-compiler/CMakeLists.txt index 4e5b76f86..f7617fed9 100644 --- a/wamr-compiler/CMakeLists.txt +++ b/wamr-compiler/CMakeLists.txt @@ -58,25 +58,38 @@ if (DEFINED WAMR_BUILD_AOT_FUNC_PREFIX) add_definitions(-DAOT_FUNC_PREFIX="${WAMR_BUILD_AOT_FUNC_PREFIX}") endif () -# Set WAMR_BUILD_TARGET, currently values supported: -# "X86_64", "AMD_64", "X86_32", "ARM_32", "MIPS_32", "XTENSA_32" if (NOT WAMR_BUILD_TARGET) - if (CMAKE_SIZEOF_VOID_P EQUAL 8) - # Build as X86_64 by default in 64-bit platform + if (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64") set (WAMR_BUILD_TARGET "X86_64") - else () - # Build as X86_32 by default in 32-bit platform + elseif (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "i686") set (WAMR_BUILD_TARGET "X86_32") - endif () + elseif (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "amd64") + set (WAMR_BUILD_TARGET "AMD_64") + elseif (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "aarch64" + OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64") + set (WAMR_BUILD_TARGET "AARCH64") + elseif (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "arm.*") + message(STATUS "Assuming ${CMAKE_HOST_SYSTEM_PROCESSOR} as ARM_32") + set (WAMR_BUILD_TARGET "ARM_32") + elseif (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "mips") + set (WAMR_BUILD_TARGET "MIPS_32") + elseif (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "xtensa") + set (WAMR_BUILD_TARGET "XTENSA_32") + elseif (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "riscv64") + set (WAMR_BUILD_TARGET "RISCV64") + elseif (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "riscv") + message(STATUS "Assuming ${CMAKE_HOST_SYSTEM_PROCESSOR} as RISCV32") + set (WAMR_BUILD_TARGET "RISCV32") + else () + message (FATAL_ERROR "Unsupported CMAKE_HOST_SYSTEM_PROCESSOR " + "${CMAKE_HOST_SYSTEM_PROCESSOR}") + endif() + if (WAMR_BUILD_PLATFORM STREQUAL "windows") if (("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "Win32")) set (WAMR_BUILD_TARGET "X86_32") endif() - elseif (WAMR_BUILD_PLATFORM STREQUAL "darwin") - if (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64") - set (WAMR_BUILD_TARGET "AARCH64") - endif () - endif() + endif () endif () string(TOUPPER ${WAMR_BUILD_TARGET} WAMR_BUILD_TARGET) From 6fb376cbe3252cbdf5b7b47df417e55e5a1bc1d5 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 17 Jan 2024 16:17:08 +0800 Subject: [PATCH 18/53] Disable quick aot entry for interp and fast-jit (#3039) Quick aot/jit entry is only for aot and llvm jit now, no need to enable it for interpreter and fast-jit when aot and llvm jit are disabled. --- build-scripts/config_common.cmake | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index b9d5a0413..5d710b2e1 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -457,18 +457,22 @@ if (WAMR_BUILD_LINUX_PERF EQUAL 1) add_definitions (-DWASM_ENABLE_LINUX_PERF=1) message (" Linux perf support enabled") endif () -if (NOT DEFINED WAMR_BUILD_QUICK_AOT_ENTRY) - # Enable quick aot/jit entries by default - set (WAMR_BUILD_QUICK_AOT_ENTRY 1) -endif () -if (WAMR_BUILD_QUICK_AOT_ENTRY EQUAL 1) - add_definitions (-DWASM_ENABLE_QUICK_AOT_ENTRY=1) - message (" Quick AOT/JIT entries enabled") +if (WAMR_BUILD_AOT EQUAL 1 OR WAMR_BUILD_JIT EQUAL 1) + if (NOT DEFINED WAMR_BUILD_QUICK_AOT_ENTRY) + # Enable quick aot/jit entries by default + set (WAMR_BUILD_QUICK_AOT_ENTRY 1) + endif () + if (WAMR_BUILD_QUICK_AOT_ENTRY EQUAL 1) + add_definitions (-DWASM_ENABLE_QUICK_AOT_ENTRY=1) + message (" Quick AOT/JIT entries enabled") + else () + add_definitions (-DWASM_ENABLE_QUICK_AOT_ENTRY=0) + message (" Quick AOT/JIT entries disabled") + endif () else () + # Disable quick aot/jit entries for interp and fast-jit add_definitions (-DWASM_ENABLE_QUICK_AOT_ENTRY=0) - message (" Quick AOT/JIT entries disabled") endif () - if (APPLE) EXEC_PROGRAM(sw_vers ARGS -productVersion OUTPUT_VARIABLE OS_X_VERSION) STRING(REGEX MATCH "([0-9]+)" OS_X_VERSION ${OS_X_VERSION}) From dcaaed01601f22ba00233c9077d7369d102e80be Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Wed, 17 Jan 2024 16:26:53 +0800 Subject: [PATCH 19/53] nuttx: Add option to enable quick aot entry (#3040) --- product-mini/platforms/nuttx/wamr.mk | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index ad921157c..37b633131 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -142,6 +142,12 @@ else CFLAGS += -DWASM_ENABLE_AOT=0 endif +ifeq ($(CONFIG_INTERPRETERS_WAMR_AOT_QUICK_ENTRY),y) +CFLAGS += -DWASM_ENABLE_QUICK_AOT_ENTRY=1 +else +CFLAGS += -DWASM_ENABLE_QUICK_AOT_ENTRY=0 +endif + ifeq ($(CONFIG_INTERPRETERS_WAMR_AOT_WORD_ALIGN_READ),y) CFLAGS += -DWASM_ENABLE_WORD_ALIGN_READ=1 else From 64e82ca102f6a65df53225b98209c2223cb7f7a0 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Wed, 17 Jan 2024 19:29:31 +0800 Subject: [PATCH 20/53] Disable WASM_ENABLE_SHARED_MEMORY_MMAP by default (#3042) Fix compilation warning. --- core/config.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/config.h b/core/config.h index 440272676..ed8af92f2 100644 --- a/core/config.h +++ b/core/config.h @@ -502,4 +502,9 @@ #define WASM_ENABLE_QUICK_AOT_ENTRY 1 #endif +/* Disable mmap based shared memory by default */ +#ifndef WASM_ENABLE_SHARED_MEMORY_MMAP +#define WASM_ENABLE_SHARED_MEMORY_MMAP 0 +#endif + #endif /* end of _CONFIG_H_ */ From a9807058a60e0f606ead2f38e853447dbfc1403b Mon Sep 17 00:00:00 2001 From: tkernelcn <90441159+tkernelcn@users.noreply.github.com> Date: Wed, 17 Jan 2024 21:20:35 +0800 Subject: [PATCH 21/53] Set CONFIG_HAS_CAP_ENTER to support posix file api for freertos (#3041) Since there is no so rich api in freertos like embedded system, simply set CONFIG_HAS_CAP_ENTER to 1 to support posix file api for freertos. Test file api in wasm app pass. --- .../libc-wasi/sandboxed-system-primitives/src/ssp_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h index e31889769..298dea674 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h @@ -34,7 +34,7 @@ #define CONFIG_HAS_GETRANDOM 0 #endif -#if defined(__CloudABI__) +#if defined(__CloudABI__) || defined(BH_PLATFORM_FREERTOS) #define CONFIG_HAS_CAP_ENTER 1 #else #define CONFIG_HAS_CAP_ENTER 0 From d13a54f860a67e669988f22d69bcfd643594646f Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 18 Jan 2024 10:22:09 +0900 Subject: [PATCH 22/53] Revert "Enable MAP_32BIT for macOS (#2992)" (#3032) Revert "Do not use pagezero size option if osx version >= 13 (#3025)" and "Enable MAP_32BIT for macOS (#2992)". Discussion: https://github.com/bytecodealliance/wasm-micro-runtime/issues/3009 --- build-scripts/config_common.cmake | 11 ----------- core/shared/platform/common/posix/posix_memmap.c | 16 ++-------------- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 5d710b2e1..76ebcb0b7 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -473,14 +473,3 @@ else () # Disable quick aot/jit entries for interp and fast-jit add_definitions (-DWASM_ENABLE_QUICK_AOT_ENTRY=0) endif () -if (APPLE) - EXEC_PROGRAM(sw_vers ARGS -productVersion OUTPUT_VARIABLE OS_X_VERSION) - STRING(REGEX MATCH "([0-9]+)" OS_X_VERSION ${OS_X_VERSION}) - if (OS_X_VERSION LESS 13) - set(PAGE_ZERO_SIZE_OPTION "-Wl,-pagezero_size,0x4000") - endif () - - # On recent macOS versions, by default, the size of page zero is 4GB. - # Shrink it to make MAP_32BIT mmap can work. - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PAGE_ZERO_SIZE_OPTION}") -endif () diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index de13c065f..1ab99fd91 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -78,19 +78,15 @@ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) map_prot |= PROT_EXEC; #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) +#ifndef __APPLE__ if (flags & MMAP_MAP_32BIT) map_flags |= MAP_32BIT; +#endif #endif if (flags & MMAP_MAP_FIXED) map_flags |= MAP_FIXED; -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) -#if defined(__APPLE__) -retry_without_map_32bit: -#endif -#endif - #if defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64) /* As AOT relocation in RISCV64 may require that the code/data mapped * is in range 0 to 2GB, we try to map the memory with hint address @@ -148,14 +144,6 @@ retry_without_map_32bit: } if (addr == MAP_FAILED) { -#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) -#if defined(__APPLE__) - if ((map_flags & MAP_32BIT) != 0) { - map_flags &= ~MAP_32BIT; - goto retry_without_map_32bit; - } -#endif -#endif #if BH_ENABLE_TRACE_MMAP != 0 os_printf("mmap failed\n"); #endif From b97370e3a87cc64b9d58eb6e06f169a19f0597dd Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 18 Jan 2024 16:01:03 +0900 Subject: [PATCH 23/53] samples/terminate: Add a sample to demonstrate wasm_runtime_terminate (#3043) This is basically a modified copy of the "shared-module" example. --- .../compilation_on_android_ubuntu.yml | 6 + .github/workflows/compilation_on_macos.yml | 6 + .github/workflows/nightly_run.yml | 6 + samples/terminate/.gitignore | 1 + samples/terminate/CMakeLists.txt | 97 ++++++++++ samples/terminate/README.md | 4 + samples/terminate/build.sh | 63 ++++++ samples/terminate/run.sh | 3 + samples/terminate/src/main.c | 183 ++++++++++++++++++ samples/terminate/wasm-apps/testapp.wat | 22 +++ 10 files changed, 391 insertions(+) create mode 100644 samples/terminate/.gitignore create mode 100644 samples/terminate/CMakeLists.txt create mode 100644 samples/terminate/README.md create mode 100755 samples/terminate/build.sh create mode 100755 samples/terminate/run.sh create mode 100644 samples/terminate/src/main.c create mode 100644 samples/terminate/wasm-apps/testapp.wat diff --git a/.github/workflows/compilation_on_android_ubuntu.yml b/.github/workflows/compilation_on_android_ubuntu.yml index d842159f5..d6fe4b51d 100644 --- a/.github/workflows/compilation_on_android_ubuntu.yml +++ b/.github/workflows/compilation_on_android_ubuntu.yml @@ -451,6 +451,12 @@ jobs: ./build.sh ./run.sh + - name: Build Sample [terminate] + run: | + cd samples/terminate + ./build.sh + ./run.sh + test: needs: [ diff --git a/.github/workflows/compilation_on_macos.yml b/.github/workflows/compilation_on_macos.yml index 92da67861..fbf81ae1f 100644 --- a/.github/workflows/compilation_on_macos.yml +++ b/.github/workflows/compilation_on_macos.yml @@ -333,3 +333,9 @@ jobs: cd samples/shared-module ./build.sh ./run.sh + + - name: Build Sample [terminate] + run: | + cd samples/terminate + ./build.sh + ./run.sh diff --git a/.github/workflows/nightly_run.yml b/.github/workflows/nightly_run.yml index 69cfb6f1b..17d6f35bd 100644 --- a/.github/workflows/nightly_run.yml +++ b/.github/workflows/nightly_run.yml @@ -509,6 +509,12 @@ jobs: cd samples/shared-module ./build.sh ./run.sh + + - name: Build Sample [terminate] + run: | + cd samples/terminate + ./build.sh + ./run.sh test: needs: [ diff --git a/samples/terminate/.gitignore b/samples/terminate/.gitignore new file mode 100644 index 000000000..0fa8a76bd --- /dev/null +++ b/samples/terminate/.gitignore @@ -0,0 +1 @@ +/out/ \ No newline at end of file diff --git a/samples/terminate/CMakeLists.txt b/samples/terminate/CMakeLists.txt new file mode 100644 index 000000000..d9b5a053d --- /dev/null +++ b/samples/terminate/CMakeLists.txt @@ -0,0 +1,97 @@ +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +cmake_minimum_required (VERSION 3.14) + +include(CheckPIESupported) + +project (terminate) + +set (CMAKE_CXX_STANDARD 17) + +################ runtime settings ################ +string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM) +if (APPLE) + add_definitions(-DBH_PLATFORM_DARWIN) +endif () + +# Reset default linker flags +set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") +set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") + +# WAMR features switch + +# Set WAMR_BUILD_TARGET, currently values supported: +# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", +# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]" +if (NOT DEFINED WAMR_BUILD_TARGET) + if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)") + set (WAMR_BUILD_TARGET "AARCH64") + elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64") + set (WAMR_BUILD_TARGET "RISCV64") + elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) + # Build as X86_64 by default in 64-bit platform + set (WAMR_BUILD_TARGET "X86_64") + elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) + # Build as X86_32 by default in 32-bit platform + set (WAMR_BUILD_TARGET "X86_32") + else () + message(SEND_ERROR "Unsupported build target platform!") + endif () +endif () + +if (NOT CMAKE_BUILD_TYPE) + set (CMAKE_BUILD_TYPE Debug) +endif () + +set (WAMR_BUILD_LIBC_WASI 1) +set (WAMR_BUILD_THREAD_MGR 1) +set (WAMR_BUILD_INTERP 1) +set (WAMR_BUILD_AOT 1) +set (WAMR_BUILD_JIT 0) + +# fast interpreter +# set (WAMR_BUILD_FAST_INTERP 1) + +# fast-jit +# set (WAMR_BUILD_FAST_JIT 1) + +# llvm jit +# set (WAMR_BUILD_JIT 1) +# set (LLVM_DIR /usr/local/opt/llvm@14/lib/cmake/llvm) + +set (WAMR_BUILD_REF_TYPES 1) + +if (NOT MSVC) + # linker flags + if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang")) + set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections") + endif () + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security") + 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")) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register") + endif () + endif () +endif () + +# build out vmlib +set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..) +include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) + +add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) + +################ application related ################ +include_directories(${CMAKE_CURRENT_LIST_DIR}/src) +include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) + +add_executable (terminate src/main.c ${UNCOMMON_SHARED_SOURCE}) + +check_pie_supported() +set_target_properties (terminate PROPERTIES POSITION_INDEPENDENT_CODE ON) + +if (APPLE) + target_link_libraries (terminate vmlib -lm -ldl -lpthread ${LLVM_AVAILABLE_LIBS}) +else () + target_link_libraries (terminate vmlib -lm -ldl -lpthread -lrt ${LLVM_AVAILABLE_LIBS}) +endif () diff --git a/samples/terminate/README.md b/samples/terminate/README.md new file mode 100644 index 000000000..89a9c16bc --- /dev/null +++ b/samples/terminate/README.md @@ -0,0 +1,4 @@ +The "terminate" sample project +============================== + +This sample demonstrates wasm_runtime_terminate API. diff --git a/samples/terminate/build.sh b/samples/terminate/build.sh new file mode 100755 index 000000000..4c882e981 --- /dev/null +++ b/samples/terminate/build.sh @@ -0,0 +1,63 @@ +# +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# + +#!/bin/bash + +CURR_DIR=$PWD +WAMR_DIR=${PWD}/../.. +OUT_DIR=${PWD}/out + +WASM_APPS=${PWD}/wasm-apps + + +rm -rf ${OUT_DIR} +mkdir ${OUT_DIR} +mkdir ${OUT_DIR}/wasm-apps + + +echo "##################### build terminate project" +cd ${CURR_DIR} +mkdir -p cmake_build +cd cmake_build +cmake .. -DCMAKE_BUILD_TYPE=Debug +make -j ${nproc} +if [ $? != 0 ];then + echo "BUILD_FAIL terminate exit as $?\n" + exit 2 +fi + +cp -a terminate ${OUT_DIR} + +printf "\n" + +echo "##################### build wasm apps" + +cd ${WASM_APPS} + +for i in `ls *.wat` +do +APP_SRC="$i" +OUT_FILE=${i%.*}.wasm + +# Note: the CI installs wabt in /opt/wabt +if type wat2wasm; then + WAT2WASM=${WAT2WASM:-wat2wasm} +elif [ -x /opt/wabt/bin/wat2wasm ]; then + WAT2WASM=${WAT2WASM:-/opt/wabt/bin/wat2wasm} +fi + +${WAT2WASM} -o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC} + +# aot +# wamrc -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE} +# mv ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE} + +if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then + echo "build ${OUT_FILE} success" +else + echo "build ${OUT_FILE} fail" +fi +done +echo "##################### build wasm apps done" diff --git a/samples/terminate/run.sh b/samples/terminate/run.sh new file mode 100755 index 000000000..0a75fea2a --- /dev/null +++ b/samples/terminate/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +out/terminate -f out/wasm-apps/testapp.wasm diff --git a/samples/terminate/src/main.c b/samples/terminate/src/main.c new file mode 100644 index 000000000..2dca37f68 --- /dev/null +++ b/samples/terminate/src/main.c @@ -0,0 +1,183 @@ + +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include +#include +#include +#include + +#include "wasm_export.h" +#include "bh_read_file.h" +#include "bh_getopt.h" + +void +print_usage(void) +{ + fprintf(stdout, "Options:\r\n"); + fprintf(stdout, " -f [path of wasm file] \n"); +} + +static void * +runner(void *vp) +{ + wasm_module_inst_t inst = vp; + bool ok = wasm_runtime_init_thread_env(); + assert(ok); + wasm_application_execute_main(inst, 0, NULL); + wasm_runtime_destroy_thread_env(); + return inst; +} + +int +main(int argc, char *argv_main[]) +{ + int exit_code = 1; + static char global_heap_buf[512 * 1024]; + char *buffer; + char error_buf[128]; + int opt; + char *wasm_path = NULL; + int ret; + int pipe_fds[2]; + + const unsigned int N = 4; + wasm_module_t module = NULL; + wasm_module_inst_t module_inst[N]; + pthread_t th[N]; + unsigned int i; + uint32 buf_size, stack_size = 8092, heap_size = 8092; + + for (i = 0; i < N; i++) { + module_inst[i] = NULL; + } + + RuntimeInitArgs init_args; + memset(&init_args, 0, sizeof(RuntimeInitArgs)); + + while ((opt = getopt(argc, argv_main, "hf:")) != -1) { + switch (opt) { + case 'f': + wasm_path = optarg; + break; + case 'h': + print_usage(); + return 0; + case '?': + print_usage(); + return 0; + } + } + if (optind == 1) { + print_usage(); + return 0; + } + + memset(&init_args, 0, sizeof(init_args)); + init_args.mem_alloc_type = Alloc_With_Pool; + init_args.mem_alloc_option.pool.heap_buf = global_heap_buf; + init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf); + + if (!wasm_runtime_full_init(&init_args)) { + printf("Init runtime environment failed.\n"); + return -1; + } + + buffer = bh_read_file_to_buffer(wasm_path, &buf_size); + + if (!buffer) { + printf("Open wasm app file [%s] failed.\n", wasm_path); + goto fail; + } + + module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf, + sizeof(error_buf)); + if (!module) { + printf("Load wasm module failed. error: %s\n", error_buf); + goto fail; + } + + /* Ensure that fd_read on FD 0 blocks. */ + ret = pipe(pipe_fds); + if (ret != 0) { + goto fail; + } + wasm_runtime_set_wasi_args_ex(module, NULL, 0, NULL, 0, NULL, 0, NULL, 0, + pipe_fds[0], -1, -1); + + for (i = 0; i < N; i++) { + module_inst[i] = wasm_runtime_instantiate(module, stack_size, heap_size, + error_buf, sizeof(error_buf)); + + if (!module_inst[i]) { + printf("Instantiate wasm module failed. error: %s\n", error_buf); + goto fail; + } + + /* Note: ensure that module inst has an exec env so that + * it can receive the termination request. + */ + wasm_runtime_get_exec_env_singleton(module_inst[i]); + + if ((i % 2) == 0) { + printf("terminating thread %u before starting\n", i); + wasm_runtime_terminate(module_inst[i]); + } + + printf("starting thread %u\n", i); + ret = pthread_create(&th[i], NULL, runner, module_inst[i]); + if (ret != 0) { + goto fail; + } + } + + printf("sleeping a bit to ensure that the threads actually started\n"); + sleep(1); + + for (i = 0; i < N; i++) { + if ((i % 2) != 0) { + printf("terminating thread %u\n", i); + wasm_runtime_terminate(module_inst[i]); + } + } + + for (i = 0; i < N; i++) { + printf("joining thread %u\n", i); + void *status; + ret = pthread_join(th[i], &status); + if (ret != 0) { + goto fail; + } + } + + for (i = 0; i < N; i++) { + const char *exception = wasm_runtime_get_exception(module_inst[i]); + if (exception != NULL) { + if (!strstr(exception, "terminated by user")) { + printf("thread %u got an exception: %s (unexpected)\n", i, + exception); + goto fail; + } + printf("thread %u got an exception: %s (expected)\n", i, exception); + } + else { + printf("thread %u got no exception (unexpected)\n", i); + goto fail; + } + } + + exit_code = 0; +fail: + for (i = 0; i < N; i++) { + if (module_inst[i]) + wasm_runtime_deinstantiate(module_inst[i]); + } + if (module) + wasm_runtime_unload(module); + if (buffer) + BH_FREE(buffer); + wasm_runtime_destroy(); + return exit_code; +} diff --git a/samples/terminate/wasm-apps/testapp.wat b/samples/terminate/wasm-apps/testapp.wat new file mode 100644 index 000000000..0b75a7c62 --- /dev/null +++ b/samples/terminate/wasm-apps/testapp.wat @@ -0,0 +1,22 @@ +;; Copyright (C) 2024 YAMAMOTO Takashi +;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +(module + (func $fd_read (import "wasi_snapshot_preview1" "fd_read") (param i32 i32 i32 i32) (result i32)) + (func (export "_start") + ;; read from FD 0 + i32.const 100 ;; iov_base + i32.const 200 ;; buffer + i32.store + i32.const 104 ;; iov_len + i32.const 1 + i32.store + i32.const 0 ;; fd 0 + i32.const 100 ;; iov_base + i32.const 1 ;; iov count + i32.const 300 ;; retp (out) + call $fd_read + unreachable + ) + (memory (export "memory") 1) +) From fde98331ff4df22c0357ca0234407dc083f57f6e Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 18 Jan 2024 21:44:09 +0900 Subject: [PATCH 24/53] examples/terminate: Use wasm_runtime_spawn_exec_env as well (#3049) --- samples/terminate/CMakeLists.txt | 1 + samples/terminate/src/main.c | 48 +++++++++++++++++++++---- samples/terminate/wasm-apps/testapp.wat | 33 ++++++++++++++++- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/samples/terminate/CMakeLists.txt b/samples/terminate/CMakeLists.txt index d9b5a053d..246b835dd 100644 --- a/samples/terminate/CMakeLists.txt +++ b/samples/terminate/CMakeLists.txt @@ -45,6 +45,7 @@ if (NOT CMAKE_BUILD_TYPE) endif () set (WAMR_BUILD_LIBC_WASI 1) +set (WAMR_BUILD_LIB_WASI_THREADS 1) set (WAMR_BUILD_THREAD_MGR 1) set (WAMR_BUILD_INTERP 1) set (WAMR_BUILD_AOT 1) diff --git a/samples/terminate/src/main.c b/samples/terminate/src/main.c index 2dca37f68..4885b0b11 100644 --- a/samples/terminate/src/main.c +++ b/samples/terminate/src/main.c @@ -21,7 +21,7 @@ print_usage(void) } static void * -runner(void *vp) +runner_with_sigleton_exec_env(void *vp) { wasm_module_inst_t inst = vp; bool ok = wasm_runtime_init_thread_env(); @@ -31,6 +31,22 @@ runner(void *vp) return inst; } +static void * +runner_with_spawn_exec_env(void *vp) +{ + wasm_exec_env_t env = vp; + wasm_module_inst_t inst = wasm_runtime_get_module_inst(env); + wasm_function_inst_t func; + bool ok = wasm_runtime_init_thread_env(); + assert(ok); + func = wasm_runtime_lookup_function(inst, "block_forever", NULL); + assert(func != NULL); + wasm_runtime_call_wasm(env, func, 0, NULL); + wasm_runtime_destroy_spawned_exec_env(env); + wasm_runtime_destroy_thread_env(); + return inst; +} + int main(int argc, char *argv_main[]) { @@ -108,6 +124,9 @@ main(int argc, char *argv_main[]) pipe_fds[0], -1, -1); for (i = 0; i < N; i++) { + bool use_wasm_runtime_spawn_exec_env = i / 2 == 0; + wasm_exec_env_t env; + module_inst[i] = wasm_runtime_instantiate(module, stack_size, heap_size, error_buf, sizeof(error_buf)); @@ -119,17 +138,33 @@ main(int argc, char *argv_main[]) /* Note: ensure that module inst has an exec env so that * it can receive the termination request. */ - wasm_runtime_get_exec_env_singleton(module_inst[i]); + env = wasm_runtime_get_exec_env_singleton(module_inst[i]); + assert(env != NULL); + if (use_wasm_runtime_spawn_exec_env) { + env = wasm_runtime_spawn_exec_env(env); + assert(env != NULL); + } if ((i % 2) == 0) { printf("terminating thread %u before starting\n", i); wasm_runtime_terminate(module_inst[i]); } - printf("starting thread %u\n", i); - ret = pthread_create(&th[i], NULL, runner, module_inst[i]); - if (ret != 0) { - goto fail; + if (use_wasm_runtime_spawn_exec_env) { + printf("starting thread %u (spawn_exec_env)\n", i); + ret = pthread_create(&th[i], NULL, runner_with_spawn_exec_env, env); + if (ret != 0) { + wasm_runtime_destroy_spawned_exec_env(env); + goto fail; + } + } + else { + printf("starting thread %u (singleton exec_env)\n", i); + ret = pthread_create(&th[i], NULL, runner_with_sigleton_exec_env, + module_inst[i]); + if (ret != 0) { + goto fail; + } } } @@ -148,6 +183,7 @@ main(int argc, char *argv_main[]) void *status; ret = pthread_join(th[i], &status); if (ret != 0) { + printf("pthread_join failed for thread %u\n", i); goto fail; } } diff --git a/samples/terminate/wasm-apps/testapp.wat b/samples/terminate/wasm-apps/testapp.wat index 0b75a7c62..349535da7 100644 --- a/samples/terminate/wasm-apps/testapp.wat +++ b/samples/terminate/wasm-apps/testapp.wat @@ -3,7 +3,7 @@ (module (func $fd_read (import "wasi_snapshot_preview1" "fd_read") (param i32 i32 i32 i32) (result i32)) - (func (export "_start") + (func $block_forever (export "block_forever") ;; read from FD 0 i32.const 100 ;; iov_base i32.const 200 ;; buffer @@ -18,5 +18,36 @@ call $fd_read unreachable ) + (func (export "_start") + call $block_forever + ) + + ;; a dumb malloc/free implementation + (func (export "malloc") (param i32) (result i32) + local.get 0 + i32.const 65535 + i32.add + i32.const 65536 + i32.div_u + memory.grow + local.set 0 + local.get 0 + i32.const -1 + i32.eq + if + i32.const 0 + return + end + local.get 0 + i32.const 65536 + i32.mul + ) + (func (export "free") (param i32)) + (memory (export "memory") 1) + + ;; fake globals to make wasm_set_aux_stack happy + (global (export "__heap_base") i32 (i32.const 0x10000)) + (global (export "__data_end") i32 (i32.const 0x10000)) + (global (mut i32) (i32.const 0x10000)) ) From 9bcf6b4dd3ea9b29d3354d9bf84bb0c6e7093c03 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 19 Jan 2024 08:55:35 +0800 Subject: [PATCH 25/53] Enable quick aot entry when hw bound check is disabled (#3044) - Enable quick aot entry when hw bound check is disabled - Remove unnecessary ret_type argument in the quick aot entries - Declare detailed prototype of aot function to call in each quick aot entry --- core/iwasm/aot/aot_runtime.c | 33 +- core/iwasm/common/wasm_native.c | 981 +++++++++---------- core/iwasm/interpreter/wasm_interp_classic.c | 13 +- 3 files changed, 490 insertions(+), 537 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 8f7032c43..64359191d 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1426,14 +1426,10 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, #if WASM_ENABLE_QUICK_AOT_ENTRY != 0 /* Quick call if the quick aot entry is registered */ if (!signature && func_type->quick_aot_entry) { - void (*invoke_native)( - void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) = func_type->quick_aot_entry; - invoke_native(func_ptr, - func_type->result_count > 0 - ? func_type->types[func_type->param_count] - : VALUE_TYPE_VOID, - exec_env, argv, argv_ret); + void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv, + uint32 *argv_ret) = + func_type->quick_aot_entry; + invoke_native(func_ptr, exec_env, argv, argv_ret); ret = !aot_copy_exception(module_inst, NULL); } else @@ -1472,7 +1468,26 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, } #define invoke_native_internal invoke_native_with_hw_bound_check #else /* else of OS_ENABLE_HW_BOUND_CHECK */ -#define invoke_native_internal wasm_runtime_invoke_native +static inline bool +invoke_native_internal(WASMExecEnv *exec_env, void *func_ptr, + const WASMType *func_type, const char *signature, + void *attachment, uint32 *argv, uint32 argc, + uint32 *argv_ret) +{ +#if WASM_ENABLE_QUICK_AOT_ENTRY != 0 + /* Quick call if the quick aot entry is registered */ + if (!signature && func_type->quick_aot_entry) { + AOTModuleInstance *module_inst = + (AOTModuleInstance *)exec_env->module_inst; + void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv, + uint32 *argv_ret) = func_type->quick_aot_entry; + invoke_native(func_ptr, exec_env, argv, argv_ret); + return !aot_copy_exception(module_inst, NULL); + } +#endif + return wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature, + attachment, argv, argc, argv_ret); +} #endif /* end of OS_ENABLE_HW_BOUND_CHECK */ bool diff --git a/core/iwasm/common/wasm_native.c b/core/iwasm/common/wasm_native.c index 69ae6f2a8..e56521781 100644 --- a/core/iwasm/common/wasm_native.c +++ b/core/iwasm/common/wasm_native.c @@ -618,819 +618,760 @@ wasm_native_destroy() #if WASM_ENABLE_QUICK_AOT_ENTRY != 0 static void -invoke_no_args_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_no_args_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; + void (*native_code)(WASMExecEnv *) = func_ptr; native_code(exec_env); } static void -invoke_no_args_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_no_args_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *) = func_ptr; argv_ret[0] = native_code(exec_env); } static void -invoke_no_args_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_no_args_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env); + int64 (*native_code)(WASMExecEnv *) = func_ptr; + int64 ret = native_code(exec_env); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_i_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_i_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; + void (*native_code)(WASMExecEnv *, int32) = func_ptr; native_code(exec_env, argv[0]); } static void -invoke_i_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_i_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32) = func_ptr; argv_ret[0] = native_code(exec_env, argv[0]); } static void -invoke_i_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_i_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0]); + int64 (*native_code)(WASMExecEnv *, int32) = func_ptr; + int64 ret = native_code(exec_env, argv[0]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_I_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_I_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv)); + void (*native_code)(WASMExecEnv *, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv)); } static void -invoke_I_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_I_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv)); + int32 (*native_code)(WASMExecEnv *, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv)); } static void -invoke_I_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_I_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv)); + int64 (*native_code)(WASMExecEnv *, int64) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_ii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_ii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; + void (*native_code)(WASMExecEnv *, int32, int32) = func_ptr; native_code(exec_env, argv[0], argv[1]); } static void -invoke_ii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_ii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32) = func_ptr; argv_ret[0] = native_code(exec_env, argv[0], argv[1]); } static void -invoke_ii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_ii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], argv[1]); + int64 (*native_code)(WASMExecEnv *, int32, int32) = func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iI_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1)); + void (*native_code)(WASMExecEnv *, int32, int64) = func_ptr; + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1)); } static void -invoke_iI_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1)); + int32 (*native_code)(WASMExecEnv *, int32, int64) = func_ptr; + argv_ret[0] = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1)); } static void -invoke_iI_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1)); + int64 (*native_code)(WASMExecEnv *, int32, int64) = func_ptr; + int64 ret = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_Ii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Ii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2]); + void (*native_code)(WASMExecEnv *, int64, int32) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2]); } static void -invoke_Ii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Ii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2]); + int32 (*native_code)(WASMExecEnv *, int64, int32) = func_ptr; + argv_ret[0] = + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2]); } static void -invoke_Ii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Ii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2]); + int64 (*native_code)(WASMExecEnv *, int64, int32) = func_ptr; + int64 ret = + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_II_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_II_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2)); + void (*native_code)(WASMExecEnv *, int64, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2)); } static void -invoke_II_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_II_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2)); + int32 (*native_code)(WASMExecEnv *, int64, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2)); } static void -invoke_II_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_II_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2)); + int64 (*native_code)(WASMExecEnv *, int64, int64) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; + void (*native_code)(WASMExecEnv *, int32, int32, int32) = func_ptr; native_code(exec_env, argv[0], argv[1], argv[2]); } static void -invoke_iii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32, int32) = func_ptr; argv_ret[0] = native_code(exec_env, argv[0], argv[1], argv[2]); } static void -invoke_iii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], argv[1], argv[2]); + int64 (*native_code)(WASMExecEnv *, int32, int32, int32) = func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1], argv[2]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iiI_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], argv[1], GET_I64_FROM_ADDR(argv + 2)); + void (*native_code)(WASMExecEnv *, int32, int32, int64) = func_ptr; + native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2)); } static void -invoke_iiI_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2)); +} +static void +invoke_iiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int64 (*native_code)(WASMExecEnv *, int32, int32, int64) = func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2)); + PUT_I64_TO_ADDR(argv_ret, ret); +} + +static void +invoke_iIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + void (*native_code)(WASMExecEnv *, int32, int64, int32) = func_ptr; + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + argv[3]); +} +static void +invoke_iIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int32 (*native_code)(WASMExecEnv *, int32, int64, int32) = func_ptr; + argv_ret[0] = native_code(exec_env, argv[0], + GET_I64_FROM_ADDR((uint32 *)argv + 1), argv[3]); +} +static void +invoke_iIi_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int64 (*native_code)(WASMExecEnv *, int32, int64, int32) = func_ptr; + int64 ret = native_code(exec_env, argv[0], + GET_I64_FROM_ADDR((uint32 *)argv + 1), argv[3]); + PUT_I64_TO_ADDR(argv_ret, ret); +} + +static void +invoke_iII_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + void (*native_code)(WASMExecEnv *, int32, int64, int64) = func_ptr; + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3)); +} +static void +invoke_iII_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int32 (*native_code)(WASMExecEnv *, int32, int64, int64) = func_ptr; argv_ret[0] = - native_code(exec_env, argv[0], argv[1], GET_I64_FROM_ADDR(argv + 2)); + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3)); } static void -invoke_iiI_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iII_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = - native_code(exec_env, argv[0], argv[1], GET_I64_FROM_ADDR(argv + 2)); + int64 (*native_code)(WASMExecEnv *, int32, int64, int64) = func_ptr; + int64 ret = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iIi_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Iii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), argv[3]); + void (*native_code)(WASMExecEnv *, int64, int32, int32) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], argv[3]); } static void -invoke_iIi_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Iii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), argv[3]); + int32 (*native_code)(WASMExecEnv *, int64, int32, int32) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], argv[3]); } static void -invoke_iIi_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Iii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), argv[3]); + int64 (*native_code)(WASMExecEnv *, int64, int32, int32) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], argv[3]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iII_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3)); + void (*native_code)(WASMExecEnv *, int64, int32, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], + GET_I64_FROM_ADDR((uint32 *)argv + 3)); } static void -invoke_iII_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3)); + int32 (*native_code)(WASMExecEnv *, int64, int32, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], GET_I64_FROM_ADDR((uint32 *)argv + 3)); } static void -invoke_iII_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3)); + int64 (*native_code)(WASMExecEnv *, int64, int32, int64) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], GET_I64_FROM_ADDR((uint32 *)argv + 3)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_Iii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], argv[3]); + void (*native_code)(WASMExecEnv *, int64, int64, int32) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]); } static void -invoke_Iii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], argv[3]); + int32 (*native_code)(WASMExecEnv *, int64, int64, int32) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]); } static void -invoke_Iii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIi_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], argv[3]); + int64 (*native_code)(WASMExecEnv *, int64, int64, int32) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_IiI_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_III_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3)); + void (*native_code)(WASMExecEnv *, int64, int64, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4)); } static void -invoke_IiI_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_III_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3)); + int32 (*native_code)(WASMExecEnv *, int64, int64, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4)); } static void -invoke_IiI_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_III_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3)); + int64 (*native_code)(WASMExecEnv *, int64, int64, int64) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_IIi_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - argv[4]); -} -static void -invoke_IIi_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), argv[4]); -} -static void -invoke_IIi_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), argv[4]); - PUT_I64_TO_ADDR(argv_ret, ret); -} - -static void -invoke_III_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4)); -} -static void -invoke_III_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; - argv_ret[0] = - native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), GET_I64_FROM_ADDR(argv + 4)); -} -static void -invoke_III_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint64 (*native_code)() = func_ptr; - uint64 ret = - native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), GET_I64_FROM_ADDR(argv + 4)); - PUT_I64_TO_ADDR(argv_ret, ret); -} - -static void -invoke_iiii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - void (*native_code)() = func_ptr; + void (*native_code)(WASMExecEnv *, int32, int32, int32, int32) = func_ptr; native_code(exec_env, argv[0], argv[1], argv[2], argv[3]); } static void -invoke_iiii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32, int32, int32) = func_ptr; argv_ret[0] = native_code(exec_env, argv[0], argv[1], argv[2], argv[3]); } static void -invoke_iiii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], argv[1], argv[2], argv[3]); + int64 (*native_code)(WASMExecEnv *, int32, int32, int32, int32) = func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1], argv[2], argv[3]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iiiI_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; + void (*native_code)(WASMExecEnv *, int32, int32, int32, int64) = func_ptr; native_code(exec_env, argv[0], argv[1], argv[2], - GET_I64_FROM_ADDR(argv + 3)); + GET_I64_FROM_ADDR((uint32 *)argv + 3)); } static void -invoke_iiiI_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32, int32, int64) = func_ptr; argv_ret[0] = native_code(exec_env, argv[0], argv[1], argv[2], - GET_I64_FROM_ADDR(argv + 3)); + GET_I64_FROM_ADDR((uint32 *)argv + 3)); } static void -invoke_iiiI_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], argv[1], argv[2], - GET_I64_FROM_ADDR(argv + 3)); + int64 (*native_code)(WASMExecEnv *, int32, int32, int32, int64) = func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1], argv[2], + GET_I64_FROM_ADDR((uint32 *)argv + 3)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iiIi_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], argv[1], GET_I64_FROM_ADDR(argv + 2), - argv[4]); + void (*native_code)(WASMExecEnv *, int32, int32, int64, int32) = func_ptr; + native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]); } static void -invoke_iiIi_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32, int64, int32) = func_ptr; argv_ret[0] = native_code(exec_env, argv[0], argv[1], - GET_I64_FROM_ADDR(argv + 2), argv[4]); + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]); } static void -invoke_iiIi_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiIi_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], argv[1], - GET_I64_FROM_ADDR(argv + 2), argv[4]); + int64 (*native_code)(WASMExecEnv *, int32, int32, int64, int32) = func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iiII_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiII_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], argv[1], GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4)); + void (*native_code)(WASMExecEnv *, int32, int32, int64, int64) = func_ptr; + native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4)); } static void -invoke_iiII_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiII_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32, int64, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4)); +} +static void +invoke_iiII_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int64 (*native_code)(WASMExecEnv *, int32, int32, int64, int64) = func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1], + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4)); + PUT_I64_TO_ADDR(argv_ret, ret); +} + +static void +invoke_iIii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + void (*native_code)(WASMExecEnv *, int32, int64, int32, int32) = func_ptr; + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + argv[3], argv[4]); +} +static void +invoke_iIii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int32 (*native_code)(WASMExecEnv *, int32, int64, int32, int32) = func_ptr; argv_ret[0] = - native_code(exec_env, argv[0], argv[1], GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4)); + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + argv[3], argv[4]); } static void -invoke_iiII_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iIii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = - native_code(exec_env, argv[0], argv[1], GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4)); + int64 (*native_code)(WASMExecEnv *, int32, int64, int32, int32) = func_ptr; + int64 ret = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + argv[3], argv[4]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iIii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iIiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), argv[3], + void (*native_code)(WASMExecEnv *, int32, int64, int32, int64) = func_ptr; + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + argv[3], GET_I64_FROM_ADDR((uint32 *)argv + 4)); +} +static void +invoke_iIiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int32 (*native_code)(WASMExecEnv *, int32, int64, int32, int64) = func_ptr; + argv_ret[0] = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + argv[3], GET_I64_FROM_ADDR((uint32 *)argv + 4)); +} +static void +invoke_iIiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int64 (*native_code)(WASMExecEnv *, int32, int64, int32, int64) = func_ptr; + int64 ret = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + argv[3], GET_I64_FROM_ADDR((uint32 *)argv + 4)); + PUT_I64_TO_ADDR(argv_ret, ret); +} + +static void +invoke_iIIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + void (*native_code)(WASMExecEnv *, int32, int64, int64, int32) = func_ptr; + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3), argv[5]); +} +static void +invoke_iIIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int32 (*native_code)(WASMExecEnv *, int32, int64, int64, int32) = func_ptr; + argv_ret[0] = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3), argv[5]); +} +static void +invoke_iIIi_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int64 (*native_code)(WASMExecEnv *, int32, int64, int64, int32) = func_ptr; + int64 ret = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3), argv[5]); + PUT_I64_TO_ADDR(argv_ret, ret); +} + +static void +invoke_iIII_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + void (*native_code)(WASMExecEnv *, int32, int64, int64, int64) = func_ptr; + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3), + GET_I64_FROM_ADDR((uint32 *)argv + 5)); +} +static void +invoke_iIII_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int32 (*native_code)(WASMExecEnv *, int32, int64, int64, int64) = func_ptr; + argv_ret[0] = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3), + GET_I64_FROM_ADDR((uint32 *)argv + 5)); +} +static void +invoke_iIII_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int64 (*native_code)(WASMExecEnv *, int32, int64, int64, int64) = func_ptr; + int64 ret = + native_code(exec_env, argv[0], GET_I64_FROM_ADDR((uint32 *)argv + 1), + GET_I64_FROM_ADDR((uint32 *)argv + 3), + GET_I64_FROM_ADDR((uint32 *)argv + 5)); + PUT_I64_TO_ADDR(argv_ret, ret); +} + +static void +invoke_Iiii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + void (*native_code)(WASMExecEnv *, int64, int32, int32, int32) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], argv[3], argv[4]); } static void -invoke_iIii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Iiii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - argv[3], argv[4]); + int32 (*native_code)(WASMExecEnv *, int64, int32, int32, int32) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], argv[3], argv[4]); } static void -invoke_iIii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_Iiii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - argv[3], argv[4]); + int64 (*native_code)(WASMExecEnv *, int64, int32, int32, int32) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], argv[3], argv[4]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iIiI_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), argv[3], - GET_I64_FROM_ADDR(argv + 4)); -} -static void -invoke_iIiI_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - argv[3], GET_I64_FROM_ADDR(argv + 4)); -} -static void -invoke_iIiI_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - argv[3], GET_I64_FROM_ADDR(argv + 4)); - PUT_I64_TO_ADDR(argv_ret, ret); + void (*native_code)(WASMExecEnv *, int64, int32, int32, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], argv[3], + GET_I64_FROM_ADDR((uint32 *)argv + 4)); } static void -invoke_iIIi_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3), argv[5]); -} -static void -invoke_iIIi_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3), argv[5]); -} -static void -invoke_iIIi_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3), argv[5]); - PUT_I64_TO_ADDR(argv_ret, ret); -} - -static void -invoke_iIII_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - void (*native_code)() = func_ptr; - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3), GET_I64_FROM_ADDR(argv + 5)); -} -static void -invoke_iIII_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int64, int32, int32, int64) = func_ptr; argv_ret[0] = - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3), GET_I64_FROM_ADDR(argv + 5)); + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], + argv[3], GET_I64_FROM_ADDR((uint32 *)argv + 4)); } + static void -invoke_iIII_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = - native_code(exec_env, argv[0], GET_I64_FROM_ADDR(argv + 1), - GET_I64_FROM_ADDR(argv + 3), GET_I64_FROM_ADDR(argv + 5)); + int64 (*native_code)(WASMExecEnv *, int64, int32, int32, int64) = func_ptr; + int64 ret = + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], + argv[3], GET_I64_FROM_ADDR((uint32 *)argv + 4)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_Iiii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], argv[3], argv[4]); + void (*native_code)(WASMExecEnv *, int64, int32, int64, int32) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], + GET_I64_FROM_ADDR((uint32 *)argv + 3), argv[5]); } static void -invoke_Iiii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - argv[3], argv[4]); -} -static void -invoke_Iiii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - argv[3], argv[4]); - PUT_I64_TO_ADDR(argv_ret, ret); -} - -static void -invoke_IiiI_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], argv[3], - GET_I64_FROM_ADDR(argv + 4)); -} - -static void -invoke_IiiI_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - argv[3], GET_I64_FROM_ADDR(argv + 4)); -} - -static void -invoke_IiiI_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - argv[3], GET_I64_FROM_ADDR(argv + 4)); - PUT_I64_TO_ADDR(argv_ret, ret); -} - -static void -invoke_IiIi_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3), argv[5]); -} -static void -invoke_IiIi_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3), argv[5]); -} -static void -invoke_IiIi_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3), argv[5]); - PUT_I64_TO_ADDR(argv_ret, ret); -} - -static void -invoke_IiII_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3), GET_I64_FROM_ADDR(argv + 5)); -} -static void -invoke_IiII_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) -{ - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int64, int32, int64, int32) = func_ptr; argv_ret[0] = - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3), GET_I64_FROM_ADDR(argv + 5)); + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], + GET_I64_FROM_ADDR((uint32 *)argv + 3), argv[5]); } static void -invoke_IiII_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiIi_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = - native_code(exec_env, GET_I64_FROM_ADDR(argv), argv[2], - GET_I64_FROM_ADDR(argv + 3), GET_I64_FROM_ADDR(argv + 5)); + int64 (*native_code)(WASMExecEnv *, int64, int32, int64, int32) = func_ptr; + int64 ret = + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], + GET_I64_FROM_ADDR((uint32 *)argv + 3), argv[5]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_IIii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiII_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - argv[4], argv[5]); + void (*native_code)(WASMExecEnv *, int64, int32, int64, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), argv[2], + GET_I64_FROM_ADDR((uint32 *)argv + 3), + GET_I64_FROM_ADDR((uint32 *)argv + 5)); } static void -invoke_IIii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiII_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), argv[4], argv[5]); + int32 (*native_code)(WASMExecEnv *, int64, int32, int64, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], GET_I64_FROM_ADDR((uint32 *)argv + 3), + GET_I64_FROM_ADDR((uint32 *)argv + 5)); } static void -invoke_IIii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IiII_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), argv[4], argv[5]); + int64 (*native_code)(WASMExecEnv *, int64, int32, int64, int64) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + argv[2], GET_I64_FROM_ADDR((uint32 *)argv + 3), + GET_I64_FROM_ADDR((uint32 *)argv + 5)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_IIiI_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - argv[4], GET_I64_FROM_ADDR(argv + 5)); + void (*native_code)(WASMExecEnv *, int64, int64, int32, int32) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4], argv[5]); } static void -invoke_IIiI_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), argv[4], - GET_I64_FROM_ADDR(argv + 5)); + int32 (*native_code)(WASMExecEnv *, int64, int64, int32, int32) = func_ptr; + argv_ret[0] = + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4], argv[5]); } static void -invoke_IIiI_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), argv[4], - GET_I64_FROM_ADDR(argv + 5)); + int64 (*native_code)(WASMExecEnv *, int64, int64, int32, int32) = func_ptr; + int64 ret = + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4], argv[5]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_IIIi_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIiI_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4), argv[6]); + void (*native_code)(WASMExecEnv *, int64, int64, int32, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4], + GET_I64_FROM_ADDR((uint32 *)argv + 5)); } static void -invoke_IIIi_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIiI_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4), argv[6]); + int32 (*native_code)(WASMExecEnv *, int64, int64, int32, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4], + GET_I64_FROM_ADDR((uint32 *)argv + 5)); } static void -invoke_IIIi_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIiI_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code(exec_env, GET_I64_FROM_ADDR(argv), - GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4), argv[6]); + int64 (*native_code)(WASMExecEnv *, int64, int64, int32, int64) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), argv[4], + GET_I64_FROM_ADDR((uint32 *)argv + 5)); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_IIII_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIIi_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; - native_code(exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4), GET_I64_FROM_ADDR(argv + 6)); + void (*native_code)(WASMExecEnv *, int64, int64, int64, int32) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4), argv[6]); } static void -invoke_IIII_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIIi_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; - argv_ret[0] = native_code( - exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4), GET_I64_FROM_ADDR(argv + 6)); + int32 (*native_code)(WASMExecEnv *, int64, int64, int64, int32) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4), argv[6]); } static void -invoke_IIII_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIIi_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = native_code( - exec_env, GET_I64_FROM_ADDR(argv), GET_I64_FROM_ADDR(argv + 2), - GET_I64_FROM_ADDR(argv + 4), GET_I64_FROM_ADDR(argv + 6)); + int64 (*native_code)(WASMExecEnv *, int64, int64, int64, int32) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4), argv[6]); PUT_I64_TO_ADDR(argv_ret, ret); } static void -invoke_iiiii_v(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_IIII_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - void (*native_code)() = func_ptr; + void (*native_code)(WASMExecEnv *, int64, int64, int64, int64) = func_ptr; + native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4), + GET_I64_FROM_ADDR((uint32 *)argv + 6)); +} +static void +invoke_IIII_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int32 (*native_code)(WASMExecEnv *, int64, int64, int64, int64) = func_ptr; + argv_ret[0] = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4), + GET_I64_FROM_ADDR((uint32 *)argv + 6)); +} +static void +invoke_IIII_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + int64 (*native_code)(WASMExecEnv *, int64, int64, int64, int64) = func_ptr; + int64 ret = native_code(exec_env, GET_I64_FROM_ADDR((uint32 *)argv), + GET_I64_FROM_ADDR((uint32 *)argv + 2), + GET_I64_FROM_ADDR((uint32 *)argv + 4), + GET_I64_FROM_ADDR((uint32 *)argv + 6)); + PUT_I64_TO_ADDR(argv_ret, ret); +} + +static void +invoke_iiiii_v(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) +{ + void (*native_code)(WASMExecEnv *, int32, int32, int32, int32, int32) = + func_ptr; native_code(exec_env, argv[0], argv[1], argv[2], argv[3], argv[4]); } static void -invoke_iiiii_i(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiiii_i(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint32 (*native_code)() = func_ptr; + int32 (*native_code)(WASMExecEnv *, int32, int32, int32, int32, int32) = + func_ptr; argv_ret[0] = native_code(exec_env, argv[0], argv[1], argv[2], argv[3], argv[4]); } static void -invoke_iiiii_I(void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) +invoke_iiiii_I(void *func_ptr, void *exec_env, int32 *argv, int32 *argv_ret) { - uint64 (*native_code)() = func_ptr; - uint64 ret = + int64 (*native_code)(WASMExecEnv *, int32, int32, int32, int32, int32) = + func_ptr; + int64 ret = native_code(exec_env, argv[0], argv[1], argv[2], argv[3], argv[4]); PUT_I64_TO_ADDR(argv_ret, ret); } diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index c5e8879ee..67a6237dc 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -4207,14 +4207,11 @@ llvm_jit_call_func_bytecode(WASMModuleInstance *module_inst, #if WASM_ENABLE_QUICK_AOT_ENTRY != 0 /* Quick call if the quick jit entry is registered */ if (func_type->quick_aot_entry) { - void (*invoke_native)( - void *func_ptr, uint8 ret_type, void *exec_env, uint32 *argv, - uint32 *argv_ret) = func_type->quick_aot_entry; - invoke_native(module_inst->func_ptrs[func_idx], - func_type->result_count > 0 - ? func_type->types[func_type->param_count] - : VALUE_TYPE_VOID, - exec_env, argv, argv); + void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv, + uint32 *argv_ret) = + func_type->quick_aot_entry; + invoke_native(module_inst->func_ptrs[func_idx], exec_env, argv, + argv); ret = !wasm_copy_exception(module_inst, NULL); } else From 8b37048823677d9229b7458b064a515b19744c9c Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 19 Jan 2024 10:55:01 +0900 Subject: [PATCH 26/53] Do not inherit WASM_SUSPEND_FLAG_BLOCKING from the parent thread (#3051) It seems that some users want to wrap rather large chunk of code with wasm_runtime_begin_blocking_op/wasm_runtime_end_blocking_op. If the wrapped code happens to have a call to e.g. wasm_runtime_spawn_exec_env, WASM_SUSPEND_FLAG_BLOCKING is inherited to the child exec_env and it may cause unexpected behaviors. --- core/iwasm/common/wasm_suspend_flags.h | 2 ++ core/iwasm/libraries/thread-mgr/thread_manager.c | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/iwasm/common/wasm_suspend_flags.h b/core/iwasm/common/wasm_suspend_flags.h index b182b2b5f..92661b7bd 100644 --- a/core/iwasm/common/wasm_suspend_flags.h +++ b/core/iwasm/common/wasm_suspend_flags.h @@ -35,6 +35,8 @@ typedef union WASMSuspendFlags { #define WASM_SUSPEND_FLAGS_FETCH_AND(s_flags, val) \ BH_ATOMIC_32_FETCH_AND(s_flags.flags, val) +#define WASM_SUSPEND_FLAG_INHERIT_MASK (~WASM_SUSPEND_FLAG_BLOCKING) + #if WASM_SUSPEND_FLAGS_IS_ATOMIC != 0 #define WASM_SUSPEND_FLAGS_LOCK(lock) (void)0 #define WASM_SUSPEND_FLAGS_UNLOCK(lock) (void)0 diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index a3fc6fe92..377983a69 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -574,7 +574,8 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) } /* Inherit suspend_flags of parent thread */ - new_exec_env->suspend_flags.flags = exec_env->suspend_flags.flags; + new_exec_env->suspend_flags.flags = + (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK); if (!wasm_cluster_add_exec_env(cluster, new_exec_env)) { goto fail4; @@ -729,7 +730,8 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env, } /* Inherit suspend_flags of parent thread */ - new_exec_env->suspend_flags.flags = exec_env->suspend_flags.flags; + new_exec_env->suspend_flags.flags = + (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK); if (!wasm_cluster_add_exec_env(cluster, new_exec_env)) goto fail2; From 54ef0e4de77d3758d564c188feb570029445c9a0 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 19 Jan 2024 11:24:48 +0800 Subject: [PATCH 27/53] Bump NuttX version to 12.4.x in CI (#3047) --- .github/workflows/compilation_on_nuttx.yml | 6 +++--- .github/workflows/spec_test_on_nuttx.yml | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/compilation_on_nuttx.yml b/.github/workflows/compilation_on_nuttx.yml index 8e1f4aad0..5368b06a1 100644 --- a/.github/workflows/compilation_on_nuttx.yml +++ b/.github/workflows/compilation_on_nuttx.yml @@ -52,7 +52,7 @@ jobs: build_iwasm_on_nuttx: runs-on: ubuntu-latest container: - image: ghcr.io/apache/nuttx/apache-nuttx-ci-linux@sha256:4b4cbf0b70512e61ada9cdcb76b97e90ad478b85e4d0774d05a95fa32caa8c39 + image: ghcr.io/apache/nuttx/apache-nuttx-ci-linux@sha256:d9261eacf6c6ebe656c571757751c803e8f04c3ae9b820320a5ea5dd57b7205a strategy: matrix: @@ -90,14 +90,14 @@ jobs: uses: actions/checkout@v3 with: repository: apache/incubator-nuttx - ref: releases/12.3 + ref: releases/12.4 path: nuttx - name: Checkout NuttX Apps uses: actions/checkout@v3 with: repository: apache/incubator-nuttx-apps - ref: releases/12.3 + ref: releases/12.4 path: apps - name: Checkout WAMR diff --git a/.github/workflows/spec_test_on_nuttx.yml b/.github/workflows/spec_test_on_nuttx.yml index 957f82f34..864bb0b7c 100644 --- a/.github/workflows/spec_test_on_nuttx.yml +++ b/.github/workflows/spec_test_on_nuttx.yml @@ -28,13 +28,13 @@ jobs: with: os: "ubuntu-22.04" arch: "ARM RISCV AArch64" - container_image: ghcr.io/apache/nuttx/apache-nuttx-ci-linux@sha256:4b4cbf0b70512e61ada9cdcb76b97e90ad478b85e4d0774d05a95fa32caa8c39 + container_image: ghcr.io/apache/nuttx/apache-nuttx-ci-linux@sha256:d9261eacf6c6ebe656c571757751c803e8f04c3ae9b820320a5ea5dd57b7205a spec_test_on_qemu: runs-on: ubuntu-latest needs: [build_llvm_libraries] container: - image: ghcr.io/apache/nuttx/apache-nuttx-ci-linux@sha256:4b4cbf0b70512e61ada9cdcb76b97e90ad478b85e4d0774d05a95fa32caa8c39 + image: ghcr.io/apache/nuttx/apache-nuttx-ci-linux@sha256:d9261eacf6c6ebe656c571757751c803e8f04c3ae9b820320a5ea5dd57b7205a strategy: matrix: target_config: [ @@ -99,14 +99,14 @@ jobs: uses: actions/checkout@v3 with: repository: apache/incubator-nuttx - ref: releases/12.3 + ref: releases/12.4 path: nuttx - name: Checkout NuttX Apps uses: actions/checkout@v3 with: repository: apache/incubator-nuttx-apps - ref: releases/12.3 + ref: releases/12.4 path: apps - name: Checkout WAMR From a7545df5d015951871a4ba2f31269c706cdefd86 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 19 Jan 2024 12:31:18 +0800 Subject: [PATCH 28/53] classic-interp: Handle SIMD opcode when JIT is enabled (#3046) Though SIMD isn't supported by interpreter, when JIT is enabled, developer may run `iwasm --interp ` to trigger the SIMD opcode in interpreter, which isn't handled before this PR. --- core/iwasm/interpreter/wasm_interp_classic.c | 6 ++++++ core/iwasm/interpreter/wasm_opcode.h | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 67a6237dc..8c3f0ae6f 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -3843,6 +3843,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP(WASM_OP_REF_NULL) HANDLE_OP(WASM_OP_REF_IS_NULL) HANDLE_OP(WASM_OP_REF_FUNC) +#endif +#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SIMD != 0 + /* SIMD isn't supported by interpreter, but when JIT is + enabled, `iwasm --interp ` may be run to + trigger the SIMD opcode in interpreter */ + HANDLE_OP(WASM_OP_SIMD_PREFIX) #endif HANDLE_OP(WASM_OP_UNUSED_0x14) HANDLE_OP(WASM_OP_UNUSED_0x15) diff --git a/core/iwasm/interpreter/wasm_opcode.h b/core/iwasm/interpreter/wasm_opcode.h index ce5e358a2..276d15c3d 100644 --- a/core/iwasm/interpreter/wasm_opcode.h +++ b/core/iwasm/interpreter/wasm_opcode.h @@ -683,6 +683,13 @@ typedef enum WASMAtomicEXTOpcode { #define SET_GOTO_TABLE_ELEM(opcode) [opcode] = HANDLE_OPCODE(opcode) +#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SIMD != 0 +#define SET_GOTO_TABLE_SIMD_PREFIX_ELEM() \ + SET_GOTO_TABLE_ELEM(WASM_OP_SIMD_PREFIX), +#else +#define SET_GOTO_TABLE_SIMD_PREFIX_ELEM() +#endif + /* * Macro used to generate computed goto tables for the C interpreter. */ @@ -906,6 +913,7 @@ typedef enum WASMAtomicEXTOpcode { HANDLE_OPCODE(EXT_OP_IF), /* 0xd5 */ \ HANDLE_OPCODE(EXT_OP_BR_TABLE_CACHE), /* 0xd6 */ \ SET_GOTO_TABLE_ELEM(WASM_OP_MISC_PREFIX), /* 0xfc */ \ + SET_GOTO_TABLE_SIMD_PREFIX_ELEM() /* 0xfd */ \ SET_GOTO_TABLE_ELEM(WASM_OP_ATOMIC_PREFIX), /* 0xfe */ \ DEF_DEBUG_BREAK_HANDLE() \ }; From 17b0111577dd083856a17bbaaea5a470276509b4 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 19 Jan 2024 19:31:21 +0900 Subject: [PATCH 29/53] wasm_runtime_begin_blocking_op: A comment about usage expectation (#3056) --- core/iwasm/include/wasm_export.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 4a23b8d19..dda058d08 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -1607,6 +1607,9 @@ wasm_runtime_get_context(wasm_module_inst_t inst, void *key); * host functions. It wraps an operation which possibly blocks for long * to prepare for async termination. * + * For simplicity, we recommend to wrap only the very minimum piece of + * the code with this. Ideally, just a single system call. + * * eg. * * if (!wasm_runtime_begin_blocking_op(exec_env)) { From ec6d9cb6bea6297a4ed2f4eac26e14bab5d375a0 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 19 Jan 2024 19:29:21 +0800 Subject: [PATCH 30/53] Check arguments before calling bh_hash_map_find (#3055) Check whether the arguments are NULL before calling bh_hash_map_find, or lots of "HashMap find elem failed: map or key is NULL" warnings may be dumped. Reported in #3053. --- core/iwasm/common/wasm_shared_memory.c | 5 +++-- .../libraries/lib-pthread/lib_pthread_wrapper.c | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/core/iwasm/common/wasm_shared_memory.c b/core/iwasm/common/wasm_shared_memory.c index 70e84a375..e7110c0f1 100644 --- a/core/iwasm/common/wasm_shared_memory.c +++ b/core/iwasm/common/wasm_shared_memory.c @@ -176,8 +176,9 @@ acquire_wait_info(void *address, AtomicWaitNode *wait_node) AtomicWaitInfo *wait_info = NULL; bh_list_status ret; - if (address) - wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address); + bh_assert(address != NULL); + + wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address); if (!wait_node) { return wait_info; diff --git a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c index a178306b2..3092f5d03 100644 --- a/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c +++ b/core/iwasm/libraries/lib-pthread/lib_pthread_wrapper.c @@ -468,7 +468,7 @@ get_thread_info(wasm_exec_env_t exec_env, uint32 handle) WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); ClusterInfoNode *info = get_cluster_info(cluster); - if (!info) { + if (!info || !handle) { return NULL; } @@ -1144,6 +1144,10 @@ sem_open_wrapper(wasm_exec_env_t exec_env, const char *name, int32 oflags, * For Unix like system, it's dedicated for multiple processes. */ + if (!name) { /* avoid passing NULL to bh_hash_map_find and os_sem_open */ + return -1; + } + if ((info_node = bh_hash_map_find(sem_info_map, (void *)name))) { return info_node->handle; } @@ -1276,7 +1280,13 @@ sem_unlink_wrapper(wasm_exec_env_t exec_env, const char *name) (void)exec_env; int32 ret_val; - ThreadInfoNode *info_node = bh_hash_map_find(sem_info_map, (void *)name); + ThreadInfoNode *info_node; + + if (!name) { /* avoid passing NULL to bh_hash_map_find */ + return -1; + } + + info_node = bh_hash_map_find(sem_info_map, (void *)name); if (!info_node || info_node->type != T_SEM) return -1; From bc35602004657857d04455c2ef8b5212c5907d8d Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Fri, 19 Jan 2024 20:43:41 +0900 Subject: [PATCH 31/53] Fix aot large model (--size-level=0) with LLVM 18 (#3057) The recent versions LLVM uses ".ltext" section for X86 large model. cf. https://github.com/llvm/llvm-project/commit/d8a04398f9492f043ffd8fbaf2458778f7d0fcd5 This fixes https://github.com/bytecodealliance/wasm-micro-runtime/issues/3034 --- core/iwasm/aot/aot_loader.c | 12 +++++- core/iwasm/aot/debug/elf_parser.c | 19 +++++++-- core/iwasm/compilation/aot_emit_aot_file.c | 48 +++++++++++++++++----- 3 files changed, 63 insertions(+), 16 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 74513d04e..d57f00930 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -1975,6 +1975,12 @@ str2uint64(const char *buf, uint64 *p_res) #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative offset to GOT */ +static bool +is_text_section(const char *section_name) +{ + return !strcmp(section_name, ".text") || !strcmp(section_name, ".ltext"); +} + static bool do_text_relocation(AOTModule *module, AOTRelocationGroup *group, char *error_buf, uint32 error_buf_size) @@ -2063,7 +2069,7 @@ do_text_relocation(AOTModule *module, AOTRelocationGroup *group, symbol_addr = module->func_ptrs[func_index]; } #endif - else if (!strcmp(symbol, ".text")) { + else if (is_text_section(symbol)) { symbol_addr = module->code; } else if (!strcmp(symbol, ".data") || !strcmp(symbol, ".sdata") @@ -2235,7 +2241,7 @@ do_data_relocation(AOTModule *module, AOTRelocationGroup *group, for (i = 0; i < group->relocation_count; i++, relocation++) { symbol = relocation->symbol_name; - if (!strcmp(symbol, ".text")) { + if (is_text_section(symbol)) { symbol_addr = module->code; } #if WASM_ENABLE_STATIC_PGO != 0 @@ -2696,6 +2702,8 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, if (!strcmp(group->section_name, ".rel.text") || !strcmp(group->section_name, ".rela.text") + || !strcmp(group->section_name, ".rel.ltext") + || !strcmp(group->section_name, ".rela.ltext") || !strcmp(group->section_name, ".rela.literal") #ifdef BH_PLATFORM_WINDOWS || !strcmp(group->section_name, ".text") diff --git a/core/iwasm/aot/debug/elf_parser.c b/core/iwasm/aot/debug/elf_parser.c index 9fec281d3..657f9530c 100644 --- a/core/iwasm/aot/debug/elf_parser.c +++ b/core/iwasm/aot/debug/elf_parser.c @@ -100,6 +100,12 @@ get_section64(Elf64_Ehdr *eh, Elf64_Shdr *section_header) return buf + section_header->sh_offset; } +static bool +is_text_section(const char *section_name) +{ + return !strcmp(section_name, ".text") || !strcmp(section_name, ".ltext"); +} + bool get_text_section(void *buf, uint64_t *offset, uint64_t *size) { @@ -107,6 +113,7 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size) uint32 i; char *sh_str; + /* Assumption: Only one of .text or .ltext is non-empty. */ if (is64Bit(buf)) { Elf64_Ehdr *eh = (Elf64_Ehdr *)buf; Elf64_Shdr **sh_table = @@ -115,14 +122,16 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size) read_section_header_table64(eh, sh_table); sh_str = get_section64(eh, sh_table[eh->e_shstrndx]); for (i = 0; i < eh->e_shnum; i++) { - if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) { + if (is_text_section(sh_str + sh_table[i]->sh_name)) { *offset = sh_table[i]->sh_offset; *size = sh_table[i]->sh_size; sh_table[i]->sh_addr = (Elf64_Addr)(uintptr_t)((char *)buf + sh_table[i]->sh_offset); ret = true; - break; + if (*size > 0) { + break; + } } } wasm_runtime_free(sh_table); @@ -136,14 +145,16 @@ get_text_section(void *buf, uint64_t *offset, uint64_t *size) read_section_header_table(eh, sh_table); sh_str = get_section(eh, sh_table[eh->e_shstrndx]); for (i = 0; i < eh->e_shnum; i++) { - if (!strcmp(sh_str + sh_table[i]->sh_name, ".text")) { + if (is_text_section(sh_str + sh_table[i]->sh_name)) { *offset = sh_table[i]->sh_offset; *size = sh_table[i]->sh_size; sh_table[i]->sh_addr = (Elf32_Addr)(uintptr_t)((char *)buf + sh_table[i]->sh_offset); ret = true; - break; + if (*size > 0) { + break; + } } } wasm_runtime_free(sh_table); diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index 9b5fa47ed..d674b022c 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -654,7 +654,8 @@ get_relocations_size(AOTObjectData *obj_data, /* ignore the relocations to aot_func_internal#n in text section for windows platform since they will be applied in aot_emit_text_section */ - if (!strcmp(relocation_group->section_name, ".text") + if ((!strcmp(relocation_group->section_name, ".text") + || !strcmp(relocation_group->section_name, ".ltext")) && !strncmp(relocation->symbol_name, AOT_FUNC_INTERNAL_PREFIX, strlen(AOT_FUNC_INTERNAL_PREFIX)) && ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6) @@ -1819,7 +1820,8 @@ aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, for (i = 0; i < obj_data->relocation_group_count; i++, relocation_group++) { /* relocation in text section */ - if (!strcmp(relocation_group->section_name, ".text")) { + if ((!strcmp(relocation_group->section_name, ".text") + || !strcmp(relocation_group->section_name, ".ltext"))) { relocation = relocation_group->relocations; relocation_count = relocation_group->relocation_count; for (j = 0; j < relocation_count; j++) { @@ -2380,17 +2382,19 @@ aot_resolve_text(AOTObjectData *obj_data) while ( !LLVMObjectFileIsSectionIteratorAtEnd(obj_data->binary, sec_itr)) { if ((name = (char *)LLVMGetSectionName(sec_itr))) { - if (!strcmp(name, ".text")) { + if (!strcmp(name, ".text") || !strcmp(name, ".ltext")) { obj_data->text = (char *)LLVMGetSectionContents(sec_itr); obj_data->text_size = (uint32)LLVMGetSectionSize(sec_itr); } - else if (!strcmp(name, ".text.unlikely.")) { + else if (!strcmp(name, ".text.unlikely.") + || !strcmp(name, ".ltext.unlikely.")) { obj_data->text_unlikely = (char *)LLVMGetSectionContents(sec_itr); obj_data->text_unlikely_size = (uint32)LLVMGetSectionSize(sec_itr); } - else if (!strcmp(name, ".text.hot.")) { + else if (!strcmp(name, ".text.hot.") + || !strcmp(name, ".ltext.hot.")) { obj_data->text_hot = (char *)LLVMGetSectionContents(sec_itr); obj_data->text_hot_size = @@ -2909,11 +2913,13 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data) (char *)LLVMGetSectionName(contain_section); LLVMDisposeSectionIterator(contain_section); - if (!strcmp(contain_section_name, ".text.unlikely.")) { + if (!strcmp(contain_section_name, ".text.unlikely.") + || !strcmp(contain_section_name, ".ltext.unlikely.")) { func->text_offset = align_uint(obj_data->text_size, 4) + LLVMGetSymbolAddress(sym_itr); } - else if (!strcmp(contain_section_name, ".text.hot.")) { + else if (!strcmp(contain_section_name, ".text.hot.") + || !strcmp(contain_section_name, ".ltext.hot.")) { func->text_offset = align_uint(obj_data->text_size, 4) + align_uint(obj_data->text_unlikely_size, 4) @@ -2945,12 +2951,14 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data) (char *)LLVMGetSectionName(contain_section); LLVMDisposeSectionIterator(contain_section); - if (!strcmp(contain_section_name, ".text.unlikely.")) { + if (!strcmp(contain_section_name, ".text.unlikely.") + || !strcmp(contain_section_name, ".ltext.unlikely.")) { func->text_offset_of_aot_func_internal = align_uint(obj_data->text_size, 4) + LLVMGetSymbolAddress(sym_itr); } - else if (!strcmp(contain_section_name, ".text.hot.")) { + else if (!strcmp(contain_section_name, ".text.hot.") + || !strcmp(contain_section_name, ".ltext.hot.")) { func->text_offset_of_aot_func_internal = align_uint(obj_data->text_size, 4) + align_uint(obj_data->text_unlikely_size, 4) @@ -3211,6 +3219,12 @@ is_relocation_section_name(AOTObjectData *obj_data, char *section_name) || !strcmp(section_name, ".rel.text.unlikely.") || !strcmp(section_name, ".rela.text.hot.") || !strcmp(section_name, ".rel.text.hot.") + || !strcmp(section_name, ".rela.ltext") + || !strcmp(section_name, ".rel.ltext") + || !strcmp(section_name, ".rela.ltext.unlikely.") + || !strcmp(section_name, ".rel.ltext.unlikely.") + || !strcmp(section_name, ".rela.ltext.hot.") + || !strcmp(section_name, ".rel.ltext.hot.") || !strcmp(section_name, ".rela.literal") || !strcmp(section_name, ".rela.data") || !strcmp(section_name, ".rel.data") @@ -3249,7 +3263,9 @@ static bool is_readonly_section(const char *name) { return !strcmp(name, ".rel.text") || !strcmp(name, ".rela.text") - || !strcmp(name, ".rela.literal") || !strcmp(name, ".text"); + || !strcmp(name, ".rel.ltext") || !strcmp(name, ".rela.ltext") + || !strcmp(name, ".rela.literal") || !strcmp(name, ".text") + || !strcmp(name, ".ltext"); } static bool @@ -3342,12 +3358,24 @@ aot_resolve_object_relocation_groups(AOTObjectData *obj_data) || !strcmp(relocation_group->section_name, ".rela.text.hot.")) { relocation_group->section_name = ".rela.text"; } + else if (!strcmp(relocation_group->section_name, + ".rela.ltext.unlikely.") + || !strcmp(relocation_group->section_name, + ".rela.ltext.hot.")) { + relocation_group->section_name = ".rela.ltext"; + } else if (!strcmp(relocation_group->section_name, ".rel.text.unlikely.") || !strcmp(relocation_group->section_name, ".rel.text.hot.")) { relocation_group->section_name = ".rel.text"; } + else if (!strcmp(relocation_group->section_name, + ".rel.ltext.unlikely.") + || !strcmp(relocation_group->section_name, + ".rel.ltext.hot.")) { + relocation_group->section_name = ".rel.ltext"; + } /* * Relocations in read-only sections are problematic, From 1977ad23efb3b3f2d4ae0ddf49096a05464f488f Mon Sep 17 00:00:00 2001 From: Xu Jun Date: Fri, 19 Jan 2024 19:58:12 +0800 Subject: [PATCH 32/53] fast-interp: Fix dynamic offset error issue in else branch (#3058) Reported in https://github.com/bytecodealliance/wasm-micro-runtime/issues/3026. --- core/iwasm/interpreter/wasm_loader.c | 9 +++++++++ core/iwasm/interpreter/wasm_mini_loader.c | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index f02f23d83..2f98d0277 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -7400,6 +7400,15 @@ re_scan: bh_memcpy_s(loader_ctx->frame_offset, size, block->param_frame_offsets, size); loader_ctx->frame_offset += (size / sizeof(int16)); + + /* recover dynamic offset */ + for (i = 0; i < block->available_param_num; i++) { + if (block->param_frame_offsets[i] + >= loader_ctx->dynamic_offset) { + loader_ctx->dynamic_offset = + block->param_frame_offsets[i] + 1; + } + } } #endif diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index b6d81aa2f..857212e17 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -5813,6 +5813,15 @@ re_scan: bh_memcpy_s(loader_ctx->frame_offset, size, block->param_frame_offsets, size); loader_ctx->frame_offset += (size / sizeof(int16)); + + /* recover dynamic offset */ + for (i = 0; i < block->available_param_num; i++) { + if (block->param_frame_offsets[i] + >= loader_ctx->dynamic_offset) { + loader_ctx->dynamic_offset = + block->param_frame_offsets[i] + 1; + } + } } #endif From 627debcefb1324a13d4f360a4346964fcde62dc5 Mon Sep 17 00:00:00 2001 From: Marcin Kolny Date: Sun, 21 Jan 2024 23:33:26 +0000 Subject: [PATCH 33/53] Add flag to control Winsocket initialization (#3060) When WAMR is embedded to other application, the lifecycle of the socket might conflict with other usecases. E.g. if WAMR is deinitialized before any other use of sockets, the application goes into the invalid state. The new flag allows host application to take control over the socket initialization. --- core/config.h | 11 +++++++++++ core/shared/platform/windows/win_socket.c | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/core/config.h b/core/config.h index ed8af92f2..ef322647f 100644 --- a/core/config.h +++ b/core/config.h @@ -231,6 +231,17 @@ #define WASM_ENABLE_LOG 1 #endif +/* When this flag is set, WAMR will not automatically + * initialize sockets on Windows platforms. The host + * application is responsible for calling WSAStartup() + * before executing WAMR code that uses sockets, and + * calling WSACleanup() after. + * This flag passes control of socket initialization from + * WAMR to the host application. */ +#ifndef WASM_ENABLE_HOST_SOCKET_INIT +#define WASM_ENABLE_HOST_SOCKET_INIT 0 +#endif + #ifndef WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS #if defined(BUILD_TARGET_X86_32) || defined(BUILD_TARGET_X86_64) \ || defined(BUILD_TARGET_AARCH64) diff --git a/core/shared/platform/windows/win_socket.c b/core/shared/platform/windows/win_socket.c index 91d38fd8b..b19e5b097 100644 --- a/core/shared/platform/windows/win_socket.c +++ b/core/shared/platform/windows/win_socket.c @@ -32,6 +32,7 @@ static bool is_winsock_inited = false; int init_winsock() { +#if WASM_ENABLE_HOST_SOCKET_INIT == 0 WSADATA wsaData; if (!is_winsock_inited) { @@ -42,6 +43,7 @@ init_winsock() is_winsock_inited = true; } +#endif return BHT_OK; } @@ -49,9 +51,11 @@ init_winsock() void deinit_winsock() { +#if WASM_ENABLE_HOST_SOCKET_INIT == 0 if (is_winsock_inited) { WSACleanup(); } +#endif } int From 4bafa40052b3420e99eaef6f5bfd8bde0cfc4b15 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 23 Jan 2024 12:52:25 +0900 Subject: [PATCH 34/53] nuttx: If STACK_GUARD_SIZE is not set, leave it to config.h (#2927) cf. https://github.com/apache/nuttx-apps/pull/2241 --- product-mini/platforms/nuttx/wamr.mk | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 37b633131..341034e51 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -198,9 +198,7 @@ CSRCS += utils.c VPATH += $(IWASM_ROOT)/libraries/debug-engine endif -ifeq ($(CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE),) -CFLAGS += -DWASM_STACK_GUARD_SIZE=0 -else +ifneq ($(CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE),) CFLAGS += -DWASM_STACK_GUARD_SIZE=CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE endif From 7f035d4206217d0bcbc913a35d9de9837ed8120b Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 23 Jan 2024 12:20:28 +0800 Subject: [PATCH 35/53] perf_tune.md: Add refine the calling processes between host and wasm (#3065) --- doc/perf_tune.md | 109 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/doc/perf_tune.md b/doc/perf_tune.md index 5cd631a19..b366f09c0 100644 --- a/doc/perf_tune.md +++ b/doc/perf_tune.md @@ -190,3 +190,112 @@ $ ./FlameGraph/flamegraph.pl out.folded > perf.foo.wasm.svg > > Then you will see a new file named _out.folded.translated_ which contains the translated folded stacks. > All wasm functions are translated to its original names with a prefix like "[Wasm]" + +## 8. Refine the calling processes between host native and wasm application + +In some scenarios, there may be lots of callings between host native and wasm application, e.g. frequent callings to AOT/JIT functions from host native or frequent callings to host native from AOT/JIT functions. It is important to refine these calling processes to speedup them, WAMR provides several methods: + +### 8.1 Refine callings to native APIs registered by `wasm_runtime_register_natives` from AOT code + +When wamrc compiles the wasm file to AOT code, it may generate LLVM IR to call the native API from an AOT function, and if it doesn't know the native API's signature, the generated LLVM IR has to call the runtime API `aot_invoke_native` to invoke the native API, which is a relatively slow way. If developer registers native APIs during execution by calling `wasm_runtime_register_natives` or by `iwasm --native-lib=`, then developer can also register native APIs with the same signatures to the AOT compiler by `wamrc --native-lib=`, so as to let the AOT compiler pre-know the native API's signature, and generate optimized LLVM IR to quickly call to the native API. + +The below sample registers an API `int test_add(int, int)` to the AOT compiler: + +```C +/* test_add.c */ + +#include "wasm_export.h" + +static int +test_add_wrapper(wasm_exec_env_t exec_env, int x, int y) { + return 0; /* empty function is enough */ +} + +#define REG_NATIVE_FUNC(func_name, signature) \ + { #func_name, func_name##_wrapper, signature, NULL } + +static NativeSymbol native_symbols[] = { + REG_NATIVE_FUNC(test_add, "(ii)i") +}; + +uint32_t +get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols) +{ + *p_module_name = "env"; + *p_native_symbols = native_symbols; + return sizeof(native_symbols) / sizeof(NativeSymbol); +} +``` +```bash +# build native lib +gcc -O3 -fPIC -shared -I /core/iwasm/include -o libtest_add.so test_add.c +# register native lib to aot compiler +wamrc --native-lib=./libtest_add.so -o +``` + +> Note: no need to do anything for LLVM JIT since the native APIs must have been registered before execution and JIT compiler already knows the native APIs' signatures. + +### 8.2 Refine callings to native APIs registered by wasm-c-api `wasm_instance_new` from AOT code + +In wasm-c-api mode, when the native APIs are registered by `wasm_instance_new(..., imports, ...)`, developer can use `wamrc --invoke-c-api-import` option to generate the AOT file, which treats the unknown import function as wasm-c-api import function and generates optimized LLVM IR to speedup the calling process. + +> Note: no need to do anything for LLVM JIT since the similar flag has been set to JIT compiler in wasm-c-api `wasm_engine_new` when LLVM JIT is enabled. + +### 8.3 Refine callings to AOT/JIT functions from host native + +Currently by default WAMR runtime has registered many quick AOT/JIT entries to speedup the calling processes to call AOT/JIT functions from host native, as long as developer doesn't disable it by using `cmake -DWAMR_BUILD_QUICK_AOT_ENTRY=0` or setting the compiler macro `WASM_ENABLE_QUICK_AOT_ENTRY` to 0 in the makefile. These quick AOT/JIT entries include: + +1. wasm function contains 0 to 4 arguments and 0 to 1 results, with the type of each argument is i32 or i64 and the type of result is i32, i64 or void. These functions are like: + +```C +// no argument +i32 foo(), i64 foo(), void foo() +// one argument, each argument is i32 or i64 +i32 foo(i32/i64), i64 foo(i32/i64), void(i32/i64) +// two arguments, each argument is i32 or i64 +i32 foo(i32/i64, i32/i64), i64 foo(i32/i64, i32/i64), void(i32/i64, i32/i64) +// three arguments, each argument is i32 or i64 +i32 foo(i32/i64, i32/i64, i32/i64), i64 foo(i32/i64, i32/i64, i32/i64), void(i32/i64, i32/i64, i32/i64) +// four arguments, each argument is i32 or i64 +i32 foo(i32/i64, i32/i64, i32/i64, i32/i64) +i64 foo(i32/i64, i32/i64, i32/i64, i32/i64) +void(i32/i64, i32/i64, i32/i64, i32/i64) +``` + +2. wasm function contains 5 arguments and 0 to 1 results, with the type of each argument is i32 and the type of result is i32, i64 or void. These functions are like: + +```C +i32 foo(i32, i32, i32, i32, i32) +i64 foo(i32, i32, i32, i32, i32) +void foo(i32, i32, i32, i32, i32) +``` + +To speedup the calling processes, developer had better ensure that the signatures of the wasm functions to expose are like above, or add some conversions to achieve it. For example, if a wasm function to call is `f32 foo(f32)`, developer can define a new function `i32 foo1(i32)` like below and export it: +```C +int32 foo1(int32 arg_i32) +{ + float arg_f32 = *(float *)&arg_i32; + float res_f32 = foo(f32); + int32 res_i32 = *(int32 *)&res_i32; + return res_i32; +} +``` +And in the host embedder: +``` + uint32 argv[2]; + float arg_f32 = ...; /* argument to foo */ + float res_f32; + bool ret; + + argv[0] = *(uint32 *)&arg_f32; + func = wasm_runtime_lookup_function(module_inst, "foo1", NULL); + ret = wasm_runtime_call_wasm(exec_env, func, 1, argv); + if (!ret) { + /* handle exception */ + printf("%s\n", wasm_runtime_get_exception(module_inst)); + } + else { + /* the return value is stored in argv[0] */ + res_f32 = *(float *)&argv[0]; + } +``` From c8b59588a758c8f969203c1828981d96a3647ce5 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 23 Jan 2024 12:21:20 +0800 Subject: [PATCH 36/53] Enhance setting write gs base with cmake variable (#3066) In linux x86-64, developer can use cmake variable to configure whether to enable writing linear memory base address to x86 GS register or not: - `cmake -DWAMR_DISABLE_WRITE_GS_BASE=1`: disabled it - `cmake -DWAMR_DISABLE_WRITE_GS_BASE=0`: enabled it - `cmake` without `-DWAMR_DISABLE_WRITE_GS_BASE=1/0`: auto-detected by the compiler --- build-scripts/config_common.cmake | 65 +++++++++++++++++++------------ 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 76ebcb0b7..6e13d0ffe 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -421,32 +421,49 @@ if (WAMR_BUILD_STATIC_PGO EQUAL 1) add_definitions (-DWASM_ENABLE_STATIC_PGO=1) message (" AOT static PGO enabled") endif () -if (WAMR_DISABLE_WRITE_GS_BASE EQUAL 1) - add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=1) - message (" Write linear memory base addr to x86 GS register disabled") -elseif (WAMR_BUILD_TARGET STREQUAL "X86_64" - AND WAMR_BUILD_PLATFORM STREQUAL "linux") - set (TEST_WRGSBASE_SOURCE "${CMAKE_BINARY_DIR}/test_wrgsbase.c") - file (WRITE "${TEST_WRGSBASE_SOURCE}" " - #include - #include - int main() { - uint64_t value; - asm volatile (\"wrgsbase %0\" : : \"r\"(value)); - printf(\"WRGSBASE instruction is available.\\n\"); - return 0; - }") - # Try to compile and run the test program - try_run (TEST_WRGSBASE_RESULT - TEST_WRGSBASE_COMPILED - ${CMAKE_BINARY_DIR}/test_wrgsbase - SOURCES ${TEST_WRGSBASE_SOURCE} - CMAKE_FLAGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - ) - #message("${TEST_WRGSBASE_COMPILED}, ${TEST_WRGSBASE_RESULT}") - if (NOT TEST_WRGSBASE_RESULT EQUAL 0) +if (WAMR_BUILD_TARGET STREQUAL "X86_64" + AND WAMR_BUILD_PLATFORM STREQUAL "linux") + if (WAMR_DISABLE_WRITE_GS_BASE EQUAL 1) + # disabled by user + set (DISABLE_WRITE_GS_BASE 1) + elseif (WAMR_DISABLE_WRITE_GS_BASE EQUAL 0) + # enabled by user + set (DISABLE_WRITE_GS_BASE 0) + elseif (CMAKE_CROSSCOMPILING) + # disabled in cross compilation environment + set (DISABLE_WRITE_GS_BASE 1) + else () + # auto-detected by the compiler + set (TEST_WRGSBASE_SOURCE "${CMAKE_BINARY_DIR}/test_wrgsbase.c") + file (WRITE "${TEST_WRGSBASE_SOURCE}" " + #include + #include + int main() { + uint64_t value; + asm volatile (\"wrgsbase %0\" : : \"r\"(value)); + printf(\"WRGSBASE instruction is available.\\n\"); + return 0; + }") + # Try to compile and run the test program + try_run (TEST_WRGSBASE_RESULT + TEST_WRGSBASE_COMPILED + ${CMAKE_BINARY_DIR}/test_wrgsbase + SOURCES ${TEST_WRGSBASE_SOURCE} + CMAKE_FLAGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + ) + #message("${TEST_WRGSBASE_COMPILED}, ${TEST_WRGSBASE_RESULT}") + if (TEST_WRGSBASE_RESULT EQUAL 0) + set (DISABLE_WRITE_GS_BASE 0) + else () + set (DISABLE_WRITE_GS_BASE 1) + endif () + endif () + if (DISABLE_WRITE_GS_BASE EQUAL 1) add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=1) message (" Write linear memory base addr to x86 GS register disabled") + else () + add_definitions (-DWASM_DISABLE_WRITE_GS_BASE=0) + message (" Write linear memory base addr to x86 GS register enabled") endif () endif () if (WAMR_CONFIGUABLE_BOUNDS_CHECKS EQUAL 1) From b44aa654b8f41112dd02a84c2e3246e669e818b6 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 23 Jan 2024 14:00:19 +0900 Subject: [PATCH 37/53] aot_reloc_x86_64.c: Suggest to try --size-level=0 as well (#3067) cf. https://github.com/bytecodealliance/wasm-micro-runtime/issues/3035 --- core/iwasm/aot/arch/aot_reloc_x86_64.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/core/iwasm/aot/arch/aot_reloc_x86_64.c b/core/iwasm/aot/arch/aot_reloc_x86_64.c index b5be24593..d1f5cb5ac 100644 --- a/core/iwasm/aot/arch/aot_reloc_x86_64.c +++ b/core/iwasm/aot/arch/aot_reloc_x86_64.c @@ -163,7 +163,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, error_buf, error_buf_size, "AOT module load failed: " "relocation truncated to fit R_X86_64_PC32 failed. " - "Try using wamrc with --size-level=1 option."); + "Try using wamrc with --size-level=1 or 0 option."); return false; } @@ -196,7 +196,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, snprintf(buf, sizeof(buf), "AOT module load failed: " "relocation truncated to fit %s failed. " - "Try using wamrc with --size-level=1 option.", + "Try using wamrc with --size-level=1 or 0 option.", reloc_type == R_X86_64_32 ? "R_X86_64_32" : "R_X86_64_32S"); set_error_buf(error_buf, error_buf_size, buf); @@ -236,15 +236,16 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, target_addr -= sizeof(int32); #endif if ((int32)target_addr != target_addr) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "relocation truncated to fit " + set_error_buf( + error_buf, error_buf_size, + "AOT module load failed: " + "relocation truncated to fit " #if !defined(BH_PLATFORM_WINDOWS) - "R_X86_64_PLT32 failed. " + "R_X86_64_PLT32 failed. " #else - "IMAGE_REL_AMD64_32 failed." + "IMAGE_REL_AMD64_32 failed." #endif - "Try using wamrc with --size-level=1 option."); + "Try using wamrc with --size-level=1 or 0 option."); return false; } *(int32 *)(target_section_addr + reloc_offset) = (int32)target_addr; From ab97d543e0f7c93a2ff4c4bfef351176c751f1ca Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Tue, 23 Jan 2024 16:20:01 +0900 Subject: [PATCH 38/53] wasm_cluster_destroy_spawned_exec_env: Avoid "invalid exec env" trap (#3068) Possible alternatives: * Make wasm_cluster_destroy_spawned_exec_env take two exec_env. One for wasm execution and another to specify the target to destroy. * Make execute functions to switch exec_env as briefly discussed in https://github.com/bytecodealliance/wasm-micro-runtime/pull/2047 --- core/iwasm/libraries/thread-mgr/thread_manager.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 377983a69..75c66a7c3 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -604,11 +604,24 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env) WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env); bh_assert(cluster != NULL); + WASMExecEnv *exec_env_tls = NULL; + +#ifdef OS_ENABLE_HW_BOUND_CHECK + /* Note: free_aux_stack can execute the module's "free" function + * using the specified exec_env. In case of OS_ENABLE_HW_BOUND_CHECK, + * it needs to match the TLS exec_env if available. (Consider a native + * function which calls wasm_cluster_destroy_spawned_exec_env.) + */ + exec_env_tls = wasm_runtime_get_exec_env_tls(); +#endif + if (exec_env_tls == NULL) { + exec_env_tls = exec_env; + } os_mutex_lock(&cluster->lock); /* Free aux stack space */ - free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom); + free_aux_stack(exec_env_tls, exec_env->aux_stack_bottom.bottom); /* Remove exec_env */ wasm_cluster_del_exec_env_internal(cluster, exec_env, false); /* Destroy exec_env */ From 9f643405295997b214e908d7b9a9325a9a4761ef Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 23 Jan 2024 21:38:30 +0800 Subject: [PATCH 39/53] Fix some issues reported by CodeQL (#3064) Refer to https://github.com/bytecodealliance/wasm-micro-runtime/pull/2812 and https://github.com/bytecodealliance/wasm-micro-runtime/security/code-scanning?query=pr%3A2812+is%3Aopen --- core/iwasm/aot/aot_loader.c | 5 ++--- core/iwasm/common/wasm_c_api.c | 8 ++++---- core/iwasm/common/wasm_native.c | 2 +- core/iwasm/common/wasm_runtime_common.c | 3 ++- core/iwasm/interpreter/wasm_loader.c | 6 +++--- core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c | 3 ++- core/iwasm/libraries/thread-mgr/thread_manager.c | 2 +- core/shared/platform/common/posix/posix_socket.c | 4 ++-- core/shared/utils/bh_hashmap.c | 4 +++- 9 files changed, 20 insertions(+), 17 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index d57f00930..948ac4dfd 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -3134,8 +3134,7 @@ resolve_execute_mode(const uint8 *buf, uint32 size, bool *p_mode, p += 8; while (p < p_end) { read_uint32(p, p_end, section_type); - if (section_type <= AOT_SECTION_TYPE_SIGANATURE - || section_type == AOT_SECTION_TYPE_TARGET_INFO) { + if (section_type <= AOT_SECTION_TYPE_SIGANATURE) { read_uint32(p, p_end, section_size); CHECK_BUF(p, p_end, section_size); if (section_type == AOT_SECTION_TYPE_TARGET_INFO) { @@ -3150,7 +3149,7 @@ resolve_execute_mode(const uint8 *buf, uint32 size, bool *p_mode, break; } } - else if (section_type > AOT_SECTION_TYPE_SIGANATURE) { + else { /* section_type > AOT_SECTION_TYPE_SIGANATURE */ set_error_buf(error_buf, error_buf_size, "resolve execute mode failed"); break; diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index bdc5315eb..873dc3b3f 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -2294,7 +2294,7 @@ wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary) (uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size, error_buf, (uint32)sizeof(error_buf)); if (!(module_ex->module_comm_rt)) { - LOG_ERROR(error_buf); + LOG_ERROR("%s", error_buf); goto free_vec; } @@ -2367,7 +2367,7 @@ wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary) } else { ret = false; - LOG_VERBOSE(error_buf); + LOG_VERBOSE("%s", error_buf); } return ret; @@ -3359,7 +3359,7 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params, wasm_runtime_set_exception(func->inst_comm_rt, NULL); if (!wasm_runtime_call_wasm(exec_env, func_comm_rt, argc, argv)) { if (wasm_runtime_get_exception(func->inst_comm_rt)) { - LOG_DEBUG(wasm_runtime_get_exception(func->inst_comm_rt)); + LOG_DEBUG("%s", wasm_runtime_get_exception(func->inst_comm_rt)); goto failed; } } @@ -5044,7 +5044,7 @@ failed: *trap = wasm_trap_new(store, &message); wasm_byte_vec_delete(&message); } - LOG_DEBUG(error_buf); + LOG_DEBUG("%s", error_buf); wasm_instance_delete_internal(instance); return NULL; } diff --git a/core/iwasm/common/wasm_native.c b/core/iwasm/common/wasm_native.c index e56521781..d2492bc49 100644 --- a/core/iwasm/common/wasm_native.c +++ b/core/iwasm/common/wasm_native.c @@ -194,7 +194,7 @@ wasm_native_resolve_symbol(const char *module_name, const char *field_name, { NativeSymbolsNode *node, *node_next; const char *signature = NULL; - void *func_ptr = NULL, *attachment; + void *func_ptr = NULL, *attachment = NULL; node = g_native_symbols_list; while (node) { diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 2bd29205e..4434a2a0f 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -2905,7 +2905,8 @@ copy_string_array(const char *array[], uint32 array_size, char **buf_ptr, /* We add +1 to generate null-terminated array of strings */ total_size = sizeof(char *) * ((uint64)array_size + 1); if (total_size >= UINT32_MAX - || (total_size > 0 && !(list = wasm_runtime_malloc((uint32)total_size))) + /* total_size must be larger than 0, don' check it again */ + || !(list = wasm_runtime_malloc((uint32)total_size)) || buf_size >= UINT32_MAX || (buf_size > 0 && !(buf = wasm_runtime_malloc((uint32)buf_size)))) { diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 2f98d0277..db82ab494 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -327,7 +327,7 @@ check_utf8_str(const uint8 *str, uint32 len) return false; } } - else if (chr >= 0xE1 && chr <= 0xEF) { + else { /* chr >= 0xE1 && chr <= 0xEF */ if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) { return false; } @@ -341,13 +341,13 @@ check_utf8_str(const uint8 *str, uint32 len) return false; } } - else if (chr >= 0xF1 && chr <= 0xF3) { + else if (chr <= 0xF3) { /* and also chr >= 0xF1 */ if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF || p[3] < 0x80 || p[3] > 0xBF) { return false; } } - else if (chr == 0xF4) { + else { /* chr == 0xF4 */ if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF || p[3] < 0x80 || p[3] > 0xBF) { return false; diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index d2545f7bd..0a7419baa 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -2013,7 +2013,8 @@ copy_buffer_to_iovec_app(wasm_module_inst_t module_inst, uint8 *buf_begin, } if (buf >= buf_begin + buf_size - || buf + data->buf_len < buf /* integer overflow */ + /* integer overflow */ + || data->buf_len > UINTPTR_MAX - (uintptr_t)buf || buf + data->buf_len > buf_begin + buf_size || size_to_copy == 0) { break; diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 75c66a7c3..76638c694 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -787,7 +787,7 @@ wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst, { /* workaround about passing instantiate-linking information */ CApiFuncImport **new_c_api_func_imports = NULL; - CApiFuncImport *c_api_func_imports; + CApiFuncImport *c_api_func_imports = NULL; uint32 import_func_count = 0; uint32 size_in_bytes = 0; diff --git a/core/shared/platform/common/posix/posix_socket.c b/core/shared/platform/common/posix/posix_socket.c index 7bdcb529e..0293b08d8 100644 --- a/core/shared/platform/common/posix/posix_socket.c +++ b/core/shared/platform/common/posix/posix_socket.c @@ -884,7 +884,7 @@ os_socket_set_ip_ttl(bh_socket_t socket, uint8_t ttl_s) int os_socket_get_ip_ttl(bh_socket_t socket, uint8_t *ttl_s) { - socklen_t opt_len = sizeof(ttl_s); + socklen_t opt_len = sizeof(*ttl_s); if (getsockopt(socket, IPPROTO_IP, IP_TTL, ttl_s, &opt_len) != 0) { return BHT_ERROR; } @@ -906,7 +906,7 @@ os_socket_set_ip_multicast_ttl(bh_socket_t socket, uint8_t ttl_s) int os_socket_get_ip_multicast_ttl(bh_socket_t socket, uint8_t *ttl_s) { - socklen_t opt_len = sizeof(ttl_s); + socklen_t opt_len = sizeof(*ttl_s); if (getsockopt(socket, IPPROTO_IP, IP_MULTICAST_TTL, ttl_s, &opt_len) != 0) { return BHT_ERROR; diff --git a/core/shared/utils/bh_hashmap.c b/core/shared/utils/bh_hashmap.c index 3502239ad..794b7a746 100644 --- a/core/shared/utils/bh_hashmap.c +++ b/core/shared/utils/bh_hashmap.c @@ -51,7 +51,9 @@ bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func, + sizeof(HashMapElem *) * (uint64)size + (use_lock ? sizeof(korp_mutex) : 0); - if (total_size >= UINT32_MAX || !(map = BH_MALLOC((uint32)total_size))) { + /* size <= HASH_MAP_MAX_SIZE, so total_size won't be larger than + UINT32_MAX, no need to check integer overflow */ + if (!(map = BH_MALLOC((uint32)total_size))) { LOG_ERROR("HashMap create failed: alloc memory failed.\n"); return NULL; } From 0eb788d71159bf6cb7f0ea46fd504b83888d9e97 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Wed, 24 Jan 2024 09:45:57 +0800 Subject: [PATCH 40/53] build_wamr.md: Update the document (#3074) Add description for `WAMR_BUILD_LINUX_PERF`, `WAMR_BUILD_QUICK_AOT_ENTRY` and `WAMR_BUILD_MODULE_INST_CONTEXT`. And add some reference links. --- doc/build_wamr.md | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 1ac62409c..a239a4050 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -62,6 +62,7 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM #### **Enable Multi-Module feature** - **WAMR_BUILD_MULTI_MODULE**=1/0, default to disable if not set +> Note: See [Multiple Modules as Dependencies](./multi_module.md) for more details. #### **Enable WASM mini loader** @@ -82,6 +83,8 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM - **WAMR_BUILD_LIB_PTHREAD**=1/0, default to disable if not set > Note: The dependent feature of lib pthread such as the `shared memory` and `thread manager` will be enabled automatically. +> See [WAMR pthread library](./pthread_library.md) for more details. + #### **Enable lib-pthread-semaphore** - **WAMR_BUILD_LIB_PTHREAD_SEMAPHORE**=1/0, default to disable if not set > Note: This feature depends on `lib-pthread`, it will be enabled automatically if this feature is enabled. @@ -90,8 +93,11 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM - **WAMR_BUILD_LIB_WASI_THREADS**=1/0, default to disable if not set > Note: The dependent feature of lib wasi-threads such as the `shared memory` and `thread manager` will be enabled automatically. +> See [wasi-threads](./pthread_impls.md#wasi-threads-new) and [Introduction to WAMR WASI threads](https://bytecodealliance.github.io/wamr.dev/blog/introduction-to-wamr-wasi-threads) for more details. + #### **Enable lib wasi-nn** - **WAMR_BUILD_WASI_NN**=1/0, default to disable if not set +> Note: See [WASI-NN](../core/iwasm/libraries/wasi-nn) for more details. #### **Enable lib wasi-nn GPU mode** - **WAMR_BUILD_WASI_NN_ENABLE_GPU**=1/0, default to disable if not set @@ -137,12 +143,17 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM > Note: if it is enabled, developer can use API `void wasm_runtime_dump_mem_consumption(wasm_exec_env_t exec_env)` to dump the memory consumption info. Currently we only profile the memory consumption of module, module_instance and exec_env, the memory consumed by other components such as `wasi-ctx`, `multi-module` and `thread-manager` are not included. +> Also refer to [Memory usage estimation for a module](./memory_usage.md). + #### **Enable performance profiling (Experiment)** - **WAMR_BUILD_PERF_PROFILING**=1/0, default to disable if not set > Note: if it is enabled, developer can use API `void wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst)` to dump the performance consumption info. Currently we only profile the performance consumption of each WASM function. > The function name searching sequence is the same with dump call stack feature. +> Also refer to [Tune the performance of running wasm/aot file](./perf_tune.md). + + #### **Enable the global heap** - **WAMR_BUILD_GLOBAL_HEAP_POOL**=1/0, default to disable if not set for all *iwasm* applications, except for the platforms Alios and Zephyr. @@ -192,7 +203,7 @@ Currently we only profile the memory consumption of module, module_instance and #### **Enable source debugging features** - **WAMR_BUILD_DEBUG_INTERP**=1/0, default to 0 if not set -> Note: There are some other setup required by source debugging, please refer to [source_debugging.md](./source_debugging.md) for more details. +> Note: There are some other setup required by source debugging, please refer to [source_debugging.md](./source_debugging.md) and [WAMR source debugging basic](https://bytecodealliance.github.io/wamr.dev/blog/wamr-source-debugging-basic) for more details. #### **Enable load wasm custom sections** - **WAMR_BUILD_LOAD_CUSTOM_SECTION**=1/0, default to disable if not set @@ -207,12 +218,34 @@ Currently we only profile the memory consumption of module, module_instance and - **WAMR_BUILD_STACK_GUARD_SIZE**=n, default to N/A if not set. > Note: By default, the stack guard size is 1K (1024) or 24K (if uvwasi enabled). -### **Disable the writing linear memory base address to x86 GS segment register +### **Disable the writing linear memory base address to x86 GS segment register** - **WAMR_DISABLE_WRITE_GS_BASE**=1/0, default to enable if not set and supported by platform > Note: by default only platform [linux x86-64](https://github.com/bytecodealliance/wasm-micro-runtime/blob/5fb5119239220b0803e7045ca49b0a29fe65e70e/core/shared/platform/linux/platform_internal.h#L67) will enable this feature, for 32-bit platforms it's automatically disabled even when the flag is set to 0. In linux x86-64, writing the linear memory base address to x86 GS segment register may be used to speedup the linear memory access for LLVM AOT/JIT, when `--enable-segue=[]` option is added for `wamrc` or `iwasm`. +> See [Enable segue optimization for wamrc when generating the aot file](./perf_tune.md#3-enable-segue-optimization-for-wamrc-when-generating-the-aot-file) for more details. + ### **Enable running PGO(Profile-Guided Optimization) instrumented AOT file** - **WAMR_BUILD_STATIC_PGO**=1/0, default to disable if not set +> Note: See [Use the AOT static PGO method](./perf_tune.md#5-use-the-aot-static-pgo-method) for more details. + +### **Enable linux perf support** +- **WAMR_BUILD_LINUX_PERF**=1/0, enable linux perf support to generate the flamegraph to analyze the performance of a wasm application, default to disable if not set +> Note: See [Use linux-perf](./perf_tune.md#7-use-linux-perf) for more details. + +### **Enable module instance context APIs** +- **WAMR_BUILD_MODULE_INST_CONTEXT**=1/0, enable module instance context APIs which can set one or more contexts created by the embedder for a wasm module instance, default to enable if not set: +```C + wasm_runtime_create_context_key + wasm_runtime_destroy_context_key + wasm_runtime_set_context + wasm_runtime_set_context_spread + wasm_runtime_get_context +``` +> Note: See [wasm_export.h](../core/iwasm/include/wasm_export.h) for more details. + +### **Enable quick AOT/JTI entries** +- **WAMR_BUILD_QUICK_AOT_ENTRY**=1/0, enable registering quick call entries to speedup the aot/jit func call process, default to enable if not set +> Note: See [Refine callings to AOT/JIT functions from host native](./perf_tune.md#83-refine-callings-to-aotjit-functions-from-host-native) for more details. **Combination of configurations:** From f56154ed80459c2d39c0dc4bb07473ac02659d92 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 24 Jan 2024 10:46:53 +0900 Subject: [PATCH 41/53] thread-mgr: Fix locking problems around aux stack allocation (#3073) Fixes: https://github.com/bytecodealliance/wasm-micro-runtime/issues/3069 --- .../libraries/thread-mgr/thread_manager.c | 101 ++++++++---------- 1 file changed, 44 insertions(+), 57 deletions(-) diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 76638c694..bdfc38dde 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -137,9 +137,10 @@ final: return ret; } -/* The caller must lock cluster->lock */ -static bool -allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size) +/* The caller must not have any locks */ +bool +wasm_cluster_allocate_aux_stack(WASMExecEnv *exec_env, uint32 *p_start, + uint32 *p_size) { WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0 @@ -149,8 +150,8 @@ allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size) stack_end = wasm_runtime_module_malloc_internal(module_inst, exec_env, cluster->stack_size, NULL); - *start = stack_end + cluster->stack_size; - *size = cluster->stack_size; + *p_start = stack_end + cluster->stack_size; + *p_size = cluster->stack_size; return stack_end != 0; #else @@ -158,27 +159,33 @@ allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size) /* If the module doesn't have aux stack info, it can't create any threads */ - if (!cluster->stack_segment_occupied) + + os_mutex_lock(&cluster->lock); + if (!cluster->stack_segment_occupied) { + os_mutex_unlock(&cluster->lock); return false; + } for (i = 0; i < cluster_max_thread_num; i++) { if (!cluster->stack_segment_occupied[i]) { - if (start) - *start = cluster->stack_tops[i]; - if (size) - *size = cluster->stack_size; + if (p_start) + *p_start = cluster->stack_tops[i]; + if (p_size) + *p_size = cluster->stack_size; cluster->stack_segment_occupied[i] = true; + os_mutex_unlock(&cluster->lock); return true; } } + os_mutex_unlock(&cluster->lock); return false; #endif } -/* The caller must lock cluster->lock */ -static bool -free_aux_stack(WASMExecEnv *exec_env, uint32 start) +/* The caller must not have any locks */ +bool +wasm_cluster_free_aux_stack(WASMExecEnv *exec_env, uint32 start) { WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); @@ -199,43 +206,19 @@ free_aux_stack(WASMExecEnv *exec_env, uint32 start) #else uint32 i; + os_mutex_lock(&cluster->lock); for (i = 0; i < cluster_max_thread_num; i++) { if (start == cluster->stack_tops[i]) { cluster->stack_segment_occupied[i] = false; + os_mutex_unlock(&cluster->lock); return true; } } + os_mutex_unlock(&cluster->lock); return false; #endif } -bool -wasm_cluster_allocate_aux_stack(WASMExecEnv *exec_env, uint32 *p_start, - uint32 *p_size) -{ - WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); - bool ret; - - os_mutex_lock(&cluster->lock); - ret = allocate_aux_stack(exec_env, p_start, p_size); - os_mutex_unlock(&cluster->lock); - - return ret; -} - -bool -wasm_cluster_free_aux_stack(WASMExecEnv *exec_env, uint32 start) -{ - WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env); - bool ret; - - os_mutex_lock(&cluster->lock); - ret = free_aux_stack(exec_env, start); - os_mutex_unlock(&cluster->lock); - - return ret; -} - WASMCluster * wasm_cluster_create(WASMExecEnv *exec_env) { @@ -535,6 +518,13 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) goto fail1; } + if (!wasm_cluster_allocate_aux_stack(exec_env, &aux_stack_start, + &aux_stack_size)) { + LOG_ERROR("thread manager error: " + "failed to allocate aux stack space for new thread"); + goto fail1; + } + os_mutex_lock(&cluster->lock); if (cluster->has_exception || cluster->processing) { @@ -561,16 +551,10 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) goto fail2; } - if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) { - LOG_ERROR("thread manager error: " - "failed to allocate aux stack space for new thread"); - goto fail3; - } - /* Set aux stack for current thread */ if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start, aux_stack_size)) { - goto fail4; + goto fail3; } /* Inherit suspend_flags of parent thread */ @@ -578,20 +562,19 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env) (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK); if (!wasm_cluster_add_exec_env(cluster, new_exec_env)) { - goto fail4; + goto fail3; } os_mutex_unlock(&cluster->lock); return new_exec_env; -fail4: - /* free the allocated aux stack space */ - free_aux_stack(exec_env, aux_stack_start); fail3: wasm_exec_env_destroy_internal(new_exec_env); fail2: os_mutex_unlock(&cluster->lock); + /* free the allocated aux stack space */ + wasm_cluster_free_aux_stack(exec_env, aux_stack_start); fail1: wasm_runtime_deinstantiate_internal(new_module_inst, true); @@ -618,10 +601,12 @@ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env) exec_env_tls = exec_env; } + /* Free aux stack space */ + wasm_cluster_free_aux_stack(exec_env_tls, + exec_env->aux_stack_bottom.bottom); + os_mutex_lock(&cluster->lock); - /* Free aux stack space */ - free_aux_stack(exec_env_tls, exec_env->aux_stack_bottom.bottom); /* Remove exec_env */ wasm_cluster_del_exec_env_internal(cluster, exec_env, false); /* Destroy exec_env */ @@ -667,6 +652,9 @@ thread_manager_start_routine(void *arg) wasm_cluster_thread_exited(exec_env); #endif + /* Free aux stack space */ + wasm_cluster_free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom); + os_mutex_lock(&cluster_list_lock); os_mutex_lock(&cluster->lock); @@ -687,8 +675,6 @@ thread_manager_start_routine(void *arg) os_printf("========================================\n"); #endif - /* Free aux stack space */ - free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom); /* Remove exec_env */ wasm_cluster_del_exec_env_internal(cluster, exec_env, false); /* Destroy exec_env */ @@ -1063,6 +1049,9 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval) wasm_cluster_thread_exited(exec_env); #endif + /* Free aux stack space */ + wasm_cluster_free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom); + /* App exit the thread, free the resources before exit native thread */ os_mutex_lock(&cluster_list_lock); @@ -1081,8 +1070,6 @@ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval) module_inst = exec_env->module_inst; - /* Free aux stack space */ - free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom); /* Remove exec_env */ wasm_cluster_del_exec_env_internal(cluster, exec_env, false); /* Destroy exec_env */ From 1505e6170486acf72d2c3c2593665d1d0c91438c Mon Sep 17 00:00:00 2001 From: Marcin Kolny Date: Wed, 24 Jan 2024 03:21:13 +0000 Subject: [PATCH 42/53] Remove a lot of "unused parameter" warnings (#3075) They might shadow some of the real issues, so better to keep the number of warnings as low as possible. --- .../libc-builtin/libc_builtin_wrapper.c | 48 ++++- .../libraries/libc-wasi/libc_wasi_wrapper.c | 191 +++++++++--------- .../sandboxed-system-primitives/src/posix.c | 20 +- .../platform/common/posix/posix_blocking_op.c | 1 + .../platform/common/posix/posix_clock.c | 2 + .../platform/common/posix/posix_memmap.c | 3 + 6 files changed, 167 insertions(+), 98 deletions(-) diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c index fec8fcc97..8e6a65a4a 100644 --- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c +++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c @@ -473,12 +473,16 @@ snprintf_wrapper(wasm_exec_env_t exec_env, char *str, uint32 size, static int puts_wrapper(wasm_exec_env_t exec_env, const char *str) { + (void)exec_env; + return os_printf("%s\n", str); } static int putchar_wrapper(wasm_exec_env_t exec_env, int c) { + (void)exec_env; + os_printf("%c", c); return 1; } @@ -585,6 +589,8 @@ strchr_wrapper(wasm_exec_env_t exec_env, const char *s, int32 c) static int32 strcmp_wrapper(wasm_exec_env_t exec_env, const char *s1, const char *s2) { + (void)exec_env; + /* s1 and s2 have been checked by runtime */ return strcmp(s1, s2); } @@ -641,6 +647,8 @@ strncpy_wrapper(wasm_exec_env_t exec_env, char *dst, const char *src, static uint32 strlen_wrapper(wasm_exec_env_t exec_env, const char *s) { + (void)exec_env; + /* s has been checked by runtime */ return (uint32)strlen(s); } @@ -693,6 +701,7 @@ free_wrapper(wasm_exec_env_t exec_env, void *ptr) static int32 atoi_wrapper(wasm_exec_env_t exec_env, const char *s) { + (void)exec_env; /* s has been checked by runtime */ return atoi(s); } @@ -757,6 +766,8 @@ static int32 strncasecmp_wrapper(wasm_exec_env_t exec_env, const char *s1, const char *s2, uint32 n) { + (void)exec_env; + /* s1 and s2 have been checked by runtime */ return strncasecmp(s1, s2, n); } @@ -764,6 +775,8 @@ strncasecmp_wrapper(wasm_exec_env_t exec_env, const char *s1, const char *s2, static uint32 strspn_wrapper(wasm_exec_env_t exec_env, const char *s, const char *accept) { + (void)exec_env; + /* s and accept have been checked by runtime */ return (uint32)strspn(s, accept); } @@ -771,6 +784,8 @@ strspn_wrapper(wasm_exec_env_t exec_env, const char *s, const char *accept) static uint32 strcspn_wrapper(wasm_exec_env_t exec_env, const char *s, const char *reject) { + (void)exec_env; + /* s and reject have been checked by runtime */ return (uint32)strcspn(s, reject); } @@ -787,60 +802,80 @@ strstr_wrapper(wasm_exec_env_t exec_env, const char *s, const char *find) static int32 isupper_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isupper(c); } static int32 isalpha_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isalpha(c); } static int32 isspace_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isspace(c); } static int32 isgraph_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isgraph(c); } static int32 isprint_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isprint(c); } static int32 isdigit_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isdigit(c); } static int32 isxdigit_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isxdigit(c); } static int32 tolower_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return tolower(c); } static int32 toupper_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return toupper(c); } static int32 isalnum_wrapper(wasm_exec_env_t exec_env, int32 c) { + (void)exec_env; + return isalnum(c); } @@ -899,7 +934,10 @@ __cxa_allocate_exception_wrapper(wasm_exec_env_t exec_env, uint32 thrown_size) static void __cxa_begin_catch_wrapper(wasm_exec_env_t exec_env, void *exception_object) -{} +{ + (void)exec_env; + (void)exception_object; +} static void __cxa_throw_wrapper(wasm_exec_env_t exec_env, void *thrown_exception, @@ -908,6 +946,10 @@ __cxa_throw_wrapper(wasm_exec_env_t exec_env, void *thrown_exception, wasm_module_inst_t module_inst = get_module_inst(exec_env); char buf[32]; + (void)thrown_exception; + (void)tinfo; + (void)table_elem_idx; + snprintf(buf, sizeof(buf), "%s", "exception thrown by stdc++"); wasm_runtime_set_exception(module_inst, buf); } @@ -924,6 +966,8 @@ clock_gettime_wrapper(wasm_exec_env_t exec_env, uint32 clk_id, wasm_module_inst_t module_inst = get_module_inst(exec_env); uint64 time; + (void)clk_id; + if (!validate_native_addr(ts_app, sizeof(struct timespec_app))) return (uint32)-1; @@ -937,6 +981,8 @@ clock_gettime_wrapper(wasm_exec_env_t exec_env, uint32 clk_id, static uint64 clock_wrapper(wasm_exec_env_t exec_env) { + (void)exec_env; + /* Convert to nano seconds as CLOCKS_PER_SEC in wasi-sdk */ return os_time_get_boot_us() * 1000; diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 0a7419baa..0b69de6b9 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c @@ -72,7 +72,7 @@ min_uint32(uint32_t a, uint32_t b) } static inline struct fd_table * -wasi_ctx_get_curfds(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx) +wasi_ctx_get_curfds(wasi_ctx_t wasi_ctx) { if (!wasi_ctx) return NULL; @@ -88,7 +88,7 @@ wasi_ctx_get_argv_environ(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx) } static inline struct fd_prestats * -wasi_ctx_get_prestats(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx) +wasi_ctx_get_prestats(wasi_ctx_t wasi_ctx) { if (!wasi_ctx) return NULL; @@ -96,7 +96,7 @@ wasi_ctx_get_prestats(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx) } static inline struct addr_pool * -wasi_ctx_get_addr_pool(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx) +wasi_ctx_get_addr_pool(wasi_ctx_t wasi_ctx) { if (!wasi_ctx) return NULL; @@ -292,7 +292,7 @@ wasi_fd_prestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx); + struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx); wasi_prestat_t prestat; wasi_errno_t err; @@ -317,7 +317,7 @@ wasi_fd_prestat_dir_name(wasm_exec_env_t exec_env, wasi_fd_t fd, char *path, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx); + struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -330,8 +330,8 @@ wasi_fd_close(wasm_exec_env_t exec_env, wasi_fd_t fd) { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); - struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); + struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -344,7 +344,7 @@ wasi_fd_datasync(wasm_exec_env_t exec_env, wasi_fd_t fd) { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -358,7 +358,7 @@ wasi_fd_pread(wasm_exec_env_t exec_env, wasi_fd_t fd, iovec_app_t *iovec_app, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); wasi_iovec_t *iovec, *iovec_begin; uint64 total_size; size_t nread; @@ -412,7 +412,7 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); wasi_ciovec_t *ciovec, *ciovec_begin; uint64 total_size; size_t nwritten; @@ -465,7 +465,7 @@ wasi_fd_read(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); wasi_iovec_t *iovec, *iovec_begin; uint64 total_size; size_t nread; @@ -517,8 +517,8 @@ wasi_fd_renumber(wasm_exec_env_t exec_env, wasi_fd_t from, wasi_fd_t to) { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); - struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); + struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -532,7 +532,7 @@ wasi_fd_seek(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filedelta_t offset, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -549,7 +549,7 @@ wasi_fd_tell(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t *newoffset) { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -566,7 +566,7 @@ wasi_fd_fdstat_get(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); wasi_fdstat_t fdstat; wasi_errno_t err; @@ -590,7 +590,7 @@ wasi_fd_fdstat_set_flags(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -605,7 +605,7 @@ wasi_fd_fdstat_set_rights(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -619,7 +619,7 @@ wasi_fd_sync(wasm_exec_env_t exec_env, wasi_fd_t fd) { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -634,7 +634,7 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); wasi_ciovec_t *ciovec, *ciovec_begin; uint64 total_size; size_t nwritten; @@ -687,7 +687,7 @@ wasi_fd_advise(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -701,7 +701,7 @@ wasi_fd_allocate(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -715,7 +715,7 @@ wasi_path_create_directory(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -732,8 +732,8 @@ wasi_path_link(wasm_exec_env_t exec_env, wasi_fd_t old_fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); - struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); + struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -752,7 +752,7 @@ wasi_path_open(wasm_exec_env_t exec_env, wasi_fd_t dirfd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); wasi_fd_t fd = (wasi_fd_t)-1; /* set fd_app -1 if path open failed */ wasi_errno_t err; @@ -776,7 +776,7 @@ wasi_fd_readdir(wasm_exec_env_t exec_env, wasi_fd_t fd, void *buf, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); size_t bufused; wasi_errno_t err; @@ -802,7 +802,7 @@ wasi_path_readlink(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); size_t bufused; wasi_errno_t err; @@ -828,7 +828,7 @@ wasi_path_rename(wasm_exec_env_t exec_env, wasi_fd_t old_fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -844,7 +844,7 @@ wasi_fd_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -862,7 +862,7 @@ wasi_fd_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -877,7 +877,7 @@ wasi_fd_filestat_set_size(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -892,7 +892,7 @@ wasi_path_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -912,7 +912,7 @@ wasi_path_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -929,8 +929,8 @@ wasi_path_symlink(wasm_exec_env_t exec_env, const char *old_path, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); - struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); + struct fd_prestats *prestats = wasi_ctx_get_prestats(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -945,7 +945,7 @@ wasi_path_unlink_file(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -959,7 +959,7 @@ wasi_path_remove_directory(wasm_exec_env_t exec_env, wasi_fd_t fd, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return (wasi_errno_t)-1; @@ -1076,7 +1076,7 @@ wasi_poll_oneoff(wasm_exec_env_t exec_env, const wasi_subscription_t *in, { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); size_t nevents = 0; wasi_errno_t err; @@ -1128,6 +1128,8 @@ wasi_proc_raise(wasm_exec_env_t exec_env, wasi_signal_t sig) static wasi_errno_t wasi_random_get(wasm_exec_env_t exec_env, void *buf, uint32 buf_len) { + (void)exec_env; + return wasmtime_ssp_random_get(buf, buf_len); } @@ -1142,7 +1144,7 @@ wasi_sock_accept(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_fdflags_t flags, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasi_ssp_sock_accept(exec_env, curfds, fd, flags, fd_new); } @@ -1161,7 +1163,7 @@ wasi_sock_addr_local(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(addr, sizeof(__wasi_addr_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasi_ssp_sock_addr_local(exec_env, curfds, fd, addr); } @@ -1180,7 +1182,7 @@ wasi_sock_addr_remote(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(addr, sizeof(__wasi_addr_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasi_ssp_sock_addr_remote(exec_env, curfds, fd, addr); } @@ -1200,7 +1202,7 @@ wasi_sock_addr_resolve(wasm_exec_env_t exec_env, const char *host, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); ns_lookup_list = wasi_ctx_get_ns_lookup_list(wasi_ctx); return wasi_ssp_sock_addr_resolve(exec_env, curfds, ns_lookup_list, host, @@ -1219,8 +1221,8 @@ wasi_sock_bind(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); - addr_pool = wasi_ctx_get_addr_pool(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); + addr_pool = wasi_ctx_get_addr_pool(wasi_ctx); return wasi_ssp_sock_bind(exec_env, curfds, addr_pool, fd, addr); } @@ -1228,6 +1230,9 @@ wasi_sock_bind(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr) static wasi_errno_t wasi_sock_close(wasm_exec_env_t exec_env, wasi_fd_t fd) { + (void)exec_env; + (void)fd; + return __WASI_ENOSYS; } @@ -1242,8 +1247,8 @@ wasi_sock_connect(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); - addr_pool = wasi_ctx_get_addr_pool(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); + addr_pool = wasi_ctx_get_addr_pool(wasi_ctx); return wasi_ssp_sock_connect(exec_env, curfds, addr_pool, fd, addr); } @@ -1262,7 +1267,7 @@ wasi_sock_get_broadcast(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_broadcast(exec_env, curfds, fd, is_enabled); } @@ -1281,7 +1286,7 @@ wasi_sock_get_keep_alive(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_keep_alive(exec_env, curfds, fd, is_enabled); } @@ -1301,7 +1306,7 @@ wasi_sock_get_linger(wasm_exec_env_t exec_env, wasi_fd_t fd, bool *is_enabled, || !validate_native_addr(linger_s, sizeof(int))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_linger(exec_env, curfds, fd, is_enabled, linger_s); @@ -1321,7 +1326,7 @@ wasi_sock_get_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(size, sizeof(wasi_size_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_recv_buf_size(exec_env, curfds, fd, size); } @@ -1340,7 +1345,7 @@ wasi_sock_get_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(timeout_us, sizeof(uint64_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_recv_timeout(exec_env, curfds, fd, timeout_us); } @@ -1359,7 +1364,7 @@ wasi_sock_get_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_reuse_addr(exec_env, curfds, fd, is_enabled); } @@ -1378,7 +1383,7 @@ wasi_sock_get_reuse_port(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_reuse_port(exec_env, curfds, fd, is_enabled); } @@ -1397,7 +1402,7 @@ wasi_sock_get_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(size, sizeof(__wasi_size_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_send_buf_size(exec_env, curfds, fd, size); } @@ -1416,7 +1421,7 @@ wasi_sock_get_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(timeout_us, sizeof(uint64_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_send_timeout(exec_env, curfds, fd, timeout_us); } @@ -1435,7 +1440,7 @@ wasi_sock_get_tcp_fastopen_connect(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_tcp_fastopen_connect(exec_env, curfds, fd, is_enabled); @@ -1455,7 +1460,7 @@ wasi_sock_get_tcp_no_delay(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_tcp_no_delay(exec_env, curfds, fd, is_enabled); } @@ -1474,7 +1479,7 @@ wasi_sock_get_tcp_quick_ack(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_tcp_quick_ack(exec_env, curfds, fd, is_enabled); @@ -1494,7 +1499,7 @@ wasi_sock_get_tcp_keep_idle(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(time_s, sizeof(uint32_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_tcp_keep_idle(exec_env, curfds, fd, time_s); } @@ -1513,7 +1518,7 @@ wasi_sock_get_tcp_keep_intvl(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(time_s, sizeof(uint32_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_tcp_keep_intvl(exec_env, curfds, fd, time_s); } @@ -1532,7 +1537,7 @@ wasi_sock_get_ip_multicast_loop(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_ip_multicast_loop(exec_env, curfds, fd, ipv6, is_enabled); @@ -1551,7 +1556,7 @@ wasi_sock_get_ip_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8_t *ttl_s) if (!validate_native_addr(ttl_s, sizeof(uint8_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_ip_ttl(exec_env, curfds, fd, ttl_s); } @@ -1570,7 +1575,7 @@ wasi_sock_get_ip_multicast_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(ttl_s, sizeof(uint8_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_ip_multicast_ttl(exec_env, curfds, fd, ttl_s); } @@ -1589,7 +1594,7 @@ wasi_sock_get_ipv6_only(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(is_enabled, sizeof(bool))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_get_ipv6_only(exec_env, curfds, fd, is_enabled); } @@ -1604,7 +1609,7 @@ wasi_sock_listen(wasm_exec_env_t exec_env, wasi_fd_t fd, uint32 backlog) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasi_ssp_sock_listen(exec_env, curfds, fd, backlog); } @@ -1621,7 +1626,7 @@ wasi_sock_open(wasm_exec_env_t exec_env, wasi_fd_t poolfd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasi_ssp_sock_open(exec_env, curfds, poolfd, af, socktype, sockfd); } @@ -1636,7 +1641,7 @@ wasi_sock_set_broadcast(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_broadcast(exec_env, curfds, fd, is_enabled); } @@ -1652,7 +1657,7 @@ wasi_sock_set_keep_alive(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_keep_alive(exec_env, curfds, fd, is_enabled); } @@ -1668,7 +1673,7 @@ wasi_sock_set_linger(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_linger(exec_env, curfds, fd, is_enabled, linger_s); @@ -1684,7 +1689,7 @@ wasi_sock_set_recv_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, size_t size) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_recv_buf_size(exec_env, curfds, fd, size); } @@ -1700,7 +1705,7 @@ wasi_sock_set_recv_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_recv_timeout(exec_env, curfds, fd, timeout_us); } @@ -1716,7 +1721,7 @@ wasi_sock_set_reuse_addr(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_reuse_addr(exec_env, curfds, fd, is_enabled); } @@ -1732,7 +1737,7 @@ wasi_sock_set_reuse_port(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_reuse_port(exec_env, curfds, fd, is_enabled); } @@ -1747,7 +1752,7 @@ wasi_sock_set_send_buf_size(wasm_exec_env_t exec_env, wasi_fd_t fd, size_t size) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_send_buf_size(exec_env, curfds, fd, size); } @@ -1763,7 +1768,7 @@ wasi_sock_set_send_timeout(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_send_timeout(exec_env, curfds, fd, timeout_us); } @@ -1779,7 +1784,7 @@ wasi_sock_set_tcp_fastopen_connect(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_tcp_fastopen_connect(exec_env, curfds, fd, is_enabled); @@ -1796,7 +1801,7 @@ wasi_sock_set_tcp_no_delay(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_tcp_no_delay(exec_env, curfds, fd, is_enabled); } @@ -1812,7 +1817,7 @@ wasi_sock_set_tcp_quick_ack(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_tcp_quick_ack(exec_env, curfds, fd, is_enabled); @@ -1829,7 +1834,7 @@ wasi_sock_set_tcp_keep_idle(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_tcp_keep_idle(exec_env, curfds, fd, time_s); } @@ -1845,7 +1850,7 @@ wasi_sock_set_tcp_keep_intvl(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_tcp_keep_intvl(exec_env, curfds, fd, time_s); } @@ -1861,7 +1866,7 @@ wasi_sock_set_ip_multicast_loop(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_ip_multicast_loop(exec_env, curfds, fd, ipv6, is_enabled); @@ -1882,7 +1887,7 @@ wasi_sock_set_ip_add_membership(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(imr_multiaddr, sizeof(__wasi_addr_ip_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_ip_add_membership( exec_env, curfds, fd, imr_multiaddr, imr_interface); @@ -1903,7 +1908,7 @@ wasi_sock_set_ip_drop_membership(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!validate_native_addr(imr_multiaddr, sizeof(__wasi_addr_ip_t))) return __WASI_EINVAL; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_ip_drop_membership( exec_env, curfds, fd, imr_multiaddr, imr_interface); @@ -1919,7 +1924,7 @@ wasi_sock_set_ip_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, uint8_t ttl_s) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_ip_ttl(exec_env, curfds, fd, ttl_s); } @@ -1935,7 +1940,7 @@ wasi_sock_set_ip_multicast_ttl(wasm_exec_env_t exec_env, wasi_fd_t fd, if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_ip_multicast_ttl(exec_env, curfds, fd, ttl_s); } @@ -1950,7 +1955,7 @@ wasi_sock_set_ipv6_only(wasm_exec_env_t exec_env, wasi_fd_t fd, bool is_enabled) if (!wasi_ctx) return __WASI_EACCES; - curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + curfds = wasi_ctx_get_curfds(wasi_ctx); return wasmtime_ssp_sock_set_ipv6_only(exec_env, curfds, fd, is_enabled); } @@ -2049,7 +2054,7 @@ wasi_sock_recv_from(wasm_exec_env_t exec_env, wasi_fd_t sock, **/ wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); uint64 total_size; uint8 *buf_begin = NULL; wasi_errno_t err; @@ -2153,7 +2158,7 @@ wasi_sock_send(wasm_exec_env_t exec_env, wasi_fd_t sock, **/ wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); uint64 buf_size = 0; uint8 *buf = NULL; wasi_errno_t err; @@ -2193,12 +2198,12 @@ wasi_sock_send_to(wasm_exec_env_t exec_env, wasi_fd_t sock, **/ wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); uint64 buf_size = 0; uint8 *buf = NULL; wasi_errno_t err; size_t send_bytes = 0; - struct addr_pool *addr_pool = wasi_ctx_get_addr_pool(module_inst, wasi_ctx); + struct addr_pool *addr_pool = wasi_ctx_get_addr_pool(wasi_ctx); if (!wasi_ctx) { return __WASI_EINVAL; @@ -2227,7 +2232,7 @@ wasi_sock_shutdown(wasm_exec_env_t exec_env, wasi_fd_t sock, wasi_sdflags_t how) { wasm_module_inst_t module_inst = get_module_inst(exec_env); wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst); - struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx); + struct fd_table *curfds = wasi_ctx_get_curfds(wasi_ctx); if (!wasi_ctx) return __WASI_EINVAL; @@ -2238,6 +2243,8 @@ wasi_sock_shutdown(wasm_exec_env_t exec_env, wasi_fd_t sock, wasi_sdflags_t how) static wasi_errno_t wasi_sched_yield(wasm_exec_env_t exec_env) { + (void)exec_env; + return wasmtime_ssp_sched_yield(); } diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index d24d49393..2363c73d2 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -1017,9 +1017,13 @@ wasmtime_ssp_fd_fdstat_get(wasm_exec_env_t exec_env, struct fd_table *curfds, __wasi_fd_t fd, __wasi_fdstat_t *buf) { struct fd_table *ft = curfds; - rwlock_rdlock(&ft->lock); struct fd_entry *fe; - __wasi_errno_t error = fd_table_get_entry(ft, fd, 0, 0, &fe); + __wasi_errno_t error; + + (void)exec_env; + + rwlock_rdlock(&ft->lock); + error = fd_table_get_entry(ft, fd, 0, 0, &fe); if (error != __WASI_ESUCCESS) { rwlock_unlock(&ft->lock); return error; @@ -1071,9 +1075,13 @@ wasmtime_ssp_fd_fdstat_set_rights(wasm_exec_env_t exec_env, __wasi_rights_t fs_rights_inheriting) { struct fd_table *ft = curfds; - rwlock_wrlock(&ft->lock); struct fd_entry *fe; - __wasi_errno_t error = + __wasi_errno_t error; + + (void)exec_env; + + rwlock_wrlock(&ft->lock); + error = fd_table_get_entry(ft, fd, fs_rights_base, fs_rights_inheriting, &fe); if (error != 0) { rwlock_unlock(&ft->lock); @@ -2980,7 +2988,9 @@ argv_environ_init(struct argv_environ_values *argv_environ, char *argv_buf, void argv_environ_destroy(struct argv_environ_values *argv_environ) -{} +{ + (void)argv_environ; +} void fd_table_destroy(struct fd_table *ft) diff --git a/core/shared/platform/common/posix/posix_blocking_op.c b/core/shared/platform/common/posix/posix_blocking_op.c index 560828a06..e56f84cf1 100644 --- a/core/shared/platform/common/posix/posix_blocking_op.c +++ b/core/shared/platform/common/posix/posix_blocking_op.c @@ -15,6 +15,7 @@ static void blocking_op_sighandler(int signo) { /* nothing */ + (void)signo; } void diff --git a/core/shared/platform/common/posix/posix_clock.c b/core/shared/platform/common/posix/posix_clock.c index 280306c42..41413211c 100644 --- a/core/shared/platform/common/posix/posix_clock.c +++ b/core/shared/platform/common/posix/posix_clock.c @@ -73,6 +73,8 @@ os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision, clockid_t nclock_id; __wasi_errno_t error = wasi_clockid_to_clockid(clock_id, &nclock_id); + (void)precision; + if (error != __WASI_ESUCCESS) return error; diff --git a/core/shared/platform/common/posix/posix_memmap.c b/core/shared/platform/common/posix/posix_memmap.c index 1ab99fd91..aeb4530ab 100644 --- a/core/shared/platform/common/posix/posix_memmap.c +++ b/core/shared/platform/common/posix/posix_memmap.c @@ -266,5 +266,8 @@ os_icache_flush(void *start, size_t len) { #if defined(__APPLE__) || defined(__MACH__) sys_icache_invalidate(start, len); +#else + (void)start; + (void)len; #endif } From 3fcd79867d8e3d0efd1499f0c2a7a8b2d0ac1a47 Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Wed, 24 Jan 2024 06:05:07 +0100 Subject: [PATCH 43/53] Forward log and log level to custom bh_log callback (#3070) Follow-up on #2907. The log level is needed in the host embedder to better integrate with the embedder's logger. Allow the developer to customize his bh_log callback with `cmake -DWAMR_BH_LOG=`, and update sample/basic to show the usage. --- build-scripts/config_common.cmake | 3 +++ core/shared/utils/bh_log.c | 2 ++ core/shared/utils/bh_log.h | 6 ++++++ doc/build_wamr.md | 12 +++++++++++- samples/basic/build.sh | 2 +- samples/basic/src/main.c | 28 +++++++++++++++++++++++++++- 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 6e13d0ffe..cb0b7210b 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -338,6 +338,9 @@ endif () if (DEFINED WAMR_BH_VPRINTF) add_definitions (-DBH_VPRINTF=${WAMR_BH_VPRINTF}) endif () +if (DEFINED WAMR_BH_LOG) + add_definitions (-DBH_LOG=${WAMR_BH_LOG}) +endif () if (WAMR_DISABLE_APP_ENTRY EQUAL 1) message (" WAMR application entry functions excluded") endif () diff --git a/core/shared/utils/bh_log.c b/core/shared/utils/bh_log.c index 7a9465e0c..1ffd9b764 100644 --- a/core/shared/utils/bh_log.c +++ b/core/shared/utils/bh_log.c @@ -17,6 +17,7 @@ bh_log_set_verbose_level(uint32 level) log_verbose_level = level; } +#ifndef BH_LOG void bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...) { @@ -56,6 +57,7 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...) os_printf("\n"); } +#endif static uint32 last_time_ms = 0; static uint32 total_time_ms = 0; diff --git a/core/shared/utils/bh_log.h b/core/shared/utils/bh_log.h index e0bc61da2..53921b250 100644 --- a/core/shared/utils/bh_log.h +++ b/core/shared/utils/bh_log.h @@ -38,8 +38,14 @@ typedef enum { void bh_log_set_verbose_level(uint32 level); +#ifndef BH_LOG void bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...); +#else +void +BH_LOG(uint32 log_level, const char *file, int line, const char *fmt, ...); +#define bh_log BH_LOG +#endif #ifdef BH_PLATFORM_NUTTX diff --git a/doc/build_wamr.md b/doc/build_wamr.md index a239a4050..6857478a9 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -190,7 +190,17 @@ Currently we only profile the memory consumption of module, module_instance and > } > ``` > -> and then use `cmake -DWAMR_BH_VPRINTF=my_vprintf ..` to pass the callback function, or add `BH_VPRINTF=my_vprintf` macro for the compiler, e.g. add line `add_defintions(-DBH_VPRINTF=my_vprintf)` in CMakeListst.txt. +> and then use `cmake -DWAMR_BH_VPRINTF=my_vprintf ..` to pass the callback function, or add `BH_VPRINTF=my_vprintf` macro for the compiler, e.g. add line `add_defintions(-DBH_VPRINTF=my_vprintf)` in CMakeListst.txt. See [basic sample](../samples/basic/src/main.c) for a usage example. + +#### **WAMR_BH_LOG**=, default to disable if not set +> Note: if the log_callback function is provided by the developer, WAMR logs are redirected to such callback. For example: +> ```C +> void my_log(uint32 log_level, const char *file, int line, const char *fmt, ...) +> { +> /* Usage of custom logger */ +> } +> ``` +> See [basic sample](../samples/basic/src/main.c) for a usage example. #### **Enable reference types feature** - **WAMR_BUILD_REF_TYPES**=1/0, default to disable if not set diff --git a/samples/basic/build.sh b/samples/basic/build.sh index bc75ac783..c1d598a8c 100755 --- a/samples/basic/build.sh +++ b/samples/basic/build.sh @@ -21,7 +21,7 @@ echo "#####################build basic project" cd ${CURR_DIR} mkdir -p cmake_build cd cmake_build -cmake .. -DCMAKE_BUILD_TYPE=Debug +cmake .. -DCMAKE_BUILD_TYPE=Debug -DWAMR_BH_VPRINTF=my_vprintf -DWAMR_BH_LOG=my_log make -j ${nproc} if [ $? != 0 ];then echo "BUILD_FAIL basic exit as $?\n" diff --git a/samples/basic/src/main.c b/samples/basic/src/main.c index c35da3179..ca580af33 100644 --- a/samples/basic/src/main.c +++ b/samples/basic/src/main.c @@ -15,6 +15,30 @@ get_pow(int x, int y); int32_t calculate_native(int32_t n, int32_t func1, int32_t func2); +void +my_log(uint32 log_level, const char *file, int line, const char *fmt, ...) +{ + char buf[200]; + snprintf(buf, 200, + log_level == WASM_LOG_LEVEL_VERBOSE ? "[WamrLogger - VERBOSE] %s" + : "[WamrLogger] %s", + fmt); + + va_list ap; + va_start(ap, fmt); + vprintf(buf, ap); + va_end(ap); +} + +int +my_vprintf(const char *format, va_list ap) +{ + /* Print in blue */ + char buf[200]; + snprintf(buf, 200, "\x1b[34m%s\x1b[0m", format); + return vprintf(buf, ap); +} + void print_usage(void) { @@ -95,6 +119,7 @@ main(int argc, char *argv_main[]) printf("Init runtime environment failed.\n"); return -1; } + wasm_runtime_set_log_level(WASM_LOG_LEVEL_VERBOSE); buffer = bh_read_file_to_buffer(wasm_path, &buf_size); @@ -103,7 +128,8 @@ main(int argc, char *argv_main[]) goto fail; } - module = wasm_runtime_load(buffer, buf_size, error_buf, sizeof(error_buf)); + module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf, + sizeof(error_buf)); if (!module) { printf("Load wasm module failed. error: %s\n", error_buf); goto fail; From 61fe78c9ff44aef9f173a1687604ceb24c9b5c46 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Wed, 24 Jan 2024 12:42:45 +0700 Subject: [PATCH 44/53] Fix download link for wasi-sdk (#3077) The wasi-sdk repo moved from CraneStation to the WebAssembly org on GitHub. --- doc/build_wasm_app.md | 2 +- samples/file/README.md | 2 +- samples/native-lib/README.md | 2 +- samples/socket-api/README.md | 2 +- tests/benchmarks/coremark/README.md | 2 +- tests/benchmarks/polybench/README.md | 2 +- tests/benchmarks/sightglass/README.md | 2 +- wamr-sdk/README.md | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/build_wasm_app.md b/doc/build_wasm_app.md index 23119456e..7747d9ac3 100644 --- a/doc/build_wasm_app.md +++ b/doc/build_wasm_app.md @@ -5,7 +5,7 @@ Prepare WASM building environments For C and C++, WASI-SDK version 19.0+ is the major tool supported by WAMR to build WASM applications. Also, we can use [Emscripten SDK (EMSDK)](https://github.com/emscripten-core/emsdk), but it is not recommended. And there are some other compilers such as the standard clang compiler, which might also work [here](./other_wasm_compilers.md). -To install WASI SDK, please download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. +To install WASI SDK, please download the [wasi-sdk release](https://github.com/WebAssembly/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. The official *wasi-sdk release* doesn't fully support *latest 128-bit SIMD spec* yet. WAMR provides a script in [build-wasi-sdk](../test-tools/build-wasi-sdk/) to generate another wasi-sdk with *llvm-15* from source code and installs it at *../test-tools/wasi-sdk*. If you plan to build WASM applications with *latest 128-bit SIMD*, please use it instead of the official release. diff --git a/samples/file/README.md b/samples/file/README.md index 8b34719ef..98f0cc3a7 100644 --- a/samples/file/README.md +++ b/samples/file/README.md @@ -5,7 +5,7 @@ This sample can also demonstrate the SGX IPFS (Intel Protected File System), ena ## Preparation -Please install WASI SDK, download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. +Please install WASI SDK, download the [wasi-sdk release](https://github.com/WebAssembly/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. For testing with SGX IPFS, follow the instructions in [the documentation of SGX for WAMR](../../doc/linux_sgx.md#sgx-intel-protected-file-system). ## Build the sample diff --git a/samples/native-lib/README.md b/samples/native-lib/README.md index 80500ade0..be1cc6e74 100644 --- a/samples/native-lib/README.md +++ b/samples/native-lib/README.md @@ -29,7 +29,7 @@ get_native_lib(char **p_module_name, NativeSymbol **p_native_symbols) ## Preparation -Please install WASI SDK, download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. +Please install WASI SDK, download the [wasi-sdk release](https://github.com/WebAssembly/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. ## Build the sample diff --git a/samples/socket-api/README.md b/samples/socket-api/README.md index a3bc5ac15..911dfb7ab 100644 --- a/samples/socket-api/README.md +++ b/samples/socket-api/README.md @@ -6,7 +6,7 @@ how they communicate with each other. ## Preparation -Please install WASI SDK, download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. +Please install WASI SDK, download the [wasi-sdk release](https://github.com/WebAssembly/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. And install wabt, download the [wabt release](https://github.com/WebAssembly/wabt/releases) and extract the archive to default path `/opt/wabt` ## Build the sample diff --git a/tests/benchmarks/coremark/README.md b/tests/benchmarks/coremark/README.md index 4e88069f8..5b874e988 100644 --- a/tests/benchmarks/coremark/README.md +++ b/tests/benchmarks/coremark/README.md @@ -10,7 +10,7 @@ Please build iwasm and wamrc, refer to: - [Build iwasm on Linux](../../../doc/build_wamr.md#linux), or [Build iwasm on MacOS](../../../doc/build_wamr.md#macos) - [Build wamrc AOT compiler](../../../README.md#build-wamrc-aot-compiler) -And install WASI SDK, please download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. +And install WASI SDK, please download the [wasi-sdk release](https://github.com/WebAssembly/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. And then run `./build.sh` to build the source code, file `coremark.exe`, `coremark.wasm` and `coremark.aot` will be generated. diff --git a/tests/benchmarks/polybench/README.md b/tests/benchmarks/polybench/README.md index d8b3db961..7b6623f22 100644 --- a/tests/benchmarks/polybench/README.md +++ b/tests/benchmarks/polybench/README.md @@ -10,7 +10,7 @@ Please build iwasm and wamrc, refer to: - [Build iwasm on Linux](../../../doc/build_wamr.md#linux), or [Build iwasm on MacOS](../../../doc/build_wamr.md#macos) - [Build wamrc AOT compiler](../../../README.md#build-wamrc-aot-compiler) -And install WASI SDK, please download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. +And install WASI SDK, please download the [wasi-sdk release](https://github.com/WebAssembly/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. And then run `./build.sh` to build the source code, the folder `out` will be created and files will be generated under it. diff --git a/tests/benchmarks/sightglass/README.md b/tests/benchmarks/sightglass/README.md index ff34c7d70..3f54fda1b 100644 --- a/tests/benchmarks/sightglass/README.md +++ b/tests/benchmarks/sightglass/README.md @@ -10,7 +10,7 @@ Please build iwasm and wamrc, refer to: - [Build iwasm on Linux](../../../doc/build_wamr.md#linux), or [Build iwasm on MacOS](../../../doc/build_wamr.md#macos) - [Build wamrc AOT compiler](../../../README.md#build-wamrc-aot-compiler) -And install WASI SDK, please download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. +And install WASI SDK, please download the [wasi-sdk release](https://github.com/WebAssembly/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`. And then run `./build.sh` to build the source code, the folder `out` will be created and files will be generated under it. diff --git a/wamr-sdk/README.md b/wamr-sdk/README.md index 14b172e02..fd926af86 100644 --- a/wamr-sdk/README.md +++ b/wamr-sdk/README.md @@ -8,7 +8,7 @@ Usually there are two tasks for integrating the WAMR into a particular project: The **[WAMR SDK](./wamr-sdk)** tools is helpful to finish the two tasks quickly. It supports menu configuration for selecting WAMR components and builds the WAMR to a SDK package that includes **runtime SDK** and **APP SDK**. The runtime SDK is used for building the native application and the APP SDK should be shipped to WASM application developers. -**Note**: [WASI-SDK](https://github.com/CraneStation/wasi-sdk/releases) version 7 and above should be installed before building the WAMR SDK. +**Note**: [WASI-SDK](https://github.com/WebAssembly/wasi-sdk/releases) version 7 and above should be installed before building the WAMR SDK. From d815bbc3dbd455bd4afa59a3339d9881e0f294a0 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Wed, 24 Jan 2024 14:35:45 +0700 Subject: [PATCH 45/53] README.md: Fix typo `tunning` to `tuning` (#3078) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dfdae2da8..31156b9cc 100644 --- a/README.md +++ b/README.md @@ -75,9 +75,9 @@ The following platforms are supported, click each link below for how to build iw - [Blog: The WAMR memory model](https://bytecodealliance.github.io/wamr.dev/blog/the-wamr-memory-model/) - [Blog: Understand WAMR heaps](https://bytecodealliance.github.io/wamr.dev/blog/understand-the-wamr-heaps/) and [stacks](https://bytecodealliance.github.io/wamr.dev/blog/understand-the-wamr-stacks/) - [Blog: Introduction to WAMR running modes](https://bytecodealliance.github.io/wamr.dev/blog/introduction-to-wamr-running-modes/) -- [Memory usage tunning](./doc/memory_tune.md): the memory model and how to tune the memory usage +- [Memory usage tuning](./doc/memory_tune.md): the memory model and how to tune the memory usage - [Memory usage profiling](./doc/build_wamr.md#enable-memory-profiling-experiment): how to profile the memory usage -- [Performance tunning](./doc/perf_tune.md): how to tune the performance +- [Performance tuning](./doc/perf_tune.md): how to tune the performance - [Benchmarks](./tests/benchmarks): checkout these links for how to run the benchmarks: [PolyBench](./tests/benchmarks/polybench), [CoreMark](./tests/benchmarks/coremark), [Sightglass](./tests/benchmarks/sightglass), [JetStream2](./tests/benchmarks/jetstream) - [Performance and footprint data](https://github.com/bytecodealliance/wasm-micro-runtime/wiki/Performance): the performance and footprint data From bf9fb2e680892f2c298104c37d608cf182c8d345 Mon Sep 17 00:00:00 2001 From: Gavin Hayes Date: Wed, 24 Jan 2024 03:04:48 -0500 Subject: [PATCH 46/53] cosmopolitan: Update compiler and update platform_internal.h (#3079) This fixes the cosmopolitan platform. - Switch `build_cosmocc.sh` and platform documentation to explicitly use the x86_64 cosmocc compiler as multi-arch cosmocc won't work here. Older version `cosmocc` just did a x86_64 build. - Add missing items from `platform_internal.h` to fix build. --- core/shared/platform/cosmopolitan/platform_internal.h | 7 +++++++ product-mini/README.md | 6 +++--- product-mini/platforms/cosmopolitan/build_cosmocc.sh | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/shared/platform/cosmopolitan/platform_internal.h b/core/shared/platform/cosmopolitan/platform_internal.h index 66515e305..726021160 100644 --- a/core/shared/platform/cosmopolitan/platform_internal.h +++ b/core/shared/platform/cosmopolitan/platform_internal.h @@ -55,6 +55,7 @@ typedef pthread_t korp_tid; 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; #define OS_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER @@ -67,6 +68,12 @@ typedef int os_file_handle; typedef DIR *os_dir_stream; typedef int os_raw_file_handle; +static inline os_file_handle +os_get_invalid_handle() +{ + return -1; +} + #if WASM_DISABLE_WRITE_GS_BASE == 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) #define os_writegsbase(base_addr) \ diff --git a/product-mini/README.md b/product-mini/README.md index 22acfeaf4..4a8275618 100644 --- a/product-mini/README.md +++ b/product-mini/README.md @@ -447,12 +447,12 @@ make ## Cosmopolitan Libc Currently, only x86_64 architecture with interpreter modes is supported. -Clone the Cosmopolitan Libc. Setup `cosmocc` as described in [Getting Started](https://github.com/jart/cosmopolitan/#getting-started) being sure to get it into `PATH`. +Setup `cosmocc` as described in [Getting Started](https://github.com/jart/cosmopolitan/#getting-started) being sure to get its `bin` directory into `PATH`. Build iwasm ``` Bash -export CC=cosmocc -export CXX=cosmoc++ +export CC=x86_64-unknown-cosmo-cc +export CXX=x86_64-unknown-cosmo-c++ rm -rf build mkdir build cmake -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1 -B build diff --git a/product-mini/platforms/cosmopolitan/build_cosmocc.sh b/product-mini/platforms/cosmopolitan/build_cosmocc.sh index b2ce15d33..8d96027e1 100755 --- a/product-mini/platforms/cosmopolitan/build_cosmocc.sh +++ b/product-mini/platforms/cosmopolitan/build_cosmocc.sh @@ -2,8 +2,8 @@ # Copyright (C) 2023 Dylibso. All rights reserved. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -export CC=cosmocc -export CXX=cosmoc++ +export CC=x86_64-unknown-cosmo-cc +export CXX=x86_64-unknown-cosmo-c++ rm -rf build mkdir build cmake -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1 -B build From 9afbeab1c8936926f8a4d6c8bd23116b7fe41f8a Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 25 Jan 2024 17:29:34 +0900 Subject: [PATCH 47/53] wasi: Apply wasm_runtime_begin_blocking_op to poll as well (#3080) While we used a different approach for poll_oneoff [1], the implementation works only when the poll list includes an absolute clock event. That is, if we have a thread which is polling on descriptors without a timeout, we fail to terminate the thread. This commit fixes it by applying wasm_runtime_begin_blocking_op to poll as well. [1] https://github.com/bytecodealliance/wasm-micro-runtime/pull/1951 --- .../src/blocking_op.c | 21 +++++++++++++++++++ .../src/blocking_op.h | 8 ++++++- .../sandboxed-system-primitives/src/posix.c | 9 ++++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c index ec1481c2e..4dcc4f5b0 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.c @@ -7,6 +7,7 @@ #include "ssp_config.h" #include "blocking_op.h" +#include "libc_errno.h" __wasi_errno_t blocking_op_close(wasm_exec_env_t exec_env, os_file_handle handle, @@ -170,3 +171,23 @@ blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle, wasm_runtime_end_blocking_op(exec_env); return error; } + +#ifndef BH_PLATFORM_WINDOWS +/* REVISIT: apply the os_file_handle style abstraction for pollfd? */ +__wasi_errno_t +blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds, + int timeout_ms, int *retp) +{ + int ret; + if (!wasm_runtime_begin_blocking_op(exec_env)) { + return __WASI_EINTR; + } + ret = poll(pfds, nfds, timeout_ms); + wasm_runtime_end_blocking_op(exec_env); + if (ret == -1) { + return convert_errno(errno); + } + *retp = ret; + return 0; +} +#endif diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h index afaa4a4f0..9c36d7df6 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/blocking_op.h @@ -50,4 +50,10 @@ __wasi_errno_t blocking_op_openat(wasm_exec_env_t exec_env, os_file_handle handle, const char *path, __wasi_oflags_t oflags, __wasi_fdflags_t fd_flags, __wasi_lookupflags_t lookup_flags, - wasi_libc_file_access_mode access_mode, os_file_handle *out); \ No newline at end of file + wasi_libc_file_access_mode access_mode, os_file_handle *out); + +#ifndef BH_PLATFORM_WINDOWS +__wasi_errno_t +blocking_op_poll(wasm_exec_env_t exec_env, struct pollfd *pfds, nfds_t nfds, + int timeout, int *retp); +#endif diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index 2363c73d2..03ff50a3f 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -2230,11 +2230,10 @@ wasmtime_ssp_poll_oneoff(wasm_exec_env_t exec_env, struct fd_table *curfds, timeout = -1; } - int ret = poll(pfds, nsubscriptions, timeout); - - __wasi_errno_t error = 0; - if (ret == -1) { - error = convert_errno(errno); + int ret; + int error = blocking_op_poll(exec_env, pfds, nsubscriptions, timeout, &ret); + if (error != 0) { + /* got an error */ } else if (ret == 0 && *nevents == 0 && clock_subscription != NULL) { // No events triggered. Trigger the clock event. From 6daaf6d27a014ecbc4aa67995122f7507a8ea084 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Thu, 25 Jan 2024 17:19:14 +0800 Subject: [PATCH 48/53] Fix inconsistent code style in aot_loader.c (#3082) --- core/iwasm/aot/aot_loader.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 948ac4dfd..31ccb3853 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -66,15 +66,15 @@ exchange_uint32(uint8 *p_data) } static void -exchange_uint64(uint8 *pData) +exchange_uint64(uint8 *p_data) { uint32 value; - value = *(uint32 *)pData; - *(uint32 *)pData = *(uint32 *)(pData + 4); - *(uint32 *)(pData + 4) = value; - exchange_uint32(pData); - exchange_uint32(pData + 4); + value = *(uint32 *)p_data; + *(uint32 *)p_data = *(uint32 *)(p_data + 4); + *(uint32 *)(p_data + 4) = value; + exchange_uint32(p_data); + exchange_uint32(p_data + 4); } static union { From 313ce8cb6107e7237464b5eb4bb0c05b395008c0 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 26 Jan 2024 09:45:59 +0800 Subject: [PATCH 49/53] Fix memory/table segment checks in memory.init/table.init (#3081) According to the wasm core spec, the checks for the table segments in `table.init` opcode are similar to the checks for `memory.init` opcode: - The size of a passive segment is shrunk to zero after `data.drop` (or `elem.drop`) opcode is executed, and the segment can be used to do `memory.init` (or `table.init`) again - The `memory.init` only traps when `s+n > len(data.data)` or `d+n > len(mem.data)` and `table.init` only traps when `s+n > len(elem.elem)` or `d+n > len(tab.elem)` - The active segment can also be used to do `memory.init` (or `table.init`), while it behaves like a dropped passive segment https://github.com/WebAssembly/bulk-memory-operations/blob/master/proposals/bulk-memory-operations/Overview.md ``` Segments can also be shrunk to size zero by using the following new instructions: - data.drop: discard the data in an data segment - elem.drop: discard the data in an element segment An active segment is equivalent to a passive segment, but with an implicit memory.init followed by a data.drop (or table.init followed by a elem.drop) that is prepended to the module's start function. ``` ps. https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-memorymathsfmemoryinitx%E2%91%A0 https://webassembly.github.io/spec/core/bikeshed/#-hrefsyntax-instr-tablemathsftableinitxy%E2%91%A0 https://github.com/bytecodealliance/wasm-micro-runtime/issues/3020 --- core/iwasm/aot/aot_runtime.c | 33 ++++++++++-------- core/iwasm/fast-jit/fe/jit_emit_table.c | 25 ++++++-------- core/iwasm/interpreter/wasm_interp_classic.c | 35 +++++++------------ core/iwasm/interpreter/wasm_interp_fast.c | 36 +++++++------------- core/iwasm/interpreter/wasm_runtime.c | 30 +++++++++------- 5 files changed, 73 insertions(+), 86 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 64359191d..11a19e939 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1179,6 +1179,10 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, "failed to allocate bitmaps"); goto fail; } + for (i = 0; i < module->mem_init_data_count; i++) { + if (!module->mem_init_data_list[i]->is_passive) + bh_bitmap_set_bit(common->data_dropped, i); + } } #endif #if WASM_ENABLE_REF_TYPES != 0 @@ -1190,6 +1194,10 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, "failed to allocate bitmaps"); goto fail; } + for (i = 0; i < module->table_init_data_count; i++) { + if (wasm_elem_is_active(module->table_init_data_list[i]->mode)) + bh_bitmap_set_bit(common->elem_dropped, i); + } } #endif @@ -2621,6 +2629,7 @@ aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, { AOTTableInstance *tbl_inst; AOTTableInitData *tbl_seg; + uint32 *tbl_seg_elems = NULL, tbl_seg_len = 0; const AOTModule *module = (AOTModule *)module_inst->module; tbl_inst = module_inst->tables[tbl_idx]; @@ -2629,7 +2638,15 @@ aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, tbl_seg = module->table_init_data_list[tbl_seg_idx]; bh_assert(tbl_seg); - if (offset_len_out_of_bounds(src_offset, length, tbl_seg->func_index_count) + if (!bh_bitmap_get_bit( + ((AOTModuleInstanceExtra *)module_inst->e)->common.elem_dropped, + tbl_seg_idx)) { + /* table segment isn't dropped */ + tbl_seg_elems = tbl_seg->func_indexes; + tbl_seg_len = tbl_seg->func_index_count; + } + + if (offset_len_out_of_bounds(src_offset, length, tbl_seg_len) || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) { aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); return; @@ -2639,22 +2656,10 @@ aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx, return; } - if (bh_bitmap_get_bit( - ((AOTModuleInstanceExtra *)module_inst->e)->common.elem_dropped, - tbl_seg_idx)) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - - if (!wasm_elem_is_passive(tbl_seg->mode)) { - aot_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - bh_memcpy_s((uint8 *)tbl_inst + offsetof(AOTTableInstance, elems) + dst_offset * sizeof(uint32), (tbl_inst->cur_size - dst_offset) * sizeof(uint32), - tbl_seg->func_indexes + src_offset, length * sizeof(uint32)); + tbl_seg_elems + src_offset, length * sizeof(uint32)); } void diff --git a/core/iwasm/fast-jit/fe/jit_emit_table.c b/core/iwasm/fast-jit/fe/jit_emit_table.c index a2f5ce513..26bc35394 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_table.c +++ b/core/iwasm/fast-jit/fe/jit_emit_table.c @@ -88,17 +88,21 @@ fail: } static int -wasm_init_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 elem_idx, +wasm_init_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 seg_idx, uint32 dst_offset, uint32 len, uint32 src_offset) { WASMTableInstance *tbl; uint32 tbl_sz; - WASMTableSeg *elem; - uint32 elem_len; + WASMTableSeg *tbl_seg = inst->module->table_segments + seg_idx; + uint32 *tbl_seg_elems = NULL, tbl_seg_len = 0; - elem = inst->module->table_segments + elem_idx; - elem_len = elem->function_count; - if (offset_len_out_of_bounds(src_offset, len, elem_len)) + if (!bh_bitmap_get_bit(inst->e->common.elem_dropped, seg_idx)) { + /* table segment isn't dropped */ + tbl_seg_elems = tbl_seg->func_indexes; + tbl_seg_len = tbl_seg->function_count; + } + + if (offset_len_out_of_bounds(src_offset, len, tbl_seg_len)) goto out_of_bounds; tbl = inst->tables[tbl_idx]; @@ -109,17 +113,10 @@ wasm_init_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 elem_idx, if (!len) return 0; - if (bh_bitmap_get_bit(inst->e->common.elem_dropped, elem_idx)) - goto out_of_bounds; - - if (!wasm_elem_is_passive(inst->module->table_segments[elem_idx].mode)) - goto out_of_bounds; - bh_memcpy_s((uint8 *)tbl + offsetof(WASMTableInstance, elems) + dst_offset * sizeof(uint32), (uint32)((tbl_sz - dst_offset) * sizeof(uint32)), - elem->func_indexes + src_offset, - (uint32)(len * sizeof(uint32))); + tbl_seg_elems + src_offset, (uint32)(len * sizeof(uint32))); return 0; out_of_bounds: diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 8c3f0ae6f..0c0ab1f5b 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -3262,6 +3262,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, uint32 tbl_idx, elem_idx; uint32 n, s, d; WASMTableInstance *tbl_inst; + uint32 *tbl_seg_elems = NULL, tbl_seg_len = 0; read_leb_uint32(frame_ip, frame_ip_end, elem_idx); bh_assert(elem_idx < module->module->table_seg_count); @@ -3275,10 +3276,18 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, s = (uint32)POP_I32(); d = (uint32)POP_I32(); - if (offset_len_out_of_bounds( - s, n, + if (!bh_bitmap_get_bit(module->e->common.elem_dropped, + elem_idx)) { + /* table segment isn't dropped */ + tbl_seg_elems = module->module->table_segments[elem_idx] - .function_count) + .func_indexes; + tbl_seg_len = + module->module->table_segments[elem_idx] + .function_count; + } + + if (offset_len_out_of_bounds(s, n, tbl_seg_len) || offset_len_out_of_bounds(d, n, tbl_inst->cur_size)) { wasm_set_exception(module, @@ -3290,30 +3299,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, break; } - if (bh_bitmap_get_bit(module->e->common.elem_dropped, - elem_idx)) { - wasm_set_exception(module, - "out of bounds table access"); - goto got_exception; - } - - if (!wasm_elem_is_passive( - module->module->table_segments[elem_idx] - .mode)) { - wasm_set_exception(module, - "out of bounds table access"); - goto got_exception; - } - bh_memcpy_s( (uint8 *)tbl_inst + offsetof(WASMTableInstance, elems) + d * sizeof(uint32), (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), - module->module->table_segments[elem_idx] - .func_indexes - + s, - (uint32)(n * sizeof(uint32))); + tbl_seg_elems + s, (uint32)(n * sizeof(uint32))); break; } diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 86aaa5e6d..889dafbf7 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -3023,7 +3023,6 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, data = NULL; } else { - seg_len = (uint64)module->module->data_segments[segment] ->data_length; @@ -3106,6 +3105,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, uint32 tbl_idx, elem_idx; uint32 n, s, d; WASMTableInstance *tbl_inst; + uint32 *tbl_seg_elems = NULL, tbl_seg_len = 0; elem_idx = read_uint32(frame_ip); bh_assert(elem_idx < module->module->table_seg_count); @@ -3119,10 +3119,18 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, s = (uint32)POP_I32(); d = (uint32)POP_I32(); - if (offset_len_out_of_bounds( - s, n, + if (!bh_bitmap_get_bit(module->e->common.elem_dropped, + elem_idx)) { + /* table segment isn't dropped */ + tbl_seg_elems = module->module->table_segments[elem_idx] - .function_count) + .func_indexes; + tbl_seg_len = + module->module->table_segments[elem_idx] + .function_count; + } + + if (offset_len_out_of_bounds(s, n, tbl_seg_len) || offset_len_out_of_bounds(d, n, tbl_inst->cur_size)) { wasm_set_exception(module, @@ -3134,30 +3142,12 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, break; } - if (bh_bitmap_get_bit(module->e->common.elem_dropped, - elem_idx)) { - wasm_set_exception(module, - "out of bounds table access"); - goto got_exception; - } - - if (!wasm_elem_is_passive( - module->module->table_segments[elem_idx] - .mode)) { - wasm_set_exception(module, - "out of bounds table access"); - goto got_exception; - } - bh_memcpy_s( (uint8 *)tbl_inst + offsetof(WASMTableInstance, elems) + d * sizeof(uint32), (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)), - module->module->table_segments[elem_idx] - .func_indexes - + s, - (uint32)(n * sizeof(uint32))); + tbl_seg_elems + s, (uint32)(n * sizeof(uint32))); break; } case WASM_OP_ELEM_DROP: diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 560a14c12..79454539d 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1670,6 +1670,10 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, "failed to allocate bitmaps"); goto fail; } + for (i = 0; i < module->data_seg_count; i++) { + if (!module->data_segments[i]->is_passive) + bh_bitmap_set_bit(module_inst->e->common.data_dropped, i); + } } #endif #if WASM_ENABLE_REF_TYPES != 0 @@ -1682,6 +1686,10 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, "failed to allocate bitmaps"); goto fail; } + for (i = 0; i < module->table_seg_count; i++) { + if (wasm_elem_is_active(module->table_segments[i].mode)) + bh_bitmap_set_bit(module_inst->e->common.elem_dropped, i); + } } #endif @@ -3278,6 +3286,7 @@ llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx, { WASMTableInstance *tbl_inst; WASMTableSeg *tbl_seg; + uint32 *tbl_seg_elems = NULL, tbl_seg_len = 0; bh_assert(module_inst->module_type == Wasm_Module_Bytecode); @@ -3287,7 +3296,13 @@ llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx, bh_assert(tbl_inst); bh_assert(tbl_seg); - if (offset_len_out_of_bounds(src_offset, length, tbl_seg->function_count) + if (!bh_bitmap_get_bit(module_inst->e->common.elem_dropped, tbl_seg_idx)) { + /* table segment isn't dropped */ + tbl_seg_elems = tbl_seg->func_indexes; + tbl_seg_len = tbl_seg->function_count; + } + + if (offset_len_out_of_bounds(src_offset, length, tbl_seg_len) || offset_len_out_of_bounds(dst_offset, length, tbl_inst->cur_size)) { jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); return; @@ -3297,21 +3312,10 @@ llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx, return; } - if (bh_bitmap_get_bit(module_inst->e->common.elem_dropped, tbl_seg_idx)) { - jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - - if (!wasm_elem_is_passive(tbl_seg->mode)) { - jit_set_exception_with_id(module_inst, EXCE_OUT_OF_BOUNDS_TABLE_ACCESS); - return; - } - bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems) + dst_offset * sizeof(uint32), (uint32)sizeof(uint32) * (tbl_inst->cur_size - dst_offset), - tbl_seg->func_indexes + src_offset, - (uint32)(length * sizeof(uint32))); + tbl_seg_elems + src_offset, (uint32)(length * sizeof(uint32))); } void From ad46290770e1be0e65dbe4bd42899514b0cfaf29 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 26 Jan 2024 16:55:27 +0800 Subject: [PATCH 50/53] Update outdated reference link in multi_module.md (#3092) --- doc/multi_module.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/multi_module.md b/doc/multi_module.md index 1c22863f9..9ab26673e 100644 --- a/doc/multi_module.md +++ b/doc/multi_module.md @@ -6,7 +6,7 @@ WAMR loads all dependencies recursively according to the _import section_ of a m > WAMR only implements the load-time dynamic linking. Please refer to [dynamic linking](https://webassembly.org/docs/dynamic-linking/) for more details. -WAMR follows [WASI Command/Reactor Model](https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md#current-unstable-abi). The WASI model separates modules into commands and reactors. A Command is the main module that requires exports of reactors(submodules). +WAMR follows [WASI Command/Reactor Model](https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md#current-unstable-abi). The WASI model separates modules into commands and reactors. A Command is the main module that requires exports of reactors(submodules). if `WASM_ENABLE_LIBC_WASI` is enabled, any module imports a WASI APIs, like `(import "wasi_snapshot_preview1" "XXX")`, should follow restrictions of the _WASI application ABI_: From 9fb5fcc70935b8c7ed92c8b9419224c3bb04fb43 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 26 Jan 2024 17:02:24 +0800 Subject: [PATCH 51/53] Add comments to suppress warning from clang-tidy (#3088) Suppress style warnings for macro definition, name of these macros is inconsistent with others (upper case). --- core/iwasm/aot/aot_loader.c | 4 ++++ core/iwasm/aot/aot_runtime.c | 2 +- core/iwasm/common/wasm_application.c | 2 +- core/iwasm/common/wasm_runtime_common.c | 6 +++++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 31ccb3853..282dc1d67 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -214,6 +214,8 @@ GET_U16_FROM_ADDR(const uint8 *p) p += sizeof(type); \ } while (0) +/* NOLINTBEGIN, disable lint for this region with clang-tidy */ + #define read_byte_array(p, p_end, addr, len) \ do { \ CHECK_BUF(p, p_end, len); \ @@ -236,6 +238,8 @@ GET_U16_FROM_ADDR(const uint8 *p) #define read_uint32(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint32) #define read_uint64(p, p_end, res) TEMPLATE_READ(p, p_end, res, uint64) +/* NOLINTEND */ + /* Legal values for bin_type */ #define BIN_TYPE_ELF32L 0 /* 32-bit little endian */ #define BIN_TYPE_ELF32B 1 /* 32-bit big endian */ diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 11a19e939..ad57c4c0a 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1474,7 +1474,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, (void)jmpbuf_node_pop; return ret; } -#define invoke_native_internal invoke_native_with_hw_bound_check +#define invoke_native_internal invoke_native_with_hw_bound_check /* NOLINT */ #else /* else of OS_ENABLE_HW_BOUND_CHECK */ static inline bool invoke_native_internal(WASMExecEnv *exec_env, void *func_ptr, diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index 93c9342da..e13f7f843 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -46,7 +46,7 @@ static union { char b; } __ue = { .a = 1 }; -#define is_little_endian() (__ue.b == 1) +#define is_little_endian() (__ue.b == 1) /* NOLINT */ /** * Implementation of wasm_application_execute_main() diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 4434a2a0f..eaee4f225 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -785,6 +785,7 @@ align_ptr(const uint8 *p, uint32 b) return false; \ } while (0) +/* NOLINTNEXTLINE */ #define read_uint16(p, p_end, res) \ do { \ p = (uint8 *)align_ptr(p, sizeof(uint16)); \ @@ -793,6 +794,7 @@ align_ptr(const uint8 *p, uint32 b) p += sizeof(uint16); \ } while (0) +/* NOLINTNEXTLINE */ #define read_uint32(p, p_end, res) \ do { \ p = (uint8 *)align_ptr(p, sizeof(uint32)); \ @@ -3537,7 +3539,7 @@ static union { char b; } __ue = { .a = 1 }; -#define is_little_endian() (__ue.b == 1) +#define is_little_endian() (__ue.b == 1) /* NOLINT */ bool wasm_runtime_register_natives(const char *module_name, @@ -4446,6 +4448,7 @@ typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64 *, uint64); typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64 *, uint64); typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64 *, uint64); +/* NOLINTBEGIN */ static volatile Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative; static volatile Float32FuncPtr invokeNative_Float32 = @@ -4461,6 +4464,7 @@ static volatile VoidFuncPtr invokeNative_Void = typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64 *, uint64); static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative; #endif +/* NOLINTEND */ #if defined(_WIN32) || defined(_WIN32_) #define MAX_REG_FLOATS 4 From 99bbad8cdb9e23dbb040102e4531d4cdfbc00711 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Fri, 26 Jan 2024 18:06:21 +0800 Subject: [PATCH 52/53] perf profiling: Adjust the calculation of execution time (#3089) --- core/iwasm/aot/aot_runtime.c | 7 +++---- core/iwasm/interpreter/wasm_interp_classic.c | 7 +++---- core/iwasm/interpreter/wasm_interp_fast.c | 8 +++----- core/iwasm/interpreter/wasm_runtime.c | 7 +++---- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index ad57c4c0a..731cacbea 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -2845,14 +2845,13 @@ aot_free_frame(WASMExecEnv *exec_env) AOTFrame *prev_frame = cur_frame->prev_frame; #if WASM_ENABLE_PERF_PROFILING != 0 - cur_frame->func_perf_prof_info->total_exec_time += - os_time_thread_cputime_us() - cur_frame->time_started; + uint64 elapsed = os_time_thread_cputime_us() - cur_frame->time_started; + cur_frame->func_perf_prof_info->total_exec_time += elapsed; cur_frame->func_perf_prof_info->total_exec_cnt++; /* parent function */ if (prev_frame) - prev_frame->func_perf_prof_info->children_exec_time = - cur_frame->func_perf_prof_info->total_exec_time; + prev_frame->func_perf_prof_info->children_exec_time += elapsed; #endif wasm_exec_env_free_wasm_frame(exec_env, cur_frame); diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 0c0ab1f5b..7364a25fa 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -867,13 +867,12 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame) #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { WASMInterpFrame *prev_frame = frame->prev_frame; - frame->function->total_exec_time += - os_time_thread_cputime_us() - frame->time_started; + uint64 elapsed = os_time_thread_cputime_us() - frame->time_started; + frame->function->total_exec_time += elapsed; frame->function->total_exec_cnt++; if (prev_frame && prev_frame->function) - prev_frame->function->children_exec_time += - frame->function->total_exec_time; + prev_frame->function->children_exec_time += elapsed; } #endif wasm_exec_env_free_wasm_frame(exec_env, frame); diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 889dafbf7..521f547f0 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -901,14 +901,12 @@ FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame) #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { WASMInterpFrame *prev_frame = frame->prev_frame; - - frame->function->total_exec_time += - os_time_thread_cputime_us() - frame->time_started; + uint64 elapsed = os_time_thread_cputime_us() - frame->time_started; + frame->function->total_exec_time += elapsed; frame->function->total_exec_cnt++; if (prev_frame && prev_frame->function) - prev_frame->function->children_exec_time += - frame->function->total_exec_time; + prev_frame->function->children_exec_time += elapsed; } #endif wasm_exec_env_free_wasm_frame(exec_env, frame); diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 79454539d..43844f2c6 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -3455,14 +3455,13 @@ llvm_jit_free_frame(WASMExecEnv *exec_env) #if WASM_ENABLE_PERF_PROFILING != 0 if (frame->function) { - frame->function->total_exec_time += - os_time_thread_cputime_us() - frame->time_started; + uint64 elapsed = os_time_thread_cputime_us() - frame->time_started; + frame->function->total_exec_time += elapsed; frame->function->total_exec_cnt++; /* parent function */ if (prev_frame) - prev_frame->function->children_exec_time = - frame->function->total_exec_time; + prev_frame->function->children_exec_time += elapsed; } #endif wasm_exec_env_free_wasm_frame(exec_env, frame); From 83c3ef0f467def718ea5e829f7dfff711d4c0ef3 Mon Sep 17 00:00:00 2001 From: tkernelcn <90441159+tkernelcn@users.noreply.github.com> Date: Fri, 26 Jan 2024 18:38:55 +0800 Subject: [PATCH 53/53] freertos: Thread exit more common (#3094) In native side, even not call `os_thread_exit` at end of thread code, can also exit correctly. --- core/shared/platform/common/freertos/freertos_thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shared/platform/common/freertos/freertos_thread.c b/core/shared/platform/common/freertos/freertos_thread.c index e3524ac60..8d57fda53 100644 --- a/core/shared/platform/common/freertos/freertos_thread.c +++ b/core/shared/platform/common/freertos/freertos_thread.c @@ -204,7 +204,7 @@ os_thread_wrapper(void *arg) thread_data_list_add(thread_data); thread_data->start_routine(thread_data->arg); - os_thread_cleanup(); + os_thread_exit(NULL); } int