-
Notifications
You must be signed in to change notification settings - Fork 5k
PersistedAssemblyBuilder: local tokens emitted incorrectly because of the scoping levels #114758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Tagging subscribers to this area: @dotnet/area-system-reflection-emit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Buyaa for picking this up!
...ibraries/System.Reflection.Emit/tests/PortablePdb/ILGeneratorScopesAndSequencePointsTests.cs
Outdated
Show resolved
Hide resolved
...ibraries/System.Reflection.Emit/tests/PortablePdb/ILGeneratorScopesAndSequencePointsTests.cs
Show resolved
Hide resolved
ilGenerator.Emit(OpCodes.Call, methodInfo); | ||
LocalBuilder lb1 = ilGenerator.DeclareLocal(typeof(System.Int32)); | ||
Label label3 = ilGenerator.DefineLabel(); | ||
ilGenerator.Emit(OpCodes.Stloc_0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this use EmitStLoc(ilGenerator, lb1.LocalIndex)
like the original issue (here and below using lb2
) instead of hard-coding the 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That method doesn't affect the bug, just adds more rows, and only called twice
@@ -12,6 +12,8 @@ internal static class MetadataSignatureHelper | |||
{ | |||
internal static BlobBuilder GetLocalSignature(List<LocalBuilder> locals, ModuleBuilderImpl module) | |||
{ | |||
locals.Sort((l1, l2) => l1.LocalIndex - l2.LocalIndex); // sort by created order |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to add a List<LocalBuilder>
field to ILGenerator
and add to it whenever DeclareLocal()
is called and then use that? That should prevent the need for sorting, but requires some dual tracking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is an another way, if you prefer that let me know. For me there is not much difference. Note that this GetLocalSignature(...)
method only called once per method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again.
In the PersistedAssemblyBuilder ILGenerator local variables are added to a Scope, this is important for PDB builder to emit local variables for each local Scope, but for assembly builder Scopes has no effect, all locals loaded from the Scopes added to a list:
runtime/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ILGeneratorImpl.cs
Lines 931 to 948 in 248d6f4
and used for creating local signature for a method body:
runtime/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/ModuleBuilderImpl.cs
Lines 361 to 363 in 248d6f4
When locals created in nested scope with different order, for example inner scope locals created before the outer scope locals are listed after the outer scope locals and added to the signature with that order and causing the bug #114097
As we need to keep the locals scoped for PDB generation, I think we should order the locals list by id before generating the locals signature
Fixes #114097