Skip to content

Ensure HasPrecode flag is read before the entry point slot - #137043

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-136918-hasprecode-acquire
Nov 5, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
AndyAyersMS:fix-136918-hasprecode-acquire

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

MethodDesc::HasPrecode() is read and then followed by a read of the entry point slot (GetPrecode() -> GetStableEntryPoint()). The writer publishes the slot first and then sets HasPrecode with InterlockedUpdateFlags3, which is ordered. The reader's two loads are plain loads, so on arm64 hardware like a Mac M4 they may be reordered, which would let the reader observe HasPrecode set together with a stale null slot. A null Precode* returned to CEEInfo::getFunctionEntryPoint becomes a call through cell 0x4000, which matches the dump in #136918.

This reads the flag with acquire semantics so it pairs with the release on the writer side. All readers that follow HasPrecode() with GetPrecode() are covered. It compiles to ldaprh on arm64 and is free on x64.

This is unconfirmed as the cause of #136918. I could not reproduce the crash in the runtime (about 12 hours of targeted stress, including SDK-shaped dotnet test runs, on an M4 Max and an M1 Max). A message-passing litmus test modeling this protocol does show the reordering on M4 with a relaxed reader (thousands of misses per tens of millions of trials) and none with an acquire reader. I did not run the runtime test suites or benchmarks for this change.

Related to #136918

Note

This PR description was drafted with the assistance of GitHub Copilot (AI-generated).

We must ensure the HasPrecode value is read before the entry point slot is
read. Otherwise hardware like a Mac M4 may reorder the reads and observe a
null slot.

Possible cause of dotnet#136918, though I was unable to reproduce it locally.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: a804587b-9821-47d4-8d74-7f9e3822f595
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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: @agocke
See info in area-owners.md if you want to be subscribed.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

@davidwrighton PTAL

No local repro, but this seems a likely explanation for the crash. Also seems like this pattern may exist for other similar checks on the method desc. I have not looked for those.

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.

🔵 Needs a closer look

The CoreCLR entry-point publication ordering change requires runtime-maintainer review because ARM64 runtime validation is missing and its connection to crash #136918 remains unconfirmed.

No open issues 🎉

What changed in this PR

This PR strengthens CoreCLR’s precode publication ordering so readers observe HasPrecode before reading the entry point slot. It addresses a possible cause of #136918, but the connection to that crash remains unconfirmed.

Changes:

  • Uses VolatileLoad to give the HasPrecode flag read acquire semantics.
  • Adds a comment explaining the required read ordering.
File Description
src/​coreclr/​vm/​method.hpp Orders the HasPrecode flag read before subsequent entry point slot reads.

Review effort: Balanced

@AndyAyersMS
AndyAyersMS merged commit 313d5cd into dotnet:main Nov 5, 2026
104 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 6, 2026
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.

3 participants