@@ -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
11071125fail :
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