From 2cb2120e2e0d7dd963290691d39a07e3b00553a3 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 3 Dec 2021 01:31:25 +0800 Subject: [PATCH 01/12] Refine code when aux stack global isn't found --- core/iwasm/interpreter/wasm_loader.c | 14 +++++++++++++- core/iwasm/interpreter/wasm_mini_loader.c | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 286a7aa572..fdbefcab71 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -3040,6 +3040,18 @@ load_from_sections(WASMModule *module, WASMSection *sections, break; } } + if (!aux_stack_top_global) { + /* Auxiliary stack global isn't found, it must be unused + in the wasm app, as if it is used, the global must be + defined. Here we set it to __heap_base global and set + its size to 0. */ + aux_stack_top_global = aux_heap_base_global; + aux_stack_top = aux_heap_base; + module->aux_stack_top_global_index = + module->aux_heap_base_global_index; + module->aux_stack_bottom = aux_stack_top; + module->aux_stack_size = 0; + } break; } } @@ -3084,7 +3096,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, export->name, export->index); /* resolve retain function. - If not find, reset malloc function index */ + If not found, reset malloc function index */ export_tmp = module->exports; for (j = 0; j < module->export_count; j++, export_tmp++) { if ((export_tmp->kind == EXPORT_KIND_FUNC) diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 0565188c31..4a43b068f5 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -1959,6 +1959,18 @@ load_from_sections(WASMModule *module, WASMSection *sections, break; } } + if (!aux_stack_top_global) { + /* Auxiliary stack global isn't found, it must be unused + in the wasm app, as if it is used, the global must be + defined. Here we set it to __heap_base global and set + its size to 0. */ + aux_stack_top_global = aux_heap_base_global; + aux_stack_top = aux_heap_base; + module->aux_stack_top_global_index = + module->aux_heap_base_global_index; + module->aux_stack_bottom = aux_stack_top; + module->aux_stack_size = 0; + } break; } } @@ -2002,7 +2014,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, export->name, export->index); /* resolve retain function. - If not find, reset malloc function index */ + If not found, reset malloc function index */ export_tmp = module->exports; for (j = 0; j < module->export_count; j++, export_tmp++) { if ((export_tmp->kind == EXPORT_KIND_FUNC) From e39a9ec08ffc2a8a0f0dca1aecd1e441a9aae8a5 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 7 Dec 2021 01:58:16 +0800 Subject: [PATCH 02/12] Fix compilation error for target X86_32 --- build-scripts/config_common.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index c60725d4aa..02ae865cd7 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -62,8 +62,8 @@ if (CMAKE_SIZEOF_VOID_P EQUAL 8) endif () else () include(CheckCCompilerFlag) - Check_C_Compiler_Flag( -m32 M32_OK ) - if (M32_OK) + Check_C_Compiler_Flag(-m32 M32_OK) + if (M32_OK OR WAMR_BUILD_TARGET STREQUAL "X86_32") add_definitions (-m32) set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32") set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -m32") From 2e48be96f7c1facc77f80bc53b661020b3579bbf Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 7 Dec 2021 04:41:53 +0800 Subject: [PATCH 03/12] Implement relocation R_AARCH64_JUMP26 for aarch64 --- core/iwasm/aot/arch/aot_reloc_aarch64.c | 1 + 1 file changed, 1 insertion(+) diff --git a/core/iwasm/aot/arch/aot_reloc_aarch64.c b/core/iwasm/aot/arch/aot_reloc_aarch64.c index 2634eff4fe..4c46eaa003 100644 --- a/core/iwasm/aot/arch/aot_reloc_aarch64.c +++ b/core/iwasm/aot/arch/aot_reloc_aarch64.c @@ -143,6 +143,7 @@ apply_relocation(AOTModule *module, uint8 *target_section_addr, { switch (reloc_type) { case R_AARCH64_CALL26: + case R_AARCH64_JUMP26: { void *S, *P = (void *)(target_section_addr + reloc_offset); int64 X, A, initial_addend; From eaf7aa851f03f071b4c0555c6841e33adcbda497 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 9 Dec 2021 02:19:46 +0800 Subject: [PATCH 04/12] Exclude IoT-APP-Store-Demo from coding guideline check --- ci/coding_guidelines_check.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/coding_guidelines_check.py b/ci/coding_guidelines_check.py index 7eed447169..8e583ccb0e 100644 --- a/ci/coding_guidelines_check.py +++ b/ci/coding_guidelines_check.py @@ -29,6 +29,7 @@ "**/samples/wasm-c-api/src/*.*", "**/samples/workload/*", "**/test-tools/wasi-sdk/*", + "**/test-tools/IoT-APP-Store-Demo/*", "**/tests/wamr-test-suites/workspace/*", "**/wamr-sdk/*", ] From e1f15e520dfc113eac90bb1f3d61c70a118d6929 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 9 Dec 2021 02:46:03 +0800 Subject: [PATCH 05/12] Fix SGX run XIP error --- .../enclave-sample/Enclave/Enclave.cpp | 72 +++++++++++++++++-- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp index 1407d536cf..5a05d298e0 100644 --- a/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp +++ b/product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp @@ -119,6 +119,71 @@ handle_cmd_destroy_runtime() LOG_VERBOSE("Destroy runtime success.\n"); } +static uint8 * +align_ptr(const uint8 *p, uint32 b) +{ + uintptr_t v = (uintptr_t)p; + uintptr_t m = b - 1; + return (uint8 *)((v + m) & ~m); +} + +#define AOT_SECTION_TYPE_TARGET_INFO 0 +#define AOT_SECTION_TYPE_SIGANATURE 6 +#define E_TYPE_XIP 4 + +#define CHECK_BUF(buf, buf_end, length) \ + do { \ + if (buf + length < buf || buf + length > buf_end) \ + return false; \ + } while (0) + +#define read_uint16(p, p_end, res) \ + do { \ + p = (uint8 *)align_ptr(p, sizeof(uint16)); \ + CHECK_BUF(p, p_end, sizeof(uint16)); \ + res = *(uint16 *)p; \ + p += sizeof(uint16); \ + } while (0) + +#define read_uint32(p, p_end, res) \ + do { \ + p = (uint8 *)align_ptr(p, sizeof(uint32)); \ + CHECK_BUF(p, p_end, sizeof(uint32)); \ + res = *(uint32 *)p; \ + p += sizeof(uint32); \ + } while (0) + +static bool +is_xip_file(const uint8 *buf, uint32 size) +{ + const uint8 *p = buf, *p_end = buf + size; + uint32 section_type, section_size; + uint16 e_type; + + if (get_package_type(buf, size) != Wasm_Module_AoT) + return false; + CHECK_BUF(p, p_end, 8); + p += 8; + while (p < p_end) { + read_uint32(p, p_end, section_type); + read_uint32(p, p_end, section_size); + CHECK_BUF(p, p_end, section_size); + + if (section_type == AOT_SECTION_TYPE_TARGET_INFO) { + p += 4; + read_uint16(p, p_end, e_type); + if (e_type == E_TYPE_XIP) { + return true; + } + } + else if (section_type >= AOT_SECTION_TYPE_SIGANATURE) { + return false; + } + p += section_size; + } + return false; +} + static void handle_cmd_load_module(uint64 *args, uint32 argc) { @@ -129,15 +194,10 @@ handle_cmd_load_module(uint64 *args, uint32 argc) uint32 error_buf_size = *(uint32 *)args++; uint64 total_size = sizeof(EnclaveModule) + (uint64)wasm_file_size; EnclaveModule *enclave_module; - bool is_xip_file = false; bh_assert(argc == 4); -#if WASM_ENABLE_AOT != 0 - is_xip_file = wasm_runtime_is_xip_file((uint8 *)wasm_file, wasm_file_size); -#endif - - if (!is_xip_file) { + if (!is_xip_file((uint8 *)wasm_file, wasm_file_size)) { if (total_size >= UINT32_MAX || !(enclave_module = (EnclaveModule *)wasm_runtime_malloc( (uint32)total_size))) { From 9cc7e279844aa5eb7f74f336df2bd3246c8fa361 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 10 Dec 2021 02:08:48 +0800 Subject: [PATCH 06/12] Refine some codes and fix several issues --- core/iwasm/aot/aot_runtime.c | 12 +- core/iwasm/common/wasm_shared_memory.c | 4 +- core/iwasm/compilation/aot_compiler.h | 14 +- core/iwasm/compilation/aot_emit_function.c | 2 + core/iwasm/include/wasm_export.h | 5 +- core/iwasm/interpreter/wasm.h | 20 +-- core/iwasm/interpreter/wasm_interp_classic.c | 73 +++++---- core/iwasm/interpreter/wasm_interp_fast.c | 26 +-- core/iwasm/interpreter/wasm_loader.c | 133 +++++++--------- core/iwasm/interpreter/wasm_mini_loader.c | 159 +++++++++---------- product-mini/platforms/linux/CMakeLists.txt | 2 +- 11 files changed, 219 insertions(+), 231 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index c769a527e1..0d9dd49ea5 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -216,8 +216,6 @@ table_instantiate(AOTModuleInstance *module_inst, AOTModule *module, /* fill table with element segment content */ for (i = 0; i < module->table_init_data_count; i++) { - AOTTableInstance *tbl_inst; - table_seg = module->table_init_data_list[i]; #if WASM_ENABLE_REF_TYPES != 0 @@ -1404,6 +1402,16 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0; bool ret; + if (argc < func_type->param_cell_num) { + char buf[128]; + snprintf(buf, sizeof(buf), + "invalid argument count %u, must be no smaller than %u", argc, + func_type->param_cell_num); + aot_set_exception(module_inst, buf); + return false; + } + argc = func_type->param_cell_num; + /* set thread handle and stack boundary */ wasm_exec_env_set_thread_info(exec_env); diff --git a/core/iwasm/common/wasm_shared_memory.c b/core/iwasm/common/wasm_shared_memory.c index 6fc9bf2073..8ea794f935 100644 --- a/core/iwasm/common/wasm_shared_memory.c +++ b/core/iwasm/common/wasm_shared_memory.c @@ -283,10 +283,10 @@ destroy_wait_info(void *wait_info) } static void -release_wait_info(HashMap *wait_map, AtomicWaitInfo *wait_info, void *address) +release_wait_info(HashMap *wait_map_, AtomicWaitInfo *wait_info, void *address) { if (wait_info->wait_list->len == 0) { - bh_hash_map_remove(wait_map, address, NULL, NULL); + bh_hash_map_remove(wait_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 549a0e304d..7177de2979 100644 --- a/core/iwasm/compilation/aot_compiler.h +++ b/core/iwasm/compilation/aot_compiler.h @@ -311,35 +311,35 @@ check_type_compatible(uint8 src_type, uint8 dst_type) if (!(func_type = \ LLVMFunctionType(ret_type, param_types, argc, false))) { \ aot_set_last_error("llvm add function type failed."); \ - return false; \ + goto fail; \ } \ if (comp_ctx->is_jit_mode) { \ /* JIT mode, call the function directly */ \ if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \ aot_set_last_error("llvm add pointer type failed."); \ - return false; \ + goto fail; \ } \ if (!(value = I64_CONST((uint64)(uintptr_t)name)) \ || !(func = LLVMConstIntToPtr(value, func_ptr_type))) { \ aot_set_last_error("create LLVM value failed."); \ - return false; \ + goto fail; \ } \ } \ else if (comp_ctx->is_indirect_mode) { \ int32 func_index; \ if (!(func_ptr_type = LLVMPointerType(func_type, 0))) { \ aot_set_last_error("create LLVM function type failed."); \ - return false; \ + goto fail; \ } \ \ func_index = aot_get_native_symbol_index(comp_ctx, #name); \ if (func_index < 0) { \ - return false; \ + goto fail; \ } \ if (!(func = aot_get_func_from_table( \ comp_ctx, func_ctx->native_symbol, func_ptr_type, \ func_index))) { \ - return false; \ + goto fail; \ } \ } \ else { \ @@ -349,7 +349,7 @@ check_type_compatible(uint8 src_type, uint8 dst_type) && !(func = LLVMAddFunction(comp_ctx->module, func_name, \ func_type))) { \ aot_set_last_error("llvm add function failed."); \ - return false; \ + goto fail; \ } \ } \ } while (0) diff --git a/core/iwasm/compilation/aot_emit_function.c b/core/iwasm/compilation/aot_emit_function.c index c53dc933e2..be7dea91c7 100644 --- a/core/iwasm/compilation/aot_emit_function.c +++ b/core/iwasm/compilation/aot_emit_function.c @@ -345,6 +345,8 @@ call_aot_free_frame_func(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx) } return true; +fail: + return false; } #endif /* end of (WASM_ENABLE_DUMP_CALL_STACK != 0) \ || (WASM_ENABLE_PERF_PROFILING != 0) */ diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 8ad85a3d2b..6b66953407 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -477,7 +477,10 @@ wasm_runtime_get_module_inst(wasm_exec_env_t exec_env); * @param exec_env the execution environment to call the function, * which must be created from wasm_create_exec_env() * @param function the function to call - * @param argc the number of arguments + * @param argc total cell number that the function parameters occupy, + * a cell is a slot of the uint32 array argv[], e.g. i32/f32 argument + * occupies one cell, i64/f64 argument occupies two cells, note that + * it might be different from the parameter number of the function * @param argv the arguments. If the function has return value, * the first (or first two in case 64-bit return value) element of * argv stores the return value of the called WASM function after this diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index bda5b3467d..63da4a061c 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -529,25 +529,7 @@ wasm_value_type_size(uint8 value_type) inline static uint16 wasm_value_type_cell_num(uint8 value_type) { - if (value_type == VALUE_TYPE_VOID) - return 0; - else if (value_type == VALUE_TYPE_I32 || value_type == VALUE_TYPE_F32 -#if WASM_ENABLE_REF_TYPES != 0 - || value_type == VALUE_TYPE_FUNCREF - || value_type == VALUE_TYPE_EXTERNREF -#endif - ) - return 1; - else if (value_type == VALUE_TYPE_I64 || value_type == VALUE_TYPE_F64) - return 2; -#if WASM_ENABLE_SIMD != 0 - else if (value_type == VALUE_TYPE_V128) - return 4; -#endif - else { - bh_assert(0); - } - return 0; + return wasm_value_type_size(value_type) / 4; } inline static uint32 diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 347da3fe0e..fe85f37976 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -256,18 +256,19 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign) --frame_csp; \ } while (0) -#define POP_CSP_N(n) \ - do { \ - uint32 *frame_sp_old = frame_sp; \ - uint32 cell_num = 0; \ - POP_CSP_CHECK_OVERFLOW(n + 1); \ - frame_csp -= n; \ - frame_ip = (frame_csp - 1)->target_addr; \ - /* copy arity values of block */ \ - frame_sp = (frame_csp - 1)->frame_sp; \ - cell_num = (frame_csp - 1)->cell_num; \ - word_copy(frame_sp, frame_sp_old - cell_num, cell_num); \ - frame_sp += cell_num; \ +#define POP_CSP_N(n) \ + do { \ + uint32 *frame_sp_old = frame_sp; \ + uint32 cell_num_to_copy; \ + POP_CSP_CHECK_OVERFLOW(n + 1); \ + frame_csp -= n; \ + frame_ip = (frame_csp - 1)->target_addr; \ + /* copy arity values of block */ \ + frame_sp = (frame_csp - 1)->frame_sp; \ + cell_num_to_copy = (frame_csp - 1)->cell_num; \ + word_copy(frame_sp, frame_sp_old - cell_num_to_copy, \ + cell_num_to_copy); \ + frame_sp += cell_num_to_copy; \ } while (0) /* Pop the given number of elements from the given frame's stack. */ @@ -367,11 +368,11 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign) PUSH_##src_op_type(cval); \ } while (0) -#define DEF_OP_EQZ(src_op_type) \ - do { \ - int32 val; \ - val = POP_##src_op_type() == 0; \ - PUSH_I32(val); \ +#define DEF_OP_EQZ(src_op_type) \ + do { \ + int32 pop_val; \ + pop_val = POP_##src_op_type() == 0; \ + PUSH_I32(pop_val); \ } while (0) #define DEF_OP_CMP(src_type, src_op_type, cond) \ @@ -434,9 +435,9 @@ read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign) #define DEF_OP_MATH(src_type, src_op_type, method) \ do { \ - src_type val; \ - val = POP_##src_op_type(); \ - PUSH_##src_op_type(method(val)); \ + src_type src_val; \ + src_val = POP_##src_op_type(); \ + PUSH_##src_op_type(method(src_val)); \ } while (0) #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \ @@ -1384,7 +1385,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP(WASM_OP_TABLE_SET) { - uint32 tbl_idx, elem_idx, val; + uint32 tbl_idx, elem_idx, elem_val; WASMTableInstance *tbl_inst; read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); @@ -1392,14 +1393,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, tbl_inst = wasm_get_table_inst(module, tbl_idx); - val = POP_I32(); + elem_val = POP_I32(); elem_idx = POP_I32(); if (elem_idx >= tbl_inst->cur_size) { wasm_set_exception(module, "out of bounds table access"); goto got_exception; } - ((uint32 *)(tbl_inst->base_addr))[elem_idx] = val; + ((uint32 *)(tbl_inst->base_addr))[elem_idx] = elem_val; HANDLE_OP_END(); } @@ -1414,9 +1415,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP(WASM_OP_REF_IS_NULL) { - uint32 val; - val = POP_I32(); - PUSH_I32(val == NULL_REF ? 1 : 0); + uint32 ref_val; + ref_val = POP_I32(); + PUSH_I32(ref_val == NULL_REF ? 1 : 0); HANDLE_OP_END(); } @@ -2955,16 +2956,16 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, case WASM_OP_MEMORY_FILL: { uint32 dst, len; - uint8 val, *mdst; + uint8 fill_val, *mdst; frame_ip++; len = POP_I32(); - val = POP_I32(); + fill_val = POP_I32(); dst = POP_I32(); CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); - memset(mdst, val, len); + memset(mdst, fill_val, len); break; } @@ -3119,7 +3120,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, } case WASM_OP_TABLE_FILL: { - uint32 tbl_idx, n, val, i; + uint32 tbl_idx, n, fill_val; WASMTableInstance *tbl_inst; read_leb_uint32(frame_ip, frame_ip_end, tbl_idx); @@ -3128,7 +3129,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, tbl_inst = wasm_get_table_inst(module, tbl_idx); n = POP_I32(); - val = POP_I32(); + fill_val = POP_I32(); i = POP_I32(); /* TODO: what if the element is not passive? */ @@ -3142,7 +3143,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, } for (; n != 0; i++, n--) { - ((uint32 *)(tbl_inst->base_addr))[i] = val; + ((uint32 *)(tbl_inst->base_addr))[i] = fill_val; } break; @@ -3708,13 +3709,15 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env, frame here. */ unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num); - if (argc != function->param_cell_num) { + if (argc < function->param_cell_num) { char buf[128]; - snprintf(buf, sizeof(buf), "invalid argument count %d, expected %d", - argc, function->param_cell_num); + snprintf(buf, sizeof(buf), + "invalid argument count %u, must be no smaller than %u", argc, + function->param_cell_num); wasm_set_exception(module_inst, buf); return; } + argc = function->param_cell_num; if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) { wasm_set_exception((WASMModuleInstance *)exec_env->module_inst, diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index ad2c6a69ec..a3f4f9cb61 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -1346,7 +1346,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP(WASM_OP_TABLE_SET) { - uint32 tbl_idx, elem_idx, val; + uint32 tbl_idx, elem_idx, elem_val; WASMTableInstance *tbl_inst; tbl_idx = read_uint32(frame_ip); @@ -1354,14 +1354,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, tbl_inst = wasm_get_table_inst(module, tbl_idx); - val = POP_I32(); + elem_val = POP_I32(); elem_idx = POP_I32(); if (elem_idx >= tbl_inst->cur_size) { wasm_set_exception(module, "out of bounds table access"); goto got_exception; } - ((uint32 *)tbl_inst->base_addr)[elem_idx] = val; + ((uint32 *)tbl_inst->base_addr)[elem_idx] = elem_val; HANDLE_OP_END(); } @@ -1373,9 +1373,9 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, HANDLE_OP(WASM_OP_REF_IS_NULL) { - uint32 val; - val = POP_I32(); - PUSH_I32(val == NULL_REF ? 1 : 0); + uint32 ref_val; + ref_val = POP_I32(); + PUSH_I32(ref_val == NULL_REF ? 1 : 0); HANDLE_OP_END(); } @@ -3045,7 +3045,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, } case WASM_OP_TABLE_FILL: { - uint32 tbl_idx, n, val, i; + uint32 tbl_idx, n, fill_val, i; WASMTableInstance *tbl_inst; tbl_idx = read_uint32(frame_ip); @@ -3054,7 +3054,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, tbl_inst = wasm_get_table_inst(module, tbl_idx); n = POP_I32(); - val = POP_I32(); + fill_val = POP_I32(); i = POP_I32(); if (i + n > tbl_inst->cur_size) { @@ -3064,7 +3064,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, } for (; n != 0; i++, n--) { - ((uint32 *)(tbl_inst->base_addr))[i] = val; + ((uint32 *)(tbl_inst->base_addr))[i] = fill_val; } break; @@ -3729,13 +3729,15 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env, frame here. */ unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num); - if (argc != function->param_cell_num) { + if (argc < function->param_cell_num) { char buf[128]; - snprintf(buf, sizeof(buf), "invalid argument count %d, expected %d", - argc, function->param_cell_num); + snprintf(buf, sizeof(buf), + "invalid argument count %u, must be no smaller than %u", argc, + function->param_cell_num); wasm_set_exception(module_inst, buf); return; } + argc = function->param_cell_num; if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) { wasm_set_exception((WASMModuleInstance *)exec_env->module_inst, diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index e3f0649ec5..0d99babdfb 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -587,6 +587,13 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, CHECK_BUF(p, p_end, 1); type->types[param_count + j] = read_uint8(p); } + for (j = 0; j < param_count + result_count; j++) { + if (!is_value_type(type->types[j])) { + set_error_buf(error_buf, error_buf_size, + "unknown value type"); + return false; + } + } param_cell_num = wasm_get_cell_num(type->types, param_count); ret_cell_num = @@ -891,7 +898,7 @@ register_sub_module(const WASMModule *parent_module, return true; } - node = wasm_runtime_malloc(sizeof(WASMRegisteredModule)); + node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0); if (!node) { return false; } @@ -1904,17 +1911,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, CHECK_BUF(p_code, buf_code_end, 1); /* 0x7F/0x7E/0x7D/0x7C */ type = read_uint8(p_code); - if ((type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) -#if WASM_ENABLE_SIMD != 0 -#if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) - && type != VALUE_TYPE_V128 -#endif -#endif -#if WASM_ENABLE_REF_TYPES != 0 - && type != VALUE_TYPE_FUNCREF - && type != VALUE_TYPE_EXTERNREF -#endif - ) { + if (!is_value_type(type)) { if (type == VALUE_TYPE_V128) set_error_buf(error_buf, error_buf_size, "v128 value type requires simd feature"); @@ -3250,7 +3247,7 @@ static void record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op) { WASMFastOPCodeNode *fast_op = - wasm_runtime_malloc(sizeof(WASMFastOPCodeNode)); + loader_malloc(sizeof(WASMFastOPCodeNode), NULL, 0); if (fast_op) { fast_op->offset = pos - module->load_addr; fast_op->orig_op = orig_op; @@ -4449,40 +4446,34 @@ static WASMLoaderContext * wasm_loader_ctx_init(WASMFunction *func) { WASMLoaderContext *loader_ctx = - wasm_runtime_malloc(sizeof(WASMLoaderContext)); + loader_malloc(sizeof(WASMLoaderContext), NULL, 0); if (!loader_ctx) return NULL; - memset(loader_ctx, 0, sizeof(WASMLoaderContext)); loader_ctx->frame_ref_size = 32; if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = - wasm_runtime_malloc(loader_ctx->frame_ref_size))) + loader_malloc(loader_ctx->frame_ref_size, NULL, 0))) goto fail; - memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size); - loader_ctx->frame_ref_boundary = - loader_ctx->frame_ref_bottom + loader_ctx->frame_ref_size; + loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32; loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8; if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = - wasm_runtime_malloc(loader_ctx->frame_csp_size))) + loader_malloc(loader_ctx->frame_csp_size, NULL, 0))) goto fail; - memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size); loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8; #if WASM_ENABLE_FAST_INTERP != 0 loader_ctx->frame_offset_size = sizeof(int16) * 32; if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset = - wasm_runtime_malloc(loader_ctx->frame_offset_size))) + loader_malloc(loader_ctx->frame_offset_size, NULL, 0))) goto fail; - memset(loader_ctx->frame_offset_bottom, 0, loader_ctx->frame_offset_size); loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32; loader_ctx->num_const = 0; loader_ctx->const_buf_size = sizeof(Const) * 8; if (!(loader_ctx->const_buf = - wasm_runtime_malloc(loader_ctx->const_buf_size))) + loader_malloc(loader_ctx->const_buf_size, NULL, 0))) goto fail; - memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size); loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset = loader_ctx->max_dynamic_offset = @@ -4766,9 +4757,9 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf, static bool wasm_loader_ctx_reinit(WASMLoaderContext *ctx) { - if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size))) + if (!(ctx->p_code_compiled = + loader_malloc(ctx->code_compiled_size, NULL, 0))) return false; - memset(ctx->p_code_compiled, 0, ctx->code_compiled_size); ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size; /* clean up frame ref */ @@ -5479,7 +5470,8 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */ if (return_count == 1) { uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]); - if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) { + if (cell <= 2 /* V128 isn't supported whose cell num is 4 */ + && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) { /* insert op_copy before else opcode */ if (opcode == WASM_OP_ELSE) skip_label(); @@ -6121,10 +6113,10 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, #endif /* set current block's stack polymorphic state */ -#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \ - do { \ - BranchBlock *cur_block = loader_ctx->frame_csp - 1; \ - cur_block->is_stack_polymorphic = flag; \ +#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \ + do { \ + BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \ + _cur_block->is_stack_polymorphic = flag; \ } while (0) #define BLOCK_HAS_PARAM(block_type) \ @@ -6184,12 +6176,12 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org; uint32 param_count, local_count, global_count; uint8 *param_types, *local_types, local_type, global_type; - BlockType func_type; + BlockType func_block_type; uint16 *local_offsets, local_offset; uint32 type_idx, func_idx, local_idx, global_idx, table_idx; - uint32 table_seg_idx, data_seg_idx, count, i, align, mem_offset; + uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i; int32 i32_const = 0; - int64 i64; + int64 i64_const; uint8 opcode; bool return_value = false; WASMLoaderContext *loader_ctx; @@ -6199,8 +6191,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, int16 operand_offset = 0; uint8 last_op = 0; bool disable_emit, preserve_local = false; - float32 f32; - float64 f64; + float32 f32_const; + float64 f64_const; LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n", func->param_cell_num, func->local_cell_num, func->ret_cell_num); @@ -6211,8 +6203,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, param_count = func->func_type->param_count; param_types = func->func_type->types; - func_type.is_value_type = false; - func_type.u.type = func->func_type; + func_block_type.is_value_type = false; + func_block_type.u.type = func->func_type; local_count = func->local_count; local_types = func->local_types; @@ -6236,7 +6228,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, } #endif - PUSH_CSP(LABEL_TYPE_FUNCTION, func_type, p); + PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p); while (p < p_end) { opcode = *p++; @@ -6461,24 +6453,26 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, * fail */ if (cur_block->label_type == LABEL_TYPE_IF && !cur_block->else_addr) { - uint32 param_count = 0, ret_count = 0; - uint8 *param_types = NULL, *ret_types = NULL; - BlockType *block_type = &cur_block->block_type; - if (block_type->is_value_type) { - if (block_type->u.value_type != VALUE_TYPE_VOID) { - ret_count = 1; - ret_types = &block_type->u.value_type; + uint32 block_param_count = 0, block_ret_count = 0; + uint8 *block_param_types = NULL, *block_ret_types = NULL; + BlockType *cur_block_type = &cur_block->block_type; + if (cur_block_type->is_value_type) { + if (cur_block_type->u.value_type != VALUE_TYPE_VOID) { + block_ret_count = 1; + block_ret_types = &cur_block_type->u.value_type; } } else { - param_count = block_type->u.type->param_count; - ret_count = block_type->u.type->result_count; - param_types = block_type->u.type->types; - ret_types = block_type->u.type->types + param_count; + block_param_count = cur_block_type->u.type->param_count; + block_ret_count = cur_block_type->u.type->result_count; + block_param_types = cur_block_type->u.type->types; + block_ret_types = + cur_block_type->u.type->types + block_param_count; } - if (param_count != ret_count - || (param_count - && memcmp(param_types, ret_types, param_count))) { + if (block_param_count != block_ret_count + || (block_param_count + && memcmp(block_param_types, block_ret_types, + block_param_count))) { set_error_buf(error_buf, error_buf_size, "type mismatch: else branch missing"); goto fail; @@ -6810,13 +6804,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, } if (available_stack_cell > 0) { - if (*(loader_ctx->frame_ref - 1) == REF_I32 - || *(loader_ctx->frame_ref - 1) == REF_F32 -#if WASM_ENABLE_REF_TYPES != 0 - || *(loader_ctx->frame_ref - 1) == REF_FUNCREF - || *(loader_ctx->frame_ref - 1) == REF_EXTERNREF -#endif - ) { + if (is_32bit_type(*(loader_ctx->frame_ref - 1))) { loader_ctx->frame_ref--; loader_ctx->stack_cell_num--; #if WASM_ENABLE_FAST_INTERP != 0 @@ -6827,8 +6815,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, loader_ctx->dynamic_offset--; #endif } - else if (*(loader_ctx->frame_ref - 1) == REF_I64_1 - || *(loader_ctx->frame_ref - 1) == REF_F64_1) { + else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) { loader_ctx->frame_ref -= 2; loader_ctx->stack_cell_num -= 2; #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0) @@ -7604,11 +7591,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, break; case WASM_OP_I64_CONST: - read_leb_int64(p, p_end, i64); + read_leb_int64(p, p_end, i64_const); #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); disable_emit = true; - GET_CONST_OFFSET(VALUE_TYPE_I64, i64); + GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const); #endif PUSH_I64(); break; @@ -7618,9 +7605,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); disable_emit = true; - bh_memcpy_s((uint8 *)&f32, sizeof(float32), p_org, + bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org, sizeof(float32)); - GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32); + GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const); #endif PUSH_F32(); break; @@ -7631,9 +7618,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, skip_label(); disable_emit = true; /* Some MCU may require 8-byte align */ - bh_memcpy_s((uint8 *)&f64, sizeof(float64), p_org, + bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org, sizeof(float64)); - GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64); + GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const); #endif PUSH_F64(); break; @@ -8932,14 +8919,16 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, func->const_cell_num = loader_ctx->const_cell_num; if (func->const_cell_num > 0) { + int32 j; + if (!(func->consts = func_const = loader_malloc( func->const_cell_num * 4, error_buf, error_buf_size))) goto fail; func_const_end = func->consts + func->const_cell_num * 4; /* reverse the const buf */ - for (int i = loader_ctx->num_const - 1; i >= 0; i--) { - Const *c = (Const *)(loader_ctx->const_buf + i * sizeof(Const)); + for (j = loader_ctx->num_const - 1; j >= 0; j--) { + Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const)); if (c->value_type == VALUE_TYPE_F64 || c->value_type == VALUE_TYPE_I64) { bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), @@ -8968,7 +8957,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, (void)table_idx; (void)table_seg_idx; (void)data_seg_idx; - (void)i64; + (void)i64_const; (void)local_offset; (void)p_org; (void)mem_offset; diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 4a43b068f5..51dde8fd2d 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -359,6 +359,9 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, CHECK_BUF(p, p_end, 1); type->types[param_count + j] = read_uint8(p); } + for (j = 0; j < param_count + result_count; j++) { + bh_assert(is_value_type(type->types[j])); + } param_cell_num = wasm_get_cell_num(type->types, param_count); ret_cell_num = @@ -971,12 +974,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, CHECK_BUF(p_code, buf_code_end, 1); /* 0x7F/0x7E/0x7D/0x7C */ type = read_uint8(p_code); - bh_assert((type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32) -#if WASM_ENABLE_REF_TYPES != 0 - || type == VALUE_TYPE_FUNCREF - || type == VALUE_TYPE_EXTERNREF -#endif - ); + bh_assert(is_value_type(type)); for (k = 0; k < sub_local_count; k++) { func->local_types[local_type_index++] = type; } @@ -3146,40 +3144,34 @@ static WASMLoaderContext * wasm_loader_ctx_init(WASMFunction *func) { WASMLoaderContext *loader_ctx = - wasm_runtime_malloc(sizeof(WASMLoaderContext)); + loader_malloc(sizeof(WASMLoaderContext), NULL, 0); if (!loader_ctx) return false; - memset(loader_ctx, 0, sizeof(WASMLoaderContext)); loader_ctx->frame_ref_size = 32; if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = - wasm_runtime_malloc(loader_ctx->frame_ref_size))) + loader_malloc(loader_ctx->frame_ref_size, NULL, 0))) goto fail; - memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size); - loader_ctx->frame_ref_boundary = - loader_ctx->frame_ref_bottom + loader_ctx->frame_ref_size; + loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32; loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8; if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = - wasm_runtime_malloc(loader_ctx->frame_csp_size))) + loader_malloc(loader_ctx->frame_csp_size, NULL, 0))) goto fail; - memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size); loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8; #if WASM_ENABLE_FAST_INTERP != 0 loader_ctx->frame_offset_size = sizeof(int16) * 32; if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset = - wasm_runtime_malloc(loader_ctx->frame_offset_size))) + loader_malloc(loader_ctx->frame_offset_size, NULL, 0))) goto fail; - memset(loader_ctx->frame_offset_bottom, 0, loader_ctx->frame_offset_size); loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32; loader_ctx->num_const = 0; loader_ctx->const_buf_size = sizeof(Const) * 8; if (!(loader_ctx->const_buf = - wasm_runtime_malloc(loader_ctx->const_buf_size))) + loader_malloc(loader_ctx->const_buf_size, NULL, 0))) goto fail; - memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size); loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset = loader_ctx->max_dynamic_offset = @@ -3436,9 +3428,9 @@ wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf, static bool wasm_loader_ctx_reinit(WASMLoaderContext *ctx) { - if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size))) + if (!(ctx->p_code_compiled = + loader_malloc(ctx->code_compiled_size, NULL, 0))) return false; - memset(ctx->p_code_compiled, 0, ctx->code_compiled_size); ctx->p_code_compiled_end = ctx->p_code_compiled + ctx->code_compiled_size; /* clean up frame ref */ @@ -4240,7 +4232,8 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */ if (return_count == 1) { uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]); - if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) { + if (cell <= 2 /* V128 isn't supported whose cell num is 4 */ + && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) { /* insert op_copy before else opcode */ if (opcode == WASM_OP_ELSE) skip_label(); @@ -4715,10 +4708,10 @@ copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block, #endif /* set current block's stack polymorphic state */ -#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \ - do { \ - BranchBlock *cur_block = loader_ctx->frame_csp - 1; \ - cur_block->is_stack_polymorphic = flag; \ +#define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \ + do { \ + BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \ + _cur_block->is_stack_polymorphic = flag; \ } while (0) #define BLOCK_HAS_PARAM(block_type) \ @@ -4740,11 +4733,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org; uint32 param_count, local_count, global_count; uint8 *param_types, *local_types, local_type, global_type; - BlockType func_type; + BlockType func_block_type; uint16 *local_offsets, local_offset; - uint32 count, i, local_idx, global_idx, u32, align, mem_offset; + uint32 count, local_idx, global_idx, u32, align, mem_offset, i; int32 i32, i32_const = 0; - int64 i64; + int64 i64_const; uint8 opcode, u8; bool return_value = false; WASMLoaderContext *loader_ctx; @@ -4757,8 +4750,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, int16 operand_offset = 0; uint8 last_op = 0; bool disable_emit, preserve_local = false; - float32 f32; - float64 f64; + float32 f32_const; + float64 f64_const; LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n", func->param_cell_num, func->local_cell_num, func->ret_cell_num); @@ -4769,8 +4762,8 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, param_count = func->func_type->param_count; param_types = func->func_type->types; - func_type.is_value_type = false; - func_type.u.type = func->func_type; + func_block_type.is_value_type = false; + func_block_type.u.type = func->func_type; local_count = func->local_count; local_types = func->local_types; @@ -4794,7 +4787,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, } #endif - PUSH_CSP(LABEL_TYPE_FUNCTION, func_type, p); + PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p); while (p < p_end) { opcode = *p++; @@ -5006,28 +4999,29 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, * fail */ if (cur_block->label_type == LABEL_TYPE_IF && !cur_block->else_addr) { - uint32 param_count = 0, ret_count = 0; - uint8 *param_types = NULL, *ret_types = NULL; - BlockType *block_type = &cur_block->block_type; - if (block_type->is_value_type) { - if (block_type->u.value_type != VALUE_TYPE_VOID) { - ret_count = 1; - ret_types = &block_type->u.value_type; + uint32 block_param_count = 0, block_ret_count = 0; + uint8 *block_param_types = NULL, *block_ret_types = NULL; + BlockType *cur_block_type = &cur_block->block_type; + if (cur_block_type->is_value_type) { + if (cur_block_type->u.value_type != VALUE_TYPE_VOID) { + block_ret_count = 1; + block_ret_types = &cur_block_type->u.value_type; } } else { - param_count = block_type->u.type->param_count; - ret_count = block_type->u.type->result_count; - param_types = block_type->u.type->types; - ret_types = block_type->u.type->types + param_count; + block_param_count = cur_block_type->u.type->param_count; + block_ret_count = cur_block_type->u.type->result_count; + block_param_types = cur_block_type->u.type->types; + block_ret_types = + cur_block_type->u.type->types + block_param_count; } - bh_assert( - param_count == ret_count - && (!param_count - || !memcmp(param_types, ret_types, param_count))); - (void)ret_types; - (void)ret_count; - (void)param_types; + bh_assert(block_param_count == block_ret_count + && (!block_param_count + || !memcmp(block_param_types, block_ret_types, + block_param_count))); + (void)block_ret_types; + (void)block_ret_count; + (void)block_param_types; } POP_CSP(); @@ -5291,8 +5285,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, && !cur_block->is_stack_polymorphic)); if (available_stack_cell > 0) { - if (*(loader_ctx->frame_ref - 1) == REF_I32 - || *(loader_ctx->frame_ref - 1) == REF_F32) { + if (is_32bit_type(*(loader_ctx->frame_ref - 1))) { loader_ctx->frame_ref--; loader_ctx->stack_cell_num--; #if WASM_ENABLE_FAST_INTERP != 0 @@ -5303,7 +5296,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, loader_ctx->dynamic_offset--; #endif } - else { + else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) { loader_ctx->frame_ref -= 2; loader_ctx->stack_cell_num -= 2; #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0) @@ -5317,6 +5310,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, loader_ctx->dynamic_offset -= 2; #endif } + else { + bh_assert(0); + } } else { #if WASM_ENABLE_FAST_INTERP != 0 @@ -5969,11 +5965,11 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, break; case WASM_OP_I64_CONST: - read_leb_int64(p, p_end, i64); + read_leb_int64(p, p_end, i64_const); #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); disable_emit = true; - GET_CONST_OFFSET(VALUE_TYPE_I64, i64); + GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const); #endif PUSH_I64(); break; @@ -5983,9 +5979,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, #if WASM_ENABLE_FAST_INTERP != 0 skip_label(); disable_emit = true; - bh_memcpy_s((uint8 *)&f32, sizeof(float32), p_org, + bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org, sizeof(float32)); - GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32); + GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const); #endif PUSH_F32(); break; @@ -5996,9 +5992,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, skip_label(); disable_emit = true; /* Some MCU may require 8-byte align */ - bh_memcpy_s((uint8 *)&f64, sizeof(float64), p_org, + bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org, sizeof(float64)); - GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64); + GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const); #endif PUSH_F64(); break; @@ -6605,25 +6601,28 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, goto re_scan; func->const_cell_num = loader_ctx->const_cell_num; - if (func->const_cell_num > 0 - && !(func->consts = func_const = loader_malloc( - func->const_cell_num * 4, error_buf, error_buf_size))) { - goto fail; - } - func_const_end = func->consts + func->const_cell_num * 4; - /* reverse the const buf */ - for (int i = loader_ctx->num_const - 1; i >= 0; i--) { - Const *c = (Const *)(loader_ctx->const_buf + i * sizeof(Const)); - if (c->value_type == VALUE_TYPE_F64 - || c->value_type == VALUE_TYPE_I64) { - bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), - &(c->value.f64), (uint32)sizeof(int64)); - func_const += sizeof(int64); - } - else { - bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), - &(c->value.f32), (uint32)sizeof(int32)); - func_const += sizeof(int32); + if (func->const_cell_num > 0) { + int32 j; + + if (!(func->consts = func_const = loader_malloc( + func->const_cell_num * 4, error_buf, error_buf_size))) + goto fail; + + func_const_end = func->consts + func->const_cell_num * 4; + /* reverse the const buf */ + for (j = loader_ctx->num_const - 1; j >= 0; j--) { + Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const)); + if (c->value_type == VALUE_TYPE_F64 + || c->value_type == VALUE_TYPE_I64) { + bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), + &(c->value.f64), (uint32)sizeof(int64)); + func_const += sizeof(int64); + } + else { + bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), + &(c->value.f32), (uint32)sizeof(int32)); + func_const += sizeof(int32); + } } } @@ -6641,7 +6640,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, (void)u8; (void)u32; (void)i32; - (void)i64; + (void)i64_const; (void)global_count; (void)local_count; (void)local_offset; diff --git a/product-mini/platforms/linux/CMakeLists.txt b/product-mini/platforms/linux/CMakeLists.txt index 7692d6107e..6b739753b3 100644 --- a/product-mini/platforms/linux/CMakeLists.txt +++ b/product-mini/platforms/linux/CMakeLists.txt @@ -112,7 +112,7 @@ add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE") -set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security") +set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow") # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion") if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") From 7707a6ada576f9e61330a2b818fd3f94053640c9 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 10 Dec 2021 04:04:39 +0800 Subject: [PATCH 07/12] Fix some shadow warnings --- core/iwasm/aot/aot_loader.c | 8 ++-- core/iwasm/interpreter/wasm_interp_classic.c | 8 ++-- core/iwasm/interpreter/wasm_interp_fast.c | 14 +++---- core/iwasm/interpreter/wasm_mini_loader.c | 40 ++++++++++---------- core/iwasm/libraries/debug-engine/handler.c | 6 +-- 5 files changed, 39 insertions(+), 37 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index a786a50323..b8c1a3cc4c 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -1454,14 +1454,14 @@ load_text_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module, /* Now code points to an ELF object, we pull it down to .text section */ uint64 offset; uint64 size; - char *buf = module->code; - module->elf_hdr = buf; - if (!get_text_section(buf, &offset, &size)) { + char *code_buf = module->code; + module->elf_hdr = code_buf; + if (!get_text_section(code_buf, &offset, &size)) { set_error_buf(error_buf, error_buf_size, "get text section of ELF failed"); return false; } - module->code = buf + offset; + module->code = code_buf + offset; module->code_size -= (uint32)offset; } #endif diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index fe85f37976..c7a2e71a87 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -3168,15 +3168,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, switch (opcode) { case WASM_OP_ATOMIC_NOTIFY: { - uint32 count, ret; + uint32 notify_count, ret; - count = POP_I32(); + notify_count = POP_I32(); addr = POP_I32(); CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); CHECK_ATOMIC_MEMORY_ACCESS(); ret = wasm_runtime_atomic_notify( - (WASMModuleInstanceCommon *)module, maddr, count); + (WASMModuleInstanceCommon *)module, maddr, notify_count); bh_assert((int32)ret >= 0); PUSH_I32(ret); @@ -3185,7 +3185,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, case WASM_OP_ATOMIC_WAIT32: { uint64 timeout; - uint32 expect, addr, ret; + uint32 expect, ret; timeout = POP_I64(); expect = POP_I32(); diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index a3f4f9cb61..160b0e301f 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -2885,15 +2885,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, case WASM_OP_MEMORY_FILL: { uint32 dst, len; - uint8 val, *mdst; + uint8 fill_val, *mdst; len = POP_I32(); - val = POP_I32(); + fill_val = POP_I32(); dst = POP_I32(); CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst); - memset(mdst, val, len); + memset(mdst, fill_val, len); break; } @@ -3089,15 +3089,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, switch (opcode) { case WASM_OP_ATOMIC_NOTIFY: { - uint32 count, ret; + uint32 notify_count, ret; - count = POP_I32(); + notify_count = POP_I32(); addr = POP_I32(); CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); CHECK_ATOMIC_MEMORY_ACCESS(4); ret = wasm_runtime_atomic_notify( - (WASMModuleInstanceCommon *)module, maddr, count); + (WASMModuleInstanceCommon *)module, maddr, notify_count); bh_assert((int32)ret >= 0); PUSH_I32(ret); @@ -3106,7 +3106,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, case WASM_OP_ATOMIC_WAIT32: { uint64 timeout; - uint32 expect, addr, ret; + uint32 expect, ret; timeout = POP_I64(); expect = POP_I32(); diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 51dde8fd2d..ca705c8af0 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -60,6 +60,25 @@ is_64bit_type(uint8 type) return false; } +static bool +is_value_type(uint8 type) +{ + if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64 + || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64 +#if WASM_ENABLE_REF_TYPES != 0 + || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF +#endif + ) + return true; + return false; +} + +static bool +is_byte_a_type(uint8 type) +{ + return is_value_type(type) || (type == VALUE_TYPE_VOID); +} + static void read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign, uint64 *p_result, char *error_buf, uint32 error_buf_size) @@ -1442,6 +1461,8 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, } } + (void)table_index; + (void)function_count; bh_assert(p == p_end); LOG_VERBOSE("Load table segment section success.\n"); return true; @@ -4418,25 +4439,6 @@ reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode, bh_assert(module->import_memory_count + module->memory_count > 0); \ } while (0) -static bool -is_value_type(uint8 type) -{ - if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64 - || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64 -#if WASM_ENABLE_REF_TYPES != 0 - || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF -#endif - ) - return true; - return false; -} - -static bool -is_byte_a_type(uint8 type) -{ - return is_value_type(type) || (type == VALUE_TYPE_VOID); -} - static bool wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, char *error_buf, uint32 error_buf_size) diff --git a/core/iwasm/libraries/debug-engine/handler.c b/core/iwasm/libraries/debug-engine/handler.c index 0b3744c23e..62d7785485 100644 --- a/core/iwasm/libraries/debug-engine/handler.c +++ b/core/iwasm/libraries/debug-engine/handler.c @@ -222,11 +222,11 @@ handle_generay_query(WASMGDBServer *server, char *payload) WASMDebugMemoryInfo *mem_info = wasm_debug_instance_get_memregion( (WASMDebugInstance *)server->thread->debug_instance, addr); if (mem_info) { - char name[256]; - mem2hex(mem_info->name, name, strlen(mem_info->name)); + char name_buf[256]; + mem2hex(mem_info->name, name_buf, strlen(mem_info->name)); sprintf(tmpbuf, "start:%lx;size:%lx;permissions:%s;name:%s;", (uint64)mem_info->start, mem_info->size, - mem_info->permisson, name); + mem_info->permisson, name_buf); write_packet(server, tmpbuf); wasm_debug_instance_destroy_memregion( (WASMDebugInstance *)server->thread->debug_instance, mem_info); From 5e30de55a34cc4a5ce94ca2b4c9471def970f2da Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 10 Dec 2021 17:23:09 +0800 Subject: [PATCH 08/12] Fix load table segment error --- core/iwasm/interpreter/wasm_mini_loader.c | 32 ++++++++--------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index ca705c8af0..9bf1971392 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -1245,7 +1245,6 @@ check_table_index(const WASMModule *module, uint32 table_index, char *error_buf, return true; } -#if WASM_ENABLE_REF_TYPES != 0 static bool load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module, uint32 *p_table_index, char *error_buf, uint32 error_buf_size) @@ -1263,6 +1262,7 @@ load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module, return true; } +#if WASM_ENABLE_REF_TYPES != 0 static bool load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type, bool elemkind_zero, char *error_buf, uint32 error_buf_size) @@ -1433,28 +1433,18 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, return false; } #else - read_leb_uint32(p, p_end, table_index); - bh_assert(table_index - < module->import_table_count + module->table_count); - - table_segment->table_index = table_index; - - /* initialize expression */ - if (!load_init_expr(&p, p_end, &(table_segment->base_offset), - VALUE_TYPE_I32, error_buf, error_buf_size)) + /* + * like: 00 41 05 0b 04 00 01 00 01 + * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2) + */ + if (!load_table_index(&p, p_end, module, + &table_segment->table_index, error_buf, + error_buf_size)) return false; - - read_leb_uint32(p, p_end, function_count); - table_segment->function_count = function_count; - total_size = sizeof(uint32) * (uint64)function_count; - if (total_size > 0 - && !(table_segment->func_indexes = (uint32 *)loader_malloc( - total_size, error_buf, error_buf_size))) { + if (!load_init_expr(&p, p_end, &table_segment->base_offset, + VALUE_TYPE_I32, error_buf, error_buf_size)) return false; - } - - if (!load_func_index_vec(&p, p_end, module, table_segment, - table_segment->mode == 0 ? false : true, + if (!load_func_index_vec(&p, p_end, module, table_segment, false, error_buf, error_buf_size)) return false; #endif /* WASM_ENABLE_REF_TYPES != 0 */ From 04aa534f6e0d218d9d3f936b2e238aa37fe14c19 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 10 Dec 2021 17:26:43 +0800 Subject: [PATCH 09/12] Fix code format --- core/iwasm/interpreter/wasm_interp_classic.c | 3 ++- core/iwasm/interpreter/wasm_interp_fast.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index c7a2e71a87..419d6d4936 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -3176,7 +3176,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, CHECK_ATOMIC_MEMORY_ACCESS(); ret = wasm_runtime_atomic_notify( - (WASMModuleInstanceCommon *)module, maddr, notify_count); + (WASMModuleInstanceCommon *)module, maddr, + notify_count); bh_assert((int32)ret >= 0); PUSH_I32(ret); diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index 160b0e301f..424f09f865 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -3097,7 +3097,8 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, CHECK_ATOMIC_MEMORY_ACCESS(4); ret = wasm_runtime_atomic_notify( - (WASMModuleInstanceCommon *)module, maddr, notify_count); + (WASMModuleInstanceCommon *)module, maddr, + notify_count); bh_assert((int32)ret >= 0); PUSH_I32(ret); From 021f52e5e0139e604855c8b851c138e14be4ac90 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 10 Dec 2021 23:27:00 +0800 Subject: [PATCH 10/12] Fix wasm_value_type_size not handle VALUE_TYPE_VOID issue --- core/iwasm/interpreter/wasm.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index 63da4a061c..653e906520 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -520,6 +520,8 @@ wasm_value_type_size(uint8 value_type) case VALUE_TYPE_V128: return sizeof(int64) * 2; #endif + case VALUE_TYPE_VOID: + return 0; default: bh_assert(0); } From 54df4734c8632596f5868d65096a1cea65c836b7 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 11 Dec 2021 21:02:27 +0800 Subject: [PATCH 11/12] Fix wamrc endian/bit-width check invalid issue on win32 target --- core/iwasm/compilation/aot_emit_aot_file.c | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index be8310585e..f36f526af9 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -149,17 +149,17 @@ static void dump_buf(uint8 *buf, uint32 size, char *title) #endif static bool -is_32bit_binary(LLVMBinaryRef binary) +is_32bit_binary(const AOTObjectData *obj_data) { - LLVMBinaryType type = LLVMBinaryGetType(binary); - return (type == LLVMBinaryTypeELF32L || type == LLVMBinaryTypeELF32B); + /* bit 1: 0 is 32-bit, 1 is 64-bit */ + return obj_data->target_info.bin_type & 2 ? false : true; } static bool -is_little_endian_binary(LLVMBinaryRef binary) +is_little_endian_binary(const AOTObjectData *obj_data) { - LLVMBinaryType type = LLVMBinaryGetType(binary); - return (type == LLVMBinaryTypeELF32L || type == LLVMBinaryTypeELF64L); + /* bit 0: 0 is little-endian, 1 is big-endian */ + return obj_data->target_info.bin_type & 1 ? false : true; } static bool @@ -568,7 +568,7 @@ get_func_section_size(AOTCompData *comp_data, AOTObjectData *obj_data) /* text offsets + function type indexs */ uint32 size = 0; - if (is_32bit_binary(obj_data->binary)) + if (is_32bit_binary(obj_data)) size = (uint32)sizeof(uint32) * comp_data->func_count; else size = (uint32)sizeof(uint64) * comp_data->func_count; @@ -852,7 +852,7 @@ get_relocation_section_size(AOTCompContext *comp_ctx, AOTObjectData *obj_data) return (uint32)sizeof(uint32) + symbol_table_size + get_relocation_groups_size(relocation_groups, relocation_group_count, - is_32bit_binary(obj_data->binary)); + is_32bit_binary(obj_data)); } static uint32 @@ -1181,7 +1181,7 @@ get_name_section_size(AOTCompData *comp_data) return 0; } - max_aot_buf_size = 4 * (p_end - p); + max_aot_buf_size = 4 * (uint32)(p_end - p); if (!(buf = comp_data->aot_name_section_buf = wasm_runtime_malloc(max_aot_buf_size))) { aot_set_last_error("allocate memory for custom name section failed."); @@ -1689,7 +1689,7 @@ aot_emit_func_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, EMIT_U32(section_size); for (i = 0; i < obj_data->func_count; i++, func++) { - if (is_32bit_binary(obj_data->binary)) + if (is_32bit_binary(obj_data)) EMIT_U32(func->text_offset); else EMIT_U64(func->text_offset); @@ -1816,7 +1816,7 @@ aot_emit_relocation_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, /* emit each relocation */ for (j = 0; j < relocation_group->relocation_count; j++, relocation++) { offset = align_uint(offset, 4); - if (is_32bit_binary(obj_data->binary)) { + if (is_32bit_binary(obj_data)) { EMIT_U32(relocation->relocation_offset); EMIT_U32(relocation->relocation_addend); } @@ -1883,9 +1883,9 @@ aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset, /* sub section id + name section size */ EMIT_U32(sizeof(uint32) * 1 + comp_data->aot_name_section_size); EMIT_U32(AOT_CUSTOM_SECTION_NAME); - bh_memcpy_s((uint8 *)(buf + offset), buf_end - buf, + bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf), comp_data->aot_name_section_buf, - comp_data->aot_name_section_size); + (uint32)comp_data->aot_name_section_size); offset += comp_data->aot_name_section_size; *p_offset = offset; @@ -2321,8 +2321,8 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data, LLVMRelocationIteratorRef rel_itr; AOTRelocation *relocation = group->relocations; uint32 size; - bool is_binary_32bit = is_32bit_binary(obj_data->binary); - bool is_binary_little_endian = is_little_endian_binary(obj_data->binary); + bool is_binary_32bit = is_32bit_binary(obj_data); + bool is_binary_little_endian = is_little_endian_binary(obj_data); bool has_addend = str_starts_with(group->section_name, ".rela"); uint8 *rela_content = NULL; From 6c8505f4e65bc363a2a9caa582122d9c53e2a733 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 16 Dec 2021 00:26:44 +0800 Subject: [PATCH 12/12] Clear more compile warnings reported by -Wshadow flag --- core/iwasm/aot/aot_runtime.c | 2 +- core/iwasm/compilation/aot_emit_control.c | 62 +++++++++++----------- core/iwasm/compilation/aot_emit_memory.c | 12 ++--- core/iwasm/compilation/aot_emit_variable.c | 8 +-- core/iwasm/compilation/aot_llvm.c | 7 +-- core/iwasm/interpreter/wasm_loader.c | 1 - 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 0d9dd49ea5..e2eb94962c 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1403,7 +1403,7 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, bool ret; if (argc < func_type->param_cell_num) { - char buf[128]; + char buf[108]; snprintf(buf, sizeof(buf), "invalid argument count %u, must be no smaller than %u", argc, func_type->param_cell_num); diff --git a/core/iwasm/compilation/aot_emit_control.c b/core/iwasm/compilation/aot_emit_control.c index 4763a9bd4f..9c90cd0069 100644 --- a/core/iwasm/compilation/aot_emit_control.c +++ b/core/iwasm/compilation/aot_emit_control.c @@ -74,41 +74,41 @@ format_block_name(char *name, uint32 name_size, uint32 block_index, #define SET_BUILDER_POS(llvm_block) \ LLVMPositionBuilderAtEnd(comp_ctx->builder, llvm_block) -#define CREATE_RESULT_VALUE_PHIS(block) \ - do { \ - if (block->result_count && !block->result_phis) { \ - uint32 i; \ - uint64 size; \ - LLVMBasicBlockRef block_curr = CURR_BLOCK(); \ - /* Allocate memory */ \ - size = sizeof(LLVMValueRef) * (uint64)block->result_count; \ - if (size >= UINT32_MAX \ - || !(block->result_phis = \ - wasm_runtime_malloc((uint32)size))) { \ - aot_set_last_error("allocate memory failed."); \ - goto fail; \ - } \ - SET_BUILDER_POS(block->llvm_end_block); \ - for (i = 0; i < block->result_count; i++) { \ - if (!(block->result_phis[i] = LLVMBuildPhi( \ - comp_ctx->builder, \ - TO_LLVM_TYPE(block->result_types[i]), "phi"))) { \ - aot_set_last_error("llvm build phi failed."); \ - goto fail; \ - } \ - } \ - SET_BUILDER_POS(block_curr); \ - } \ +#define CREATE_RESULT_VALUE_PHIS(block) \ + do { \ + if (block->result_count && !block->result_phis) { \ + uint32 _i; \ + uint64 _size; \ + LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \ + /* Allocate memory */ \ + _size = sizeof(LLVMValueRef) * (uint64)block->result_count; \ + if (_size >= UINT32_MAX \ + || !(block->result_phis = \ + wasm_runtime_malloc((uint32)_size))) { \ + aot_set_last_error("allocate memory failed."); \ + goto fail; \ + } \ + SET_BUILDER_POS(block->llvm_end_block); \ + for (_i = 0; _i < block->result_count; _i++) { \ + if (!(block->result_phis[_i] = LLVMBuildPhi( \ + comp_ctx->builder, \ + TO_LLVM_TYPE(block->result_types[_i]), "phi"))) { \ + aot_set_last_error("llvm build phi failed."); \ + goto fail; \ + } \ + } \ + SET_BUILDER_POS(_block_curr); \ + } \ } while (0) #define ADD_TO_RESULT_PHIS(block, value, idx) \ do { \ - LLVMBasicBlockRef block_curr = CURR_BLOCK(); \ + LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \ LLVMTypeRef phi_ty = LLVMTypeOf(block->result_phis[idx]); \ LLVMTypeRef value_ty = LLVMTypeOf(value); \ bh_assert(LLVMGetTypeKind(phi_ty) == LLVMGetTypeKind(value_ty)); \ bh_assert(LLVMGetTypeContext(phi_ty) == LLVMGetTypeContext(value_ty)); \ - LLVMAddIncoming(block->result_phis[idx], &value, &block_curr, 1); \ + LLVMAddIncoming(block->result_phis[idx], &value, &_block_curr, 1); \ (void)phi_ty; \ (void)value_ty; \ } while (0) @@ -122,10 +122,10 @@ format_block_name(char *name, uint32 name_size, uint32 block_index, } \ } while (0) -#define ADD_TO_PARAM_PHIS(block, value, idx) \ - do { \ - LLVMBasicBlockRef block_curr = CURR_BLOCK(); \ - LLVMAddIncoming(block->param_phis[idx], &value, &block_curr, 1); \ +#define ADD_TO_PARAM_PHIS(block, value, idx) \ + do { \ + LLVMBasicBlockRef _block_curr = CURR_BLOCK(); \ + LLVMAddIncoming(block->param_phis[idx], &value, &_block_curr, 1); \ } while (0) static LLVMBasicBlockRef diff --git a/core/iwasm/compilation/aot_emit_memory.c b/core/iwasm/compilation/aot_emit_memory.c index c089f426e2..2c2cc99688 100644 --- a/core/iwasm/compilation/aot_emit_memory.c +++ b/core/iwasm/compilation/aot_emit_memory.c @@ -85,7 +85,7 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, LLVMValueRef mem_base_addr, mem_check_bound; LLVMBasicBlockRef block_curr = LLVMGetInsertBlock(comp_ctx->builder); LLVMBasicBlockRef check_succ; - AOTValue *aot_value; + AOTValue *aot_value_top; uint32 local_idx_of_aot_value = 0; bool is_target_64bit, is_local_of_aot_value = false; #if WASM_ENABLE_SHARED_MEMORY != 0 @@ -114,13 +114,13 @@ aot_check_memory_overflow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, } } - aot_value = + aot_value_top = func_ctx->block_stack.block_list_end->value_stack.value_list_end; - if (aot_value) { - /* aot_value is freed in the following POP_I32(addr), + if (aot_value_top) { + /* aot_value_top is freed in the following POP_I32(addr), so save its fields here for further use */ - is_local_of_aot_value = aot_value->is_local; - local_idx_of_aot_value = aot_value->local_idx; + is_local_of_aot_value = aot_value_top->is_local; + local_idx_of_aot_value = aot_value_top->local_idx; } POP_I32(addr); diff --git a/core/iwasm/compilation/aot_emit_variable.c b/core/iwasm/compilation/aot_emit_variable.c index 36069f3f45..7ac6645d31 100644 --- a/core/iwasm/compilation/aot_emit_variable.c +++ b/core/iwasm/compilation/aot_emit_variable.c @@ -32,7 +32,7 @@ aot_compile_op_get_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, { char name[32]; LLVMValueRef value; - AOTValue *aot_value; + AOTValue *aot_value_top; CHECK_LOCAL(local_idx); @@ -45,10 +45,10 @@ aot_compile_op_get_local(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx, PUSH(value, get_local_type(func_ctx, local_idx)); - aot_value = + aot_value_top = func_ctx->block_stack.block_list_end->value_stack.value_list_end; - aot_value->is_local = true; - aot_value->local_idx = local_idx; + aot_value_top->is_local = true; + aot_value_top->local_idx = local_idx; return true; fail: diff --git a/core/iwasm/compilation/aot_llvm.c b/core/iwasm/compilation/aot_llvm.c index a1fe757bfb..796def61f7 100644 --- a/core/iwasm/compilation/aot_llvm.c +++ b/core/iwasm/compilation/aot_llvm.c @@ -1813,7 +1813,7 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option) if (option->enable_simd) { char *tmp; - bool ret; + bool check_simd_ret; comp_ctx->enable_simd = true; @@ -1822,9 +1822,10 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option) goto fail; } - ret = aot_check_simd_compatibility(comp_ctx->target_arch, tmp); + check_simd_ret = + aot_check_simd_compatibility(comp_ctx->target_arch, tmp); LLVMDisposeMessage(tmp); - if (!ret) { + if (!check_simd_ret) { aot_set_last_error("SIMD compatibility check failed, " "try adding --cpu= to specify a cpu " "or adding --disable-simd to disable SIMD"); diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index de4a038138..21f828da2a 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -456,7 +456,6 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) case INIT_EXPR_TYPE_V128_CONST: { - uint8 flag; uint64 high, low; if (type != VALUE_TYPE_V128)