Skip to content

bindgen doesn't handle mozilla::AlignedStorage correctly #868

Open
@upsuper

Description

@upsuper

Input C/C++ Header

typedef unsigned long long uint64_t;

template<typename T>
struct AlignedStorage2 {
  union U {
    char mBytes[sizeof(T)];
    uint64_t mDummy;
  } u;
};

struct Foo {
  uint64_t a;
  uint64_t b;
};

class Bar {
  AlignedStorage2<Foo> m;
};

Bindgen Invocation

$ bindgen input.hpp

Actual Results

#[repr(C)]
pub struct __BindgenUnionField<T>(::std::marker::PhantomData<T>);
impl <T> __BindgenUnionField<T> {
    #[inline]
    pub fn new() -> Self { __BindgenUnionField(::std::marker::PhantomData) }
    #[inline]
    pub unsafe fn as_ref(&self) -> &T { ::std::mem::transmute(self) }
    #[inline]
    pub unsafe fn as_mut(&mut self) -> &mut T { ::std::mem::transmute(self) }
}
impl <T> ::std::default::Default for __BindgenUnionField<T> {
    #[inline]
    fn default() -> Self { Self::new() }
}
impl <T> ::std::clone::Clone for __BindgenUnionField<T> {
    #[inline]
    fn clone(&self) -> Self { Self::new() }
}
impl <T> ::std::marker::Copy for __BindgenUnionField<T> { }
impl <T> ::std::fmt::Debug for __BindgenUnionField<T> {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        fmt.write_str("__BindgenUnionField")
    }
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct AlignedStorage2 {
    pub u: AlignedStorage2_U,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct AlignedStorage2_U {
    pub mBytes: __BindgenUnionField<*mut ::std::os::raw::c_char>,
    pub mDummy: __BindgenUnionField<u64>,
    pub bindgen_union_field: u64,
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Foo {
    pub a: u64,
    pub b: u64,
}
#[test]
fn bindgen_test_layout_Foo() {
    assert_eq!(::std::mem::size_of::<Foo>() , 16usize , concat ! (
               "Size of: " , stringify ! ( Foo ) ));
    assert_eq! (::std::mem::align_of::<Foo>() , 8usize , concat ! (
                "Alignment of " , stringify ! ( Foo ) ));
    assert_eq! (unsafe {
                & ( * ( 0 as * const Foo ) ) . a as * const _ as usize } ,
                0usize , concat ! (
                "Alignment of field: " , stringify ! ( Foo ) , "::" ,
                stringify ! ( a ) ));
    assert_eq! (unsafe {
                & ( * ( 0 as * const Foo ) ) . b as * const _ as usize } ,
                8usize , concat ! (
                "Alignment of field: " , stringify ! ( Foo ) , "::" ,
                stringify ! ( b ) ));
}
impl Clone for Foo {
    fn clone(&self) -> Self { *self }
}
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Bar {
    pub m: AlignedStorage2,
}
#[test]
fn bindgen_test_layout_Bar() {
    assert_eq!(::std::mem::size_of::<Bar>() , 16usize , concat ! (
               "Size of: " , stringify ! ( Bar ) ));
    assert_eq! (::std::mem::align_of::<Bar>() , 8usize , concat ! (
                "Alignment of " , stringify ! ( Bar ) ));
    assert_eq! (unsafe {
                & ( * ( 0 as * const Bar ) ) . m as * const _ as usize } ,
                0usize , concat ! (
                "Alignment of field: " , stringify ! ( Bar ) , "::" ,
                stringify ! ( m ) ));
}
impl Clone for Bar {
    fn clone(&self) -> Self { *self }
}
#[test]
fn __bindgen_test_layout_AlignedStorage2_open0_Foo_close0_instantiation() {
    assert_eq!(::std::mem::size_of::<AlignedStorage2>() , 16usize , concat ! (
               "Size of template specialization: " , stringify ! (
               AlignedStorage2 ) ));
    assert_eq!(::std::mem::align_of::<AlignedStorage2>() , 8usize , concat ! (
               "Alignment of template specialization: " , stringify ! (
               AlignedStorage2 ) ));
}

Expected Results

In the generated code above, the size of AlignedStorage2 is 8 bytes, but in the layout test, it asserts the size is 16 bytes, which is the correct size for the original C++ struct.

I'm not sure what should we do here. I guess it is possible for bindgen to generate the correct size for AlignedStorage2.

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