From 0103f6429cdf31d411d2d7b05fbd292c8e08bb76 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 21 Aug 2020 15:11:31 +0800 Subject: [PATCH] Refactor error/exception strings to reduce binary size (#359) --- core/iwasm/aot/aot_loader.c | 166 ++++----- core/iwasm/aot/aot_runtime.c | 38 +- core/iwasm/common/wasm_runtime_common.c | 59 ++- core/iwasm/common/wasm_runtime_common.h | 4 - core/iwasm/compilation/aot_emit_memory.c | 41 --- core/iwasm/interpreter/wasm_interp_classic.c | 39 +- core/iwasm/interpreter/wasm_interp_fast.c | 31 +- core/iwasm/interpreter/wasm_loader.c | 358 ++++++++----------- core/iwasm/interpreter/wasm_mini_loader.c | 37 +- core/iwasm/interpreter/wasm_runtime.c | 53 ++- 10 files changed, 321 insertions(+), 505 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 312f8c3db..a17cce012 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -19,8 +19,26 @@ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); + if (error_buf != NULL) { + snprintf(error_buf, error_buf_size, + "AOT module load failed: %s", string); + } +} + +static void +set_error_buf_v(char *error_buf, uint32 error_buf_size, + const char *format, ...) +{ + va_list args; + char buf[128]; + + if (error_buf != NULL) { + va_start(args, format); + vsnprintf(buf, sizeof(buf), format, args); + va_end(args); + snprintf(error_buf, error_buf_size, + "AOT module load failed: %s", buf); + } } #define exchange_uint8(p_data) (void)0 @@ -64,8 +82,7 @@ check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length, char *error_buf, uint32 error_buf_size) { if (buf + length > buf_end) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: unexpect end."); + set_error_buf(error_buf, error_buf_size, "unexpect end"); return false; } return true; @@ -169,8 +186,7 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + "allocate memory failed"); return NULL; } @@ -200,8 +216,7 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module, if (!bh_hash_map_insert(set, c_str, c_str)) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "insert string to hash map failed."); + "insert string to hash map failed"); wasm_runtime_free(c_str); return NULL; } @@ -233,18 +248,15 @@ get_aot_file_target(AOTTargetInfo *target_info, machine_type = "xtensa"; break; default: - if (error_buf) - snprintf(error_buf, error_buf_size, - "AOT module load failed: unknown machine type %d.", - target_info->e_machine); + set_error_buf_v(error_buf, error_buf_size, + "unknown machine type %d", + target_info->e_machine); return false; } if (strncmp(target_info->arch, machine_type, strlen(machine_type))) { - if (error_buf) - snprintf(error_buf, error_buf_size, - "AOT module load failed: " - "machine type (%s) isn't consistent with target type (%s).", - machine_type, target_info->arch); + set_error_buf_v(error_buf, error_buf_size, + "machine type (%s) isn't consistent with target type (%s)", + machine_type, target_info->arch); return false; } snprintf(target_buf, target_buf_size, "%s", target_info->arch); @@ -264,12 +276,9 @@ check_machine_info(AOTTargetInfo *target_info, return false; if (strcmp(target_expected, target_got)) { - if (error_buf) { - snprintf(error_buf, error_buf_size, - "AOT module load failed: invalid target type, " - "expected %s but got %s.", - target_expected, target_got); - } + set_error_buf_v(error_buf, error_buf_size, + "invalid target type, expected %s but got %s", + target_expected, target_got); return false; } @@ -297,39 +306,35 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end, if (p != buf_end) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid section size."); + "invalid section size"); return false; } /* Check target endian type */ is_target_little_endian = target_info.bin_type & 1 ? false : true; if (is_little_endian() != is_target_little_endian) { - if (error_buf) - snprintf(error_buf, error_buf_size, - "AOT module load failed: " - "invalid target endian type, expected %s but got %s.", - is_little_endian() ? "little endian" : "big endian", - is_target_little_endian ? "little endian" : "big endian"); + set_error_buf_v(error_buf, error_buf_size, + "invalid target endian type, expected %s but got %s", + is_little_endian() ? "little endian" : "big endian", + is_target_little_endian ? "little endian" : "big endian"); return false; } /* Check target bit width */ is_target_64_bit = target_info.bin_type & 2 ? true : false; if ((sizeof(void*) == 8 ? true : false) != is_target_64_bit) { - if (error_buf) - snprintf(error_buf, error_buf_size, - "AOT module load failed: " - "invalid target bit width, expected %s but got %s.", - sizeof(void*) == 8 ? "64-bit" : "32-bit", - is_target_64_bit ? "64-bit" : "32-bit"); + set_error_buf_v(error_buf, error_buf_size, + "invalid target bit width, expected %s but got %s", + sizeof(void*) == 8 ? "64-bit" : "32-bit", + is_target_64_bit ? "64-bit" : "32-bit"); return false; } /* Check target elf file type */ if (target_info.e_type != E_TYPE_REL) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid object file type, " - "expected relocatable file type but got others."); + "invalid object file type, " + "expected relocatable file type but got others"); return false; } @@ -340,7 +345,7 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end, if (target_info.e_version != E_VERSION_CURRENT) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid elf file version."); + "invalid elf file version"); return false; } @@ -639,7 +644,6 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, if (param_count > UINT16_MAX || result_count > UINT16_MAX) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " "param count or result count too large"); return false; } @@ -660,7 +664,6 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end, result_count); if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " "param count or result count too large"); return false; } @@ -866,8 +869,7 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end, for (i = 0; i < module->import_func_count; i++) { read_uint16(buf, buf_end, import_funcs[i].func_type_index); if (import_funcs[i].func_type_index >= module->func_type_count) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: unknown type."); + set_error_buf(error_buf, error_buf_size, "unknown type"); return false; } import_funcs[i].func_type = module->func_types[import_funcs[i].func_type_index]; @@ -967,8 +969,7 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end, if (!(data_sections[i].data = os_mmap(NULL, data_sections[i].size, map_prot, map_flags))) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "allocate memory failed."); + "allocate memory failed"); return false; } #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) @@ -1034,7 +1035,6 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end, && (module->start_func_index >= module->import_func_count + module->func_count)) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " "invalid start function index"); return false; } @@ -1053,7 +1053,6 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end, if (p != p_end) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " "invalid init data section size"); return false; } @@ -1073,7 +1072,7 @@ load_text_section(const uint8 *buf, const uint8 *buf_end, if (module->func_count > 0 && buf_end == buf) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid code size."); + "invalid code size"); return false; } @@ -1120,8 +1119,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, } if (text_offset >= module->code_size) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "invalid function code offset."); + "invalid function code offset"); return false; } module->func_ptrs[i] = (uint8*)module->code + text_offset; @@ -1154,15 +1152,13 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, for (i = 0; i < module->func_count; i++) { read_uint32(p, p_end, module->func_type_indexes[i]); if (module->func_type_indexes[i] >= module->func_type_count) { - set_error_buf(error_buf, error_buf_size, - "AOT module load failed: unknown type."); + set_error_buf(error_buf, error_buf_size, "unknown type"); return false; } } if (p != buf_end) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " "invalid function section size"); return false; } @@ -1204,8 +1200,7 @@ load_exports(const uint8 **p_buf, const uint8 *buf_end, if (export_funcs[i].index >= module->func_count + module->import_func_count) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "function index is out of range."); + "function index is out of range"); return false; } #endif @@ -1232,7 +1227,6 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, if (p != p_end) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " "invalid export section size"); return false; } @@ -1299,7 +1293,7 @@ do_text_relocation(AOTModule *module, if (group->relocation_count > 0 && !aot_text) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid text relocation count."); + "invalid text relocation count"); return false; } @@ -1321,11 +1315,8 @@ do_text_relocation(AOTModule *module, p = symbol + strlen(AOT_FUNC_PREFIX); if (*p == '\0' || (func_index = (uint32)atoi(p)) > module->func_count) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "AOT module load failed: " - "invalid import symbol %s.", - symbol); + set_error_buf_v(error_buf, error_buf_size, + "invalid import symbol %s", symbol); goto check_symbol_fail; } symbol_addr = module->func_ptrs[func_index]; @@ -1339,11 +1330,8 @@ do_text_relocation(AOTModule *module, || !strncmp(symbol, ".rodata.cst", strlen(".rodata.cst"))) { symbol_addr = get_data_section_addr(module, symbol, NULL); if (!symbol_addr) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "AOT module load failed: " - "invalid data section (%s).", - symbol); + set_error_buf_v(error_buf, error_buf_size, + "invalid data section (%s)", symbol); goto check_symbol_fail; } } @@ -1351,11 +1339,8 @@ do_text_relocation(AOTModule *module, symbol_addr = module->literal; } else if (!(symbol_addr = resolve_target_sym(symbol, &symbol_index))) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "AOT module load failed: " - "resolve symbol %s failed.", - symbol); + set_error_buf_v(error_buf, error_buf_size, + "resolve symbol %s failed", symbol); goto check_symbol_fail; } @@ -1400,8 +1385,7 @@ do_data_relocation(AOTModule *module, } else { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "invalid data relocation section name."); + "invalid data relocation section name"); return false; } @@ -1409,7 +1393,7 @@ do_data_relocation(AOTModule *module, &data_size); if (group->relocation_count > 0 && !data_addr) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid data relocation count."); + "invalid data relocation count"); return false; } @@ -1419,11 +1403,8 @@ do_data_relocation(AOTModule *module, symbol_addr = module->code; } else { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "AOT module load failed: " - "invalid relocation symbol %s.", - symbol); + set_error_buf_v(error_buf, error_buf_size, + "invalid relocation symbol %s", symbol); return false; } @@ -1494,8 +1475,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, symbol_offsets, symbol_count, error_buf, error_buf_size)) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "validate symbol table failed."); + "validate symbol table failed"); goto fail; } @@ -1521,8 +1501,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, if (name_index >= symbol_count) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "symbol index out of range."); + "symbol index out of range"); goto fail; } @@ -1568,8 +1547,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, if (symbol_index >= symbol_count) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "symbol index out of range."); + "symbol index out of range"); goto fail; } @@ -1630,7 +1608,7 @@ load_from_sections(AOTModule *module, AOTSection *sections, || (last_section_type != (uint32)-1 && section_type != last_section_type + 1)) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid section order."); + "invalid section order"); return false; } last_section_type = section_type; @@ -1672,7 +1650,7 @@ load_from_sections(AOTModule *module, AOTSection *sections, if (last_section_type != AOT_SECTION_TYPE_RELOCATION) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: section missing."); + "section missing"); return false; } @@ -1747,8 +1725,7 @@ create_module(char *error_buf, uint32 error_buf_size) NULL, aot_free))) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "create const string set failed."); + "create const string set failed"); wasm_runtime_free(module); return NULL; } @@ -1838,8 +1815,7 @@ create_sections(const uint8 *buf, uint32 size, map_prot, map_flags))) { wasm_runtime_free(section); set_error_buf(error_buf, error_buf_size, - "AOT module load failed: " - "mmap memory failed."); + "mmap memory failed"); goto fail; } #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) @@ -1874,14 +1850,14 @@ create_sections(const uint8 *buf, uint32 size, } else { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: invalid section id."); + "invalid section id"); goto fail; } } if (!section_list) { set_error_buf(error_buf, error_buf_size, - "AOT module load failed: create section list failed."); + "create section list failed"); return false; } @@ -2032,7 +2008,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx, (void *)LLVMGetFunctionAddress(comp_ctx->exec_engine, func_name))) { set_error_buf(error_buf, error_buf_size, - "Get function address fail."); + "get function address failed"); goto fail3; } } diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index 79235d61f..4081c673c 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -13,8 +13,10 @@ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); + if (error_buf != NULL) { + snprintf(error_buf, error_buf_size, + "AOT module instantiate failed: %s", string); + } } static void * @@ -25,8 +27,7 @@ runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size) if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed: " - "allocate memory failed."); + "allocate memory failed"); return NULL; } @@ -60,8 +61,7 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module, switch (init_expr->init_expr_type) { case INIT_EXPR_TYPE_GET_GLOBAL: if (init_expr->u.global_index >= module->import_global_count + i) { - set_error_buf(error_buf, error_buf_size, - "Instantiate global failed: unknown global."); + set_error_buf(error_buf, error_buf_size, "unknown global"); return false; } memcpy(p, @@ -333,14 +333,12 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, if (total_size >= UINT32_MAX || !(p = mapped_mem = os_mmap(NULL, map_size, MMAP_PROT_NONE, MMAP_MAP_NONE))) { - set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed: mmap memory failed."); + set_error_buf(error_buf, error_buf_size, "mmap memory failed"); return NULL; } if (os_mprotect(p, total_size, MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) { - set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed: mprotec memory failed."); + set_error_buf(error_buf, error_buf_size, "mprotec memory failed"); os_munmap(mapped_mem, map_size); return NULL; } @@ -364,8 +362,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, if (!(heap_handle = mem_allocator_create(memory_inst->heap_data.ptr, heap_size))) { set_error_buf(error_buf, error_buf_size, - "AOT module instantiate failed:" - "init app heap failed."); + "init app heap failed"); goto fail1; } memory_inst->heap_handle.ptr = heap_handle; @@ -392,8 +389,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module, if (!shared_memory_set_memory_inst((WASMModuleCommon *)module, (WASMMemoryInstanceCommon *)memory_inst)) { set_error_buf(error_buf, error_buf_size, - "Instantiate memory failed:" - "allocate memory failed."); + "allocate memory failed"); goto fail2; } } @@ -659,7 +655,7 @@ execute_start_function(AOTModuleInstance *module_inst) if (!(exec_env = wasm_exec_env_create((WASMModuleInstanceCommon*)module_inst, module_inst->default_wasm_stack_size))) { - aot_set_exception(module_inst, "allocate memory failed."); + aot_set_exception(module_inst, "allocate memory failed"); return false; } @@ -988,7 +984,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, if (aot_exec_env && (aot_exec_env != exec_env)) { - aot_set_exception(module_inst, "Invalid exec env."); + aot_set_exception(module_inst, "invalid exec env"); return false; } @@ -999,7 +995,7 @@ invoke_native_with_hw_bound_check(WASMExecEnv *exec_env, void *func_ptr, /* First time to call aot function, protect one page */ if (os_mprotect(stack_min_addr, page_size * guard_page_count, MMAP_PROT_NONE) != 0) { - aot_set_exception(module_inst, "Set protected page failed."); + aot_set_exception(module_inst, "set protected page failed"); return false; } } @@ -1132,7 +1128,7 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst, if (!(exec_env = wasm_exec_env_create((WASMModuleInstanceCommon*)module_inst, module_inst->default_wasm_stack_size))) { - aot_set_exception(module_inst, "allocate memory failed."); + aot_set_exception(module_inst, "allocate memory failed"); return false; } @@ -1471,12 +1467,10 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) if (total_page_count < cur_page_count /* integer overflow */ || total_page_count > max_page_count) { - aot_set_exception(module_inst, "fail to enlarge memory."); return false; } if (total_size >= UINT32_MAX) { - aot_set_exception(module_inst, "fail to enlarge memory."); return false; } @@ -1501,7 +1495,6 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) /* Restore heap's lock if memory re-alloc failed */ mem_allocator_reinit_lock(memory_inst->heap_handle.ptr); } - aot_set_exception(module_inst, "fail to enlarge memory."); return false; } bh_memcpy_s(memory_data, (uint32)total_size, @@ -1522,7 +1515,6 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) + (memory_data - memory_data_old); if (mem_allocator_migrate(memory_inst->heap_handle.ptr, heap_handle_old) != 0) { - aot_set_exception(module_inst, "fail to enlarge memory."); return false; } } @@ -1562,14 +1554,12 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count) if (total_page_count < cur_page_count /* integer overflow */ || total_page_count > max_page_count) { - aot_set_exception(module_inst, "fail to enlarge memory."); return false; } if (os_mprotect(memory_inst->memory_data_end.ptr, num_bytes_per_page * inc_page_count, MMAP_PROT_READ | MMAP_PROT_WRITE) != 0) { - aot_set_exception(module_inst, "fail to enlarge memory."); return false; } diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index e1fdbd130..20e97f301 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -49,16 +49,6 @@ static void wasm_runtime_destroy_registered_module_list(); #endif /* WASM_ENABLE_MULTI_MODULE */ -void -set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, - ...) -{ - va_list args; - va_start(args, format); - vsnprintf(error_buf, error_buf_size, format, args); - va_end(args); -} - static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { @@ -76,11 +66,11 @@ runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst, || !(mem = wasm_runtime_malloc((uint32)size))) { if (module_inst != NULL) { wasm_runtime_set_exception(module_inst, - "allocate memory failed."); + "allocate memory failed"); } else if (error_buf != NULL) { set_error_buf(error_buf, error_buf_size, - "allocate memory failed."); + "allocate memory failed"); } return NULL; } @@ -308,8 +298,9 @@ wasm_runtime_register_module_internal(const char *module_name, /* module has different name */ LOG_DEBUG("module(%p) has been registered with name %s", module, node->module_name); - set_error_buf_v(error_buf, error_buf_size, - "can not rename the module"); + set_error_buf(error_buf, error_buf_size, + "Register module failed: " + "failed to rename the module"); return false; } else { @@ -359,14 +350,16 @@ wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module, if (!module_name || !module) { LOG_DEBUG("module_name and module are required"); - set_error_buf_v(error_buf, error_buf_size, - "module_name and module are required"); + set_error_buf(error_buf, error_buf_size, + "Register module failed: " + "module_name and module are required"); return false; } if (wasm_runtime_is_built_in_module(module_name)) { LOG_DEBUG("%s is a built-in module name", module_name); set_error_buf(error_buf, error_buf_size, + "Register module failed: " "can not register as a built-in module"); return false; } @@ -1325,7 +1318,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, (module_inst, (uint32)argv_buf_len, (void**)&argv_buf))) { set_error_buf(error_buf, error_buf_size, - "Init wasi environment failed: allocate memory failed."); + "Init wasi environment failed: allocate memory failed"); goto fail; } @@ -1349,7 +1342,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, (module_inst, (uint32)env_buf_len, (void**)&env_buf))) { set_error_buf(error_buf, error_buf_size, - "Init wasi environment failed: allocate memory failed."); + "Init wasi environment failed: allocate memory failed"); goto fail; } @@ -1368,7 +1361,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, (module_inst, sizeof(struct argv_environ_values), (void**)&argv_environ))) { set_error_buf(error_buf, error_buf_size, - "Init wasi environment failed: allocate memory failed."); + "Init wasi environment failed: allocate memory failed"); goto fail; } @@ -1383,7 +1376,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, if (!fd_table_init(curfds)) { set_error_buf(error_buf, error_buf_size, "Init wasi environment failed: " - "init fd table failed."); + "init fd table failed"); goto fail; } fd_table_inited = true; @@ -1391,7 +1384,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, if (!fd_prestats_init(prestats)) { set_error_buf(error_buf, error_buf_size, "Init wasi environment failed: " - "init fd prestats failed."); + "init fd prestats failed"); goto fail; } fd_prestats_inited = true; @@ -1403,7 +1396,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, env_buf, env_buf_len)) { set_error_buf(error_buf, error_buf_size, "Init wasi environment failed: " - "init argument environment failed."); + "init argument environment failed"); goto fail; } argv_environ_inited = true; @@ -1413,7 +1406,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst, || !fd_table_insert_existing(curfds, 1, 1) || !fd_table_insert_existing(curfds, 2, 2)) { set_error_buf(error_buf, error_buf_size, - "Init wasi environment failed: init fd table failed."); + "Init wasi environment failed: init fd table failed"); goto fail; } @@ -1689,7 +1682,7 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, if (!func) { wasm_runtime_set_exception(module_inst, - "lookup main function failed."); + "lookup main function failed"); return false; } @@ -1697,7 +1690,7 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, if (module_inst->module_type == Wasm_Module_Bytecode) { if (((WASMFunctionInstance*)func)->is_import_func) { wasm_runtime_set_exception(module_inst, - "lookup main function failed."); + "lookup main function failed"); return false; } func_type = ((WASMFunctionInstance*)func)->u.func->func_type; @@ -1710,7 +1703,7 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, if (!check_main_func_type(func_type)) { wasm_runtime_set_exception(module_inst, - "invalid function type of main function."); + "invalid function type of main function"); return false; } @@ -1726,7 +1719,7 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, wasm_runtime_module_malloc(module_inst, (uint32)total_size, (void**)&argv_buf))) { wasm_runtime_set_exception(module_inst, - "allocate memory failed."); + "allocate memory failed"); return false; } @@ -1942,7 +1935,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, func = resolve_function(module_inst, name); if (!func) { - snprintf(buf, sizeof(buf), "lookup function %s failed.", name); + snprintf(buf, sizeof(buf), "lookup function %s failed", name); wasm_runtime_set_exception(module_inst, buf); goto fail; } @@ -1955,7 +1948,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, && !wasm_func->import_func_inst #endif ) { - snprintf(buf, sizeof(buf), "lookup function %s failed.", name); + snprintf(buf, sizeof(buf), "lookup function %s failed", name); wasm_runtime_set_exception(module_inst, buf); goto fail; } @@ -1976,7 +1969,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, if (type->param_count != (uint32)argc) { wasm_runtime_set_exception(module_inst, - "invalid input argument count."); + "invalid input argument count"); goto fail; } @@ -1994,7 +1987,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, char *endptr = NULL; bh_assert(argv[i] != NULL); if (argv[i][0] == '\0') { - snprintf(buf, sizeof(buf), "invalid input argument %d.", i); + snprintf(buf, sizeof(buf), "invalid input argument %d", i); wasm_runtime_set_exception(module_inst, buf); goto fail; } @@ -2074,14 +2067,14 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst, } } if (endptr && *endptr != '\0' && *endptr != '_') { - snprintf(buf, sizeof(buf), "invalid input argument %d: %s.", + snprintf(buf, sizeof(buf), "invalid input argument %d: %s", i, argv[i]); wasm_runtime_set_exception(module_inst, buf); goto fail; } if (errno != 0) { snprintf(buf, sizeof(buf), - "prepare function argument error, errno: %d.", errno); + "prepare function argument error, errno: %d", errno); wasm_runtime_set_exception(module_inst, buf); goto fail; } diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index d8590a974..8d6767de5 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -79,10 +79,6 @@ typedef struct WASMMemoryInstanceCommon { typedef package_type_t PackageType; typedef wasm_section_t WASMSection, AOTSection; -void -set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, - ...); - /* See wasm_export.h for description */ bool wasm_runtime_init(void); diff --git a/core/iwasm/compilation/aot_emit_memory.c b/core/iwasm/compilation/aot_emit_memory.c index 771513946..68ed1ef7e 100644 --- a/core/iwasm/compilation/aot_emit_memory.c +++ b/core/iwasm/compilation/aot_emit_memory.c @@ -700,47 +700,6 @@ aot_compile_op_memory_grow(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx) } PUSH_I32(ret_value); - - /* To be simple, call wasm_runtime_set_exception() no matter - enlarge success or not */ - param_types[1] = INT8_PTR_TYPE; - ret_type = VOID_TYPE; - if (!(func_type = LLVMFunctionType(ret_type, param_types, 2, false))) { - aot_set_last_error("llvm add function type failed."); - return false; - } - - 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; - } - if (!(value = I64_CONST((uint64)(uintptr_t)wasm_runtime_set_exception)) - || !(func = LLVMConstIntToPtr(value, func_ptr_type))) { - aot_set_last_error("create LLVM value failed."); - return false; - } - } - else { - char *func_name = "wasm_runtime_set_exception"; - /* AOT mode, delcare the function */ - if (!(func = LLVMGetNamedFunction(comp_ctx->module, func_name)) - && !(func = LLVMAddFunction(comp_ctx->module, - func_name, func_type))) { - aot_set_last_error("llvm add function failed."); - return false; - } - } - - /* Call function wasm_runtime_set_exception(aot_inst, NULL) */ - param_values[1] = LLVMConstNull(INT8_PTR_TYPE); - CHECK_LLVM_CONST(param_values[1]); - if (!(LLVMBuildCall(comp_ctx->builder, func, param_values, 2, ""))) { - aot_set_last_error("llvm build call failed."); - return false; - } - return true; fail: return false; diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index ab37459f0..ec9a10237 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -903,7 +903,7 @@ ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) frame->prev_frame = prev_frame; else { wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, - "WASM interp failed: stack overflow."); + "stack overflow"); } return frame; @@ -1244,8 +1244,7 @@ label_pop_csp_n: uint64 total_size = sizeof(uint32) * (uint64)count; if (total_size >= UINT32_MAX || !(depths = wasm_runtime_malloc((uint32)total_size))) { - wasm_set_exception(module, - "WASM interp failed: allocate memory failed."); + wasm_set_exception(module, "allocate memory failed"); goto got_exception; } } @@ -1303,7 +1302,7 @@ label_pop_csp_n: */ read_leb_uint32(frame_ip, frame_ip_end, tidx); if (tidx >= module->module->type_count) { - wasm_set_exception(module, "type index is overflow"); + wasm_set_exception(module, "unknown type"); goto got_exception; } cur_type = wasm_types[tidx]; @@ -1839,10 +1838,6 @@ label_pop_csp_n: if (!wasm_enlarge_memory(module, delta)) { /* fail to memory.grow, return -1 */ PUSH_I32(-1); - if (wasm_get_exception(module)) { - os_printf("%s\n", wasm_get_exception(module)); - wasm_set_exception(module, NULL); - } } else { /* success, return previous page count */ @@ -2762,7 +2757,7 @@ label_pop_csp_n: } #endif /* WASM_ENABLE_BULK_MEMORY */ default: - wasm_set_exception(module, "WASM interp failed: unsupported opcode."); + wasm_set_exception(module, "unsupported opcode"); goto got_exception; break; } @@ -3107,7 +3102,7 @@ label_pop_csp_n: #if WASM_ENABLE_LABELS_AS_VALUES == 0 default: - wasm_set_exception(module, "WASM interp failed: unsupported opcode."); + wasm_set_exception(module, "unsupported opcode"); goto got_exception; } #endif @@ -3137,7 +3132,7 @@ label_pop_csp_n: HANDLE_OP (EXT_OP_COPY_STACK_TOP_I64): HANDLE_OP (EXT_OP_COPY_STACK_VALUES): { - wasm_set_exception(module, "WASM interp failed: unsupported opcode."); + wasm_set_exception(module, "unsupported opcode"); goto got_exception; } #endif @@ -3192,7 +3187,7 @@ label_pop_csp_n: + (uint64)cur_wasm_func->max_stack_cell_num + ((uint64)cur_wasm_func->max_block_num) * sizeof(WASMBranchBlock) / 4; if (all_cell_num >= UINT32_MAX) { - wasm_set_exception(module, "WASM interp failed: stack overflow."); + wasm_set_exception(module, "stack overflow"); goto got_exception; } @@ -3288,7 +3283,7 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, if ((uint8*)&prev_frame < exec_env->native_stack_boundary) { wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, - "WASM interp failed: native stack overflow."); + "native stack overflow"); return; } @@ -3310,20 +3305,16 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, #if WASM_ENABLE_MULTI_MODULE != 0 if (function->import_module_inst) { LOG_DEBUG("it is a function of a sub module"); - wasm_interp_call_func_import(module_inst, - exec_env, - function, - frame); + wasm_interp_call_func_import(module_inst, exec_env, + function, frame); } else #endif { LOG_DEBUG("it is an native function"); /* it is a native function */ - wasm_interp_call_func_native(module_inst, - exec_env, - function, - frame); + wasm_interp_call_func_native(module_inst, exec_env, + function, frame); } } else { @@ -3339,10 +3330,12 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, if (function->ret_cell_num) { LOG_DEBUG("first return value argv[0]=%d", argv[0]); - } else { + } + else { LOG_DEBUG("no return value"); } - } else { + } + else { LOG_DEBUG("meet an exception %s", wasm_get_exception(module_inst)); } diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index ce39dc3b4..2a19d8aea 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -802,8 +802,7 @@ copy_stack_values(WASMModuleInstance *module, uint64 total_size = sizeof(uint32) * (uint64)total_cell_num; if (total_size >= UINT32_MAX || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) { - wasm_set_exception(module, - "WASM interp failed: allocate memory failed."); + wasm_set_exception(module, "allocate memory failed"); return false; } } @@ -951,7 +950,7 @@ ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame) frame->prev_frame = prev_frame; else { wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, - "WASM interp failed: stack overflow."); + "stack overflow"); } return frame; @@ -1789,10 +1788,6 @@ recover_br_info: if (!wasm_enlarge_memory(module, delta)) { /* fail to memory.grow, return -1 */ frame_lp[addr_ret] = -1; - if (wasm_get_exception(module)) { - os_printf("%s\n", wasm_get_exception(module)); - wasm_set_exception(module, NULL); - } } else { /* success, return previous page count */ @@ -2756,7 +2751,7 @@ recover_br_info: } #endif /* WASM_ENABLE_BULK_MEMORY */ default: - wasm_set_exception(module, "WASM interp failed: unsupported opcode."); + wasm_set_exception(module, "unsupported opcode"); goto got_exception; break; } @@ -3111,7 +3106,7 @@ recover_br_info: #if WASM_ENABLE_LABELS_AS_VALUES == 0 default: - wasm_set_exception(module, "WASM interp failed: unsupported opcode."); + wasm_set_exception(module, "unsupported opcode"); goto got_exception; } #endif @@ -3155,7 +3150,7 @@ recover_br_info: HANDLE_OP (EXT_OP_LOOP): HANDLE_OP (EXT_OP_IF): { - wasm_set_exception(module, "WASM interp failed: unsupported opcode."); + wasm_set_exception(module, "unsupported opcode"); goto got_exception; } #endif @@ -3234,7 +3229,7 @@ recover_br_info: + (uint64)cur_func->const_cell_num + (uint64)cur_wasm_func->max_stack_cell_num; if (all_cell_num >= UINT32_MAX) { - wasm_set_exception(module, "WASM interp failed: stack overflow."); + wasm_set_exception(module, "stack overflow"); goto got_exception; } @@ -3336,7 +3331,7 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, if ((uint8*)&prev_frame < exec_env->native_stack_boundary) { wasm_set_exception((WASMModuleInstance*)exec_env->module_inst, - "WASM interp failed: native stack overflow."); + "native stack overflow"); return; } @@ -3359,19 +3354,15 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, #if WASM_ENABLE_MULTI_MODULE != 0 if (function->import_module_inst) { LOG_DEBUG("it is a function of a sub module"); - wasm_interp_call_func_import(module_inst, - exec_env, - function, - frame); + wasm_interp_call_func_import(module_inst, exec_env, + function, frame); } else #endif { LOG_DEBUG("it is an native function"); - wasm_interp_call_func_native(module_inst, - exec_env, - function, - frame); + wasm_interp_call_func_native(module_inst, exec_env, + function, frame); } } else { diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index f9f8d1016..7156231b9 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -20,8 +20,26 @@ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); + if (error_buf != NULL) { + snprintf(error_buf, error_buf_size, + "WASM module load failed: %s", string); + } +} + +static void +set_error_buf_v(char *error_buf, uint32 error_buf_size, + const char *format, ...) +{ + va_list args; + char buf[128]; + + if (error_buf != NULL) { + va_start(args, format); + vsnprintf(buf, sizeof(buf), format, args); + va_end(args); + snprintf(error_buf, error_buf_size, + "WASM module load failed: %s", buf); + } } static bool @@ -30,7 +48,6 @@ check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length, { if (buf + length > buf_end) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "unexpected end of section or function"); return false; } @@ -42,8 +59,7 @@ check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length, char *error_buf, uint32 error_buf_size) { if (buf + length > buf_end) { - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: unexpected end"); + set_error_buf(error_buf, error_buf_size, "unexpected end"); return false; } return true; @@ -74,7 +90,6 @@ skip_leb(const uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, while (true) { if (bcnt + 1 > (maxbits + 6) / 7) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "integer representation too long"); return false; } @@ -126,7 +141,6 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, while (true) { if (bcnt + 1 > (maxbits + 6) / 7) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "integer representation too long"); return false; } @@ -184,8 +198,7 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, return true; fail_integer_too_large: - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: integer too large"); + set_error_buf(error_buf, error_buf_size, "integer too large"); fail: return false; } @@ -226,8 +239,7 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " - "allocate memory failed."); + "allocate memory failed"); return NULL; } @@ -278,7 +290,6 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module, if (!check_utf8_str(str, len)) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "invalid UTF-8 encoding"); return NULL; } @@ -381,8 +392,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, return true; fail_type_mismatch: set_error_buf(error_buf, error_buf_size, - "WASM module load failed: type mismatch or " - "constant expression required."); + "type mismatch or constant expression required"); fail: return false; } @@ -413,7 +423,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, flag = read_uint8(p); if (flag != 0x60) { set_error_buf(error_buf, error_buf_size, - "Load type section failed: invalid type flag."); + "invalid type flag"); return false; } @@ -429,7 +439,6 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (param_count > UINT16_MAX || result_count > UINT16_MAX) { set_error_buf(error_buf, error_buf_size, - "Load type section failed: " "param count or result count too large"); return false; } @@ -459,7 +468,6 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, result_count); if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) { set_error_buf(error_buf, error_buf_size, - "Load type section failed: " "param count or result count too large"); return false; } @@ -469,8 +477,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load type section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -512,8 +519,8 @@ wasm_loader_find_export(const WASMModule *module, if (i == module->export_count) { LOG_DEBUG("can not find an export %d named %s in the module %s", export_kind, field_name, module_name); - set_error_buf_v(error_buf, error_buf_size, - "unknown import or incompatible import type"); + set_error_buf(error_buf, error_buf_size, + "unknown import or incompatible import type"); return NULL; } @@ -521,7 +528,7 @@ wasm_loader_find_export(const WASMModule *module, LOG_DEBUG("%s in the module %s is out of index (%d >= %d )", field_name, module_name, export->index, export_index_boundary); - set_error_buf_v(error_buf, error_buf_size, "incompatible import type"); + set_error_buf(error_buf, error_buf_size, "incompatible import type"); return NULL; } @@ -544,7 +551,7 @@ wasm_loader_resolve_function(const char *module_name, if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { LOG_DEBUG("can not find a module named %s for function", module_name); - set_error_buf_v(error_buf, error_buf_size, "unknown import"); + set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } @@ -575,7 +582,7 @@ wasm_loader_resolve_function(const char *module_name, if (!wasm_type_equal(expected_function_type, target_function_type)) { LOG_DEBUG("%s.%s failed the type check", module_name, function_name); - set_error_buf_v(error_buf, error_buf_size, "incompatible import type"); + set_error_buf(error_buf, error_buf_size, "incompatible import type"); return NULL; } @@ -596,7 +603,7 @@ wasm_loader_resolve_table(const char *module_name, const char *table_name, if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { LOG_DEBUG("can not find a module named %s for table", module_name); - set_error_buf_v(error_buf, error_buf_size, "unknown import"); + set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } @@ -622,7 +629,7 @@ wasm_loader_resolve_table(const char *module_name, const char *table_name, LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)", module_name, table_name, table->init_size, table->max_size, init_size, max_size); - set_error_buf_v(error_buf, error_buf_size, "incompatible import type"); + set_error_buf(error_buf, error_buf_size, "incompatible import type"); return NULL; } @@ -643,7 +650,7 @@ wasm_loader_resolve_memory(const char *module_name, const char *memory_name, if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { LOG_DEBUG("can not find a module named %s for memory", module_name); - set_error_buf_v(error_buf, error_buf_size, "unknown import"); + set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } @@ -672,7 +679,7 @@ wasm_loader_resolve_memory(const char *module_name, const char *memory_name, LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)", module_name, memory_name, memory->init_page_count, memory->max_page_count, init_page_count, max_page_count); - set_error_buf_v(error_buf, error_buf_size, "incompatible import type"); + set_error_buf(error_buf, error_buf_size, "incompatible import type"); return NULL; } return memory; @@ -693,7 +700,7 @@ wasm_loader_resolve_global(const char *module_name, if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) { LOG_DEBUG("can not find a module named %s for global", module_name); - set_error_buf_v(error_buf, error_buf_size, "unknown import"); + set_error_buf(error_buf, error_buf_size, "unknown import"); return NULL; } @@ -719,7 +726,7 @@ wasm_loader_resolve_global(const char *module_name, LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)", module_name, global_name, global->type, global->is_mutable, type, is_mutable); - set_error_buf_v(error_buf, error_buf_size, "incompatible import type"); + set_error_buf(error_buf, error_buf_size, "incompatible import type"); return NULL; } return global; @@ -747,9 +754,7 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module, *p_buf = p; if (declare_type_index >= parent_module->type_count) { - set_error_buf(error_buf, error_buf_size, - "Load import section failed: unknown type."); - LOG_DEBUG("the type index is out of range"); + set_error_buf(error_buf, error_buf_size, "unknown type"); return false; } @@ -758,8 +763,7 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module, is_built_in_module = wasm_runtime_is_built_in_module(sub_module_name); if (is_built_in_module) { LOG_DEBUG("%s is a function of a built-in module %s", - function_name, - sub_module_name); + function_name, sub_module_name); /* check built-in modules */ linked_func = wasm_native_resolve_symbol(sub_module_name, function_name, @@ -771,8 +775,7 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module, #if WASM_ENABLE_MULTI_MODULE != 0 else { LOG_DEBUG("%s is a function of a sub-module %s", - function_name, - sub_module_name); + function_name, sub_module_name); linked_func = wasm_loader_resolve_function(sub_module_name, function_name, declare_func_type, @@ -783,15 +786,13 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module, if (!linked_func) { #if WASM_ENABLE_SPEC_TEST != 0 - set_error_buf(error_buf, - error_buf_size, + set_error_buf(error_buf, error_buf_size, "unknown import or incompatible import type"); return false; #else #if WASM_ENABLE_WAMR_COMPILER == 0 - LOG_WARNING( - "warning: fail to link import function (%s, %s)", - sub_module_name, function_name); + LOG_WARNING("warning: fail to link import function (%s, %s)", + sub_module_name, function_name); #endif #endif } @@ -1068,9 +1069,7 @@ load_global_import(const WASMModule *parent_module, *p_buf = p; if (declare_mutable >= 2) { - set_error_buf(error_buf, error_buf_size, - "Load import section failed: " - "invalid mutability"); + set_error_buf(error_buf, error_buf_size, "invalid mutability"); return false; } @@ -1108,8 +1107,8 @@ load_global_import(const WASMModule *parent_module, if (!ret) { #if WASM_ENABLE_SPEC_TEST != 0 - set_error_buf_v(error_buf, error_buf_size, - "unknown import or incompatible import type"); + set_error_buf(error_buf, error_buf_size, + "unknown import or incompatible import type"); return false; #endif } @@ -1298,7 +1297,7 @@ load_depended_module(const WASMModule *parent_module, LOG_DEBUG("error: there is no sub_module reader to load %s", sub_module_name); set_error_buf_v(error_buf, error_buf_size, - "error: there is no sub_module reader to load %s", + "no sub module reader to load %s", sub_module_name); return NULL; } @@ -1308,7 +1307,7 @@ load_depended_module(const WASMModule *parent_module, if (ret) { LOG_DEBUG("find a circular dependency on %s", sub_module_name); set_error_buf_v(error_buf, error_buf_size, - "error: find a circular dependency on %s", + "found circular dependency on %s", sub_module_name); return NULL; } @@ -1325,7 +1324,7 @@ load_depended_module(const WASMModule *parent_module, if (!ret) { LOG_DEBUG("read the file of %s failed", sub_module_name); set_error_buf_v(error_buf, error_buf_size, - "error: can not read the module file of %s", + "failed to read module file of %s", sub_module_name); goto DELETE_FROM_LOADING_LIST; } @@ -1360,12 +1359,9 @@ load_depended_module(const WASMModule *parent_module, REGISTER_SUB_MODULE: ret = register_sub_module(parent_module, sub_module_name, sub_module); if (!ret) { - LOG_DEBUG("error: can not register a sub module %s with its parent", - sizeof(WASMRegisteredModule)); - set_error_buf_v( - error_buf, error_buf_size, - "error: can not register a sub module %s with its parent", - sizeof(WASMRegisteredModule)); + set_error_buf_v(error_buf, error_buf_size, + "failed to register sub module %s", + sub_module_name); /* * since it is in the global module list, there is no need to * unload the module. the runtime_destroy() will do it @@ -1451,7 +1447,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, module->import_table_count++; if (module->import_table_count > 1) { set_error_buf(error_buf, error_buf_size, - "Load import section failed: multiple tables"); + "multiple tables"); return false; } break; @@ -1464,7 +1460,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, module->import_memory_count++; if (module->import_memory_count > 1) { set_error_buf(error_buf, error_buf_size, - "Load import section failed: multiple memories"); + "multiple memories"); return false; } break; @@ -1477,7 +1473,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, default: set_error_buf(error_buf, error_buf_size, - "Load import section failed: invalid import kind"); + "invalid import kind"); return false; } } @@ -1608,7 +1604,6 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, default: set_error_buf(error_buf, error_buf_size, - "Load import section failed: " "invalid import kind"); return false; } @@ -1631,8 +1626,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load import section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -1698,7 +1692,6 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, if (func_count != code_count) { set_error_buf(error_buf, error_buf_size, - "Load function section failed: " "function and code section have inconsistent lengths"); return false; } @@ -1715,9 +1708,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, /* Resolve function type */ read_leb_uint32(p, p_end, type_index); if (type_index >= module->type_count) { - set_error_buf(error_buf, error_buf_size, - "Load function section failed: " - "unknown type."); + set_error_buf(error_buf, error_buf_size, "unknown type"); return false; } @@ -1725,8 +1716,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, if (code_size == 0 || p_code + code_size > buf_code_end) { set_error_buf(error_buf, error_buf_size, - "Load function section failed: " - "invalid function code size."); + "invalid function code size"); return false; } @@ -1741,7 +1731,6 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, read_leb_uint32(p_code, buf_code_end, sub_local_count); if (sub_local_count > UINT32_MAX - local_count) { set_error_buf(error_buf, error_buf_size, - "Load function section failed: " "too many locals"); return false; } @@ -1786,8 +1775,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, if (local_type_index + sub_local_count <= local_type_index || local_type_index + sub_local_count > local_count) { set_error_buf(error_buf, error_buf_size, - "Load function section failed: " - "invalid local count."); + "invalid local count"); return false; } CHECK_BUF(p_code, buf_code_end, 1); @@ -1795,8 +1783,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, type = read_uint8(p_code); if (type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) { set_error_buf(error_buf, error_buf_size, - "Load function section failed: " - "invalid local type."); + "invalid local type"); return false; } for (k = 0; k < sub_local_count; k++) { @@ -1817,8 +1804,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end, } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load function section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -1860,8 +1846,7 @@ load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load table section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -1903,8 +1888,7 @@ load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load memory section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -1941,9 +1925,7 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, global->type = read_uint8(p); mutable = read_uint8(p); if (mutable >= 2) { - set_error_buf(error_buf, error_buf_size, - "Load import section failed: " - "invalid mutability"); + set_error_buf(error_buf, error_buf_size, "invalid mutability"); return false; } global->is_mutable = mutable ? true : false; @@ -1970,8 +1952,7 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load global section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -2031,44 +2012,43 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, switch(export->kind) { /*function index*/ case EXPORT_KIND_FUNC: - if (index >= module->function_count + module->import_function_count) { + if (index >= module->function_count + + module->import_function_count) { set_error_buf(error_buf, error_buf_size, - "Load export section failed: " - "unknown function."); + "unknown function"); return false; } break; /*table index*/ case EXPORT_KIND_TABLE: - if (index >= module->table_count + module->import_table_count) { + if (index >= module->table_count + + module->import_table_count) { set_error_buf(error_buf, error_buf_size, - "Load export section failed: " - "unknown table."); + "unknown table"); return false; } break; /*memory index*/ case EXPORT_KIND_MEMORY: - if (index >= module->memory_count + module->import_memory_count) { + if (index >= module->memory_count + + module->import_memory_count) { set_error_buf(error_buf, error_buf_size, - "Load export section failed: " - "unknown memory."); + "unknown memory"); return false; } break; /*global index*/ case EXPORT_KIND_GLOBAL: - if (index >= module->global_count + module->import_global_count) { + if (index >= module->global_count + + module->import_global_count) { set_error_buf(error_buf, error_buf_size, - "Load export section failed: " - "unknown global."); + "unknown global"); return false; } break; default: set_error_buf(error_buf, error_buf_size, - "Load export section failed: " - "invalid export kind."); + "invalid export kind"); return false; } } @@ -2076,7 +2056,7 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (p != p_end) { set_error_buf(error_buf, error_buf_size, - "Load export section failed: section size mismatch"); + "section size mismatch"); return false; } @@ -2108,9 +2088,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m table_segment = module->table_segments; for (i = 0; i < table_segment_count; i++, table_segment++) { if (p >= p_end) { - set_error_buf(error_buf, error_buf_size, - "Load table segment section failed: " - "unexpected end"); + set_error_buf(error_buf, error_buf_size, "unexpected end"); return false; } read_leb_uint32(p, p_end, table_index); @@ -2140,7 +2118,6 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m if (function_index >= module->import_function_count + module->function_count) { set_error_buf(error_buf, error_buf_size, - "Load table segment section failed: " "unknown function"); return false; } @@ -2150,8 +2127,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load table segment section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -2267,8 +2243,7 @@ check_mem_index: } if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load data segment section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -2290,8 +2265,7 @@ load_datacount_section(const uint8 *buf, const uint8 *buf_end, WASMModule *modul module->data_seg_count1 = data_seg_count1; if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load datacount section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -2322,7 +2296,6 @@ load_code_section(const uint8 *buf, const uint8 *buf_end, if (func_count != code_count) { set_error_buf(error_buf, error_buf_size, - "Load code section failed: " "function and code section have inconsistent lengths"); return false; } @@ -2345,9 +2318,7 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (start_function >= module->function_count + module->import_function_count) { - set_error_buf(error_buf, error_buf_size, - "Load start section failed: " - "unknown function."); + set_error_buf(error_buf, error_buf_size, "unknown function"); return false; } @@ -2358,17 +2329,14 @@ load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, module->functions[start_function - module->import_function_count] ->func_type; if (type->param_count != 0 || type->result_count != 0) { - set_error_buf(error_buf, error_buf_size, - "Load start section failed: " - "invalid start function."); + set_error_buf(error_buf, error_buf_size, "invalid start function"); return false; } module->start_function = start_function; if (p != p_end) { - set_error_buf(error_buf, error_buf_size, - "Load start section failed: section size mismatch"); + set_error_buf(error_buf, error_buf_size, "section size mismatch"); return false; } @@ -2386,8 +2354,7 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, uint32 name_len; if (p >= p_end) { - set_error_buf(error_buf, error_buf_size, - "Load custom section failed: unexpected end"); + set_error_buf(error_buf, error_buf_size, "unexpected end"); return false; } @@ -2395,14 +2362,12 @@ load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module, if (name_len == 0 || p + name_len > p_end) { - set_error_buf(error_buf, error_buf_size, - "Load custom section failed: unexpected end"); + set_error_buf(error_buf, error_buf_size, "unexpected end"); return false; } if (!check_utf8_str(p, name_len)) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "invalid UTF-8 encoding"); return false; } @@ -2465,44 +2430,53 @@ load_from_sections(WASMModule *module, WASMSection *sections, switch (section->section_type) { case SECTION_TYPE_USER: /* unsupported user section, ignore it. */ - if (!load_user_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_user_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_TYPE: - if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_type_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_IMPORT: - if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_import_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_FUNC: if (!load_function_section(buf, buf_end, buf_code, buf_code_end, - module, error_buf, error_buf_size)) + module, error_buf, error_buf_size)) return false; break; case SECTION_TYPE_TABLE: - if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_table_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_MEMORY: - if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_memory_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_GLOBAL: - if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_global_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_EXPORT: - if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_export_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_START: - if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_start_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_ELEM: - if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_table_segment_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; case SECTION_TYPE_CODE: @@ -2511,18 +2485,20 @@ load_from_sections(WASMModule *module, WASMSection *sections, return false; break; case SECTION_TYPE_DATA: - if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_data_segment_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; #if WASM_ENABLE_BULK_MEMORY != 0 case SECTION_TYPE_DATACOUNT: - if (!load_datacount_section(buf, buf_end, module, error_buf, error_buf_size)) + if (!load_datacount_section(buf, buf_end, module, + error_buf, error_buf_size)) return false; break; #endif default: set_error_buf(error_buf, error_buf_size, - "WASM module load failed: invalid section id"); + "invalid section id"); return false; } @@ -2533,7 +2509,8 @@ load_from_sections(WASMModule *module, WASMSection *sections, handle_table = wasm_interp_get_handle_table(); #endif - total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE * BLOCK_ADDR_CONFLICT_SIZE; + total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE + * BLOCK_ADDR_CONFLICT_SIZE; if (!(block_addr_cache = loader_malloc (total_size, error_buf, error_buf_size))) { return false; @@ -2847,7 +2824,6 @@ create_sections(const uint8 *buf, uint32 size, if (last_section_index != (uint8)-1 && (section_index <= last_section_index)) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "junk after last section"); return false; } @@ -2877,7 +2853,7 @@ create_sections(const uint8 *buf, uint32 size, } else { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: invalid section id"); + "invalid section id"); return false; } } @@ -2922,7 +2898,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module, if (magic_number != WASM_MAGIC_NUMBER) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: magic header not detected"); + "magic header not detected"); return false; } @@ -2933,7 +2909,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module, if (version != WASM_CURRENT_VERSION) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: unknown binary version"); + "unknown binary version"); return false; } @@ -2958,7 +2934,6 @@ wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_bu } if (!load(buf, size, module, error_buf, error_buf_size)) { - LOG_VERBOSE("Load module failed, %s", error_buf); goto fail; } @@ -3436,10 +3411,9 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, break; #endif default: - if (error_buf) - snprintf(error_buf, error_buf_size, - "WASM loader find block addr failed: " - "invalid opcode fc %02x.", opcode); + set_error_buf_v(error_buf, error_buf_size, + "%s %02x %02x", + "unsupported opcode", 0xfc, opcode); return false; } break; @@ -3461,10 +3435,9 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, } #endif default: - if (error_buf) - snprintf(error_buf, error_buf_size, - "WASM loader find block addr failed: " - "invalid opcode %02x.", opcode); + set_error_buf_v(error_buf, error_buf_size, + "%s %02x", + "unsupported opcode", opcode); return false; } } @@ -3488,7 +3461,7 @@ fail: #if WASM_DEBUG_PREPROCESSOR != 0 #define LOG_OP(...) os_printf(__VA_ARGS__) #else -#define LOG_OP(...) +#define LOG_OP(...) (void)0 #endif #define PATCH_ELSE 0 @@ -3611,7 +3584,7 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, #define CHECK_CSP_POP() do { \ if (ctx->csp_num < 1) { \ set_error_buf(error_buf, error_buf_size, \ - "WASM module load failed: type mismatch: " \ + "type mismatch: " \ "expect data but block stack was empty"); \ goto fail; \ } \ @@ -3698,7 +3671,6 @@ check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) && stack_cell_num < 2)) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "type mismatch: expect data but stack was empty"); return false; } @@ -3711,10 +3683,10 @@ check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type, || (type == VALUE_TYPE_F64 && (*(frame_ref - 2) != REF_F64_1 || *(frame_ref - 1) != REF_F64_2))) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s%s%s", - "WASM module load failed: type mismatch: expect ", - type_str[type - VALUE_TYPE_F64], " but got other"); + set_error_buf_v(error_buf, error_buf_size, "%s%s%s", + "type mismatch: expect ", + type_str[type - VALUE_TYPE_F64], + " but got other"); return false; } @@ -3949,7 +3921,6 @@ fail: int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \ if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \ set_error_buf(error_buf, error_buf_size, \ - "WASM module load failed: " \ "pre-compiled label offset out of range"); \ goto fail; \ } \ @@ -4892,8 +4863,7 @@ fail: read_leb_uint32(p, p_end, local_idx); \ if (local_idx >= param_count + local_count) { \ set_error_buf(error_buf, error_buf_size, \ - "WASM module load failed: " \ - "unknown local."); \ + "unknown local"); \ goto fail; \ } \ local_type = local_idx < param_count \ @@ -4914,8 +4884,7 @@ check_memory(WASMModule *module, { if (module->memory_count == 0 && module->import_memory_count == 0) { - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: unknown memory"); + set_error_buf(error_buf, error_buf_size, "unknown memory"); return false; } return true; @@ -4997,7 +4966,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, if (loader_ctx->csp_num < depth + 1) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: unknown label, " + "unknown label, " "unexpected end of section or function"); return false; } @@ -5110,7 +5079,6 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, /* Check stack is empty */ if (loader_ctx->stack_cell_num != block->stack_cell_num) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "type mismatch: stack size does not match block type"); goto fail; } @@ -5129,7 +5097,6 @@ check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block, /* Check stack cell num equals return cell num */ if (available_stack_cell != return_cell_num) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "type mismatch: stack size does not match block type"); goto fail; } @@ -5324,7 +5291,6 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, if (!(loader_ctx = wasm_loader_ctx_init(func))) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "allocate memory failed"); goto fail; } @@ -5334,7 +5300,6 @@ re_scan: if (loader_ctx->code_compiled_size > 0) { if (!wasm_loader_ctx_reinit(loader_ctx)) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "allocate memory failed"); goto fail; } @@ -5390,7 +5355,6 @@ handle_op_block_and_loop: read_leb_uint32(p, p_end, type_index); if (type_index >= module->type_count) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "unknown type"); goto fail; } @@ -5502,7 +5466,6 @@ handle_op_block_and_loop: if (loader_ctx->csp_num < 2 || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "opcode else found without matched opcode if"); goto fail; } @@ -5576,7 +5539,6 @@ handle_op_block_and_loop: if (param_count != ret_count || (param_count && memcmp(param_types, ret_types, param_count))) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " "type mismatch: else branch missing"); goto fail; } @@ -5679,7 +5641,6 @@ handle_op_block_and_loop: || (ret_count && 0 != memcmp(ret_types, tmp_ret_types, ret_count))) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "type mismatch: br_table targets must " "all use same result type"); goto fail; @@ -5726,8 +5687,7 @@ handle_op_block_and_loop: if (func_idx >= module->import_function_count + module->function_count) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " - "unknown function."); + "unknown function"); goto fail; } @@ -5770,7 +5730,6 @@ handle_op_block_and_loop: if (module->table_count == 0 && module->import_table_count == 0) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "call indirect with unknown table"); goto fail; } @@ -5784,7 +5743,6 @@ handle_op_block_and_loop: /* reserved byte 0x00 */ if (*p++ != 0x00) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "zero flag expected"); goto fail; } @@ -5793,7 +5751,6 @@ handle_op_block_and_loop: if (type_idx >= module->type_count) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "unknown type"); goto fail; } @@ -5830,7 +5787,6 @@ handle_op_block_and_loop: if (available_stack_cell <= 0 && !cur_block->is_stack_polymorphic) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "type mismatch, opcode drop was found " "but stack was empty"); goto fail; @@ -5887,7 +5843,6 @@ handle_op_block_and_loop: if (available_stack_cell <= 0 && !cur_block->is_stack_polymorphic) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "type mismatch, opcode select was found " "but stack was empty"); goto fail; @@ -6094,8 +6049,7 @@ handle_op_block_and_loop: read_leb_uint32(p, p_end, global_idx); if (global_idx >= global_count) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " - "unknown global."); + "unknown global"); goto fail; } @@ -6134,8 +6088,7 @@ handle_op_block_and_loop: read_leb_uint32(p, p_end, global_idx); if (global_idx >= global_count) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " - "unknown global."); + "unknown global"); goto fail; } @@ -6145,8 +6098,7 @@ handle_op_block_and_loop: : module->globals[global_idx - module->import_global_count] .is_mutable; if (!is_mutable) { - set_error_buf(error_buf, - error_buf_size, + set_error_buf(error_buf, error_buf_size, "global is immutable"); goto fail; } @@ -6300,7 +6252,6 @@ handle_op_block_and_loop: /* reserved byte 0x00 */ if (*p++ != 0x00) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "zero flag expected"); goto fail; } @@ -6314,7 +6265,6 @@ handle_op_block_and_loop: /* reserved byte 0x00 */ if (*p++ != 0x00) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "zero flag expected"); goto fail; } @@ -6632,10 +6582,8 @@ handle_op_block_and_loop: goto fail_zero_flag_expected; if (segment_index >= module->data_seg_count) { - char msg[128]; - snprintf(msg, 128, "WASM loader prepare bytecode failed: " - "unknown data segment %d", segment_index); - set_error_buf(error_buf, error_buf_size, msg); + set_error_buf(error_buf, error_buf_size, + "unknown data segment"); goto fail; } @@ -6653,7 +6601,6 @@ handle_op_block_and_loop: #endif if (segment_index >= module->data_seg_count) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "unknown data segment"); goto fail; } @@ -6689,29 +6636,24 @@ handle_op_block_and_loop: break; fail_zero_flag_expected: set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "zero flag expected"); goto fail; fail_unknown_memory: set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "unknown memory 0"); goto fail; fail_data_cnt_sec_require: set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "data count section required"); goto fail; /* TODO: to support bulk table operation */ #endif /* WASM_ENABLE_BULK_MEMORY */ default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "WASM module load failed: " - "invalid opcode 0xfc %02x.", opcode1); + set_error_buf_v(error_buf, error_buf_size, + "%s %02x %02x", + "unsupported opcode", 0xfc, opcode1); goto fail; - break; } break; } @@ -6755,7 +6697,6 @@ fail_data_cnt_sec_require: /* reserved byte 0x00 */ if (*p++ != 0x00) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "zero flag expected"); goto fail; } @@ -6850,20 +6791,18 @@ fail_data_cnt_sec_require: PUSH_I64(); break; default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "WASM module load failed: " - "invalid opcode 0xfe %02x.", opcode); + set_error_buf_v(error_buf, error_buf_size, + "%s %02x %02x", + "unsupported opcode", 0xfe, opcode); goto fail; } break; } #endif /* end of WASM_ENABLE_SHARED_MEMORY */ default: - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, - "WASM module load failed: " - "invalid opcode %02x.", opcode); + set_error_buf_v(error_buf, error_buf_size, + "%s %02x", + "unsupported opcode", opcode); goto fail; } @@ -6874,8 +6813,7 @@ fail_data_cnt_sec_require: if (loader_ctx->csp_num > 0) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " - "function body must end with END opcode."); + "function body must end with END opcode"); goto fail; } diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index a56c2a382..d1541a68d 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -21,7 +21,8 @@ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); + snprintf(error_buf, error_buf_size, + "WASM module load failed: %s", string); } #define CHECK_BUF(buf, buf_end, length) do { \ @@ -168,8 +169,7 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size) if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " - "allocate memory failed."); + "allocate memory failed"); return NULL; } @@ -378,8 +378,7 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module, is_built_in_module = wasm_runtime_is_built_in_module(sub_module_name); if (is_built_in_module) { LOG_DEBUG("%s is a function of a built-in module %s", - function_name, - sub_module_name); + function_name, sub_module_name); /* check built-in modules */ linked_func = wasm_native_resolve_symbol(sub_module_name, function_name, @@ -391,15 +390,13 @@ load_function_import(const WASMModule *parent_module, WASMModule *sub_module, if (!linked_func) { #if WASM_ENABLE_SPEC_TEST != 0 - set_error_buf(error_buf, - error_buf_size, + set_error_buf(error_buf, error_buf_size, "unknown import or incompatible import type"); return false; #else #if WASM_ENABLE_WAMR_COMPILER == 0 - LOG_WARNING( - "warning: fail to link import function (%s, %s)", - sub_module_name, function_name); + LOG_WARNING("warning: fail to link import function (%s, %s)", + sub_module_name, function_name); #endif #endif } @@ -538,8 +535,8 @@ load_global_import(const WASMModule *parent_module, #endif /* WASM_ENABLE_LIBC_BUILTIN */ if (!ret) { - set_error_buf_v(error_buf, error_buf_size, - "unknown import or incompatible import type"); + set_error_buf(error_buf, error_buf_size, + "unknown import or incompatible import type"); return false; } @@ -597,6 +594,7 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory, p_org = p; read_leb_uint32(p, p_end, memory->flags); bh_assert(p - p_org <= 1); + (void)p_org; #if WASM_ENABLE_SHARED_MEMORY == 0 bh_assert(memory->flags <= 1); #else @@ -1536,7 +1534,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, #endif default: set_error_buf(error_buf, error_buf_size, - "WASM module load failed: invalid section id"); + "invalid section id"); return false; } @@ -1934,8 +1932,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module, exchange32((uint8*)&version); if (version != WASM_CURRENT_VERSION) { - set_error_buf(error_buf, error_buf_size, - "WASM module load failed: unknown binary version"); + set_error_buf(error_buf, error_buf_size, "unknown binary version"); return false; } @@ -1959,7 +1956,6 @@ wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_bu } if (!load(buf, size, module, error_buf, error_buf_size)) { - LOG_VERBOSE("Load module failed, %s", error_buf); goto fail; } @@ -2459,7 +2455,7 @@ wasm_loader_find_block_addr(BlockAddr *block_addr_cache, #if WASM_DEBUG_PREPROCESSOR != 0 #define LOG_OP(...) os_printf(__VA_ARGS__) #else -#define LOG_OP(...) +#define LOG_OP(...) (void)0 #endif #define PATCH_ELSE 0 @@ -3878,7 +3874,7 @@ wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, if (loader_ctx->csp_num < depth + 1) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: unknown label, " + "unknown label, " "unexpected end of section or function"); return false; } @@ -4196,7 +4192,6 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func, if (!(loader_ctx = wasm_loader_ctx_init(func))) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "allocate memory failed"); goto fail; } @@ -4206,7 +4201,6 @@ re_scan: if (loader_ctx->code_compiled_size > 0) { if (!wasm_loader_ctx_reinit(loader_ctx)) { set_error_buf(error_buf, error_buf_size, - "WASM loader prepare bytecode failed: " "allocate memory failed"); goto fail; } @@ -5604,8 +5598,7 @@ handle_op_block_and_loop: if (loader_ctx->csp_num > 0) { set_error_buf(error_buf, error_buf_size, - "WASM module load failed: " - "function body must end with END opcode."); + "function body must end with END opcode"); goto fail; } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 9e1c427cc..d89e68e4b 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -17,8 +17,10 @@ static void set_error_buf(char *error_buf, uint32 error_buf_size, const char *string) { - if (error_buf != NULL) - snprintf(error_buf, error_buf_size, "%s", string); + if (error_buf != NULL) { + snprintf(error_buf, error_buf_size, + "WASM module instantiate failed: %s", string); + } } WASMModule* @@ -50,8 +52,7 @@ runtime_malloc(uint64 size, char *error_buf, uint32 error_buf_size) if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) { set_error_buf(error_buf, error_buf_size, - "WASM module instantiate failed: " - "allocate memory failed."); + "allocate memory failed"); return NULL; } @@ -262,17 +263,13 @@ memory_instantiate(WASMModuleInstance *module_inst, if (heap_size > 0 && !(memory->heap_handle = mem_allocator_create(memory->heap_data, heap_size))) { - set_error_buf(error_buf, error_buf_size, - "Instantiate memory failed: " - "init app heap failed."); + set_error_buf(error_buf, error_buf_size, "init app heap failed"); goto fail1; } #if WASM_ENABLE_SHARED_MEMORY != 0 if (0 != os_mutex_init(&memory->mem_lock)) { - set_error_buf(error_buf, error_buf_size, - "Instantiate memory failed: " - "init mutex failed."); + set_error_buf(error_buf, error_buf_size, "init mutex failed"); goto fail2; } if (is_shared_memory) { @@ -281,8 +278,7 @@ memory_instantiate(WASMModuleInstance *module_inst, (WASMModuleCommon *)module_inst->module, (WASMMemoryInstanceCommon *)memory)) { set_error_buf(error_buf, error_buf_size, - "Instantiate memory failed: " - "allocate memory failed."); + "allocate memory failed"); goto fail3; } } @@ -735,8 +731,7 @@ globals_instantiate(const WASMModule *module, sub_module_inst->globals, sub_module_inst->global_count, &(global->initial_value)); if (!ret) { - set_error_buf(error_buf, error_buf_size, - "Instantiate global failed: unknown global."); + set_error_buf(error_buf, error_buf_size, "unknown global"); return NULL; } } @@ -774,8 +769,7 @@ globals_instantiate(const WASMModule *module, parse_init_expr(init_expr, globals, global_count, &(global->initial_value)); if (!ret) { - set_error_buf(error_buf, error_buf_size, - "Instantiate global failed: unknown global."); + set_error_buf(error_buf, error_buf_size, "unknown global"); return NULL; } } @@ -811,8 +805,7 @@ globals_instantiate_fix(WASMGlobalInstance *globals, ret = parse_init_expr(init_expr, globals, global_count, &global->initial_value); if (!ret) { - set_error_buf(error_buf, error_buf_size, - "Instantiate global failed: unknown global."); + set_error_buf(error_buf, error_buf_size, "unknown global"); return false; } } @@ -1024,18 +1017,18 @@ sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst, bh_list_first_elem(module->import_module_list); while (sub_module_list_node) { + WASMSubModInstNode *sub_module_inst_list_node; WASMModule *sub_module = (WASMModule*)sub_module_list_node->module; - WASMModuleInstance *sub_module_inst = wasm_instantiate( - sub_module, false, stack_size, heap_size, error_buf, error_buf_size); + WASMModuleInstance *sub_module_inst = + wasm_instantiate(sub_module, false, stack_size, heap_size, + error_buf, error_buf_size); if (!sub_module_inst) { LOG_DEBUG("instantiate %s failed", sub_module_list_node->module_name); - set_error_buf_v(error_buf, error_buf_size, "instantiate %s failed", - sub_module_list_node->module_name); return false; } - WASMSubModInstNode *sub_module_inst_list_node = runtime_malloc + sub_module_inst_list_node = runtime_malloc (sizeof(WASMSubModInstNode), error_buf, error_buf_size); if (!sub_module_inst_list_node) { LOG_DEBUG("Malloc WASMSubModInstNode failed, SZ:%d", @@ -1102,7 +1095,6 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, error_buf, error_buf_size))) { return NULL; } - memset(module_inst, 0, (uint32)sizeof(WASMModuleInstance)); module_inst->module = module; @@ -1264,7 +1256,7 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, LOG_DEBUG("base_offset(%d) > memory_size(%d)", base_offset, memory_size); set_error_buf(error_buf, error_buf_size, - "data segment does not fit."); + "data segment does not fit"); wasm_deinstantiate(module_inst, false); return NULL; } @@ -1274,9 +1266,8 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, if (base_offset + length > memory_size) { LOG_DEBUG("base_offset(%d) + length(%d) > memory_size(%d)", base_offset, length, memory_size); - set_error_buf( - error_buf, error_buf_size, - "Instantiate module failed: data segment does not fit."); + set_error_buf(error_buf, error_buf_size, + "data segment does not fit"); wasm_deinstantiate(module_inst, false); return NULL; } @@ -1538,7 +1529,7 @@ wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst, if (!(exec_env = wasm_exec_env_create( (WASMModuleInstanceCommon*)module_inst, module_inst->default_wasm_stack_size))) { - wasm_set_exception(module_inst, "allocate memory failed."); + wasm_set_exception(module_inst, "allocate memory failed"); return false; } @@ -1764,12 +1755,10 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) if (total_page_count < memory->cur_page_count /* integer overflow */ || total_page_count > memory->max_page_count) { - wasm_set_exception(module, "fail to enlarge memory."); return false; } if (total_size >= UINT32_MAX) { - wasm_set_exception(module, "fail to enlarge memory."); return false; } @@ -1793,7 +1782,6 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) /* Restore heap's lock if memory re-alloc failed */ mem_allocator_reinit_lock(memory->heap_handle); } - wasm_set_exception(module, "fail to enlarge memory."); return false; } bh_memcpy_s((uint8 *)new_memory, (uint32)total_size, @@ -1809,7 +1797,6 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) ((uint8 *)new_memory - (uint8 *)memory); if (mem_allocator_migrate(new_memory->heap_handle, heap_handle_old) != 0) { - wasm_set_exception(module, "fail to enlarge memory."); return false; } }