forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericMethod.cpp
More file actions
250 lines (212 loc) · 10.4 KB
/
GenericMethod.cpp
File metadata and controls
250 lines (212 loc) · 10.4 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "il2cpp-config.h"
#include "metadata/GenericMetadata.h"
#include "metadata/GenericMethod.h"
#include "metadata/GenericSharing.h"
#include "metadata/Il2CppGenericMethodCompare.h"
#include "metadata/Il2CppGenericMethodHash.h"
#include "os/Mutex.h"
#include "utils/Memory.h"
#include "vm/Class.h"
#include "vm/Exception.h"
#include "vm/GenericClass.h"
#include "vm/MetadataAlloc.h"
#include "vm/MetadataCache.h"
#include "vm/MetadataLock.h"
#include "vm/Method.h"
#include "vm/Runtime.h"
#include "vm/Type.h"
#include "utils/Il2CppHashMap.h"
#include "il2cpp-class-internals.h"
#include "il2cpp-runtime-metadata.h"
#include "il2cpp-runtime-stats.h"
#include <string>
#include "hybridclr/metadata/MetadataUtil.h"
#include "hybridclr/metadata/MetadataModule.h"
#include "hybridclr/interpreter/InterpreterModule.h"
using il2cpp::metadata::GenericMetadata;
using il2cpp::metadata::GenericSharing;
using il2cpp::os::FastAutoLock;
using il2cpp::vm::Class;
using il2cpp::vm::GenericClass;
using il2cpp::vm::MetadataCalloc;
using il2cpp::vm::MetadataCache;
using il2cpp::vm::Method;
using il2cpp::vm::Runtime;
using il2cpp::vm::Type;
namespace il2cpp
{
namespace metadata
{
typedef Il2CppReaderWriterLockedHashMap<const Il2CppGenericMethod*, MethodInfo*, Il2CppGenericMethodHash, Il2CppGenericMethodCompare> Il2CppGenericMethodMap;
static Il2CppGenericMethodMap s_GenericMethodMap;
static Il2CppGenericMethodMap s_PendingGenericMethodMap;
static void AGenericMethodWhichIsTooDeeplyNestedWasInvoked()
{
vm::Exception::Raise(vm::Exception::GetMaxmimumNestedGenericsException());
}
const MethodInfo* GenericMethod::GetGenericVirtualMethod(const MethodInfo* vtableSlotMethod, const MethodInfo* genericVirtualMethod)
{
IL2CPP_NOT_IMPLEMENTED_NO_ASSERT(GetGenericVirtualMethod, "We should only do the following slow method lookup once and then cache on type itself.");
const Il2CppGenericInst* classInst = NULL;
if (vtableSlotMethod->is_inflated)
{
classInst = vtableSlotMethod->genericMethod->context.class_inst;
vtableSlotMethod = vtableSlotMethod->genericMethod->methodDefinition;
}
Il2CppGenericMethod gmethod = { 0 };
gmethod.methodDefinition = vtableSlotMethod;
gmethod.context.class_inst = classInst;
gmethod.context.method_inst = genericVirtualMethod->genericMethod->context.method_inst;
return metadata::GenericMethod::GetMethod(&gmethod, true);
}
const MethodInfo* GenericMethod::GetMethod(const Il2CppGenericMethod* gmethod, bool copyMethodPtr)
{
// This can be NULL only when we have hit the generic recursion depth limit.
if (gmethod == NULL)
{
MethodInfo* newMethod = (MethodInfo*)MetadataCalloc(1, sizeof(MethodInfo));
newMethod->methodPointer = AGenericMethodWhichIsTooDeeplyNestedWasInvoked;
return newMethod;
}
// First check for an already constructed generic method using the shared/reader lock
MethodInfo* existingMethod;
if (s_GenericMethodMap.TryGet(gmethod, &existingMethod))
return existingMethod;
return CreateMethodLocked(gmethod, copyMethodPtr);
}
const MethodInfo* GenericMethod::CreateMethodLocked(const Il2CppGenericMethod* gmethod, bool copyMethodPtr)
{
// We need to inflate a new generic method, take the metadata mutex
// All code below this point can and does assume mutual exclusion
FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
// Recheck the s_GenericMethodMap in case there was a race to add this generic method
MethodInfo* existingMethod;
if (s_GenericMethodMap.TryGet(gmethod, &existingMethod))
return existingMethod;
// GetMethodLocked may be called recursively, we keep tracking of pending inflations
if (s_PendingGenericMethodMap.TryGet(gmethod, &existingMethod))
return existingMethod;
if (copyMethodPtr)
gmethod = MetadataCache::GetGenericMethod(gmethod->methodDefinition, gmethod->context.class_inst, gmethod->context.method_inst);
const MethodInfo* methodDefinition = gmethod->methodDefinition;
Il2CppClass* declaringClass = methodDefinition->klass;
if (gmethod->context.class_inst)
{
Il2CppGenericClass* genericClassDeclaringType = GenericMetadata::GetGenericClass(methodDefinition->klass, gmethod->context.class_inst);
declaringClass = GenericClass::GetClass(genericClassDeclaringType);
// we may fail if we cannot construct generic type
if (!declaringClass)
return NULL;
}
MethodInfo* newMethod = (MethodInfo*)MetadataCalloc(1, sizeof(MethodInfo));
// we set the pending generic method map here because the initialization may recurse and try to retrieve the same generic method
// this is safe because we *always* take the lock when retrieving the MethodInfo from a generic method.
// if we move lock to only if MethodInfo needs constructed then we need to revisit this since we could return a partially initialized MethodInfo
s_PendingGenericMethodMap.Add(gmethod, newMethod);
newMethod->klass = declaringClass;
newMethod->flags = methodDefinition->flags;
newMethod->iflags = methodDefinition->iflags;
newMethod->slot = methodDefinition->slot;
newMethod->name = methodDefinition->name;
newMethod->is_generic = false;
newMethod->is_inflated = true;
newMethod->token = methodDefinition->token;
newMethod->return_type = GenericMetadata::InflateIfNeeded(methodDefinition->return_type, &gmethod->context, true);
newMethod->parameters_count = methodDefinition->parameters_count;
newMethod->parameters = GenericMetadata::InflateParameters(methodDefinition->parameters, methodDefinition->parameters_count, &gmethod->context, true);
newMethod->genericMethod = gmethod;
if (!gmethod->context.method_inst)
{
if (methodDefinition->is_generic)
newMethod->is_generic = true;
if (!declaringClass->generic_class)
{
newMethod->genericContainerHandle = methodDefinition->genericContainerHandle;
}
newMethod->methodMetadataHandle = methodDefinition->methodMetadataHandle;
}
else if (!il2cpp::metadata::GenericMetadata::ContainsGenericParameters(newMethod))
{
// we only need RGCTX for generic instance methods
newMethod->rgctx_data = GenericMetadata::InflateRGCTX(gmethod->methodDefinition->klass->image, gmethod->methodDefinition->token, &gmethod->context);
}
newMethod->invoker_method = MetadataCache::GetInvokerMethodPointer(methodDefinition, &gmethod->context);
newMethod->methodPointer = MetadataCache::GetMethodPointer(methodDefinition, &gmethod->context, true, true);
bool isAdjustorThunkMethod = newMethod->klass->valuetype && hybridclr::metadata::IsInstanceMethod(newMethod);
if (newMethod->methodPointer == nullptr)
{
if ((hybridclr::metadata::IsInterpreterMethod(newMethod) || hybridclr::metadata::MetadataModule::IsImplementedByInterpreter(newMethod)))
{
newMethod->invoker_method = hybridclr::interpreter::InterpreterModule::GetMethodInvoker(newMethod);
newMethod->methodPointerCallByInterp = hybridclr::interpreter::InterpreterModule::GetMethodPointer(newMethod);
if (isAdjustorThunkMethod)
{
newMethod->virtualMethodPointerCallByInterp = hybridclr::interpreter::InterpreterModule::GetAdjustThunkMethodPointer(newMethod);
}
else
{
newMethod->virtualMethodPointerCallByInterp = newMethod->methodPointerCallByInterp;
}
newMethod->methodPointer = newMethod->virtualMethodPointerCallByInterp;
newMethod->isInterpterImpl = true;
newMethod->initInterpCallMethodPointer = true;
}
else
{
// not init anything
}
}
else
{
newMethod->virtualMethodPointerCallByInterp = newMethod->methodPointer;
newMethod->methodPointerCallByInterp = isAdjustorThunkMethod ? MetadataCache::GetMethodPointer(methodDefinition, &gmethod->context, false, true) : newMethod->methodPointer;
newMethod->initInterpCallMethodPointer = true;
}
++il2cpp_runtime_stats.inflated_method_count;
// The generic method is fully created,
// Update the generic method map, this needs to take an exclusive lock
// **** This must happen with the metadata lock held and be released before the metalock is released ****
// **** This prevents deadlocks and ensures that there is no race condition
// **** creating a new method adding it to s_GenericMethodMap and removing it from s_PendingGenericMethodMap
s_GenericMethodMap.Add(gmethod, newMethod);
// Remove the method from the pending table
s_PendingGenericMethodMap.Remove(gmethod);
return newMethod;
}
const Il2CppGenericContext* GenericMethod::GetContext(const Il2CppGenericMethod* gmethod)
{
return &gmethod->context;
}
static std::string FormatGenericArguments(const Il2CppGenericInst* inst)
{
std::string output;
if (inst)
{
output.append("<");
for (size_t i = 0; i < inst->type_argc; ++i)
{
if (i != 0)
output.append(", ");
output.append(Type::GetName(inst->type_argv[i], IL2CPP_TYPE_NAME_FORMAT_FULL_NAME));
}
output.append(">");
}
return output;
}
std::string GenericMethod::GetFullName(const Il2CppGenericMethod* gmethod)
{
const MethodInfo* method = gmethod->methodDefinition;
std::string output;
output.append(Type::GetName(&gmethod->methodDefinition->klass->byval_arg, IL2CPP_TYPE_NAME_FORMAT_FULL_NAME));
output.append(FormatGenericArguments(gmethod->context.class_inst));
output.append("::");
output.append(Method::GetName(method));
output.append(FormatGenericArguments(gmethod->context.method_inst));
return output;
}
void GenericMethod::ClearStatics()
{
s_GenericMethodMap.Clear();
}
} /* namespace vm */
} /* namespace il2cpp */