Skip to content

MonoVM: Type.GetInterfaceMap may return null TargetMethods entries #53933

Closed

Description

@rolfbjarne

Description

Type.GetInterfaceMap returns null entries for TargetMethods sometimes.

Test case:
consoleapp-3c50b29.zip

Repro:

  • Install .NET 6.0.100-preview.6.21308.62
  • Download & extract test case
  • Run the run-with-mono.sh script
$ ./run-with-mono.sh 
+ DOTNET=dotnet
+ dotnet publish -r osx-x64 /p:UseMonoRuntime=true
Microsoft (R) Build Engine version 17.0.0-preview-21304-01+a5ea6d2ca for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /Users/rolf/test/dotnet/consoleapp/myproject.csproj (in 93 ms).
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  myproject -> /Users/rolf/test/dotnet/consoleapp/bin/Debug/net6.0/osx-x64/myproject.dll
  myproject -> /Users/rolf/test/dotnet/consoleapp/bin/Debug/net6.0/osx-x64/publish/
+ MONO_ENV_OPTIONS=--version
+ ./bin/Debug/net6.0/osx-x64/publish/myproject
Mono JIT compiler version 6.0.0.0 (6.0.21.30601 @Commit: 5b8e1780ad7740e632c285bc2af13a8d0b20c5a9)
Copyright (C) Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           
	SIGSEGV:       normal
	Notification:  kqueue
	Architecture:  amd64
	Disabled:      
	Misc:          softdebug 
	Interpreter:   yes
	LLVM:          supported, not enabled.
	Suspend:       hybrid
	GC:            sgen (concurrent by default)
+ ./bin/Debug/net6.0/osx-x64/publish/myproject Xamarin.Mac.dll
Running on .NET 6.0.0-preview.6.21306.1
Loading: /Users/rolf/test/dotnet/consoleapp/Xamarin.Mac.dll
Got interface map for type CoreBluetooth.CBCentralManagerDelegate and interface CoreBluetooth.ICBCentralManagerDelegate with 1 entries:
    1 Void UpdatedState(CoreBluetooth.CBCentralManager) -> null

Compare to when running with .NET:

$ dotnet run *.dll
Running on .NET 6.0.0-preview.6.21306.1
Loading: /Users/rolf/test/dotnet/consoleapp/Xamarin.Mac.dll
Got interface map for type CoreBluetooth.CBCentralManagerDelegate and interface CoreBluetooth.ICBCentralManagerDelegate with 1 entries:
    1 Void UpdatedState(CoreBluetooth.CBCentralManager) -> Void UpdatedState(CoreBluetooth.CBCentralManager)

Configuration

.NET 6.0.100-preview.6.21308.62

Regression?

Yes (since 6.0.100-preview.6.21280.2).

Other information

