Skip to content

Bug with FieldOffset and marshaling by value #12226

@muntyan

Description

@muntyan

Attached is a repro for where a struct with Explicit layout and FieldOffsets modeling a C union gets marshaled as garbage to C through p/invoke on linux and mac, while it works as expected on windows. I haven't tried to figure out what exactly breaks it, but the sample should be simple enough. It assumes 64-bit linux.

C code is like this:

    struct Repro
    {
        int discriminator;
        union
        {
            int integer;
            void* pointer;
            char* string;
         };
    };
void consume(struct Repro repro); // passing the struct by value

and C# binding is like this:

        [StructLayout(LayoutKind.Explicit)]
        public struct Repro
        {
            [FieldOffset(0)]
            public int discriminator;
            [FieldOffset(8)]
            public int integer;
            [FieldOffset(8)]
            public IntPtr string_;
            [FieldOffset(8)]
            public IntPtr pointer;
        }

        [DllImport("repro")]
        public static extern void consume(Repro value);

It results in garbage passed to C code, see attached. It outputs

    discriminator: 497673040
    pointer: 0x7f7cec7f5818

instead of expected 8 and 42.

To reproduce unpack the attachment and run ./repro.sh in it.

repro.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions