Skip to content
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 three threading+callback issues #2173

Merged
merged 7 commits into from
May 5, 2023
Merged

Conversation

cpetig
Copy link
Contributor

@cpetig cpetig commented May 3, 2023

Testcase is available at #2172 , fixes also #2149 and an unreported mistake in the original code I copied the fix from.

@cpetig
Copy link
Contributor Author

cpetig commented May 3, 2023

This is a superset of https://github.com/bytecodealliance/wasm-micr4o-runtime/pull/2170 and misses the mentioned future refactor and both cleanup codes (including the one included there).

Also the copying code is inserted in a different place here and in pull 2170, the other one looks more in line with the existing pthread code. I suspect that this change in wasm_runtime.c makes the original workaround in lib_pthread_wrapper.c unnecessary.

->e->c_api_func_imports
!= NULL) {
/* workaround about passing instantiate-linking information */
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpetig It is to fix the issue of c_api_func_imports are not passed to new threads reported in #2149 , right? My suggestion is to apply the changes to lib_wasi_threads_wrapper.c like #2170 since (1) it is only related to multi-threading, adding code here may increase the footprint, (2) the fixing for AOT mode is invalid, the related code is in aot_instantiate: https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/aot/aot_runtime.c#L1089

@eloparco How about we merging the fixing of #2170 to this PR? Just use this PR to fix all these issues? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, there may be duplicated code in lib_wasi_threads_wrapper.c and lib_pthread_wrapper.c, after discussing with @lum1n0us, we suggest to add an API in core/iwasm/libraries/thread-mgr/thread_manager.{h|c}, e.g.,

bool
wasm_cluster_dup_c_api_imports(WASMModuleInstance *module_inst_dst,
                               const WASMModuleInstance *module_inst_src);

@cpetig @eloparco How about that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib_pthread_wrapper.c has a same workaround. It doesn't seem clever to write the same workaround twice. Shall we merge them together and store it somewhere public, maybe thread_manager?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wenyongh (2) the fixing for AOT mode is invalid, the related code is in aot_instantiate

Do I understand you correctly that https://github.com/cpetig/wasm-micro-runtime/blob/main/core/iwasm/libraries/thread-mgr/thread_manager.c#L758 is not necessary because this is taken care of in aot_runtime.c ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we merging the fixing of #2170 to this PR? Just use this PR to fix all these issues? Thanks.

Makes sense, I'll close the other PR.

lib_pthread_wrapper.c has a same workaround. It doesn't seem clever to write the same workaround twice. Shall we merge them together and store it somewhere public, maybe thread_manager?

Yes, that's what I had in mind as well when in my PR I wrote "refactor common code".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand you correctly that https://github.com/cpetig/wasm-micro-runtime/blob/main/core/iwasm/libraries/thread-mgr/thread_manager.c#L758 is not necessary because this is taken care of in aot_runtime.c ?

I think it's probably the other way around, at the beginning you were putting your changes in wasm_runtime.c so you would have needed to change aot_runtime.c as well. If the changes are in the pthread and wasi thread files no change in aot_runtime.c should be required.

@eloparco
Copy link
Contributor

eloparco commented May 4, 2023

I closed the other PR, let's discuss here instead. For #2170 (comment), yes, no need for the additional free, it's done already in

wasm_runtime_free(module_inst->e->c_api_func_imports);

Comment on lines 737 to 739
wasm_cluster_dup_c_api_imports(wasm_module_inst_t module_inst_dst,
wasm_module_inst_t module_inst_src,
wasm_module_t module)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had better use WASMModuleInstanceCommon * instead of wasm_module_inst_t, we normally use the former internally.
And no need to add argument module, we can get it from module_inst->module

Comment on lines 741 to 742
if (((const WASMModuleInstance *)module_inst_src)->e->c_api_func_imports
!= NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not use assume that the module instance is type of bytecode here, it may be type of AOT, the module_inst_src->e may be WASMModuleInstanceExtra or AOTModuleInstanceExtra, had better:

        CApiFuncImport **new_c_api_func_imports = NULL;
        CApiFuncImport *c_api_func_imports;
        uint32 import_func_count = 0;
        uint32 size_in_bytes = 0;

#if WASM_ENABLE_INTERP != 0
        if (module_inst_src->module_type == Wasm_Module_Bytecode) {
             WASMModuleInstanceExtra *e_src =
                (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst_src)
                    ->e;
              if (e_src->c_api_func_imports != NULL) {
                   ...
              }
         }
#endif
#if WASM_ENABLE_AOT != 0
        if (module_inst_src->module_type == Wasm_Module_AoT) {
             AOTModuleInstanceExtra *e_src =
                (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_src)
                    ->e;
             if (e_src->c_api_func_imports != NULL) {
                 ...
             }
        }
#endif

Copy link
Contributor

@wenyongh wenyongh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lum1n0us
Copy link
Collaborator

lum1n0us commented May 5, 2023

LGTM

@wenyongh wenyongh merged commit 5a23ae4 into bytecodealliance:main May 5, 2023
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
…ance#2173)

Fix issue reported in bytecodealliance#2172: wasm-c-api `wasm_func_call` may use a wrong exec_env
when multi-threading is enabled, with error "invalid exec env" reported

Fix issue reported in bytecodealliance#2149: main instance's `c_api_func_imports` are not passed to
the counterpart of new thread's instance in wasi-threads mode

Fix issue of invalid size calculated to copy `c_api_func_imports` in pthread mode

And refactor the code to use `wasm_cluster_dup_c_api_imports` to copy the
`c_api_func_imports` to new thread for wasi-threads mode and pthread mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants