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

[Regression] Invalid program when marhsalling delegate with both value type return and parameter #45637

Closed
ceztko opened this issue Dec 5, 2020 · 7 comments

Comments

@ceztko
Copy link

ceztko commented Dec 5, 2020

Description

I'm receiving an System.InvalidProgramException with message "Common Language Runtime detected an invalid program" when trying to marshal an UnmanagedFunctionPointer delegate that has both return and parameters that are value types. This happens only on x86 build with .NET core runtime. It works with x64 build or using .NET Framework runtime.

The minimal program to reproduce is with the following on the C# side:

using System;
using System.Runtime.InteropServices;

namespace TestDelegateValueTypeReturnInterop
{
    class Program
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        delegate ValueType ValueTypeReturnDelegate(ValueType p1);

        static ValueTypeReturnDelegate Callback;

        static Program()
        {
            Callback = callback;
        }

        static void Main(string[] args)
        {
            Foo(Callback);
        }

        static ValueType callback(ValueType p1)
        {
            return new ValueType();
        }

        [DllImport("DelegateValueTypeReturnNative", CallingConvention = CallingConvention.Cdecl)]
        static extern void Foo(ValueTypeReturnDelegate func);
    }

    [StructLayout(LayoutKind.Sequential)]
    struct ValueType
    {
        IntPtr F1;
        UIntPtr F2;
    }
}

and this on C++ side:

#include <cstdint>

using namespace std;

extern "C"
{
    struct ValueType
    {
        intptr_t F1;
        uintptr_t F2;
    };

    typedef ValueType(*ValueTypeReturnDelegate)(ValueType p1);

    __declspec(dllexport) void Foo(ValueTypeReturnDelegate func)
    {
        auto v = func({ });
    }
}

The program is also attached with a full standalone solution. Running it with x86 platform ( (Debug or Release is the same) fails with "TestDelegateValueTypeReturnInteropCore" and and works "TestDelegateValueTypeReturnInteropNet452" projects. On x64 platform the program works on both runtimes.

TestDelegateValueTypeReturnInterop.zip

Configuration

  • .NET Core 3.1.404
  • x86
  • Windows 10 2004 29041.630

Regression?

The problem is a regression, the same program works on .NET Framework both on x86 and x64. Testing with 4.8 (release 528372).

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-Interop-coreclr untriaged New issue has not been triaged by the area owner labels Dec 5, 2020
@jkotas
Copy link
Member

jkotas commented Dec 5, 2020

cc @jkoritzinsky

@AaronRobinsonMSFT
Copy link
Member

This doesn't crash on .NET 5, but does exhibit some unusual behavior. Once we fully root cause this we can make a servicing decision.

@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 6.0.0 milestone Dec 8, 2020
@AaronRobinsonMSFT AaronRobinsonMSFT removed the untriaged New issue has not been triaged by the area owner label Dec 8, 2020
@AaronRobinsonMSFT
Copy link
Member

Actually this is fine in .NET 5 - GA. In our main branch there is an issue due to some refactoring, but will be fixed by @jkoritzinsky's PR #46238.

I have been able to reproduce this locally on .NET Core 3.1 and I think (thanks @jkoritzinsky for helping root cause) this may be fixed by #35026. I will verify this locally.

@AaronRobinsonMSFT
Copy link
Member

Thanks @jkoritzinsky for the stellar memory! This was found and fixed in .NET 5, but never back ported.

@AaronRobinsonMSFT
Copy link
Member

@ceztko Thank you for reporting this. Given this is for 3.1 we need a strong customer case. Are you completely blocked by this issue? Is it possible to upgrade to and run on .NET 5.0?

@ceztko
Copy link
Author

ceztko commented Jan 7, 2021

Thank you very much for taking care of this issue! Fortunately for me, I have no imminent use case. I stumbled in this issue in a big testing advanced interop strategies for strings and thought it was important to report. Feel free to stand by with the back port until an user with relevant use case replies here (if ever).

@AaronRobinsonMSFT
Copy link
Member

@ceztko Thanks for the reply. Since this isn't blocking anyone at the moment I will close this issue. The issue will be searchable and since we now know the fix will be easy to apply if it becomes a blocking high priority concern.

@AaronRobinsonMSFT AaronRobinsonMSFT modified the milestones: 6.0.0, Future Jan 7, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Feb 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants