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
10 changed files with 327 additions and 236 deletions

View File

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