Skip to content

wasm-ld segfaulting when attempting to use --wrap=malloc #595

Closed

Description

@elasota

Trying to use a custom imported allocator but it's causing wasm-ld to segfault.

Been trying to repro this with upstream LLVM but so far having difficulties with clang++ ignoring --sysroot, so for now...

Using wasi-sdk-30.0-x86_64-windows and wasi-sysroot-30.0:

Save this file as "wraptest.cpp":

#include <stdlib.h>

void extern_dosomething(void *);

void *extern_malloc(size_t size);

void* __wrap_malloc(size_t size)
{
    return extern_malloc(size);
}

[[clang::export_name("whatever")]]
void whatever(int a, int b)
{
	void *mem = malloc(a);
	extern_dosomething(mem);
}

Run:

wasm32-wasi-clang++ -O3 -Wl,--allow-undefined -Wl,--wrap=malloc --sysroot=<path to wasi-sysroot-30.0> -mexec-model=reactor -o wraptest.wasm wraptest.cpp

Crashes with segfault:

Exception Code: 0xC0000005
 #0 0x00007ff69cfcee5d (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x32ee5d)
 #1 0x00007ff69cfcf42b (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x32f42b)
 #2 0x00007ff69cfb8bc6 (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x318bc6)
 #3 0x00007ff69cfbefe3 (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x31efe3)
 #4 0x00007ff69cfbfd66 (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x31fd66)
 #5 0x00007ff69cfa80f2 (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x3080f2)
 #6 0x00007ff69cfa7006 (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x307006)
 #7 0x00007ff69cd5956a (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0xb956a)
 #8 0x00007ff69ccea151 (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x4a151)
 #9 0x00007ff69ccea3da (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x4a3da)
#10 0x00007ff69e3537b0 (C:\Users\Eric\Downloads\wasi-sdk-30.0-x86_64-windows\bin\wasm-ld.exe+0x16b37b0)
#11 0x00007ffd6dcfe8d7 (C:\WINDOWS\System32\KERNEL32.DLL+0x2e8d7)
#12 0x00007ffd6fc4c40c (C:\WINDOWS\SYSTEM32\ntdll.dll+0x8c40c)
wasm32-wasi-clang++: error: linker command failed due to signal (use -v to see invocation)

Activity

  1. elasota commented on Mar 2, 2026

    @elasota
    Author

    Seems __wrap_malloc needs to be extern "C" so seems like the problem is more like it's crashing if it can't resolve it and a more minimal version is just:

    #include <stdlib.h>
    
    void extern_dosomething(void *);
    
    [[clang::export_name("whatever")]]
    void whatever(int a, int b)
    {
    	void *mem = malloc(a);
    	extern_dosomething(mem);
    }
    
  2. sbc100 commented on Mar 2, 2026

    @sbc100
    Member

    This looks like must be bug in wasm-ld, although I'm having trouble replicating it with emscripten.

    $ ./emcc -O3 -Wl,--allow-undefined -Wl,--wrap=malloc  test.cpp
    error: undefined symbol: _Z18extern_dosomethingPv (referenced by root reference (e.g. compiled C/C++ code))
    warning: To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0`
    warning: __Z18extern_dosomethingPv may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
    error: undefined symbol: __wrap_malloc (referenced by root reference (e.g. compiled C/C++ code))
    warning: ___wrap_malloc may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
    Error: Aborting compilation due to previous errors
        at finalCombiner (file:///usr/local/google/home/sbc/dev/wasm/emscripten/src/jsifier.mjs:926:13)
        at Module.runJSify (file:///usr/local/google/home/sbc/dev/wasm/emscripten/src/jsifier.mjs:962:5)
        at file:///usr/local/google/home/sbc/dev/wasm/emscripten/tools/compiler.mjs:108:17
    emcc: error: '/usr/local/google/home/sbc/dev/wasm/emsdk/node/22.16.0_64bit/bin/node /usr/local/google/home/sbc/dev/wasm/emscripten/tools/compiler.mjs -' failed (returned 1)
    

    Here we see the wasm-ld command succeed and then undefined symbol are reported later.

    If you are able to build a repro case using just clang and wasm-ld (preferably built from tip-of-tree with symbol information), could you open llvm issue for this?

  3. elasota commented on Mar 2, 2026

    @elasota
    Author

    Was able to get the intermediates with -v and try it with LLVM 22.1.0.

    It happens with the wasm-sdk-30 wasm-ld, but doesn't happen in the LLVM 22.1.0 wasm-ld, so maybe this was fixed upstream at some point?

  4. sbc100 commented on Mar 2, 2026

    @sbc100
    Member

    Indeed, sounds like its already fixed upstream, so maybe its just question of waiting for the next release of wasi-sdk.

    @alexcrichton is there a wasi-sdk with LLVM 22.1.0 coming soon?

  5. alexcrichton commented on Mar 2, 2026

    @alexcrichton
    Collaborator

    We were just waiting for LLVM 22.1.0 historically, which happened last week I now realize, so time to rebase some things and get things merged.

  6. sbc100 commented on Mar 2, 2026

    @sbc100
    Member

    Great. Maybe we can leave this open for now and close as part of the next release.

  7. added a commit that references this issue on Mar 3, 2026
    b7d8add
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions