You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Emscripten Atomics API misdepends on malloc and free #12786
#include<emscripten/threading.h>intmain()
{
int a;
emscripten_atomic_store_u32((void*)&a, 0);
}
>em++ test.cpp -o a.html -s MALLOC=none -s USE_PTHREADS=1
error: undefined symbol: emscripten_builtin_free (referenced by top-level compiled C/C++ code)
warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
warning: _emscripten_builtin_free may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: emscripten_builtin_memalign (referenced by top-level compiled C/C++ code)
warning: _emscripten_builtin_memalign may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
em++: error: 'E:/code/emsdk/node/12.18.1_64bit/bin/node.exe E:\code\emsdk\emscripten\master\src\compiler.js C:\Users\jukkaj\AppData\Local\Temp\tmp7cep0abl.txt' failed (1)
Commenting out emscripten_atomic_store_u32((void*)&a, 0); drops the build error.
It looks like this may be due to the fact that library_pthread_wasm.c contains both the TLS init and atomics code. Splitting the two files into library_pthread_atomics.c and library_pthread_tls.c seems to work around the issue.
On an unrelated note, is there any reason we still have functions like emscripten_atomic_store_u32 given that both C and C++ have native support for atomics (https://en.cppreference.com/w/c/atomic/atomic_store)? Do we continue to maintain these emscripten specific functions basically for legacy support at this point or are they useful for some purpose?
Basically for legacy support, but it does also provide clarified documentation, as it allows one to read through in one header the set of atomic APIs that are supported when compiling to wasm. I think it would make sense to add in the header that contains them, for each function, a snippet "emscripten_atomic_store_u32: identical to GCC built-ins __atomic_store or __c11_atomic_store, use those for portable code.".
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
This issue does still exist, although the flags have been renamed:
-> -pthread does not support passing -sMALLOC=none, and also
-> when using -sWASM_WORKERS, one cannot pass -sMALLOC=none.
Results on latest origin/main
┌────────────────────────────────────────┬───────────────────────────────┬───────────────────┐
│ Program │ Flags │ Result │
├────────────────────────────────────────┼───────────────────────────────┼───────────────────┤
│ issue's test.cpp │ -sMALLOC=none │ links │
├────────────────────────────────────────┼───────────────────────────────┼───────────────────┤
│ issue's test.cpp │ -sMALLOC=none -sSHARED_MEMORY │ links │
├────────────────────────────────────────┼───────────────────────────────┼───────────────────┤
│ issue's test.cpp │ -sMALLOC=none -pthread │ fails │
├────────────────────────────────────────┼───────────────────────────────┼───────────────────┤
│ empty main │ -sMALLOC=none -pthread │ fails identically │
├────────────────────────────────────────┼───────────────────────────────┼───────────────────┤
│ std::atomic store │ -sMALLOC=none -pthread │ fails identically │
├────────────────────────────────────────┼───────────────────────────────┼───────────────────┤
│ issue's test.cpp plus a user allocator │ -sMALLOC=none -pthread │ links │
├────────────────────────────────────────┼───────────────────────────────┼───────────────────┤
│ empty main │ -sMALLOC=none -sWASM_WORKERS │ fails │
└────────────────────────────────────────┴───────────────────────────────┴───────────────────┘
I'm not sure about the pthread scenario, although when I originally created WASM_WORKERS, one of the use cases I did have in mind was "I want to be able to create tiny multithreaded programs that don't drag in malloc" (hence why there's a function that allows the user to pass in the stack to wasm worker creation)
It may be possible to make the above trivial program link without malloc.. but actually starting a new pthread is going to always require malloc (unless we add some kind of in-place/bring-your-own version of pthread_create.. but that would be a new API).
In the mean I'm work currently working on reducing pthread_create to just a single allocation. See #28449
I think it'll probably be fine to require pthread -> malloc.. but for Wasm Workers it would be great to not require it.
I asked Claude to prototype breaking WASM_WORKERS from -sMALLOC=none. It created 33f6dda .. somewhat straightforward, effectively decoupling the link libs.
I'll defer posting that as a PR right now, since I'm more interested in other lower hanging fruit cleanups now. Might come back to this at some point.
Activity
juj commentedon Nov 15, 2020
It looks like this may be due to the fact that
library_pthread_wasm.ccontains both the TLS init and atomics code. Splitting the two files intolibrary_pthread_atomics.candlibrary_pthread_tls.cseems to work around the issue.sbc100 commentedon Nov 15, 2020
sgtm.
On an unrelated note, is there any reason we still have functions like
emscripten_atomic_store_u32given that both C and C++ have native support for atomics (https://en.cppreference.com/w/c/atomic/atomic_store)? Do we continue to maintain these emscripten specific functions basically for legacy support at this point or are they useful for some purpose?juj commentedon Nov 15, 2020
Basically for legacy support, but it does also provide clarified documentation, as it allows one to read through in one header the set of atomic APIs that are supported when compiling to wasm. I think it would make sense to add in the header that contains them, for each function, a snippet
"emscripten_atomic_store_u32: identical to GCC built-ins __atomic_store or __c11_atomic_store, use those for portable code.".stale commentedon Apr 17, 2022
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
juj commentedon Nov 30, 2026
This issue does still exist, although the flags have been renamed:
->
-pthreaddoes not support passing-sMALLOC=none, and also-> when using
-sWASM_WORKERS, one cannot pass-sMALLOC=none.I'm not sure about the pthread scenario, although when I originally created WASM_WORKERS, one of the use cases I did have in mind was "I want to be able to create tiny multithreaded programs that don't drag in malloc" (hence why there's a function that allows the user to pass in the stack to wasm worker creation)
sbc100 commentedon Nov 30, 2026
It may be possible to make the above trivial program link without malloc.. but actually starting a new pthread is going to always require malloc (unless we add some kind of in-place/bring-your-own version of
pthread_create.. but that would be a new API).In the mean I'm work currently working on reducing pthread_create to just a single allocation. See #28449
juj commentedon Nov 30, 2026
I think it'll probably be fine to require pthread -> malloc.. but for Wasm Workers it would be great to not require it.
I asked Claude to prototype breaking WASM_WORKERS from -sMALLOC=none. It created 33f6dda .. somewhat straightforward, effectively decoupling the link libs.
I'll defer posting that as a PR right now, since I'm more interested in other lower hanging fruit cleanups now. Might come back to this at some point.