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

Marshalling strings with certain characters fails when inside a struct #1355

Closed
ducakar opened this issue Jan 7, 2020 · 2 comments
Closed

Comments

@ducakar
Copy link

ducakar commented Jan 7, 2020

Affected versions: .NET Core 3.1
OS: Arch Linux, Buildroot

Marshalling a struct containing a string field via P/Invoke fails when the string contains certain unicode characters. The following exception is thrown:

Unhandled exception. System.Runtime.InteropServices.COMException (0x8007007A): The data area passed to a system call is too small.
 (0x8007007A)
   at System.StubHelpers.ValueClassMarshaler.ConvertToNative(IntPtr dst, IntPtr src, IntPtr pMT, CleanupWorkListElement& pCleanupWorkList)
   at Program.foo(Label label)
   at Program.Main(String[] args) in /home/davorin/Razvoj/Concepts/Test/Program.cs:line 7

It seems the issue is only triggered when the string contains characters that occupy more than 2 bytes in UTF-16. It doesn't matter whether the struct is passed by value, by pointer or in an array.

Minimal test case:

using System.Runtime.InteropServices;

class Program
{
    static void Main()
    {
        foo(new Label { Text = "" });
    }

    [DllImport("foo")]
    static extern void foo(Label label);

    [StructLayout(LayoutKind.Sequential)]
    struct Label
    {
        public string Text;
    }
}
// Compile as a shared library:
// gcc -shared -o libfoo.so foo.c

struct Label {
  const char* text;
};

void foo(struct Label label) {
}
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-Interop-coreclr untriaged New issue has not been triaged by the area owner labels Jan 7, 2020
@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Jan 8, 2020
@jeffschwMSFT jeffschwMSFT added this to the 5.0 milestone Jan 8, 2020
@jeffschwMSFT
Copy link
Member

cc @jkoritzinsky

@jkoritzinsky
Copy link
Member

This is already fixed in master (validated on 3c0cea3). This was likely fixed as part of dotnet/coreclr#26340.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
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