HetCreep 81a6f78c8b Fix AccessViolationException when scanning Unity 6 modules with protected memory pages (#267)
* Fix AccessViolation scanning Unity 6 modules with protected pages

FindSignatureInModule did a raw linear byte walk over the entire loaded
module. On Unity 6000.x the GameAssembly image maps regions as PAGE_NOACCESS
/ guard pages, so dereferencing them throws a process-fatal
AccessViolationException before the caller's signature-exhaustion fallback
can run (observed registering injected types on Priconne, Unity 6000.0.58f2).

Temporarily flip every committed region of the module to PAGE_EXECUTE_READWRITE
for the duration of the scan via VirtualQuery/VirtualProtect, then restore the
original protections in a finally block. Non-committed (MEM_FREE/MEM_RESERVE)
regions are skipped. Mirrors the approach used by downstream forks that already
support Unity 6.

* 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.

* Scan only readable regions instead of changing page protections

Per @ds5678's review on #267 (relaying Discord feedback): modifying page protections
(VirtualProtect to PAGE_EXECUTE_READWRITE) is dangerous and unnecessary. VirtualQuery
already enumerates the module's regions, so skip the non-readable / guard / uncommitted
ones during the signature scan rather than forcing them readable. Removes VirtualProtect
and the RWX flip entirely; keeps the Windows-only guard (VirtualQuery is kernel32) with
the plain whole-module scan as the off-Windows fallback.

* Bound the signature scan to each region + apply review refinements

The per-region scan could read up to mask.Length-1 bytes past a readable region into the
adjacent guard / PAGE_NOACCESS page -> a fatal AccessViolationException. Cap
FindSignatureInBlock's loop at blockSize - mask.Length so every read stays inside the region.
Per @ds5678's review: GetModuleRegions now returns the list instead of an out parameter, and
its comment is XML documentation.

* Use TerraFX.Interop.Windows for VirtualQuery instead of a manual P/Invoke

Replace the hand-written kernel32 VirtualQuery DllImport and MEMORY_BASIC_INFORMATION struct with the TerraFX.Interop.Windows bindings, pinned to 10.0.22621.2 (the last release targeting .NET 6). The readable-region scan is unchanged.

* Annotate the region scan as Windows-only for CA1416

The TerraFX MEMORY_BASIC_INFORMATION fields are annotated for Windows 6.1+. Mark GetModuleRegions with the matching SupportedOSPlatform attribute and guard the call with OperatingSystem.IsWindowsVersionAtLeast(6, 1) so the platform-compatibility analyzer is satisfied.

* 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.
2026-07-08 00:12:53 -07:00
2022-06-24 16:08:13 +03:00
2022-07-02 17:58:20 +03:00
2022-09-29 16:00:30 -04:00
2021-11-03 22:29:29 +02:00
2021-11-03 22:29:29 +02:00
2021-11-03 22:31:05 +02:00
2026-06-20 21:32:23 +02:00
2024-05-18 20:24:30 +02:00
2022-05-29 01:52:38 +03:00
2026-02-24 22:19:35 +01:00

GitHub Workflow Status GitHub release (latest by date)

CLI Generator Runtime HarmonySupport
NuGet.org (release) BepInEx NuGet (CLI) BepInEx NuGet (Generator) BepInEx NuGet (Runtime) BepInEx NuGet (HarmonySupport )
nuget.bepinex.dev (CI) BepInEx NuGet (CLI) BepInEx NuGet (Generator) BepInEx NuGet (Runtime) BepInEx NuGet (HarmonySupport)

BepInEx fork of Il2CppAssemblyUnhollower is now Il2CppInterop!

Looking for old README and guides? Check out legacy-unhollower branch.

Il2CppInterop is a framework for bridging together Unity's Il2Cpp and .NET's CoreCLR runtimes. The framework various interoperability tools:

  • A tool to generate .NET assemblies from Cpp2IL's output.
  • A runtime library to work with Il2Cpp objects in CoreCLR.
  • Libraries to integrate other tools with Il2Cpp.

The framework allows the use of Il2Cpp domain and objects in it from a managed .NET domain. This includes generic types and methods, arrays, and new object creation.

This project started out as fork of knah/Il2CppAssemblyUnhollower but has been since been modified with new API and fixes to be a standalone project.

Getting started

For plugin developers:

For tool integrators and advanced user:

Used libraries

Bundled into output files:

  • iced by 0xd4d, an x86 disassembler used for xref scanning and possibly more in the future

Used by generator itself:

  • Mono.Cecil by jbevain, the main tool to produce assemblies
S
Description
A tool interoperate between CoreCLR and Il2Cpp at runtime
Readme LGPL-3.0
9.1 MiB
0 Stars 1 Watchers 0 Forks
v1.5.3
Latest
2026-11-12 11:09:44 -10:00
Languages
C# 99.9%