Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes on x32 when xref pointers are "negative" #53

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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