Skip to content

Commit

Permalink
Merge pull request #53 from js6pak/fix-x32-xref-sign-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak authored Oct 21, 2022
2 parents 880b9d5 + 0cb4e9e commit bd0e66f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
10 changes: 5 additions & 5 deletions Il2CppInterop.Common/XrefScans/XrefInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ namespace Il2CppInterop.Common.XrefScans;
public readonly struct XrefInstance
{
public readonly XrefType Type;
public readonly IntPtr Pointer;
public readonly IntPtr FoundAt;
public readonly nint Pointer;
public readonly nint FoundAt;

public XrefInstance(XrefType type, IntPtr pointer, IntPtr foundAt)
public XrefInstance(XrefType type, nint pointer, nint foundAt)
{
Type = type;
Pointer = pointer;
FoundAt = foundAt;
}

internal XrefInstance RelativeToBase(long baseAddress)
internal XrefInstance RelativeToBase(nint baseAddress)
{
return new XrefInstance(Type, (IntPtr)((long)Pointer - baseAddress), (IntPtr)((long)FoundAt - baseAddress));
return new XrefInstance(Type, Pointer - baseAddress, FoundAt - baseAddress);
}
}
2 changes: 1 addition & 1 deletion Il2CppInterop.Common/XrefScans/XrefScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static IEnumerable<XrefInstance> XrefScanImpl(Decoder decoder, bool ski
{
var targetAddress = ExtractTargetAddress(instruction);
if (targetAddress != 0)
yield return new XrefInstance(XrefType.Method, (IntPtr)targetAddress, (IntPtr)instruction.IP);
yield return new XrefInstance(XrefType.Method, (nint)targetAddress, (nint)instruction.IP);
continue;
}

Expand Down
20 changes: 9 additions & 11 deletions Il2CppInterop.Generator/Passes/Pass16ScanMethodRefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public static void DoPass(RewriteGlobalContext context, GeneratorOptions options
MemoryMappedFileAccess.Read);
using var accessor = mappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);

IntPtr gameAssemblyPtr;
nint gameAssemblyPtr;

unsafe
{
byte* fileStartPtr = null;
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref fileStartPtr);
gameAssemblyPtr = (IntPtr)fileStartPtr;
gameAssemblyPtr = (nint)fileStartPtr;
}

context.HasGcWbarrierFieldWrite =
Expand All @@ -45,7 +45,7 @@ public static void DoPass(RewriteGlobalContext context, GeneratorOptions options
if (!Pass15GenerateMemberContexts.HasObfuscatedMethods) return;

var methodToCallersMap = new ConcurrentDictionary<long, List<XrefInstance>>();
var methodToCalleesMap = new ConcurrentDictionary<long, List<long>>();
var methodToCalleesMap = new ConcurrentDictionary<long, List<nint>>();

context.MethodStartAddresses.Sort();

Expand All @@ -58,8 +58,7 @@ public static void DoPass(RewriteGlobalContext context, GeneratorOptions options

if (!options.NoXrefCache)
{
var pair = XrefScanMetadataGenerationUtil.FindMetadataInitForMethod(originalTypeMethod,
(long)gameAssemblyPtr);
var pair = XrefScanMetadataGenerationUtil.FindMetadataInitForMethod(originalTypeMethod, gameAssemblyPtr);
originalTypeMethod.MetadataInitFlagRva = pair.FlagRva;
originalTypeMethod.MetadataInitTokenRva = pair.TokenRva;
}
Expand All @@ -73,14 +72,13 @@ public static void DoPass(RewriteGlobalContext context, GeneratorOptions options
XrefScanner.DecoderForAddress(IntPtr.Add(gameAssemblyPtr, (int)address), (int)length),
true))
{
var callTarget = callTargetGlobal.RelativeToBase((long)gameAssemblyPtr +
originalTypeMethod.FileOffset - originalTypeMethod.Rva);
var callTarget = callTargetGlobal.RelativeToBase(gameAssemblyPtr + (nint)originalTypeMethod.FileOffset - (nint)originalTypeMethod.Rva);
if (callTarget.Type == XrefType.Method)
{
var targetRelative = (long)callTarget.Pointer;
var targetRelative = callTarget.Pointer;
methodToCallersMap.GetOrAdd(targetRelative, _ => new List<XrefInstance>()).AddLocked(
new XrefInstance(XrefType.Method, (IntPtr)originalTypeMethod.Rva, callTarget.FoundAt));
methodToCalleesMap.GetOrAdd(originalTypeMethod.Rva, _ => new List<long>())
new XrefInstance(XrefType.Method, (nint)originalTypeMethod.Rva, callTarget.FoundAt));
methodToCalleesMap.GetOrAdd(originalTypeMethod.Rva, _ => new List<nint>())
.AddLocked(targetRelative);
}

Expand Down Expand Up @@ -113,7 +111,7 @@ void MarkMethodAlive(long address)
}
}

private static unsafe bool FindByteSequence(IntPtr basePtr, long length, string str)
private static unsafe bool FindByteSequence(nint basePtr, long length, string str)
{
var bytes = (byte*)basePtr;
var sequence = Encoding.UTF8.GetBytes(str);
Expand Down

0 comments on commit bd0e66f

Please sign in to comment.