Fix concurrent Reflection.Emit TypeLoadException (#129230)#130696
Open
steveisok wants to merge 1 commit into
Open
Fix concurrent Reflection.Emit TypeLoadException (#129230)#130696steveisok wants to merge 1 commit into
steveisok wants to merge 1 commit into
Conversation
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: 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. |
Contributor
|
Tagging subscribers to this area: @agocke |
Contributor
There was a problem hiding this comment.
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
AddEventToLookUpTableappend fromRegMeta::_DefineEvent. - Remove the emit-side
AddPropertyToLookUpTableappend fromRegMeta::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 on lines
2751
to
2752
| IfFailGo(_SetPropertyProps(*pmdProp, dwPropFlags, dwCPlusTypeFlag, pValue, cchValue, mdSetter, | ||
| mdGetter, rmdOtherMethods)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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