Our tests ran into this when bumping to the latest .NET version (dotnet/macios#11852).

Looking at the dotnet/runtime commits since it worked, there's a possible cause here: 5d64814 (CC @bholmes).

Activity

  1. ghost added
    untriaged
    on Jun 9, 2021
  2. added this to the 6.0.0 milestone on Jun 9, 2021
  3. akoeplinger commented on Jun 9, 2021

    @akoeplinger
    Member

    /cc @lambdageek looks like a regression most likely from #52566

  4. CoffeeFlux commented on Jun 9, 2021

    @CoffeeFlux
    Contributor
  5. removed
    untriaged
    on Jun 9, 2021
  6. marek-safar commented on Jun 9, 2021

    @marek-safar
    Contributor
  7. self-assigned this
    on Jun 9, 2021
  8. lambdageek commented on Jun 9, 2021

    @lambdageek
    Member

    Here's the repro (.NET 6 Preview 4, using CoreCLR):

    using System;
    
    interface I {
        public void M ();
    }
    
    abstract class C : I {
        public abstract void M ();
    }
    
    interface ID {
        public void M () { /* I have a DIM */ Console.WriteLine ("ID"); }
    }
    
    abstract class CD : ID  {
        public abstract void M ();
    }
    
    class H : CD {
        public override void M () { Console.WriteLine ("H"); }
    }
    
    interface I3 : ID {
        abstract void ID.M (); // reabstracted
    }
    
    abstract class J : I3 {
        public abstract void M ();
    }
    
    namespace hark
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
    
    	    var inputs = new (Type,Type)[] { (typeof(C), typeof(I)), (typeof(CD), typeof(ID)),
    		    (typeof (J), typeof (ID)), (typeof (J), typeof (I3))};
    
    	    foreach (var (type,iface) in inputs) {
    		dumpIfaces (type, iface);
    	    }
    	    
    
    	    (new H() as ID).M();
            }
    
    	public static void dumpIfaces (Type type, Type iface) {
    	    var map = type.GetInterfaceMap (iface);
    
    	    Console.WriteLine ($"Got interface map for type {type} and interface {iface} with {map.InterfaceMethods.Length} entries:");
    	    for (int i = 0; i < map.InterfaceMethods.Length; i++) {
    		Console.WriteLine ($"    {i + 1} {map.InterfaceMethods [i]} -> {(map.TargetMethods [i] == null ? "null" : map.TargetMethods [i])}");
    		}
    	}
        }
    }

    Should Print

    Hello World!
    Got interface map for type C and interface I with 1 entries:
        1 Void M() -> Void M()
    Got interface map for type CD and interface ID with 1 entries:
        1 Void M() -> Void M()
    Got interface map for type J and interface ID with 1 entries:
        1 Void M() -> Void M()
    Got interface map for type J and interface I3 with 1 entries:
        1 Void ID.M() -> null
    H
    

    Mono prints

    Hello World!
    Got interface map for type C and interface I with 1 entries:
        1 Void M() -> null
    Got interface map for type CD and interface ID with 1 entries:
        1 Void M() -> null
    Got interface map for type J and interface ID with 1 entries:
        1 Void M() -> null
    Got interface map for type J and interface I3 with 1 entries:
        1 Void ID.M() -> null
    H
    
  9. bholmes commented on Jun 9, 2021

    @bholmes
    Contributor

    I suspect that I did not cover the case when the found target method is abstract, but is on an a class.

    I think a check needs to be added here to ensure that the found method is an interface.

    However I am still trying to understand why the 4th case provided above returns null.

  10. bholmes commented on Jun 9, 2021

    @bholmes
    Contributor

    @bholmes I'm unclear in what scenario with reabstraction you want to return NULL in #52566 ?

    Lines 106 and 111.

    if (declMethod.Equals(ifooDefaultMethod))
    {
    foundMatchMask |= 1;
    CheckEqual(ref failed, implMethod, null);
    }
    else if (declMethod.Equals(ifooOtherMethod))
    {
    foundMatchMask |= 2;
    CheckEqual(ref failed, implMethod, null);
    }

  11. lambdageek commented on Jun 9, 2021

    @lambdageek
    Member

    @bholmes here's what I have so far

    diff --git a/src/mono/mono/metadata/icall.c b/src/mono/mono/metadata/icall.c
    index cddaa2171f1..9f43cf045bd 100644
    --- a/src/mono/mono/metadata/icall.c
    +++ b/src/mono/mono/metadata/icall.c
    @@ -2525,6 +2525,22 @@ fail:
     	return NULL_HANDLE_ARRAY;
     }
     
    +static gboolean
    +method_is_reabstracted (MonoMethod *method)
    +{
    +	/* only on interfaces */
    +	/* method is marked "virtual abstract" */
    +	return method->flags & METHOD_ATTRIBUTE_FINAL && method->flags & METHOD_ATTRIBUTE_ABSTRACT;
    +}
    +
    +static gboolean
    +method_is_dim (MonoMethod *method)
    +{
    +	/* only valid on interface methods*/
    +	/* method is marked "virtual final" but not "virtual final abstract" */
    +	return method->flags & METHOD_ATTRIBUTE_FINAL && method->flags & METHOD_ATTRIBUTE_VIRTUAL && !(method->flags & METHOD_ATTRIBUTE_ABSTRACT);
    +}
    +
     static gboolean
     set_interface_map_data_method_object (MonoMethod *method, MonoClass *iclass, int ioffset, MonoClass *klass, MonoArrayHandle targets, MonoArrayHandle methods, int i, MonoError *error)
     {
    @@ -2558,7 +2574,13 @@ set_interface_map_data_method_object (MonoMethod *method, MonoClass *iclass, int
     		}
     	}
     
    -	if (foundMethod->flags & METHOD_ATTRIBUTE_ABSTRACT)
    +	/* if the iterface method is reabstracted, and
    +	 * either the found implementation method is abstract, or
    +	 * the found implementation method is from another DIM (meaning klass or any of its parents implemented the method),
    +	 * then say the target method is null. */
    +	if (method_is_reabstracted (method) &&
    +	    ((foundMethod->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
    +	     (mono_class_is_interface (foundMethod->klass) && method_is_dim (foundMethod))))
     		MONO_HANDLE_ARRAY_SETREF (targets, i, NULL_HANDLE);
     	else {
     		MONO_HANDLE_ASSIGN (member, mono_method_get_object_handle (foundMethod, mono_class_is_interface (foundMethod->klass) ? foundMethod->klass : klass, error));
    -- 

    I'm trying to add some tests to the testsuite to cover this.

    There's another case that is also annoying (and responsible for the method_is_dim case above):

    interface ID {
        public void M () { /* I have a DIM */ Console.WriteLine ("ID"); }
    }
    
    abstract class CD : ID  {
        public abstract void M ();
    }
    
    class H : CD {
        public override void M () { Console.WriteLine ("H"); }
    }
    
    interface I3 : ID {
        abstract void ID.M (); // reabstracted
    }
    
    abstract class J : I3 {
        public abstract void M ();
    }
    
    interface I4 : I3 {
        void ID.M () { // unabstracted
    	Console.WriteLine ("I4");
        }
    }
    
    class K : I4 { }
    
    interface I5 : I4 {
        abstract void ID.M ();  // reabstracted again!
    }
    
    abstract class L : I5 { public abstract void M (); }

    Calling GetInterfaceMap on L with interfaces I5 or I3 the target method is null. Calling it on L with I4 returns the unabstracted method from I4, calling it on L with ID returns the method from L.

  12. 3 remaining items

  13. ghost removed
    in-pr
    on Jun 15, 2021
  14. lambdageek commented on Jun 15, 2021

    @lambdageek
    Member

    Need to fix this in mono/mono, too

  15. ghost locked as resolved and limited conversation to collaborators on Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions