Skip to content
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

Add support for .NET 8.0 and DynamicILInfo to DynamicMethodBodyReader #508

Merged
merged 2 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/DotNet/Emit/DynamicMethodBodyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum DynamicMethodBodyReaderOptions {
/// </summary>
public class DynamicMethodBodyReader : MethodBodyReaderBase, ISignatureReaderHelper {
static readonly ReflectionFieldInfo rtdmOwnerFieldInfo = new ReflectionFieldInfo("m_owner");
static readonly ReflectionFieldInfo dmResolverFieldInfo = new ReflectionFieldInfo("m_resolver");
static readonly ReflectionFieldInfo dmResolverFieldInfo = new ReflectionFieldInfo("m_resolver", "_resolver");
static readonly ReflectionFieldInfo rslvCodeFieldInfo = new ReflectionFieldInfo("m_code");
static readonly ReflectionFieldInfo rslvDynamicScopeFieldInfo = new ReflectionFieldInfo("m_scope");
static readonly ReflectionFieldInfo rslvMethodFieldInfo = new ReflectionFieldInfo("m_method");
Expand All @@ -55,6 +55,8 @@ public class DynamicMethodBodyReader : MethodBodyReaderBase, ISignatureReaderHel
static readonly ReflectionFieldInfo ehEndFinallyFieldInfo = new ReflectionFieldInfo("m_endFinally");
static readonly ReflectionFieldInfo vamMethodFieldInfo = new ReflectionFieldInfo("m_method");
static readonly ReflectionFieldInfo vamDynamicMethodFieldInfo = new ReflectionFieldInfo("m_dynamicMethod");
static readonly ReflectionFieldInfo dmDynamicILInfoFieldInfo = new ReflectionFieldInfo("m_DynamicILInfo", "_dynamicILInfo");
static readonly ReflectionFieldInfo dynILInfoMaxStackFieldInfo = new ReflectionFieldInfo("m_maxStackSize");

readonly ModuleDef module;
readonly Importer importer;
Expand Down Expand Up @@ -99,7 +101,7 @@ void InitializeField(Type type) {
if (fieldInfo is not null)
return;

var flags = SR.BindingFlags.Instance | SR.BindingFlags.Public | SR.BindingFlags.NonPublic;
const SR.BindingFlags flags = SR.BindingFlags.Instance | SR.BindingFlags.Public | SR.BindingFlags.NonPublic;
fieldInfo = type.GetField(fieldName1, flags);
if (fieldInfo is null && fieldName2 is not null)
fieldInfo = type.GetField(fieldName2, flags);
Expand Down Expand Up @@ -175,13 +177,15 @@ public DynamicMethodBodyReader(ModuleDef module, object obj, Importer importer,

if (obj is DynamicMethod dynMethod) {
methodName = dynMethod.Name;
obj = dmResolverFieldInfo.Read(obj);
obj = dmResolverFieldInfo.Read(obj) ?? dmDynamicILInfoFieldInfo.Read(obj);;
if (obj is null)
throw new Exception("No resolver found");
}

if (obj.GetType().ToString() != "System.Reflection.Emit.DynamicResolver")
throw new Exception("Couldn't find DynamicResolver");
string objTypeName = obj.GetType().ToString();
bool isILInfo = objTypeName == "System.Reflection.Emit.DynamicILInfo";
if (objTypeName != "System.Reflection.Emit.DynamicResolver" && !isILInfo)
throw new Exception("Couldn't find DynamicResolver or DynamicILInfo");

var code = rslvCodeFieldInfo.Read(obj) as byte[];
if (code is null)
Expand All @@ -191,7 +195,7 @@ public DynamicMethodBodyReader(ModuleDef module, object obj, Importer importer,
if (delMethod is null)
throw new Exception("No method");
initLocals = delMethod.InitLocals;
maxStack = (int)rslvMaxStackFieldInfo.Read(obj);
maxStack = isILInfo ? (int)dynILInfoMaxStackFieldInfo.Read(obj) : (int)rslvMaxStackFieldInfo.Read(obj);

var scope = rslvDynamicScopeFieldInfo.Read(obj);
if (scope is null)
Expand All @@ -203,8 +207,12 @@ public DynamicMethodBodyReader(ModuleDef module, object obj, Importer importer,
for (int i = 0; i < tokensList.Count; i++)
tokens.Add(tokensList[i]);

ehInfos = (IList<object>)rslvExceptionsFieldInfo.Read(obj);
ehHeader = rslvExceptionHeaderFieldInfo.Read(obj) as byte[];
if (isILInfo)
ehHeader = rslvExceptionsFieldInfo.Read(obj) as byte[];
else {
ehInfos = (IList<object>)rslvExceptionsFieldInfo.Read(obj);
ehHeader = rslvExceptionHeaderFieldInfo.Read(obj) as byte[];
}

UpdateLocals(rslvLocalsFieldInfo.Read(obj) as byte[]);
reader = ByteArrayDataReaderFactory.CreateReader(code);
Expand Down
3 changes: 3 additions & 0 deletions src/DotNet/Emit/MethodTableToTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ static MethodTableToTypeConverter() {
#endif
moduleBuilder = asmb.DefineDynamicModule("DynMod");
}

// In .NET 8+, ILGenerator is an abstract class and the actual ILGenerator implementation is found in RuntimeILGenerator.
localSignatureFieldInfo ??= Type.GetType("System.Reflection.Emit.RuntimeILGenerator")?.GetField("m_localSignature", BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
}

/// <summary>
Expand Down