Skip to content

Fix cDAC big-RVA field offset detection to mask packed type bits - #130501

Merged
max-charlamb merged 1 commit into
dotnet:mainfrom
max-charlamb:dev/max-charlamb/cdac-fielddesc-bigrva-offset
Jul 10, 2026
Merged

max-charlamb merged 1 commit into
dotnet:mainfrom
max-charlamb:dev/max-charlamb/cdac-fielddesc-bigrva-offset

Conversation

@max-charlamb

Copy link
Copy Markdown
Member

Summary

Fixes a bug in the cDAC's RuntimeTypeSystem contract where big-RVA field offsets were never resolved from metadata.

FieldDesc::DWord2 packs the 27-bit field offset in its low bits and the 5-bit field type in its high bits (m_dwOffset : 27 / m_type : 5, see src/coreclr/vm/field.h). GetFieldDescOffset compared the full DWord2 against the FIELD_OFFSET_BIG_RVA sentinel:

if (fieldDesc.DWord2 == _target.ReadGlobal<uint>(Constants.Globals.FieldOffsetBigRVA))

But the sentinel is a 27-bit offset value (FIELD_OFFSET_MAX - 5). Every real field has a non-zero type in the high 5 bits, so DWord2 can never equal the 27-bit sentinel. The big-RVA branch was therefore dead, and callers received the sentinel value back as the field offset instead of the RVA read from metadata.

Fix

Mask off the type bits before comparing, matching the native runtime, which compares the m_dwOffset bitfield directly (FieldDesc::GetOffset):

uint offset = fieldDesc.DWord2 & (uint)FieldDescFlags2.OffsetMask;
if (offset == _target.ReadGlobal<uint>(Constants.Globals.FieldOffsetBigRVA))

This also mirrors the sibling IsFieldDescEnCNew, which already masks with OffsetMask before comparing its sentinel — good evidence this was a simple oversight in GetFieldDescOffset.

Testing

  • Added MethodTableTests.GetFieldDescOffset_BigRVA_ResolvesFromMetadata and GetFieldDescOffset_NonBigRVA_ReturnsMaskedOffset, plus a MockFieldDesc helper in the RuntimeTypeSystem mock.
  • Verified the big-RVA test fails without the fix (returns 0x07FFFFFA, the leaked sentinel) and passes with it (returns the RVA 0x1234).
  • Full MethodTableTests suite (136 tests) passes.

Note on dump tests: a real big-RVA field requires an RVA static whose offset exceeds FIELD_OFFSET_MAX (~128 MB), which isn't reproducible in a debuggee/dump test, so this is validated via unit tests instead.

Note

This PR was created with GitHub Copilot.

FieldDesc::DWord2 packs the 27-bit field offset in its low bits and the 5-bit
field type in its high bits. GetFieldDescOffset compared the full DWord2 against
the FIELD_OFFSET_BIG_RVA sentinel, but the sentinel is a 27-bit offset value, so
for any real field (which always has a non-zero type in the high bits) the
comparison never matched. As a result big-RVA fields were never resolved from
metadata and callers saw the sentinel value returned as the offset.

Mask off the type bits before comparing, matching the native runtime, which
compares the m_dwOffset bitfield directly (FieldDesc::GetOffset in
src/coreclr/vm/field.h). This also mirrors the sibling IsFieldDescEnCNew, which
already masks with OffsetMask before comparing its sentinel.

Adds unit tests for the big-RVA and normal offset paths, plus a MockFieldDesc
helper in the RuntimeTypeSystem mock. The big-RVA test fails without the fix
(the sentinel leaks out as the offset).

Co-authored-by: Copilot <[email protected]>

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.

Pull request overview

Fixes cDAC IRuntimeTypeSystem.GetFieldDescOffset big-RVA detection by masking out the packed field-type bits in FieldDesc.DWord2 before comparing against the FIELD_OFFSET_BIG_RVA sentinel, enabling correct fallback to metadata RVA resolution.

Changes:

  • Mask FieldDesc.DWord2 with FieldDescFlags2.OffsetMask before comparing against Constants.Globals.FieldOffsetBigRVA, and reuse the masked value for the return path.
  • Extend cDAC unit-test mocks with a MockFieldDesc layout and an AddFieldDesc helper for creating packed FieldDesc instances.
  • Add unit tests covering both the non-big-RVA masked-offset behavior and big-RVA metadata RVA resolution.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/RuntimeTypeSystem_1.cs Fixes big-RVA detection by comparing the masked 27-bit offset rather than the full packed DWord2.
