13 Commits
Author SHA1 Message Date
Chris Dobbyn dbc82f05b8 v6: Fix plugin loading on native arm64 macOS (#1405)
* fix(macos): load plugins when the game runs as native arm64

This is the v6 port of #1402, which fixed the same failure on v5-lts.

MonoMod picks DetourNativeMonoPosixPlatform on Mono, whose MakeWritable and
MakeExecutable call mprotect with PROT_READ|PROT_WRITE|PROT_EXEC. macOS on
Apple Silicon answers EACCES, because a process without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement can never
hold a page that is writable and executable at once. Unity ships
com.apple.security.cs.allow-jit instead, so Mono's code lives in a MAP_JIT
region, which also rejects mach_vm_write with KERN_INVALID_ADDRESS.

The EACCES escapes DetourRuntimeILPlatform's constructor, which runs inside
DetourHelper.Runtime's initialiser after _RuntimeInit is already set, so every
later read of DetourHelper.Runtime returns null and every Harmony patch dies on
a NullReferenceException in DetourHelper.GetIdentifiable. On v6 the first
casualty is ConsoleSetOutFix.Apply, which is not wrapped in Utility.TryDo, so
the whole preloader dies before it opens LogOutput.log. The only trace is a
preloader_*.log beside the executable.

v6 reaches this more easily than v5 does: run_bepinex_mono.sh sets
ARCHPREFERENCE="arm64,x86_64" unconditionally on Apple Silicon, so a universal
game runs as native arm64 by default and no opt-in is needed to hit the bug.

AppleSiliconDetourFix installs a detour platform that keeps
DetourNativeARMPlatform's AArch64 encoding and routes every code write through
doorstop_jit_memcpy, which toggles pthread_jit_write_protect_np around its own
memcpy. The toggle cannot be done from managed code: it drops execute
permission for the calling thread, and the caller is itself running
JIT-compiled code.

XTermFix forced DetourNativeX86Platform on every non-Windows target. On Apple
Silicon that encoded x86 branches into arm64 method bodies and reinstated the
failing mprotect, so it is now skipped when the new fix applies. XTermFix also
cleared DetourHelper.Native back to null on the way out, which would discard
the platform installed here, so it now restores what it found.

doorstop_jit_memcpy landed in UnityDoorstop via NeighTools/UnityDoorstop#114
and is not in the pinned 4.5.0 release. Until DoorstopVersion is bumped, this
fix reports its absence as a warning through the preloader instead of failing
silently, which is what happens today.

Verified on an M4 Pro against Valheim 6000.0.75f1 (Unity Mono), with a probe
plugin that Harmony-patches an already-JIT-compiled method:

  arm64,   unpatched build: preloader dies, no LogOutput.log
  arm64,   this build:      plugin loads, Harmony patch applies
  x86_64,  this build:      plugin loads, Harmony patch applies

* Move AppleSiliconDetourFix to BepInEx.Preloader.Core

The IL2CPP preloader needs the same class, so keep one copy where both
preloaders already reference it rather than a copy per runtime.

* Cut the comments back to one line each
2026-11-05 19:50:19 +01:00
ghorsington b7b8dd2d84 Add HarmonyBackend config to Il2Cpp as well 2022-01-01 13:37:48 +02:00
ghorsington 81141fcfc4 Add LogInterpolatedStringHandler overload for default logger 2021-12-21 18:49:07 +02:00
ghorsington 325e0df14b Use C# 10, reformat project 2021-12-21 16:09:39 +02:00
ghorsington ff4e1b7c7f Reformat code after last commits 2021-01-26 20:47:02 +02:00
ghorsington ecb61f60d8 Apply code style 2021-01-23 22:04:11 +02:00
ghorsington 88e3dfb7e7 Update some code to use C#9 features 2021-01-05 22:54:45 +02:00
Bepis 798d008f1d Document all undocumented code
Rebase of 78484b7
2020-11-06 11:27:39 +11:00
Bepis 7f880390a0 Simplify ConsoleSetOut fix; redirect console messages to logging system
Rebase of f2b7b99
2020-11-06 02:08:15 +11:00
Bepis 25887324b7 Fix stdout not being written to Unity log when console is enabled
Rebase of a404365
2020-11-06 01:08:36 +11:00
Bepis 8a6966d040 Add runtime fix for console logging
Rebase of a00b095
2020-11-06 00:03:13 +11:00
Bepis b1f218f328 Remove no longer necessary harmony fix
Rebase of c096209
2020-11-05 23:44:11 +11:00
Bepis dc5d9f444f Initial framework split 2020-04-27 20:07:10 +10:00