Skip to content

Commit 10f1bf3

Browse files
authored
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.
1 parent a35d39b commit 10f1bf3

File tree

2 files changed

+204
-44
lines changed

2 files changed

+204
-44
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
932932
#ifdef OS_ENABLE_HW_BOUND_CHECK
933933
WASMExecEnv *exec_env_tls = NULL;
934934
#endif
935-
WASMExecEnv *exec_env = NULL;
935+
WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
936936
bool ret = false;
937937

938938
#if WASM_ENABLE_LIBC_WASI != 0
@@ -990,11 +990,29 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
990990
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
991991
}
992992
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;
993+
/* Try using the existing exec_env */
994+
#ifdef OS_ENABLE_HW_BOUND_CHECK
995+
exec_env = exec_env_tls;
996+
#endif
997+
#if WASM_ENABLE_THREAD_MGR != 0
998+
if (!exec_env)
999+
exec_env = wasm_clusters_search_exec_env(
1000+
(WASMModuleInstanceCommon *)module_inst);
1001+
#endif
1002+
if (!exec_env) {
1003+
if (!(exec_env = exec_env_created = wasm_exec_env_create(
1004+
(WASMModuleInstanceCommon *)module_inst,
1005+
module_inst->default_wasm_stack_size))) {
1006+
aot_set_exception(module_inst, "allocate memory failed");
1007+
return false;
1008+
}
1009+
}
1010+
else {
1011+
/* Temporarily replace exec_env's module inst with current
1012+
module inst to ensure that the exec_env's module inst
1013+
is the correct one. */
1014+
module_inst_main = exec_env->module_inst;
1015+
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
9981016
}
9991017
}
10001018

@@ -1033,11 +1051,17 @@ execute_post_instantiate_functions(AOTModuleInstance *module_inst,
10331051
ret = true;
10341052

10351053
fail:
1036-
if (is_sub_inst)
1054+
if (is_sub_inst) {
10371055
/* Restore the parent exec_env's module inst */
10381056
exec_env_main->module_inst = module_inst_main;
1039-
else
1040-
wasm_exec_env_destroy(exec_env);
1057+
}
1058+
else {
1059+
if (module_inst_main)
1060+
/* Restore the existing exec_env's module inst */
1061+
exec_env->module_inst = module_inst_main;
1062+
if (exec_env_created)
1063+
wasm_exec_env_destroy(exec_env_created);
1064+
}
10411065

10421066
return ret;
10431067
}
@@ -1596,6 +1620,8 @@ execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
15961620
#ifdef OS_ENABLE_HW_BOUND_CHECK
15971621
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
15981622
#endif
1623+
WASMExecEnv *exec_env_created = NULL;
1624+
WASMModuleInstanceCommon *module_inst_old = NULL;
15991625
uint32 argv[2], argc;
16001626
bool ret;
16011627

@@ -1616,19 +1642,43 @@ execute_malloc_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
16161642
== (WASMModuleInstanceCommon *)module_inst);
16171643
}
16181644
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;
1645+
/* Try using the existing exec_env */
1646+
#ifdef OS_ENABLE_HW_BOUND_CHECK
1647+
exec_env = exec_env_tls;
1648+
#endif
1649+
#if WASM_ENABLE_THREAD_MGR != 0
1650+
if (!exec_env)
1651+
exec_env = wasm_clusters_search_exec_env(
1652+
(WASMModuleInstanceCommon *)module_inst);
1653+
#endif
1654+
if (!exec_env) {
1655+
if (!(exec_env = exec_env_created = wasm_exec_env_create(
1656+
(WASMModuleInstanceCommon *)module_inst,
1657+
module_inst->default_wasm_stack_size))) {
1658+
wasm_set_exception(module_inst, "allocate memory failed");
1659+
return false;
1660+
}
1661+
}
1662+
else {
1663+
/* Temporarily replace exec_env's module inst with current
1664+
module inst to ensure that the exec_env's module inst
1665+
is the correct one. */
1666+
module_inst_old = exec_env->module_inst;
1667+
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
16241668
}
16251669
}
16261670

16271671
ret = aot_call_function(exec_env, malloc_func, argc, argv);
16281672

1629-
if (retain_func && ret) {
1673+
if (retain_func && ret)
16301674
ret = aot_call_function(exec_env, retain_func, 1, argv);
1631-
}
1675+
1676+
if (module_inst_old)
1677+
/* Restore the existing exec_env's module inst */
1678+
exec_env->module_inst = module_inst_old;
1679+
1680+
if (exec_env_created)
1681+
wasm_exec_env_destroy(exec_env_created);
16321682

16331683
if (ret)
16341684
*p_result = argv[0];
@@ -1642,7 +1692,10 @@ execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
16421692
#ifdef OS_ENABLE_HW_BOUND_CHECK
16431693
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
16441694
#endif
1695+
WASMExecEnv *exec_env_created = NULL;
1696+
WASMModuleInstanceCommon *module_inst_old = NULL;
16451697
uint32 argv[2];
1698+
bool ret;
16461699

16471700
argv[0] = offset;
16481701

@@ -1656,15 +1709,42 @@ execute_free_function(AOTModuleInstance *module_inst, WASMExecEnv *exec_env,
16561709
== (WASMModuleInstanceCommon *)module_inst);
16571710
}
16581711
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;
1712+
/* Try using the existing exec_env */
1713+
#ifdef OS_ENABLE_HW_BOUND_CHECK
1714+
exec_env = exec_env_tls;
1715+
#endif
1716+
#if WASM_ENABLE_THREAD_MGR != 0
1717+
if (!exec_env)
1718+
exec_env = wasm_clusters_search_exec_env(
1719+
(WASMModuleInstanceCommon *)module_inst);
1720+
#endif
1721+
if (!exec_env) {
1722+
if (!(exec_env = exec_env_created = wasm_exec_env_create(
1723+
(WASMModuleInstanceCommon *)module_inst,
1724+
module_inst->default_wasm_stack_size))) {
1725+
wasm_set_exception(module_inst, "allocate memory failed");
1726+
return false;
1727+
}
1728+
}
1729+
else {
1730+
/* Temporarily replace exec_env's module inst with current
1731+
module inst to ensure that the exec_env's module inst
1732+
is the correct one. */
1733+
module_inst_old = exec_env->module_inst;
1734+
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
16641735
}
16651736
}
16661737

1667-
return aot_call_function(exec_env, free_func, 1, argv);
1738+
ret = aot_call_function(exec_env, free_func, 1, argv);
1739+
1740+
if (module_inst_old)
1741+
/* Restore the existing exec_env's module inst */
1742+
exec_env->module_inst = module_inst_old;
1743+
1744+
if (exec_env_created)
1745+
wasm_exec_env_destroy(exec_env_created);
1746+
1747+
return ret;
16681748
}
16691749

16701750
uint32

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
10161016
#ifdef OS_ENABLE_HW_BOUND_CHECK
10171017
WASMExecEnv *exec_env_tls = NULL;
10181018
#endif
1019-
WASMExecEnv *exec_env = NULL;
1019+
WASMExecEnv *exec_env = NULL, *exec_env_created = NULL;
10201020
bool ret = false;
10211021

10221022
#if WASM_ENABLE_LIBC_WASI != 0
@@ -1074,11 +1074,29 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
10741074
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
10751075
}
10761076
else {
1077-
if (!(exec_env =
1078-
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
1079-
module_inst->default_wasm_stack_size))) {
1080-
wasm_set_exception(module_inst, "allocate memory failed");
1081-
return false;
1077+
/* Try using the existing exec_env */
1078+
#ifdef OS_ENABLE_HW_BOUND_CHECK
1079+
exec_env = exec_env_tls;
1080+
#endif
1081+
#if WASM_ENABLE_THREAD_MGR != 0
1082+
if (!exec_env)
1083+
exec_env = wasm_clusters_search_exec_env(
1084+
(WASMModuleInstanceCommon *)module_inst);
1085+
#endif
1086+
if (!exec_env) {
1087+
if (!(exec_env = exec_env_created = wasm_exec_env_create(
1088+
(WASMModuleInstanceCommon *)module_inst,
1089+
module_inst->default_wasm_stack_size))) {
1090+
wasm_set_exception(module_inst, "allocate memory failed");
1091+
return false;
1092+
}
1093+
}
1094+
else {
1095+
/* Temporarily replace exec_env's module inst with current
1096+
module inst to ensure that the exec_env's module inst
1097+
is the correct one. */
1098+
module_inst_main = exec_env->module_inst;
1099+
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
10821100
}
10831101
}
10841102

@@ -1105,11 +1123,17 @@ execute_post_instantiate_functions(WASMModuleInstance *module_inst,
11051123
ret = true;
11061124

11071125
fail:
1108-
if (is_sub_inst)
1126+
if (is_sub_inst) {
11091127
/* Restore the parent exec_env's module inst */
11101128
exec_env_main->module_inst = module_inst_main;
1111-
else
1112-
wasm_exec_env_destroy(exec_env);
1129+
}
1130+
else {
1131+
if (module_inst_main)
1132+
/* Restore the existing exec_env's module inst */
1133+
exec_env->module_inst = module_inst_main;
1134+
if (exec_env_created)
1135+
wasm_exec_env_destroy(exec_env_created);
1136+
}
11131137

11141138
return ret;
11151139
}
@@ -1123,6 +1147,8 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
11231147
#ifdef OS_ENABLE_HW_BOUND_CHECK
11241148
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
11251149
#endif
1150+
WASMExecEnv *exec_env_created = NULL;
1151+
WASMModuleInstanceCommon *module_inst_old = NULL;
11261152
uint32 argv[2], argc;
11271153
bool ret;
11281154

