Skip to content

Packed structs are passed incorrectly to foreign functions on x86_64 #16574

Closed
@dotdash

Description

@dotdash

packed.c:

#include <stdio.h>

struct __attribute__((packed)) Foo {
    char a;
    short b;
    char c;
};

void foo(struct Foo foo) {
    printf("%d, %d, %d", foo.a, foo.b, foo.c);
}

ffi.rs

#[packed]
struct Foo {
    a: i8,
    b: i16,
    c: i8,
}

#[link(name = "packed")]
extern {
    fn foo(f: Foo);
}

fn main() {
    unsafe {
        foo(Foo {
            a: 1,
            b: 2,
            c: 3,
        });
    }
}

Output (different each time when compiled with optimizations):

-96, -32761, 88

This happens because the expected function signature is:

define void @foo(%struct.B* byval nocapture readonly align 8 %b) #0 

but rust generates:

declare void @foo({ i64 }) unnamed_addr #0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationO-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions