IL2CPP: preloader dies on Apple Silicon in ConsoleSetOutFix with an ILHook NullReferenceException
What happened?
On Apple Silicon, BepInEx 6 IL2CPP dies in the preloader before it writes a log. ConsoleSetOutFix.Apply() throws HarmonyException: IL Compile Error, wrapping a NullReferenceException from MonoMod.RuntimeDetour.ILHook..ctor. The same build under Rosetta gets all the way to Runtime invoke patched.
This is the CoreCLR twin of #1303, which #1402 fixed for v5 on Mono. There, DetourHelper.Runtime stayed null because the detour platform constructor threw once and get_Runtime caches that result. The stack here is the same shape, so I suspect the same root cause with a different detour platform underneath.
I have not diagnosed which platform MonoMod picks on arm64 CoreCLR, or why its constructor fails — only that the failure is reproducible and architecture-dependent on identical managed assemblies.
Filing rather than fixing because the v5 fix hinged on a specific mprotect/MAP_JIT interaction that I measured before patching, and I have not done that work for CoreCLR yet.
Steps to reproduce
- Kingdom Two Crowns 2.4.0, a universal macOS binary, Unity 6000.0.61f1, IL2CPP metadata v31.
- Install BepInEx 6 IL2CPP with #1407 applied, an arm64
libdobby.dylib(BepInEx/Dobby#16) and an arm64 .NET 6.0.7 runtime indotnet/. - Launch through
run_bepinex.sh, which setsARCHPREFERENCE="arm64,x86_64"so the universal binary runs native. - No
LogOutput.logappears. Apreloader_<date>.logis written beside the executable. - Relaunch with
ARCHPREFERENCE="x86_64,arm64". Everything below runs fine.
Without #1407 this is unreachable: Paths.SetExecutablePath throws first, on both architectures.
BepInEx Distribution
Bleeding Edge from BepisBuilds
Log outputs
HarmonyLib.HarmonyException: IL Compile Error (unknown location)
---> System.NullReferenceException: Object reference not set to an instance of an object.
at MonoMod.RuntimeDetour.ILHook..ctor(MethodBase from, Manipulator manipulator, ILHookConfig& config)
at MonoMod.RuntimeDetour.ILHook..ctor(MethodBase from, Manipulator manipulator, ILHookConfig config)
at HarmonyLib.Public.Patching.ManagedMethodPatcher.DetourTo(MethodBase replacement)
at HarmonyLib.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo)
--- End of inner exception stack trace ---
at HarmonyLib.PatchClassProcessor.ReportException(Exception exception, MethodBase original)
at HarmonyLib.PatchClassProcessor.Patch()
at HarmonyLib.Harmony.PatchAll(Type type)
at HarmonyLib.Harmony.CreateAndPatchAll(Type type, String harmonyInstanceId)
at BepInEx.Preloader.RuntimeFixes.ConsoleSetOutFix.Apply() in BepInEx.Preloader.Core/RuntimeFixes/ConsoleSetOutFix.cs:line 18
at BepInEx.Unity.IL2CPP.Preloader.Run() in Runtimes/Unity/BepInEx.Unity.IL2CPP/Preloader.cs:line 28
at BepInEx.Unity.IL2CPP.UnityPreloaderRunner.PreloaderMain() in Runtimes/Unity/BepInEx.Unity.IL2CPP/UnityPreloadRunner.cs:line 27Same build, same game, ARCHPREFERENCE="x86_64,arm64":
[Message: BepInEx] Chainloader initialized
[Debug : Preloader] Runtime invoke pointer: 0x132569130
[Debug : Preloader] Runtime invoke patchedEnvironment
- OS: macOS 15 (kernel 26.6.2), Apple Silicon
- Game: Kingdom Two Crowns 2.4.0, universal binary (x86_64 + arm64)
- Unity: 6000.0.61f1, IL2CPP, metadata v31
- BepInEx: 6.0.0-be.788+5b766a3, plus #1407
- Runtime: .NET 6.0.7, osx-arm64
- Doorstop: 4.5.0 universal
- Dobby: arm64, built per BepInEx/Dobby#16Related
- #1303 and #1402 — the same failure shape on v5 / Mono, already fixed
- #899 — native Apple Silicon support, the umbrella issue
- #1407 — the macOS path fixes needed to reach this code at all
- #1403 — a separate arm64 defect,
FollowExportThunksdecoding x86 on arm64 - BepInEx/Dobby#16 — the arm64
libdobby.dylibthis needs
Source: BepInEx/BepInEx