src/native/managed/cdac/tests/UnitTests/MockDescriptors/MockDescriptors.RuntimeTypeSystem.cs Adds a FieldDesc mock layout and helper allocation routine used by new tests.
src/native/managed/cdac/tests/UnitTests/MethodTableTests.cs Adds unit tests that validate masked offsets for normal fields and metadata-based RVA resolution for big-RVA fields.

@max-charlamb
max-charlamb requested a review from rcj1 July 10, 2026 16:46
@max-charlamb
max-charlamb marked this pull request as ready for review July 10, 2026 16:46
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.

@max-charlamb
max-charlamb marked this pull request as draft July 10, 2026 16:57
@max-charlamb
max-charlamb marked this pull request as ready for review July 10, 2026 16:59
@max-charlamb
max-charlamb enabled auto-merge (squash) July 10, 2026 18:11
@max-charlamb

Copy link
Copy Markdown
Member Author

/ba-g osx infra timeout

@max-charlamb
max-charlamb merged commit 24c0317 into dotnet:main Jul 10, 2026
84 of 87 checks passed
@max-charlamb
max-charlamb deleted the dev/max-charlamb/cdac-fielddesc-bigrva-offset branch July 10, 2026 19:55
max-charlamb pushed a commit to max-charlamb/runtime that referenced this pull request Jul 10, 2026
Merging main brought in the big-RVA offset fix (dotnet#130501), whose unit tests call
the two-arg GetFieldDescOffset(ptr, FieldDefinition?) overload that this PR
removed in favor of the single-arg GetFieldDescOffset(ptr) (which resolves the
FieldDefinition internally). Update the tests to the new API:

- Non-big-RVA test: call GetFieldDescOffset(ptr) and assert the masked offset.
- Big-RVA test: assert the sentinel is detected after masking the type bits --
  with no enclosing MethodTable/metadata wired, the detected big-RVA path finds
  no FieldDefinition and throws InvalidOperationException. This still fails
  against the pre-fix comparison (which never detects the sentinel), so it
  remains a valid regression guard, without pulling Loader/EcmaMetadata mocks
  into the RuntimeTypeSystem-only test harness.

Removes the now-unused metadata-building helper and its usings.

Co-authored-by: Copilot <[email protected]>
max-charlamb added a commit that referenced this pull request Jul 10, 2026
…leData changes (#130531)

## Summary
Small docs-only follow-up aligning the cDAC data-contract documentation
with two recently merged changes that updated the contract
implementations but not their docs.

### 1. `RuntimeTypeSystem.md` -- big-RVA offset masking (follow-up to
#130501)
#130501 fixed big-RVA field-offset detection by masking the packed type
bits before comparing `FieldDesc::DWord2` against the
`FIELD_OFFSET_BIG_RVA` sentinel, but left the `GetFieldDescOffset`
pseudocode showing the old unmasked comparison. Updated the pseudocode
to mask the offset out of the packed `DWord2` (27-bit offset + 5-bit
type) before the sentinel check, matching the shipped implementation and
native `FieldDesc::GetOffset`.

### 2. `GC.md` -- `JupiterRefCount` removal (follow-up to #130448)
#130448 removed the vestigial `JupiterRefCount` field from the cDAC
`HandleData` contract, but left the `CreateHandleData` pseudocode
setting it. Removed the stale `handleData.JupiterRefCount = 0;`
assignment. (The COM ABI struct still exposes the field and reports `0`,
unchanged.)

Docs-only change.

---------

Co-authored-by: Max Charlamb <[email protected]>
Co-authored-by: Copilot <[email protected]>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 11, 2026
max-charlamb pushed a commit to max-charlamb/runtime that referenced this pull request Jul 13, 2026
Merging main brought in the big-RVA offset fix (dotnet#130501), whose unit tests call
the two-arg GetFieldDescOffset(ptr, FieldDefinition?) overload that this PR
removed in favor of the single-arg GetFieldDescOffset(ptr) (which resolves the
FieldDefinition internally). Update the tests to the new API:

- Non-big-RVA test: call GetFieldDescOffset(ptr) and assert the masked offset.
- Big-RVA test: assert the sentinel is detected after masking the type bits --
  with no enclosing MethodTable/metadata wired, the detected big-RVA path finds
  no FieldDefinition and throws InvalidOperationException. This still fails
  against the pre-fix comparison (which never detects the sentinel), so it
  remains a valid regression guard, without pulling Loader/EcmaMetadata mocks
  into the RuntimeTypeSystem-only test harness.

Removes the now-unused metadata-building helper and its usings.

Co-authored-by: Copilot <[email protected]>
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…0501)

## Summary
Fixes a bug in the cDAC's `RuntimeTypeSystem` contract where big-RVA
field offsets were never resolved from metadata.

`FieldDesc::DWord2` packs the **27-bit field offset** in its low bits
and the **5-bit field type** in its high bits (`m_dwOffset : 27` /
`m_type : 5`, see `src/coreclr/vm/field.h`). `GetFieldDescOffset`
compared the **full `DWord2`** against the `FIELD_OFFSET_BIG_RVA`
sentinel:

```csharp
if (fieldDesc.DWord2 == _target.ReadGlobal<uint>(Constants.Globals.FieldOffsetBigRVA))
```

But the sentinel is a 27-bit offset value (`FIELD_OFFSET_MAX - 5`).
Every real field has a non-zero type in the high 5 bits, so `DWord2` can
never equal the 27-bit sentinel. The big-RVA branch was therefore dead,
and callers received the sentinel value back as the field offset instead
of the RVA read from metadata.

## Fix
Mask off the type bits before comparing, matching the native runtime,
which compares the `m_dwOffset` bitfield directly
(`FieldDesc::GetOffset`):

```csharp
uint offset = fieldDesc.DWord2 & (uint)FieldDescFlags2.OffsetMask;
if (offset == _target.ReadGlobal<uint>(Constants.Globals.FieldOffsetBigRVA))
```

This also mirrors the sibling `IsFieldDescEnCNew`, which already masks
with `OffsetMask` before comparing its sentinel — good evidence this was
a simple oversight in `GetFieldDescOffset`.

## Testing
- Added
`MethodTableTests.GetFieldDescOffset_BigRVA_ResolvesFromMetadata` and
`GetFieldDescOffset_NonBigRVA_ReturnsMaskedOffset`, plus a
`MockFieldDesc` helper in the RuntimeTypeSystem mock.
- Verified the big-RVA test **fails without the fix** (returns
`0x07FFFFFA`, the leaked sentinel) and **passes with it** (returns the
RVA `0x1234`).
- Full `MethodTableTests` suite (136 tests) passes.

**Note on dump tests:** a real big-RVA field requires an RVA static
whose offset exceeds `FIELD_OFFSET_MAX` (~128 MB), which isn't
reproducible in a debuggee/dump test, so this is validated via unit
tests instead.

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

Co-authored-by: Max Charlamb <[email protected]>
Co-authored-by: Copilot <[email protected]>
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…leData changes (#130531)

## Summary
Small docs-only follow-up aligning the cDAC data-contract documentation
with two recently merged changes that updated the contract
implementations but not their docs.

### 1. `RuntimeTypeSystem.md` -- big-RVA offset masking (follow-up to
#130501)
#130501 fixed big-RVA field-offset detection by masking the packed type
bits before comparing `FieldDesc::DWord2` against the
`FIELD_OFFSET_BIG_RVA` sentinel, but left the `GetFieldDescOffset`
pseudocode showing the old unmasked comparison. Updated the pseudocode
to mask the offset out of the packed `DWord2` (27-bit offset + 5-bit
type) before the sentinel check, matching the shipped implementation and
native `FieldDesc::GetOffset`.

### 2. `GC.md` -- `JupiterRefCount` removal (follow-up to #130448)
#130448 removed the vestigial `JupiterRefCount` field from the cDAC
`HandleData` contract, but left the `CreateHandleData` pseudocode
setting it. Removed the stale `handleData.JupiterRefCount = 0;`
assignment. (The COM ABI struct still exposes the field and reports `0`,
unchanged.)

Docs-only change.

---------

Co-authored-by: Max Charlamb <[email protected]>
Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 11, 2026
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