-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.IO; | ||
using System.Reflection.Metadata; | ||
using System.Reflection.Metadata.Ecma335; | ||
using System.Runtime.Loader; | ||
using Xunit; | ||
|
||
namespace System.Reflection.Emit.Tests | ||
|
@@ -368,5 +369,86 @@ private class TestDocument : ISymbolDocumentWriter | |
public void SetCheckSum(Guid algorithmId, byte[] checkSum) => throw new NotImplementedException(); | ||
public void SetSource(byte[] source) => throw new NotImplementedException(); | ||
} | ||
|
||
[Fact] | ||
public void InnerScopeLocalCreatedBeforeOuterScopeLocal() | ||
{ | ||
|
||
PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); | ||
Emit(type); | ||
|
||
using var stream = new MemoryStream(); | ||
ab.Save(stream); | ||
|
||
stream.Seek(0, SeekOrigin.Begin); | ||
var assembly = AssemblyLoadContext.Default.LoadFromStream(stream); | ||
var method = assembly.GetType("MyType")!.GetMethod("Test"); | ||
method!.Invoke(null, null); // should not throw | ||
|
||
buyaa-n marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static void Emit(TypeBuilder typeBuilder) | ||
{ | ||
MethodBuilder methb = typeBuilder.DefineMethod("Test", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), []); | ||
ILGenerator ilGenerator = methb.GetILGenerator(); | ||
Label label0 = ilGenerator.DefineLabel(); | ||
ilGenerator.Emit(OpCodes.Ldc_I4_0); | ||
ilGenerator.Emit(OpCodes.Ldc_I4_0); | ||
ilGenerator.Emit(OpCodes.Beq, label0); | ||
ilGenerator.BeginScope(); | ||
Label label1 = ilGenerator.DefineLabel(); | ||
Label label2 = ilGenerator.DefineLabel(); | ||
ilGenerator.MarkLabel(label1); | ||
MethodInfo methodInfo = typeof(Util).GetMethod(nameof(Util.CCC), new Type[] { }); | ||
ilGenerator.Emit(OpCodes.Call, methodInfo); | ||
ilGenerator.Emit(OpCodes.Ldc_I4, 0); | ||
ilGenerator.Emit(OpCodes.Beq, label2); | ||
methodInfo = typeof(Util).GetMethod(nameof(Util.NNN), new Type[] { }); | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
ilGenerator.Emit(OpCodes.Ldc_I4_3); | ||
ilGenerator.Emit(OpCodes.Ldloc_0); | ||
ilGenerator.Emit(OpCodes.Ceq); | ||
ilGenerator.Emit(OpCodes.Ldc_I4_0); | ||
ilGenerator.Emit(OpCodes.Beq, label3); | ||
methodInfo = typeof(Util).GetMethod(nameof(Util.OOO), new Type[] { }); | ||
ilGenerator.Emit(OpCodes.Call, methodInfo); | ||
ilGenerator.MarkLabel(label3); | ||
ilGenerator.Emit(OpCodes.Br, label1); | ||
ilGenerator.MarkLabel(label2); | ||
ilGenerator.EndScope(); | ||
ilGenerator.MarkLabel(label0); | ||
methodInfo = typeof(TestClass).GetMethod(nameof(TestClass.New), new Type[] { }); | ||
ilGenerator.Emit(OpCodes.Call, methodInfo); | ||
LocalBuilder lb2 = ilGenerator.DeclareLocal(typeof(TestClass)); | ||
ilGenerator.Emit(OpCodes.Stloc_1); | ||
ilGenerator.Emit(OpCodes.Ldloc_1); | ||
methodInfo = typeof(Util).GetMethod(nameof(Util.EEE), new Type[] { typeof(TestClass) }); | ||
ilGenerator.Emit(OpCodes.Call, methodInfo); | ||
ilGenerator.Emit(OpCodes.Ret); | ||
|
||
typeBuilder.CreateType(); | ||
} | ||
} | ||
} | ||
|
||
public class TestClass : object | ||
{ | ||
public static TestClass New() | ||
{ | ||
return new TestClass(); | ||
} | ||
} | ||
|
||
|
||
public static class Util | ||
{ | ||
public static bool CCC() => false; | ||
|
||
public static void EEE(TestClass b) { } | ||
|
||
public static int NNN() => 3; | ||
|
||
public static void OOO() { } | ||
} | ||
} |
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 toILGenerator
and add to it wheneverDeclareLocal()
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.