MonoMod / MonoMod.Common

Common code used by MonoMod and other .NET modding libraries. Not to be confused with MonoMod.Utils (for mods).
MIT License
75 stars 33 forks source link

Parse MethodDescs to obtain function ptrs on .NET Core 3.0+ #25

Open 0x0ade opened 2 years ago

0x0ade commented 2 years ago

This PR introduces MethodDesc parsing for .NET Core 3.0+, which can be opt out of by setting the environment variable MONOMOD_RUNTIMEDETOUR_NETCORE30PLUS_OLDFTNPTR to 1.

Quoting myself from the MonoMod Discord server:

I see it as either "keep up with runtime internals by parsing methoddescs" or "keep up with runtime internals on a per platform basis by trying to understand what the JIT emits" in the past, the runtime was a black box and "fixing" RuntimeMethodHandle.GetFunctionPointer might've been less effort than trying to understand methoddescs but nowadays precode walking is stable enough on framework, coreclr is open source, yet another platform is on the horizon, and it feels like there are more combinations of stubs and platforms than there are variations of method descriptors to grab pointers from

Reducing the dependency on precode walking should also theoretically help with getting new platforms up and running faster, such as ARM. My current focus is fixing an edge case interaction between RuntimeDetour and coreclr's backpatcher though, discovered by tModLoader modders, where RuntimeDetour's precode walker misinterprets the location of the method desc in the method table as the location of the code and zeroes it out, shortly before the runtime dies on a backpatch attempt.