From a2015fbcf28c15faa2e64e171e5c722cd9b48f56 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Mon, 1 Apr 2024 11:01:18 +0800 Subject: [PATCH] Cumulative fixes for release/1.3.x --- .github/workflows/nightly_run.yml | 4 +- CMakeLists.txt | 2 +- CONTRIBUTING.md | 2 +- core/iwasm/aot/aot_loader.c | 11 ++ core/iwasm/common/wasm_memory.c | 36 ++-- core/iwasm/common/wasm_runtime_common.c | 4 +- core/iwasm/common/wasm_shared_memory.c | 4 +- core/iwasm/compilation/aot_compiler.h | 7 +- core/iwasm/compilation/aot_emit_aot_file.c | 31 ++-- core/iwasm/compilation/aot_llvm.c | 2 +- .../compilation/debug/dwarf_extractor.cpp | 38 +++-- core/iwasm/fast-jit/fe/jit_emit_control.c | 2 +- core/iwasm/interpreter/wasm_interp_classic.c | 2 +- core/iwasm/interpreter/wasm_loader.c | 154 ++++++++++++++---- core/iwasm/interpreter/wasm_mini_loader.c | 149 +++++++++++++---- .../lib-socket/lib_socket_wasi.cmake | 2 +- .../libc-builtin/libc_builtin_wrapper.c | 2 +- .../shared/platform/common/posix/posix_file.c | 6 +- .../shared/platform/esp-idf/espidf_platform.c | 6 +- .../platform/zephyr/platform_internal.h | 11 +- .../platform/zephyr/shared_platform.cmake | 4 +- core/shared/platform/zephyr/zephyr_platform.c | 20 ++- language-bindings/go/wamr/module.go | 4 +- 23 files changed, 372 insertions(+), 131 deletions(-) diff --git a/.github/workflows/nightly_run.yml b/.github/workflows/nightly_run.yml index 8b9a0ed5e..21b1be901 100644 --- a/.github/workflows/nightly_run.yml +++ b/.github/workflows/nightly_run.yml @@ -632,7 +632,9 @@ jobs: run: echo "TEST_ON_X86_32=true" >> $GITHUB_ENV - name: set additional tsan options - run: echo "TSAN_OPTIONS=suppressions=$PWD/tsan_suppressions.txt" >> $GITHUB_ENV + run: | + echo "TSAN_OPTIONS=suppressions=$PWD/tsan_suppressions.txt" >> $GITHUB_ENV + sudo sysctl vm.mmap_rnd_bits=28 working-directory: tests/wamr-test-suites #only download llvm libraries in jit and aot mode diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c8799494..8df86ddd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,7 +151,7 @@ if (WAMR_BUILD_WASM_CACHE EQUAL 1) endif () if (MINGW) - target_link_libraries (iwasm_shared -lWs2_32) + target_link_libraries (iwasm_shared INTERFACE -lWs2_32 -lwsock32) endif () install (TARGETS iwasm_shared LIBRARY DESTINATION lib) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9210b3deb..0e04101d2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -27,7 +27,7 @@ We Use Github Flow, So All Code Changes Happen Through Pull Requests. Pull reque Coding Style =============================== Please use [K&R](https://en.wikipedia.org/wiki/Indentation_style#K.26R) coding style, such as 4 spaces for indentation rather than tabs etc. -We suggest use Eclipse like IDE or stable coding format tools to make your code compliant to K&R format. +We suggest using VS Code like IDE or stable coding format tools, like clang-format, to make your code compliant to the customized format(in .clang-format). Report bugs =================== diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 282dc1d67..6983601c4 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -2072,6 +2072,17 @@ do_text_relocation(AOTModule *module, AOTRelocationGroup *group, } symbol_addr = module->func_ptrs[func_index]; } + else if (!strncmp(symbol, "_" AOT_FUNC_INTERNAL_PREFIX, + strlen("_" AOT_FUNC_INTERNAL_PREFIX))) { + p = symbol + strlen("_" AOT_FUNC_INTERNAL_PREFIX); + if (*p == '\0' + || (func_index = (uint32)atoi(p)) > module->func_count) { + set_error_buf_v(error_buf, error_buf_size, "invalid symbol %s", + symbol); + goto check_symbol_fail; + } + symbol_addr = module->func_ptrs[func_index]; + } #endif else if (is_text_section(symbol)) { symbol_addr = module->code; diff --git a/core/iwasm/common/wasm_memory.c b/core/iwasm/common/wasm_memory.c index 33ba44161..2f78b3327 100644 --- a/core/iwasm/common/wasm_memory.c +++ b/core/iwasm/common/wasm_memory.c @@ -6,7 +6,6 @@ #include "wasm_runtime_common.h" #include "../interpreter/wasm_runtime.h" #include "../aot/aot_runtime.h" -#include "bh_platform.h" #include "mem_alloc.h" #include "wasm_memory.h" @@ -45,11 +44,11 @@ static unsigned int global_pool_size; static bool wasm_memory_init_with_pool(void *mem, unsigned int bytes) { - mem_allocator_t _allocator = mem_allocator_create(mem, bytes); + mem_allocator_t allocator = mem_allocator_create(mem, bytes); - if (_allocator) { + if (allocator) { memory_mode = MEMORY_MODE_POOL; - pool_allocator = _allocator; + pool_allocator = allocator; global_pool_size = bytes; return true; } @@ -76,18 +75,18 @@ wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func, } #else static bool -wasm_memory_init_with_allocator(void *_malloc_func, void *_realloc_func, - void *_free_func) +wasm_memory_init_with_allocator(void *malloc_func_ptr, void *realloc_func_ptr, + void *free_func_ptr) { - if (_malloc_func && _free_func && _malloc_func != _free_func) { + if (malloc_func_ptr && free_func_ptr && malloc_func_ptr != free_func_ptr) { memory_mode = MEMORY_MODE_ALLOCATOR; - malloc_func = _malloc_func; - realloc_func = _realloc_func; - free_func = _free_func; + malloc_func = malloc_func_ptr; + realloc_func = realloc_func_ptr; + free_func = free_func_ptr; return true; } - LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", _malloc_func, - _realloc_func, _free_func); + LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n", + malloc_func_ptr, realloc_func_ptr, free_func_ptr); return false; } #endif @@ -115,18 +114,13 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type, alloc_option->pool.heap_size); } else if (mem_alloc_type == Alloc_With_Allocator) { + return wasm_memory_init_with_allocator( #if WASM_MEM_ALLOC_WITH_USER_DATA != 0 - return wasm_memory_init_with_allocator( alloc_option->allocator.user_data, - alloc_option->allocator.malloc_func, - alloc_option->allocator.realloc_func, - alloc_option->allocator.free_func); -#else - return wasm_memory_init_with_allocator( - alloc_option->allocator.malloc_func, - alloc_option->allocator.realloc_func, - alloc_option->allocator.free_func); #endif + alloc_option->allocator.malloc_func, + alloc_option->allocator.realloc_func, + alloc_option->allocator.free_func); } else if (mem_alloc_type == Alloc_With_System_Allocator) { memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR; diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index eaee4f225..9ba2db9ec 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -3574,7 +3574,7 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, { WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env); typedef void (*NativeRawFuncPtr)(WASMExecEnv *, uint64 *); - NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr; + NativeRawFuncPtr invoke_native_raw = (NativeRawFuncPtr)func_ptr; uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size; uint32 *argv_src = argv, i, argc1, ptr_len; uint32 arg_i32; @@ -3662,7 +3662,7 @@ wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr, } exec_env->attachment = attachment; - invokeNativeRaw(exec_env, argv1); + invoke_native_raw(exec_env, argv1); exec_env->attachment = NULL; if (func_type->result_count > 0) { diff --git a/core/iwasm/common/wasm_shared_memory.c b/core/iwasm/common/wasm_shared_memory.c index e7110c0f1..3856b7d19 100644 --- a/core/iwasm/common/wasm_shared_memory.c +++ b/core/iwasm/common/wasm_shared_memory.c @@ -232,14 +232,14 @@ destroy_wait_info(void *wait_info) } static void -map_try_release_wait_info(HashMap *wait_map_, AtomicWaitInfo *wait_info, +map_try_release_wait_info(HashMap *wait_hash_map, AtomicWaitInfo *wait_info, void *address) { if (wait_info->wait_list->len > 0) { return; } - bh_hash_map_remove(wait_map_, address, NULL, NULL); + bh_hash_map_remove(wait_hash_map, address, NULL, NULL); destroy_wait_info(wait_info); } diff --git a/core/iwasm/compilation/aot_compiler.h b/core/iwasm/compilation/aot_compiler.h index b6347c89d..22f95ccfe 100644 --- a/core/iwasm/compilation/aot_compiler.h +++ b/core/iwasm/compilation/aot_compiler.h @@ -203,7 +203,12 @@ check_type_compatible(uint8 src_type, uint8 dst_type) goto fail; \ } \ memset(aot_value, 0, sizeof(AOTValue)); \ - aot_value->type = value_type; \ + if (comp_ctx->enable_ref_types \ + && (value_type == VALUE_TYPE_FUNCREF \ + || value_type == VALUE_TYPE_EXTERNREF)) \ + aot_value->type = VALUE_TYPE_I32; \ + else \ + aot_value->type = value_type; \ aot_value->value = llvm_value; \ aot_value_stack_push( \ &func_ctx->block_stack.block_list_end->value_stack, aot_value); \ diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index d674b022c..e43c8939c 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -1004,28 +1004,28 @@ 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 void -exchange_uint128(uint8 *pData) +exchange_uint128(uint8 *p_data) { /* swap high 64bit and low 64bit */ - uint64 value = *(uint64 *)pData; - *(uint64 *)pData = *(uint64 *)(pData + 8); - *(uint64 *)(pData + 8) = value; + uint64 value = *(uint64 *)p_data; + *(uint64 *)p_data = *(uint64 *)(p_data + 8); + *(uint64 *)(p_data + 8) = value; /* exchange high 64bit */ - exchange_uint64(pData); + exchange_uint64(p_data); /* exchange low 64bit */ - exchange_uint64(pData + 8); + exchange_uint64(p_data + 8); } static union { @@ -3106,7 +3106,12 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data, * Note: aot_stack_sizes_section_name section only contains * stack_sizes table. */ - if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)) { + if (!strcmp(relocation->symbol_name, aot_stack_sizes_name) + /* in windows 32, the symbol name may start with '_' */ + || (strlen(relocation->symbol_name) > 0 + && relocation->symbol_name[0] == '_' + && !strcmp(relocation->symbol_name + 1, + aot_stack_sizes_name))) { /* discard const */ relocation->symbol_name = (char *)aot_stack_sizes_section_name; } diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index 25b2e4e2a..b6dfcf6f2 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -619,7 +619,7 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, LLVMModuleRef module, const char *prefix = AOT_FUNC_PREFIX; const bool need_precheck = comp_ctx->enable_stack_bound_check || comp_ctx->enable_stack_estimation; - LLVMValueRef precheck_func; + LLVMValueRef precheck_func = NULL; if (need_precheck) { precheck_func = aot_add_llvm_func1(comp_ctx, module, func_index, aot_func_type->param_count, diff --git a/core/iwasm/compilation/debug/dwarf_extractor.cpp b/core/iwasm/compilation/debug/dwarf_extractor.cpp index d322aefe5..06618ad70 100644 --- a/core/iwasm/compilation/debug/dwarf_extractor.cpp +++ b/core/iwasm/compilation/debug/dwarf_extractor.cpp @@ -354,10 +354,27 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx, LLVMDIBuilderCreateExpression(DIB, NULL, 0); auto variable_list = function.GetBlock().GetVariables(extractor->target, true, false, false); + unsigned int variable_offset = 0; if (num_function_args != variable_list.GetSize()) { - LOG_ERROR( - "function args number dismatch!:value number=%d, function args=%d", - variable_list.GetSize(), num_function_args); + // A hack to detect C++ "this" pointer. + // + // REVISIT: is there a more reliable way? + // At the DWARF level, we can probably look at DW_AT_object_pointer + // and DW_AT_artificial. I'm not sure how it can be done via the + // LLDB API though. + if (num_function_args + 1 == variable_list.GetSize()) { + SBValue variable(variable_list.GetValueAtIndex(0)); + const char *varname = variable.GetName(); + if (varname != NULL && !strcmp(varname, "this")) { + variable_offset = 1; + } + } + if (!variable_offset) { + LOG_ERROR("function args number dismatch!:function %s %s value " + "number=%d, function args=%d", + function_name, function.GetMangledName(), + variable_list.GetSize(), num_function_args); + } } LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation( @@ -378,23 +395,24 @@ lldb_function_to_function_dbi(const AOTCompContext *comp_ctx, LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression, ParamLocation, block_curr); - for (uint32_t function_arg_idx = 0; - function_arg_idx < variable_list.GetSize(); ++function_arg_idx) { - SBValue variable(variable_list.GetValueAtIndex(function_arg_idx)); + for (uint32_t function_arg_idx = 0; function_arg_idx < num_function_args; + ++function_arg_idx) { + uint32_t variable_idx = variable_offset + function_arg_idx; + SBValue variable(variable_list.GetValueAtIndex(variable_idx)); if (variable.IsValid()) { SBDeclaration dec(variable.GetDeclaration()); auto valtype = variable.GetType(); LLVMMetadataRef ParamLocation = LLVMDIBuilderCreateDebugLocation( comp_ctx->context, dec.GetLine(), dec.GetColumn(), FunctionMetadata, NULL); + const char *varname = variable.GetName(); LLVMMetadataRef ParamVar = LLVMDIBuilderCreateParameterVariable( - DIB, FunctionMetadata, variable.GetName(), - strlen(variable.GetName()), function_arg_idx + 1 + 1, + DIB, FunctionMetadata, varname, varname ? strlen(varname) : 0, + variable_idx + 1 + 1, File, // starts form 1, and 1 is exenv, dec.GetLine(), ParamTypes[function_arg_idx + 1], true, LLVMDIFlagZero); - LLVMValueRef Param = - LLVMGetParam(func_ctx->func, function_arg_idx + 1); + LLVMValueRef Param = LLVMGetParam(func_ctx->func, variable_idx + 1); LLVMDIBuilderInsertDbgValueAtEnd(DIB, Param, ParamVar, ParamExpression, ParamLocation, block_curr); diff --git a/core/iwasm/fast-jit/fe/jit_emit_control.c b/core/iwasm/fast-jit/fe/jit_emit_control.c index 47ab1d51e..fc6b9737e 100644 --- a/core/iwasm/fast-jit/fe/jit_emit_control.c +++ b/core/iwasm/fast-jit/fe/jit_emit_control.c @@ -1101,7 +1101,7 @@ jit_compile_op_br_if(JitCompContext *cc, uint32 br_depth, } } - /* Only opy parameters or results when their count > 0 and + /* Only copy parameters or results when their count > 0 and the src/dst addr are different */ copy_arities = check_copy_arities(block_dst, jit_frame); diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 7ce16b5df..0348bbe22 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -3844,7 +3844,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, #if WASM_ENABLE_SHARED_MEMORY != 0 HANDLE_OP(WASM_OP_ATOMIC_PREFIX) { - uint32 offset = 0, align, addr; + uint32 offset = 0, align = 0, addr; uint32 opcode1; read_leb_uint32(frame_ip, frame_ip_end, opcode1); diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index ec17e4a09..4208bcea7 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -5832,11 +5832,11 @@ fail: wasm_loader_emit_ptr(loader_ctx, NULL); \ } while (0) -#define emit_br_info(frame_csp) \ - do { \ - if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \ - error_buf_size)) \ - goto fail; \ +#define emit_br_info(frame_csp, is_br) \ + do { \ + if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \ + error_buf_size)) \ + goto fail; \ } while (0) #define LAST_OP_OUTPUT_I32() \ @@ -6223,7 +6223,7 @@ apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type) static bool wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, - char *error_buf, uint32 error_buf_size) + bool is_br, char *error_buf, uint32 error_buf_size) { /* br info layout: * a) arity of target block @@ -6271,6 +6271,8 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, /* Part e */ dynamic_offset = frame_csp->dynamic_offset + wasm_get_cell_num(types, arity); + if (is_br) + ctx->dynamic_offset = dynamic_offset; for (i = (int32)arity - 1; i >= 0; i--) { cell = (uint8)wasm_value_type_cell_num(types[i]); dynamic_offset -= cell; @@ -6872,13 +6874,6 @@ fail: local_offset = local_offsets[local_idx]; \ } while (0) -#define CHECK_BR(depth) \ - do { \ - if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \ - error_buf_size)) \ - goto fail; \ - } while (0) - static bool check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size) { @@ -7061,7 +7056,7 @@ check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf, #endif /* end of WASM_ENABLE_SHARED_MEMORY */ static bool -wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, +wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode, char *error_buf, uint32 error_buf_size) { BranchBlock *target_block, *cur_block; @@ -7071,6 +7066,20 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, int32 i, available_stack_cell; uint16 cell_num; + uint8 *frame_ref_old = loader_ctx->frame_ref; + uint8 *frame_ref_after_popped = NULL; + uint8 frame_ref_tmp[4] = { 0 }; + uint8 *frame_ref_buf = frame_ref_tmp; + uint32 stack_cell_num_old = loader_ctx->stack_cell_num; +#if WASM_ENABLE_FAST_INTERP != 0 + int16 *frame_offset_old = loader_ctx->frame_offset; + int16 *frame_offset_after_popped = NULL; + int16 frame_offset_tmp[4] = { 0 }; + int16 *frame_offset_buf = frame_offset_tmp; + uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset; +#endif + bool ret = false; + bh_assert(loader_ctx->csp_num > 0); if (loader_ctx->csp_num - 1 < depth) { set_error_buf(error_buf, error_buf_size, @@ -7102,6 +7111,38 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, #endif POP_TYPE(types[i]); } + + /* Backup stack data since it may be changed in the below + push operations, and the stack data may be used when + checking other target blocks of opcode br_table */ + if (opcode == WASM_OP_BR_TABLE) { + uint64 total_size; + + frame_ref_after_popped = loader_ctx->frame_ref; + total_size = (uint64)sizeof(uint8) + * (frame_ref_old - frame_ref_after_popped); + if (total_size > sizeof(frame_ref_tmp) + && !(frame_ref_buf = loader_malloc(total_size, error_buf, + error_buf_size))) { + goto fail; + } + bh_memcpy_s(frame_ref_buf, (uint32)total_size, + frame_ref_after_popped, (uint32)total_size); + +#if WASM_ENABLE_FAST_INTERP != 0 + frame_offset_after_popped = loader_ctx->frame_offset; + total_size = (uint64)sizeof(int16) + * (frame_offset_old - frame_offset_after_popped); + if (total_size > sizeof(frame_offset_tmp) + && !(frame_offset_buf = loader_malloc(total_size, error_buf, + error_buf_size))) { + goto fail; + } + bh_memcpy_s(frame_offset_buf, (uint32)total_size, + frame_offset_after_popped, (uint32)total_size); +#endif + } + for (i = 0; i < (int32)arity; i++) { #if WASM_ENABLE_FAST_INTERP != 0 bool disable_emit = true; @@ -7110,7 +7151,41 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, #endif PUSH_TYPE(types[i]); } - return true; + +#if WASM_ENABLE_FAST_INTERP != 0 + emit_br_info(target_block, opcode == WASM_OP_BR); +#endif + + /* Restore the stack data, note that frame_ref_bottom, + frame_reftype_map_bottom, frame_offset_bottom may be + re-allocated in the above push operations */ + if (opcode == WASM_OP_BR_TABLE) { + uint32 total_size; + + /* The stack operand num should not be smaller than before + after pop and push operations */ + bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old); + loader_ctx->stack_cell_num = stack_cell_num_old; + loader_ctx->frame_ref = + loader_ctx->frame_ref_bottom + stack_cell_num_old; + total_size = (uint32)sizeof(uint8) + * (frame_ref_old - frame_ref_after_popped); + bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size, + frame_ref_buf, total_size); + +#if WASM_ENABLE_FAST_INTERP != 0 + loader_ctx->frame_offset = + loader_ctx->frame_offset_bottom + stack_cell_num_old; + total_size = (uint32)sizeof(int16) + * (frame_offset_old - frame_offset_after_popped); + bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size, + total_size, frame_offset_buf, total_size); + (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old; +#endif + } + + ret = true; + goto cleanup_and_return; } available_stack_cell = @@ -7120,32 +7195,46 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, for (i = (int32)arity - 1; i >= 0; i--) { if (!check_stack_top_values(frame_ref, available_stack_cell, types[i], error_buf, error_buf_size)) - return false; + goto fail; cell_num = wasm_value_type_cell_num(types[i]); frame_ref -= cell_num; available_stack_cell -= cell_num; } - return true; +#if WASM_ENABLE_FAST_INTERP != 0 + emit_br_info(target_block, opcode == WASM_OP_BR); +#endif + ret = true; + +cleanup_and_return: fail: - return false; + if (frame_ref_buf && frame_ref_buf != frame_ref_tmp) + wasm_runtime_free(frame_ref_buf); +#if WASM_ENABLE_FAST_INTERP != 0 + if (frame_offset_buf && frame_offset_buf != frame_offset_tmp) + wasm_runtime_free(frame_offset_tmp); +#endif + + return ret; } static BranchBlock * check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end, - char *error_buf, uint32 error_buf_size) + uint8 opcode, char *error_buf, uint32 error_buf_size) { uint8 *p = *p_buf, *p_end = buf_end; BranchBlock *frame_csp_tmp; uint32 depth; read_leb_uint32(p, p_end, depth); - CHECK_BR(depth); + bh_assert(loader_ctx->csp_num > 0); + if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf, + error_buf_size)) { + goto fail; + } + frame_csp_tmp = loader_ctx->frame_csp - depth - 1; -#if WASM_ENABLE_FAST_INTERP != 0 - emit_br_info(frame_csp_tmp); -#endif *p_buf = p; return frame_csp_tmp; @@ -7876,8 +7965,9 @@ re_scan: SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true); /* check the target catching block: LABEL_TYPE_CATCH */ - if (!(frame_csp_tmp = check_branch_block( - loader_ctx, &p, p_end, error_buf, error_buf_size))) + if (!(frame_csp_tmp = + check_branch_block(loader_ctx, &p, p_end, opcode, + error_buf, error_buf_size))) goto fail; if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH @@ -8132,8 +8222,9 @@ re_scan: case WASM_OP_BR: { - if (!(frame_csp_tmp = check_branch_block( - loader_ctx, &p, p_end, error_buf, error_buf_size))) + if (!(frame_csp_tmp = + check_branch_block(loader_ctx, &p, p_end, opcode, + error_buf, error_buf_size))) goto fail; RESET_STACK(); @@ -8145,8 +8236,9 @@ re_scan: { POP_I32(); - if (!(frame_csp_tmp = check_branch_block( - loader_ctx, &p, p_end, error_buf, error_buf_size))) + if (!(frame_csp_tmp = + check_branch_block(loader_ctx, &p, p_end, opcode, + error_buf, error_buf_size))) goto fail; break; @@ -8158,7 +8250,7 @@ re_scan: uint32 ret_count = 0; #if WASM_ENABLE_FAST_INTERP == 0 uint8 *p_depth_begin, *p_depth; - uint32 depth, j; + uint32 depth = 0, j; BrTableCache *br_table_cache = NULL; p_org = p - 1; @@ -8175,7 +8267,7 @@ re_scan: #endif for (i = 0; i <= count; i++) { if (!(frame_csp_tmp = - check_branch_block(loader_ctx, &p, p_end, + check_branch_block(loader_ctx, &p, p_end, opcode, error_buf, error_buf_size))) goto fail; diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index c24706627..b6ddbcd4e 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -4087,11 +4087,11 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf, wasm_loader_emit_ptr(loader_ctx, NULL); \ } while (0) -#define emit_br_info(frame_csp) \ - do { \ - if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \ - error_buf_size)) \ - goto fail; \ +#define emit_br_info(frame_csp, is_br) \ + do { \ + if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \ + error_buf_size)) \ + goto fail; \ } while (0) #define LAST_OP_OUTPUT_I32() \ @@ -4476,7 +4476,7 @@ apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type) static bool wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, - char *error_buf, uint32 error_buf_size) + bool is_br, char *error_buf, uint32 error_buf_size) { /* br info layout: * a) arity of target block @@ -4525,6 +4525,8 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp, /* Part e */ dynamic_offset = frame_csp->dynamic_offset + wasm_get_cell_num(types, arity); + if (is_br) + ctx->dynamic_offset = dynamic_offset; for (i = (int32)arity - 1; i >= 0; i--) { cell = (uint8)wasm_value_type_cell_num(types[i]); dynamic_offset -= cell; @@ -5203,20 +5205,13 @@ fail: local_offset = local_offsets[local_idx]; \ } while (0) -#define CHECK_BR(depth) \ - do { \ - if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \ - error_buf_size)) \ - goto fail; \ - } while (0) - #define CHECK_MEMORY() \ do { \ bh_assert(module->import_memory_count + module->memory_count > 0); \ } while (0) static bool -wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, +wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode, char *error_buf, uint32 error_buf_size) { BranchBlock *target_block, *cur_block; @@ -5226,6 +5221,20 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, int32 i, available_stack_cell; uint16 cell_num; + uint8 *frame_ref_old = loader_ctx->frame_ref; + uint8 *frame_ref_after_popped = NULL; + uint8 frame_ref_tmp[4] = { 0 }; + uint8 *frame_ref_buf = frame_ref_tmp; + uint32 stack_cell_num_old = loader_ctx->stack_cell_num; +#if WASM_ENABLE_FAST_INTERP != 0 + int16 *frame_offset_old = loader_ctx->frame_offset; + int16 *frame_offset_after_popped = NULL; + int16 frame_offset_tmp[4] = { 0 }; + int16 *frame_offset_buf = frame_offset_tmp; + uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset; +#endif + bool ret = false; + bh_assert(loader_ctx->csp_num > 0); if (loader_ctx->csp_num - 1 < depth) { set_error_buf(error_buf, error_buf_size, @@ -5257,6 +5266,38 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, #endif POP_TYPE(types[i]); } + + /* Backup stack data since it may be changed in the below + push operations, and the stack data may be used when + checking other target blocks of opcode br_table */ + if (opcode == WASM_OP_BR_TABLE) { + uint64 total_size; + + frame_ref_after_popped = loader_ctx->frame_ref; + total_size = (uint64)sizeof(uint8) + * (frame_ref_old - frame_ref_after_popped); + if (total_size > sizeof(frame_ref_tmp) + && !(frame_ref_buf = loader_malloc(total_size, error_buf, + error_buf_size))) { + goto fail; + } + bh_memcpy_s(frame_ref_buf, (uint32)total_size, + frame_ref_after_popped, (uint32)total_size); + +#if WASM_ENABLE_FAST_INTERP != 0 + frame_offset_after_popped = loader_ctx->frame_offset; + total_size = (uint64)sizeof(int16) + * (frame_offset_old - frame_offset_after_popped); + if (total_size > sizeof(frame_offset_tmp) + && !(frame_offset_buf = loader_malloc(total_size, error_buf, + error_buf_size))) { + goto fail; + } + bh_memcpy_s(frame_offset_buf, (uint32)total_size, + frame_offset_after_popped, (uint32)total_size); +#endif + } + for (i = 0; i < (int32)arity; i++) { #if WASM_ENABLE_FAST_INTERP != 0 bool disable_emit = true; @@ -5265,7 +5306,41 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, #endif PUSH_TYPE(types[i]); } - return true; + +#if WASM_ENABLE_FAST_INTERP != 0 + emit_br_info(target_block, opcode == WASM_OP_BR); +#endif + + /* Restore the stack data, note that frame_ref_bottom, + frame_reftype_map_bottom, frame_offset_bottom may be + re-allocated in the above push operations */ + if (opcode == WASM_OP_BR_TABLE) { + uint32 total_size; + + /* The stack operand num should not be smaller than before + after pop and push operations */ + bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old); + loader_ctx->stack_cell_num = stack_cell_num_old; + loader_ctx->frame_ref = + loader_ctx->frame_ref_bottom + stack_cell_num_old; + total_size = (uint32)sizeof(uint8) + * (frame_ref_old - frame_ref_after_popped); + bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size, + frame_ref_buf, total_size); + +#if WASM_ENABLE_FAST_INTERP != 0 + loader_ctx->frame_offset = + loader_ctx->frame_offset_bottom + stack_cell_num_old; + total_size = (uint32)sizeof(int16) + * (frame_offset_old - frame_offset_after_popped); + bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size, + total_size, frame_offset_buf, total_size); + (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old; +#endif + } + + ret = true; + goto cleanup_and_return; } available_stack_cell = @@ -5275,32 +5350,46 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, for (i = (int32)arity - 1; i >= 0; i--) { if (!check_stack_top_values(frame_ref, available_stack_cell, types[i], error_buf, error_buf_size)) - return false; + goto fail; cell_num = wasm_value_type_cell_num(types[i]); frame_ref -= cell_num; available_stack_cell -= cell_num; } - return true; +#if WASM_ENABLE_FAST_INTERP != 0 + emit_br_info(target_block, opcode == WASM_OP_BR); +#endif + ret = true; + +cleanup_and_return: fail: - return false; + if (frame_ref_buf && frame_ref_buf != frame_ref_tmp) + wasm_runtime_free(frame_ref_buf); +#if WASM_ENABLE_FAST_INTERP != 0 + if (frame_offset_buf && frame_offset_buf != frame_offset_tmp) + wasm_runtime_free(frame_offset_tmp); +#endif + + return ret; } static BranchBlock * check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end, - char *error_buf, uint32 error_buf_size) + uint8 opcode, char *error_buf, uint32 error_buf_size) { uint8 *p = *p_buf, *p_end = buf_end; BranchBlock *frame_csp_tmp; uint32 depth; read_leb_uint32(p, p_end, depth); - CHECK_BR(depth); + bh_assert(loader_ctx->csp_num > 0); + if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf, + error_buf_size)) { + goto fail; + } + frame_csp_tmp = loader_ctx->frame_csp - depth - 1; -#if WASM_ENABLE_FAST_INTERP != 0 - emit_br_info(frame_csp_tmp); -#endif *p_buf = p; return frame_csp_tmp; @@ -5925,8 +6014,9 @@ re_scan: case WASM_OP_BR: { - if (!(frame_csp_tmp = check_branch_block( - loader_ctx, &p, p_end, error_buf, error_buf_size))) + if (!(frame_csp_tmp = + check_branch_block(loader_ctx, &p, p_end, opcode, + error_buf, error_buf_size))) goto fail; RESET_STACK(); @@ -5938,8 +6028,9 @@ re_scan: { POP_I32(); - if (!(frame_csp_tmp = check_branch_block( - loader_ctx, &p, p_end, error_buf, error_buf_size))) + if (!(frame_csp_tmp = + check_branch_block(loader_ctx, &p, p_end, opcode, + error_buf, error_buf_size))) goto fail; break; @@ -5951,7 +6042,7 @@ re_scan: uint32 ret_count = 0; #if WASM_ENABLE_FAST_INTERP == 0 uint8 *p_depth_begin, *p_depth; - uint32 depth, j; + uint32 depth = 0, j; BrTableCache *br_table_cache = NULL; p_org = p - 1; @@ -5968,7 +6059,7 @@ re_scan: #endif for (i = 0; i <= count; i++) { if (!(frame_csp_tmp = - check_branch_block(loader_ctx, &p, p_end, + check_branch_block(loader_ctx, &p, p_end, opcode, error_buf, error_buf_size))) goto fail; diff --git a/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake b/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake index 209b0c4c9..8ddddffeb 100644 --- a/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake +++ b/core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake @@ -3,7 +3,7 @@ cmake_minimum_required (VERSION 2.8...3.16) -project(socket_wasi_ext) +project(socket_wasi_ext LANGUAGES C) add_library(${PROJECT_NAME} STATIC ${CMAKE_CURRENT_LIST_DIR}/src/wasi/wasi_socket_ext.c) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/inc/) diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c index 8e6a65a4a..923840f3b 100644 --- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c +++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c @@ -426,7 +426,7 @@ sprintf_wrapper(wasm_exec_env_t exec_env, char *str, const char *format, if (!wasm_runtime_get_native_addr_range(module_inst, (uint8 *)str, NULL, &native_end_offset)) { wasm_runtime_set_exception(module_inst, "out of bounds memory access"); - return false; + return 0; } ctx.str = str; diff --git a/core/shared/platform/common/posix/posix_file.c b/core/shared/platform/common/posix/posix_file.c index ad5589f73..8e53da8b6 100644 --- a/core/shared/platform/common/posix/posix_file.c +++ b/core/shared/platform/common/posix/posix_file.c @@ -384,7 +384,7 @@ os_openat(os_file_handle handle, const char *path, __wasi_oflags_t oflags, // Linux returns ENXIO instead of EOPNOTSUPP when opening a socket. if (openat_errno == ENXIO) { struct stat sb; - int ret = fstatat(fd, path, &sb, + int ret = fstatat(handle, path, &sb, (lookup_flags & __WASI_LOOKUP_SYMLINK_FOLLOW) ? 0 : AT_SYMLINK_NOFOLLOW); @@ -396,7 +396,7 @@ os_openat(os_file_handle handle, const char *path, __wasi_oflags_t oflags, if (openat_errno == ENOTDIR && (open_flags & (O_NOFOLLOW | O_DIRECTORY)) != 0) { struct stat sb; - int ret = fstatat(fd, path, &sb, AT_SYMLINK_NOFOLLOW); + int ret = fstatat(handle, path, &sb, AT_SYMLINK_NOFOLLOW); if (S_ISLNK(sb.st_mode)) { return __WASI_ELOOP; } @@ -1006,4 +1006,4 @@ char * os_realpath(const char *path, char *resolved_path) { return realpath(path, resolved_path); -} \ 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 0a1dd3c9d..b1033db54 100644 --- a/core/shared/platform/esp-idf/espidf_platform.c +++ b/core/shared/platform/esp-idf/espidf_platform.c @@ -226,7 +226,7 @@ unlinkat(int fd, const char *path, int flag) } int -utimensat(int fd, const char *path, const struct timespec *ts, int flag) +utimensat(int fd, const char *path, const struct timespec ts[2], int flag) { errno = ENOSYS; return -1; @@ -249,7 +249,7 @@ ftruncate(int fd, off_t length) #endif int -futimens(int fd, const struct timespec *times) +futimens(int fd, const struct timespec times[2]) { errno = ENOSYS; return -1; @@ -260,4 +260,4 @@ nanosleep(const struct timespec *req, struct timespec *rem) { errno = ENOSYS; return -1; -} \ No newline at end of file +} diff --git a/core/shared/platform/zephyr/platform_internal.h b/core/shared/platform/zephyr/platform_internal.h index a5d563a6c..85d4a832d 100644 --- a/core/shared/platform/zephyr/platform_internal.h +++ b/core/shared/platform/zephyr/platform_internal.h @@ -50,6 +50,10 @@ #include #endif /* end of KERNEL_VERSION_NUMBER < 0x030200 */ +#if KERNEL_VERSION_NUMBER >= 0x030300 /* version 3.3.0 */ +#include +#endif /* end of KERNEL_VERSION_NUMBER > 0x030300 */ + #ifdef CONFIG_ARM_MPU #if KERNEL_VERSION_NUMBER < 0x030200 /* version 3.2.0 */ #include @@ -96,7 +100,8 @@ void abort(void); size_t strspn(const char *s, const char *accept); size_t strcspn(const char *s, const char *reject); -/* math functions which are not provided by os */ +/* math functions which are not provided by os with minimal libc */ +#if defined(CONFIG_MINIMAL_LIBC) double atan(double x); double atan2(double y, double x); double sqrt(double x); @@ -123,6 +128,10 @@ double scalbn(double x, int n); unsigned long long int strtoull(const char *nptr, char **endptr, int base); double strtod(const char *nptr, char **endptr); float strtof(const char *nptr, char **endptr); +#else +#include +#endif /* CONFIG_MINIMAL_LIBC */ + /* clang-format on */ #if KERNEL_VERSION_NUMBER >= 0x030100 /* version 3.1.0 */ diff --git a/core/shared/platform/zephyr/shared_platform.cmake b/core/shared/platform/zephyr/shared_platform.cmake index 9b043b52f..dfd45a406 100644 --- a/core/shared/platform/zephyr/shared_platform.cmake +++ b/core/shared/platform/zephyr/shared_platform.cmake @@ -8,7 +8,9 @@ add_definitions(-DBH_PLATFORM_ZEPHYR) include_directories(${PLATFORM_SHARED_DIR}) include_directories(${PLATFORM_SHARED_DIR}/../include) -include (${CMAKE_CURRENT_LIST_DIR}/../common/math/platform_api_math.cmake) +if(${CONFIG_MINIMAL_LIBC}) + include (${CMAKE_CURRENT_LIST_DIR}/../common/math/platform_api_math.cmake) +endif() file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c) diff --git a/core/shared/platform/zephyr/zephyr_platform.c b/core/shared/platform/zephyr/zephyr_platform.c index 1a5b6621d..0fb8e2bd4 100644 --- a/core/shared/platform/zephyr/zephyr_platform.c +++ b/core/shared/platform/zephyr/zephyr_platform.c @@ -25,9 +25,11 @@ disable_mpu_rasr_xn(void) would most likely be set at index 2. */ for (index = 0U; index < 8; index++) { MPU->RNR = index; +#ifdef MPU_RASR_XN_Msk if (MPU->RASR & MPU_RASR_XN_Msk) { MPU->RASR |= ~MPU_RASR_XN_Msk; } +#endif } } #endif /* end of CONFIG_ARM_MPU */ @@ -72,18 +74,20 @@ bh_platform_destroy() void * os_malloc(unsigned size) { - return NULL; + return malloc(size); } void * os_realloc(void *ptr, unsigned size) { - return NULL; + return realloc(ptr, size); } void os_free(void *ptr) -{} +{ + free(ptr); +} int os_dumps_proc_mem_info(char *out, unsigned int size) @@ -202,10 +206,14 @@ void os_dcache_flush() { #if defined(CONFIG_CPU_CORTEX_M7) && defined(CONFIG_ARM_MPU) +#if KERNEL_VERSION_NUMBER < 0x030300 /* version 3.3.0 */ uint32 key; key = irq_lock(); SCB_CleanDCache(); irq_unlock(key); +#else + sys_cache_data_flush_all(); +#endif #elif defined(CONFIG_SOC_CVF_EM7D) && defined(CONFIG_ARC_MPU) \ && defined(CONFIG_CACHE_FLUSHING) __asm__ __volatile__("sync"); @@ -216,7 +224,11 @@ os_dcache_flush() void os_icache_flush(void *start, size_t len) -{} +{ +#if KERNEL_VERSION_NUMBER >= 0x030300 /* version 3.3.0 */ + sys_cache_instr_flush_range(start, len); +#endif +} void set_exec_mem_alloc_func(exec_mem_alloc_func_t alloc_func, diff --git a/language-bindings/go/wamr/module.go b/language-bindings/go/wamr/module.go index 13480b221..7fd131ea6 100644 --- a/language-bindings/go/wamr/module.go +++ b/language-bindings/go/wamr/module.go @@ -117,8 +117,8 @@ func (self *Module) SetWasiArgsEx(dirList [][]byte, mapDirList [][]byte, C.wasm_runtime_set_wasi_args_ex(self.module, dirPtr, dirCount, mapDirPtr, mapDirCount, envPtr, envCount, argvPtr, argc, - C.int(stdinfd), C.int(stdoutfd), - C.int(stderrfd)) + C.int64_t(stdinfd), C.int64_t(stdoutfd), + C.int64_t(stderrfd)) } /* Set module's wasi network address pool */