Skip to content

Commit

Permalink
Fix array allocations in attributes having nulls, causing NREs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Sep 29, 2021
1 parent 5f41f28 commit 1b834be
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Il2CppStringToConstantAction(MethodAnalysis<Instruction> context, Instruc
_destReg = Utils.GetRegisterNameNew(instruction.Op0Register);
}

_constantMade = context.MakeConstant(typeof(Il2CppString), new Il2CppString(_detectedString, instruction.MemoryDisplacement64), reg: _destReg);
_constantMade = context.MakeConstant(typeof(Il2CppString), new Il2CppString(_detectedString, instruction.Op0Kind.IsImmediate() ? instruction.Immediate32 : instruction.MemoryDisplacement64), reg: _destReg);

if (instruction.Mnemonic == Mnemonic.Push)
{
Expand Down
9 changes: 8 additions & 1 deletion Cpp2IL.Core/Analysis/ResultModels/Il2CppString.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Cpp2IL.Core.Analysis.ResultModels
using System;
using LibCpp2IL;

namespace Cpp2IL.Core.Analysis.ResultModels
{
public class Il2CppString
{
Expand All @@ -8,6 +11,10 @@ public class Il2CppString
public Il2CppString(string containedString, ulong addr)
{
ContainedString = containedString;

if (!LibCpp2IlMain.Binary!.TryMapVirtualAddressToRaw(addr, out _))
throw new Exception($"Invalid il2cpp string creation - 0x{addr:X} cannot be mapped to the binary.");

Address = addr;
}

Expand Down
4 changes: 4 additions & 0 deletions Cpp2IL.Core/AttributeRestorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Cpp2IL.Core.Analysis;
using Cpp2IL.Core.Analysis.Actions.Base;
Expand Down Expand Up @@ -487,6 +488,9 @@ private static object AllocateArray(AllocatedArray array)
var arrayType = Type.GetType(typeForArrayToCreateNow.FullName) ?? throw new Exception($"Could not resolve array type {array.ArrayType.ElementType.FullName}");
var arr = Array.CreateInstance(arrayType, array.Size);

if (array.KnownValuesAtOffsets.Count != array.Size)
throw new Exception($"Failed to populate known array - only have {array.KnownValuesAtOffsets.Count} known values for an array of length {array.Size}.");

foreach (var (index, value) in array.KnownValuesAtOffsets)
{
try
Expand Down
4 changes: 4 additions & 0 deletions Cpp2IL.Core/Cpp2IlApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,18 @@ public static void SaveAssemblies(string toWhere, List<AssemblyDefinition> assem
if (reference != null)
assembly.MainModule.AssemblyReferences.Remove(reference);

#if !DEBUG
try
{
#endif
assembly.Write(dllPath);
#if !DEBUG
}
catch (Exception e)
{
throw new DllSaveException(dllPath, e);
}
#endif
}
}

Expand Down
8 changes: 6 additions & 2 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static void ResolvePathsFromCommandLine(string gamePath, string? inputEx
//APK
//Metadata: assets/bin/Data/Managed/Metadata
//Binary: lib/(armeabi-v7a)|(arm64-v8a)/libil2cpp.so

Logger.InfoNewline($"Attempting to extract required files from APK {gamePath}");

using var stream = File.OpenRead(gamePath);
Expand Down Expand Up @@ -100,7 +100,7 @@ private static void ResolvePathsFromCommandLine(string gamePath, string? inputEx
ggmStream.Read(ggmBytes, 0, 0x40);

args.UnityVersion = Cpp2IlApi.GetVersionFromGlobalGameManagers(ggmBytes);

Logger.InfoNewline($"Determined game's unity version to be {string.Join(".", args.UnityVersion)}");

args.Valid = true;
Expand Down Expand Up @@ -176,11 +176,14 @@ public static int Main(string[] args)

Logger.InfoNewline("Running on " + Environment.OSVersion.Platform);

#if !DEBUG
try
{
#endif
var runtimeArgs = GetRuntimeOptionsFromCommandLine(args);

return MainWithArgs(runtimeArgs);
#if !DEBUG
}
catch (DllSaveException e)
{
Expand All @@ -203,6 +206,7 @@ public static int Main(string[] args)
Logger.ErrorNewline($"Execution Failed: {e.Message}");
return -1;
}
#endif
}

public static int MainWithArgs(Cpp2IlRuntimeArgs runtimeArgs)
Expand Down

0 comments on commit 1b834be

Please sign in to comment.