Skip to content

Fix passing/getting packed structs to/from foreign functions. #16584

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

Merged
merged 3 commits into from
Aug 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/librustc/middle/trans/cabi_x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,15 @@ fn classify_ty(ty: Type) -> Vec<RegClass> {
}

fn classify_struct(tys: &[Type],
cls: &mut [RegClass], i: uint,
off: uint) {
cls: &mut [RegClass],
i: uint,
off: uint,
packed: bool) {
let mut field_off = off;
for ty in tys.iter() {
field_off = align(field_off, *ty);
if !packed {
field_off = align(field_off, *ty);
}
classify(*ty, cls, i, field_off);
field_off += ty_size(*ty);
}
Expand Down Expand Up @@ -219,7 +223,7 @@ fn classify_ty(ty: Type) -> Vec<RegClass> {
unify(cls, ix + off / 8u, SSEDs);
}
Struct => {
classify_struct(ty.field_types().as_slice(), cls, ix, off);
classify_struct(ty.field_types().as_slice(), cls, ix, off, ty.is_packed());
}
Array => {
let len = ty.array_length();
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-make/extern-fn-with-packed-struct/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-include ../tools.mk

all:
$(CC) -std=c99 test.c -c -o $(TMPDIR)/test.o
$(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o
$(RUSTC) test.rs -L $(TMPDIR)
$(call RUN,test) || exit 1
11 changes: 11 additions & 0 deletions src/test/run-make/extern-fn-with-packed-struct/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Pragma needed cause of gcc bug on windows: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52991
#pragma pack(1)
struct __attribute__((packed)) Foo {
char a;
short b;
char c;
};

struct Foo foo(struct Foo foo) {
return foo;
}
30 changes: 30 additions & 0 deletions src/test/run-make/extern-fn-with-packed-struct/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[packed]
#[deriving(PartialEq, Show)]
struct Foo {
a: i8,
b: i16,
c: i8
}

#[link(name = "test", kind = "static")]
extern {
fn foo(f: Foo) -> Foo;
}

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