Skip to content

Commit b503474

Browse files
committed
Core: Fix exception when calling ToString on a param during initialization
1 parent b98e1ea commit b503474

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

Cpp2IL.Core/Model/Contexts/ApplicationAnalysisContext.cs

+18-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ApplicationAnalysisContext : ContextWithDataStorage
1919
/// The IL2CPP binary file this application was loaded from
2020
/// </summary>
2121
public Il2CppBinary Binary;
22-
22+
2323
/// <summary>
2424
/// The IL2CPP global-metadata file this application was loaded from.
2525
/// </summary>
@@ -34,12 +34,12 @@ public class ApplicationAnalysisContext : ContextWithDataStorage
3434
/// The instruction set helper class associated with the instruction set that this application was compiled with.
3535
/// </summary>
3636
public Cpp2IlInstructionSet InstructionSet;
37-
37+
3838
/// <summary>
3939
/// Contains references to some commonly-used System types.
4040
/// </summary>
4141
public SystemTypesContext SystemTypes;
42-
42+
4343
/// <summary>
4444
/// All the managed assemblies contained within the metadata file.
4545
/// </summary>
@@ -54,17 +54,22 @@ public class ApplicationAnalysisContext : ContextWithDataStorage
5454
/// A dictionary of method pointers to the corresponding method, which may or may not be generic.
5555
/// </summary>
5656
public readonly Dictionary<ulong, List<MethodAnalysisContext>> MethodsByAddress = new();
57-
57+
5858
/// <summary>
5959
/// A dictionary of all the generic method variants to their corresponding analysis contexts.
6060
/// </summary>
6161
public readonly Dictionary<Cpp2IlMethodRef, ConcreteGenericMethodAnalysisContext> ConcreteGenericMethodsByRef = new();
6262

63-
/// <summary>
63+
/// <summary>
6464
/// Key Function Addresses for the binary file. Populated on-demand
6565
/// </summary>
6666
private BaseKeyFunctionAddresses? _keyFunctionAddresses;
6767

68+
/// <summary>
69+
/// True if this ApplicationAnalysisContext has finished initialization of all of its child contexts, else false.
70+
/// </summary>
71+
public bool HasFinishedInitializing { get; private set; }
72+
6873
public ApplicationAnalysisContext(Il2CppBinary binary, Il2CppMetadata metadata, float metadataVersion)
6974
{
7075
Binary = binary;
@@ -79,7 +84,7 @@ public ApplicationAnalysisContext(Il2CppBinary binary, Il2CppMetadata metadata,
7984
{
8085
throw new InstructionSetHandlerNotRegisteredException(binary.InstructionSetId);
8186
}
82-
87+
8388
Logger.VerboseNewline("\tUsing instruction set handler: " + InstructionSet.GetType().FullName);
8489

8590
foreach (var assemblyDefinition in Metadata.AssemblyDefinitions)
@@ -91,8 +96,10 @@ public ApplicationAnalysisContext(Il2CppBinary binary, Il2CppMetadata metadata,
9196
}
9297

9398
SystemTypes = new(this);
94-
99+
95100
PopulateMethodsByAddressTable();
101+
102+
HasFinishedInitializing = true;
96103
}
97104

98105
/// <summary>
@@ -147,19 +154,19 @@ private void PopulateMethodsByAddressTable()
147154
}
148155

149156
public TypeAnalysisContext? ResolveContextForType(Il2CppTypeDefinition typeDefinition) => GetAssemblyByName(typeDefinition.DeclaringAssembly!.Name!)?.TypesByDefinition[typeDefinition];
150-
157+
151158
public BaseKeyFunctionAddresses GetOrCreateKeyFunctionAddresses()
152159
{
153-
if (_keyFunctionAddresses == null)
160+
if (_keyFunctionAddresses == null)
154161
(_keyFunctionAddresses = InstructionSet.CreateKeyFunctionAddressesInstance()).Find(this);
155162

156163
return _keyFunctionAddresses;
157164
}
158165

159166
public MultiAssemblyInjectedType InjectTypeIntoAllAssemblies(string ns, string name, TypeAnalysisContext baseType)
160167
{
161-
var types = Assemblies.Select(a => (InjectedTypeAnalysisContext) a.InjectType(ns, name, baseType)).ToArray();
162-
168+
var types = Assemblies.Select(a => (InjectedTypeAnalysisContext)a.InjectType(ns, name, baseType)).ToArray();
169+
163170
return new(types);
164171
}
165172

Cpp2IL.Core/Model/Contexts/ParameterAnalysisContext.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public ParameterAnalysisContext(Il2CppParameterDefinition? definition, int param
8181

8282
public override string ToString()
8383
{
84+
if (!AppContext.HasFinishedInitializing)
85+
//Cannot safely access ParameterTypeContext.Name if we haven't finished initializing as it may require doing system type lookups etc.
86+
return $"Parameter {Name} (ordinal {ParamIndex}) of {DeclaringMethod}";
87+
8488
var result = new StringBuilder();
8589

8690
if (ParameterAttributes.HasFlag(ParameterAttributes.Out))
@@ -113,4 +117,4 @@ public ITypeInfoProvider ParameterTypeInfoProvider
113117
public string ParameterName => Name;
114118

115119
#endregion
116-
}
120+
}

0 commit comments

Comments
 (0)