Skip to content

NativeAOT is crashing on static interface method #90333

Closed
@hez2010

Description

@hez2010

Description

On .NET 8 Preview 7.

Repro:

using System.Diagnostics.CodeAnalysis;
using System.Reflection;

IUnknown obj = new Impl();
if (obj.QueryInterface<IBar>(out var bar)) bar.Bar();
if (obj.QueryInterface<IFoo>(out var foo)) foo.Foo();

interface IComInterface
{
    abstract static Guid IID { get; }
}

interface IUnknown : IComInterface
{
    static Guid IComInterface.IID => new("00000000-0000-0000-C000-000000000046");
    static Guid GetIID<TVtbl>() where TVtbl : IComInterface => TVtbl.IID;

    [UnconditionalSuppressMessage("ILLink", "IL2111")]
    [UnconditionalSuppressMessage("ILLink", "IL2075")]
    bool QueryInterface<
        [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.NonPublicProperties)] T
        >([NotNullWhen(true)] out T? result) where T : IComInterface
    {
        foreach (var p in GetType()
            .GetInterfaces()
            .SelectMany(([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicProperties)] i)
                => i.GetProperty("IComInterface.IID", BindingFlags.NonPublic | BindingFlags.Static)
                    is PropertyInfo propertyInfo ? [propertyInfo] : Array.Empty<PropertyInfo>()))
        {
            if (p.GetValue(this) is Guid guid && guid == GetIID<T>())
            {
                result = (T)this;
                return true;
            }
        }
        result = default;
        return false;
    }
}

interface IFoo : IComInterface, IUnknown
{
    static Guid IComInterface.IID => new("12345678-1234-1234-1234-1234567890AB");
    void Foo();
}

interface IBar : IComInterface, IUnknown
{
    static Guid IComInterface.IID => new("12345678-1234-1234-1234-1234567890CD");
    void Bar();
}

interface IBaz : IComInterface, IFoo, IBar
{
    static Guid IComInterface.IID => new("12345678-1234-1234-1234-1234567890EF");
}

partial struct Impl : IBaz
{
    public void Foo() => Console.WriteLine("foo");
    public void Bar() => Console.WriteLine("bar");
}

Reproduction Steps

Build the source with NativeAOT and run it.

Expected behavior

No crashing.

Actual behavior

Crashing.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions