|
@@ -1016,7 +1016,7 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
|
|
|
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
|
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
|
|
WASMExecEnv *exec_env_tls = NULL;
|
|
WASMExecEnv *exec_env_tls = NULL;
|
|
|
#endif
|
|
#endif
|
|
|
- WASMExecEnv *exec_env = NULL;
|
|
|
|
|
|
|
+ WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
|
|
|
bool ret = false;
|
|
bool ret = false;
|
|
|
|
|
|
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
|
#if WASM_ENABLE_LIBC_WASI != 0
|
|
@@ -1074,11 +1074,29 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
|
|
|
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
|
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- if (!(exec_env =
|
|
|
|
|
- wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
|
|
|
|
- module_inst->default_wasm_stack_size))) {
|
|
|
|
|
- wasm_set_exception(module_inst, "allocate memory failed");
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ /* 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;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1105,11 +1123,17 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
|
|
|
ret = true;
|
|
ret = true;
|
|
|
|
|
|
|
|
fail:
|
|
fail:
|
|
|
- if (is_sub_inst)
|
|
|
|
|
|
|
+ if (is_sub_inst) {
|
|
|
/* Restore the parent exec_env's module inst */
|
|
/* Restore the parent exec_env's module inst */
|
|
|
exec_env_main->module_inst = module_inst_main;
|
|
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;
|
|
return ret;
|
|
|
}
|
|
}
|
|
@@ -1123,6 +1147,8 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|
|
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
|
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
|
|
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
|
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
|
|
#endif
|
|
#endif
|
|
|
|
|
+ WASMExecEnv *exec_env_created = NULL;
|
|
|
|
|
+ WASMModuleInstanceCommon *module_inst_old = NULL;
|
|
|
uint32 argv[2], argc;
|
|
uint32 argv[2], argc;
|
|
|
bool ret;
|
|
bool ret;
|
|
|
|
|
|
|
@@ -1151,19 +1177,43 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|
|
== (WASMModuleInstanceCommon *)module_inst);
|
|
== (WASMModuleInstanceCommon *)module_inst);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- if (!(exec_env =
|
|
|
|
|
- wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
|
|
|
|
- module_inst->default_wasm_stack_size))) {
|
|
|
|
|
- wasm_set_exception(module_inst, "allocate memory failed");
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ /* 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);
|
|
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);
|
|
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)
|
|
if (ret)
|
|
|
*p_result = argv[0];
|
|
*p_result = argv[0];
|
|
@@ -1177,7 +1227,10 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|
|
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
|
#ifdef OS_ENABLE_HW_BOUND_CHECK
|
|
|
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
|
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
|
|
|
#endif
|
|
#endif
|
|
|
|
|
+ WASMExecEnv *exec_env_created = NULL;
|
|
|
|
|
+ WASMModuleInstanceCommon *module_inst_old = NULL;
|
|
|
uint32 argv[2];
|
|
uint32 argv[2];
|
|
|
|
|
+ bool ret;
|
|
|
|
|
|
|
|
argv[0] = offset;
|
|
argv[0] = offset;
|
|
|
|
|
|
|
@@ -1191,15 +1244,42 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
|
|
|
== (WASMModuleInstanceCommon *)module_inst);
|
|
== (WASMModuleInstanceCommon *)module_inst);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- if (!(exec_env =
|
|
|
|
|
- wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
|
|
|
|
|
- module_inst->default_wasm_stack_size))) {
|
|
|
|
|
- wasm_set_exception(module_inst, "allocate memory failed");
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ /* 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
|
|
#if WASM_ENABLE_MULTI_MODULE != 0
|