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

Support more generics in callvirt handler #139

Merged
merged 2 commits into from
Mar 23, 2024

Conversation

puff
Copy link
Sponsor Contributor

@puff puff commented Mar 22, 2024

Callvirt doesn't support devirtualizing methods with generic parameters, or finding explicit method implementations using generics in.
Thus, this code will error:

var module = ModuleDefinition.FromModule(typeof(Usage).Module);
var method = module.GetAllTypes().First(x => x.Name == "Usage")?.Methods.First(x => x.Name == "Use");
var vm = new CilVirtualMachine(module, false);
vm.Invoker = DefaultInvokers.StepIn;

var thread = vm.CreateThread();
var ret = thread.Call(method!, Array.Empty<BitVector>());
Console.WriteLine(Usage.Use() == ret!.AsSpan().I32);
vm.DestroyThread(thread);

public interface ITestInterface<T>
{
    public T TestMethod<U>(T i, U u)
    {
        return i;
    }
}

public class TestClass<C> : ITestInterface<int>
{
    int ITestInterface<int>.TestMethod<U>(int i, U u)
    {
        return i;
    }
}

public static class Usage {
    public static int Use()
    {
        var tc = new TestClass<long>();
        var leet2 = ((ITestInterface<int>)tc).TestMethod<short>(1337, 2);
        return leet2;
    }
}

This PR fixes it by instantiating generics before comparison, and when returning the devirtualized result.
I also removed the baseMethod.IsVirtual check as it prevents the code from executing as well.
EDIT: Re-added the baseMethod.IsVirtual check, I got confused with it earlier :P

@Washi1337 Washi1337 merged commit ab38780 into Washi1337:master Mar 23, 2024
1 check passed
@Washi1337
Copy link
Owner

Thanks!

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.

None yet

2 participants