Skip to content

Commit 1e7b56b

Browse files
committed
Guard the page-protection scan workaround to Windows only
Per @ds5678's review on #267: VirtualProtect/VirtualQuery are kernel32 (Windows-only) and PAGE_NOACCESS is a Windows page state. Run the make-readable / restore dance only when OperatingSystem.IsWindows(); on other platforms fall back to the direct FindSignatureInBlock scan (its prior behaviour), so the kernel32 P/Invokes are never called off-Windows. Also satisfies the CA1416 platform-compatibility analyzer.
1 parent a3c8137 commit 1e7b56b

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Il2CppInterop.Runtime/MemoryUtils.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ public static nint FindSignatureInModule(ProcessModule module, SignatureDefiniti
2525
// linear byte walk in FindSignatureInBlock dereferences protected memory and throws AccessViolationException
2626
// (an unrecoverable, process-fatal CSE) before the caller's signature-exhaustion fallback can run. Temporarily
2727
// make every region of the module readable for the duration of the scan, then restore the original protections.
28-
GetModuleRegions(module, out var protectedRegions);
29-
SetModuleRegions(protectedRegions, PAGE_EXECUTE_READWRITE);
28+
// VirtualProtect/VirtualQuery are kernel32 (Windows-only) and PAGE_NOACCESS is a Windows page state, so this
29+
// workaround runs on Windows only; on other platforms fall back to the direct scan (the prior behaviour).
30+
List<MEMORY_BASIC_INFORMATION> protectedRegions = null;
31+
var onWindows = OperatingSystem.IsWindows();
32+
if (onWindows)
33+
{
34+
GetModuleRegions(module, out protectedRegions);
35+
SetModuleRegions(protectedRegions, PAGE_EXECUTE_READWRITE);
36+
}
3037
nint ptr;
3138
try
3239
{
@@ -40,7 +47,8 @@ public static nint FindSignatureInModule(ProcessModule module, SignatureDefiniti
4047
}
4148
finally
4249
{
43-
SetModuleRegions(protectedRegions);
50+
if (onWindows)
51+
SetModuleRegions(protectedRegions);
4452
}
4553

4654
if (ptr != 0 && sigDef.xref)

0 commit comments

Comments
 (0)