Harmony
Harmony copied to clipboard
Patch failure on macOS with Apple Silicon chip & Unity 2021.2 arm version
Harmony can't apply patch when running inside Unity 2021.2 arm version on Apple Silicon chip. Here is an exception stack:
Exception: mprotect returned EACCES
MonoMod.RuntimeDetour.Platforms.DetourNativeMonoPosixPlatform.SetMemPerms (System.IntPtr start, System.UInt64 len, MonoMod.RuntimeDetour.Platforms.DetourNativeMonoPosixPlatform+MmapProts prot) (at <c38a8c3281a047488dac8c9d063b7abb>:0)
MonoMod.RuntimeDetour.Platforms.DetourNativeMonoPosixPlatform.MakeWritable (System.IntPtr src, System.UInt32 size) (at <c38a8c3281a047488dac8c9d063b7abb>:0)
MonoMod.RuntimeDetour.DetourHelper.MakeWritable (MonoMod.RuntimeDetour.IDetourNativePlatform plat, MonoMod.RuntimeDetour.NativeDetourData detour) (at <c38a8c3281a047488dac8c9d063b7abb>:0)
MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform._HookSelftest (System.Reflection.MethodInfo from, System.Reflection.MethodInfo to) (at <c38a8c3281a047488dac8c9d063b7abb>:0)
MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform..ctor () (at <c38a8c3281a047488dac8c9d063b7abb>:0)
MonoMod.RuntimeDetour.Platforms.DetourRuntimeMonoPlatform..ctor () (at <c38a8c3281a047488dac8c9d063b7abb>:0)
MonoMod.RuntimeDetour.DetourHelper.get_Runtime () (at <c38a8c3281a047488dac8c9d063b7abb>:0)
HarmonyLib.HarmonySharedState.WithState[T] (System.Func`1[TResult] action) (at <c38a8c3281a047488dac8c9d063b7abb>:0)
HarmonyLib.HarmonySharedState.GetPatchInfo (System.Reflection.MethodBase method) (at <c38a8c3281a047488dac8c9d063b7abb>:0)
HarmonyLib.PatchProcessor.Patch () (at <c38a8c3281a047488dac8c9d063b7abb>:0)
HarmonyLib.Harmony.Patch (System.Reflection.MethodBase original, HarmonyLib.HarmonyMethod prefix, HarmonyLib.HarmonyMethod postfix, HarmonyLib.HarmonyMethod transpiler, HarmonyLib.HarmonyMethod finalizer) (at <c38a8c3281a047488dac8c9d063b7abb>:0)
Fiftytwo.AddressablesAnalyzeHack.InstallAddressablesAnalyzeHack () (at Assets/Scripts/Editor/AddressablesAnalyzeHack.cs:73)
That architecture is not supported yet. There are currently no concrete plans to support it. Mostly because MonoMod.Common must support it so I ping @0x0ade
MonoMod currently "requires" RWX under the hood but has been set up for W^X for the most part already, with hopefully only minimal changes required. The main problems so far are:
- My free time has become really limited lately.
- I don't own a M1 Mac to test things on.
- Old versions of .NET and mono explicitly require RWX, thus we'd need a way to detect whether the environment requires RWX or W^X.
If you want to help with that, even if it's just testing, feel free to ping me in either the MonoMod Discord server general chat or the Harmony Discord server contributor chat.
I think I got almost (or basically) the same issue. I'm using Godot 3.4.4 on an m1 mac:
System.TypeInitializationException: The type initializer for 'HarmonyLib.HarmonySharedState' threw an exception. ---> System.Exception: mprotect returned EACCES
at MonoMod.RuntimeDetour.Platforms.DetourNativeMonoPosixPlatform.SetMemPerms (System.IntPtr start, System.UInt64 len, MonoMod.RuntimeDetour.Platforms.DetourNativeMonoPosixPlatform+MmapProts prot) [0x00043] in <212c5fc0752a439c852373c986c00088>:0
at MonoMod.RuntimeDetour.Platforms.DetourNativeMonoPosixPlatform.MakeWritable (System.IntPtr src, System.UInt32 size) [0x00000] in <212c5fc0752a439c852373c986c00088>:0
at MonoMod.RuntimeDetour.DetourHelper.MakeWritable (MonoMod.RuntimeDetour.IDetourNativePlatform plat, MonoMod.RuntimeDetour.NativeDetourData detour) [0x00000] in <212c5fc0752a439c852373c986c00088>:0
at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform._HookSelftest (System.Reflection.MethodInfo from, System.Reflection.MethodInfo to) [0x00035] in <212c5fc0752a439c852373c986c00088>:0
at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform..ctor () [0x0004a] in <212c5fc0752a439c852373c986c00088>:0
at MonoMod.RuntimeDetour.Platforms.DetourRuntimeMonoPlatform..ctor () [0x00000] in <212c5fc0752a439c852373c986c00088>:0
at MonoMod.RuntimeDetour.DetourHelper.get_Runtime () [0x00044] in <212c5fc0752a439c852373c986c00088>:0
at HarmonyLib.HarmonySharedState..cctor () [0x000d8] in <212c5fc0752a439c852373c986c00088>:0
I can open a separate issue if that's better but the callstacks are pretty similar.
It would be a pretty big shame if Harmony didn't work on mac in the future (especially when m1 becomes all there is)...
Hello @pardeike! Thanks for the great work for this repository. Do you have an ETA for current issue?
No I don't have an ETA at all @imurashka. I am pretty sure it's not even up to me to fix it and both @0x0ade and I don't have the equipment to test or develop this.
@pardeike, understood. Thanks for quick reply!
Just adding a "us too" comment - our game is running into this same issue now that we updated to 2021 unity and we would love to find a solution. Currently I'm considering just testing if an innocuous patch fails, and showing an error message saying "Your platform is incompatible with Harmony based mods" with a guide on how to run the game via "Open using Rosetta" which someone on your discord suggested to our user that had the issue.
For reference our issue: https://bitbucket.org/bbucklew/cavesofqud-public-issue-tracker/issues/7599/harmony-incompatibility-on-beta-branch-for
Also @0x0ade, is there an issue open for this already on monomod? Something that describes the problem a little better? I would love to put out some feelers to see if I can find someone to help push this forward more, but I'm not even sure what skillsets would be nice to have :)
Anyway, I'd consider throwing some cash in the pool on a bountysource to solve this if we have a good writeup of the problem space for someone to read and engage
https://github.com/pardeike/Harmony/tree/feature/monomod-core will probably solve this when its completed.
This is an issue better addressed in MonoMod.Core which is the library that Harmony uses to do the low level stuff.
Sorry if the link was already posted, but could I get a link to the issue in the MonoMod.Core repo? So that I can keep following the progress on this.
I did not imply that there is an existing issue. It might exist but ultimately someone from their team (like @0x0ade or @nike4613) can give you guidance.
https://github.com/MonoMod/MonoMod/issues/90 is probably the closest to a tracking issue we have for this. I don't have Apple Silicon (or even really a machine with an ARM chip I can use), so architecture support is currently missing, then the MacOS System implementation needs to be modified to behave using Apple's W^X APIs on M1.