FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix JIT compilation for methods on generic types by JBou · Pull Request #298 · MonoMod/MonoMod · GitHub

Fix JIT compilation for methods on generic types - #298

Open
JBou wants to merge 1 commit into
MonoMod:reorganizefrom
JBou:fix_generics
Open

Fix JIT compilation for methods on generic types#298
JBou wants to merge 1 commit into
MonoMod:reorganizefrom
JBou:fix_generics

Conversation

JBou commented Mar 10, 2026

Copy link
Copy Markdown

Until now, I had to stay on Harmony 2.2.2, because patches on generic types where not working with Harmony 2.3 using MonoMod.Core anymore. This PR would allow to patch them again. I verified in our production app, and am able to apply the patches again.

Changes:

Update FxCoreBaseRuntime.Compile to pass generic type arguments to RuntimeHelpers.PrepareMethod when the method is declared on a generic type. This ensures that the runtime prepares the correct method instantiation.

Add unit tests Test_GenericClassDetour and Test_GenericClassDetourWithoutGenericParameter to DetourTest to verify detours on generic types.

Update `FxCoreBaseRuntime.Compile` to pass generic type arguments to `RuntimeHelpers.PrepareMethod` when the method is declared on a generic type. This ensures that the runtime prepares the correct method instantiation.

Add unit tests `Test_GenericClassDetour` and `Test_GenericClassDetourWithoutGenericParameter` to `DetourTest` to verify detours on generic types.

nike4613 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We do not support patching generics at all at the moment, and while I'm perfectly willing to take improvements in this area, this is an extremely difficult area, and I doubt this fixes the fundamental problem.

Assert.Equal(1, obj1.GetDouble());
Assert.Equal(1, obj2.GetDouble());

