Skip to content

Commit ce69b03

Browse files
committed
Use TerraFX MEM/PAGE constants instead of hand-rolled hex
Drop the manual MEM_COMMIT/PAGE_GUARD/PAGE_READABLE consts and the `using static Windows` in favour of the constants TerraFX already exposes (MEM.*, PAGE.*), qualify VirtualQuery, and default-init the MEMORY_BASIC_INFORMATION. The readable mask moves inside the windows6.1 guard so the const fold stays on a supported-platform path.
1 parent 5682fe6 commit ce69b03

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

Il2CppInterop.Runtime/MemoryUtils.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,11 @@
55
using System.Runtime.Versioning;
66
using Il2CppInterop.Common.XrefScans;
77
using TerraFX.Interop.Windows;
8-
using static TerraFX.Interop.Windows.Windows;
98

109
namespace Il2CppInterop.Runtime;
1110

1211
internal class MemoryUtils
1312
{
14-
private const uint MEM_COMMIT = 0x1000;
15-
private const uint PAGE_GUARD = 0x100;
16-
17-
// Protections that permit reading: READONLY|READWRITE|WRITECOPY|EXECUTE_READ|EXECUTE_READWRITE|EXECUTE_WRITECOPY.
18-
private const uint PAGE_READABLE = 0x02 | 0x04 | 0x08 | 0x20 | 0x40 | 0x80;
19-
2013
public static unsafe nint FindSignatureInModule(ProcessModule module, SignatureDefinition sigDef)
2114
{
2215
// On newer Unity (6000.x) the loaded GameAssembly maps some pages PAGE_NOACCESS / guard pages; the raw
@@ -28,11 +21,14 @@ public static unsafe nint FindSignatureInModule(ProcessModule module, SignatureD
2821
nint ptr = 0;
2922
if (OperatingSystem.IsWindowsVersionAtLeast(6, 1))
3023
{
24+
// Protections that permit reading; TerraFX defines no single composite of these.
25+
const uint pageReadable = PAGE.PAGE_READONLY | PAGE.PAGE_READWRITE | PAGE.PAGE_WRITECOPY |
26+
PAGE.PAGE_EXECUTE_READ | PAGE.PAGE_EXECUTE_READWRITE | PAGE.PAGE_EXECUTE_WRITECOPY;
3127
var regions = GetModuleRegions(module);
3228
foreach (var region in regions)
3329
{
34-
if (region.State != MEM_COMMIT || (region.Protect & PAGE_GUARD) != 0 ||
35-
(region.Protect & PAGE_READABLE) == 0)
30+
if (region.State != MEM.MEM_COMMIT || (region.Protect & PAGE.PAGE_GUARD) != 0 ||
31+
(region.Protect & pageReadable) == 0)
3632
continue;
3733
ptr = FindSignatureInBlock((nint)region.BaseAddress, (long)region.RegionSize,
3834
sigDef.pattern, sigDef.mask, sigDef.offset);
@@ -92,8 +88,8 @@ internal static unsafe List<MEMORY_BASIC_INFORMATION> GetModuleRegions(ProcessMo
9288
var currentAddress = (long)module.BaseAddress;
9389
while (currentAddress < moduleEndAddress)
9490
{
95-
MEMORY_BASIC_INFORMATION memoryInfo;
96-
var result = VirtualQuery((void*)currentAddress, &memoryInfo, (nuint)sizeof(MEMORY_BASIC_INFORMATION));
91+
MEMORY_BASIC_INFORMATION memoryInfo = default;
92+
var result = Windows.VirtualQuery((void*)currentAddress, &memoryInfo, (nuint)sizeof(MEMORY_BASIC_INFORMATION));
9793
if (result == 0)
9894
break; // error, or reached the end of the module's mapped memory
9995

0 commit comments

Comments
 (0)