rust-lang / rust-lang/rust-bindgen

bindgen doesn't handle mozilla::AlignedStorage correctly

Open
#868 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-templates E-hard I-failing-layout-test
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the case with the C++ header shown in input.hpp and the bindgen input.hpp invocation. Compare the generated AlignedStorage2 definition with __bindgen_test_layout_AlignedStorage2_open0_Foo_close0_instantiation; done means the generated layout represents the C++ size of 16 bytes and the layout test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.