Skip to content

Conversation

janvorli
Copy link
Member

@janvorli janvorli commented Oct 3, 2025

There are few cases where GCFrame is created by managed code as a managed local. When that code is interpreted, this local is on the interpreter stack which is NOT the processor stack. So various debug checks and also removing these GCFrames from the per-thread chain doesn't work.

This change adds an "os stack location" member to the GCFrame that is set to "this" for GCFrames allocated in native code. For the ones allocated by the interpreted managed code, it is set to the InterpMethodContextFrame of the frame that created it. That value is then used instead of the raw GCFrame pointer to compare locations of these frames.

There are few cases where GCFrame is created by managed code as a
managed local. When that code is interpreted, this local is on the
interpreter stack which is NOT the processor stack. So various debug
checks and also removing these GCFrames from the per-thread chain
doesn't work.

This change adds an "os stack location" member to the GCFrame that
is set to "this" for GCFrames allocated in native code. For the ones
allocated by the interpreted managed code, it is set to the
InterpMethodContextFrame of the frame that created it. That value is
then used instead of the raw GCFrame pointer to compare locations of
these frames.
@janvorli janvorli added this to the 11.0.0 milestone Oct 3, 2025
@janvorli janvorli requested a review from davidwrighton October 3, 2025 20:02
@janvorli janvorli self-assigned this Oct 3, 2025
@Copilot Copilot AI review requested due to automatic review settings October 3, 2025 20:02
Copy link
Contributor

@Copilot 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 GCFrame handling for interpreted managed code by adding an OS stack location tracking mechanism. The issue occurs when GCFrames are allocated on the interpreter stack rather than the processor stack, causing debug checks and frame removal operations to fail.

Key changes:

  • Added GetOSStackLocation() method to GCFrame that returns the appropriate stack location for comparison operations
  • Updated GCFrame constructor and registration logic to properly set OS stack location for interpreted code scenarios
  • Modified debug assertions and frame popping logic to use OS stack location instead of raw GCFrame pointers

Reviewed Changes

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

Show a summary per file
File Description
src/coreclr/vm/threads.h Moved GetGCFrame() method declaration from inline to separate implementation
src/coreclr/vm/threads.cpp Implemented GetGCFrame() method with updated debug assertion using OS stack location
src/coreclr/vm/frames.h Added GetOSStackLocation() method and m_osStackLocation member for interpreter support
src/coreclr/vm/frames.cpp Updated GCFrame constructor and Push() method to use OS stack location for ordering checks
src/coreclr/vm/exceptionhandling.cpp Modified frame popping logic to use OS stack location for comparison
src/coreclr/vm/eetwain.cpp Added interpreter-specific logic to set OS stack location during GCFrame registration

Copy link
Contributor

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

@davidwrighton
Copy link
Member

I like the approach, but there are too many build failures for me to sign off here.

@kg
Copy link
Member

kg commented Oct 3, 2025

OSStack is a little misleading because on WASM it's not an OS stack or a native stack, but instead is just in linear memory managed by us. But I think that's fine, it's right on every other target.

@janvorli
Copy link
Member Author

janvorli commented Oct 3, 2025

OSStack is a little misleading

I've picked the naming that David used for similar thing few days ago.

@janvorli janvorli merged commit 6257846 into dotnet:main Oct 7, 2025
146 checks passed
@janvorli janvorli deleted the fix-managed-allocated-gcframe-handling branch October 7, 2025 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants