From bf78863c5611de857caae3a998cbbf441dbd90cd Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Thu, 7 Nov 2024 13:38:42 +0800 Subject: [PATCH 01/28] Wasm loader enhancement: check code size in code entry (#3892) add wasm loader check: in code entry, the code size should match the size of vec(locals) + expr, and expr should end with opcode end --- core/iwasm/interpreter/wasm_loader.c | 26 +++++++++++++++-------- core/iwasm/interpreter/wasm_mini_loader.c | 2 ++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index ae823d7be..04fa2473e 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -3610,6 +3610,17 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, #endif } + /* Code size in code entry can't be smaller than size of vec(locals) + * + expr(at least 1 for opcode end). And expressions are encoded by + * their instruction sequence terminated with an explicit 0x0B + * opcode for end. */ + if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) { + set_error_buf( + error_buf, error_buf_size, + "section size mismatch: function body END opcode expected"); + return false; + } + /* Alloc memory, layout: function structure + local types */ code_size = (uint32)(p_code_end - p_code); @@ -15837,15 +15848,12 @@ re_scan: } if (loader_ctx->csp_num > 0) { - if (cur_func_idx < module->function_count - 1) - /* Function with missing end marker (between two functions) */ - set_error_buf(error_buf, error_buf_size, "END opcode expected"); - else - /* Function with missing end marker - (at EOF or end of code sections) */ - set_error_buf(error_buf, error_buf_size, - "unexpected end of section or function, " - "or section size mismatch"); + /* unmatched end opcodes result from unbalanced control flow structures, + * for example, br_table with inconsistent target count (1 declared, 2 + * given), or simply superfluous end opcodes */ + set_error_buf( + error_buf, error_buf_size, + "unexpected end opcodes from unbalanced control flow structures"); goto fail; } diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 0d1f83704..006a38c1c 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -1183,6 +1183,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, local_count += sub_local_count; } + bh_assert(p_code_end > p_code && *(p_code_end - 1) == WASM_OP_END); + /* Alloc memory, layout: function structure + local types */ code_size = (uint32)(p_code_end - p_code); From 397f6633491052a3d4607039f6a87834b9a8524e Mon Sep 17 00:00:00 2001 From: Fadumina Barre Date: Fri, 8 Nov 2024 08:26:37 +0000 Subject: [PATCH 02/28] fix(uwp): Gate NTSTATUS definition behind WINAPI_PARTITION_DESKTOP for UWP builds --- core/shared/platform/windows/win_clock.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/shared/platform/windows/win_clock.c b/core/shared/platform/windows/win_clock.c index ec0bc8566..c402330aa 100644 --- a/core/shared/platform/windows/win_clock.c +++ b/core/shared/platform/windows/win_clock.c @@ -10,9 +10,11 @@ #define NANOSECONDS_PER_SECOND 1000000000ULL #define NANOSECONDS_PER_TICK 100 +#if WINAPI_PARTITION_DESKTOP extern NTSTATUS NtQueryTimerResolution(PULONG MinimumResolution, PULONG MaximumResolution, PULONG CurrentResolution); +#endif static __wasi_errno_t calculate_monotonic_clock_frequency(uint64 *out_frequency) From fdda259d362b418f1f2a624c10d5a44bcdf81d51 Mon Sep 17 00:00:00 2001 From: James Ring Date: Tue, 12 Nov 2024 22:52:27 -0800 Subject: [PATCH 03/28] Fix linked global initialization in multimodule (#3905) While resolving linked globals in multi-module mode, WAMR tries to copy the linked global's initial value into the destination global in the current module. However, a bug in the implementation causes the copy to be done from the InitializerExpression struct, not from its WASMValue field. This did not come up in WAMR's spec test runner because those are built with WASM_ENABLE_SPEC_TEST, which means these globals are resolved as builtins, not linked globals, which goes through a different (presumably not faulty) path. --- core/iwasm/interpreter/wasm_runtime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index accb40319..0f1ccd937 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1209,7 +1209,7 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst, /* The linked global instance has been initialized, we just need to copy the value. */ bh_memcpy_s(&(global->initial_value), sizeof(WASMValue), - &(global_import->import_global_linked->init_expr), + &(global_import->import_global_linked->init_expr.u), sizeof(WASMValue)); } else From 75f5fa46ab7064951b7f1fa4487c67f01a9762b5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:11:11 +0800 Subject: [PATCH 04/28] build(deps): bump github/codeql-action from 3.27.0 to 3.27.1 (#3902) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.27.0 to 3.27.1. - [Release notes](https://github.com/github/codeql-action/releases) - [Commits](https://github.com/github/codeql-action/compare/v3.27.0...v3.27.1) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql.yml | 6 +++--- .github/workflows/supply_chain.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 62bcd0c31..a3228e20d 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -53,7 +53,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3.27.0 + uses: github/codeql-action/init@v3.27.1 with: languages: ${{ matrix.language }} @@ -70,7 +70,7 @@ jobs: - run: | ./.github/scripts/codeql_buildscript.sh - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3.27.0 + uses: github/codeql-action/analyze@v3.27.1 with: category: "/language:${{matrix.language}}" upload: false @@ -99,7 +99,7 @@ jobs: output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif - name: Upload CodeQL results to code scanning - uses: github/codeql-action/upload-sarif@v3.27.0 + uses: github/codeql-action/upload-sarif@v3.27.1 with: sarif_file: ${{ steps.step1.outputs.sarif-output }} category: "/language:${{matrix.language}}" diff --git a/.github/workflows/supply_chain.yml b/.github/workflows/supply_chain.yml index 5e331c08c..28efecc47 100644 --- a/.github/workflows/supply_chain.yml +++ b/.github/workflows/supply_chain.yml @@ -60,6 +60,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@3aa71356c75a8edd8430d54dff2982203a28be45 # v2.2.4 + uses: github/codeql-action/upload-sarif@acb9cb18eec7e3a113ef83cff0be91e75cfd9526 # v2.2.4 with: sarif_file: results.sarif From 226bf22f9e61c33345d831c16e756ec40a90b689 Mon Sep 17 00:00:00 2001 From: James Ring Date: Tue, 12 Nov 2024 23:11:33 -0800 Subject: [PATCH 05/28] GlobalValueSet was moved to IRPartitionLayer recently, but we have a local definition anyway (#3899) --- core/iwasm/compilation/aot_orc_extra.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/iwasm/compilation/aot_orc_extra.cpp b/core/iwasm/compilation/aot_orc_extra.cpp index dad9e04c0..d9cf3e711 100644 --- a/core/iwasm/compilation/aot_orc_extra.cpp +++ b/core/iwasm/compilation/aot_orc_extra.cpp @@ -177,7 +177,7 @@ LLVMOrcLLLazyJITBuilderSetJITTargetMachineBuilder( LLVMOrcDisposeJITTargetMachineBuilder(JTMP); } -static Optional +static Optional PartitionFunction(GlobalValueSet Requested) { std::vector GVsToAdd; From 0e4dffc47922bb6fcdcaed7de2a6edfe8c48a7cd Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:16:13 +0800 Subject: [PATCH 06/28] Fix a leak in wasm_loader_emit_br_info (#3900) Reference Info: 377955855 wamr:wasm_mutator_fuzz_loader: Direct-leak in wasm_loader_emit_br_info https://issues.oss-fuzz.com/issues/377955855 --- core/iwasm/common/wasm_application.c | 3 ++- core/iwasm/interpreter/wasm_loader.c | 22 +++++++++++++--------- core/iwasm/interpreter/wasm_mini_loader.c | 18 +++++++++--------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/core/iwasm/common/wasm_application.c b/core/iwasm/common/wasm_application.c index 3b3be16c0..b5928d95c 100644 --- a/core/iwasm/common/wasm_application.c +++ b/core/iwasm/common/wasm_application.c @@ -105,7 +105,8 @@ execute_main(WASMModuleInstanceCommon *module_inst, int32 argc, char *argv[]) bool ret, is_import_func = true, is_memory64 = false; #if WASM_ENABLE_MEMORY64 != 0 WASMModuleInstance *wasm_module_inst = (WASMModuleInstance *)module_inst; - is_memory64 = wasm_module_inst->memories[0]->is_memory64; + if (wasm_module_inst->memory_count > 0) + is_memory64 = wasm_module_inst->memories[0]->is_memory64; #endif exec_env = wasm_runtime_get_exec_env_singleton(module_inst); diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 04fa2473e..418456210 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -9885,13 +9885,6 @@ fail: } #endif /* WASM_ENABLE_FAST_INTERP */ -#define RESERVE_BLOCK_RET() \ - do { \ - if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \ - error_buf_size)) \ - goto fail; \ - } while (0) - #define PUSH_TYPE(type) \ do { \ if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \ @@ -11612,7 +11605,10 @@ re_scan: #if WASM_ENABLE_FAST_INTERP != 0 /* if the result of if branch is in local or const area, add a * copy op */ - RESERVE_BLOCK_RET(); + if (!reserve_block_ret(loader_ctx, opcode, disable_emit, + error_buf, error_buf_size)) { + goto fail; + } emit_empty_label_addr_and_frame_ip(PATCH_END); apply_label_patch(loader_ctx, 1, PATCH_ELSE); @@ -11672,7 +11668,15 @@ re_scan: #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); /* copy the result to the block return address */ - RESERVE_BLOCK_RET(); + if (!reserve_block_ret(loader_ctx, opcode, disable_emit, + error_buf, error_buf_size)) { + /* it could be tmp frame_csp allocated from opcode like + * OP_BR and not counted in loader_ctx->csp_num, it won't + * be freed in wasm_loader_ctx_destroy(loader_ctx) so need + * to free the loader_ctx->frame_csp if fails */ + free_label_patch_list(loader_ctx->frame_csp); + goto fail; + } apply_label_patch(loader_ctx, 0, PATCH_END); free_label_patch_list(loader_ctx->frame_csp); diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 006a38c1c..a1fb3102f 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -5592,13 +5592,6 @@ fail: #endif /* WASM_ENABLE_FAST_INTERP */ -#define RESERVE_BLOCK_RET() \ - do { \ - if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \ - error_buf_size)) \ - goto fail; \ - } while (0) - #define PUSH_TYPE(type) \ do { \ if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \ @@ -6366,7 +6359,10 @@ re_scan: #if WASM_ENABLE_FAST_INTERP != 0 /* if the result of if branch is in local or const area, add a * copy op */ - RESERVE_BLOCK_RET(); + if (!reserve_block_ret(loader_ctx, opcode, disable_emit, + error_buf, error_buf_size)) { + goto fail; + } emit_empty_label_addr_and_frame_ip(PATCH_END); apply_label_patch(loader_ctx, 1, PATCH_ELSE); @@ -6426,7 +6422,11 @@ re_scan: #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); /* copy the result to the block return address */ - RESERVE_BLOCK_RET(); + if (!reserve_block_ret(loader_ctx, opcode, disable_emit, + error_buf, error_buf_size)) { + free_label_patch_list(loader_ctx->frame_csp); + goto fail; + } apply_label_patch(loader_ctx, 0, PATCH_END); free_label_patch_list(loader_ctx->frame_csp); From 0119b17526ae447c5c57784d9deb90c04af51fe5 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Mon, 18 Nov 2024 20:01:00 +0800 Subject: [PATCH 07/28] Correct the table index calculation in aot_instantiation (#3903) `module_inst->table_count = module->import_table_count + module->table_count`, using it as an index will go through `module->import_tables` and `module->tables`, but aot init data is only available for non-import tables. --- core/iwasm/aot/aot_runtime.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 01e04a3ec..5d50f255c 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1785,7 +1785,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, bool ret = false; #endif - /* Check heap size */ + /* Align and validate heap size */ heap_size = align_uint(heap_size, 8); if (heap_size > APP_HEAP_SIZE_MAX) heap_size = APP_HEAP_SIZE_MAX; @@ -2001,7 +2001,11 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, AOTTableInstance *table_inst; table_elem_type_t *table_data; - table = &module->tables[i]; + /* bypass imported table since AOTImportTable doesn't have init_expr */ + if (i < module->import_table_count) + continue; + + table = &module->tables[i - module->import_table_count]; bh_assert(table); if (table->init_expr.init_expr_type == INIT_EXPR_NONE) { From 2975e2ffb8e83691c1dbaa685253c2d4bf63b03b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:45:50 +0800 Subject: [PATCH 08/28] build(deps): bump github/codeql-action from 3.27.1 to 3.27.4 (#3912) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.27.1 to 3.27.4. - [Release notes](https://github.com/github/codeql-action/releases) - [Commits](https://github.com/github/codeql-action/compare/v3.27.1...v3.27.4) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql.yml | 6 +++--- .github/workflows/supply_chain.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index a3228e20d..c71f305aa 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -53,7 +53,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3.27.1 + uses: github/codeql-action/init@v3.27.4 with: languages: ${{ matrix.language }} @@ -70,7 +70,7 @@ jobs: - run: | ./.github/scripts/codeql_buildscript.sh - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3.27.1 + uses: github/codeql-action/analyze@v3.27.4 with: category: "/language:${{matrix.language}}" upload: false @@ -99,7 +99,7 @@ jobs: output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif - name: Upload CodeQL results to code scanning - uses: github/codeql-action/upload-sarif@v3.27.1 + uses: github/codeql-action/upload-sarif@v3.27.4 with: sarif_file: ${{ steps.step1.outputs.sarif-output }} category: "/language:${{matrix.language}}" diff --git a/.github/workflows/supply_chain.yml b/.github/workflows/supply_chain.yml index 28efecc47..1d5baa6b1 100644 --- a/.github/workflows/supply_chain.yml +++ b/.github/workflows/supply_chain.yml @@ -60,6 +60,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@acb9cb18eec7e3a113ef83cff0be91e75cfd9526 # v2.2.4 + uses: github/codeql-action/upload-sarif@a1695c562bbfa68dc5ab58c9b5e9f616b52bf5be # v2.2.4 with: sarif_file: results.sarif From f2b87d773e88790fd2eac89d828f78836c219900 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:47:05 +0800 Subject: [PATCH 09/28] Support external toolchain on Windows for aot compiler (#3911) allowing custom ARC toolchain on Windows --- build-scripts/build_llvm.py | 20 ++++++++- core/iwasm/compilation/aot_compiler.c | 43 ++----------------- core/iwasm/compilation/aot_emit_aot_file.c | 20 ++------- core/iwasm/compilation/aot_llvm.c | 10 ----- core/iwasm/compilation/aot_llvm.h | 12 ++++++ core/shared/utils/bh_common.c | 50 ++++++++++++++++++++++ core/shared/utils/bh_common.h | 10 +++++ 7 files changed, 97 insertions(+), 68 deletions(-) diff --git a/build-scripts/build_llvm.py b/build-scripts/build_llvm.py index 7de55b6a0..ec6bb3954 100755 --- a/build-scripts/build_llvm.py +++ b/build-scripts/build_llvm.py @@ -102,12 +102,27 @@ def build_llvm(llvm_dir, platform, backends, projects, use_clang=False, extra_fl "default": [], } + experimental_backends = ["ARC", "Xtensa"] + normal_backends = [s for s in backends if s not in experimental_backends] + LLVM_TARGETS_TO_BUILD = [ - '-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(backends) + '"' - if backends + '-DLLVM_TARGETS_TO_BUILD:STRING="' + ";".join(normal_backends) + '"' + if normal_backends else '-DLLVM_TARGETS_TO_BUILD:STRING="AArch64;ARM;Mips;RISCV;X86"' ] + # if not on ARC platform, but want to add expeirmental backend ARC as target + if platform != "ARC" and "ARC" in backends: + LLVM_TARGETS_TO_BUILD.extend( + LLVM_EXTRA_COMPILE_OPTIONS["arc"] + ) + + if platform != "Xtensa" and "Xtensa" in backends: + print( + "Currently it's not supported to build Xtensa backend on non-Xtensa platform" + ) + return None + LLVM_PROJECTS_TO_BUILD = [ '-DLLVM_ENABLE_PROJECTS:STRING="' + ";".join(projects) + '"' if projects else "" ] @@ -240,6 +255,7 @@ def main(): "X86", "Xtensa", ], + default=[], help="identify LLVM supported backends, separate by space, like '--arch ARM Mips X86'", ) parser.add_argument( diff --git a/core/iwasm/compilation/aot_compiler.c b/core/iwasm/compilation/aot_compiler.c index 07734b3b4..82f70ca3d 100644 --- a/core/iwasm/compilation/aot_compiler.c +++ b/core/iwasm/compilation/aot_compiler.c @@ -4093,39 +4093,6 @@ aot_compile_wasm(AOTCompContext *comp_ctx) return true; } -#if !(defined(_WIN32) || defined(_WIN32_)) -char * -aot_generate_tempfile_name(const char *prefix, const char *extension, - char *buffer, uint32 len) -{ - int fd, name_len; - - name_len = snprintf(buffer, len, "%s-XXXXXX", prefix); - - if ((fd = mkstemp(buffer)) <= 0) { - aot_set_last_error("make temp file failed."); - return NULL; - } - - /* close and remove temp file */ - close(fd); - unlink(buffer); - - /* Check if buffer length is enough */ - /* name_len + '.' + extension + '\0' */ - if (name_len + 1 + strlen(extension) + 1 > len) { - aot_set_last_error("temp file name too long."); - return NULL; - } - - snprintf(buffer + name_len, len - name_len, ".%s", extension); - return buffer; -} -#else - -errno_t -_mktemp_s(char *nameTemplate, size_t sizeInChars); - char * aot_generate_tempfile_name(const char *prefix, const char *extension, char *buffer, uint32 len) @@ -4134,7 +4101,8 @@ aot_generate_tempfile_name(const char *prefix, const char *extension, name_len = snprintf(buffer, len, "%s-XXXXXX", prefix); - if (_mktemp_s(buffer, name_len + 1) != 0) { + if (!bh_mkstemp(buffer, name_len + 1)) { + aot_set_last_error("make temp file failed."); return NULL; } @@ -4148,7 +4116,6 @@ aot_generate_tempfile_name(const char *prefix, const char *extension, snprintf(buffer + name_len, len - name_len, ".%s", extension); return buffer; } -#endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */ bool aot_emit_llvm_file(AOTCompContext *comp_ctx, const char *file_name) @@ -4227,7 +4194,6 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name) bh_print_time("Begin to emit object file"); -#if !(defined(_WIN32) || defined(_WIN32_)) if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) { char cmd[1024]; int ret; @@ -4270,7 +4236,7 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name) file_name, bc_file_name); LOG_VERBOSE("invoking external LLC compiler:\n\t%s", cmd); - ret = system(cmd); + ret = bh_system(cmd); /* remove temp bitcode file */ unlink(bc_file_name); @@ -4323,7 +4289,7 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name) file_name, asm_file_name); LOG_VERBOSE("invoking external ASM compiler:\n\t%s", cmd); - ret = system(cmd); + ret = bh_system(cmd); /* remove temp assembly file */ unlink(asm_file_name); @@ -4336,7 +4302,6 @@ aot_emit_object_file(AOTCompContext *comp_ctx, char *file_name) return true; } -#endif /* end of !(defined(_WIN32) || defined(_WIN32_)) */ if (!strncmp(LLVMGetTargetName(target), "arc", 3)) /* Emit to assembly file instead for arc target diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index 8fa205308..9b2436a2b 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -4292,10 +4292,6 @@ aot_obj_data_create(AOTCompContext *comp_ctx) bh_print_time("Begin to emit object file"); if (comp_ctx->external_llc_compiler || comp_ctx->external_asm_compiler) { -#if defined(_WIN32) || defined(_WIN32_) - aot_set_last_error("external toolchain not supported on Windows"); - goto fail; -#else /* Generate a temp file name */ int ret; char obj_file_name[64]; @@ -4323,27 +4319,18 @@ aot_obj_data_create(AOTCompContext *comp_ctx) aot_set_last_error("create mem buffer with file failed."); goto fail; } -#endif /* end of defined(_WIN32) || defined(_WIN32_) */ } else if (!strncmp(LLVMGetTargetName(target), "arc", 3)) { -#if defined(_WIN32) || defined(_WIN32_) - aot_set_last_error("emit object file on Windows is unsupported."); - goto fail; -#else /* Emit to assembly file instead for arc target as it cannot emit to object file */ char file_name[] = "wasm-XXXXXX", buf[128]; - int fd, ret; + int ret; - if ((fd = mkstemp(file_name)) <= 0) { + if (!bh_mkstemp(file_name, sizeof(file_name))) { aot_set_last_error("make temp file failed."); goto fail; } - /* close and remove temp file */ - close(fd); - unlink(file_name); - snprintf(buf, sizeof(buf), "%s%s", file_name, ".s"); if (LLVMTargetMachineEmitToFile(comp_ctx->target_machine, comp_ctx->module, buf, LLVMAssemblyFile, @@ -4364,7 +4351,7 @@ aot_obj_data_create(AOTCompContext *comp_ctx) "/opt/zephyr-sdk/arc-zephyr-elf/bin/arc-zephyr-elf-gcc ", "-mcpu=arcem -o ", file_name, ".o -c ", file_name, ".s"); /* TODO: use try..catch to handle possible exceptions */ - ret = system(buf); + ret = bh_system(buf); /* remove temp assembly file */ snprintf(buf, sizeof(buf), "%s%s", file_name, ".s"); unlink(buf); @@ -4391,7 +4378,6 @@ aot_obj_data_create(AOTCompContext *comp_ctx) aot_set_last_error("create mem buffer with file failed."); goto fail; } -#endif /* end of defined(_WIN32) || defined(_WIN32_) */ } else { if (LLVMTargetMachineEmitToMemoryBuffer( diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index fb1c4308b..14ee4dd2b 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -2746,10 +2746,6 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) /* verify external llc compiler */ comp_ctx->external_llc_compiler = getenv("WAMRC_LLC_COMPILER"); if (comp_ctx->external_llc_compiler) { -#if defined(_WIN32) || defined(_WIN32_) - comp_ctx->external_llc_compiler = NULL; - LOG_WARNING("External LLC compiler not supported on Windows."); -#else if (access(comp_ctx->external_llc_compiler, X_OK) != 0) { LOG_WARNING("WAMRC_LLC_COMPILER [%s] not found, fallback to " "default pipeline", @@ -2761,17 +2757,12 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) LOG_VERBOSE("Using external LLC compiler [%s]", comp_ctx->external_llc_compiler); } -#endif } /* verify external asm compiler */ if (!comp_ctx->external_llc_compiler) { comp_ctx->external_asm_compiler = getenv("WAMRC_ASM_COMPILER"); if (comp_ctx->external_asm_compiler) { -#if defined(_WIN32) || defined(_WIN32_) - comp_ctx->external_asm_compiler = NULL; - LOG_WARNING("External ASM compiler not supported on Windows."); -#else if (access(comp_ctx->external_asm_compiler, X_OK) != 0) { LOG_WARNING( "WAMRC_ASM_COMPILER [%s] not found, fallback to " @@ -2784,7 +2775,6 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option) LOG_VERBOSE("Using external ASM compiler [%s]", comp_ctx->external_asm_compiler); } -#endif } } diff --git a/core/iwasm/compilation/aot_llvm.h b/core/iwasm/compilation/aot_llvm.h index 0dce988bc..9c608d301 100644 --- a/core/iwasm/compilation/aot_llvm.h +++ b/core/iwasm/compilation/aot_llvm.h @@ -37,6 +37,18 @@ #include "aot_orc_extra.h" #include "aot_comp_option.h" +#if defined(_WIN32) || defined(_WIN32_) +#include +#define access _access +/* On windows there is no X_OK flag to check for executablity, only check for + * existence */ +#ifdef X_OK +#undef X_OK +#endif +#define X_OK 00 +#define unlink _unlink +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/core/shared/utils/bh_common.c b/core/shared/utils/bh_common.c index 7fe123c91..62f36caf1 100644 --- a/core/shared/utils/bh_common.c +++ b/core/shared/utils/bh_common.c @@ -165,3 +165,53 @@ wa_strdup(const char *s) } return s1; } + +#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 +int +bh_system(const char *cmd) +{ + int ret; + +#if !(defined(_WIN32) || defined(_WIN32_)) + ret = system(cmd); +#else + ret = _spawnlp(_P_WAIT, "cmd.exe", "/c", cmd, NULL); +#endif + + return ret; +} + +#if defined(_WIN32) || defined(_WIN32_) +errno_t +_mktemp_s(char *nameTemplate, size_t sizeInChars); +#endif + +bool +bh_mkstemp(char *file_name, size_t name_len) +{ + int fd; + +#if !(defined(_WIN32) || defined(_WIN32_)) + (void)name_len; + /* On Linux, it generates a unique temporary filename from template, creates + * and opens the file, and returns an open file descriptor for the file. */ + if ((fd = mkstemp(file_name)) <= 0) { + goto fail; + } + + /* close and remove temp file */ + close(fd); + unlink(file_name); +#else + /* On Windows, it generates a unique temporary file name but does not create + * or open the file */ + if (_mktemp_s(file_name, name_len) != 0) { + goto fail; + } +#endif + + return true; +fail: + return false; +} +#endif /* End of WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 */ diff --git a/core/shared/utils/bh_common.h b/core/shared/utils/bh_common.h index adae722bb..093e62208 100644 --- a/core/shared/utils/bh_common.h +++ b/core/shared/utils/bh_common.h @@ -66,6 +66,16 @@ bh_strdup(const char *s); char * wa_strdup(const char *s); +#if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 +/* Executes a system command in bash/cmd.exe */ +int +bh_system(const char *cmd); + +/* Tests whether can create a temporary file with the given name */ +bool +bh_mkstemp(char *filename, size_t name_len); +#endif + #ifdef __cplusplus } #endif From f1d03db8e58a268e1745ece954b44772c9522349 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:22:36 +0800 Subject: [PATCH 10/28] Fix CI wamr-ide error (#3913) The recent version of the rust toolchain will emit ref types opcodes, which needs to enable this feature in the `iwasm` build. The vector format parsing logic has some errors in the current version. I disabled the check for now and am waiting for further investigation. --- .github/workflows/compilation_on_android_ubuntu.yml | 2 +- .../src/test/suite/extension.test.ts | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/workflows/compilation_on_android_ubuntu.yml b/.github/workflows/compilation_on_android_ubuntu.yml index 8ba6e0e80..1ea36418e 100644 --- a/.github/workflows/compilation_on_android_ubuntu.yml +++ b/.github/workflows/compilation_on_android_ubuntu.yml @@ -828,7 +828,7 @@ jobs: run: | mkdir build cd build - cmake .. -DWAMR_BUILD_DEBUG_INTERP=1 + cmake .. -DWAMR_BUILD_DEBUG_INTERP=1 -DWAMR_BUILD_REF_TYPES=1 make working-directory: product-mini/platforms/linux diff --git a/test-tools/wamr-ide/VSCode-Extension/src/test/suite/extension.test.ts b/test-tools/wamr-ide/VSCode-Extension/src/test/suite/extension.test.ts index d1420dfa5..91d54853e 100644 --- a/test-tools/wamr-ide/VSCode-Extension/src/test/suite/extension.test.ts +++ b/test-tools/wamr-ide/VSCode-Extension/src/test/suite/extension.test.ts @@ -196,11 +196,14 @@ suite('Inegration Tests', function () { ); // Vector - assert.equal( - namesToVariables['vector'].value, - ' (5) vec![1, 2, 3, 4, 12]', - 'The Vector summary string looks different than expected' - ); + // TODO: The vector format conversion have some problem now, can't see the actual value + // - (5) vec![{...}, {...}, {...}, {...}, {...}, ...] + // + (5) vec![1, 2, 3, 4, 12] + // assert.equal( + // namesToVariables['vector'].value, + // ' (5) vec![1, 2, 3, 4, 12]', + // 'The Vector summary string looks different than expected' + // ); // Map assert.equal( From 62aca1727908848c194dfe52c7d62397f5df8441 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:01:24 +0800 Subject: [PATCH 11/28] Check possible integer overflow in aot memory boundary check (#3920) Check possible integer overflow in aot memory boundary check when the wasm memory is 64-bit. --- core/iwasm/compilation/aot_emit_memory.c | 38 ++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/core/iwasm/compilation/aot_emit_memory.c b/core/iwasm/compilation/aot_emit_memory.c index 869a1dbb2..9adf96ac5 100644 --- a/core/iwasm/compilation/aot_emit_memory.c +++ b/core/iwasm/compilation/aot_emit_memory.c @@ -273,10 +273,24 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, } /* offset1 = offset + addr; */ - /* TODO: check whether integer overflow occurs when memory is 64-bit - and boundary check is enabled */ BUILD_OP(Add, offset_const, addr, offset1, "offset1"); + if (is_memory64 && comp_ctx->enable_bound_check) { + /* Check whether integer overflow occurs in offset + addr */ + LLVMBasicBlockRef check_integer_overflow_end; + ADD_BASIC_BLOCK(check_integer_overflow_end, + "check_integer_overflow_end"); + LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr); + + BUILD_ICMP(LLVMIntULT, offset1, offset_const, cmp1, "cmp1"); + if (!aot_emit_exception(comp_ctx, func_ctx, + EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp1, + check_integer_overflow_end)) { + goto fail; + } + SET_BUILD_POS(check_integer_overflow_end); + } + if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) { LLVMBasicBlockRef app_addr_in_shared_heap, app_addr_in_linear_mem; LLVMValueRef is_in_shared_heap, shared_heap_check_bound = NULL; @@ -303,7 +317,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, LLVMPositionBuilderAtEnd(comp_ctx->builder, block_curr); if (!is_target_64bit) { - /* Check whether interger overflow occurs in addr + offset */ + /* Check whether integer overflow occurs in addr + offset */ LLVMBasicBlockRef check_integer_overflow_end; ADD_BASIC_BLOCK(check_integer_overflow_end, "check_integer_overflow_end"); @@ -1215,10 +1229,24 @@ check_bulk_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, goto fail; } - /* TODO: check whether integer overflow occurs when memory is 64-bit - and boundary check is enabled */ BUILD_OP(Add, offset, bytes, max_addr, "max_addr"); + if (is_memory64 && comp_ctx->enable_bound_check) { + /* Check whether integer overflow occurs in offset + addr */ + LLVMBasicBlockRef check_integer_overflow_end; + ADD_BASIC_BLOCK(check_integer_overflow_end, + "check_integer_overflow_end"); + LLVMMoveBasicBlockAfter(check_integer_overflow_end, block_curr); + + BUILD_ICMP(LLVMIntULT, max_addr, offset, cmp, "cmp"); + if (!aot_emit_exception(comp_ctx, func_ctx, + EXCE_OUT_OF_BOUNDS_MEMORY_ACCESS, true, cmp, + check_integer_overflow_end)) { + goto fail; + } + SET_BUILD_POS(check_integer_overflow_end); + } + if (comp_ctx->enable_shared_heap /* TODO: && mem_idx == 0 */) { LLVMBasicBlockRef app_addr_in_shared_heap, app_addr_in_linear_mem; LLVMValueRef shared_heap_start_off, shared_heap_check_bound; From 00c2aa10a8428aa38a0197c97455a8bc3a24937d Mon Sep 17 00:00:00 2001 From: James Ring Date: Sat, 23 Nov 2024 19:30:00 -0800 Subject: [PATCH 12/28] Drop declarative elements on module instantiation (#3922) --- core/iwasm/aot/aot_runtime.c | 4 +++- core/iwasm/interpreter/wasm_runtime.c | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 5d50f255c..8a33a7271 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1905,7 +1905,9 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent, goto fail; } for (i = 0; i < module->table_init_data_count; i++) { - if (wasm_elem_is_active(module->table_init_data_list[i]->mode)) + if (wasm_elem_is_active(module->table_init_data_list[i]->mode) + || wasm_elem_is_declarative( + module->table_init_data_list[i]->mode)) bh_bitmap_set_bit(common->elem_dropped, i); } } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 0f1ccd937..f9378aac9 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2467,7 +2467,8 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent, goto fail; } for (i = 0; i < module->table_seg_count; i++) { - if (wasm_elem_is_active(module->table_segments[i].mode)) + if (wasm_elem_is_active(module->table_segments[i].mode) + || wasm_elem_is_declarative(module->table_segments[i].mode)) bh_bitmap_set_bit(module_inst->e->common.elem_dropped, i); } } From 9d8150efaee35240638a756ddd35b23f1376a4a1 Mon Sep 17 00:00:00 2001 From: Dylan Johnston <18252447+dpjohnst@users.noreply.github.com> Date: Sun, 24 Nov 2024 14:31:55 +1100 Subject: [PATCH 13/28] Fix WASI Path Mapping Processing (#3923) Filesystem paths can be mapped from the host path to a guest path using the format `::`. Previously `strtok` was used to find the `::` delimiter. Unfortunately `strtok` processes each delimiter character individually. This meant that the code was ~equivalent to `strtok(mapping_copy, ":")` which breaks with Windows-style paths (E.g. `C:\my_path\`). To fix this `strstr` is used to search for the exact delimiter. --- core/iwasm/common/wasm_runtime_common.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index fb16aa0f3..26aab9664 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -3631,8 +3631,14 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, bh_memcpy_s(mapping_copy, max_len, map_dir_list[i], (uint32)(strlen(map_dir_list[i]) + 1)); - map_mapped = strtok(mapping_copy, "::"); - map_host = strtok(NULL, "::"); + + const char *delim = "::"; + char *delim_pos = strstr(mapping_copy, delim); + if (delim_pos) { + *delim_pos = '\0'; + map_mapped = mapping_copy; + map_host = delim_pos + strlen(delim); + } if (!map_mapped || !map_host) { if (error_buf) From dbdf3df60b8bc4fc81b46f7d53335e986b2cfb15 Mon Sep 17 00:00:00 2001 From: James Ring Date: Sat, 23 Nov 2024 19:32:34 -0800 Subject: [PATCH 14/28] Use plain assignment rather than bh_memcpy_s (#3924) --- core/iwasm/interpreter/wasm_runtime.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index f9378aac9..c3f35916c 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1208,9 +1208,8 @@ globals_instantiate(WASMModule *module, WASMModuleInstance *module_inst, /* The linked global instance has been initialized, we just need to copy the value. */ - bh_memcpy_s(&(global->initial_value), sizeof(WASMValue), - &(global_import->import_global_linked->init_expr.u), - sizeof(WASMValue)); + global->initial_value = + global_import->import_global_linked->init_expr.u; } else #endif From b0c6d5c23a243cb51f2672f552882de24305f42b Mon Sep 17 00:00:00 2001 From: WenLY1 <130950131+WenLY1@users.noreply.github.com> Date: Sun, 24 Nov 2024 11:34:38 +0800 Subject: [PATCH 15/28] add testcases for shared heap and fix POP_MEM_OFFSET of memory64 (#3916) - add testcases for shared_heap - fix POP_MEM_OFFSET and POP_TBL_ELEM_IDX of memory64 Signed-off-by: wenlingyun1 --- core/iwasm/interpreter/wasm_interp_classic.c | 4 +- tests/unit/shared-heap/CMakeLists.txt | 2 +- tests/unit/shared-heap/shared_heap_test.cc | 132 +++++++++++++++--- .../unit/shared-heap/wasm-apps/CMakeLists.txt | 35 ++++- tests/unit/shared-heap/wasm-apps/test.c | 14 +- .../shared-heap/wasm-apps/test_addr_conv.c | 32 +++++ 6 files changed, 197 insertions(+), 22 deletions(-) create mode 100644 tests/unit/shared-heap/wasm-apps/test_addr_conv.c diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 7041fd89c..834311f7e 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -593,8 +593,8 @@ wasm_interp_get_frame_ref(WASMInterpFrame *frame) #endif #if WASM_ENABLE_MEMORY64 != 0 -#define POP_MEM_OFFSET() (is_memory64 ? POP_I64() : POP_I32()) -#define POP_TBL_ELEM_IDX() (is_table64 ? POP_I64() : POP_I32()) +#define POP_MEM_OFFSET() (is_memory64 ? POP_I64() : (uint32)POP_I32()) +#define POP_TBL_ELEM_IDX() (is_table64 ? POP_I64() : (uint32)POP_I32()) #else #define POP_MEM_OFFSET() POP_I32() #define POP_TBL_ELEM_IDX() POP_I32() diff --git a/tests/unit/shared-heap/CMakeLists.txt b/tests/unit/shared-heap/CMakeLists.txt index 6baf420f8..2b06c537f 100644 --- a/tests/unit/shared-heap/CMakeLists.txt +++ b/tests/unit/shared-heap/CMakeLists.txt @@ -8,7 +8,7 @@ project(test-shared-heap) add_definitions(-DRUN_ON_LINUX) set(WAMR_BUILD_APP_FRAMEWORK 0) -set(WAMR_BUILD_AOT 0) +set(WAMR_BUILD_AOT 1) set(WAMR_BUILD_INTERP 1) set(WAMR_BUILD_FAST_INTERP 1) set(WAMR_BUILD_JIT 0) diff --git a/tests/unit/shared-heap/shared_heap_test.cc b/tests/unit/shared-heap/shared_heap_test.cc index 5e45d3111..deb4bbb38 100644 --- a/tests/unit/shared-heap/shared_heap_test.cc +++ b/tests/unit/shared-heap/shared_heap_test.cc @@ -92,37 +92,28 @@ destroy_module_env(struct ret_env module_env) } } -TEST_F(shared_heap_test, test_shared_heap) +static void test_shared_heap(WASMSharedHeap *shared_heap, const char *file, const char *func_name, uint32 argc, uint32 argv[]) { struct ret_env tmp_module_env; WASMFunctionInstanceCommon *func_test = nullptr; bool ret = false; - uint32 argv[1] = { 65535 }; const char *exception = nullptr; - SharedHeapInitArgs args; - WASMSharedHeap *shared_heap = nullptr; - args.size = 1024; - shared_heap = wasm_runtime_create_shared_heap(&args); - tmp_module_env = load_wasm((char *)"test.wasm", 0); + tmp_module_env = load_wasm((char *)file, 0); - if (!shared_heap) { - printf("Failed to create shared heap\n"); - goto test_failed; - } if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst, shared_heap)) { printf("Failed to attach shared heap\n"); goto test_failed; } - func_test = wasm_runtime_lookup_function( - tmp_module_env.wasm_module_inst, "test"); + func_test = wasm_runtime_lookup_function(tmp_module_env.wasm_module_inst, + func_name); if (!func_test) { printf("\nFailed to wasm_runtime_lookup_function!\n"); goto test_failed; } ret = - wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, 1, argv); + wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, argc, argv); if (!ret) { printf("\nFailed to wasm_runtime_call_wasm!\n"); const char *s = wasm_runtime_get_exception(tmp_module_env.wasm_module_inst); @@ -131,12 +122,119 @@ TEST_F(shared_heap_test, test_shared_heap) } wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst); - - EXPECT_EQ(10, argv[0]); - destroy_module_env(tmp_module_env); return; test_failed: destroy_module_env(tmp_module_env); EXPECT_EQ(1, 0); } + +TEST_F(shared_heap_test, test_shared_heap_basic) +{ + SharedHeapInitArgs args; + WASMSharedHeap *shared_heap = nullptr; + uint32 argv[1] = { 0 }; + + args.size = 1024; + shared_heap = wasm_runtime_create_shared_heap(&args); + + if (!shared_heap) { + printf("Failed to create shared heap\n"); + EXPECT_EQ(1, 0); + } + + // test wasm + test_shared_heap(shared_heap, "test.wasm", "test", 1, argv); + EXPECT_EQ(10, argv[0]); + + // test aot + test_shared_heap(shared_heap, "test.aot", "test", 1, argv); + EXPECT_EQ(10, argv[0]); + +} + +TEST_F(shared_heap_test, test_shared_heap_malloc_fail) +{ + SharedHeapInitArgs args; + WASMSharedHeap *shared_heap = nullptr; + uint32 argv[1] = { 0 }; + + args.size = 1024; + shared_heap = wasm_runtime_create_shared_heap(&args); + + if (!shared_heap) { + printf("Failed to create shared heap\n"); + EXPECT_EQ(1, 0); + } + + // test wasm + test_shared_heap(shared_heap, "test.wasm", "test_malloc_fail", 1, argv); + EXPECT_EQ(1, argv[0]); + + // test aot + test_shared_heap(shared_heap, "test.aot", "test_malloc_fail", 1, argv); + EXPECT_EQ(1, argv[0]); +} + +#ifndef native_function +#define native_function(func_name, signature) \ + { #func_name, (void *)glue_##func_name, signature, NULL } + +#endif +#ifndef nitems +#define nitems(_a) (sizeof(_a) / sizeof(0 [(_a)])) +#endif /* nitems */ +uintptr_t glue_test_addr_conv(wasm_exec_env_t env, uintptr_t addr) +{ + wasm_module_inst_t module_inst = get_module_inst(env); + uintptr_t ret; + void *native_addr = (void *)addr; + uintptr_t app_addr = addr_native_to_app(native_addr); + + native_addr = addr_app_to_native(app_addr); + if (native_addr != (void *)addr) + { + EXPECT_EQ(1, 0); + } + return app_addr; +} + +static NativeSymbol g_test_native_symbols[] = +{ + native_function(test_addr_conv,"(*)i"), +}; + +TEST_F(shared_heap_test, test_addr_conv) +{ + SharedHeapInitArgs args; + WASMSharedHeap *shared_heap = nullptr; + uint32 argv[1] = { 0 }; + struct ret_env tmp_module_env; + WASMFunctionInstanceCommon *func_test = nullptr; + bool ret = false; + const char *exception = nullptr; + wasm_module_inst_t module_inst = tmp_module_env.wasm_module_inst; + + ret = wasm_native_register_natives("env", g_test_native_symbols, + nitems(g_test_native_symbols)); + if (!ret) + { + EXPECT_EQ(1, 0); + return; + } + + args.size = 1024; + shared_heap = wasm_runtime_create_shared_heap(&args); + if (!shared_heap) { + printf("Failed to create shared heap\n"); + EXPECT_EQ(1, 0); + } + + // test wasm + test_shared_heap(shared_heap, "test_addr_conv.wasm", "test", 1, argv); + EXPECT_EQ(1, argv[0]); + + // test aot + test_shared_heap(shared_heap, "test_addr_conv.aot", "test", 1, argv); + EXPECT_EQ(1, argv[0]); +} diff --git a/tests/unit/shared-heap/wasm-apps/CMakeLists.txt b/tests/unit/shared-heap/wasm-apps/CMakeLists.txt index 3627a2c14..097f66ae5 100644 --- a/tests/unit/shared-heap/wasm-apps/CMakeLists.txt +++ b/tests/unit/shared-heap/wasm-apps/CMakeLists.txt @@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.14) project(wasm-apps) set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) +set(WAMRC_ROOT_DIR ${WAMR_ROOT_DIR}/wamr-compiler/build) set(CMAKE_SYSTEM_PROCESSOR wasm32) set(CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot) @@ -36,4 +37,36 @@ add_custom_command(TARGET test.wasm POST_BUILD ${CMAKE_CURRENT_BINARY_DIR}/test.wasm ${CMAKE_CURRENT_BINARY_DIR}/../ COMMENT "Copy test.wasm to the same directory of google test" - ) \ No newline at end of file + ) + +add_custom_command(TARGET test.wasm POST_BUILD + COMMAND ${WAMRC_ROOT_DIR}/wamrc --opt-level=0 --enable-shared-heap --bounds-checks=1 + -o + test.aot + test.wasm + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/test.aot + ${CMAKE_CURRENT_BINARY_DIR}/../ + COMMENT "Copy test.aot to the same directory of google test" + ) + +add_executable(test_addr_conv.wasm test_addr_conv.c) +target_link_libraries(test.wasm) + +add_custom_command(TARGET test_addr_conv.wasm POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/test_addr_conv.wasm + ${CMAKE_CURRENT_BINARY_DIR}/../ + COMMENT "Copy test_addr_conv.wasm to the same directory of google test" + ) + +add_custom_command(TARGET test_addr_conv.wasm POST_BUILD + COMMAND ${WAMRC_ROOT_DIR}/wamrc --opt-level=0 --enable-shared-heap --bounds-checks=1 + -o + test_addr_conv.aot + test_addr_conv.wasm + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/test_addr_conv.aot + ${CMAKE_CURRENT_BINARY_DIR}/../ + COMMENT "Copy test_addr_conv.aot to the same directory of google test" + ) diff --git a/tests/unit/shared-heap/wasm-apps/test.c b/tests/unit/shared-heap/wasm-apps/test.c index b83ee64ff..bd0df19c2 100644 --- a/tests/unit/shared-heap/wasm-apps/test.c +++ b/tests/unit/shared-heap/wasm-apps/test.c @@ -13,10 +13,22 @@ shared_heap_free(void *offset); int test() { - int *ptr = (int *)shared_heap_malloc(10); + int *ptr = (int *)shared_heap_malloc(4); *ptr = 10; int a = *ptr; shared_heap_free(ptr); return a; } + +int +test_malloc_fail() +{ + int *ptr = (int *)shared_heap_malloc(8192); + + if (ptr == NULL) { + return 1; + } + shared_heap_free(ptr); + return 0; +} diff --git a/tests/unit/shared-heap/wasm-apps/test_addr_conv.c b/tests/unit/shared-heap/wasm-apps/test_addr_conv.c new file mode 100644 index 000000000..f91764c84 --- /dev/null +++ b/tests/unit/shared-heap/wasm-apps/test_addr_conv.c @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2024 Xiaomi Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include + +extern void * +shared_heap_malloc(int size); +extern void +shared_heap_free(void *offset); +extern void * +test_addr_conv(void *ptr); + +int +test() +{ + int *ptr = NULL; + int *ptr2 = NULL; + + ptr = (int *)shared_heap_malloc(4); + + if (ptr == NULL) { + return 0; + } + ptr2 = test_addr_conv(ptr); + if (ptr2 != ptr) { + return 0; + } + shared_heap_free(ptr); + return 1; +} From 1d111a38d68abf71f1fa513ee21927e764aeec38 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:08:51 +0800 Subject: [PATCH 16/28] Fix loader small bug (#3928) --- core/iwasm/interpreter/wasm_loader.c | 7 ++++++- core/iwasm/interpreter/wasm_mini_loader.c | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 418456210..79ed996d8 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -9855,7 +9855,12 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, else { loader_ctx->frame_offset = frame_offset; loader_ctx->dynamic_offset = dynamic_offset; - PUSH_OFFSET_TYPE(return_types[i]); + if (!(wasm_loader_push_frame_offset( + loader_ctx, return_types[i], disable_emit, + operand_offset, error_buf, error_buf_size))) { + wasm_runtime_free(emit_data); + goto fail; + } wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); loader_ctx->frame_offset = frame_offset_org; loader_ctx->dynamic_offset = dynamic_offset_org; diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index a1fb3102f..4dad55523 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -5561,7 +5561,12 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, else { loader_ctx->frame_offset = frame_offset; loader_ctx->dynamic_offset = dynamic_offset; - PUSH_OFFSET_TYPE(return_types[i]); + if (!(wasm_loader_push_frame_offset( + loader_ctx, return_types[i], disable_emit, + operand_offset, error_buf, error_buf_size))) { + wasm_runtime_free(emit_data); + goto fail; + } wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); loader_ctx->frame_offset = frame_offset_org; loader_ctx->dynamic_offset = dynamic_offset_org; From 7b553cd420e9cd1f93817d054345f8733a681295 Mon Sep 17 00:00:00 2001 From: Maks Litskevich Date: Wed, 27 Nov 2024 14:06:07 +0300 Subject: [PATCH 17/28] Enable ref types by default (#3894) --- CMakeLists.txt | 4 ++-- doc/build_wamr.md | 2 +- product-mini/platforms/linux/CMakeLists.txt | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40658e9ac..3efd6aa82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,8 +113,8 @@ if (NOT DEFINED WAMR_BUILD_SIMD) endif () if (NOT DEFINED WAMR_BUILD_REF_TYPES) - # Disable reference types by default - set (WAMR_BUILD_REF_TYPES 0) + # Enable reference types by default + set (WAMR_BUILD_REF_TYPES 1) endif () set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 4537ee084..abf663177 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -223,7 +223,7 @@ Currently we only profile the memory consumption of module, module_instance and > 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 +- **WAMR_BUILD_REF_TYPES**=1/0, default to enable if not set #### **Exclude WAMR application entry functions** - **WAMR_DISABLE_APP_ENTRY**=1/0, default to disable if not set diff --git a/product-mini/platforms/linux/CMakeLists.txt b/product-mini/platforms/linux/CMakeLists.txt index 2e37b75f9..321c4a955 100644 --- a/product-mini/platforms/linux/CMakeLists.txt +++ b/product-mini/platforms/linux/CMakeLists.txt @@ -108,8 +108,8 @@ if (NOT DEFINED WAMR_BUILD_SIMD) endif () if (NOT DEFINED WAMR_BUILD_REF_TYPES) - # Disable reference types by default - set (WAMR_BUILD_REF_TYPES 0) + # Enable reference types by default + set (WAMR_BUILD_REF_TYPES 1) endif () if (NOT DEFINED WAMR_BUILD_DEBUG_INTERP) From 27d96aac02abad895793b7a0d1aa20707ad54566 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Wed, 27 Nov 2024 19:06:22 +0800 Subject: [PATCH 18/28] Update README.md to clarify Windows toolchain support and ESP-IDF reference (#3917) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d89d0cd17..05368b929 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,8 @@ The WAMR VMcore supports the following architectures: - XTENSA, MIPS, ARC The following platforms are supported, click each link below for how to build iwasm on that platform. Refer to [WAMR porting guide](./doc/port_wamr.md) for how to port WAMR to a new platform. -- [Linux](./product-mini/README.md#linux), [Linux SGX (Intel Software Guard Extension)](./doc/linux_sgx.md), [MacOS](./product-mini/README.md#macos), [Android](./product-mini/README.md#android), [Windows](./product-mini/README.md#windows), [Windows (MinGW)](./product-mini/README.md#mingw) -- [Zephyr](./product-mini/README.md#zephyr), [AliOS-Things](./product-mini/README.md#alios-things), [VxWorks](./product-mini/README.md#vxworks), [NuttX](./product-mini/README.md#nuttx), [RT-Thread](./product-mini/README.md#RT-Thread), [ESP-IDF](./product-mini/README.md#esp-idf) +- [Linux](./product-mini/README.md#linux), [Linux SGX (Intel Software Guard Extension)](./doc/linux_sgx.md), [MacOS](./product-mini/README.md#macos), [Android](./product-mini/README.md#android), [Windows](./product-mini/README.md#windows), [Windows (MinGW, MSVC)](./product-mini/README.md#mingw) +- [Zephyr](./product-mini/README.md#zephyr), [AliOS-Things](./product-mini/README.md#alios-things), [VxWorks](./product-mini/README.md#vxworks), [NuttX](./product-mini/README.md#nuttx), [RT-Thread](./product-mini/README.md#RT-Thread), [ESP-IDF(FreeRTOS)](./product-mini/README.md#esp-idf) ## Getting started From fd91b51cfb36345b7a3c4fd66771171dc865385d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 28 Nov 2024 11:49:23 +0800 Subject: [PATCH 19/28] build(deps): bump github/codeql-action from 3.27.4 to 3.27.5 (#3931) Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.27.4 to 3.27.5. - [Release notes](https://github.com/github/codeql-action/releases) - [Commits](https://github.com/github/codeql-action/compare/v3.27.4...v3.27.5) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql.yml | 6 +++--- .github/workflows/supply_chain.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index c71f305aa..9ed8919c7 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -53,7 +53,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3.27.4 + uses: github/codeql-action/init@v3.27.5 with: languages: ${{ matrix.language }} @@ -70,7 +70,7 @@ jobs: - run: | ./.github/scripts/codeql_buildscript.sh - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3.27.4 + uses: github/codeql-action/analyze@v3.27.5 with: category: "/language:${{matrix.language}}" upload: false @@ -99,7 +99,7 @@ jobs: output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif - name: Upload CodeQL results to code scanning - uses: github/codeql-action/upload-sarif@v3.27.4 + uses: github/codeql-action/upload-sarif@v3.27.5 with: sarif_file: ${{ steps.step1.outputs.sarif-output }} category: "/language:${{matrix.language}}" diff --git a/.github/workflows/supply_chain.yml b/.github/workflows/supply_chain.yml index 1d5baa6b1..f2aa485ea 100644 --- a/.github/workflows/supply_chain.yml +++ b/.github/workflows/supply_chain.yml @@ -60,6 +60,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@a1695c562bbfa68dc5ab58c9b5e9f616b52bf5be # v2.2.4 + uses: github/codeql-action/upload-sarif@3d3d628990a5f99229dd9fa1821cc5a4f31b613b # v2.2.4 with: sarif_file: results.sarif From 8698d22e6738a278eb9639ba9c8860456550771f Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Thu, 28 Nov 2024 11:49:55 +0800 Subject: [PATCH 20/28] add thread cpu time for zephyr (#3937) --- core/shared/platform/zephyr/zephyr_time.c | 15 +++++++++++++-- product-mini/platforms/zephyr/simple/prj.conf | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/shared/platform/zephyr/zephyr_time.c b/core/shared/platform/zephyr/zephyr_time.c index 78bc3e076..59b720e41 100644 --- a/core/shared/platform/zephyr/zephyr_time.c +++ b/core/shared/platform/zephyr/zephyr_time.c @@ -14,6 +14,17 @@ os_time_get_boot_us() uint64 os_time_thread_cputime_us(void) { - /* FIXME if u know the right api */ - return os_time_get_boot_us(); + k_tid_t tid; + struct k_thread_runtime_stats stats; + uint32 clock_freq; + uint64 cpu_cycles, time_in_us = 0; + + tid = k_current_get(); + if (k_thread_runtime_stats_get(tid, &stats) == 0) { + cpu_cycles = stats.execution_cycles; + clock_freq = CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC; + time_in_us = (cpu_cycles * 1000000) / clock_freq; + } + + return time_in_us; } diff --git a/product-mini/platforms/zephyr/simple/prj.conf b/product-mini/platforms/zephyr/simple/prj.conf index c269b8ab4..8ab3c52f8 100644 --- a/product-mini/platforms/zephyr/simple/prj.conf +++ b/product-mini/platforms/zephyr/simple/prj.conf @@ -5,3 +5,4 @@ CONFIG_STACK_SENTINEL=y CONFIG_PRINTK=y CONFIG_LOG=y CONFIG_LOG_BUFFER_SIZE=4096 +CONFIG_THREAD_RUNTIME_STATS=y From e09613c722ca45b2f2d659024d109786f212182b Mon Sep 17 00:00:00 2001 From: James Ring Date: Wed, 27 Nov 2024 19:50:16 -0800 Subject: [PATCH 21/28] support WASM_FUNCREF return type in argv_to_results (#3936) --- core/iwasm/common/wasm_c_api.c | 1 + 1 file changed, 1 insertion(+) diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 0c5e37eab..500f99dc8 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -3330,6 +3330,7 @@ argv_to_results(const uint32 *argv, const wasm_valtype_vec_t *result_defs, break; #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0 case WASM_EXTERNREF: + case WASM_FUNCREF: result->of.ref = (struct wasm_ref_t *)(*(uintptr_t *)argv); argv += sizeof(uintptr_t) / sizeof(uint32); break; From 838dd81e682249fbd2d7ee16d6999dafb87481b9 Mon Sep 17 00:00:00 2001 From: James Ring Date: Wed, 27 Nov 2024 19:50:44 -0800 Subject: [PATCH 22/28] don't return an uninitialized trap if argv_to_results fails (#3935) Currently, if argv_to_results fails (e.g. because an unsupported type is encountered), an non-null trap with an uninitialized message is returned. --- core/iwasm/common/wasm_c_api.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index 500f99dc8..d5c45a441 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -3442,6 +3442,8 @@ wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params, if (result_count) { if (!argv_to_results(argv, wasm_functype_results(func->type), results)) { + wasm_runtime_set_exception(func->inst_comm_rt, + "argv_to_results failed"); goto failed; } results->num_elems = result_count; From 739efd78e9bd601b59a3688506396bc7968fbbf2 Mon Sep 17 00:00:00 2001 From: Dylan Johnston <18252447+dpjohnst@users.noreply.github.com> Date: Sun, 1 Dec 2024 23:40:43 +1100 Subject: [PATCH 23/28] Fix incorrect assignment in win_file.c (#3939) win_error should be compared to ERROR_INVALID_HANDLE but was instead being assigned the value. --- core/shared/platform/windows/win_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shared/platform/windows/win_file.c b/core/shared/platform/windows/win_file.c index 408d0d00c..770c5c741 100644 --- a/core/shared/platform/windows/win_file.c +++ b/core/shared/platform/windows/win_file.c @@ -1758,7 +1758,7 @@ os_closedir(os_dir_stream dir_stream) if (!success) { DWORD win_error = GetLastError(); - if (win_error = ERROR_INVALID_HANDLE) + if (win_error == ERROR_INVALID_HANDLE) BH_FREE(dir_stream); return convert_windows_error_code(win_error); } From aabe83074ebfce6be5332765ed71d093975d8a24 Mon Sep 17 00:00:00 2001 From: TianlongLiang <111852609+TianlongLiang@users.noreply.github.com> Date: Fri, 6 Dec 2024 14:39:53 +0800 Subject: [PATCH 24/28] Improvements for platform thread APIs on Windows and Zephyr (#3941) * improvements for os_thread_join on Windows and Zephyr --- core/shared/platform/windows/win_thread.c | 72 +++++++++++++++++++-- core/shared/platform/zephyr/zephyr_thread.c | 14 ++-- core/shared/utils/bh_atomic.h | 3 +- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/core/shared/platform/windows/win_thread.c b/core/shared/platform/windows/win_thread.c index f37250fa4..438e16040 100644 --- a/core/shared/platform/windows/win_thread.c +++ b/core/shared/platform/windows/win_thread.c @@ -60,6 +60,63 @@ static DWORD thread_data_key; static void(WINAPI *GetCurrentThreadStackLimits_Kernel32)(PULONG_PTR, PULONG_PTR) = NULL; +static void +thread_data_list_add(os_thread_data *thread_data) +{ + os_mutex_lock(&thread_data_list_lock); + /* If already in list, just return */ + os_thread_data *p = &supervisor_thread_data; + while (p) { + if (p == thread_data) { + os_mutex_unlock(&thread_data_list_lock); + return; + } + p = p->next; + } + thread_data->next = supervisor_thread_data.next; + supervisor_thread_data.next = thread_data; + os_mutex_unlock(&thread_data_list_lock); +} + +static void +thread_data_list_remove(os_thread_data *thread_data) +{ + os_mutex_lock(&thread_data_list_lock); + /* Search and remove it from list */ + os_thread_data *p = &supervisor_thread_data; + while (p && p->next != thread_data) + p = p->next; + + if (p && p->next) { + bh_assert(p->next == thread_data); + p->next = p->next->next; + /* Release the resources in thread_data */ + os_cond_destroy(&thread_data->wait_cond); + os_mutex_destroy(&thread_data->wait_lock); + os_sem_destroy(&thread_data->wait_node.sem); + BH_FREE(thread_data); + } + os_mutex_unlock(&thread_data_list_lock); +} + +static os_thread_data * +thread_data_list_lookup(korp_tid tid) +{ + os_thread_data *thread_data = (os_thread_data *)tid; + os_mutex_lock(&thread_data_list_lock); + os_thread_data *p = supervisor_thread_data.next; + while (p) { + if (p == thread_data) { + /* Found */ + os_mutex_unlock(&thread_data_list_lock); + return p; + } + p = p->next; + } + os_mutex_unlock(&thread_data_list_lock); + return NULL; +} + int os_sem_init(korp_sem *sem); int @@ -254,10 +311,7 @@ os_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start, } /* Add thread data into thread data list */ - os_mutex_lock(&thread_data_list_lock); - thread_data->next = supervisor_thread_data.next; - supervisor_thread_data.next = thread_data; - os_mutex_unlock(&thread_data_list_lock); + thread_data_list_add(thread_data); /* Wait for the thread routine to set thread_data's tid and add thread_data to thread data list */ @@ -302,8 +356,12 @@ os_thread_join(korp_tid thread, void **p_retval) curr_thread_data->wait_node.next = NULL; /* Get thread data of thread to join */ - thread_data = (os_thread_data *)thread; - bh_assert(thread_data); + thread_data = thread_data_list_lookup(thread); + + if (thread_data == NULL) { + os_printf("Can't join thread %p, it does not exist", thread); + return BHT_ERROR; + } os_mutex_lock(&thread_data->wait_lock); @@ -312,6 +370,7 @@ os_thread_join(korp_tid thread, void **p_retval) if (p_retval) *p_retval = thread_data->thread_retval; os_mutex_unlock(&thread_data->wait_lock); + thread_data_list_remove(thread_data); return BHT_OK; } @@ -332,6 +391,7 @@ os_thread_join(korp_tid thread, void **p_retval) os_sem_wait(&curr_thread_data->wait_node.sem); if (p_retval) *p_retval = curr_thread_data->wait_node.retval; + thread_data_list_remove(thread_data); return BHT_OK; } diff --git a/core/shared/platform/zephyr/zephyr_thread.c b/core/shared/platform/zephyr/zephyr_thread.c index 628a842d6..31aaad7a9 100644 --- a/core/shared/platform/zephyr/zephyr_thread.c +++ b/core/shared/platform/zephyr/zephyr_thread.c @@ -393,6 +393,16 @@ os_thread_join(korp_tid thread, void **value_ptr) os_thread_data *thread_data; os_thread_wait_node *node; + /* Get thread data */ + thread_data = thread_data_list_lookup(thread); + + if (thread_data == NULL) { + os_printf( + "Can't join thread %p, probably already exited or does not exist", + thread); + return BHT_OK; + } + /* Create wait node and append it to wait list */ if (!(node = BH_MALLOC(sizeof(os_thread_wait_node)))) return BHT_ERROR; @@ -400,10 +410,6 @@ os_thread_join(korp_tid thread, void **value_ptr) sem_init(&node->sem, 0, 1); node->next = NULL; - /* Get thread data */ - thread_data = thread_data_list_lookup(thread); - bh_assert(thread_data != NULL); - mutex_lock(&thread_data->wait_list_lock, K_FOREVER); if (!thread_data->thread_wait_list) thread_data->thread_wait_list = node; diff --git a/core/shared/utils/bh_atomic.h b/core/shared/utils/bh_atomic.h index 5148497f8..57cb4d1c9 100644 --- a/core/shared/utils/bh_atomic.h +++ b/core/shared/utils/bh_atomic.h @@ -114,7 +114,8 @@ typedef uint16 bh_atomic_16_t; #endif /* On some 32-bit platform, disable 64-bit atomic operations, otherwise - * undefined reference to `__atomic_load_8' */ + * undefined reference to `__atomic_load_8', if on Zephyr, can add board related + * macro in autoconf.h to control */ #ifndef WASM_UINT64_IS_ATOMIC #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) \ && !defined(__OpenBSD__) && (defined(__riscv) || defined(__arm__)) \ From c32a6ceae1114d177b493ecaf949213ac29db910 Mon Sep 17 00:00:00 2001 From: kk Date: Fri, 6 Dec 2024 14:54:37 +0800 Subject: [PATCH 25/28] Refactor SConscript and add file checks in iwasm.c (#3945) --- core/iwasm/common/SConscript | 11 ++++------- product-mini/platforms/rt-thread/iwasm.c | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/iwasm/common/SConscript b/core/iwasm/common/SConscript index 0a55f244b..eb69744e9 100644 --- a/core/iwasm/common/SConscript +++ b/core/iwasm/common/SConscript @@ -13,13 +13,10 @@ cwd = GetCurrentDir() src = Glob('*.c') -if rtconfig.ARCH == 'arm': - if re.match('^cortex-m.*', rtconfig.CPU): - src += ['arch/invokeNative_thumb.s'] - elif re.match('^cortex-a.*', rtconfig.CPU): - src += ['arch/invokeNative_arm.s'] -elif rtconfig.ARCH == 'ia32': - src += ['arch/invokeNative_ia32.s'] +if rtconfig.ARCH == 'arm' and re.match('^cortex-m.*', rtconfig.CPU): + src += ['arch/invokeNative_thumb.s'] +else: + src.append(f"arch/invokeNative_{rtconfig.ARCH}.s") CPPPATH = [cwd, cwd + '/../include'] diff --git a/product-mini/platforms/rt-thread/iwasm.c b/product-mini/platforms/rt-thread/iwasm.c index 7d15a041d..f8932bdec 100644 --- a/product-mini/platforms/rt-thread/iwasm.c +++ b/product-mini/platforms/rt-thread/iwasm.c @@ -192,6 +192,21 @@ my_read_file_to_buffer(char *filename, rt_uint32_t *size) { struct stat f_stat; + if (!filename || !size) { + rt_set_errno(-EINVAL); + return RT_NULL; + } + + if (stat(filename, &f_stat) != 0) { + rt_set_errno(errno); + return RT_NULL; + } + + if (f_stat.st_size <= 0) { + rt_set_errno(-EINVAL); + return RT_NULL; + } + rt_uint8_t *buff = rt_malloc(f_stat.st_size); *size = 0; if (!buff) { From f665e7b739c83506bc44d7a08a26561a3f93a6ff Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Dec 2024 10:39:04 +0000 Subject: [PATCH 26/28] build(deps): bump github/codeql-action from 3.27.5 to 3.27.6 Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.27.5 to 3.27.6. - [Release notes](https://github.com/github/codeql-action/releases) - [Commits](https://github.com/github/codeql-action/compare/v3.27.5...v3.27.6) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/codeql.yml | 6 +++--- .github/workflows/supply_chain.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 9ed8919c7..88f13cc8b 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -53,7 +53,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3.27.5 + uses: github/codeql-action/init@v3.27.6 with: languages: ${{ matrix.language }} @@ -70,7 +70,7 @@ jobs: - run: | ./.github/scripts/codeql_buildscript.sh - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3.27.5 + uses: github/codeql-action/analyze@v3.27.6 with: category: "/language:${{matrix.language}}" upload: false @@ -99,7 +99,7 @@ jobs: output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif - name: Upload CodeQL results to code scanning - uses: github/codeql-action/upload-sarif@v3.27.5 + uses: github/codeql-action/upload-sarif@v3.27.6 with: sarif_file: ${{ steps.step1.outputs.sarif-output }} category: "/language:${{matrix.language}}" diff --git a/.github/workflows/supply_chain.yml b/.github/workflows/supply_chain.yml index f2aa485ea..2d605f6c9 100644 --- a/.github/workflows/supply_chain.yml +++ b/.github/workflows/supply_chain.yml @@ -60,6 +60,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@3d3d628990a5f99229dd9fa1821cc5a4f31b613b # v2.2.4 + uses: github/codeql-action/upload-sarif@6f9e628e6f9a18c785dd746325ba455111df1b67 # v2.2.4 with: sarif_file: results.sarif From 591b74057101fbb8ae91dc9ecc14a41684c2f325 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Tue, 10 Dec 2024 20:26:14 +0800 Subject: [PATCH 27/28] Consume the placeholders that were put when emitting table info (#3940) --- core/iwasm/aot/aot_loader.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index dd26bbee5..239e328bd 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -1333,7 +1333,12 @@ load_import_table_list(const uint8 **p_buf, const uint8 *buf_end, if (wasm_is_type_multi_byte_type(import_table->table_type.elem_type)) { read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable); } + else #endif + { + /* Skip 1 byte */ + buf += 1; + } read_uint32(buf, buf_end, import_table->table_type.init_size); read_uint32(buf, buf_end, import_table->table_type.max_size); #if WASM_ENABLE_GC != 0 @@ -1393,7 +1398,12 @@ load_table_list(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module, if (wasm_is_type_multi_byte_type(table->table_type.elem_type)) { read_uint8(buf, buf_end, ref_type.ref_ht_common.nullable); } + else #endif + { + /* Skip 1 byte */ + buf += 1; + } read_uint32(buf, buf_end, table->table_type.init_size); read_uint32(buf, buf_end, table->table_type.max_size); #if WASM_ENABLE_GC != 0 @@ -1481,7 +1491,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end, else #endif { - /* Skip 8 byte for ref type info */ + /* Skip 8 byte(2+2+4) for ref type info */ buf += 8; } From bebdd4ad174a49005b62e4074e71cd32b05640e8 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 10 Dec 2024 20:26:32 +0800 Subject: [PATCH 28/28] Fix aot table instantiate (#3946) --- core/iwasm/aot/aot_runtime.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 8a33a7271..0f7b5d3d9 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -689,20 +689,23 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module, tbl_inst->cur_size = import_table->table_type.init_size; tbl_inst->max_size = aot_get_imp_tbl_data_slots(import_table, false); - tbl_inst->elem_type = module->tables[i].table_type.elem_type; + tbl_inst->elem_type = import_table->table_type.elem_type; + tbl_inst->is_table64 = + import_table->table_type.flags & TABLE64_FLAG; #if WASM_ENABLE_GC != 0 tbl_inst->elem_ref_type.elem_ref_type = - module->tables[i].table_type.elem_ref_type; + import_table->table_type.elem_ref_type; #endif } else { AOTTable *table = module->tables + (i - module->import_table_count); tbl_inst->cur_size = table->table_type.init_size; tbl_inst->max_size = aot_get_tbl_data_slots(table, false); - tbl_inst->elem_type = module->tables[i].table_type.elem_type; + tbl_inst->elem_type = table->table_type.elem_type; + tbl_inst->is_table64 = table->table_type.flags & TABLE64_FLAG; #if WASM_ENABLE_GC != 0 tbl_inst->elem_ref_type.elem_ref_type = - module->tables[i].table_type.elem_ref_type; + table->table_type.elem_ref_type; #endif }