Skip to content

Commit 3004ee6

Browse files
committed
Stop abusing shared memory lock to protect exception
Use a separate global lock instead. Fixes: #2407
1 parent ff151fb commit 3004ee6

File tree

3 files changed

+43
-36
lines changed

3 files changed

+43
-36
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,21 +2379,15 @@ wasm_set_exception(WASMModuleInstance *module_inst, const char *exception)
23792379
{
23802380
WASMExecEnv *exec_env = NULL;
23812381

2382-
#if WASM_ENABLE_SHARED_MEMORY != 0
2383-
if (module_inst->memory_count > 0)
2384-
shared_memory_lock(module_inst->memories[0]);
2385-
#endif
2382+
exception_lock(module_inst);
23862383
if (exception) {
23872384
snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception),
23882385
"Exception: %s", exception);
23892386
}
23902387
else {
23912388
module_inst->cur_exception[0] = '\0';
23922389
}
2393-
#if WASM_ENABLE_SHARED_MEMORY != 0
2394-
if (module_inst->memory_count > 0)
2395-
shared_memory_unlock(module_inst->memories[0]);
2396-
#endif
2390+
exception_unlock(module_inst);
23972391

23982392
#if WASM_ENABLE_THREAD_MGR != 0
23992393
exec_env =
@@ -2453,10 +2447,7 @@ wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf)
24532447
{
24542448
bool has_exception = false;
24552449

2456-
#if WASM_ENABLE_SHARED_MEMORY != 0
2457-
if (module_inst->memory_count > 0)
2458-
shared_memory_lock(module_inst->memories[0]);
2459-
#endif
2450+
exception_lock(module_inst);
24602451
if (module_inst->cur_exception[0] != '\0') {
24612452
/* NULL is passed if the caller is not interested in getting the
24622453
* exception content, but only in knowing if an exception has been
@@ -2468,10 +2459,7 @@ wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf)
24682459
sizeof(module_inst->cur_exception));
24692460
has_exception = true;
24702461
}
2471-
#if WASM_ENABLE_SHARED_MEMORY != 0
2472-
if (module_inst->memory_count > 0)
2473-
shared_memory_unlock(module_inst->memories[0]);
2474-
#endif
2462+
exception_unlock(module_inst);
24752463

24762464
return has_exception;
24772465
}

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,16 @@ void
668668
wasm_propagate_wasi_args(WASMModule *module);
669669
#endif
670670

671+
#if WASM_ENABLE_THREAD_MGR != 0
672+
void
673+
exception_lock(WASMModuleInstance *module_inst);
674+
void
675+
exception_unlock(WASMModuleInstance *module_inst);
676+
#else
677+
#define exception_lock(module_inst) (void)(module_inst)
678+
#define exception_unlock(module_inst) (void)(module_inst)
679+
#endif
680+
671681
#ifdef __cplusplus
672682
}
673683
#endif

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

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#include "debug_engine.h"
1717
#endif
1818

19-
#if WASM_ENABLE_SHARED_MEMORY != 0
20-
#include "wasm_shared_memory.h"
21-
#endif
22-
2319
typedef struct {
2420
bh_list_link l;
2521
void (*destroy_cb)(WASMCluster *);
@@ -32,6 +28,8 @@ static bh_list cluster_list_head;
3228
static bh_list *const cluster_list = &cluster_list_head;
3329
static korp_mutex cluster_list_lock;
3430

31+
static korp_mutex _exception_lock;
32+
3533
typedef void (*list_visitor)(void *, void *);
3634

3735
static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM;
@@ -52,6 +50,10 @@ thread_manager_init()
5250
return false;
5351
if (os_mutex_init(&cluster_list_lock) != 0)
5452
return false;
53+
if (os_mutex_init(&_exception_lock) != 0) {
54+
os_mutex_destroy(&cluster_list_lock);
55+
return false;
56+
}
5557
return true;
5658
}
5759

@@ -66,6 +68,7 @@ thread_manager_destroy()
6668
cluster = next;
6769
}
6870
wasm_cluster_cancel_all_callbacks();
71+
os_mutex_destroy(&_exception_lock);
6972
os_mutex_destroy(&cluster_list_lock);
7073
}
7174

@@ -1253,20 +1256,14 @@ set_exception_visitor(void *node, void *user_data)
12531256
(WASMModuleInstance *)get_module_inst(curr_exec_env);
12541257

12551258
/* Only spread non "wasi proc exit" exception */
1256-
#if WASM_ENABLE_SHARED_MEMORY != 0
1257-
if (curr_wasm_inst->memory_count > 0)
1258-
shared_memory_lock(curr_wasm_inst->memories[0]);
1259-
#endif
1259+
exception_lock(curr_wasm_inst);
12601260
if (!strstr(wasm_inst->cur_exception, "wasi proc exit")) {
12611261
bh_memcpy_s(curr_wasm_inst->cur_exception,
12621262
sizeof(curr_wasm_inst->cur_exception),
12631263
wasm_inst->cur_exception,
12641264
sizeof(wasm_inst->cur_exception));
12651265
}
1266-
#if WASM_ENABLE_SHARED_MEMORY != 0
1267-
if (curr_wasm_inst->memory_count > 0)
1268-
shared_memory_unlock(curr_wasm_inst->memories[0]);
1269-
#endif
1266+
exception_unlock(curr_wasm_inst);
12701267

12711268
/* Terminate the thread so it can exit from dead loops */
12721269
set_thread_cancel_flags(curr_exec_env);
@@ -1283,15 +1280,9 @@ clear_exception_visitor(void *node, void *user_data)
12831280
WASMModuleInstance *curr_wasm_inst =
12841281
(WASMModuleInstance *)get_module_inst(curr_exec_env);
12851282

1286-
#if WASM_ENABLE_SHARED_MEMORY != 0
1287-
if (curr_wasm_inst->memory_count > 0)
1288-
shared_memory_lock(curr_wasm_inst->memories[0]);
1289-
#endif
1283+
exception_lock(curr_wasm_inst);
12901284
curr_wasm_inst->cur_exception[0] = '\0';
1291-
#if WASM_ENABLE_SHARED_MEMORY != 0
1292-
if (curr_wasm_inst->memory_count > 0)
1293-
shared_memory_unlock(curr_wasm_inst->memories[0]);
1294-
#endif
1285+
exception_unlock(curr_wasm_inst);
12951286
}
12961287
}
12971288

@@ -1353,3 +1344,21 @@ wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
13531344

13541345
return is_thread_terminated;
13551346
}
1347+
1348+
void
1349+
exception_lock(WASMModuleInstance *module_inst)
1350+
{
1351+
/*
1352+
* Note: this lock could be per module instance if desirable.
1353+
* We can revisit on AOT version bump.
1354+
* It probably doesn't matter though because the exception handling
1355+
* logic should not be executed too frequently anyway.
1356+
*/
1357+
os_mutex_lock(&_exception_lock);
1358+
}
1359+
1360+
void
1361+
exception_unlock(WASMModuleInstance *module_inst)
1362+
{
1363+
os_mutex_unlock(&_exception_lock);
1364+
}

0 commit comments

Comments
 (0)