Skip to content

Remove metadata token deletion (HAS_DELETE) support - #137789

Merged
davidwrighton merged 6 commits into
dotnet:mainfrom
davidwrighton:remove-md-deletion
Nov 21, 2026
Merged

davidwrighton merged 6 commits into
dotnet:mainfrom
davidwrighton:remove-md-deletion

Conversation

@davidwrighton

@davidwrighton davidwrighton commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

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.

The HAS_DELETE / _Deleted token concept was only reachable through
MDUpdateIncremental mode plus IMetaDataEmit::DeleteToken, which nothing in
the runtime uses. The read/write importer's filtering of deleted fields was
also incorrect when a FieldPtr table reordered fields.

- Remove deleted-record filtering from the RW internal importer and the
  public RegMeta enumerators.
- IMetaDataEmit::DeleteToken now returns E_NOTIMPL.
- Stop setting HAS_DELETE in incremental update mode.
- Reject metadata with HAS_DELETE set unless it is an EnC delta
  (System.Reflection.Metadata sets the bit on all deltas).

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.

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.

🟡 Changes recommended

Normal assemblies can bypass deleted-metadata rejection by setting both HAS_DELETE and DELTA_ONLY, exposing deleted fields; compatibility and test coverage also remain unresolved.

4 open issues
What changed in this PR

Removes legacy HAS_DELETE support from CoreCLR’s metadata engine, superseding the earlier FieldPtr filtering fix.

Changes:

  • Removes deleted-record filtering and incremental-mode deletion bookkeeping.
  • Makes IMetaDataEmit::DeleteToken return E_NOTIMPL while preserving its interface slot.
  • Rejects non-delta metadata carrying HAS_DELETE and adds an image-loading regression test.
File Description
src/​tests/​reflection/​DeletedMetadata/​DeletedMetadataRejected.csproj Configures the CoreCLR-specific regression test.
src/​tests/​reflection/​DeletedMetadata/​DeletedMetadataRejected.cs Builds a raw metadata image and checks load rejection.
src/​coreclr/​tools/​metainfo/​mdinfo.cpp Removes the incremental-mode retry.
src/​coreclr/​md/​runtime/​metamodel.cpp Adds shared schema rejection with a delta-flag exemption.
src/​coreclr/​md/​inc/​metamodelrw.h Removes the HasDelete helper.
src/​coreclr/​md/​inc/​metamodel.h Updates the HAS_DELETE comment to describe EnC-only support.
src/​coreclr/​md/​enc/​metamodelrw.cpp Removes deletion-related update-mode checks and flag setting.
src/​coreclr/​md/​enc/​mdinternalrw.cpp Removes internal deleted-record filtering.
src/​coreclr/​md/​compiler/​regmeta_import.cpp Simplifies type-definition enumeration.
src/​coreclr/​md/​compiler/​regmeta_emit.cpp Removes deleted-name filtering from global-method marking.
src/​coreclr/​md/​compiler/​import.cpp Removes deletion-aware member and permission enumeration.
src/​coreclr/​md/​compiler/​emit.cpp Replaces token deletion with E_NOTIMPL.
src/​coreclr/​md/​compiler/​assemblymd.cpp Simplifies exported-type enumeration.

Review effort: Balanced

Comment thread src/coreclr/md/runtime/metamodel.cpp Resolved
Comment thread src/coreclr/md/runtime/metamodel.cpp Resolved

@jkoritzinsky jkoritzinsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe the _Deleted convention is still used by EnC scenarios, so we might need to keep it for enumerators. We can definitely delete the DeleteToken operation from within the native metadata compiler though.

@jkotas

jkotas commented Nov 18, 2026

Copy link
Copy Markdown
Member

I believe the _Deleted convention is still used by EnC scenarios

It should not be #118903 (it never actually worked well).

@jkoritzinsky

Copy link
Copy Markdown
Member

Missed that. Nice! Happy to be done with this convention!

