Refactor error/exception strings to reduce binary size (#359)

This commit is contained in:
Wenyong Huang
2020-08-21 15:11:31 +08:00
committed by GitHub
parent 89d2937cde
commit 0103f6429c
10 changed files with 321 additions and 505 deletions

View File

@ -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));
}

View File

@ -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 {

File diff suppressed because it is too large Load Diff

View File

@ -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;
}

View File

@ -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;
}
}