-
Notifications
You must be signed in to change notification settings - Fork 750
Fix memory sharing #2415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix memory sharing #2415
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1196,22 +1196,23 @@ wasm_runtime_unload(WASMModuleCommon *module) | |
| } | ||
|
|
||
| WASMModuleInstanceCommon * | ||
| wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst, | ||
| wasm_runtime_instantiate_internal(WASMModuleCommon *module, | ||
| WASMModuleInstanceCommon *parent, | ||
| WASMExecEnv *exec_env_main, uint32 stack_size, | ||
| uint32 heap_size, char *error_buf, | ||
| uint32 error_buf_size) | ||
| { | ||
| #if WASM_ENABLE_INTERP != 0 | ||
| if (module->module_type == Wasm_Module_Bytecode) | ||
| return (WASMModuleInstanceCommon *)wasm_instantiate( | ||
| (WASMModule *)module, is_sub_inst, exec_env_main, stack_size, | ||
| heap_size, error_buf, error_buf_size); | ||
| (WASMModule *)module, (WASMModuleInstance *)parent, exec_env_main, | ||
| stack_size, heap_size, error_buf, error_buf_size); | ||
| #endif | ||
| #if WASM_ENABLE_AOT != 0 | ||
| if (module->module_type == Wasm_Module_AoT) | ||
| return (WASMModuleInstanceCommon *)aot_instantiate( | ||
| (AOTModule *)module, is_sub_inst, exec_env_main, stack_size, | ||
| heap_size, error_buf, error_buf_size); | ||
| (AOTModule *)module, (AOTModuleInstance *)parent, exec_env_main, | ||
| stack_size, heap_size, error_buf, error_buf_size); | ||
| #endif | ||
| set_error_buf(error_buf, error_buf_size, | ||
| "Instantiate module failed, invalid module type"); | ||
|
|
@@ -1224,7 +1225,7 @@ wasm_runtime_instantiate(WASMModuleCommon *module, uint32 stack_size, | |
| uint32 error_buf_size) | ||
| { | ||
| return wasm_runtime_instantiate_internal( | ||
| module, false, NULL, stack_size, heap_size, error_buf, error_buf_size); | ||
| module, NULL, NULL, stack_size, heap_size, error_buf, error_buf_size); | ||
| } | ||
|
|
||
| void | ||
|
|
@@ -2310,10 +2311,8 @@ wasm_set_exception(WASMModuleInstance *module_inst, const char *exception) | |
| WASMExecEnv *exec_env = NULL; | ||
|
|
||
| #if WASM_ENABLE_SHARED_MEMORY != 0 | ||
| WASMSharedMemNode *node = | ||
| wasm_module_get_shared_memory((WASMModuleCommon *)module_inst->module); | ||
| if (node) | ||
| os_mutex_lock(&node->shared_mem_lock); | ||
| if (module_inst->memory_count > 0) | ||
| shared_memory_lock(module_inst->memories[0]); | ||
|
Comment on lines
+2314
to
+2315
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wenyongh Do you remember why we need to lock the shared memory when setting exception?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #2407
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I remember that one instance's exception may be accessed by other threads when spreading non "wasi proc exit" exception. It was introduced in this PR #1995 In thread_manager.c, function set_exception_visitor: /* Only spread non "wasi proc exit" exception */
#if WASM_ENABLE_SHARED_MEMORY != 0
WASMSharedMemNode *shared_mem_node = wasm_module_get_shared_memory(
(WASMModuleCommon *)curr_wasm_inst->module);
if (shared_mem_node)
os_mutex_lock(&shared_mem_node->shared_mem_lock);
#endif
if (!strstr(wasm_inst->cur_exception, "wasi proc exit")) {
bh_memcpy_s(curr_wasm_inst->cur_exception,
sizeof(curr_wasm_inst->cur_exception),
wasm_inst->cur_exception,
sizeof(wasm_inst->cur_exception));
}
#if WASM_ENABLE_SHARED_MEMORY != 0
if (shared_mem_node)
os_mutex_unlock(&shared_mem_node->shared_mem_lock);
#endif
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that's the reason. When a thread runs into an exception it spreads it to other threads by using
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks a lot!
Comment on lines
+2314
to
+2315
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wenyongh Do you remember why we need to lock the shared memory when setting exception? |
||
| #endif | ||
| if (exception) { | ||
| snprintf(module_inst->cur_exception, sizeof(module_inst->cur_exception), | ||
|
|
@@ -2323,8 +2322,8 @@ wasm_set_exception(WASMModuleInstance *module_inst, const char *exception) | |
| module_inst->cur_exception[0] = '\0'; | ||
| } | ||
| #if WASM_ENABLE_SHARED_MEMORY != 0 | ||
| if (node) | ||
| os_mutex_unlock(&node->shared_mem_lock); | ||
| if (module_inst->memory_count > 0) | ||
| shared_memory_unlock(module_inst->memories[0]); | ||
| #endif | ||
|
|
||
| #if WASM_ENABLE_THREAD_MGR != 0 | ||
|
|
@@ -2386,10 +2385,8 @@ wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf) | |
| bool has_exception = false; | ||
|
|
||
| #if WASM_ENABLE_SHARED_MEMORY != 0 | ||
| WASMSharedMemNode *node = | ||
| wasm_module_get_shared_memory((WASMModuleCommon *)module_inst->module); | ||
| if (node) | ||
| os_mutex_lock(&node->shared_mem_lock); | ||
| if (module_inst->memory_count > 0) | ||
| shared_memory_lock(module_inst->memories[0]); | ||
| #endif | ||
| if (module_inst->cur_exception[0] != '\0') { | ||
| /* NULL is passed if the caller is not interested in getting the | ||
|
|
@@ -2403,8 +2400,8 @@ wasm_copy_exception(WASMModuleInstance *module_inst, char *exception_buf) | |
| has_exception = true; | ||
| } | ||
| #if WASM_ENABLE_SHARED_MEMORY != 0 | ||
| if (node) | ||
| os_mutex_unlock(&node->shared_mem_lock); | ||
| if (module_inst->memory_count > 0) | ||
| shared_memory_unlock(module_inst->memories[0]); | ||
| #endif | ||
|
|
||
| return has_exception; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.