From 28125ec538c86de90d2b9be332d50760e7dd42bc Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 3 Aug 2023 09:46:56 +0900 Subject: [PATCH] Move wasm_runtime_destroy_wasi and wasi_nn_destroy calls together (#2418) And remove obsolete comment. --- core/iwasm/aot/aot_runtime.c | 16 +++++----------- core/iwasm/interpreter/wasm_runtime.c | 16 +++++----------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/core/iwasm/aot/aot_runtime.c b/core/iwasm/aot/aot_runtime.c index e0d56a69..a7d6ef2d 100644 --- a/core/iwasm/aot/aot_runtime.c +++ b/core/iwasm/aot/aot_runtime.c @@ -1264,16 +1264,6 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton); } -#if WASM_ENABLE_LIBC_WASI != 0 - /* Destroy wasi resource before freeing app heap, since some fields of - wasi contex are allocated from app heap, and if app heap is freed, - these fields will be set to NULL, we cannot free their internal data - which may allocated from global heap. */ - /* Only destroy wasi ctx in the main module instance */ - if (!is_sub_inst) - wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); -#endif - #if WASM_ENABLE_PERF_PROFILING != 0 if (module_inst->func_perf_profilings) wasm_runtime_free(module_inst->func_perf_profilings); @@ -1306,10 +1296,14 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst) wasm_runtime_free( ((AOTModuleInstanceExtra *)module_inst->e)->c_api_func_imports); + if (!is_sub_inst) { +#if WASM_ENABLE_LIBC_WASI != 0 + wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); +#endif #if WASM_ENABLE_WASI_NN != 0 - if (!is_sub_inst) wasi_nn_destroy(module_inst); #endif + } wasm_runtime_free(module_inst); } diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 84155a05..497e6b19 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -2216,16 +2216,6 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) sub_module_deinstantiate(module_inst); #endif -#if WASM_ENABLE_LIBC_WASI != 0 - /* Destroy wasi resource before freeing app heap, since some fields of - wasi contex are allocated from app heap, and if app heap is freed, - these fields will be set to NULL, we cannot free their internal data - which may allocated from global heap. */ - /* Only destroy wasi ctx in the main module instance */ - if (!is_sub_inst) - wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); -#endif - if (module_inst->memory_count > 0) memories_deinstantiate(module_inst, module_inst->memories, module_inst->memory_count); @@ -2258,10 +2248,14 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst) if (module_inst->e->c_api_func_imports) wasm_runtime_free(module_inst->e->c_api_func_imports); + if (!is_sub_inst) { +#if WASM_ENABLE_LIBC_WASI != 0 + wasm_runtime_destroy_wasi((WASMModuleInstanceCommon *)module_inst); +#endif #if WASM_ENABLE_WASI_NN != 0 - if (!is_sub_inst) wasi_nn_destroy(module_inst); #endif + } wasm_runtime_free(module_inst); }