Skip to content

JIT: Fix uninitialized gtCallMethHnd for indirect calls causing false recursive tail call detection#127293

Merged
AndyAyersMS merged 3 commits intodotnet:mainfrom
AndyAyersMS:fix/126930-uninit-gtCallMethHnd
Apr 24, 2026
Merged

JIT: Fix uninitialized gtCallMethHnd for indirect calls causing false recursive tail call detection#127293
AndyAyersMS merged 3 commits intodotnet:mainfrom
AndyAyersMS:fix/126930-uninit-gtCallMethHnd

Conversation

@AndyAyersMS
Copy link
Copy Markdown
Member

Note

This PR was generated with the assistance of GitHub Copilot.

Fixes #126930

Problem

PR #125211 removed the union between gtCallMethHnd and gtCallAddr in GenTreeCall, making them separate fields. However, gtNewCallNode was not updated to initialize gtCallMethHnd for CT_INDIRECT calls. Since the JIT arena allocator doesn't zero memory in Release builds, gtCallMethHnd contains stale arena data.

When the stale data coincidentally matches the compiled method's handle, gtIsRecursiveCall() falsely returns true for indirect calls. Combined with !call->IsVirtual() being true for CORINFO_VIRTUALCALL_LDVIRTFTN calls (which lack GTF_CALL_VIRT_* flags), fgMorphRecursiveFastTailCallIntoLoop incorrectly transforms the call into a backward jump — creating an infinite allocating loop.

This only manifests non-deterministically depending on arena memory layout, which is why it appears on x64 CI but not on arm64 or in local builds.

Fix

Three surgical changes:

  1. gentree.cpp (gtNewCallNode): Initialize gtCallMethHnd = NO_METHOD_HANDLE for CT_INDIRECT calls — the primary fix that prevents stale data.

  2. compiler.h (gtIsRecursiveCall): Defense-in-depth — return false for CT_INDIRECT calls since indirect calls can never be recursive by definition.

  3. gentree.cpp (gtCloneExprCallHelper): Same initialization for cloned indirect calls to prevent the same issue in clone paths.

… recursive tail call detection

PR dotnet#125211 broke the union between gtCallMethHnd and gtCallAddr into separate
fields but did not initialize gtCallMethHnd for CT_INDIRECT calls. Since the
arena allocator does not zero memory in Release builds, gtCallMethHnd contains
stale data that can accidentally match the compiled method's handle, causing
gtIsRecursiveCall() to return true.

Combined with the CORINFO_VIRTUALCALL_LDVIRTFTN import path (used for generic
interface calls in R2R) which creates CT_INDIRECT calls without GTF_CALL_VIRT_*
flags, the recursive tail call optimization incorrectly transforms the call into
a backward jump, creating an infinite allocating loop.

Fix by:
1. Initializing gtCallMethHnd = NO_METHOD_HANDLE in gtNewCallNode for CT_INDIRECT
2. Adding a defense-in-depth check in gtIsRecursiveCall to return false for indirect calls
3. Initializing gtCallMethHnd = NO_METHOD_HANDLE in gtCloneExprCallHelper for CT_INDIRECT

Fixes dotnet#126930

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 22, 2026 18:09
@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 Apr 22, 2026
@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.

Copy link
Copy Markdown
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

This PR fixes a CoreCLR JIT correctness issue introduced when GenTreeCall::gtCallMethHnd was split from the former gtCallAddr union: indirect call nodes (CT_INDIRECT) could leave gtCallMethHnd uninitialized, leading to nondeterministic false “recursive tail call” detection and potential infinite loops/OOMs.

Changes:

  • Initialize gtCallMethHnd to NO_METHOD_HANDLE for newly created CT_INDIRECT calls.
  • Initialize gtCallMethHnd to NO_METHOD_HANDLE when cloning CT_INDIRECT calls.
  • Add a guard in gtIsRecursiveCall(GenTreeCall*) to always return false for CT_INDIRECT calls (defense-in-depth).

Reviewed changes

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

File Description
src/coreclr/jit/gentree.cpp Ensures indirect call creation/cloning does not leave gtCallMethHnd holding stale arena data.
src/coreclr/jit/compiler.h Prevents recursive-call checks from consulting method handles on indirect calls.

Comment thread src/coreclr/jit/gentree.cpp
Copilot AI review requested due to automatic review settings April 24, 2026 00:07
@AndyAyersMS
Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib PTAL

Suggest we take this to fix the ci-optional issue and then circle back to add better protection.

Copy link
Copy Markdown
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 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/jit/gentree.cpp
@AndyAyersMS AndyAyersMS merged commit 5ff2ecf into dotnet:main Apr 24, 2026
137 of 139 checks passed
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

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: Microsoft.Extensions.Hosting.Tests.HostBuilderTests.ScopeValidationtEnabledInDevelopmentWithServiceProviderChanges

4 participants