Skip to content

Conversation

@davidwrighton
Copy link
Member

@davidwrighton davidwrighton commented Sep 18, 2025

I found that if the conservative reference pointed into the GC it caused problems in the relocation logic in the GC. For all forms of pinned references, the relocation phase isn't necessary, so add a check for it and avoid the problem.

Copilot AI review requested due to automatic review settings September 18, 2025 22:53
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 18, 2025
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

This PR fixes a bug in the garbage collection (GC) enumeration for the CLR interpreter by preventing inappropriate promotion of conservative roots during the relocate phase.

  • Adds conditional logic to skip promotion for pinned objects during relocate phase
  • Restructures the existing interior pointer handling into clearer conditional blocks
  • Prevents conservative reporting from incorrectly reporting roots during GC relocate phase

@davidwrighton
Copy link
Member Author

/azp run runtime-interpreter

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@davidwrighton davidwrighton removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 18, 2025
@dotnet-policy-service
Copy link
Contributor

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

@kg
Copy link
Member

kg commented Sep 19, 2025

LGTM

@davidwrighton
Copy link
Member Author

@jkotas could you provide an opinion here? I think that this is correct, but I'd be interested to see if you think we shouldn't have such a change outside of the FEATURE_INTERPRETER path. Notably, this is NOT localized to the interpreter codebase. It will change all pinned pointers to not be reported during the relocate phase of gc scanning.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

It looks correct to me.

Alternatively, it can be checked on the GC side as the first thing in Relocate callback. It would save us from checking the bit during promotion. The stackroot reporting is relatively expensive - it is unlikely to make a material difference.

If we keep it here, we may want to make the same change in NAOT clone of this method for consistency:

static void GcEnumObject(PTR_PTR_Object ppObj, uint32_t flags, ScanFunc* fnGcEnumRef, ScanContext* pSc)
{
//
// Sanity check that the flags contain only these values
//
assert((flags & ~(GC_CALL_INTERIOR | GC_CALL_PINNED)) == 0);
// for interior pointers, we optimize the case in which
// it points into the current threads stack area
//
if (flags & GC_CALL_INTERIOR)
PromoteCarefully(ppObj, flags, fnGcEnumRef, pSc);
else
fnGcEnumRef(ppObj, pSc, flags);
}

@davidwrighton
Copy link
Member Author

@jkotas, I decided against moving the check to GCHeap::Relocate, since that function is potentially called on somewhat more expensive constructs like promoting the background roots and such. Again, not something that is likely a problem, but stack root marking is definitely expensive enough that a single check isn't going to break the world.

@jkotas jkotas merged commit 84e3a2b into dotnet:main Sep 20, 2025
98 checks passed
xtqqczze pushed a commit to xtqqczze/dotnet-runtime that referenced this pull request Sep 20, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants