Skip to content

Warn when addFunction is used in a multi-threaded build - #28275

Merged
sbc100 merged 6 commits into
emscripten-core:mainfrom
clementperon:addfunction-threads-warning
Nov 20, 2026
Merged

sbc100 merged 6 commits into
emscripten-core:mainfrom
clementperon:addfunction-threads-warning

Conversation

@clementperon

@clementperon clementperon commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to the discussion in #28105: each thread has its own table, so a function pointer returned by addFunction is only valid in the thread that called it, and nothing said so.

In builds with assertions and shared memory (pthreads or Wasm Workers), addFunction now warns once when it has to add a function to the table:

warning: addFunction is not supported with threads: each thread has its own table, so the function pointer is only valid in the calling thread

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 internal addFunctionInternal. addFunction is 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, addFunction moves from the list of missing library functions to the list of unexported symbols. A program that calls it without including it now gets ReferenceError: addFunction is not defined instead of the hint about DEFAULT_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 addFunction documentation now states the limitation too.

Testing

With the emsdk build from test/emsdk_version.txt, on macOS arm64:

  • New other.test_add_function_threads: no warning without threads; a warning with -pthread, with -sWASM_WORKERS and with -pthread -sMAIN_MODULE=2; in all of them no warning for a function that is already in the table. The MAIN_MODULE variant fails when updateGOT or reportUndefinedSymbols calls addFunction.
  • The two dlsym paths have no test: the existing test_pthread_dlsym finds its symbols in the table and never adds one. I checked them by hand with dlsym(RTLD_DEFAULT, "emscripten_random") from two threads: same pointer in both and no warning.
  • core0.test_dylink*, core0.test_dlfcn*, core0/core2 test_pthread_d*, other.test_dylink_pthread*, other.test_add*, other.test_full_js_library* and other.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 in test_codesize_minimal_O0.json and test_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 core and other.

🤖 Generated with Claude Code

clementperon and others added 2 commits November 16, 2026 19:02
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>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread src/lib/libaddfunction.js Resolved
Comment thread test/test_other.py Resolved
Comment thread test/test_other.py Resolved
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Comment thread src/lib/libdylink.js Resolved

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm % comments

Comment thread src/lib/libaddfunction.js Resolved
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>
Comment thread test/test_other.py Resolved
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm, but I'd like to wait for @hoodmane to reply before we land. I'm hoping this is not something that is actually depended on in pyodide, for example?

@hoodmane

Copy link
Copy Markdown
Collaborator

I'm hoping this is not something that is actually depended on in pyodide

No, we have always stayed away from pthreads, both because we want dynamic linking and because our users struggle with coop/coep.

@sbc100
sbc100 enabled auto-merge (squash) November 19, 2026 04:37
@sbc100

sbc100 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

I enabled auto-merge but it looks like you have one conflict to resolve.

auto-merge was automatically disabled November 19, 2026 21:17

Head branch was pushed to by a user without write access

@clementperon

Copy link
Copy Markdown
Contributor Author

@sbc100 done

@sbc100
sbc100 merged commit 66fdafc into emscripten-core:main Nov 20, 2026
43 checks passed
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.

3 participants