Skip to content

Fix deleted field filtering through FieldPtr in MDInternalRW::EnumInit - #137787

Closed
davidwrighton wants to merge 1 commit into
dotnet:mainfrom
davidwrighton:fix-rw-deleted-field-enum
Closed

davidwrighton wants to merge 1 commit into
dotnet:mainfrom
davidwrighton:fix-rw-deleted-field-enum

Conversation

@davidwrighton

Copy link
Copy Markdown
Member

MDInternalRW::EnumInit(mdtFieldDef) handles metadata that has HAS_DELETE set. In that path it translates each FieldPtr index to a field RID, but then checks the deleted flag on GetFieldRecord(index, ...) instead of GetFieldRecord(fieldRid, ...). When the FieldPtr table reorders fields, it checks the wrong field's deleted state. As a result, deleted fields can appear in enumeration (and in reflection/type loading) while live fields are skipped. The MethodDef path in the same function, and the public importer (import.cpp), already use the translated RID.

Repro / regression test

src/tests/reflection/DeletedMetadata/DeletedFieldEnumeration builds a small library with hand-written uncompressed (#-) metadata. CoreCLR opens this format with the RW importer. The metadata has:

  • the HAS_DELETE heap bit set
  • two global fields: Live, and a deleted _Deleted field (SpecialName | RTSpecialName)
  • a FieldPtr table that reverses the physical field order

It loads the library with Assembly.Load(byte[]) and checks ManifestModule.GetFields().

  • Before the fix, it returns ["_Deleted"] instead of ["Live"]. A control image with identity FieldPtr ordering returns ["Live"].
  • After the fix, it returns ["Live"].

Validation (linux-x64 Checked)

  • src/tests/build.sh -Test reflection/reflection.csproj x64 Checked: succeeded.
  • corerun reflection.dll on the existing, unfixed runtime: DeletedFieldEnumeration failed (Expected: ["Live"], Actual: ["_Deleted"]).
  • ./build.sh clr.runtime -c checked plus ./build.sh clr.corelib -c checked, with the outputs copied into Core_Root, then corerun reflection.dll: all 15 tests passed, including DeletedFieldEnumeration.

Note

This PR description and change were generated with GitHub Copilot.

When metadata has HAS_DELETE set and uses a FieldPtr indirection table,
MDInternalRW::EnumInit(mdtFieldDef) checked the deleted state of the Field
row at the FieldPtr index instead of the Field row the FieldPtr entry
points to. With a reordered FieldPtr table this could hide live fields and
expose deleted ones. Use the translated RID, matching the MethodDef path.

Add a regression test that loads hand-crafted uncompressed (#-) metadata
with a reordered FieldPtr table and a deleted global field.

Co-authored-by: Copilot <[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.

@davidwrighton

Copy link
Copy Markdown
Member Author

Eh, some more investigation indicates that deleted fields are even less supported than I thought. Instead of fixing them, I'm going to open a PR to simply delete the deletion support from the metadata implementation.

davidwrighton added a commit that referenced this pull request Nov 21, 2026
CoreCLR's metadata engine had a legacy "deleted token" convention:
incremental emitters set `HAS_DELETE` (0x80),
`IMetaDataEmit::DeleteToken` renamed records to `_Deleted*` or cleared
their parent, and importers filtered those records out.

The runtime has no callers of `DeleteToken` and does not open scopes in
`MDUpdateIncremental` mode. Modern EnC does not require the deleted-name
convention; see #118903. The filtering also contained a
`FieldPtr` indexing bug addressed separately by #137787.
This PR removes the obsolete deletion machinery rather than retaining
that special case.

## Changes

- Remove deleted-record filtering from the internal and public metadata
enumerators and from global-method merging.
- Make public `IMetaDataEmit::DeleteToken` return `E_NOTIMPL`,
preserving its vtable slot.
- Stop setting `HAS_DELETE` in incremental update mode and remove the
associated update-mode rejection and `metainfo` retry.
- Treat `HAS_DELETE` as a reserved, ignored bit. Existing
incremental-mode images remain loadable even if they carry this bit.
- Reject `DELTA_ONLY` (0x20) outside the internal delta-application
path, for both compressed (`#~`) and uncompressed (`#-`) metadata. A
normal image with `HAS_DELETE | DELTA_ONLY` (0xA0) is rejected because
of `DELTA_ONLY`, not because of `HAS_DELETE`.
- Remove `COR_DELETED_NAME_*` and `IsDeletedName` from the native
headers. These headers are not maintained as stable, backward-compatible
public headers.
- Add `DeletedMetadataFlags`, covering successful loads with and without
`HAS_DELETE` and rejection with `DELTA_ONLY`, in both metadata formats.
- Make public `IMetaDataEmit::ApplyEditAndContinue` return `E_NOTIMPL`,
preserving its vtable slot. There are no in-repository callers of this
public API.
- Restore the `MetaDataImportOption` GUID and `CorImportOptions`
declarations with their original values, including `MDImportOptionAll =
0xFFFFFFFF`. `IMetaDataDispenserEx::SetOption` accepts the option as a
validated `VT_UI4` no-op, allowing existing callers to request
`MDImportOptionAll` before opening ordinary metadata.
- Extend the existing metadata-dispenser profiler test to exercise both
behaviors.

## Behavior Change

Deleted-record names and markers no longer hide metadata records, and
`DeleteToken` returns `E_NOTIMPL`. `HAS_DELETE` alone does not prevent
an image from loading. Ordinary loads reject `DELTA_ONLY`.

The public `IMetaDataEmit::ApplyEditAndContinue` API is retired. This is
separate from the internal `IMDInternalImport::ApplyEditAndContinue` API
used by debugger EnC and `MetadataUpdater.ApplyUpdate`. Both internal
delta readers pass `fIsDelta = true`, so those paths continue accepting
EnC deltas, including the 0xA0 flags emitted by
System.Reflection.Metadata.

## Validation

Validated locally on linux-x64 with a Checked runtime:

- `./build.sh clr.native+clr.iltools -c checked` - passed.
- `./build.sh clr.corelib+clr.nativecorelib+libs.pretest -rc checked` -
passed; refreshed stale baseline CoreLib artifacts.
- `src/tests/build.sh -Test
profiler/unittest/metadatagetdispenser.csproj x64 checked` - passed.
- `src/tests/build.sh -GenerateLayoutOnly x64 checked` - passed.
- The metadata-dispenser profiler test, run with `corerun` from its
output directory, failed against the pre-change runtime
(`ApplyEditAndContinue` returned `E_NOINTERFACE` instead of `E_NOTIMPL`)
and passed against the updated runtime, including setting
`MDImportOptionDefault` and `MDImportOptionAll` before opening scopes.
- `src/tests/build.sh -Test
reflection/DeletedMetadata/DeletedMetadataFlags.csproj x64 checked` -
passed; the generated test executable passed with exit code 100.
- `./build.sh libs.pretest -c Release -rc checked` - passed.
- `./dotnet.sh build
src/libraries/System.Runtime.Loader/tests/System.Runtime.Loader.Tests.csproj
-c Release /t:Test /p:RuntimeConfiguration=Checked
'/p:XUnitOptions=-class System.Reflection.Metadata.ApplyUpdateTest'` -
passed all 24 tests, with no failures or skips.

Supersedes #137787.

> [!NOTE]
> This PR description and the code changes were generated with GitHub
Copilot.

---------

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

1 participant