Skip to content

Fix crossgen2 JitHost OOM crash: throw on allocation failure#125422

Open
jtschuster wants to merge 8 commits intodotnet:mainfrom
jtschuster:fix/crossgen2-oom-null-deref
Open

Fix crossgen2 JitHost OOM crash: throw on allocation failure#125422
jtschuster wants to merge 8 commits intodotnet:mainfrom
jtschuster:fix/crossgen2-oom-null-deref

Conversation

@jtschuster
Copy link
Member

crossgen2's JitHost::allocateMemory returned NULL from malloc on OOM, unlike the CoreCLR VM host which uses 'new' (which throws bad_alloc). The JIT's ArenaAllocator::allocateNewPage assumes the allocator either succeeds or throws, and immediately dereferences the result without a NULL check, causing an access violation. This is happening in crossgen outerloops on ARM and x86. This change should help make those failures more obvious.

Throw std::bad_alloc on NULL returns from malloc/calloc so OOM is handled consistently across all JIT hosts. Also catch std::bad_alloc at the JitCompileMethod boundary in jitwrapper.cpp and return CORJIT_OUTOFMEM so the managed caller gets a proper error code instead of an unhandled native exception.

crossgen2's JitHost::allocateMemory returned NULL from malloc on
OOM, unlike the CoreCLR VM host which uses 'new' (throws bad_alloc).
The JIT's ArenaAllocator::allocateNewPage assumes the allocator
either succeeds or throws, and immediately dereferences the result
without a NULL check, causing an access violation.

Throw std::bad_alloc on NULL returns from malloc/calloc so OOM is
handled consistently across all JIT hosts. Also catch std::bad_alloc
at the JitCompileMethod boundary in jitwrapper.cpp and return
CORJIT_OUTOFMEM so the managed caller gets a proper error code
instead of an unhandled native exception.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 11, 2026 00:15
@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 Mar 11, 2026
@jtschuster
Copy link
Member Author

/azp run runtime-coreclr crossgen2-composite

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes crossgen2 JitHost out-of-memory behavior by converting allocation failures into std::bad_alloc, and translating those into a JIT “out of memory” result at the JitCompileMethod boundary.

Changes:

  • Throw std::bad_alloc when malloc/calloc return nullptr in the crossgen2 JIT host.
  • Catch std::bad_alloc in JitCompileMethod and return an out-of-memory status code.
  • Add <new> includes for std::bad_alloc.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/tools/aot/jitinterface/jitwrapper.cpp Catches OOM exceptions at the compile boundary and returns an OOM result.
src/coreclr/tools/aot/jitinterface/jithost.cpp Makes host allocations throw on failure instead of returning nullptr.

@dotnet-policy-service
Copy link
Contributor

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

crossgen2's JitHost::allocateMemory returned NULL from malloc on
OOM, unlike the CoreCLR VM host which uses 'new' (throws bad_alloc).
The JIT's ArenaAllocator::allocateNewPage assumes the allocator
either succeeds or throws, and immediately dereferences the result
without a NULL check, causing an access violation.

Throw std::bad_alloc on NULL returns from malloc/calloc so OOM is
handled consistently across all JIT hosts. Also catch std::bad_alloc
at the JitCompileMethod boundary in jitwrapper.cpp and return
CORJIT_OUTOFMEM so the managed caller gets a proper error code
instead of an unhandled native exception.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jtschuster and others added 2 commits March 12, 2026 12:42
Replace malloc/free with new/delete in crossgen2's jithost.cpp so that
OOM throws std::bad_alloc (caught by jitwrapper.cpp as CORJIT_OUTOFMEM)
instead of returning null and crashing. Translate CORJIT_OUTOFMEM to
OutOfMemoryException in CorInfoImpl.cs so crossgen2 can skip failed
methods gracefully.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 12, 2026 22:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

jtschuster and others added 2 commits March 13, 2026 09:24
The removal of <stdlib.h> (which transitively provided size_t) caused
a build failure on Linux where dllexport.h alone does not pull in
size_t. Add <stddef.h> which is the minimal header for size_t without
pulling in malloc/free.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 13, 2026 16:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/coreclr/tools/aot/jitinterface/jitwrapper.cpp:14

  • In the std::bad_alloc catch path, ppException is not set before returning, which can leave the out-parameter in an indeterminate state for the caller. Consider explicitly clearing it (e.g., set *ppException = nullptr when ppException is non-null) before returning CORJIT_OUTOFMEM.
DLL_EXPORT int JitCompileMethod(

*ppException = pException;
}
catch (const std::bad_alloc&)
{
delete[] value;
}

virtual void* allocateSlab(size_t size, size_t* pActualSize)
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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants