diff --git a/core/iwasm/interpreter/wasm_interp.h b/core/iwasm/interpreter/wasm_interp.h index c3c2c1b3..5f867249 100644 --- a/core/iwasm/interpreter/wasm_interp.h +++ b/core/iwasm/interpreter/wasm_interp.h @@ -74,6 +74,11 @@ wasm_interp_call_wasm(struct WASMModuleInstance *module_inst, struct WASMFunctionInstance *function, uint32 argc, uint32 argv[]); +#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 +void +wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env); +#endif + #ifdef __cplusplus } #endif diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index a241f19b..e18235d8 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -3379,6 +3379,9 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, } } else { +#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 + wasm_interp_dump_call_stack(exec_env); +#endif 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 c36567f6..bbb39a38 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -3382,6 +3382,11 @@ wasm_interp_call_wasm(WASMModuleInstance *module_inst, for (i = 0; i < function->ret_cell_num; i++) argv[i] = *(frame->lp + i); } + else { +#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 + wasm_interp_dump_call_stack(exec_env); +#endif + } wasm_exec_env_set_cur_frame(exec_env, prev_frame); FREE_FRAME(exec_env, frame); diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 4610065d..8a1e66fc 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -85,7 +85,7 @@ memories_deinstantiate(WASMModuleInstance *module_inst, { uint32 i; if (memories) { - for (i = 0; i < count; i++) + for (i = 0; i < count; i++) { if (memories[i]) { #if WASM_ENABLE_MULTI_MODULE != 0 if (memories[i]->owner != module_inst) @@ -109,8 +109,10 @@ memories_deinstantiate(WASMModuleInstance *module_inst, mem_allocator_destroy(memories[i]->heap_handle); memories[i]->heap_handle = NULL; } + wasm_runtime_free(memories[i]->memory_data); wasm_runtime_free(memories[i]); } + } wasm_runtime_free(memories); } (void)module_inst; @@ -125,7 +127,7 @@ memory_instantiate(WASMModuleInstance *module_inst, { WASMModule *module = module_inst->module; WASMMemoryInstance *memory; - uint64 total_size, memory_data_size; + uint64 memory_data_size; uint32 heap_offset = num_bytes_per_page * init_page_count; uint32 inc_page_count, aux_heap_base, global_idx; uint32 bytes_of_last_page, bytes_to_page_end; @@ -239,15 +241,17 @@ memory_instantiate(WASMModuleInstance *module_inst, } #endif - total_size = offsetof(WASMMemoryInstance, memory_data) - + memory_data_size; - /* Allocate memory space, addr data and global data */ - if (!(memory = runtime_malloc(total_size, + if (!(memory = runtime_malloc((uint64)sizeof(WASMMemoryInstance), error_buf, error_buf_size))) { return NULL; } + if (!(memory->memory_data = + runtime_malloc(memory_data_size, error_buf, error_buf_size))) { + goto fail1; + } + memory->module_type = Wasm_Module_Bytecode; memory->num_bytes_per_page = num_bytes_per_page; memory->cur_page_count = init_page_count; @@ -257,21 +261,18 @@ memory_instantiate(WASMModuleInstance *module_inst, memory->heap_data_end = memory->heap_data + heap_size; memory->memory_data_end = memory->memory_data + (uint32)memory_data_size; - bh_assert((uint32)(memory->memory_data_end - (uint8*)memory) - == (uint32)total_size); - /* Initialize heap */ if (heap_size > 0 && !(memory->heap_handle = mem_allocator_create(memory->heap_data, heap_size))) { set_error_buf(error_buf, error_buf_size, "init app heap failed"); - goto fail1; + goto fail2; } #if WASM_ENABLE_SHARED_MEMORY != 0 if (0 != os_mutex_init(&memory->mem_lock)) { set_error_buf(error_buf, error_buf_size, "init mutex failed"); - goto fail2; + goto fail3; } if (is_shared_memory) { memory->is_shared = true; @@ -280,18 +281,20 @@ memory_instantiate(WASMModuleInstance *module_inst, (WASMMemoryInstanceCommon *)memory)) { set_error_buf(error_buf, error_buf_size, "allocate memory failed"); - goto fail3; + goto fail4; } } #endif return memory; #if WASM_ENABLE_SHARED_MEMORY != 0 -fail3: +fail4: os_mutex_destroy(&memory->mem_lock); -fail2: +fail3: if (heap_size > 0) mem_allocator_destroy(memory->heap_handle); #endif +fail2: + wasm_runtime_free(memory->memory_data); fail1: wasm_runtime_free(memory); return NULL; @@ -1760,12 +1763,12 @@ wasm_get_native_addr_range(WASMModuleInstance *module_inst, bool wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) { - WASMMemoryInstance *memory = module->default_memory, *new_memory; + WASMMemoryInstance *memory = module->default_memory; + uint8 *new_memory_data, *memory_data = memory->memory_data; uint32 heap_size = memory->heap_data_end - memory->heap_data; - uint32 total_size_old = memory->memory_data_end - (uint8 *)memory; + uint32 total_size_old = memory->memory_data_end - memory_data; uint32 total_page_count = inc_page_count + memory->cur_page_count; - uint64 total_size = offsetof(WASMMemoryInstance, memory_data) - + memory->num_bytes_per_page * (uint64)total_page_count; + uint64 total_size = memory->num_bytes_per_page * (uint64)total_page_count; void *heap_handle_old = memory->heap_handle; uint8 *heap_data_old = memory->heap_data; @@ -1796,40 +1799,39 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) we cannot access its lock again. */ mem_allocator_destroy_lock(memory->heap_handle); } - if (!(new_memory = wasm_runtime_realloc(memory, (uint32)total_size))) { - if (!(new_memory = wasm_runtime_malloc((uint32)total_size))) { + if (!(new_memory_data = wasm_runtime_realloc(memory_data, (uint32)total_size))) { + if (!(new_memory_data = wasm_runtime_malloc((uint32)total_size))) { if (heap_size > 0) { /* Restore heap's lock if memory re-alloc failed */ mem_allocator_reinit_lock(memory->heap_handle); } return false; } - bh_memcpy_s((uint8 *)new_memory, (uint32)total_size, - (uint8 *)memory, total_size_old); - wasm_runtime_free(memory); + bh_memcpy_s(new_memory_data, (uint32)total_size, + memory_data, total_size_old); + wasm_runtime_free(memory_data); } - memset((uint8 *)new_memory + total_size_old, + memset(new_memory_data + total_size_old, 0, (uint32)total_size - total_size_old); if (heap_size > 0) { - new_memory->heap_handle = (uint8 *)heap_handle_old + - ((uint8 *)new_memory - (uint8 *)memory); - if (mem_allocator_migrate(new_memory->heap_handle, + memory->heap_handle = (uint8 *)heap_handle_old + + (new_memory_data - memory_data); + if (mem_allocator_migrate(memory->heap_handle, heap_handle_old) != 0) { return false; } } - new_memory->cur_page_count = total_page_count; - new_memory->heap_data = heap_data_old + - ((uint8 *)new_memory - (uint8 *)memory); - new_memory->heap_data_end = new_memory->heap_data + heap_size; - new_memory->memory_data_end = new_memory->memory_data - + new_memory->num_bytes_per_page - * total_page_count; + memory->memory_data = new_memory_data; + memory->cur_page_count = total_page_count; + memory->heap_data = heap_data_old + (new_memory_data - memory_data); + memory->heap_data_end = memory->heap_data + heap_size; + memory->memory_data_end = memory->memory_data + + memory->num_bytes_per_page + * total_page_count; - module->memories[0] = module->default_memory = new_memory; return true; } @@ -2039,7 +2041,7 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst, * module_inst->memory_count; for (i = 0; i < module_inst->memory_count; i++) { WASMMemoryInstance *memory = module_inst->memories[i]; - size = offsetof(WASMMemoryInstance, memory_data) + size = sizeof(WASMMemoryInstance) + memory->num_bytes_per_page * memory->cur_page_count; mem_conspn->memories_size += size; mem_conspn->app_heap_size += memory->heap_data_end @@ -2079,3 +2081,40 @@ wasm_get_module_inst_mem_consumption(const WASMModuleInstance *module_inst, } #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0) */ + +#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0 +void +wasm_interp_dump_call_stack(struct WASMExecEnv *exec_env) +{ + WASMModuleInstance *module_inst = + (WASMModuleInstance *)wasm_exec_env_get_module_inst(exec_env); + WASMInterpFrame *cur_frame = + wasm_exec_env_get_cur_frame(exec_env); + WASMFunctionInstance *func_inst; + const char *func_name = NULL; + uint32 n; + + os_printf("\n"); + for (n = 0; cur_frame && cur_frame->function; n++) { + func_inst = cur_frame->function; + + if (func_inst->is_import_func) { + func_name = func_inst->u.func_import->field_name; + } + else { + func_name = func_inst->u.func->field_name; + } + + /* function name not exported, print number instead */ + if (func_name == NULL) { + os_printf("#%02d $f%d \n", n, func_inst - module_inst->functions); + } + else { + os_printf("#%02d %s \n", n, func_name); + } + + cur_frame = cur_frame->prev_frame; + } + os_printf("\n"); +} +#endif /* end of WASM_ENABLE_CUSTOM_NAME_SECTION */ diff --git a/core/iwasm/interpreter/wasm_runtime.h b/core/iwasm/interpreter/wasm_runtime.h index 85243102..242d5f12 100644 --- a/core/iwasm/interpreter/wasm_runtime.h +++ b/core/iwasm/interpreter/wasm_runtime.h @@ -56,7 +56,7 @@ struct WASMMemoryInstance { /* Memory data begin address, the layout is: memory data + heap data Note: when memory is re-allocated, the heap data and memory data must be copied to new memory also. */ - uint8 memory_data[1]; + uint8 *memory_data; }; struct WASMTableInstance { diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 08c0930e..659f3d83 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -48,6 +48,8 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM - **WAMR_BUILD_CUSTOM_NAME_SECTION**=1/0, load the function name from custom name section, default to disable if not set +> Note: if it is enabled, the call stack will be dumped when exception occurs. + #### **Enable Multi-Module feature** - **WAMR_BUILD_MULTI_MODULE**=1/0, default to disable if not set