From b73edc8587861f6ec9ac4b01f914f7b693701b80 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 3 Dec 2021 15:30:32 +0800 Subject: [PATCH 1/2] Refine code when aux stack global isn't found (#862) When auxiliary stack global isn't found in wasm app, it must be unused in the wasm app, we set it to __heap_base global and set its size to 0, so as to shrink the linear memory to reduce the memory consumption. --- core/iwasm/interpreter/wasm_loader.c | 14 +++++++++++++- core/iwasm/interpreter/wasm_mini_loader.c | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 286a7aa572..fdbefcab71 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -3040,6 +3040,18 @@ load_from_sections(WASMModule *module, WASMSection *sections, break; } } + if (!aux_stack_top_global) { + /* Auxiliary stack global isn't found, it must be unused + in the wasm app, as if it is used, the global must be + defined. Here we set it to __heap_base global and set + its size to 0. */ + aux_stack_top_global = aux_heap_base_global; + aux_stack_top = aux_heap_base; + module->aux_stack_top_global_index = + module->aux_heap_base_global_index; + module->aux_stack_bottom = aux_stack_top; + module->aux_stack_size = 0; + } break; } } @@ -3084,7 +3096,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, export->name, export->index); /* resolve retain function. - If not find, reset malloc function index */ + If not found, reset malloc function index */ export_tmp = module->exports; for (j = 0; j < module->export_count; j++, export_tmp++) { if ((export_tmp->kind == EXPORT_KIND_FUNC) diff --git a/core/iwasm/interpreter/wasm_mini_loader.c b/core/iwasm/interpreter/wasm_mini_loader.c index 0565188c31..4a43b068f5 100644 --- a/core/iwasm/interpreter/wasm_mini_loader.c +++ b/core/iwasm/interpreter/wasm_mini_loader.c @@ -1959,6 +1959,18 @@ load_from_sections(WASMModule *module, WASMSection *sections, break; } } + if (!aux_stack_top_global) { + /* Auxiliary stack global isn't found, it must be unused + in the wasm app, as if it is used, the global must be + defined. Here we set it to __heap_base global and set + its size to 0. */ + aux_stack_top_global = aux_heap_base_global; + aux_stack_top = aux_heap_base; + module->aux_stack_top_global_index = + module->aux_heap_base_global_index; + module->aux_stack_bottom = aux_stack_top; + module->aux_stack_size = 0; + } break; } } @@ -2002,7 +2014,7 @@ load_from_sections(WASMModule *module, WASMSection *sections, export->name, export->index); /* resolve retain function. - If not find, reset malloc function index */ + If not found, reset malloc function index */ export_tmp = module->exports; for (j = 0; j < module->export_count; j++, export_tmp++) { if ((export_tmp->kind == EXPORT_KIND_FUNC) From c8fe1004aab482c8e5c2061a45079540e6ff5d08 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 3 Dec 2021 17:00:18 +0800 Subject: [PATCH 2/2] Don't throw exception while module_malloc failed (#860) Don't throw exception when module_malloc memory failed: - Exception will terminate the wasm app, it's not necessary since app can check the result of dynamic allocation and do some cleanup or fallback operation on failure instead of 'crash' directly. - In acquire_wait_info, call hasn_map_find only when the address isn't NULL, or there are many senseless error logs --- core/iwasm/aot/aot_runtime.c | 2 +- core/iwasm/common/wasm_shared_memory.c | 3 ++- core/iwasm/interpreter/wasm_runtime.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index b97e952832..c769a527e1 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1774,7 +1774,7 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, aot_set_exception(module_inst, "app heap corrupted"); } else { - aot_set_exception(module_inst, "out of memory"); + LOG_WARNING("warning: allocate %u bytes memory failed", size); } return 0; } diff --git a/core/iwasm/common/wasm_shared_memory.c b/core/iwasm/common/wasm_shared_memory.c index 65c98d4ef3..6fc9bf2073 100644 --- a/core/iwasm/common/wasm_shared_memory.c +++ b/core/iwasm/common/wasm_shared_memory.c @@ -224,7 +224,8 @@ acquire_wait_info(void *address, bool create) AtomicWaitInfo *wait_info = NULL; bh_list_status ret; - wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address); + if (address) + wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address); if (!create) return wait_info; diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 14a4286490..76b4184b17 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -1830,7 +1830,7 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size, wasm_set_exception(module_inst, "app heap corrupted"); } else { - wasm_set_exception(module_inst, "out of memory"); + LOG_WARNING("warning: allocate %u bytes memory failed", size); } return 0; }