mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-09 13:16:26 +00:00
Fix module_malloc/module_free issues (#2072)
Try using existing exec_env to execute wasm app's malloc/free func and execute post instantiation functions. Create a new exec_env only when no existing exec_env was found.
This commit is contained in:
parent
a35d39b353
commit
10f1bf3af7
|
@ -932,7 +932,7 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
|
|||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = NULL;
|
||||
#endif
|
||||
WASMExecEnv *exec_env = NULL;
|
||||
WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
|
||||
bool ret = false;
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
|
@ -990,13 +990,31 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
|
|||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
else {
|
||||
if (!(exec_env =
|
||||
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
||||
/* Try using the existing exec_env */
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
exec_env = exec_env_tls;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
if (!exec_env)
|
||||
exec_env = wasm_clusters_search_exec_env(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
if (!exec_env) {
|
||||
if (!(exec_env = exec_env_created = wasm_exec_env_create(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module_inst->default_wasm_stack_size))) {
|
||||
aot_set_exception(module_inst, "allocate memory failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Temporarily replace exec_env's module inst with current
|
||||
module inst to ensure that the exec_env's module inst
|
||||
is the correct one. */
|
||||
module_inst_main = exec_env->module_inst;
|
||||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
}
|
||||
|
||||
/* Execute start function for both main insance and sub instance */
|
||||
if (module->start_function) {
|
||||
|
@ -1033,11 +1051,17 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
|
|||
ret = true;
|
||||
|
||||
fail:
|
||||
if (is_sub_inst)
|
||||
if (is_sub_inst) {
|
||||
/* Restore the parent exec_env's module inst */
|
||||
exec_env_main->module_inst = module_inst_main;
|
||||
else
|
||||
wasm_exec_env_destroy(exec_env);
|
||||
}
|
||||
else {
|
||||
if (module_inst_main)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
exec_env->module_inst = module_inst_main;
|
||||
if (exec_env_created)
|
||||
wasm_exec_env_destroy(exec_env_created);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -1596,6 +1620,8 @@ execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[2], argc;
|
||||
bool ret;
|
||||
|
||||
|
@ -1616,19 +1642,43 @@ execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
== (WASMModuleInstanceCommon *)module_inst);
|
||||
}
|
||||
else {
|
||||
if (!(exec_env =
|
||||
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
||||
/* Try using the existing exec_env */
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
exec_env = exec_env_tls;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
if (!exec_env)
|
||||
exec_env = wasm_clusters_search_exec_env(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
if (!exec_env) {
|
||||
if (!(exec_env = exec_env_created = wasm_exec_env_create(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module_inst->default_wasm_stack_size))) {
|
||||
wasm_set_exception(module_inst, "allocate memory failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Temporarily replace exec_env's module inst with current
|
||||
module inst to ensure that the exec_env's module inst
|
||||
is the correct one. */
|
||||
module_inst_old = exec_env->module_inst;
|
||||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
}
|
||||
|
||||
ret = aot_call_function(exec_env, malloc_func, argc, argv);
|
||||
|
||||
if (retain_func && ret) {
|
||||
if (retain_func && ret)
|
||||
ret = aot_call_function(exec_env, retain_func, 1, argv);
|
||||
}
|
||||
|
||||
if (module_inst_old)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
exec_env->module_inst = module_inst_old;
|
||||
|
||||
if (exec_env_created)
|
||||
wasm_exec_env_destroy(exec_env_created);
|
||||
|
||||
if (ret)
|
||||
*p_result = argv[0];
|
||||
|
@ -1642,7 +1692,10 @@ execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[2];
|
||||
bool ret;
|
||||
|
||||
argv[0] = offset;
|
||||
|
||||
|
@ -1656,15 +1709,42 @@ execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
== (WASMModuleInstanceCommon *)module_inst);
|
||||
}
|
||||
else {
|
||||
if (!(exec_env =
|
||||
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
||||
/* Try using the existing exec_env */
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
exec_env = exec_env_tls;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
if (!exec_env)
|
||||
exec_env = wasm_clusters_search_exec_env(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
if (!exec_env) {
|
||||
if (!(exec_env = exec_env_created = wasm_exec_env_create(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module_inst->default_wasm_stack_size))) {
|
||||
wasm_set_exception(module_inst, "allocate memory failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Temporarily replace exec_env's module inst with current
|
||||
module inst to ensure that the exec_env's module inst
|
||||
is the correct one. */
|
||||
module_inst_old = exec_env->module_inst;
|
||||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
}
|
||||
|
||||
return aot_call_function(exec_env, free_func, 1, argv);
|
||||
ret = aot_call_function(exec_env, free_func, 1, argv);
|
||||
|
||||
if (module_inst_old)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
exec_env->module_inst = module_inst_old;
|
||||
|
||||
if (exec_env_created)
|
||||
wasm_exec_env_destroy(exec_env_created);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32
|
||||
|
|
|
@ -1016,7 +1016,7 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
|
|||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = NULL;
|
||||
#endif
|
||||
WASMExecEnv *exec_env = NULL;
|
||||
WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
|
||||
bool ret = false;
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
|
@ -1074,13 +1074,31 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
|
|||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
else {
|
||||
if (!(exec_env =
|
||||
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
||||
/* Try using the existing exec_env */
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
exec_env = exec_env_tls;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
if (!exec_env)
|
||||
exec_env = wasm_clusters_search_exec_env(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
if (!exec_env) {
|
||||
if (!(exec_env = exec_env_created = wasm_exec_env_create(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module_inst->default_wasm_stack_size))) {
|
||||
wasm_set_exception(module_inst, "allocate memory failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Temporarily replace exec_env's module inst with current
|
||||
module inst to ensure that the exec_env's module inst
|
||||
is the correct one. */
|
||||
module_inst_main = exec_env->module_inst;
|
||||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
}
|
||||
|
||||
/* Execute start function for both main insance and sub instance */
|
||||
if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) {
|
||||
|
@ -1105,11 +1123,17 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
|
|||
ret = true;
|
||||
|
||||
fail:
|
||||
if (is_sub_inst)
|
||||
if (is_sub_inst) {
|
||||
/* Restore the parent exec_env's module inst */
|
||||
exec_env_main->module_inst = module_inst_main;
|
||||
else
|
||||
wasm_exec_env_destroy(exec_env);
|
||||
}
|
||||
else {
|
||||
if (module_inst_main)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
exec_env->module_inst = module_inst_main;
|
||||
if (exec_env_created)
|
||||
wasm_exec_env_destroy(exec_env_created);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -1123,6 +1147,8 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[2], argc;
|
||||
bool ret;
|
||||
|
||||
|
@ -1151,19 +1177,43 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
== (WASMModuleInstanceCommon *)module_inst);
|
||||
}
|
||||
else {
|
||||
if (!(exec_env =
|
||||
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
||||
/* Try using the existing exec_env */
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
exec_env = exec_env_tls;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
if (!exec_env)
|
||||
exec_env = wasm_clusters_search_exec_env(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
if (!exec_env) {
|
||||
if (!(exec_env = exec_env_created = wasm_exec_env_create(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module_inst->default_wasm_stack_size))) {
|
||||
wasm_set_exception(module_inst, "allocate memory failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Temporarily replace exec_env's module inst with current
|
||||
module inst to ensure that the exec_env's module inst
|
||||
is the correct one. */
|
||||
module_inst_old = exec_env->module_inst;
|
||||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
}
|
||||
|
||||
ret = wasm_call_function(exec_env, malloc_func, argc, argv);
|
||||
|
||||
if (retain_func && ret) {
|
||||
if (retain_func && ret)
|
||||
ret = wasm_call_function(exec_env, retain_func, 1, argv);
|
||||
}
|
||||
|
||||
if (module_inst_old)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
exec_env->module_inst = module_inst_old;
|
||||
|
||||
if (exec_env_created)
|
||||
wasm_exec_env_destroy(exec_env_created);
|
||||
|
||||
if (ret)
|
||||
*p_result = argv[0];
|
||||
|
@ -1177,7 +1227,10 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
||||
#endif
|
||||
WASMExecEnv *exec_env_created = NULL;
|
||||
WASMModuleInstanceCommon *module_inst_old = NULL;
|
||||
uint32 argv[2];
|
||||
bool ret;
|
||||
|
||||
argv[0] = offset;
|
||||
|
||||
|
@ -1191,15 +1244,42 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|||
== (WASMModuleInstanceCommon *)module_inst);
|
||||
}
|
||||
else {
|
||||
if (!(exec_env =
|
||||
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
||||
/* Try using the existing exec_env */
|
||||
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
||||
exec_env = exec_env_tls;
|
||||
#endif
|
||||
#if WASM_ENABLE_THREAD_MGR != 0
|
||||
if (!exec_env)
|
||||
exec_env = wasm_clusters_search_exec_env(
|
||||
(WASMModuleInstanceCommon *)module_inst);
|
||||
#endif
|
||||
if (!exec_env) {
|
||||
if (!(exec_env = exec_env_created = wasm_exec_env_create(
|
||||
(WASMModuleInstanceCommon *)module_inst,
|
||||
module_inst->default_wasm_stack_size))) {
|
||||
wasm_set_exception(module_inst, "allocate memory failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Temporarily replace exec_env's module inst with current
|
||||
module inst to ensure that the exec_env's module inst
|
||||
is the correct one. */
|
||||
module_inst_old = exec_env->module_inst;
|
||||
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
||||
}
|
||||
}
|
||||
|
||||
return wasm_call_function(exec_env, free_func, 1, argv);
|
||||
ret = wasm_call_function(exec_env, free_func, 1, argv);
|
||||
|
||||
if (module_inst_old)
|
||||
/* Restore the existing exec_env's module inst */
|
||||
exec_env->module_inst = module_inst_old;
|
||||
|
||||
if (exec_env_created)
|
||||
wasm_exec_env_destroy(exec_env_created);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_MULTI_MODULE != 0
|
||||
|
|
Loading…
Reference in New Issue
Block a user