Repository navigation
Warn when addFunction is used in a multi-threaded build - #28275
Conversation
Each thread has its own table, so the function pointer is only valid in the calling thread. The dynamic linker, which repeats its additions in every thread, is exempt. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a warning in addFunction when it is called in multi-threaded builds with assertions enabled, as function pointers are only valid in the calling thread. It also introduces addFunctionSynced for internal use by the dynamic linker to avoid this warning. The feedback suggests defining $addFunctionSynced__deps and updating $addFunction__deps to prevent dependency resolution issues. Additionally, the feedback recommends explicitly declaring $getWasmTableEntry in the test's EM_JS_DEPS and asserting the output and warning independently to avoid fragile test assertions.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
addFunction is an alias of it, except in multi-threaded builds with assertions, where it is a wrapper that adds the warning. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
No, we have always stayed away from pthreads, both because we want dynamic linking and because our users struggle with coop/coep. |
|
I enabled auto-merge but it looks like you have one conflict to resolve. |
Head branch was pushed to by a user without write access
|
@sbc100 done |
Follow-up to the discussion in #28105: each thread has its own table, so a function pointer returned by
addFunctionis only valid in the thread that called it, and nothing said so.In builds with assertions and shared memory (pthreads or Wasm Workers),
addFunctionnow warns once when it has to add a function to the table:It stays silent when the function is already in the table, since it then returns the address the function has in every thread.
The dynamic linker adds functions itself (
updateGOT,reportUndefinedSymbols,dlsym) and repeats those additions in every thread, so it now calls an internaladdFunctionInternal.addFunctionis an alias of it, except in builds with assertions and shared memory, where it is a wrapper that adds the warning.One side effect of the alias: in assertion builds,
addFunctionmoves from the list of missing library functions to the list of unexported symbols. A program that calls it without including it now getsReferenceError: addFunction is not definedinstead of the hint aboutDEFAULT_LIBRARY_FUNCS_TO_INCLUDE.I made it a warning and not an abort because a program that only calls the pointer in the thread that added it works today. It is a one-line change if you prefer an abort.
The
addFunctiondocumentation now states the limitation too.Testing
With the emsdk build from
test/emsdk_version.txt, on macOS arm64:other.test_add_function_threads: no warning without threads; a warning with-pthread, with-sWASM_WORKERSand with-pthread -sMAIN_MODULE=2; in all of them no warning for a function that is already in the table. TheMAIN_MODULEvariant fails whenupdateGOTorreportUndefinedSymbolscallsaddFunction.dlsympaths have no test: the existingtest_pthread_dlsymfinds its symbols in the table and never adds one. I checked them by hand withdlsym(RTLD_DEFAULT, "emscripten_random")from two threads: same pointer in both and no warning.core0.test_dylink*,core0.test_dlfcn*,core0/core2test_pthread_d*,other.test_dylink_pthread*,other.test_add*,other.test_full_js_library*andother.test_closure_full_js_library*pass (204 tests, 5 skipped).codesize: three expectation files change, because'addFunction'moves between the two lists in unoptimized output:test_codesize_minimal_O0.expected.js, and gzip sizes one byte larger intest_codesize_minimal_O0.jsonandtest_unoptimized_code_size.json. No raw size changes. macOS gives different gzip sizes from CI, so I computed these in an Ubuntu 24.04 container from the outputs built here; the same method gives the current expectations for the unchanged outputs.I did not run the browser suites or the rest of
coreandother.🤖 Generated with Claude Code