Skip to content

Commit

Permalink
Cleanup post-29 restorer, performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Oct 27, 2021
1 parent 6d7c048 commit f6f012b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
28 changes: 12 additions & 16 deletions Cpp2IL.Core/AttributeRestorerPost29.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public static List<CustomAttribute> GetCustomAttributesByAttributeIndex(Il2CppIm
if (customAttributeIndex < 0)
return ret; //No attributes

var attributeDataRange = LibCpp2IlMain.TheMetadata!.AttributeDataRanges[customAttributeIndex];
var next = LibCpp2IlMain.TheMetadata!.AttributeDataRanges[customAttributeIndex + 1];
var attributeDataRange = LibCpp2IlMain.TheMetadata.AttributeDataRanges[customAttributeIndex];
// var next = LibCpp2IlMain.TheMetadata.AttributeDataRanges[customAttributeIndex + 1];

var start = LibCpp2IlMain.TheMetadata.metadataHeader.attributeDataOffset + attributeDataRange.startOffset;
var end = LibCpp2IlMain.TheMetadata.metadataHeader.attributeDataOffset + next.startOffset;
// var end = LibCpp2IlMain.TheMetadata.metadataHeader.attributeDataOffset + next.startOffset;

//Now we start actually reading. Start is a pointer to the address in the metadata file where the attribute data is.

Expand Down Expand Up @@ -122,11 +122,11 @@ private static CustomAttribute ReadAndCreateCustomAttribute(ModuleDefinition mod
bytesRead += compressedRead;
pos += compressedRead;

var numFields = LibCpp2IlMain.TheMetadata!.ReadUnityCompressedUIntAtRawAddr(pos, out compressedRead);
var numFields = LibCpp2IlMain.TheMetadata.ReadUnityCompressedUIntAtRawAddr(pos, out compressedRead);
bytesRead += compressedRead;
pos += compressedRead;

var numProps = LibCpp2IlMain.TheMetadata!.ReadUnityCompressedUIntAtRawAddr(pos, out compressedRead);
var numProps = LibCpp2IlMain.TheMetadata.ReadUnityCompressedUIntAtRawAddr(pos, out compressedRead);
bytesRead += compressedRead;
pos += compressedRead;

Expand Down Expand Up @@ -167,7 +167,7 @@ private static CustomAttribute ReadAndCreateCustomAttribute(ModuleDefinition mod
fieldIndex = -(fieldIndex + 1);

var declaringType = LibCpp2IlMain.TheMetadata.typeDefs[typeIndex];
field = declaringType!.Fields![fieldIndex].AsManaged();
field = declaringType.Fields![fieldIndex].AsManaged();
}
else
{
Expand Down Expand Up @@ -204,7 +204,7 @@ private static CustomAttribute ReadAndCreateCustomAttribute(ModuleDefinition mod
propIndex = -(propIndex + 1);

var declaringType = LibCpp2IlMain.TheMetadata.typeDefs[typeIndex];
prop = declaringType!.Properties![propIndex].AsManaged();
prop = declaringType.Properties![propIndex].AsManaged();
}
else
{
Expand All @@ -224,11 +224,11 @@ private static CustomAttribute ReadAndCreateCustomAttribute(ModuleDefinition mod
catch (Exception e)
{
Logger.WarnNewline($"Failed to parse custom attribute {constructor.DeclaringType!.FullName} due to an exception: {e.GetType()}: {e.Message}");
return MakeFallbackAttribute(module, constructor.AsManaged());
return MakeFallbackAttribute(module, constructor.AsManaged()) ?? throw new("Failed to resolve AttributeAttribute type");
}
}

private static CustomAttribute MakeFallbackAttribute(ModuleDefinition module, MethodDefinition constructor)
private static CustomAttribute? MakeFallbackAttribute(ModuleDefinition module, MethodDefinition constructor)
{
var attributeType = module.Types.SingleOrDefault(t => t.Namespace == AssemblyPopulator.InjectedNamespaceName && t.Name == "AttributeAttribute");

Expand Down Expand Up @@ -397,20 +397,18 @@ private static CustomAttribute MakeFallbackAttribute(ModuleDefinition module, Me
case Il2CppTypeEnum.IL2CPP_TYPE_OBJECT:
case Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST:
//LibIl2Cpp is slightly unclear here. It checks that they're null, but also doesn't increment the ptr.
throw new Exception("Object not implemented");
break;
throw new("Object not implemented");
case Il2CppTypeEnum.IL2CPP_TYPE_IL2CPP_TYPE_INDEX:
var typeIndex = md.ReadUnityCompressedIntAtRawAddr(pos, out var compressedBytesRead);
pos += compressedBytesRead;
bytesRead += compressedBytesRead;
if (typeIndex == -1)
ret = null;
else
{
//Weirdly, libil2cpp checks "Deserialize managed object" boolean here
//But as far as I can see, it's actually returning typeof(x)
var il2cppType = LibCpp2IlMain.Binary!.GetType(typeIndex);
ret = Utils.TryResolveTypeReflectionData(LibCpp2ILUtils.GetTypeReflectionData(il2cppType));
var il2CppType = LibCpp2IlMain.Binary!.GetType(typeIndex);
ret = Utils.TryResolveTypeReflectionData(LibCpp2ILUtils.GetTypeReflectionData(il2CppType));

if (ret == null)
throw new($"Failed to resolve type reflection data for type index {typeIndex}");
Expand All @@ -435,8 +433,6 @@ private static Il2CppTypeEnum ReadBlobType(long pos, out int bytesRead, out Type
{
var enumTypeIndex = LibCpp2IlMain.TheMetadata.ReadUnityCompressedIntAtRawAddr(pos, out var compressedBytesRead);
bytesRead += compressedBytesRead;
pos += compressedBytesRead;
var rawType = LibCpp2IlMain.Binary!.GetType(enumTypeIndex);
var typeDef = LibCpp2IlReflection.GetTypeDefinitionByTypeIndex(enumTypeIndex)!;
type = typeDef.AsManaged(); //Get enum type
ret = LibCpp2IlMain.Binary!.GetType(typeDef.elementTypeIndex).type; //Get enum underlying type's type enum
Expand Down
7 changes: 3 additions & 4 deletions Cpp2IL.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=25e25394_002D70a8_002D4b01_002D93c4_002Dbb887c4123e0/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Metadata24_1_64BitSupportIsPresent" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;xUnit::EB3CFC80-2125-48D2-AA2F-548F5AA58342::net5.0::LibCpp2ILTests.Tests&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;

<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=363a8b0b_002Df59e_002D4967_002Dbef1_002Df075c503d63d/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ctors/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=RGCTX/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
8 changes: 6 additions & 2 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime;
using CommandLine;
using Cpp2IL.Core;
using Cpp2IL.Core.Exceptions;
Expand Down Expand Up @@ -244,6 +245,8 @@ public static int MainWithArgs(Cpp2IlRuntimeArgs runtimeArgs)
if (!runtimeArgs.Valid)
throw new SoftException("Arguments have Valid = false");

GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;

ConsoleLogger.ShowVerbose = runtimeArgs.EnableVerboseLogging;

Cpp2IlApi.InitializeLibCpp2Il(runtimeArgs.PathToAssembly, runtimeArgs.PathToMetadata, runtimeArgs.UnityVersion, runtimeArgs.EnableRegistrationPrompts);
Expand All @@ -255,8 +258,9 @@ public static int MainWithArgs(Cpp2IlRuntimeArgs runtimeArgs)

BaseKeyFunctionAddresses? keyFunctionAddresses = null;

//We have to always run key function scan (if we can), so that attribute reconstruction can run.
if (LibCpp2IlMain.Binary?.InstructionSet != InstructionSet.ARM32)
//We need to run key function sweep if we can for attribute restoration
//or if we want to analyze. But we DON'T need it for restoration on v29
if (LibCpp2IlMain.Binary?.InstructionSet != InstructionSet.ARM32 && (LibCpp2IlMain.MetadataVersion < 29 || runtimeArgs.EnableAnalysis))
{
Logger.InfoNewline("Running Scan for Known Functions...");

Expand Down

0 comments on commit f6f012b

Please sign in to comment.