BepInEx / Il2CppInterop

A tool interoperate between CoreCLR and Il2Cpp at runtime
GNU Lesser General Public License v3.0
195 stars 61 forks source link

Error with enums with prefixed namespaces as generic params #66

Closed SamboyCoding closed 1 year ago

SamboyCoding commented 1 year ago

There is a logic error in this section of code: https://github.com/BepInEx/Il2CppInterop/blob/master/Il2CppInterop.Runtime/Il2CppClassPointerStore.cs#L45-L51

which means any Enum types in Il2Cpp-prefixed namespaces get their class pointer set to 0, which leads to exceptions in the hkClassFromIl2CppType patch because the type pointer is 0x20 (0 plus the offset of the relevant field, I presume). These are easily observed by trying to use any il2cpp generic type (e.g. List) with an enum type as any of the generic arguments, and also breaks accessing game classes which use fields of such a type.

The exception comes across as something like this:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Il2CppInterop.Runtime.Injection.InjectorHelpers.hkClassFromIl2CppType(Il2CppType* type, Boolean throwOnError) in /home/runner/work/Il2CppInterop/Il2CppInterop/Il2CppInterop.Runtime/Injection/InjectorHelpers.cs:line 335
   at Il2CppInterop.Runtime.IL2CPP.il2cpp_runtime_invoke(IntPtr method, IntPtr obj, Void** param, IntPtr& exc)
   at Il2CppSystem.Type.internal_from_handle(IntPtr handle)
   at Il2CppSystem.Collections.Generic.List`1..cctor()

though the actual callstack dips in and out of native code a bit more than that: image

The linked code needs to check for, and remove, any il2cpp prefix that isn't in the il2cpp metadata.

doombubbles commented 1 year ago

++ from me on the importance of this issue. It directly impacts https://github.com/doombubbles/ultimate-crosspathing (one of the most prominent BTD6 mods) and necessitates some very unsustainable workarounds.

Kasuromi commented 1 year ago

Fixed in 44f18f02fc95ebd225a43272fbe53f48aa9ac113.