Skip to content

GetFunctionPointerForDelegateTests tests fail with Specified cast is not valid on Browser #39187

Open

Description

@mdh1418

When running System.Runtime.InteropServices library tests on Browser wasm, two functions fail with Specified cast is not valid:

  • GetFunctionPointerForDelegate_MarshalledDelegateNonGeneric_ReturnsExpected
  • GetFunctionPointerForDelegate_MarshalledDelegateGeneric_ReturnsExpected

Note

When the tests are not ordered, the suite sometimes fails with the two tests above

[FAIL] System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests.GetFunctionPointerForDelegate_MarshalledDelegateNonGeneric_ReturnsExpected
System.InvalidCastException : Specified cast is not valid.
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[FAIL] System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests.GetFunctionPointerForDelegate_MarshalledDelegateGeneric_ReturnsExpected
System.InvalidCastException : Specified cast is not valid.
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

And other times, it fails with the six tests from GetDelegateForFunctionPointerTests.cs

[FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_NonGeneric_ReturnsExpected(t: typeof(System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+NonGenericDelegate))
Assert.IsType() Failure
Expected: System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+NonGenericDelegate
Actual: System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests+NonGenericDelegate
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_NonGeneric_ReturnsExpected(t: typeof(System.MulticastDelegate))
Assert.IsType() Failure
Expected: System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+NonGenericDelegate
Actual: System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests+NonGenericDelegate
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_NonGeneric_ReturnsExpected(t: typeof(System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+OtherNonGenericDelegate))
Assert.IsType() Failure
Expected: System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+NonGenericDelegate
Actual: System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests+NonGenericDelegate
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_GenericInvalidType_ReturnsExpected
Assert.IsType() Failure
Expected: System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+NonGenericDelegate
Actual: System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests+NonGenericDelegate
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_CollectibleType_ReturnsExpected
Assert.IsType() Failure
Expected: System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+NonGenericDelegate
Actual: System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests+NonGenericDelegate
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_Generic_ReturnsExpected
System.InvalidCastException : Specified cast is not valid.
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 

Activity

  1. added this to the 5.0.0 milestone on Jul 14, 2020
  2. vargaz commented on Jul 31, 2020

    @vargaz
    Contributor

    These now fail with:
    System.PlatformNotSupportedException : No native to managed transitions on this platform.
    They cannot be supported without the pinvoke generator.

  3. lewing commented on Aug 1, 2020

    @lewing
    Member

    @vargaz this sounds like net6?

  4. vargaz commented on Aug 1, 2020

    @vargaz
    Contributor

    Probably.

  5. 12 remaining items

  6. modified the milestones: 7.0.0, 8.0.0 on Aug 12, 2022
  7. lewing commented on Jul 11, 2023

    @lewing
    Member

    @ilonatommy can you check if these tests still fail?

  8. ilonatommy commented on Jul 11, 2023

    @ilonatommy
    Member

    The error message changed to:

    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_CantCast_ThrowsInvalidCastException
       System.PlatformNotSupportedException : No native to managed transition for method 'void System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests:Method (string)', missing [UnmanagedCallersOnly] attribute.
      at System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(Delegate d)
      at System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_CantCast_ThrowsInvalidCastException()
      at System.Reflection.MethodInvoker.InterpretedInvoke(Object obj, IntPtr* args)
      at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
    

    When we use the hint and add the decorator:

            [UnmanagedCallersOnly]
            private static void Method(string s) { }
    

    GetFunctionPointerForDelegateTests.cs(124,36): error CS8894: Cannot use 'string' as a parameter type on a method attributed with 'UnmanagedCallersOnly'

    When we change the method to receive char* in unsafe context, the message is back to missing [UnmanagedCallersOnly], so apparently I am not using it correctly.

    I am not sure how to fix this.

    Tests failing with this message:

    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_CantCast_ThrowsInvalidCastException
    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_GenericInvalidType_ReturnsExpected
    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_NonGeneric_ReturnsExpected(t: typeof(System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+NonGenericDelegate))
    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_NonGeneric_ReturnsExpected(t: typeof(System.MulticastDelegate))
    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_NonGeneric_ReturnsExpected(t: typeof(System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests+OtherNonGenericDelegate))
    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_Generic_ReturnsExpected
    [FAIL] System.Runtime.InteropServices.Tests.GetDelegateForFunctionPointerTests.GetDelegateForFunctionPointer_CollectibleType_ReturnsExpected
    [FAIL] System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests.GetFunctionPointerForDelegate_MarshalledDelegateGeneric_ReturnsExpected
    [FAIL] System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests.GetFunctionPointerForDelegate_NormalDelegateGeneric_ReturnsExpected
    [FAIL] System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests.GetFunctionPointerForDelegate_MarshalledDelegateNonGeneric_ReturnsExpected
    [FAIL] System.Runtime.InteropServices.Tests.GetFunctionPointerForDelegateTests.GetFunctionPointerForDelegate_NormalDelegateNonGeneric_ReturnsExpected
    
  9. lewing commented on Jul 21, 2023

    @lewing
    Member

    @vargaz is this something we expect to work?

  10. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 21, 2023
  11. modified the milestones: 8.0.0, 9.0.0 on Aug 13, 2023
  12. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jan 26, 2024
  13. lewing commented on Feb 21, 2024

    @lewing
    Member
  14. modified the milestones: 9.0.0, Future on Apr 25, 2024
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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions