mirror of
https://github.com/BepInEx/BepInEx.git
synced 2026-11-19 13:17:07 +00:00
* 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
114 lines
4.5 KiB
C#
114 lines
4.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using BepInEx.Preloader.Core;
|
|
using BepInEx.Preloader.RuntimeFixes;
|
|
using BepInEx.Unity.Mono.Preloader.RuntimeFixes;
|
|
using BepInEx.Unity.Mono.Preloader.Utils;
|
|
|
|
namespace BepInEx.Unity.Mono.Preloader;
|
|
|
|
internal static class UnityPreloaderRunner
|
|
{
|
|
// This is a list of important assemblies in BepInEx core folder that should be force-loaded
|
|
// Some games can ship these assemblies in Managed folder, in which case assembly resolving bypasses our LocalResolve
|
|
// On the other hand, renaming these assemblies is not viable because 3rd party assemblies
|
|
// that we don't build (e.g. MonoMod, Harmony, many plugins) depend on them
|
|
// As such, we load them early so that the game uses our version instead
|
|
// These assemblies should be known to be rarely edited and are known to be shipped as-is with Unity assets
|
|
private static readonly string[] CriticalAssemblies =
|
|
{
|
|
"Mono.Cecil.dll",
|
|
"Mono.Cecil.Mdb.dll",
|
|
"Mono.Cecil.Pdb.dll",
|
|
"Mono.Cecil.Rocks.dll",
|
|
"MonoMod.Utils.dll",
|
|
"MonoMod.RuntimeDetour.dll",
|
|
"0Harmony.dll"
|
|
};
|
|
|
|
private static void LoadCriticalAssemblies()
|
|
{
|
|
foreach (var criticalAssembly in CriticalAssemblies)
|
|
try
|
|
{
|
|
MonoAssemblyHelper.Load(Path.Combine(Paths.BepInExAssemblyDirectory, criticalAssembly));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// Suppress error for now
|
|
// TODO: Should we crash here if load fails? Can't use logging at this point
|
|
}
|
|
}
|
|
|
|
public static void PreloaderPreMain()
|
|
{
|
|
PlatformUtils.SetPlatform();
|
|
|
|
// After SetPlatform, before any DetourHelper read.
|
|
AppleSiliconDetourFix.Apply();
|
|
|
|
var bepinPath = Utility.ParentDirectory(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH), 2);
|
|
|
|
Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH,
|
|
bepinPath,
|
|
EnvVars.DOORSTOP_MANAGED_FOLDER_DIR,
|
|
true,
|
|
EnvVars.DOORSTOP_DLL_SEARCH_DIRS);
|
|
|
|
LoadCriticalAssemblies();
|
|
AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
|
|
PreloaderMain();
|
|
}
|
|
|
|
private static void PreloaderMain()
|
|
{
|
|
if (UnityPreloader.ConfigApplyRuntimePatches.Value)
|
|
{
|
|
XTermFix.Apply();
|
|
ConsoleSetOutFix.Apply();
|
|
}
|
|
|
|
UnityPreloader.Run();
|
|
}
|
|
|
|
private static Assembly LocalResolve(object sender, ResolveEventArgs args)
|
|
{
|
|
if (!Utility.TryParseAssemblyName(args.Name, out var assemblyName))
|
|
return null;
|
|
|
|
// Use parse assembly name on managed side because native GetName() can fail on some locales
|
|
// if the game path has "exotic" characters
|
|
|
|
var validAssemblies = AppDomain.CurrentDomain
|
|
.GetAssemblies()
|
|
.Select(a => new
|
|
{
|
|
assembly = a,
|
|
name = Utility.TryParseAssemblyName(a.FullName, out var name)
|
|
? name
|
|
: null
|
|
})
|
|
.Where(a => a.name != null && a.name.Name == assemblyName.Name)
|
|
.OrderByDescending(a => a.name.Version)
|
|
.ToList();
|
|
|
|
// First try to match by version, then just pick the best match (generally highest)
|
|
// This should mainly affect cases where the game itself loads some assembly (like Mono.Cecil)
|
|
var foundMatch = validAssemblies.FirstOrDefault(a => a.name.Version == assemblyName.Version) ??
|
|
validAssemblies.FirstOrDefault();
|
|
var foundAssembly = foundMatch?.assembly;
|
|
|
|
if (foundAssembly != null)
|
|
return foundAssembly;
|
|
|
|
if (MonoAssemblyHelper.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
|
|
|| MonoAssemblyHelper.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
|
|
|| MonoAssemblyHelper.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
|
|
return foundAssembly;
|
|
|
|
return null;
|
|
}
|
|
}
|