Skip to content

Commit 3977f0b

Browse files
authored
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.
1 parent 4c2d358 commit 3977f0b

10 files changed

Lines changed: 325 additions & 234 deletions

File tree

core/iwasm/aot/aot_runtime.c

Lines changed: 99 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -922,14 +922,14 @@ lookup_post_instantiate_func(AOTModuleInstance *module_inst,
922922

923923
static bool
924924
execute_post_instantiate_functions(AOTModuleInstance *module_inst,
925-
bool is_sub_inst)
925+
bool is_sub_inst, WASMExecEnv *exec_env_main)
926926
{
927927
AOTModule *module = (AOTModule *)module_inst->module;
928928
AOTFunctionInstance *initialize_func = NULL;
929929
AOTFunctionInstance *post_inst_func = NULL;
930930
AOTFunctionInstance *call_ctors_func = NULL;
931-
#ifdef OS_ENABLE_HW_BOUND_CHECK
932931
WASMModuleInstanceCommon *module_inst_main = NULL;
932+
#ifdef OS_ENABLE_HW_BOUND_CHECK
933933
WASMExecEnv *exec_env_tls = NULL;
934934
#endif
935935
WASMExecEnv *exec_env = NULL;
@@ -973,25 +973,29 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
973973
return true;
974974
}
975975

976-
#ifdef OS_ENABLE_HW_BOUND_CHECK
977976
if (is_sub_inst) {
978-
exec_env = exec_env_tls = wasm_runtime_get_exec_env_tls();
979-
if (exec_env_tls) {
980-
/* Temporarily replace exec_env_tls's module inst to current
981-
module inst to avoid checking failure when calling the
982-
wasm functions, and ensure that the exec_env's module inst
983-
is the correct one. */
984-
module_inst_main = exec_env_tls->module_inst;
985-
exec_env_tls->module_inst = (WASMModuleInstanceCommon *)module_inst;
986-
}
987-
}
977+
bh_assert(exec_env_main);
978+
#ifdef OS_ENABLE_HW_BOUND_CHECK
979+
exec_env_tls = wasm_runtime_get_exec_env_tls();
980+
bh_assert(exec_env_tls == exec_env_main);
981+
(void)exec_env_tls;
988982
#endif
989-
if (!exec_env
990-
&& !(exec_env =
991-
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
992-
module_inst->default_wasm_stack_size))) {
993-
aot_set_exception(module_inst, "allocate memory failed");
994-
return false;
983+
exec_env = exec_env_main;
984+
985+
/* Temporarily replace parent exec_env's module inst to current
986+
module inst to avoid checking failure when calling the
987+
wasm functions, and ensure that the exec_env's module inst
988+
is the correct one. */
989+
module_inst_main = exec_env_main->module_inst;
990+
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
991+
}
992+
else {
993+
if (!(exec_env =
994+
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
995+
module_inst->default_wasm_stack_size))) {
996+
aot_set_exception(module_inst, "allocate memory failed");
997+
return false;
998+
}
995999
}
9961000

9971001
/* Execute start function for both main insance and sub instance */
@@ -1029,17 +1033,11 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
10291033
ret = true;
10301034

10311035
fail:
1032-
#ifdef OS_ENABLE_HW_BOUND_CHECK
1033-
if (is_sub_inst && exec_env_tls) {
1034-
bh_assert(exec_env == exec_env_tls);
1035-
/* Restore the exec_env_tls's module inst */
1036-
exec_env_tls->module_inst = module_inst_main;
1037-
}
1036+
if (is_sub_inst)
1037+
/* Restore the parent exec_env's module inst */
1038+
exec_env_main->module_inst = module_inst_main;
10381039
else
10391040
wasm_exec_env_destroy(exec_env);
1040-
#else
1041-
wasm_exec_env_destroy(exec_env);
1042-
#endif
10431041