@@ -1151,19 +1177,43 @@ execute_malloc_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
11511177
== (WASMModuleInstanceCommon *)module_inst);
11521178
}
11531179
else {
1154-
if (!(exec_env =
1155-
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
1156-
module_inst->default_wasm_stack_size))) {
1157-
wasm_set_exception(module_inst, "allocate memory failed");
1158-
return false;
1180+
/* Try using the existing exec_env */
1181+
#ifdef OS_ENABLE_HW_BOUND_CHECK
1182+
exec_env = exec_env_tls;
1183+
#endif
1184+
#if WASM_ENABLE_THREAD_MGR != 0
1185+
if (!exec_env)
1186+
exec_env = wasm_clusters_search_exec_env(
1187+
(WASMModuleInstanceCommon *)module_inst);
1188+
#endif
1189+
if (!exec_env) {
1190+
if (!(exec_env = exec_env_created = wasm_exec_env_create(
1191+
(WASMModuleInstanceCommon *)module_inst,
1192+
module_inst->default_wasm_stack_size))) {
1193+
wasm_set_exception(module_inst, "allocate memory failed");
1194+
return false;
1195+
}
1196+
}
1197+
else {
1198+
/* Temporarily replace exec_env's module inst with current
1199+
module inst to ensure that the exec_env's module inst
1200+
is the correct one. */
1201+
module_inst_old = exec_env->module_inst;
1202+
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
11591203
}
11601204
}
11611205

11621206
ret = wasm_call_function(exec_env, malloc_func, argc, argv);
11631207

1164-
if (retain_func && ret) {
1208+
if (retain_func && ret)
11651209
ret = wasm_call_function(exec_env, retain_func, 1, argv);
1166-
}
1210+
1211+
if (module_inst_old)
1212+
/* Restore the existing exec_env's module inst */
1213+
exec_env->module_inst = module_inst_old;
1214+
1215+
if (exec_env_created)
1216+
wasm_exec_env_destroy(exec_env_created);
11671217

11681218
if (ret)
11691219
*p_result = argv[0];
@@ -1177,7 +1227,10 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
11771227
#ifdef OS_ENABLE_HW_BOUND_CHECK
11781228
WASMExecEnv *exec_env_tls = wasm_runtime_get_exec_env_tls();
11791229
#endif
1230+
WASMExecEnv *exec_env_created = NULL;
1231+
WASMModuleInstanceCommon *module_inst_old = NULL;
11801232
uint32 argv[2];
1233+
bool ret;
11811234

11821235
argv[0] = offset;
11831236

@@ -1191,15 +1244,42 @@ execute_free_function(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
11911244
== (WASMModuleInstanceCommon *)module_inst);
11921245
}
11931246
else {
1194-
if (!(exec_env =
1195-
wasm_exec_env_create((WASMModuleInstanceCommon *)module_inst,
1196-
module_inst->default_wasm_stack_size))) {
1197-
wasm_set_exception(module_inst, "allocate memory failed");
1198-
return false;
1247+
/* Try using the existing exec_env */
1248+
#ifdef OS_ENABLE_HW_BOUND_CHECK
1249+
exec_env = exec_env_tls;
1250+
#endif
1251+
#if WASM_ENABLE_THREAD_MGR != 0
1252+
if (!exec_env)
1253+
exec_env = wasm_clusters_search_exec_env(
1254+
(WASMModuleInstanceCommon *)module_inst);
1255+
#endif
1256+
if (!exec_env) {
1257+
if (!(exec_env = exec_env_created = wasm_exec_env_create(
1258+
(WASMModuleInstanceCommon *)module_inst,
1259+
module_inst->default_wasm_stack_size))) {
1260+
wasm_set_exception(module_inst, "allocate memory failed");
1261+
return false;
1262+
}
1263+
}
1264+
else {
1265+
/* Temporarily replace exec_env's module inst with current
1266+
module inst to ensure that the exec_env's module inst
1267+
is the correct one. */
1268+
module_inst_old = exec_env->module_inst;
1269+
exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
11991270
}
12001271
}
12011272

1202-
return wasm_call_function(exec_env, free_func, 1, argv);
1273+
ret = wasm_call_function(exec_env, free_func, 1, argv);
1274+
1275+
if (module_inst_old)
1276+
/* Restore the existing exec_env's module inst */
1277+
exec_env->module_inst = module_inst_old;
1278+
1279+
if (exec_env_created)
1280+
wasm_exec_env_destroy(exec_env_created);
1281+
1282+
return ret;
12031283
}
12041284

12051285
#if WASM_ENABLE_MULTI_MODULE != 0

0 commit comments

Comments
 (0)