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

incorrect handling of [repr(packed)] and [repr(align(n)] struct attributes #3260

Closed
nobel-sh opened this issue Nov 22, 2024 · 1 comment · Fixed by #3272
Closed

incorrect handling of [repr(packed)] and [repr(align(n)] struct attributes #3260

nobel-sh opened this issue Nov 22, 2024 · 1 comment · Fixed by #3272

Comments

@nobel-sh
Copy link
Contributor

In Rust, the #[repr(packed)] attribute is intended to remove padding between struct fields. Similarly, #[repr(align(n))] is used to adjust the alignment of a struct to n bytes. However, in the case of gccrs, it appears that these attributes are not fully respected.

// uncomment commented code to run with gccrs

// #![feature(intrinsics)]
// #[lang = "sized"]
// pub trait Sized {}

// extern "rust-intrinsic" {
//     fn size_of<T>() -> usize;
// }

extern "C"{
    fn printf(format: *const i8, ...);
}


#[repr(packed)]
struct StructPacked {
    flag: bool,
    number: i32,
    data: [u8; 16],
}

#[repr(align(16))]
struct StructAligned {
    flag: bool,
    number: i32,
    data: [u8; 16],
}

fn main(){
    let matrix = [[1.0; 4]; 4];
    let data = [0u8; 16];

    let struct_packed = StructPacked {
        flag: true,
        number: 42,
        data,
    };

    let struct_aligned = StructAligned {
        flag: true,
        number: 42,
        data,
    };

    println!("StructPacked: {}", size_of::<StructPacked>());
    println!("StructAligned: {}", size_of::<StructAligned>());

    // uncomment below for gccrs

    // unsafe{
    //     printf(
    //         "StructPacked: %zu\n\0" as *const str as *const i8,
    //         size_of::<StructPacked>(),
    //     );
    //     printf(
    //         "StructAligned: %zu\n\0" as *const str as *const i8,
    //         size_of::<StructAligned>(),
    //     );
    // }
}

rustc:

StructPacked: 21
StructAligned: 32

gccrs:

StructPacked: 24
StructAligned: 24

godbolt: https://godbolt.org/z/rornPra3c

@philberty
Copy link
Member

I wonder is the bool type throwing off our packed handling iirc I don't know if we have added the aligned support in or not will need to check

@philberty philberty self-assigned this Nov 26, 2024
@philberty philberty added this to the Remaining typecheck issues milestone Nov 26, 2024
philberty added a commit that referenced this issue Nov 26, 2024
We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes #3260

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
	* rust-backend.h (struct_type): add optional layout parameter
	(union_type): likewise
	(fill_in_fields): likewise
	* rust-gcc.cc (struct_type): likewise
	(union_type): likewise
	(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <[email protected]>
philberty added a commit that referenced this issue Nov 26, 2024
We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes #3260

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
	* rust-backend.h (struct_type): add optional layout parameter
	(union_type): likewise
	(fill_in_fields): likewise
	* rust-gcc.cc (struct_type): likewise
	(union_type): likewise
	(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <[email protected]>
philberty added a commit that referenced this issue Nov 27, 2024
We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes #3260

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
	* rust-backend.h (struct_type): add optional layout parameter
	(union_type): likewise
	(fill_in_fields): likewise
	* rust-gcc.cc (struct_type): likewise
	(union_type): likewise
	(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <[email protected]>
github-merge-queue bot pushed a commit that referenced this issue Nov 27, 2024
We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes #3260

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
	* rust-backend.h (struct_type): add optional layout parameter
	(union_type): likewise
	(fill_in_fields): likewise
	* rust-gcc.cc (struct_type): likewise
	(union_type): likewise
	(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <[email protected]>
@github-project-automation github-project-automation bot moved this from Todo to Done in libcore 1.49 Nov 27, 2024
tschwinge pushed a commit that referenced this issue Dec 4, 2024
We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes #3260

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
	* rust-backend.h (struct_type): add optional layout parameter
	(union_type): likewise
	(fill_in_fields): likewise
	* rust-gcc.cc (struct_type): likewise
	(union_type): likewise
	(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <[email protected]>
Kamiinarii78 pushed a commit to Kamiinarii78/gccrs that referenced this issue Dec 12, 2024
We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes Rust-GCC#3260

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
	* rust-backend.h (struct_type): add optional layout parameter
	(union_type): likewise
	(fill_in_fields): likewise
	* rust-gcc.cc (struct_type): likewise
	(union_type): likewise
	(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <[email protected]>
matthewjasper pushed a commit to matthewjasper/gccrs that referenced this issue Dec 21, 2024
We cannot apply aligned or packed after layout_type is called you need
to set this up first then call it.

Fixes Rust-GCC#3260

gcc/rust/ChangeLog:

	* backend/rust-compile-type.cc (TyTyResolveCompile::visit): call lauout type directly
	* rust-backend.h (struct_type): add optional layout parameter
	(union_type): likewise
	(fill_in_fields): likewise
	* rust-gcc.cc (struct_type): likewise
	(union_type): likewise
	(fill_in_fields): only layout if we required

Signed-off-by: Philip Herron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants