Skip to content

Commit eb4b5f1

Browse files
committed
fix: missing locks in wasm_c_api
- add missing lock in check_loaded_module() - add missing lock in wasm_module_exports() - add missing lock in wasm_module_serialize()
1 parent 14d84ec commit eb4b5f1

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

core/iwasm/common/wasm_c_api.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,12 +2206,15 @@ check_loaded_module(Vector *modules, char *binary_hash)
22062206
return NULL;
22072207
}
22082208

2209-
if (!module->ref_count)
2210-
/* deleted */
2211-
continue;
2209+
os_mutex_lock(&module->lock);
2210+
bool is_valid =
2211+
(module->ref_count > 0
2212+
&& memcmp(module->hash, binary_hash, SHA256_DIGEST_LENGTH) == 0);
2213+
os_mutex_unlock(&module->lock);
22122214

2213-
if (memcmp(module->hash, binary_hash, SHA256_DIGEST_LENGTH) == 0)
2215+
if (is_valid) {
22142216
return module;
2217+
}
22152218
}
22162219
return NULL;
22172220
}
@@ -2456,8 +2459,13 @@ wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
24562459
return;
24572460
}
24582461

2459-
if (((const wasm_module_ex_t *)(module))->ref_count == 0)
2462+
wasm_module_ex_t *module_ex = module_to_module_ext((wasm_module_t *)module);
2463+
os_mutex_lock(&module_ex->lock);
2464+
if (module_ex->ref_count == 0) {
2465+
os_mutex_unlock(&module_ex->lock);
24602466
return;
2467+
}
2468+
os_mutex_unlock(&module_ex->lock);
24612469

24622470
#if WASM_ENABLE_INTERP != 0
24632471
if ((*module)->module_type == Wasm_Module_Bytecode) {
@@ -2696,8 +2704,13 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out)
26962704
return;
26972705
}
26982706

2699-
if (((const wasm_module_ex_t *)(module))->ref_count == 0)
2707+
wasm_module_ex_t *module_ex = module_to_module_ext((wasm_module_t *)module);
2708+
os_mutex_lock(&module_ex->lock);
2709+
if (module_ex->ref_count == 0) {
2710+
os_mutex_unlock(&module_ex->lock);
27002711
return;
2712+
}
2713+
os_mutex_unlock(&module_ex->lock);
27012714

27022715
#if WASM_ENABLE_INTERP != 0
27032716
if ((*module)->module_type == Wasm_Module_Bytecode) {
@@ -2891,10 +2904,13 @@ wasm_module_serialize(wasm_module_t *module, own wasm_byte_vec_t *out)
28912904
if (!module || !out)
28922905
return;
28932906

2894-
if (((const wasm_module_ex_t *)(module))->ref_count == 0)
2895-
return;
2896-
28972907
module_ex = module_to_module_ext(module);
2908+
os_mutex_lock(&module_ex->lock);
2909+
if (module_ex->ref_count == 0) {
2910+
os_mutex_unlock(&module_ex->lock);
2911+
return;
2912+
}
2913+
os_mutex_unlock(&module_ex->lock);
28982914
comp_ctx = ((WASMModule *)(module_ex->module_comm_rt))->comp_ctx;
28992915
comp_data = ((WASMModule *)(module_ex->module_comm_rt))->comp_data;
29002916
bh_assert(comp_ctx != NULL && comp_data != NULL);

0 commit comments

Comments
 (0)