Skip to content

alignas alignment is not respected on Linux 32-bit #917

Closed
@jryans

Description

@jryans

Input C/C++ Header

template<class T>
class Maybe
{
  alignas(T) unsigned char mStorage[sizeof(T)];
  char mIsSome;
};

struct Keyframe
{
  Maybe<double> mOffset;
  int           mComputedOffset = 1.0;
};

Bindgen Invocation

$ bindgen maybe.h -o maybe_bindings_32.rs --opaque-type Maybe -- -x c++ -std=c++14 -m32

Actual Results

/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Maybe {
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Keyframe {
    pub mOffset: [u64; 2usize],
    pub mComputedOffset: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_Keyframe() {
    assert_eq!(::std::mem::size_of::<Keyframe>() , 24usize , concat ! (
               "Size of: " , stringify ! ( Keyframe ) ));
    assert_eq! (::std::mem::align_of::<Keyframe>() , 8usize , concat ! (
                "Alignment of " , stringify ! ( Keyframe ) ));
    assert_eq! (unsafe {
                & ( * ( 0 as * const Keyframe ) ) . mOffset as * const _ as
                usize } , 0usize , concat ! (
                "Alignment of field: " , stringify ! ( Keyframe ) , "::" ,
                stringify ! ( mOffset ) ));
    assert_eq! (unsafe {
                & ( * ( 0 as * const Keyframe ) ) . mComputedOffset as * const
                _ as usize } , 16usize , concat ! (
                "Alignment of field: " , stringify ! ( Keyframe ) , "::" ,
                stringify ! ( mComputedOffset ) ));
}
impl Clone for Keyframe {
    fn clone(&self) -> Self { *self }
}

This layout test fails on Linux 32-bit:

$ rustc --test --target i686-unknown-linux-gnu maybe_bindings_32.rs
$ ./maybe_bindings_32

running 1 test
test bindgen_test_layout_Keyframe ... FAILED

failures:

---- bindgen_test_layout_Keyframe stdout ----
	thread 'bindgen_test_layout_Keyframe' panicked at 'assertion failed: `(left == right)` (left: `20`, right: `24`): Size of: Keyframe', maybe_bindings_32.rs:15
note: Run with `RUST_BACKTRACE=1` for a backtrace.


failures:
    bindgen_test_layout_Keyframe

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

because Rust believes the alignment should be 4 bytes. alignas is trying to mandate 8 bytes, but Rust doesn't seem to agree.

Expected Results

This is currently blocking Stylo from working on Linux 32-bit, see https://bugzilla.mozilla.org/show_bug.cgi?id=1366050#c49.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions