Skip to content

libclrjit crashes on thread exit in hosts that replace global operator new (.NET 11 regression) #137487

Closed

Description

@Edenlia

Description

Since .NET 11, libclrjit defines its own global operator new/operator delete (added in #120290 under
JIT_STANDALONE_BUILD, src/coreclr/jit/alloc.cpp). Only operator new(size_t), operator new[](size_t),
operator delete(void*) and operator delete[](void*) are defined; the std::nothrow_t overloads are not.

The PAL that is statically linked into libclrjit allocates thread data with the nothrow overload and frees it with
plain delete (src/coreclr/pal/src/thread/thread.cpp):

CPalThread* AllocTHREAD() { return new(std::nothrow) CPalThread(); }
static void FreeTHREAD(CPalThread *pThread) { delete pThread; }

Because the JIT is built with hidden visibility, its operator delete(void*) binds inside libclrjit and always calls
free(). operator new(size_t, const std::nothrow_t&) is still imported from the C++ runtime
(nm -m libclrjit.dylib shows __ZdlPv as a private definition and __ZnwmRKSt9nothrow_t as an undefined import).
When the host executable replaces the global operator new — common in game engines and in programs using
mimalloc/jemalloc/tcmalloc-style replacements — the dynamic linker binds the JIT's nothrow new to the host's
allocator, while the matching delete still goes to free(). When the thread that ran jitStartup exits, the PAL's
TLS destructor frees its CPalThread and the process aborts.

.NET 10 is not affected: its libclrjit imports both operator new and operator delete (#120323, the release/10.0
backport of #120290, did not include the alloc.cpp overrides). libcoreclr 11.0.0 also imports both and is not
affected.

Reproduction Steps

clrjit-nothrow-repro.zip

Attached: clrjit-nothrow-repro.zip (source only, no third-party code). A ~100-line C++ host replaces the global
operator new/delete, starts the runtime through hostfxr_initialize_for_runtime_config on a worker thread, calls
one [UnmanagedCallersOnly] method, and lets the worker thread exit.

host.cpp, the relevant part:

// Host allocator: every block has a 16-byte header, so its pointers are never pointers malloc returned.
static void* HostAlloc(std::size_t n) { char* p = (char*)std::malloc(n + 16); return p ? p + 16 : nullptr; }
static void HostFree(void* p) { if (p) std::free((char*)p - 16); }
void* operator new(std::size_t n) { if (void* p = HostAlloc(n)) return p; throw std::bad_alloc(); }
void* operator new(std::size_t n, const std::nothrow_t&) noexcept { return HostAlloc(n); }
void operator delete(void* p) noexcept { HostFree(p); }
// ... the array, nothrow and sized overloads likewise

int main(int argc, char** argv)
{
    // dlopen <dotnet-root>/host/fxr/<ver>/libhostfxr.dylib, hostfxr_initialize_for_runtime_config(Repro.runtimeconfig.json),
    // hdt_load_assembly_and_get_function_pointer("Repro.Entry, Repro", "Run", UNMANAGEDCALLERSONLY_METHOD)
    // -- all of it on this worker thread, then call Run(20):
    std::thread worker([&] { StartRuntimeAndRun(argv[0], argv[1], argv[2]); });
    worker.join();   // <- aborts here on .NET 11 when the worker thread exits
}

Repro/Entry.cs:

public static class Entry
{
    [UnmanagedCallersOnly]
    public static int Run(int n) => Fibonacci(n);
    private static int Fibonacci(int n) => n < 2 ? n : Fibonacci(n - 1) + Fibonacci(n - 2);
}

Build and run all four cases ({.NET 11, .NET 10} × {host replaces operator new, control host}):

./run.sh <dotnet-11-root> <dotnet-10-root>

Expected behavior

The worker thread exits and the program prints worker thread exited cleanly, as it does on .NET 10 and on .NET 11
without the operator new replacement.

Actual behavior

Runtime Host replaces operator new Result
11.0.0 yes abort (SIGTRAP), exit 133
11.0.0 no exit 0
10.0.12 yes exit 0
10.0.12 no exit 0
libsystem_malloc.dylib  ___BUG_IN_CLIENT_OF_LIBMALLOC_POINTER_BEING_FREED_WAS_NOT_ALLOCATED
libclrjit.dylib         CorUnix::CPalObjectBase::ReleaseReference(CorUnix::CPalThread*) + 216
libclrjit.dylib         CorUnix::InternalEndCurrentThread(CorUnix::CPalThread*) + 168
libclrjit.dylib         InternalEndCurrentThreadWrapper(void*) + 56
libsystem_pthread.dylib _pthread_tsd_cleanup + 488
libsystem_pthread.dylib _pthread_exit + 84
libsystem_pthread.dylib _pthread_start + 148

Regression?

Yes. Works in .NET 10.0.12; fails in 11.0.0. Introduced by #120290 (the JIT_STANDALONE_BUILD overrides in
src/coreclr/jit/alloc.cpp, a diagnostic for accidental heap allocations; the release/10.0 backport #120323 did not
include them). Still present on main.

#124715 hit the same root cause from another side (SunOS: the C++ runtime's nothrow new forwarding to the JIT's
asserting new) and was resolved by relaxing the assert; the nothrow overloads were discussed there but not added.
The underlying invariant described in that PR — that nothing linked into the JIT depends on operator new — does not
hold, because the PAL allocates with new (std::nothrow).

Known Workarounds

None for hosts that must replace the global operator new. Not replacing it avoids the crash.

Configuration

  • .NET 11.0.0 (Microsoft.NETCore.App 11.0.0, SDK 11.0.100), osx-arm64; control: .NET 10.0.12
  • macOS 26.6 (25G72), Apple Silicon, Apple clang 21
  • Found while embedding .NET 11 in a game engine whose executables replace the global operator new.

Other information

I have a fix ready (PR to follow): define the remaining overloads next to the existing ones in
src/coreclr/jit/alloc.cpp — operator new(size_t, const std::nothrow_t&), operator new[](size_t, const std::nothrow_t&), operator delete(void*, const std::nothrow_t&), operator delete[](void*, const std::nothrow_t&),
and the sized operator delete(void*, size_t) / operator delete[](void*, size_t) — so every allocation and
deallocation inside libclrjit pairs malloc/free.

With the repro above, a libclrjit built from v11.0.0 with the change passes 10/10 runs in the replacing host,
where both the official 11.0.0 binary and an unpatched build from v11.0.0 fail 10/10. In the game engine where this
was found, swapping in the patched libclrjit also removes the exit crash (5/5 clean vs 0/5 for both unpatched
binaries).

Activity

  1. added
    untriaged
    on Nov 13, 2026
  2. dotnet-policy-service commented on Nov 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
    See info in area-owners.md if you want to be subscribed.

  3. added
    area-CodeGen-coreclr
    and removed on Nov 13, 2026
  4. dotnet-policy-service commented on Nov 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  5. added this to the 12.0.0 milestone on Nov 13, 2026
  6. removed
    untriaged
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions