Use pre-created exec_env for instantiation and module_malloc/free (#2047)

Use pre-created exec_env for instantiation and module_malloc/free,
use the same exec_env of the current thread to avoid potential
unexpected behavior.

And remove unnecessary shared_mem_lock in wasm_module_free,
which may cause dead lock.
This commit is contained in:
Wenyong Huang 2023-03-23 19:19:47 +08:00 committed by GitHub
parent 4c2d358980
commit 3977f0b22a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 327 additions and 236 deletions

View File

@ -922,14 +922,14 @@ lookup_post_instantiate_func(AOTModuleInstance *module_inst,
static bool static bool
execute_post_instantiate_functions(AOTModuleInstance *module_inst, execute_post_instantiate_functions(AOTModuleInstance *module_inst,
bool is_sub_inst) bool is_sub_inst, WASMExecEnv *exec_env_main)
{ {
AOTModule *module = (AOTModule *)module_inst->module; AOTModule *module = (AOTModule *)module_inst->module;
AOTFunctionInstance *initialize_func = NULL; AOTFunctionInstance *initialize_func = NULL;
AOTFunctionInstance *post_inst_func = NULL; AOTFunctionInstance *post_inst_func = NULL;
AOTFunctionInstance *call_ctors_func = NULL; AOTFunctionInstance *call_ctors_func = NULL;
#ifdef OS_ENABLE_HW_BOUND_CHECK
WASMModuleInstanceCommon *module_inst_main = NULL; WASMModuleInstanceCommon *module_inst_main = NULL;
#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;
@ -973,26 +973,30 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
return true; return true;
} }
#ifdef OS_ENABLE_HW_BOUND_CHECK
if (is_sub_inst) { if (is_sub_inst) {
exec_env = exec_env_tls = wasm_runtime_get_exec_env_tls(); bh_assert(exec_env_main);
if (exec_env_tls) { #ifdef OS_ENABLE_HW_BOUND_CHECK
/* Temporarily replace exec_env_tls's module inst to current exec_env_tls = wasm_runtime_get_exec_env_tls();
bh_assert(exec_env_tls == exec_env_main);
(void)exec_env_tls;
#endif
exec_env = exec_env_main;
/* Temporarily replace parent exec_env's module inst to current
module inst to avoid checking failure when calling the module inst to avoid checking failure when calling the
wasm functions, and ensure that the exec_env's module inst wasm functions, and ensure that the exec_env's module inst
is the correct one. */ is the correct one. */
module_inst_main = exec_env_tls->module_inst; module_inst_main = exec_env_main->module_inst;
exec_env_tls->module_inst = (WASMModuleInstanceCommon *)module_inst; exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
} }
} else {
#endif if (!(exec_env =
if (!exec_env
&& !(exec_env =
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst, wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
module_inst->default_wasm_stack_size))) { module_inst->default_wasm_stack_size))) {
aot_set_exception(module_inst, "allocate memory failed"); aot_set_exception(module_inst, "allocate memory failed");
return false; return false;
} }
}
/* Execute start function for both main insance and sub instance */ /* Execute start function for both main insance and sub instance */
if (module->start_function) { if (module->start_function) {
@ -1029,17 +1033,11 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
ret = true; ret = true;
fail: fail:
#ifdef OS_ENABLE_HW_BOUND_CHECK if (is_sub_inst)
if (is_sub_inst && exec_env_tls) { /* Restore the parent exec_env's module inst */
bh_assert(exec_env == exec_env_tls); exec_env_main->module_inst = module_inst_main;
/* Restore the exec_env_tls's module inst */
exec_env_tls->module_inst = module_inst_main;
}
else else
wasm_exec_env_destroy(exec_env); wasm_exec_env_destroy(exec_env);
#else
wasm_exec_env_destroy(exec_env);
#endif
return ret; return ret;
} }
@ -1065,8 +1063,9 @@ check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size)
} }
AOTModuleInstance * AOTModuleInstance *
aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size, aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
uint32 heap_size, char *error_buf, uint32 error_buf_size) uint32 stack_size, uint32 heap_size, char *error_buf,
uint32 error_buf_size)
{ {
AOTModuleInstance *module_inst; AOTModuleInstance *module_inst;
const uint32 module_inst_struct_size = const uint32 module_inst_struct_size =
@ -1206,7 +1205,8 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
} }
#endif #endif
if (!execute_post_instantiate_functions(module_inst, is_sub_inst)) { if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
exec_env_main)) {
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception); set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
goto fail; goto fail;
} }
@ -1557,39 +1557,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
} }
} }
bool
aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
AOTFunctionInstance *func, unsigned argc,
uint32 argv[])
{
WASMExecEnv *exec_env = NULL, *existing_exec_env = NULL;
bool ret;
#if defined(OS_ENABLE_HW_BOUND_CHECK)
existing_exec_env = exec_env = wasm_runtime_get_exec_env_tls();
#elif WASM_ENABLE_THREAD_MGR != 0
existing_exec_env = exec_env =
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
#endif
if (!existing_exec_env) {
if (!(exec_env =
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
module_inst->default_wasm_stack_size))) {
aot_set_exception(module_inst, "allocate memory failed");
return false;
}
}
ret = wasm_runtime_call_wasm(exec_env, func, argc, argv);
/* don't destroy the exec_env if it isn't created in this function */
if (!existing_exec_env)
wasm_exec_env_destroy(exec_env);
return ret;
}
void void
aot_set_exception(AOTModuleInstance *module_inst, const char *exception) aot_set_exception(AOTModuleInstance *module_inst, const char *exception)
{ {
@ -1621,7 +1588,7 @@ aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf)
} }
static bool static bool
execute_malloc_function(AOTModuleInstance *module_inst, execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
AOTFunctionInstance *malloc_func, AOTFunctionInstance *malloc_func,
AOTFunctionInstance *retain_func, uint32 size, AOTFunctionInstance *retain_func, uint32 size,
uint32 *p_result) uint32 *p_result)
@ -1639,26 +1606,28 @@ execute_malloc_function(AOTModuleInstance *module_inst,
argc = 2; argc = 2;
} }
if (exec_env) {
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
if (exec_env_tls != NULL) { if (exec_env_tls) {
bh_assert(exec_env_tls->module_inst bh_assert(exec_env_tls == exec_env);
== (WASMModuleInstanceCommon *)module_inst);
ret = aot_call_function(exec_env_tls, malloc_func, argc, argv);
if (retain_func && ret) {
ret = aot_call_function(exec_env_tls, retain_func, 1, argv);
} }
}
else
#endif #endif
{ bh_assert(exec_env->module_inst
ret = aot_create_exec_env_and_call_function(module_inst, malloc_func, == (WASMModuleInstanceCommon *)module_inst);
argc, argv); }
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;
}
}
ret = aot_call_function(exec_env, malloc_func, argc, argv);
if (retain_func && ret) { if (retain_func && ret) {
ret = aot_create_exec_env_and_call_function(module_inst, ret = aot_call_function(exec_env, retain_func, 1, argv);
retain_func, 1, argv);
}
} }
if (ret) if (ret)
@ -1667,7 +1636,7 @@ execute_malloc_function(AOTModuleInstance *module_inst,
} }
static bool static bool
execute_free_function(AOTModuleInstance *module_inst, execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
AOTFunctionInstance *free_func, uint32 offset) AOTFunctionInstance *free_func, uint32 offset)
{ {
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
@ -1676,22 +1645,31 @@ execute_free_function(AOTModuleInstance *module_inst,
uint32 argv[2]; uint32 argv[2];
argv[0] = offset; argv[0] = offset;
if (exec_env) {
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
if (exec_env_tls != NULL) { if (exec_env_tls) {
bh_assert(exec_env_tls->module_inst bh_assert(exec_env_tls == exec_env);
== (WASMModuleInstanceCommon *)module_inst);
return aot_call_function(exec_env_tls, free_func, 1, argv);
} }
else
#endif #endif
{ bh_assert(exec_env->module_inst
return aot_create_exec_env_and_call_function(module_inst, free_func, 1, == (WASMModuleInstanceCommon *)module_inst);
argv);
} }
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;
}
}
return aot_call_function(exec_env, free_func, 1, argv);
} }
uint32 uint32
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, aot_module_malloc_internal(AOTModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 size,
void **p_native_addr) void **p_native_addr)
{ {
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
@ -1729,8 +1707,8 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig); aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig);
if (!malloc_func if (!malloc_func
|| !execute_malloc_function(module_inst, malloc_func, retain_func, || !execute_malloc_function(module_inst, exec_env, malloc_func,
size, &offset)) { retain_func, size, &offset)) {
return 0; return 0;
} }
addr = offset ? (uint8 *)memory_inst->memory_data + offset : NULL; addr = offset ? (uint8 *)memory_inst->memory_data + offset : NULL;
@ -1753,7 +1731,8 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
} }
uint32 uint32
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size, aot_module_realloc_internal(AOTModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 ptr, uint32 size,
void **p_native_addr) void **p_native_addr)
{ {
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
@ -1771,6 +1750,7 @@ aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
} }
/* Only support realloc in WAMR's app heap */ /* Only support realloc in WAMR's app heap */
(void)exec_env;
if (!addr) { if (!addr) {
if (memory_inst->heap_handle if (memory_inst->heap_handle
@ -1789,7 +1769,8 @@ aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
} }
void void
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr) aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
uint32 ptr)
{ {
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst); AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
AOTModule *module = (AOTModule *)module_inst->module; AOTModule *module = (AOTModule *)module_inst->module;
@ -1823,11 +1804,32 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
free_func = aot_lookup_function(module_inst, "__unpin", "(i)i"); free_func = aot_lookup_function(module_inst, "__unpin", "(i)i");
if (free_func) if (free_func)
execute_free_function(module_inst, free_func, ptr); execute_free_function(module_inst, exec_env, free_func, ptr);
} }
} }
} }
uint32
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
void **p_native_addr)
{
return aot_module_malloc_internal(module_inst, NULL, size, p_native_addr);
}
uint32
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
void **p_native_addr)
{
return aot_module_realloc_internal(module_inst, NULL, ptr, size,
p_native_addr);
}
void
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
{
aot_module_free_internal(module_inst, NULL, ptr);
}
uint32 uint32
aot_module_dup_data(AOTModuleInstance *module_inst, const char *src, aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
uint32 size) uint32 size)

View File

@ -343,8 +343,9 @@ aot_unload(AOTModule *module);
* @return return the instantiated AOT module instance, NULL if failed * @return return the instantiated AOT module instance, NULL if failed
*/ */
AOTModuleInstance * AOTModuleInstance *
aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size, aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
uint32 heap_size, char *error_buf, uint32 error_buf_size); uint32 stack_size, uint32 heap_size, char *error_buf,
uint32 error_buf_size);
/** /**
* Deinstantiate a AOT module instance, destroy the resources. * Deinstantiate a AOT module instance, destroy the resources.
@ -387,11 +388,6 @@ bool
aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function, aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
unsigned argc, uint32 argv[]); unsigned argc, uint32 argv[]);
bool
aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
AOTFunctionInstance *function,
unsigned argc, uint32 argv[]);
/** /**
* Set AOT module instance exception with exception string * Set AOT module instance exception with exception string
* *
@ -424,6 +420,18 @@ aot_get_exception(AOTModuleInstance *module_inst);
bool bool
aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf); aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf);
uint32
aot_module_malloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
uint32 size, void **p_native_addr);
uint32
aot_module_realloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
uint32 ptr, uint32 size, void **p_native_addr);
void
aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
uint32 ptr);
uint32 uint32
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size, aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
void **p_native_addr); void **p_native_addr);

View File

@ -225,8 +225,11 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
ret = wasm_runtime_get_exception(module_inst) == NULL; ret = wasm_runtime_get_exception(module_inst) == NULL;
#if WASM_ENABLE_DUMP_CALL_STACK != 0 #if WASM_ENABLE_DUMP_CALL_STACK != 0
if (!ret) if (!ret) {
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
if (exec_env)
wasm_runtime_dump_call_stack(exec_env); wasm_runtime_dump_call_stack(exec_env);
}
#endif #endif
return ret; return ret;

View File

@ -1196,20 +1196,21 @@ wasm_runtime_unload(WASMModuleCommon *module)
WASMModuleInstanceCommon * WASMModuleInstanceCommon *
wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst, wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
uint32 stack_size, uint32 heap_size, WASMExecEnv *exec_env_main, uint32 stack_size,
char *error_buf, uint32 error_buf_size) uint32 heap_size, char *error_buf,
uint32 error_buf_size)
{ {
#if WASM_ENABLE_INTERP != 0 #if WASM_ENABLE_INTERP != 0
if (module->module_type == Wasm_Module_Bytecode) if (module->module_type == Wasm_Module_Bytecode)
return (WASMModuleInstanceCommon *)wasm_instantiate( return (WASMModuleInstanceCommon *)wasm_instantiate(
(WASMModule *)module, is_sub_inst, stack_size, heap_size, error_buf, (WASMModule *)module, is_sub_inst, exec_env_main, stack_size,
error_buf_size); heap_size, error_buf, error_buf_size);
#endif #endif
#if WASM_ENABLE_AOT != 0 #if WASM_ENABLE_AOT != 0
if (module->module_type == Wasm_Module_AoT) if (module->module_type == Wasm_Module_AoT)
return (WASMModuleInstanceCommon *)aot_instantiate( return (WASMModuleInstanceCommon *)aot_instantiate(
(AOTModule *)module, is_sub_inst, stack_size, heap_size, error_buf, (AOTModule *)module, is_sub_inst, exec_env_main, stack_size,
error_buf_size); heap_size, error_buf, error_buf_size);
#endif #endif
set_error_buf(error_buf, error_buf_size, set_error_buf(error_buf, error_buf_size,
"Instantiate module failed, invalid module type"); "Instantiate module failed, invalid module type");
@ -1222,7 +1223,7 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size,
uint32 error_buf_size) uint32 error_buf_size)
{ {
return wasm_runtime_instantiate_internal( return wasm_runtime_instantiate_internal(
module, false, stack_size, heap_size, error_buf, error_buf_size); module, false, NULL, stack_size, heap_size, error_buf, error_buf_size);
} }
void void
@ -2480,6 +2481,62 @@ wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst_comm)
return module_inst->custom_data; return module_inst->custom_data;
} }
uint32
wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env, uint32 size,
void **p_native_addr)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode)
return wasm_module_malloc_internal((WASMModuleInstance *)module_inst,
exec_env, size, p_native_addr);
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT)
return aot_module_malloc_internal((AOTModuleInstance *)module_inst,
exec_env, size, p_native_addr);
#endif
return 0;
}
uint32
wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env, uint32 ptr,
uint32 size, void **p_native_addr)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode)
return wasm_module_realloc_internal((WASMModuleInstance *)module_inst,
exec_env, ptr, size, p_native_addr);
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT)
return aot_module_realloc_internal((AOTModuleInstance *)module_inst,
exec_env, ptr, size, p_native_addr);
#endif
return 0;
}
void
wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env, uint32 ptr)
{
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode) {
wasm_module_free_internal((WASMModuleInstance *)module_inst, exec_env,
ptr);
return;
}
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
aot_module_free_internal((AOTModuleInstance *)module_inst, exec_env,
ptr);
return;
}
#endif
}
uint32 uint32
wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size, wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
void **p_native_addr) void **p_native_addr)

View File

@ -498,8 +498,9 @@ wasm_runtime_unload(WASMModuleCommon *module);
/* Internal API */ /* Internal API */
WASMModuleInstanceCommon * WASMModuleInstanceCommon *
wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst, wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
uint32 stack_size, uint32 heap_size, WASMExecEnv *exec_env_main, uint32 stack_size,
char *error_buf, uint32 error_buf_size); uint32 heap_size, char *error_buf,
uint32 error_buf_size);
/* Internal API */ /* Internal API */
void void
@ -675,6 +676,23 @@ wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
WASM_RUNTIME_API_EXTERN void * WASM_RUNTIME_API_EXTERN void *
wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst); wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst);
/* Internal API */
uint32
wasm_runtime_module_malloc_internal(WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env, uint32 size,
void **p_native_addr);
/* Internal API */
uint32
wasm_runtime_module_realloc_internal(WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env, uint32 ptr,
uint32 size, void **p_native_addr);
/* Internal API */
void
wasm_runtime_module_free_internal(WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env, uint32 ptr);
/* See wasm_export.h for description */ /* See wasm_export.h for description */
WASM_RUNTIME_API_EXTERN uint32 WASM_RUNTIME_API_EXTERN uint32
wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size, wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,

View File

@ -1003,7 +1003,7 @@ lookup_post_instantiate_func(WASMModuleInstance *module_inst,
static bool static bool
execute_post_instantiate_functions(WASMModuleInstance *module_inst, execute_post_instantiate_functions(WASMModuleInstance *module_inst,
bool is_sub_inst) bool is_sub_inst, WASMExecEnv *exec_env_main)
{ {
WASMFunctionInstance *start_func = module_inst->e->start_function; WASMFunctionInstance *start_func = module_inst->e->start_function;
WASMFunctionInstance *initialize_func = NULL; WASMFunctionInstance *initialize_func = NULL;
@ -1012,8 +1012,8 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
#if WASM_ENABLE_LIBC_WASI != 0 #if WASM_ENABLE_LIBC_WASI != 0
WASMModule *module = module_inst->module; WASMModule *module = module_inst->module;
#endif #endif
#ifdef OS_ENABLE_HW_BOUND_CHECK
WASMModuleInstanceCommon *module_inst_main = NULL; WASMModuleInstanceCommon *module_inst_main = NULL;
#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;
@ -1057,26 +1057,30 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
return true; return true;
} }
#ifdef OS_ENABLE_HW_BOUND_CHECK
if (is_sub_inst) { if (is_sub_inst) {
exec_env = exec_env_tls = wasm_runtime_get_exec_env_tls(); bh_assert(exec_env_main);
if (exec_env_tls) { #ifdef OS_ENABLE_HW_BOUND_CHECK
/* Temporarily replace exec_env_tls's module inst to current exec_env_tls = wasm_runtime_get_exec_env_tls();
bh_assert(exec_env_tls == exec_env_main);
(void)exec_env_tls;
#endif
exec_env = exec_env_main;
/* Temporarily replace parent exec_env's module inst to current
module inst to avoid checking failure when calling the module inst to avoid checking failure when calling the
wasm functions, and ensure that the exec_env's module inst wasm functions, and ensure that the exec_env's module inst
is the correct one. */ is the correct one. */
module_inst_main = exec_env_tls->module_inst; module_inst_main = exec_env_main->module_inst;
exec_env_tls->module_inst = (WASMModuleInstanceCommon *)module_inst; exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
} }
} else {
#endif if (!(exec_env =
if (!exec_env
&& !(exec_env =
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst, wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
module_inst->default_wasm_stack_size))) { module_inst->default_wasm_stack_size))) {
wasm_set_exception(module_inst, "allocate memory failed"); wasm_set_exception(module_inst, "allocate memory failed");
return false; return false;
} }
}
/* Execute start function for both main insance and sub instance */ /* Execute start function for both main insance and sub instance */
if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) { if (start_func && !wasm_call_function(exec_env, start_func, 0, NULL)) {
@ -1101,23 +1105,17 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
ret = true; ret = true;
fail: fail:
#ifdef OS_ENABLE_HW_BOUND_CHECK if (is_sub_inst)
if (is_sub_inst && exec_env_tls) { /* Restore the parent exec_env's module inst */
bh_assert(exec_env == exec_env_tls); exec_env_main->module_inst = module_inst_main;
/* Restore the exec_env_tls's module inst */
exec_env_tls->module_inst = module_inst_main;
}
else else
wasm_exec_env_destroy(exec_env); wasm_exec_env_destroy(exec_env);
#else
wasm_exec_env_destroy(exec_env);
#endif
return ret; return ret;
} }
static bool static bool
execute_malloc_function(WASMModuleInstance *module_inst, execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
WASMFunctionInstance *malloc_func, WASMFunctionInstance *malloc_func,
WASMFunctionInstance *retain_func, uint32 size, WASMFunctionInstance *retain_func, uint32 size,
uint32 *p_result) uint32 *p_result)
@ -1143,26 +1141,28 @@ execute_malloc_function(WASMModuleInstance *module_inst,
argc = 2; argc = 2;
} }
if (exec_env) {
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
if (exec_env_tls != NULL) { if (exec_env_tls) {
bh_assert(exec_env_tls->module_inst bh_assert(exec_env_tls == exec_env);
== (WASMModuleInstanceCommon *)module_inst);
ret = wasm_call_function(exec_env_tls, malloc_func, argc, argv);
if (retain_func && ret) {
ret = wasm_call_function(exec_env_tls, retain_func, 1, argv);
} }
}
else
#endif #endif
{ bh_assert(exec_env->module_inst
ret = wasm_create_exec_env_and_call_function(module_inst, malloc_func, == (WASMModuleInstanceCommon *)module_inst);
argc, argv); }
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;
}
}
ret = wasm_call_function(exec_env, malloc_func, argc, argv);
if (retain_func && ret) { if (retain_func && ret) {
ret = wasm_create_exec_env_and_call_function(module_inst, ret = wasm_call_function(exec_env, retain_func, 1, argv);
retain_func, 1, argv);
}
} }
if (ret) if (ret)
@ -1171,7 +1171,7 @@ execute_malloc_function(WASMModuleInstance *module_inst,
} }
static bool static bool
execute_free_function(WASMModuleInstance *module_inst, execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
WASMFunctionInstance *free_func, uint32 offset) WASMFunctionInstance *free_func, uint32 offset)
{ {
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
@ -1180,18 +1180,26 @@ execute_free_function(WASMModuleInstance *module_inst,
uint32 argv[2]; uint32 argv[2];
argv[0] = offset; argv[0] = offset;
if (exec_env) {
#ifdef OS_ENABLE_HW_BOUND_CHECK #ifdef OS_ENABLE_HW_BOUND_CHECK
if (exec_env_tls != NULL) { if (exec_env_tls) {
bh_assert(exec_env_tls->module_inst bh_assert(exec_env_tls == exec_env);
== (WASMModuleInstanceCommon *)module_inst);
return wasm_call_function(exec_env_tls, free_func, 1, argv);
} }
else
#endif #endif
{ bh_assert(exec_env->module_inst
return wasm_create_exec_env_and_call_function(module_inst, free_func, 1, == (WASMModuleInstanceCommon *)module_inst);
argv);
} }
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;
}
}
return wasm_call_function(exec_env, free_func, 1, argv);
} }
#if WASM_ENABLE_MULTI_MODULE != 0 #if WASM_ENABLE_MULTI_MODULE != 0
@ -1210,7 +1218,7 @@ sub_module_instantiate(WASMModule *module, WASMModuleInstance *module_inst,
WASMModuleInstance *sub_module_inst = NULL; WASMModuleInstance *sub_module_inst = NULL;
sub_module_inst = sub_module_inst =
wasm_instantiate(sub_module, false, stack_size, heap_size, wasm_instantiate(sub_module, false, NULL, stack_size, heap_size,
error_buf, error_buf_size); error_buf, error_buf_size);
if (!sub_module_inst) { if (!sub_module_inst) {
LOG_DEBUG("instantiate %s failed", LOG_DEBUG("instantiate %s failed",
@ -1555,7 +1563,8 @@ wasm_set_running_mode(WASMModuleInstance *module_inst, RunningMode running_mode)
* Instantiate module * Instantiate module
*/ */
WASMModuleInstance * WASMModuleInstance *
wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size, wasm_instantiate(WASMModule *module, bool is_sub_inst,
WASMExecEnv *exec_env_main, uint32 stack_size,
uint32 heap_size, char *error_buf, uint32 error_buf_size) uint32 heap_size, char *error_buf, uint32 error_buf_size)
{ {
WASMModuleInstance *module_inst; WASMModuleInstance *module_inst;
@ -2049,7 +2058,8 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
&module_inst->e->functions[module->start_function]; &module_inst->e->functions[module->start_function];
} }
if (!execute_post_instantiate_functions(module_inst, is_sub_inst)) { if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
exec_env_main)) {
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception); set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
goto fail; goto fail;
} }
@ -2346,39 +2356,6 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
return !wasm_copy_exception(module_inst, NULL); return !wasm_copy_exception(module_inst, NULL);
} }
bool
wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
WASMFunctionInstance *func,
unsigned argc, uint32 argv[])
{
WASMExecEnv *exec_env = NULL, *existing_exec_env = NULL;
bool ret;
#if defined(OS_ENABLE_HW_BOUND_CHECK)
existing_exec_env = exec_env = wasm_runtime_get_exec_env_tls();
#elif WASM_ENABLE_THREAD_MGR != 0
existing_exec_env = exec_env =
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
#endif
if (!existing_exec_env) {
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;
}
}
ret = wasm_runtime_call_wasm(exec_env, func, argc, argv);
/* don't destroy the exec_env if it isn't created in this function */
if (!existing_exec_env)
wasm_exec_env_destroy(exec_env);
return ret;
}
#if WASM_ENABLE_PERF_PROFILING != 0 #if WASM_ENABLE_PERF_PROFILING != 0
void void
wasm_dump_perf_profiling(const WASMModuleInstance *module_inst) wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
@ -2426,7 +2403,8 @@ wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
#endif #endif
uint32 uint32
wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size, wasm_module_malloc_internal(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 size,
void **p_native_addr) void **p_native_addr)
{ {
WASMMemoryInstance *memory = wasm_get_default_memory(module_inst); WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
@ -2443,7 +2421,7 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
} }
else if (module_inst->e->malloc_function && module_inst->e->free_function) { else if (module_inst->e->malloc_function && module_inst->e->free_function) {
if (!execute_malloc_function( if (!execute_malloc_function(
module_inst, module_inst->e->malloc_function, module_inst, exec_env, module_inst->e->malloc_function,
module_inst->e->retain_function, size, &offset)) { module_inst->e->retain_function, size, &offset)) {
return 0; return 0;
} }
@ -2471,7 +2449,8 @@ wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
} }
uint32 uint32
wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size, wasm_module_realloc_internal(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 ptr, uint32 size,
void **p_native_addr) void **p_native_addr)
{ {
WASMMemoryInstance *memory = wasm_get_default_memory(module_inst); WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
@ -2488,6 +2467,7 @@ wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
} }
/* Only support realloc in WAMR's app heap */ /* Only support realloc in WAMR's app heap */
(void)exec_env;
if (!addr) { if (!addr) {
if (memory->heap_handle if (memory->heap_handle
@ -2506,7 +2486,8 @@ wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
} }
void void
wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr) wasm_module_free_internal(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 ptr)
{ {
if (ptr) { if (ptr) {
WASMMemoryInstance *memory = wasm_get_default_memory(module_inst); WASMMemoryInstance *memory = wasm_get_default_memory(module_inst);
@ -2516,12 +2497,6 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
return; return;
} }
#if WASM_ENABLE_SHARED_MEMORY != 0
WASMSharedMemNode *node = wasm_module_get_shared_memory(
(WASMModuleCommon *)module_inst->module);
if (node)
os_mutex_lock(&node->shared_mem_lock);
#endif
addr = memory->memory_data + ptr; addr = memory->memory_data + ptr;
if (memory->heap_handle && memory->heap_data <= addr if (memory->heap_handle && memory->heap_data <= addr
@ -2531,16 +2506,33 @@ wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
else if (module_inst->e->malloc_function else if (module_inst->e->malloc_function
&& module_inst->e->free_function && memory->memory_data <= addr && module_inst->e->free_function && memory->memory_data <= addr
&& addr < memory->memory_data_end) { && addr < memory->memory_data_end) {
execute_free_function(module_inst, module_inst->e->free_function, execute_free_function(module_inst, exec_env,
ptr); module_inst->e->free_function, ptr);
} }
#if WASM_ENABLE_SHARED_MEMORY != 0
if (node)
os_mutex_unlock(&node->shared_mem_lock);
#endif
} }
} }
uint32
wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
void **p_native_addr)
{
return wasm_module_malloc_internal(module_inst, NULL, size, p_native_addr);
}
uint32
wasm_module_realloc(WASMModuleInstance *module_inst, uint32 ptr, uint32 size,
void **p_native_addr)
{
return wasm_module_realloc_internal(module_inst, NULL, ptr, size,
p_native_addr);
}
void
wasm_module_free(WASMModuleInstance *module_inst, uint32 ptr)
{
wasm_module_free_internal(module_inst, NULL, ptr);
}
uint32 uint32
wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src, wasm_module_dup_data(WASMModuleInstance *module_inst, const char *src,
uint32 size) uint32 size)

View File

@ -400,7 +400,8 @@ void
wasm_unload(WASMModule *module); wasm_unload(WASMModule *module);
WASMModuleInstance * WASMModuleInstance *
wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size, wasm_instantiate(WASMModule *module, bool is_sub_inst,
WASMExecEnv *exec_env_main, uint32 stack_size,
uint32 heap_size, char *error_buf, uint32 error_buf_size); uint32 heap_size, char *error_buf, uint32 error_buf_size);
void void
@ -432,11 +433,6 @@ bool
wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function, wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
unsigned argc, uint32 argv[]); unsigned argc, uint32 argv[]);
bool
wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
WASMFunctionInstance *function,
unsigned argc, uint32 argv[]);
void void
wasm_set_exception(WASMModuleInstance *module, const char *exception); wasm_set_exception(WASMModuleInstance *module, const char *exception);
@ -455,6 +451,20 @@ wasm_get_exception(WASMModuleInstance *module);
bool bool
wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf); wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf);
uint32
wasm_module_malloc_internal(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 size,
void **p_native_addr);
uint32
wasm_module_realloc_internal(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 ptr, uint32 size,
void **p_native_addr);
void
wasm_module_free_internal(WASMModuleInstance *module_inst,
WASMExecEnv *exec_env, uint32 ptr);
uint32 uint32
wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size, wasm_module_malloc(WASMModuleInstance *module_inst, uint32 size,
void **p_native_addr); void **p_native_addr);

View File

@ -581,7 +581,7 @@ pthread_create_wrapper(wasm_exec_env_t exec_env,
#endif #endif
if (!(new_module_inst = wasm_runtime_instantiate_internal( if (!(new_module_inst = wasm_runtime_instantiate_internal(
module, true, stack_size, 0, NULL, 0))) module, true, exec_env, stack_size, 0, NULL, 0)))
return -1; return -1;
/* Set custom_data to new module instance */ /* Set custom_data to new module instance */

View File

@ -90,7 +90,7 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size; stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
if (!(new_module_inst = wasm_runtime_instantiate_internal( if (!(new_module_inst = wasm_runtime_instantiate_internal(
module, true, stack_size, 0, NULL, 0))) module, true, exec_env, stack_size, 0, NULL, 0)))
return -1; return -1;
wasm_runtime_set_custom_data_internal( wasm_runtime_set_custom_data_internal(

View File

@ -143,8 +143,8 @@ allocate_aux_stack(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
wasm_exec_env_get_module_inst(exec_env); wasm_exec_env_get_module_inst(exec_env);
uint32 stack_end; uint32 stack_end;
stack_end = stack_end = wasm_runtime_module_malloc_internal(module_inst, exec_env,
wasm_runtime_module_malloc(module_inst, cluster->stack_size, NULL); cluster->stack_size, NULL);
*start = stack_end + cluster->stack_size; *start = stack_end + cluster->stack_size;
*size = cluster->stack_size; *size = cluster->stack_size;
@ -188,7 +188,8 @@ free_aux_stack(WASMExecEnv *exec_env, uint32 start)
bh_assert(start >= cluster->stack_size); bh_assert(start >= cluster->stack_size);
wasm_runtime_module_free(module_inst, start - cluster->stack_size); wasm_runtime_module_free_internal(module_inst, exec_env,
start - cluster->stack_size);
return true; return true;
#else #else
@ -495,7 +496,7 @@ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
#endif #endif
if (!(new_module_inst = wasm_runtime_instantiate_internal( if (!(new_module_inst = wasm_runtime_instantiate_internal(
module, true, stack_size, 0, NULL, 0))) { module, true, exec_env, stack_size, 0, NULL, 0))) {
goto fail1; goto fail1;
} }