var detour1 = DetourFactory.Current.CreateDetour(originalMethod1, replacement1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This should be in a using block.

Comment on lines +377 to +386
else
{
RuntimeHelpers.PrepareMethod(handle);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If we're going to try to do any handling of generics, it should be complete, and deal with method instantiations properly as well. As such, the generic type param list construction should probably be its own helper (maybe in utils? There may also already be an appropriate helper somewhere in the JIT hook logic.)

This comment has been minimized.

Copy link
Copy Markdown
Contributor

Test Results

Total Skipped Passed Failed
Unique 285 0 283 2
Total 49571 348 49206 17

Failing runs

❌ MonoMod.UnitTest.DetourTest.Test_GenericClassDetour

Class Name: MonoMod.UnitTest.DetourTest | Method Name: Test_GenericClassDetour | This test is sometimes failing.

Failures present in
  • test-results .NET Core 2.1 x64 on Linux/TestResults/testresults.ubuntu-latest.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on MacOS 14/TestResults/testresults.macos-14.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on MacOS 15 Intel/TestResults/testresults.macos-15-intel.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on MacOS 15/TestResults/testresults.macos-15.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on Windows 11 ARM64/TestResults/testresults.windows-11-arm.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on Windows/TestResults/testresults.windows-latest.2.1.x64.trx
  • test-results .NET Core 2.1 x86 on Windows 11 ARM64/TestResults/testresults.windows-11-arm.2.1.x86.trx
    and 4 more
❌ test-results .NET Core 2.1 x86 on Windows/TestResults/testresults.windows-latest.2.1.x86.trx MonoMod.UnitTest.DetourTest.Test_GenericClassDetour

Took 00:00:00.0130000

Failure also present in
  • test-results .NET Core 2.1 x64 on Linux/TestResults/testresults.ubuntu-latest.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on MacOS 14/TestResults/testresults.macos-14.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on MacOS 15 Intel/TestResults/testresults.macos-15-intel.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on MacOS 15/TestResults/testresults.macos-15.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on Windows 11 ARM64/TestResults/testresults.windows-11-arm.2.1.x64.trx
  • test-results .NET Core 2.1 x64 on Windows/TestResults/testresults.windows-latest.2.1.x64.trx
  • test-results .NET Core 2.1 x86 on Windows 11 ARM64/TestResults/testresults.windows-11-arm.2.1.x86.trx
    and 3 more
Exception message:
Assert.Equal() Failure
Expected: 999
Actual:   1

Stack trace:

   at MonoMod.UnitTest.DetourTest.Test_GenericClassDetour() in /_/src/MonoMod.UnitTest/RuntimeDetour/DetourTest.cs:line 103

Test Standard Output has been omitted because it is too long.

❌ MonoMod.UnitTest.DetourTest.Test_GenericClassDetourWithoutGenericParameter

Class Name: MonoMod.UnitTest.DetourTest | Method Name: Test_GenericClassDetourWithoutGenericParameter | This test is sometimes failing.

Failures present in
  • test-results System Mono on Linux ARM64/testresults.ubuntu-24.04-arm.sysmono.arm64.xml
  • test-results System Mono on Linux musl/testresults.ubuntu-latest.sysmono.x64.xml
  • test-results System Mono on Linux/testresults.ubuntu-latest.sysmono.x64.xml
  • test-results System Mono on MacOS 14/testresults.macos-14.sysmono.arm64.xml
  • test-results System Mono on MacOS 15 Intel/testresults.macos-15-intel.sysmono.x64.xml
  • test-results System Mono on MacOS 15/testresults.macos-15.sysmono.arm64.xml
❌ test-results System Mono on Linux ARM64/testresults.ubuntu-24.04-arm.sysmono.arm64.xml MonoMod.UnitTest.DetourTest.Test_GenericClassDetourWithoutGenericParameter

Took 00:00:00.0157056

Failure also present in
  • test-results System Mono on Linux musl/testresults.ubuntu-latest.sysmono.x64.xml
  • test-results System Mono on Linux/testresults.ubuntu-latest.sysmono.x64.xml
  • test-results System Mono on MacOS 14/testresults.macos-14.sysmono.arm64.xml
  • test-results System Mono on MacOS 15 Intel/testresults.macos-15-intel.sysmono.x64.xml
  • test-results System Mono on MacOS 15/testresults.macos-15.sysmono.arm64.xml
Exception message:
Assert.Throws() Failure
Expected: typeof(System.ArgumentException)
Actual:   typeof(System.InvalidOperationException): Could not execute the method because the containing type 'MonoMod.UnitTest.TestSingleGenericObject`1[T]', is not fully instantiated.
---- System.InvalidOperationException : Could not execute the method because the containing type 'MonoMod.UnitTest.TestSingleGenericObject`1[T]', is not fully instantiated.

Stack trace:

  at (wrapper managed-to-native) System.RuntimeMethodHandle.GetFunctionPointer(intptr)
  at System.RuntimeMethodHandle.GetFunctionPointer () [0x00000] in <12b418a7818c4ca0893feeaaf67f1e7f>:0 
  at MonoMod.Core.Platforms.Runtimes.MonoRuntime.Compile (System.Reflection.MethodBase method) [0x00008] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTriple.Compile (System.Reflection.MethodBase method) [0x00047] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTripleDetourFactory+Detour.CreateDetour () [0x00081] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTripleDetourFactory+DetourBase.Apply () [0x0005f] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTripleDetourFactory.CreateDetour (MonoMod.Core.CreateDetourRequest request) [0x00094] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.DetourFactory.CreateDetour (MonoMod.Core.IDetourFactory factory, System.Reflection.MethodBase source, System.Reflection.MethodBase target, System.Boolean applyByDefault) [0x0001d] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.UnitTest.DetourTest+<>c__DisplayClass3_0.<Test_GenericClassDetourWithoutGenericParameter>b__0 () [0x00005] in <b34d1dbe2e9f4934b27f15b43d680fcd>:0 
----- Inner Stack Trace -----
  at (wrapper managed-to-native) System.RuntimeMethodHandle.GetFunctionPointer(intptr)
  at System.RuntimeMethodHandle.GetFunctionPointer () [0x00000] in <12b418a7818c4ca0893feeaaf67f1e7f>:0 
  at MonoMod.Core.Platforms.Runtimes.MonoRuntime.Compile (System.Reflection.MethodBase method) [0x00008] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTriple.Compile (System.Reflection.MethodBase method) [0x00047] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTripleDetourFactory+Detour.CreateDetour () [0x00081] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTripleDetourFactory+DetourBase.Apply () [0x0005f] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.Platforms.PlatformTripleDetourFactory.CreateDetour (MonoMod.Core.CreateDetourRequest request) [0x00094] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.Core.DetourFactory.CreateDetour (MonoMod.Core.IDetourFactory factory, System.Reflection.MethodBase source, System.Reflection.MethodBase target, System.Boolean applyByDefault) [0x0001d] in <f7590871d33c41b3ab10c160d4e3b275>:0 
  at MonoMod.UnitTest.DetourTest+<>c__DisplayClass3_0.<Test_GenericClassDetourWithoutGenericParameter>b__0 () [0x00005] in <b34d1dbe2e9f4934b27f15b43d680fcd>:0 

Full results available here

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL