microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
9.74k stars 793 forks source link

alloc-override.c uses wrong type for std::nothrow_t argument in C++ operator new overrides #840

Open sbc100 opened 5 months ago

sbc100 commented 5 months ago

alloc-override.c overrides operator new, etc, using C functions with C++ mangled names. e.g:

void* _ZnwmRKSt9nothrow_t(size_t n, mi_nothrow_t tag) { MI_UNUSED(tag); return mi_new_nothrow(n); }

The requires signature of this function can be seen via c++filt:

$ c++filt _ZnwmRKSt9nothrow_t
operator new(unsigned long, std::nothrow_t const&)

Note that the second argument here (and everywhere that std::nothrow_t is uses) is passed by reference, but _ZnwmRKSt9nothrow_t is passing mi_nothrow_t by value.

On most platform, especially since all the overrides simply ignore this argument, this is not an issue, but on Wasm C signatures are requires to match between compilation unit.

As it stands, under wasm64, we get a linker warning about the std::nothrow_t argument having a different width in libmimalloc compared to libc++.

See https://github.com/emscripten-core/emscripten/pull/21127 for the proposed downstream fix.

daanx commented 2 months ago

Apologies for the late fix; I pushed a potential fix to dev/dev-slice where I redefine mi_nothrow_t to be a void* -- I guess this will fix the linker error as well?