Skip to content

Commit 2841902

Browse files
committed
Core: DummyDll: Fix API consumers getting bad value types...
If types were defined out-of-order in the metadata, and anyone tried to use the generated assembly refs without saving the assemblies to disk first, some value types could be referenced not as value types, which was incorrect. Ensure we configure the hierarchy for VTs and Enums early to prevent this.
1 parent 0e07c8a commit 2841902

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

Cpp2IL.Core/CorePlugin/AsmResolverDummyDllOutputFormat.cs

+19-3
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ public List<AssemblyDefinition> BuildAssemblies(ApplicationAnalysisContext conte
7979
List<AssemblyDefinition> ret = BuildStubAssemblies(context);
8080
Logger.VerboseNewline($"{(DateTime.Now - start).TotalMilliseconds:F1}ms", "DummyDllOutput");
8181

82-
TypeDefinitionsAsmResolver.CacheNeededTypeDefinitions();
83-
8482
start = DateTime.Now;
8583
Logger.Verbose("Configuring inheritance and generics...", "DummyDllOutput");
8684

@@ -173,6 +171,24 @@ private static AssemblyDefinition BuildStubAssembly(AssemblyAnalysisContext asse
173171
managedModule.TopLevelTypes.Add(BuildStubType(il2CppTypeDefinition));
174172
}
175173

174+
if (corLib == null)
175+
{
176+
//We *are* the corlib, so cache defs now
177+
TypeDefinitionsAsmResolver.CacheNeededTypeDefinitions();
178+
}
179+
180+
//We can get issues with consumers of the API if the base type is not set correctly for value types or enums, so we set it here (as early as possible) if we can
181+
foreach (var assemblyContextType in assemblyContext.Types)
182+
{
183+
if(assemblyContextType.Definition is not {} def || assemblyContextType.GetExtraData<TypeDefinition>("AsmResolverType") is not {} asmResolverType)
184+
continue;
185+
186+
if(def.IsValueType)
187+
asmResolverType.BaseType = managedModule.DefaultImporter.ImportType(TypeDefinitionsAsmResolver.ValueType);
188+
else if(def.IsEnumType)
189+
asmResolverType.BaseType = managedModule.DefaultImporter.ImportType(TypeDefinitionsAsmResolver.Enum);
190+
}
191+
176192
//Store the managed assembly in the context so we can use it later.
177193
assemblyContext.PutExtraData("AsmResolverAssembly", ourAssembly);
178194

@@ -227,4 +243,4 @@ private static void ConfigureTypeSize(Il2CppTypeDefinition il2CppDefinition, Typ
227243
if (packingSize != 0 || classSize != 0)
228244
asmResolverDefinition.ClassLayout = new(packingSize, classSize);
229245
}
230-
}
246+
}

Cpp2IL.Core/Utils/AsmResolver/TypeDefinitionsAsmResolver.cs

+32-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static class TypeDefinitionsAsmResolver
2828
public static TypeDefinition Object;
2929
public static TypeDefinition IConvertible;
3030
public static TypeDefinition ValueType;
31+
public static TypeDefinition Enum;
3132
public static TypeDefinition Type;
3233
public static TypeDefinition TypedReference;
3334
public static TypeDefinition String;
@@ -42,6 +43,35 @@ public static class TypeDefinitionsAsmResolver
4243
public static void Reset()
4344
{
4445
_primitiveTypeMappings.Clear();
46+
47+
Boolean = null!;
48+
SByte = null!;
49+
Byte = null!;
50+
Char = null!;
51+
Int16 = null!;
52+
UInt16 = null!;
53+
Int32 = null!;
54+
UInt32 = null!;
55+
Int64 = null!;
56+
UInt64 = null!;
57+
Single = null!;
58+
Double = null!;
59+
IntPtr = null!;
60+
UIntPtr = null!;
61+
62+
Object = null!;
63+
IConvertible = null!;
64+
ValueType = null!;
65+
Enum = null!;
66+
Type = null!;
67+
TypedReference = null!;
68+
String = null!;
69+
Array = null!;
70+
IEnumerable = null!;
71+
Exception = null!;
72+
Void = null!;
73+
Attribute = null!;
74+
MethodInfo = null!;
4575
}
4676

4777
public static TypeDefinition? GetPrimitive(string name)
@@ -56,6 +86,7 @@ public static void CacheNeededTypeDefinitions()
5686
{
5787
Object = AsmResolverUtils.TryLookupTypeDefKnownNotGeneric("System.Object")!;
5888
ValueType = AsmResolverUtils.TryLookupTypeDefKnownNotGeneric("System.ValueType")!;
89+
Enum = AsmResolverUtils.TryLookupTypeDefKnownNotGeneric("System.Enum")!;
5990
String = AsmResolverUtils.TryLookupTypeDefKnownNotGeneric("System.String")!;
6091
Int64 = AsmResolverUtils.TryLookupTypeDefKnownNotGeneric("System.Int64")!;
6192
Single = AsmResolverUtils.TryLookupTypeDefKnownNotGeneric("System.Single")!;
@@ -96,4 +127,4 @@ public static void CacheNeededTypeDefinitions()
96127
};
97128
}
98129
}
99-
}
130+
}

0 commit comments

Comments
 (0)