-
Notifications
You must be signed in to change notification settings - Fork 125
/
DynamicMethodHelper.cs
183 lines (155 loc) · 7.79 KB
/
DynamicMethodHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using AsmResolver.DotNet.Code.Cil;
using AsmResolver.DotNet.Serialized;
using AsmResolver.DotNet.Signatures;
using AsmResolver.DotNet.Signatures.Types;
using AsmResolver.IO;
using AsmResolver.PE.DotNet.Cil;
using AsmResolver.PE.DotNet.Metadata.Tables.Rows;
namespace AsmResolver.DotNet.Dynamic
{
internal static class DynamicMethodHelper
{
private static readonly MethodInfo GetTypeFromHandleUnsafeMethod;
static DynamicMethodHelper()
{
// We need to use reflection for this to stay compatible with .netstandard 2.0.
GetTypeFromHandleUnsafeMethod = typeof(Type)
.GetMethod("GetTypeFromHandleUnsafe",
(BindingFlags) (-1),
null,
new[] {typeof(IntPtr)},
null)!;
}
public static void ReadLocalVariables(CilMethodBody methodBody, MethodDefinition method, byte[] localSig)
{
if (method.Module is not SerializedModuleDefinition module)
throw new ArgumentException("Method body should reference a serialized module.");
var reader = new BinaryStreamReader(localSig);
if (ReadLocalVariableSignature(new BlobReadContext(module.ReaderContext), ref reader)
is not { } localsSignature)
{
throw new ArgumentException("Invalid local variables signature.");
}
for (int i = 0; i < localsSignature.VariableTypes.Count; i++)
methodBody.LocalVariables.Add(new CilLocalVariable(localsSignature.VariableTypes[i]));
}
private static TypeSignature ReadTypeSignature(in BlobReadContext context, ref BinaryStreamReader reader)
{
return (ElementType) reader.PeekByte() == ElementType.Internal
? ReadInternalTypeSignature(context, ref reader)
: TypeSignature.FromReader(in context, ref reader);
}
private static TypeSignature ReadInternalTypeSignature(in BlobReadContext context, ref BinaryStreamReader reader)
{
var address = IntPtr.Size switch
{
4 => new IntPtr(reader.ReadInt32()),
_ => new IntPtr(reader.ReadInt64())
};
// Let the runtime translate the address to a type and import it.
var clrType = (Type?) GetTypeFromHandleUnsafeMethod.Invoke(null, new object[] { address });
var type = clrType is not null
? new ReferenceImporter(context.ReaderContext.ParentModule).ImportType(clrType)
: InvalidTypeDefOrRef.Get(InvalidTypeSignatureError.IllegalTypeSpec);
return new TypeDefOrRefSignature(type);
}
private static LocalVariablesSignature ReadLocalVariableSignature(
in BlobReadContext context,
ref BinaryStreamReader reader)
{
var result = new LocalVariablesSignature();
result.Attributes = (CallingConventionAttributes) reader.ReadByte();
if (!reader.TryReadCompressedUInt32(out uint count))
{
context.ReaderContext.BadImage("Invalid number of local variables in local variable signature.");
return result;
}
for (int i = 0; i < count; i++)
result.VariableTypes.Add(ReadTypeSignature(context, ref reader));
return result;
}
public static void ReadReflectionExceptionHandlers(CilMethodBody methodBody,
IList<object>? ehInfos, byte[] ehHeader, ReferenceImporter importer)
{
//Sample needed!
if (ehHeader is { Length: > 4 })
throw new NotImplementedException("Exception handlers from ehHeader not supported yet.");
if (ehInfos is { Count: > 0 })
{
foreach (var ehInfo in ehInfos)
InterpretEHInfo(methodBody, importer, ehInfo);
}
}
private static void InterpretEHInfo(CilMethodBody methodBody, ReferenceImporter importer, object ehInfo)
{
for (int i = 0; i < FieldReader.ReadField<int>(ehInfo, "m_currentCatch"); i++)
{
// Get ExceptionHandlerInfo Field Values
int endFinally = FieldReader.ReadField<int>(ehInfo, "m_endFinally");
var instructions = methodBody.Instructions;
var endFinallyLabel = endFinally >= 0
? instructions.GetByOffset(endFinally)?.CreateLabel() ?? new CilOffsetLabel(endFinally)
: null;
int tryStart = FieldReader.ReadField<int>(ehInfo, "m_startAddr");
int tryEnd = FieldReader.ReadField<int>(ehInfo, "m_endAddr");
int handlerStart = FieldReader.ReadField<int[]>(ehInfo, "m_catchAddr")![i];
int handlerEnd = FieldReader.ReadField<int[]>(ehInfo, "m_catchEndAddr")![i];
var exceptionType = FieldReader.ReadField<Type?[]>(ehInfo, "m_catchClass")![i];
var handlerType = (CilExceptionHandlerType)FieldReader.ReadField<int[]>(ehInfo, "m_type")![i];
var endTryLabel = instructions.GetByOffset(tryEnd)?.CreateLabel() ?? new CilOffsetLabel(tryEnd);
// Create the handler
var handler = new CilExceptionHandler
{
HandlerType = handlerType,
TryStart = instructions.GetLabel(tryStart),
TryEnd = handlerType == CilExceptionHandlerType.Finally ? endFinallyLabel : endTryLabel,
FilterStart = null,
HandlerStart = instructions.GetLabel(handlerStart),
HandlerEnd = instructions.GetLabel(handlerEnd),
ExceptionType = exceptionType is not null ? importer.ImportType(exceptionType) : null
};
methodBody.ExceptionHandlers.Add(handler);
}
}
public static object ResolveDynamicResolver(object dynamicMethodObj)
{
//Convert dynamicMethodObj to DynamicResolver
if (dynamicMethodObj is Delegate del)
dynamicMethodObj = del.Method;
if (dynamicMethodObj is null)
throw new ArgumentNullException(nameof(dynamicMethodObj));
//We use GetType().FullName just to avoid the System.Reflection.Emit.LightWeight Dll
if (dynamicMethodObj.GetType().FullName == "System.Reflection.Emit.DynamicMethod+RTDynamicMethod")
dynamicMethodObj = FieldReader.ReadField<object>(dynamicMethodObj, "m_owner")!;
if (dynamicMethodObj.GetType().FullName == "System.Reflection.Emit.DynamicMethod")
{
object? resolver = FieldReader.ReadField<object>(dynamicMethodObj, "m_resolver");
if (resolver != null)
dynamicMethodObj = resolver;
}
//Create Resolver if it does not exist.
if (dynamicMethodObj.GetType().FullName == "System.Reflection.Emit.DynamicMethod")
{
var dynamicResolver = typeof(OpCode).Module
.GetTypes()
.First(t => t.Name == "DynamicResolver");
object? ilGenerator = dynamicMethodObj
.GetType()
.GetRuntimeMethods()
.First(q => q.Name == "GetILGenerator")
.Invoke(dynamicMethodObj, null);
//Create instance of dynamicResolver
dynamicMethodObj = Activator.CreateInstance(dynamicResolver, (BindingFlags)(-1), null, new[]
{
ilGenerator
}, null)!;
}
return dynamicMethodObj;
}
}
}