Skip to content

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

Merged
merged 4 commits into from
Apr 22, 2025

Conversation

buyaa-n
Copy link
Contributor

@buyaa-n buyaa-n commented Apr 17, 2025

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:

internal List<LocalBuilder> GetAllLocals()
{
List<LocalBuilder> locals = new List<LocalBuilder>();
if (_locals != null)
{
locals.AddRange(_locals);
}
if (_children != null)
{
foreach (Scope child in _children)
{
locals.AddRange(child.GetAllLocals());
}
}
return locals;

and used for creating local signature for a method body:
StandaloneSignatureHandle signature = il.LocalCount == 0 ? default :
_metadataBuilder.AddStandaloneSignature(_metadataBuilder.GetOrAddBlob(MetadataSignatureHelper.GetLocalSignature(il.Locals, this)));
offset = AddMethodBody(method, il, signature, methodBodyEncoder);

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

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 17, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@steveharter steveharter left a 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!

ilGenerator.Emit(OpCodes.Call, methodInfo);
LocalBuilder lb1 = ilGenerator.DeclareLocal(typeof(System.Int32));
Label label3 = ilGenerator.DefineLabel();
ilGenerator.Emit(OpCodes.Stloc_0);
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@steveharter steveharter left a comment

Choose a reason for hiding this comment

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

Thanks again.

@steveharter steveharter merged commit cc2406f into dotnet:main Apr 22, 2025
83 of 85 checks passed
@steveharter steveharter deleted the fix-114097 branch April 22, 2025 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection.Emit community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PersistedAssemblyBuilder generates invalid IL
2 participants