-
-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathApplicationAnalysisContext.cs
More file actions
291 lines (241 loc) · 10.7 KB
/
Copy pathApplicationAnalysisContext.cs
File metadata and controls
291 lines (241 loc) · 10.7 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using AssetRipper.Primitives;
using Cpp2IL.Core.Api;
using Cpp2IL.Core.Exceptions;
using Cpp2IL.Core.Il2CppApiFunctions;
using Cpp2IL.Core.Logging;
using Cpp2IL.Core.Utils;
using LibCpp2IL;
using LibCpp2IL.Metadata;
namespace Cpp2IL.Core.Model.Contexts;
/// <summary>
/// Top-level class to represent an individual il2cpp application that has been loaded into cpp2il.
/// </summary>
public class ApplicationAnalysisContext : ContextWithDataStorage
{
/// <summary>
/// The IL2CPP binary file this application was loaded from
/// </summary>
public Il2CppBinary Binary => LibCpp2IlContext.Binary;
/// <summary>
/// The IL2CPP global-metadata file this application was loaded from.
/// </summary>
public Il2CppMetadata Metadata => LibCpp2IlContext.Metadata;
/// <summary>
/// The version of the IL2CPP metadata file this application was loaded from.
/// </summary>
public float MetadataVersion => Metadata.MetadataVersion;
/// <summary>
/// The Unity version this application was compiled with.
/// </summary>
public UnityVersion UnityVersion => Metadata.UnityVersion;
/// <summary>
/// The LibCpp2IlContext instance which this ApplicationAnalysisContext belongs to, containing the binary and metadata files that this application was loaded from.
/// </summary>
public LibCpp2IlContext LibCpp2IlContext;
/// <summary>
/// The instruction set helper class associated with the instruction set that this application was compiled with.
/// </summary>
public Cpp2IlInstructionSet InstructionSet;
/// <summary>
/// Contains references to some commonly-used System types.
/// </summary>
public SystemTypesContext SystemTypes;
/// <summary>
/// All the managed assemblies contained within the metadata file.
/// </summary>
public readonly List<AssemblyAnalysisContext> Assemblies = [];
/// <summary>
/// A dictionary of all the managed assemblies, by their name.
/// </summary>
public readonly Dictionary<string, AssemblyAnalysisContext> AssembliesByName = new();
/// <summary>
/// A dictionary of method pointers to the corresponding method, which may or may not be generic.
/// </summary>
public readonly Dictionary<ulong, List<MethodAnalysisContext>> MethodsByAddress = new();
/// <summary>
/// A dictionary of all the generic method variants to their corresponding analysis contexts.
/// </summary>
public readonly Dictionary<Cpp2IlMethodRef, ConcreteGenericMethodAnalysisContext> ConcreteGenericMethodsByRef = new();
/// <summary>
/// Key Function Addresses for the binary file. Populated on-demand
/// </summary>
private BaseKeyFunctionAddresses? _keyFunctionAddresses;
/// <summary>
/// True if this ApplicationAnalysisContext has finished initialization of all of its child contexts, else false.
/// </summary>
public bool HasFinishedInitializing { get; private set; }
private readonly Dictionary<Il2CppImageDefinition, AssemblyAnalysisContext> AssembliesByImageDefinition = new();
public ApplicationAnalysisContext(LibCpp2IlContext context)
{
LibCpp2IlContext = context;
try
{
InstructionSet = InstructionSetRegistry.GetInstructionSet(context.Binary.InstructionSetId);
}
catch (Exception)
{
throw new InstructionSetHandlerNotRegisteredException(context.Binary.InstructionSetId);
}
Logger.VerboseNewline("\tUsing instruction set handler: " + InstructionSet.GetType().FullName);
foreach (var assemblyDefinition in Metadata.AssemblyDefinitions)
{
Logger.VerboseNewline($"\tProcessing assembly: {assemblyDefinition.AssemblyName.Name}...");
var aac = new AssemblyAnalysisContext(assemblyDefinition, this);
Assemblies.Add(aac);
AssembliesByName[assemblyDefinition.AssemblyName.Name] = aac;
AssembliesByImageDefinition[assemblyDefinition.Image] = aac;
}
SystemTypes = new(this);
MiscUtils.InitFunctionStarts(this);
PopulateMethodsByAddressTable();
HasFinishedInitializing = true;
}
/// <summary>
/// Populates the <see cref="MethodsByAddress"/> dictionary with all the methods in the application, including concrete generic ones.
/// </summary>
private void PopulateMethodsByAddressTable()
{
Assemblies.SelectMany(a => a.Types).SelectMany(t => t.Methods).ToList().ForEach(m =>
{
m.EnsureRawBytes();
var ptr = InstructionSet.GetPointerForMethod(m);
if (!MethodsByAddress.ContainsKey(ptr))
MethodsByAddress.Add(ptr, []);
MethodsByAddress[ptr].Add(m);
});
Logger.VerboseNewline("\tProcessing concrete generic methods...");
foreach (var methodRef in Binary.ConcreteGenericMethods.Values.SelectMany(v => v))
{
#if !DEBUG
try
{
#endif
var gm = new ConcreteGenericMethodAnalysisContext(methodRef, this);
var ptr = InstructionSet.GetPointerForMethod(gm);
if (!MethodsByAddress.ContainsKey(ptr))
MethodsByAddress[ptr] = [];
MethodsByAddress[ptr].Add(gm);
ConcreteGenericMethodsByRef[methodRef] = gm;
#if !DEBUG
}
catch (Exception e)
{
throw new("Failed to process concrete generic method: " + methodRef, e);
}
#endif
}
}
/// <summary>
/// Finds an assembly by its name and returns the analysis context for it.
/// </summary>
/// <param name="name">The name of the assembly (without any extension)</param>
/// <returns>An assembly analysis context if one can be found which matches the given name, else null.</returns>
public AssemblyAnalysisContext? GetAssemblyByName(string name)
{
if (name.Length >= 4 && name[^4] == '.' && name[^3] == 'd')
//Trim .dll extension
name = name[..^4];
else if (name.Length >= 6 && name[^6] == '.' && name[^5] == 'w')
//Trim .winmd extension
name = name[..^6];
return AssembliesByName[name];
}
public AssemblyAnalysisContext? ResolveContextForAssembly(Il2CppImageDefinition? imageDefinition)
{
return imageDefinition is not null
? AssembliesByImageDefinition[imageDefinition]
: null;
}
public AssemblyAnalysisContext? ResolveContextForAssembly(Il2CppAssemblyDefinition? assemblyDefinition)
{
return ResolveContextForAssembly(assemblyDefinition?.Image);
}
public TypeAnalysisContext? ResolveContextForType(Il2CppTypeDefinition? typeDefinition)
{
return typeDefinition is not null
? AssembliesByImageDefinition[typeDefinition.DeclaringAssembly!].GetTypeByDefinition(typeDefinition)
: null;
}
public MethodAnalysisContext? ResolveContextForMethod(Il2CppMethodDefinition? methodDefinition)
{
return ResolveContextForType(methodDefinition?.DeclaringType)?.Methods.FirstOrDefault(m => m.Definition == methodDefinition);
}
[return: NotNullIfNotNull(nameof(methodReference))]
public ConcreteGenericMethodAnalysisContext? ResolveContextForMethod(Cpp2IlMethodRef? methodReference)
{
if(methodReference == null)
return null;
return ConcreteGenericMethodsByRef.TryGetValue(methodReference, out var context) ? context : new(methodReference, this);
}
[return: NotNullIfNotNull(nameof(methodReference))]
public MethodAnalysisContext? ResolveContextForMethod(MetadataUsage? methodReference)
{
return methodReference?.Type switch
{
MetadataUsageType.MethodDef => ResolveContextForMethod(methodReference.AsMethod()),
MetadataUsageType.MethodRef => ResolveContextForMethod(methodReference.AsGenericMethodRef()),
_ => null,
};
}
public FieldAnalysisContext? ResolveContextForField(Il2CppFieldDefinition? field)
{
return ResolveContextForType(field?.DeclaringType)?.Fields.FirstOrDefault(f => f.BackingData?.Field == field);
}
public EventAnalysisContext? ResolveContextForEvent(Il2CppEventDefinition? eventDefinition)
{
return ResolveContextForType(eventDefinition?.DeclaringType)?.Events.FirstOrDefault(e => e.Definition == eventDefinition);
}
public PropertyAnalysisContext? ResolveContextForProperty(Il2CppPropertyDefinition? propertyDefinition)
{
return ResolveContextForType(propertyDefinition?.DeclaringType)?.Properties.FirstOrDefault(p => p.Definition == propertyDefinition);
}
public GenericParameterTypeAnalysisContext? ResolveContextForGenericParameter(Il2CppGenericParameter? genericParameter)
{
if (genericParameter is null)
return null;
if (genericParameter.Owner.TypeOwner is { } typeOwner)
{
return ResolveContextForType(typeOwner)?.GenericParameters[genericParameter.genericParameterIndexInOwner];
}
else
{
Debug.Assert(genericParameter.Owner.MethodOwner is not null);
return ResolveContextForMethod(genericParameter.Owner.MethodOwner)?.GenericParameters[genericParameter.genericParameterIndexInOwner];
}
}
public BaseKeyFunctionAddresses GetOrCreateKeyFunctionAddresses()
{
lock (InstructionSet)
{
if (_keyFunctionAddresses == null)
(_keyFunctionAddresses = InstructionSet.CreateKeyFunctionAddressesInstance()).Find(this);
return _keyFunctionAddresses;
}
}
public MultiAssemblyInjectedType InjectTypeIntoAllAssemblies(string ns, string name, TypeAnalysisContext? baseType, TypeAttributes typeAttributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed)
{
var types = Assemblies.Select(a => (InjectedTypeAnalysisContext)a.InjectType(ns, name, baseType, typeAttributes)).ToArray();
return new(types);
}
public InjectedAssemblyAnalysisContext InjectAssembly(
string name,
Version? version = null,
uint hashAlgorithm = 0,
uint flags = 0,
string? culture = null,
byte[]? publicKeyToken = null,
byte[]? publicKey = null)
{
var assembly = new InjectedAssemblyAnalysisContext(name, this, version, hashAlgorithm, flags, culture, publicKeyToken, publicKey);
Assemblies.Add(assembly);
AssembliesByName.Add(name, assembly);
return assembly;
}
public IEnumerable<TypeAnalysisContext> AllTypes => Assemblies.SelectMany(a => a.Types);
}