Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detouring methods that return Nullable<bool> crashes or results in a NRE #206

Open
pardeike opened this issue Dec 29, 2024 · 1 comment
Open
Labels

Comments

@pardeike
Copy link
Member

pardeike commented Dec 29, 2024

Description

This is a follow up from this bug report on Harmony: pardeike/Harmony#640

I cooked it down to a minimal case without Harmony or dynamic methods:

Example

// tested on Win 64bit, .NET 4.7.2
// but Harmony tests fail on different combinations

using MonoMod.Core;
using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        var original = typeof(SomeClass).GetMethod(nameof(SomeClass.Test), BindingFlags.Public | BindingFlags.Instance) ?? throw new NullReferenceException();
        var replacement = typeof(SomeClass).GetMethod(nameof(SomeClass.Replacement), BindingFlags.Public | BindingFlags.Static) ?? throw new NullReferenceException();
        Console.WriteLine($"Methods {original} {replacement}");
        DetourFactory.Current.CreateDetour(original, replacement);

        var instance = new SomeClass();
        instance.Test();

        Console.WriteLine("Done");
    }
}

public class SomeClass
{
    public static bool? Replacement(SomeClass instance)
    {
        _ = instance;
        Console.WriteLine("Replacement");
        return true;
    }

    public bool? Test()
    {
        Console.WriteLine("Test2");
        return false;
    }

    // comment out this to avoid the error
    public string s1 = "test";
}

Observations:

  • fields in the source method class do matter somehow (without the field, the bug does not appear)
  • making the replacement non-static does not exhibit the bug
  • changing the bool? to int? won't trigger the bug
@pardeike pardeike added the bug label Dec 29, 2024
@pardeike
Copy link
Member Author

For reference, these are the matrix tests that failed before I removed the test that tests the above case. You can see that it works on many platforms but not on all:

https://github.com/pardeike/Harmony/actions/runs/12431102708/job/34707941441

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant