Skip to content

Align PGO instrumentation data to 8 bytes within the data region - #136890

Closed
lewing wants to merge 2 commits into
mainfrom
lewing-investigate-pgo-av-134695
Closed

lewing wants to merge 2 commits into
mainfrom
lewing-investigate-pgo-av-134695

Conversation

@lewing

@lewing lewing commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

PGO entry offsets are laid out relative to the start of the data region, but countsOffset (the end of the compressed schema prefix) was only aligned to size_t. On 32-bit targets, 8-byte-aligned entries such as ValueHistogram therefore landed at positions that depended on the schema size, and code that lays out the schema from 0 disagreed with the writer:

  • getPgoInstrumentationResultsInstance re-laid out the schema from 0 and copied from countsOffset. It read counts at shifted offsets and up to 4 bytes past the allocation, causing an AV when the allocation ended on a page boundary. Two Windows x86 Helix dumps show this: countsOffset 12, ValueHistogramIntCount + 32 × ValueHistogram, and a 260-byte block read as 264.
  • The text-format PGO loader laid out from 0 and then shifted every entry by countsOffset, while its reader re-lays out from countsOffset.

Aligning countsOffset to the largest entry alignment (8) makes the layout independent of the schema size, so all of these agree. There's no change on 64-bit, where countsOffset was already a multiple of 8. The bug is present in release/9.0 and release/10.0.

This replaces the reader-side rebasing in the first commit (also the approach of #135515).

Resolves #134695

Note

This PR was drafted with GitHub Copilot.

getPgoInstrumentationResultsInstance re-laid out the schema starting at
offset 0, while the writer (allocPgoInstrumentationBySchemaInstance) and
all other readers lay it out starting at countsOffset. countsOffset is
only size_t aligned, so on 32-bit platforms 8-byte aligned entries (for
example ValueHistogram) were placed differently. The copy then read past
the end of the allocation, causing an AV when it ended on a page
boundary, and the JIT saw counts at the wrong offsets.

Fixes #134695

Co-authored-by: Copilot App <[email protected]>
@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.

Revert the reader-side rebasing and instead make countsOffset a multiple
of the largest entry alignment (8). Entry layout is relative to the data
region, so with countsOffset only size_t aligned, 32-bit layouts depended
on where the schema prefix ended. That broke getPgoInstrumentationResults-
Instance (lays out from 0) and the text-format loader (lays out from 0,
then shifts by countsOffset). No change on 64-bit, where countsOffset was
already a multiple of 8.

Co-authored-by: Copilot App <[email protected]>
@lewing lewing changed the title Fix PGO schema layout mismatch when copying dynamic PGO data Align PGO instrumentation data to 8 bytes within the data region Nov 3, 2026
@lewing

lewing commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@EgorBo, on #135515 you mentioned fixing this at the cause rather than the symptom. This PR now does that in the allocator: countsOffset is aligned to 8 within the data region, so the entry layout no longer depends on the schema size on 32-bit. That also fixes the same mismatch in the text-format PGO loader. Is this the cause you had in mind, or were you planning something different?

Note

This comment was drafted with GitHub Copilot.

@EgorBo

EgorBo commented Nov 3, 2026

Copy link
Copy Markdown
Member

I think this is basically the same band-aid rather than a complete fix. The root cause is that value probing requested 8-byte alignment on 32-bit even though it never actually needed it. As a fix, I think we can make ValueHistogram always 32-bit (and hence 4-byte aligned). It's currently only used for small integers anyway (e.g. memmove's length), so we can just truncate the value to 32 bits. It's not a correctness issue if the length is ever bigger, and that's unlikely to happen in practice anyway. As a bonus, the histograms take up less space. This might require a MIBC format version bump. I'm OOF until 11/16, so cc @dotnet/jit-contrib

@EgorBo

EgorBo commented Nov 3, 2026

Copy link
Copy Markdown
Member

@lewing

lewing commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

Closing in favor of @EgorBo's approach (making ValueHistogram a 4-byte entry on all targets). On 32-bit, ValueHistogram is the only 8-byte-aligned entry kind the JIT emits, since 64-bit counters are compiled out. Its declared layout also disagrees with how the value-profile helper writes the table and how the JIT reads it, which this alignment change doesn't address. A PR based on his commit will follow.

Note

This comment was drafted with GitHub Copilot.

@lewing lewing closed this Nov 3, 2026
lewing added a commit that referenced this pull request Nov 4, 2026
This PR uses @EgorBo's fix from #136890
([comment](#136890 (comment)));
his commit is cherry-picked as-is. `ValueHistogram` asked for 8-byte
entries and alignment it never needed. On 32-bit it was the only
8-byte-aligned kind the JIT emits. The VM lays out the schema from
`countsOffset` (only 4-byte aligned) but reads it back from offset 0, so
the two layouts disagreed and the copy read 4 bytes past the end of the
buffer. Value histograms are now 32-bit on all targets. Replaces #135515
and #136890.

A second commit fixes MIBC merging. An old (0x242) and a new (0x241)
value histogram at the same IL offset were treated as the same entry, so
depending on order the merge threw or kept the stale one. It now keeps
the new entry.

**Follow-ups / impact**
- Value-histogram records in existing MIBC and R2R data are ignored
until retrained. They don't crash.
- `MibcConfig.FormatVersion` is now 1.1. Only `dotnet-pgo merge` checks
it, with a warning.
- The JIT-EE GUID bump means SuperPMI collections need refreshing.
- Truncating values to 32 bits is safe: the importer only uses a
profiled value after a full-width range check.
- release/9.0 and release/10.0 have the same bug; servicing is up to the
maintainers.

**Validation (osx-arm64):** the checked CLR build passed, and all 5
`src/tests/JIT/PGO` tests passed. A TieredPGO app recorded correct
32-bit samples, and Tier1 used them for the profiled
memmove/SequenceEqual guard. Not run: x86 and SuperPMI.

Resolves #134695

> [!NOTE]
> This PR description was generated with GitHub Copilot.

---------

Co-authored-by: Egor Bogatov <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: fe9884cc-3de1-4d09-82cd-e2e2869df043
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.

Crash: access violation in coreclr!PgoManager::getPgoInstrumentationResults during tier-1 background JIT (Windows x86)

2 participants