10441042
return ret;
10451043
}
@@ -1065,8 +1063,9 @@ check_linked_symbol(AOTModule *module, char *error_buf, uint32 error_buf_size)
10651063
}
10661064

10671065
AOTModuleInstance *
1068-
aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
1069-
uint32 heap_size, char *error_buf, uint32 error_buf_size)
1066+
aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
1067+
uint32 stack_size, uint32 heap_size, char *error_buf,
1068+
uint32 error_buf_size)
10701069
{
10711070
AOTModuleInstance *module_inst;
10721071
const uint32 module_inst_struct_size =
@@ -1206,7 +1205,8 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
12061205
}
12071206
#endif
12081207

1209-
if (!execute_post_instantiate_functions(module_inst, is_sub_inst)) {
1208+
if (!execute_post_instantiate_functions(module_inst, is_sub_inst,
1209+
exec_env_main)) {
12101210
set_error_buf(error_buf, error_buf_size, module_inst->cur_exception);
12111211
goto fail;
12121212
}
@@ -1557,39 +1557,6 @@ aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
15571557
}
15581558
}
15591559

1560-
bool
1561-
aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
1562-
AOTFunctionInstance *func, unsigned argc,
1563-
uint32 argv[])
1564-
{
1565-
WASMExecEnv *exec_env = NULL, *existing_exec_env = NULL;
1566-
bool ret;
1567-
1568-
#if defined(OS_ENABLE_HW_BOUND_CHECK)
1569-
existing_exec_env = exec_env = wasm_runtime_get_exec_env_tls();
1570-
#elif WASM_ENABLE_THREAD_MGR != 0
1571-
existing_exec_env = exec_env =
1572-
wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
1573-
#endif
1574-
1575-
if (!existing_exec_env) {
1576-
if (!(exec_env =
1577-
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
1578-
module_inst->default_wasm_stack_size))) {
1579-
aot_set_exception(module_inst, "allocate memory failed");
1580-
return false;
1581-
}
1582-
}
1583-
1584-
ret = wasm_runtime_call_wasm(exec_env, func, argc, argv);
1585-
1586-
/* don't destroy the exec_env if it isn't created in this function */
1587-
if (!existing_exec_env)
1588-
wasm_exec_env_destroy(exec_env);
1589-
1590-
return ret;
1591-
}
1592-
15931560
void
15941561
aot_set_exception(AOTModuleInstance *module_inst, const char *exception)
15951562
{
@@ -1621,7 +1588,7 @@ aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf)
16211588
}
16221589

16231590
static bool
1624-
execute_malloc_function(AOTModuleInstance *module_inst,
1591+
execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
16251592
AOTFunctionInstance *malloc_func,
16261593
AOTFunctionInstance *retain_func, uint32 size,
16271594
uint32 *p_result)
@@ -1639,26 +1606,28 @@ execute_malloc_function(AOTModuleInstance *module_inst,
16391606
argc = 2;
16401607
}
16411608

1609+
if (exec_env) {
16421610
#ifdef OS_ENABLE_HW_BOUND_CHECK
1643-
if (exec_env_tls != NULL) {
1644-
bh_assert(exec_env_tls->module_inst
1611+
if (exec_env_tls) {
1612+
bh_assert(exec_env_tls == exec_env);
1613+
}
1614+
#endif
1615+
bh_assert(exec_env->module_inst
16451616
== (WASMModuleInstanceCommon *)module_inst);
1646-
ret = aot_call_function(exec_env_tls, malloc_func, argc, argv);
1647-
1648-
if (retain_func && ret) {
1649-
ret = aot_call_function(exec_env_tls, retain_func, 1, argv);
1617+
}
1618+
else {
1619+
if (!(exec_env =
1620+
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
1621+
module_inst->default_wasm_stack_size))) {
1622+
wasm_set_exception(module_inst, "allocate memory failed");
1623+
return false;
16501624
}
16511625
}
1652-
else
1653-
#endif
1654-
{
1655-
ret = aot_create_exec_env_and_call_function(module_inst, malloc_func,
1656-
argc, argv);
16571626

1658-
if (retain_func && ret) {
1659-
ret = aot_create_exec_env_and_call_function(module_inst,
1660-
retain_func, 1, argv);
1661-
}
1627+
ret = aot_call_function(exec_env, malloc_func, argc, argv);
1628+
1629+
if (retain_func && ret) {
1630+
ret = aot_call_function(exec_env, retain_func, 1, argv);
16621631
}
16631632

16641633
if (ret)
@@ -1667,7 +1636,7 @@ execute_malloc_function(AOTModuleInstance *module_inst,
16671636
}
16681637

16691638
static bool
1670-
execute_free_function(AOTModuleInstance *module_inst,
1639+
execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
16711640
AOTFunctionInstance *free_func, uint32 offset)
16721641
{
16731642
#ifdef OS_ENABLE_HW_BOUND_CHECK
@@ -1676,23 +1645,32 @@ execute_free_function(AOTModuleInstance *module_inst,
16761645
uint32 argv[2];
16771646

16781647
argv[0] = offset;
1648+
1649+
if (exec_env) {
16791650
#ifdef OS_ENABLE_HW_BOUND_CHECK
1680-
if (exec_env_tls != NULL) {
1681-
bh_assert(exec_env_tls->module_inst
1651+
if (exec_env_tls) {
1652+
bh_assert(exec_env_tls == exec_env);
1653+
}
1654+
#endif
1655+
bh_assert(exec_env->module_inst
16821656
== (WASMModuleInstanceCommon *)module_inst);
1683-
return aot_call_function(exec_env_tls, free_func, 1, argv);
16841657
}
1685-
else
1686-
#endif
1687-
{
1688-
return aot_create_exec_env_and_call_function(module_inst, free_func, 1,
1689-
argv);
1658+
else {
1659+
if (!(exec_env =
1660+
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
1661+
module_inst->default_wasm_stack_size))) {
1662+
wasm_set_exception(module_inst, "allocate memory failed");
1663+
return false;
1664+
}
16901665
}
1666+
1667+
return aot_call_function(exec_env, free_func, 1, argv);
16911668
}
16921669

16931670
uint32
1694-
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
1695-
void **p_native_addr)
1671+
aot_module_malloc_internal(AOTModuleInstance *module_inst,
1672+
WASMExecEnv *exec_env, uint32 size,
1673+
void **p_native_addr)
16961674
{
16971675
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
16981676
AOTModule *module = (AOTModule *)module_inst->module;
@@ -1729,8 +1707,8 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
17291707
aot_lookup_function(module_inst, malloc_func_name, malloc_func_sig);
17301708

17311709
if (!malloc_func
1732-
|| !execute_malloc_function(module_inst, malloc_func, retain_func,
1733-
size, &offset)) {
1710+
|| !execute_malloc_function(module_inst, exec_env, malloc_func,
1711+
retain_func, size, &offset)) {
17341712
return 0;
17351713
}
17361714
addr = offset ? (uint8 *)memory_inst->memory_data + offset : NULL;
@@ -1753,8 +1731,9 @@ aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
17531731
}
17541732

17551733
uint32
1756-
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
1757-
void **p_native_addr)
1734+
aot_module_realloc_internal(AOTModuleInstance *module_inst,
1735+
WASMExecEnv *exec_env, uint32 ptr, uint32 size,
1736+
void **p_native_addr)
17581737
{
17591738
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
17601739
uint8 *addr = NULL;
@@ -1771,6 +1750,7 @@ aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
17711750
}
17721751

17731752
/* Only support realloc in WAMR's app heap */
1753+
(void)exec_env;
17741754

17751755
if (!addr) {
17761756
if (memory_inst->heap_handle
@@ -1789,7 +1769,8 @@ aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
17891769
}
17901770

17911771
void
1792-
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
1772+
aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
1773+
uint32 ptr)
17931774
{
17941775
AOTMemoryInstance *memory_inst = aot_get_default_memory(module_inst);
17951776
AOTModule *module = (AOTModule *)module_inst->module;
@@ -1823,11 +1804,32 @@ aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
18231804
free_func = aot_lookup_function(module_inst, "__unpin", "(i)i");
18241805

18251806
if (free_func)
1826-
execute_free_function(module_inst, free_func, ptr);
1807+
execute_free_function(module_inst, exec_env, free_func, ptr);
18271808
}
18281809
}
18291810
}
18301811

1812+
uint32
1813+
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
1814+
void **p_native_addr)
1815+
{
1816+
return aot_module_malloc_internal(module_inst, NULL, size, p_native_addr);
1817+
}
1818+
1819+
uint32
1820+
aot_module_realloc(AOTModuleInstance *module_inst, uint32 ptr, uint32 size,
1821+
void **p_native_addr)
1822+
{
1823+
return aot_module_realloc_internal(module_inst, NULL, ptr, size,
1824+
p_native_addr);
1825+
}
1826+
1827+
void
1828+
aot_module_free(AOTModuleInstance *module_inst, uint32 ptr)
1829+
{
1830+
aot_module_free_internal(module_inst, NULL, ptr);
1831+
}
1832+
18311833
uint32
18321834
aot_module_dup_data(AOTModuleInstance *module_inst, const char *src,
18331835
uint32 size)

core/iwasm/aot/aot_runtime.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ aot_unload(AOTModule *module);
343343
* @return return the instantiated AOT module instance, NULL if failed
344344
*/
345345
AOTModuleInstance *
346-
aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
347-
uint32 heap_size, char *error_buf, uint32 error_buf_size);
346+
aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
347+
uint32 stack_size, uint32 heap_size, char *error_buf,
348+
uint32 error_buf_size);
348349

349350
/**
350351
* Deinstantiate a AOT module instance, destroy the resources.
@@ -387,11 +388,6 @@ bool
387388
aot_call_function(WASMExecEnv *exec_env, AOTFunctionInstance *function,
388389
unsigned argc, uint32 argv[]);
389390

390-
bool
391-
aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
392-
AOTFunctionInstance *function,
393-
unsigned argc, uint32 argv[]);
394-
395391
/**
396392
* Set AOT module instance exception with exception string
397393
*
@@ -424,6 +420,18 @@ aot_get_exception(AOTModuleInstance *module_inst);
424420
bool
425421
aot_copy_exception(AOTModuleInstance *module_inst, char *exception_buf);
426422

423+
uint32
424+
aot_module_malloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
425+
uint32 size, void **p_native_addr);
426+
427+
uint32
428+
aot_module_realloc_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
429+
uint32 ptr, uint32 size, void **p_native_addr);
430+
431+
void
432+
aot_module_free_internal(AOTModuleInstance *module_inst, WASMExecEnv *env,
433+
uint32 ptr);
434+
427435
uint32
428436
aot_module_malloc(AOTModuleInstance *module_inst, uint32 size,
429437
void **p_native_addr);

core/iwasm/common/wasm_application.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,11 @@ wasm_application_execute_main(WASMModuleInstanceCommon *module_inst, int32 argc,
225225
ret = wasm_runtime_get_exception(module_inst) == NULL;
226226

227227
#if WASM_ENABLE_DUMP_CALL_STACK != 0
228-
if (!ret)
229-
wasm_runtime_dump_call_stack(exec_env);
228+
if (!ret) {
229+
exec_env = wasm_runtime_get_exec_env_singleton(module_inst);
230+
if (exec_env)
231+
wasm_runtime_dump_call_stack(exec_env);
232+
}
230233
#endif
231234

232235
return ret;

0 commit comments

Comments
 (0)