Skip to content

JIT: Define nothrow and sized global new/delete in the standalone JIT - #137488

Closed
Edenlia wants to merge 1 commit into
dotnet:mainfrom
Edenlia:fix/clrjit-nothrow-new-delete
Closed

Edenlia wants to merge 1 commit into
dotnet:mainfrom
Edenlia:fix/clrjit-nothrow-new-delete

Conversation

@Edenlia

@Edenlia Edenlia commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #137487

#120290 added global operator new/operator delete overrides to the standalone JIT (JIT_STANDALONE_BUILD) as a
diagnostic for accidental heap allocations. Only the plain forms were overridden. As discussed in #124715, the PAL
linked into the JIT allocates with new (std::nothrow), so those allocations still come from the C++ runtime library,
while the matching delete binds to the JIT's override and free().

That is harmless when the process uses the default allocator, but when the host replaces the global operator new
(game engines, allocator replacements), the dynamic linker binds the JIT's nothrow import to the host's allocator. The
PAL then frees host-allocated memory with free() and the process aborts when the thread that called jitStartup
exits. .NET 10 is unaffected (the overrides were not backported), and libcoreclr imports both operators, so this only
affects the standalone JIT.

This change overrides the nothrow and sized forms too, so every allocation and deallocation inside the JIT pairs
malloc with free regardless of the host.

The nothrow overloads intentionally do not carry the "Global new called" assert. The PAL calls them outside of
compilation and after jitStartup has set g_jitHost — for example InternalEndCurrentThread →
CSynchCache::Get → operator new(size_t, const std::nothrow_t&) during thread exit. A Checked JIT with the assert
added to the nothrow overloads asserts in every run of the repro, including without any allocator replacement.

Alternatives considered:

Validation

Validated on osx-arm64 (Apple clang 21, ./build.sh clr.jit -c <config> -a arm64).

  • This branch (on main a0fecfe) builds as Release and Checked. The same commit cherry-picked onto v11.0.0 builds as
    Release and Checked.

  • nm -m libclrjit.dylib: before, operator new(size_t, const std::nothrow_t&) and operator delete(void*, const std::nothrow_t&) are undefined imports from libc++; after, both are private definitions like the plain forms.

  • Repro from the issue: each libclrjit replaces the one in an otherwise unchanged Microsoft.NETCore.App 11.0.0.
    10 runs per cell:

    libclrjit replacing host control host
    official 11.0.0 0/10 (abort) 10/10
    v11.0.0, Release 0/10 (abort) 10/10
    v11.0.0 + this change, Release 10/10 10/10
    v11.0.0, Checked 0/10 (abort) 10/10
    v11.0.0 + this change, Checked 10/10 10/10
  • In the game engine where this was found (a packaged macOS game hosting .NET 11 through coreclr_initialize), with
    only libclrjit.dylib swapped, 5 runs each: the official 11.0.0 binary and an unpatched v11.0.0 Release build
    abort at process exit every time with the same stack as the repro; v11.0.0 + this change exits cleanly 5/5.

The standalone JIT overrides the plain global operator new/delete (added
in dotnet#120290 as a diagnostic), but not the nothrow and sized forms. The
PAL linked into the JIT allocates its per-thread data and
synchronization state with new (std::nothrow) and frees it with plain
delete. The nothrow allocations therefore come from the C++ runtime
library, which the dynamic linker binds to the host's replacement when
the host replaces the global operator new, while the deallocations go
to the JIT's override and free(). Such hosts abort when the thread that
called jitStartup exits. This is a regression from .NET 10, which has no
overrides.

Override the nothrow and sized forms as well so every allocation and
deallocation in the JIT pairs malloc with free regardless of the host.
The nothrow forms do not assert: the PAL calls them outside of
compilation, including from its thread-exit cleanup after jitStartup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 13, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 13, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@Edenlia

Edenlia commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The standalone JIT allocator overload changes have no active review issues.

No open issues 🎉

What changed in this PR

This PR addresses the standalone JIT allocator mismatch reported in #137487, which can crash hosts that replace global operator new during thread exit.

Changes:

  • Adds scalar and array nothrow allocation overloads backed by malloc, plus nothrow and sized deallocation overloads backed by free.
  • Leaves nothrow allocations assertion-free and documents why PAL allocations outside compilation require this behavior.
File Description
src/​coreclr/​jit/​alloc.cpp Adds allocator overloads to keep standalone JIT allocation and deallocation paired.

Review effort: Balanced

@jakobbotsch

Copy link
Copy Markdown
Member

I submitted an alternative in #137624 that removes the runtime diagnostic in favor of a compile-time diagnostic. If possible, can you validate that this also fixes the problem for you?

@Edenlia

Edenlia commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

I submitted an alternative in #137624 that removes the runtime diagnostic in favor of a compile-time diagnostic. If possible, can you validate that this also fixes the problem for you?

Thanks @jakobbotsch, I validated #137624 and it fixes the problem for me.

Results (only libclrjit.dylib differs between rows):

libclrjit repro from #137487, replacing host repro, control host game engine package, clean exit
v11.0.0 0/10 10/10 0/5
v11.0.0 + #137624 10/10 10/10 5/5

Removing the overrides entirely seems like the better fix than adding more of them, since it takes away this whole class of linking problem rather than covering one more form. Once #137624 is merged, I'll close this PR and #137487. Thanks for the quick fix!

JulieLeeMSFT pushed a commit that referenced this pull request Nov 17, 2026
… allocators when hosting .NET 11 (#137628)

# Description

In .NET 11, the JIT added overrides of `operator new` and `operator
delete` as a diagnostic to catch unexpected uses of these. However, one
`operator new` overload was missed. On Unix-based apps that host .NET 11
and override `operator new`/`delete` with custom allocators this can
result in mismatched new/delete.

# Customer Impact

- [x] Customer reported
- [ ] Found internally

Reported in #137487. This can cause segfaults when self-hosting .NET 11
in an application that uses a custom allocator on Unix-based OS's.

# Regression

- [X] Yes
- [ ] No

Introduced in .NET 11.

# Testing

Testing was manual, by building a self-hosting app and validating the
crash, then validating it disappeared with the fix. A similar fix was
validated directly by customer in
#137488 (comment).

# Risk

Low. Change removes the diagnostic overrides entirely from .NET 11.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: ef562efe-a62e-4c98-ba36-f7dfdd60c3df
@Edenlia Edenlia closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants