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

NativeAOT is crashing on static interface method #90333

Closed
hez2010 opened this issue Aug 10, 2023 · 4 comments · Fixed by #91374
Closed

NativeAOT is crashing on static interface method #90333

hez2010 opened this issue Aug 10, 2023 · 4 comments · Fixed by #91374
Assignees
Milestone

Comments

@hez2010
Copy link
Contributor

hez2010 commented Aug 10, 2023

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 10, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 10, 2023
@jkotas jkotas added area-NativeAOT-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Aug 10, 2023
@ghost
Copy link

ghost commented Aug 10, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: hez2010
Assignees: -
Labels:

untriaged, area-NativeAOT-coreclr, needs-area-label

Milestone: -

@jkotas
Copy link
Member

jkotas commented Aug 10, 2023

Static interface methods

@jkotas jkotas changed the title NativeAOT is crashing on PropertyInfo.GetValue without managed exception NativeAOT is crashing on static interface method Aug 10, 2023
@jkotas
Copy link
Member

jkotas commented Aug 12, 2023

Reduced repro:

IUnknown obj = new Impl();
Console.WriteLine(obj.M<IBar>());

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;

    Guid M<T>() where T : IComInterface
    {
        return GetIID<T>();
    }
}

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

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

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

partial struct Impl : IBaz
{
}

@jkotas
Copy link
Member

jkotas commented Aug 12, 2023

Fails in debug runtime with:

Unhandled Exception: System.Diagnostics.DebugProvider+DebugAssertException:    at System.Diagnostics.DebugProvider.Fail(String, String) + 0x36
   at System.Diagnostics.Debug.Fail(String, String) + 0x32
   at Internal.Runtime.TypeLoader.GenericDictionaryCell.NonGenericStaticConstrainedMethodCell.Create(TypeBuilder) + 0x60
   at Internal.Runtime.TypeLoader.GenericDictionaryCell.WriteCellIntoDictionary(TypeBuilder, IntPtr*, Int32) + 0x12
   at Internal.Runtime.TypeLoader.GenericDictionary.Finish(TypeBuilder) + 0x68
   at Internal.Runtime.TypeLoader.TypeBuilder.FinishTypeAndMethodBuilding() + 0x191
   at Internal.Runtime.TypeLoader.TypeBuilder.TryBuildGenericMethod(InstantiatedMethod, IntPtr&) + 0x35
   at Internal.Runtime.TypeLoader.TypeLoaderEnvironment.TryGetGenericVirtualMethodPointer(InstantiatedMethod, IntPtr&, IntPtr&) + 0x12b
   at Internal.Runtime.TypeLoader.TypeLoaderEnvironment.ResolveGenericVirtualMethodTarget(RuntimeTypeHandle, RuntimeMethodHandle) + 0xb3
   at System.Runtime.TypeLoaderExports.<>c.<GVMLookupForSlotSlow>b__13_0(IntPtr context, IntPtr signature, Object contextObject, IntPtr& auxResult) + 0x20
   at System.Runtime.TypeLoaderExports.CacheMiss(IntPtr, IntPtr, RuntimeObjectFactory, Object) + 0x3c
   at System.Runtime.TypeLoaderExports.GVMLookupForSlotSlow(Object, RuntimeMethodHandle) + 0x5f
   at System.Runtime.TypeLoaderExports.GVMLookupForSlot(Object, RuntimeMethodHandle) + 0xa0
   at Program.<Main>$(String[] args) + 0x27

@agocke agocke added this to the 8.0.0 milestone Aug 15, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 15, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 31, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Sep 1, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Oct 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants