Il2CppInterop icon indicating copy to clipboard operation
Il2CppInterop copied to clipboard

Stack overflow in GenericMethod_GetMethod_Hook when calling ClassInjector.RegisterTypeInIl2Cpp<T>

Open jim972329667 opened this issue 5 months ago • 4 comments

What happened?

The tool crashes during the process startup phase when running on the game Lost Castle 2. Same critical error as in issue #215, but with an important discovery and a subsequent error.

First Error (with UnityLogListening enabled)

ErrorLog.log
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Il2CppInterop.Runtime.Injection.Hooks.GenericMethod_GetMethod_Hook.Hook(Il2CppInterop.Runtime.Runtime.Il2CppGenericMethod*, Boolean)
   at BepInEx.Unity.IL2CPP.IL2CPPChainloader.OnInvokeMethod(IntPtr, IntPtr, IntPtr, IntPtr)
   at Il2CppInterop.Runtime.IL2CPP.il2cpp_runtime_invoke(IntPtr, IntPtr, Void**, IntPtr ByRef)
   at Il2CppSystem.Type.GetMethod(System.String)
   at Il2CppInterop.Runtime.DelegateSupport.ConvertDelegate[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Delegate)
   at UnityEngine.Application+LogCallback.op_Implicit(System.Action`3<System.String,System.String,UnityEngine.LogType>)
   at BepInEx.Unity.IL2CPP.Logging.IL2CPPUnityLogSource..ctor()
   at BepInEx.Unity.IL2CPP.IL2CPPChainloader.OnInvokeMethod(IntPtr, IntPtr, IntPtr, IntPtr)

Disabling UnityLogListening allows the mod to load normally, bypassing the first error.

Second Error (after disabling UnityLogListening)

ErrorLog.log
Stack overflow.
   at Il2CppInterop.Runtime.Injection.Hooks.GenericMethod_GetMethod_Hook.Hook(Il2CppInterop.Runtime.Runtime.Il2CppGenericMethod*, Boolean)
   at BepInEx.Unity.IL2CPP.IL2CPPChainloader.OnInvokeMethod(IntPtr, IntPtr, IntPtr, IntPtr)
   at Il2CppInterop.Runtime.IL2CPP.il2cpp_runtime_invoke(IntPtr, IntPtr, Void**, IntPtr ByRef)
   at Il2CppSystem.Reflection.MethodInfo.MakeGenericMethod(Il2CppInterop.Runtime.InteropTypes.Arrays.Il2CppReferenceArray`1<Il2CppSystem.Type>)
   at UnityEngine.GameObject+MethodInfoStoreGeneric_AddComponent_Public_T_0`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]..cctor()
   at UnityEngine.GameObject.AddComponent[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]()
   at UniverseLib.UniversalBehaviour.Setup()
   at UniverseLib.Universe.Init(Single, System.Action, System.Action`2<System.String,UnityEngine.LogType>, UniverseLib.Config.UniverseLibConfig)
   at ZGScriptTrainer.ZGScriptTrainer.Init()
   at ZGScriptTrainer.ZGScriptTrainer.Load()
   at BepInEx.Unity.IL2CPP.IL2CPPChainloader.LoadPlugin(BepInEx.PluginInfo, System.Reflection.Assembly)
   at BepInEx.Bootstrap.BaseChainloader`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].LoadPlugins(System.Collections.Generic.IList`1<BepInPluginInfo>)
   at BepInEx.Bootstrap.BaseChainloader`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Execute()
   at BepInEx.Unity.IL2CPP.IL2CPPChainloader.OnInvokeMethod(IntPtr, IntPtr, IntPtr, IntPtr)
LogOutput.log
[Message: Preloader] BepInEx 6.0.0-be.738 - LostCastle2 (2025/9/29 21:58:02)
[Message: Preloader] Built from commit af0cba71fb69324aa4dfd651422bfa020309ce15
[Info   :   BepInEx] System platform: Windows 10 64-bit
[Info   :   BepInEx] Process bitness: 64-bit (x64)
[Info   :   BepInEx] Running under Unity 6000.0.57f1
[Info   :   BepInEx] Runtime version: 6.0.7
[Info   :   BepInEx] Runtime information: .NET 6.0.7
[Info   : Preloader] 0 patcher plugins loaded
[Info   : Preloader] 0 assemblies discovered
[Message:AssemblyPatcher] Executing 0 patch(es)
[Message:   BepInEx] Chainloader initialized
[Debug  : Preloader] Runtime invoke pointer: 0x7FFE07368360
[Debug  :DobbyDetour] Preparing detour from 0x7FFE07368360 to 0x7FFE166A3264
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7370000
[Debug  :DobbyDetour] Original: 7FFE07368360, Trampoline: 7FFDC7370000, diff: 3FFF8360
[Debug  : Preloader] Runtime invoke patched
[Debug  :InteropManager] Preloaded 200 interop assemblies in 692ms
[Debug  :   BepInEx] Examining 'E:\program files (x86)\steam\steamapps\common\Lost Castle 2\BepInEx\plugins\UniverseLib.IL2CPP.Interop.dll'
[Debug  :   BepInEx] Examining 'E:\program files (x86)\steam\steamapps\common\Lost Castle 2\BepInEx\plugins\ZGScriptTrainer.dll'
[Info   :   BepInEx] 1 plugin to load
[Info   :   BepInEx] Loading [失落城堡2内置修改器 1.1.0]
[Debug  :Il2CppInterop] il2cpp_object_get_virtual_method: 0x7FFE07368260
[Debug  :Il2CppInterop] Object::GetVirtualMethod: 0x7FFE07348290
[Debug  :Il2CppInterop] GenericMethod::GetMethod found: 0x7FFE07377880
[Debug  :DobbyDetour] Preparing detour from 0x7FFE07377880 to 0x7FFE166A3424
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7380000
[Debug  :DobbyDetour] Original: 7FFE07377880, Trampoline: 7FFDC7380000, diff: 3FFF7880
[Debug  :Il2CppInterop] il2cpp_image_get_class: 0x7FFE07367F10
[Debug  :Il2CppInterop] Image::GetType: 0x7FFE0736E410
[Debug  :Il2CppInterop] imageGetTypeXrefs.Length: 2
[Debug  :Il2CppInterop] getTypeInfoFromHandle: 7FFE07356B00
[Debug  :Il2CppInterop] MetadataCache::GetTypeInfoFromTypeDefinitionIndex found: 0x7FFE0731B220
[Debug  :DobbyDetour] Preparing detour from 0x7FFE0731B220 to 0x7FFE166A3464
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7370020
[Debug  :DobbyDetour] Original: 7FFE0731B220, Trampoline: 7FFDC7370020, diff: 3FFAB200
[Debug  :Il2CppInterop] Class::GetDefaultFieldValue found: 0x7FFE07319C80
[Debug  :DobbyDetour] Preparing detour from 0x7FFE07319C80 to 0x7FFE166A34A4
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7370040
[Debug  :DobbyDetour] Original: 7FFE07319C80, Trampoline: 7FFDC7370040, diff: 3FFA9C40
[Warning:Il2CppInterop] Class::Init signatures have been exhausted, using a substitute!
[Debug  :Il2CppInterop] Picked mono_class_instance_size as a Class::Init substitute
[Debug  :Il2CppInterop] Class::Init: 0x7FFE07368B70
[Debug  :Il2CppInterop] il2cpp_class_from_il2cpp_type: 0x7FFE07310A90
[Debug  :Il2CppInterop] Class::FromIl2CppType found: 0x7FFE073539A0
[Debug  :DobbyDetour] Preparing detour from 0x7FFE073539A0 to 0x7FFE166A34E4
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7370060
[Debug  :DobbyDetour] Original: 7FFE073539A0, Trampoline: 7FFDC7370060, diff: 3FFE3940
[Debug  :Il2CppInterop] il2cpp_class_from_name: 0x7FFE07367800
[Debug  :Il2CppInterop] Class::FromName found: 0x7FFE07353F00
[Debug  :DobbyDetour] Preparing detour from 0x7FFE07353F00 to 0x7FFE166A3524
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7370080
[Debug  :DobbyDetour] Original: 7FFE07353F00, Trampoline: 7FFDC7370080, diff: 3FFE3E80
[Debug  :Il2CppInterop] GarbageCollector::RunFinalizer found: 0x7FFE07343DC0
[Debug  :DobbyDetour] Preparing detour from 0x7FFE07343DC0 to 0x7FFE166A3564
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC73700A0
[Debug  :DobbyDetour] Original: 7FFE07343DC0, Trampoline: 7FFDC73700A0, diff: 3FFD3D20
[Info   :Il2CppInterop] Registered mono type ZGScriptTrainer.UI.Models.TooltipGUI in il2cpp domain
[Info   :  HarmonyX] ### Harmony id=ScriptTrainer.Jim97.Lost_Castle_2, version=2.10.2.0, location=E:\program files (x86)\steam\steamapps\common\Lost Castle 2\BepInEx\core\0Harmony.dll, env/clr=6.0.7, platform=Win32NT, ptrsize:runtime/env=8/Bits64, Windows
### Started from static void ZGScriptTrainer.ZGScriptTrainer::.cctor(), location E:\program files (x86)\steam\steamapps\common\Lost Castle 2\BepInEx\plugins\ZGScriptTrainer.dll
### At 2025-10-03 08.49.29
[Info   :  HarmonyX] Patching LC2.BagSystem+CheckPickUpType LC2.BagSystem::GetPickUpType(LC2.Item item) with 0 prefixes, 1 postfixes, 0 transpilers, 0 finalizers
1 postfixes:
* static void ZGScriptTrainer.ScriptPatch+BagSystemPatch::Postfix(LC2.BagSystem+CheckPickUpType& __result)

[Info   :  HarmonyX] Running ILHook manipulator on LC2.BagSystem+CheckPickUpType LC2.BagSystem::GetPickUpType(LC2.Item item)
[Info   :  HarmonyX] Writing postfixes
[Debug  :DobbyDetour] Preparing detour from 0x7FFE079689A0 to 0x7FFE166A3B24
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7970000
[Debug  :DobbyDetour] Original: 7FFE079689A0, Trampoline: 7FFDC7970000, diff: 3FFF89A0
[Info   :  HarmonyX] Patching LC2.BagSystem+CheckPickUpType LC2.BagSystem::GetPickUpType_Shop(LC2.Item item, int price) with 0 prefixes, 1 postfixes, 0 transpilers, 0 finalizers
1 postfixes:
* static void ZGScriptTrainer.ScriptPatch+BagSystemPatch::Postfix(LC2.BagSystem+CheckPickUpType& __result)

[Info   :  HarmonyX] Running ILHook manipulator on LC2.BagSystem+CheckPickUpType LC2.BagSystem::GetPickUpType_Shop(LC2.Item item, int price)
[Info   :  HarmonyX] Writing postfixes
[Debug  :DobbyDetour] Preparing detour from 0x7FFE07968400 to 0x7FFE166A3B64
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7970020
[Debug  :DobbyDetour] Original: 7FFE07968400, Trampoline: 7FFDC7970020, diff: 3FFF83E0
[Info   :  HarmonyX] Patching LC2.BagSystem+CheckPickUpType LC2.BagSystem::GetPickUpType_PassiveProps(LC2.PassiveProps passiveProps) with 0 prefixes, 1 postfixes, 0 transpilers, 0 finalizers
1 postfixes:
* static void ZGScriptTrainer.ScriptPatch+BagSystemPatch::Postfix(LC2.BagSystem+CheckPickUpType& __result)

[Info   :  HarmonyX] Running ILHook manipulator on LC2.BagSystem+CheckPickUpType LC2.BagSystem::GetPickUpType_PassiveProps(LC2.PassiveProps passiveProps)
[Info   :  HarmonyX] Writing postfixes
[Debug  :DobbyDetour] Preparing detour from 0x7FFE07967F20 to 0x7FFE166A3BA4
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7970040
[Debug  :DobbyDetour] Original: 7FFE07967F20, Trampoline: 7FFDC7970040, diff: 3FFF7EE0
[Info   :  HarmonyX] Patching LC2.Power.PowerEntry LC2.Weapon::GetNewPerk(bool addNewPerk) with 0 prefixes, 1 postfixes, 0 transpilers, 0 finalizers
1 postfixes:
* static void ZGScriptTrainer.ScriptPatch+WeaponOverridePatch_GetNewPerk::Postfix(LC2.Weapon __instance, LC2.Power.PowerEntry& __result)

[Info   :  HarmonyX] Running ILHook manipulator on LC2.Power.PowerEntry LC2.Weapon::GetNewPerk(bool addNewPerk)
[Info   :  HarmonyX] Writing postfixes
[Debug  :DobbyDetour] Preparing detour from 0x7FFE0767DBF0 to 0x7FFE166A3BE4
[Debug  :DobbyDetour] Prepared detour; Trampoline: 0x7FFDC7970060
[Debug  :DobbyDetour] Original: 7FFE0767DBF0, Trampoline: 7FFDC7970060, diff: 3FD0DB90
[Info   :  HarmonyX] ### Harmony id=com.sinai.universelib, version=2.10.2.0, location=E:\program files (x86)\steam\steamapps\common\Lost Castle 2\BepInEx\core\0Harmony.dll, env/clr=6.0.7, platform=Win32NT, ptrsize:runtime/env=8/Bits64, Windows
### Started from static void UniverseLib.Universe::.cctor(), location E:\program files (x86)\steam\steamapps\common\Lost Castle 2\BepInEx\plugins\UniverseLib.IL2CPP.Interop.dll
### At 2025-10-03 08.49.29
[Message:失落城堡2内置修改器] [UniverseLib] UniverseLib 1.5.1 initializing...
[Info   :Il2CppInterop] Registered mono type UniverseLib.UniversalBehaviour in il2cpp domain

Steps to Reproduce

  • Install BepInEx
  • With UnityLogListening enabled: Game crashes with AccessViolationException
  • With UnityLogListening disabled: Mod starts loading but crashes with StackOverflow during component initialization

BepInEx Distribution

  • BepInEx 6.0.0-be.738

Environment

  • OS: Win 11
  • Unity: 6000.0.57f1
  • Game: Lost Castle 2

jim972329667 avatar Oct 03 '25 13:10 jim972329667

Similar issue with example Unity 6000.0.5x project:

  • 6000.0.58.9625317 IL2CPP 31
  • Win10/Bepinex BE #738 x64

6000.0.58_il2cpp_sig_project.zip

Kein avatar Oct 09 '25 12:10 Kein

@jim972329667 the sig you provided works, any idea if there is a chinese fork of il2cppinterop that works for 6.x for unstripping/gen?

Kein avatar Oct 09 '25 15:10 Kein

@jim972329667 the sig you provided works, any idea if there is a chinese fork of il2cppinterop that works for 6.x for unstripping/gen?

UnityExplorer/UniverseLib still uses a few APIs that were removed in Unity 6 (e.g. Application.unityVersion, Transform.root). Until the official repo is updated you'll need to clone UniverseLib, replace the obsolete calls with the new Unity 6 equivalents, and rebuild UniverseLib.IL2CPP.dll yourself.

jim972329667 avatar Oct 09 '25 16:10 jim972329667

Neither Application.unityVersion nor Transform.root are deprecated, if they were, you'd see deprecation notice here: https://docs.unity3d.com/6000.0/Documentation/ScriptReference/Transform-root.html

They also present in mono builds of 6000.0.5x. It is cpp2il unstripping/gen problem: [Info :Il2CppInteropGen] IL unstrip statistics: 0 successful, 0 failed It literally does not work anymore on 6.x

Kein avatar Oct 09 '25 16:10 Kein

@HerpDerpinstine fixed this in the MelonLoader project with the following commit https://github.com/LavaGang/MelonLoader/commit/11bdcbfa93a8cdd5482272e8bb24c031e624ffe8

They don't maintain a fork. They instead patch this open source library at runtime... wtf

The change they made is swapping this call from Last() to First()

https://github.com/BepInEx/Il2CppInterop/blob/e2639503e82d8a5d2acf75970ec4d4b9155e8398/Il2CppInterop.Runtime/Injection/Hooks/GenericMethod_GetMethod_Hook.cs#L83

Related issues:

  • https://github.com/BepInEx/BepInEx/issues/1244
  • https://github.com/BepInEx/BepInEx/issues/1255

pixeltris avatar Jan 29 '26 19:01 pixeltris

following infomation from pixeltris, fixing & recompiling, problem resolved.

Inas-07 avatar Jan 30 '26 06:01 Inas-07