Skip to content

Commit c9a4716

Browse files
authored
Merge pull request #6 from xujuntwt95329/internal/feature
fix aux stack covered issue
2 parents 36d1b6f + 6433b48 commit c9a4716

File tree

4 files changed

+101
-17
lines changed

4 files changed

+101
-17
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#if WASM_ENABLE_SHARED_MEMORY != 0
1111
#include "../common/wasm_shared_memory.h"
1212
#endif
13+
#if WASM_ENABLE_THREAD_MGR != 0
14+
#include "../libraries/thread-mgr/thread_manager.h"
15+
#endif
1316

1417
static void
1518
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
@@ -1319,17 +1322,33 @@ aot_create_exec_env_and_call_function(AOTModuleInstance *module_inst,
13191322
WASMExecEnv *exec_env;
13201323
bool ret;
13211324

1322-
if (!(exec_env = wasm_exec_env_create((WASMModuleInstanceCommon*)module_inst,
1323-
module_inst->default_wasm_stack_size))) {
1324-
aot_set_exception(module_inst, "allocate memory failed");
1325-
return false;
1326-
}
1325+
#if WASM_ENABLE_THREAD_MGR != 0
1326+
WASMExecEnv *existing_exec_env = NULL;
13271327

1328-
/* set thread handle and stack boundary */
1329-
wasm_exec_env_set_thread_info(exec_env);
1328+
if (!(existing_exec_env = exec_env =
1329+
wasm_clusters_search_exec_env(
1330+
(WASMModuleInstanceCommon*)module_inst))) {
1331+
#endif
1332+
if (!(exec_env = wasm_exec_env_create((WASMModuleInstanceCommon*)module_inst,
1333+
module_inst->default_wasm_stack_size))) {
1334+
aot_set_exception(module_inst, "allocate memory failed");
1335+
return false;
1336+
}
1337+
1338+
/* set thread handle and stack boundary */
1339+
wasm_exec_env_set_thread_info(exec_env);
1340+
#if WASM_ENABLE_THREAD_MGR != 0
1341+
}
1342+
#endif
13301343

13311344
ret = aot_call_function(exec_env, func, argc, argv);
1332-
wasm_exec_env_destroy(exec_env);
1345+
1346+
#if WASM_ENABLE_THREAD_MGR != 0
1347+
/* don't destroy the exec_env if it's searched from the cluster */
1348+
if (!existing_exec_env)
1349+
#endif
1350+
wasm_exec_env_destroy(exec_env);
1351+
13331352
return ret;
13341353
}
13351354

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#if WASM_ENABLE_SHARED_MEMORY != 0
1414
#include "../common/wasm_shared_memory.h"
1515
#endif
16+
#if WASM_ENABLE_THREAD_MGR != 0
17+
#include "../libraries/thread-mgr/thread_manager.h"
18+
#endif
1619

1720
static void
1821
set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
@@ -1593,18 +1596,34 @@ wasm_create_exec_env_and_call_function(WASMModuleInstance *module_inst,
15931596
WASMExecEnv *exec_env;
15941597
bool ret;
15951598

1596-
if (!(exec_env = wasm_exec_env_create(
1597-
(WASMModuleInstanceCommon*)module_inst,
1598-
module_inst->default_wasm_stack_size))) {
1599-
wasm_set_exception(module_inst, "allocate memory failed");
1600-
return false;
1601-
}
1599+
#if WASM_ENABLE_THREAD_MGR != 0
1600+
WASMExecEnv *existing_exec_env = NULL;
1601+
1602+
if (!(existing_exec_env = exec_env =
1603+
wasm_clusters_search_exec_env(
1604+
(WASMModuleInstanceCommon*)module_inst))) {
1605+
#endif
1606+
if (!(exec_env = wasm_exec_env_create(
1607+
(WASMModuleInstanceCommon*)module_inst,
1608+
module_inst->default_wasm_stack_size))) {
1609+
wasm_set_exception(module_inst, "allocate memory failed");
1610+
return false;
1611+
}
16021612

1603-
/* set thread handle and stack boundary */
1604-
wasm_exec_env_set_thread_info(exec_env);
1613+
/* set thread handle and stack boundary */
1614+
wasm_exec_env_set_thread_info(exec_env);
1615+
#if WASM_ENABLE_THREAD_MGR != 0
1616+
}
1617+
#endif
16051618

16061619
ret = wasm_call_function(exec_env, func, argc, argv);
1607-
wasm_exec_env_destroy(exec_env);
1620+
1621+
#if WASM_ENABLE_THREAD_MGR != 0
1622+
/* don't destroy the exec_env if it's searched from the cluster */
1623+
if (!existing_exec_env)
1624+
#endif
1625+
wasm_exec_env_destroy(exec_env);
1626+
16081627
return ret;
16091628
}
16101629

core/iwasm/libraries/thread-mgr/thread_manager.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,49 @@ wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
281281
return ret;
282282
}
283283

284+
static WASMExecEnv *
285+
wasm_cluster_search_exec_env(WASMCluster *cluster,
286+
WASMModuleInstanceCommon *module_inst)
287+
{
288+
WASMExecEnv *node = NULL;
289+
290+
os_mutex_lock(&cluster->lock);
291+
node = bh_list_first_elem(&cluster->exec_env_list);
292+
while (node) {
293+
if (node->module_inst == module_inst) {
294+
os_mutex_unlock(&cluster->lock);
295+
return node;
296+
}
297+
node = bh_list_elem_next(node);
298+
}
299+
300+
os_mutex_unlock(&cluster->lock);
301+
return NULL;
302+
}
303+
304+
/* search the global cluster list to find if the given
305+
module instance have a corresponding exec_env */
306+
WASMExecEnv *
307+
wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst)
308+
{
309+
WASMCluster *cluster = NULL;
310+
WASMExecEnv *exec_env = NULL;
311+
312+
os_mutex_lock(&cluster_list_lock);
313+
cluster = bh_list_first_elem(cluster_list);
314+
while (cluster) {
315+
exec_env = wasm_cluster_search_exec_env(cluster, module_inst);
316+
if (exec_env) {
317+
os_mutex_unlock(&cluster_list_lock);
318+
return exec_env;
319+
}
320+
cluster = bh_list_elem_next(cluster);
321+
}
322+
323+
os_mutex_unlock(&cluster_list_lock);
324+
return NULL;
325+
}
326+
284327
WASMExecEnv *
285328
wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
286329
{

core/iwasm/libraries/thread-mgr/thread_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
106106
bool
107107
wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
108108

109+
WASMExecEnv *
110+
wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst);
111+
109112
void
110113
wasm_cluster_spread_exception(WASMExecEnv *exec_env);
111114

0 commit comments

Comments
 (0)