davidwrighton and others added 2 commits November 18, 2026 20:17
GetMDInternalInterface now accepts only the compressed (#~) table stream and
returns CLDB_E_FILE_CORRUPT for #-. The read/write metadata model is still used
for EnC/Hot Reload: deltas are opened directly with GetInternalWithRWFormat, and
EnC modules are converted from #~ via ConvertMDInternalImport.

The DAC opens Reflection.Emit module metadata, which the runtime captures in
the #- format, through GetInternalWithRWFormat directly.

Also remove the obsolete #Schema (ICR) stream handling.

Extend the DeletedMetadataRejected test to cover a successful #~ load, #~ with
HAS_DELETE, and #- with and without HAS_DELETE.

Co-authored-by: Copilot <[email protected]>
Undo the local #- image rejection change. Keep the existing metadata token
delection removal change unchanged.

Co-authored-by: Copilot <[email protected]>
Reject HAS_DELETE and DELTA_ONLY metadata during ordinary loads. Mark the two
EnC delta parsing paths explicitly so delta metadata remains accepted only
while an update is being applied.

Expand metadata loader tests to cover valid compressed and uncompressed images,
rejection of the flags in both formats, and exposure of live and _Deleted-named
fields when HAS_DELETE is absent. Isolate the CoreCLR-only test from merged
runners and NativeAOT.

Co-authored-by: Copilot <[email protected]>
davidwrighton added a commit that referenced this pull request Nov 18, 2026
The runtime doesn't use declarative security (the `DeclSecurity` table /
permission sets). Only ILASM, which emits `.permissionset`, and ILDASM,
which disassembles it, need to read or write them. Both link the
`*_ppdb` builds of the metadata engine.

This PR gates the PermissionSet implementation behind a new
`FEATURE_METADATA_PERMISSION_SETS` define, set only on
`mdcompiler_ppdb`, `mdruntime_ppdb` and `mdruntimerw_ppdb`. Every other
variant gets the stubs: the runtime (`_wks`), the DAC (`_dac`) and the
DBI (`-dbi`).

Without the feature:
- `IMetaDataImport::EnumPermissionSets` returns an empty enumeration
(`S_FALSE`, count 0, null enum handle).
- These return `E_NOTIMPL`:
  - `IMetaDataImport::GetPermissionSetProps`
- `IMetaDataEmit::DefinePermissionSet` and `SetPermissionSetProps`, plus
the internal `_DefinePermissionSet` and `_SetPermissionSetProps`

`GetPermissionSetProps` is removed from the internal `IMDInternalImport`
interface. It had no callers, because ILDASM and ILASM use the public
`IMetaDataImport`/`IMetaDataEmit` APIs. The `MDInternalRO` and
`MDInternalRW` implementations are removed along with it. The cDAC's
`IMetaDataImport` already returns `E_NOTIMPL` for the PermissionSet
APIs, so it is unaffected.

Nothing in `src/coreclr/vm` or `src/coreclr/debug` calls these APIs.

## Behavior change
Profilers and debuggers using the public metadata interfaces from the
runtime or DBI can no longer see or define permission sets. This needs
breaking-change documentation.

## Validation
On linux-x64 Checked:
- `./build.sh clr.runtime+clr.iltools+clr.corelib -c checked` succeeded.
- `build.ninja` sets `FEATURE_METADATA_PERMISSION_SETS` only for the
`mdcompiler_ppdb`, `mdruntime_ppdb` and `mdruntimerw_ppdb` objects.
- Assembled IL containing `.permissionset` on the assembly, a type and a
method with the new `ilasm`. Then ran the new `ildasm` on the output:
all three permission sets round-tripped with their blobs intact. I
repeated this after removing the internal method.
- Ran the same assembly with `corerun`: it executed normally and exited
with code 100.

The permission-set enumeration in `RegMeta::EnumPermissionSets` also
touches code that #137789 modifies, so whichever merges second may need
a trivial rebase.

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

---------

Co-authored-by: Copilot <[email protected]>
Comment thread src/coreclr/md/compiler/emit.cpp Resolved
Comment thread src/coreclr/md/runtime/metamodel.cpp Resolved

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.

🟡 Changes recommended

The change introduces public metadata API and header compatibility regressions, blocks edit-and-continue delta loading, and inconsistently filters deleted security rows.

4 open issues
4 resolved since last review

Review effort: Balanced

Comment thread src/coreclr/inc/corhdr.h Resolved
Comment thread src/coreclr/md/compiler/disp.cpp Resolved
Comment thread src/coreclr/md/compiler/import.cpp Resolved
Comment thread src/coreclr/md/runtime/metamodel.cpp Resolved
@davidwrighton
davidwrighton enabled auto-merge (squash) November 20, 2026 23:21

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.

🟡 Changes recommended

The changed metadata enumeration behavior lacks tests for live and historically marked _Deleted records across import options, including reordered FieldPtr entries.

1 open issue
4 resolved since last review

Review effort: Balanced

Comment thread src/coreclr/md/compiler/import.cpp Resolved
@davidwrighton
davidwrighton merged commit 36f8255 into dotnet:main Nov 21, 2026
100 of 102 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 23, 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.

4 participants