Skip to content

Commit

Permalink
Core: Fix v29 CA blob object array parsing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Oct 30, 2023
1 parent 563a91c commit 20ccab2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions Cpp2IL.Core/AttributeRestorerPost29.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ private static CustomAttribute ReadAndCreateCustomAttribute(ModuleDefinition mod
{
bytesRead = 0;
var managedConstructor = constructor.AsManaged();

#if !DEBUG
try
#endif
{
var ret = new CustomAttribute(module.ImportReference(managedConstructor));

Expand Down Expand Up @@ -243,15 +246,13 @@ private static CustomAttribute ReadAndCreateCustomAttribute(ModuleDefinition mod

return ret;
}
#if !DEBUG
catch (Exception e)
{
#if DEBUG
throw;
#else
Logger.WarnNewline($"Failed to parse custom attribute {constructor.DeclaringType!.FullName} due to an exception: {e.GetType()}: {e.Message}");
return MakeFallbackAttribute(module, managedConstructor) ?? throw new("Failed to resolve AttributeAttribute type");
#endif
}
#endif
}

private static object? WrapArrayValuesInCustomAttributeArguments(TypeReference typeReference, Array arr)
Expand All @@ -271,12 +272,20 @@ private static CustomAttribute ReadAndCreateCustomAttribute(ModuleDefinition mod

var objectTypeName = o?.GetType()?.FullName ?? throw new NullReferenceException($"Object {o} in array {arr} of variable type and length {arr.Length} is null, so cannot determine its type");

var typeDef = MiscUtils.TryLookupTypeDefKnownNotGeneric(objectTypeName);
TypeDefinition typeDef;
if(o is TypeDefinition)
//Special case TypeDefinition => Type
typeDef = TypeDefinitions.Type;
else
typeDef = MiscUtils.TryLookupTypeDefKnownNotGeneric(objectTypeName);

if (typeDef == null)
throw new Exception($"Couldn't resolve type {objectTypeName}");

list.Add(new CustomAttributeArgument(typeDef, o));
var typedParam = new CustomAttributeArgument(typeDef, o);

//Needs to be wrapped in an additional argument with type System.Object, because cecil
list.Add(new CustomAttributeArgument(TypeDefinitions.Object, typedParam));
}

return list.ToArray();
Expand Down Expand Up @@ -541,6 +550,7 @@ private static Il2CppTypeEnum ReadBlobType(long pos, out int bytesRead, out Type
Il2CppTypeEnum.IL2CPP_TYPE_STRING => TypeDefinitions.String.AsUnmanaged(),
Il2CppTypeEnum.IL2CPP_TYPE_BYREF => TypeDefinitions.TypedReference.AsUnmanaged(),
Il2CppTypeEnum.IL2CPP_TYPE_IL2CPP_TYPE_INDEX => TypeDefinitions.Type.AsUnmanaged(),
Il2CppTypeEnum.IL2CPP_TYPE_OBJECT => TypeDefinitions.Object.AsUnmanaged(),
_ => throw new ArgumentOutOfRangeException(nameof(typeEnum), typeEnum, null)
};
}
Expand Down

0 comments on commit 20ccab2

Please sign in to comment.