Skip to content

Fix concurrent Reflection.Emit TypeLoadException (#129230)#130696

Open
steveisok wants to merge 1 commit into
dotnet:mainfrom
steveisok:steveisok-ideal-disco
Open

Fix concurrent Reflection.Emit TypeLoadException (#129230)#130696
steveisok wants to merge 1 commit into
dotnet:mainfrom
steveisok:steveisok-ideal-disco

Conversation

@steveisok

Copy link
Copy Markdown
Member

PR #125536 added an AddPropertyToLookUpTable call inside CMiniMdRW::AddPropertyToPropertyMap (and AddEventToLookUpTable inside AddEventToEventMap) so that the ENC/hot-reload path, which reaches those functions via metamodelenc.cpp rather than RegMeta::DefineProperty, would also maintain the property/event parent lookup table (fixing #125534).

However the emit path already appended to the lookup table: DefineProperty and _DefineEvent call AddPropertyToPropertyMap/AddEventToEventMap and then also call Add*ToLookUpTable themselves, gated on HasIndirectTable. The S_FALSE branch in AddPropertyToPropertyMap fires under exactly the same condition (an indirect PropertyPtr/EventPtr table exists), so after #125536 the emit path appended the same <member, typedef> entry twice.

AddToLookUpTable only appends once the lazily-built lookup map is non-NULL (the map is built on demand by a reader in FindParentOfHelper). When a concurrent reader has built the map, the duplicate append desyncs the map's Count from the member RID, so a subsequently emitted member is recorded against the wrong parent typedef, surfacing as a TypeLoadException. This is why the regression only reproduces under concurrent emit + reflection.

Remove the now-redundant emit-side appends so the lookup table is maintained in exactly one place (AddPropertyToPropertyMap/AddEventToEventMap), which serves both the emit and ENC/hot-reload callers and preserves the #125534 fix.

Fixes #129230

PR dotnet#125536 added an AddPropertyToLookUpTable call inside
CMiniMdRW::AddPropertyToPropertyMap (and AddEventToLookUpTable inside
AddEventToEventMap) so that the ENC/hot-reload path, which reaches those
functions via metamodelenc.cpp rather than RegMeta::DefineProperty, would
also maintain the property/event parent lookup table (fixing dotnet#125534).

However the emit path already appended to the lookup table: DefineProperty
and _DefineEvent call AddPropertyToPropertyMap/AddEventToEventMap and then
also call Add*ToLookUpTable themselves, gated on HasIndirectTable. The
S_FALSE branch in AddPropertyToPropertyMap fires under exactly the same
condition (an indirect PropertyPtr/EventPtr table exists), so after dotnet#125536
the emit path appended the same <member, typedef> entry twice.

Add*ToLookUpTable only appends once the lazily-built lookup map is non-NULL
(the map is built on demand by a reader in FindParentOf*Helper). When a
concurrent reader has built the map, the duplicate append desyncs the map's
Count from the member RID, so a subsequently emitted member is recorded
against the wrong parent typedef, surfacing as a TypeLoadException. This is
why the regression only reproduces under concurrent emit + reflection.

Remove the now-redundant emit-side appends so the lookup table is maintained
in exactly one place (AddPropertyToPropertyMap/AddEventToEventMap), which
serves both the emit and ENC/hot-reload callers and preserves the dotnet#125534
fix.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@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.

Pull request overview

This PR adjusts CoreCLR’s metadata emit path to avoid duplicating updates to the property/event parent lookup tables, ensuring those tables are maintained in a single location (via CMiniMdRW::AddPropertyToPropertyMap / AddEventToEventMap). This is intended to prevent lookup-table corruption that can surface as TypeLoadException in concurrent Reflection.Emit + reflection scenarios.

Changes:

  • Remove the emit-side AddEventToLookUpTable append from RegMeta::_DefineEvent.
  • Remove the emit-side AddPropertyToLookUpTable append from RegMeta::DefineProperty.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/coreclr/md/compiler/regmeta_emit.cpp Removes redundant event lookup-table append in _DefineEvent.
src/coreclr/md/compiler/emit.cpp Removes redundant property lookup-table append in DefineProperty.

Comment thread src/coreclr/md/compiler/emit.cpp
Comment on lines 2751 to 2752
IfFailGo(_SetPropertyProps(*pmdProp, dwPropFlags, dwCPlusTypeFlag, pValue, cchValue, mdSetter,
mdGetter, rmdOtherMethods));
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.

Regression for concurrent dynamic type generation in 10.0.6

2 participants