Skip to content

Emscripten Atomics API misdepends on malloc and free #12786

Open

Description

@juj
#include <emscripten/threading.h>

int main()
{
	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.

Activity

  1. juj commented on Nov 15, 2020

    @juj
    CollaboratorAuthor

    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.

  2. sbc100 commented on Nov 15, 2020

    @sbc100
    Collaborator

    sgtm.

    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?

  3. juj commented on Nov 15, 2020

    @juj
    CollaboratorAuthor

    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.".

  4. stale commented on Apr 17, 2022

    @stale

    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.

  5. juj commented on Nov 30, 2026

    @juj
    CollaboratorAuthor

    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)

  6. sbc100 commented on Nov 30, 2026

    @sbc100
    Collaborator

    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

  7. juj commented on Nov 30, 2026

    @juj
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions