DetourRuntimeNETPlatform.GetFunctionPointer() caught in infinite loop
I am trying to use Harmony to apply some patches, however it seems to be getting stuck in an infinite loop while attempting to get the function pointer for the original method.
I have not been able to debug it more accurately yet, but it is seems to be getting stuck while executing this call: https://github.com/MonoMod/MonoMod.Common/blob/ea24867bb49621372eaf53b2ef552cbf488af55b/RuntimeDetour/Platforms/Runtime/DetourRuntimeNETPlatform.cs#L448 And it seems to keeps running into this jump: https://github.com/MonoMod/MonoMod.Common/blob/ea24867bb49621372eaf53b2ef552cbf488af55b/RuntimeDetour/Platforms/Runtime/DetourRuntimeNETPlatform.cs#L242
Interestingly, the issue did not occur when I manually patched the DLL, adding the patches and invoking the patch process from the original DLL. I have since moved the patches and harmony to my own DLL to which I have only injected a single call from the original DLL. However since I have done that, this issue has seemed to occur every time.
Stack trace
at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetNativeStart(MethodBase method)
at MonoMod.RuntimeDetour.Platforms.DetourRuntimeNETPlatform.NotThePreStub(IntPtr ptrGot, IntPtr ptrParsed, Boolean& wasPreStub)
at MonoMod.RuntimeDetour.Platforms.DetourRuntimeNETPlatform.GetFunctionPointer(MethodBase method, RuntimeMethodHandle handle)
at MonoMod.RuntimeDetour.Platforms.DetourRuntimeILPlatform.GetNativeStart(MethodBase method)
at HarmonyLib.Memory.GetMethodStart(MethodBase method, Exception& exception)
at HarmonyLib.Memory.DetourMethod(MethodBase original, MethodBase replacement)
at HarmonyLib.Memory.DetourMethodAndPersist(MethodBase original, MethodBase replacement)
at HarmonyLib.PatchFunctions.UpdateWrapper(MethodBase original, PatchInfo patchInfo)
at HarmonyLib.PatchProcessor.Patch()
at HarmonyLib.Harmony.Patch(MethodBase original, HarmonyMethod prefix, HarmonyMethod postfix, HarmonyMethod transpiler, HarmonyMethod finalizer)
Further information
The DLL I am trying to patch is using .NET Framework v4.0, my DLL as well as the Harmony DLL I am using are .NET Framework v4.8.
Please let me know about any other information I can provide Thank you for maintaining a great project 💯
My first suggestion will be to try using MonoModReorg.RuntimeDetour instead of Harmony, because that is under active development. What method are you trying to patch, and what runtime are you running on? (I assume .NET Framework, but Framework targets can be run on Mono as well.)
I will try that, thanks for the suggestion.
The method I was trying to patch is a private method as part of an internal class, particularly bound to a WinForms button click event. The application is built on top of the visual studio 2015 isolated shell.
My first suggestion will be to try using
MonoModReorg.RuntimeDetourinstead of Harmony, because that is under active development. What method are you trying to patch, and what runtime are you running on? (I assume .NET Framework, but Framework targets can be run on Mono as well.)
I've now switched to using a MonoModReorg.RuntimeDetour Hook to patch said method, and it seems to be working now. Thank you.
Is this still an issue you think can be looked at? Since I am looking to create more and more complicated patches, I would like to use Harmony if possible, making the process a little easier. Otherwise I'd also like to take a look at using HookGen once it supports the new RuntimeDetour :)
Hopefully, Harmony will eventually switch to using the same backend as Reorg's RuntimeDetour, solving the issue entirely. HookGen also does work, but I'd personally recommend against using it because it makes it much too easy to ignore hook lifetimes in non-obvious ways.
I am mentally preparing for this. I might need a few minutes from you @nike4613 to discuss some aspects of the rewrite. I'll do that later this week if that's ok with you.
Sure, that should work. Let me know when you want to do that.