Skip to content

incorrect results for structs with tail packing on x86-64 Linux #983

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

Closed
froydnj opened this issue Sep 13, 2017 · 3 comments
Closed

incorrect results for structs with tail packing on x86-64 Linux #983

froydnj opened this issue Sep 13, 2017 · 3 comments

Comments

@froydnj
Copy link
Contributor

froydnj commented Sep 13, 2017

Input C/C++ Header

template<typename T>
class LinkedListElement
{
public:
  LinkedListElement();
  ~LinkedListElement();

private:
  LinkedListElement* next;
  LinkedListElement* prev;
  const bool isSentinel;
};

class SchedulerGroup : public LinkedListElement<SchedulerGroup>
{
public:
  SchedulerGroup();
  ~SchedulerGroup();

private:
  bool isRunning;
  unsigned long eventCount;
  void* targets[8];
  void* threads[8];

public:
  virtual void DoThings();
  virtual void DoOtherThings();
};

Bindgen Invocation

$ bindgen --no-derive-debug --no-derive-copy --generate types --rustfmt-bindings tail-packing.cpp

Actual Results

ERROR:bindgen::codegen::struct_layout: Calculated wrong layout for SchedulerGroup, too more 8 bytes
/* automatically generated by rust-bindgen */

#[repr(C)]
pub struct LinkedListElement {
    pub next: *mut LinkedListElement,
    pub prev: *mut LinkedListElement,
    pub isSentinel: bool,
}
#[repr(C)]
pub struct SchedulerGroup__bindgen_vtable(::std::os::raw::c_void);
#[repr(C)]
pub struct SchedulerGroup {
    pub vtable_: *const SchedulerGroup__bindgen_vtable,
    pub _base: LinkedListElement,
    pub isRunning: bool,
    pub eventCount: ::std::os::raw::c_ulong,
    pub targets: [*mut ::std::os::raw::c_void; 8usize],
    pub threads: [*mut ::std::os::raw::c_void; 8usize],
}
#[test]
fn bindgen_test_layout_SchedulerGroup() {
    assert_eq!(
        ::std::mem::size_of::<SchedulerGroup>(),
        168usize,
        concat!("Size of: ", stringify!(SchedulerGroup))
    );
    assert_eq!(
        ::std::mem::align_of::<SchedulerGroup>(),
        8usize,
        concat!("Alignment of ", stringify!(SchedulerGroup))
    );
    assert_eq!(
        unsafe { &(*(0 as *const SchedulerGroup)).isRunning as *const _ as usize },
        25usize,
        concat!(
            "Alignment of field: ",
            stringify!(SchedulerGroup),
            "::",
            stringify!(isRunning)
        )
    );
    assert_eq!(
        unsafe { &(*(0 as *const SchedulerGroup)).eventCount as *const _ as usize },
        32usize,
        concat!(
            "Alignment of field: ",
            stringify!(SchedulerGroup),
            "::",
            stringify!(eventCount)
        )
    );
    assert_eq!(
        unsafe { &(*(0 as *const SchedulerGroup)).targets as *const _ as usize },
        40usize,
        concat!(
            "Alignment of field: ",
            stringify!(SchedulerGroup),
            "::",
            stringify!(targets)
        )
    );
    assert_eq!(
        unsafe { &(*(0 as *const SchedulerGroup)).threads as *const _ as usize },
        104usize,
        concat!(
            "Alignment of field: ",
            stringify!(SchedulerGroup),
            "::",
            stringify!(threads)
        )
    );
}
#[test]fn __bindgen_test_layout_LinkedListElement_open0_SchedulerGroup_close0_instantiation ( ){
    assert_eq!(
        ::std::mem::size_of::<LinkedListElement>(),
        24usize,
        concat!(
            "Size of template specialization: ",
            stringify!(LinkedListElement)
        )
    );
    assert_eq!(
        ::std::mem::align_of::<LinkedListElement>(),
        8usize,
        concat!(
            "Alignment of template specialization: ",
            stringify!(LinkedListElement)
        )
    );
}

Expected Results

There should not be an error about the incorrect layout of SchedulerGroup; bindgen has failed to take into account that isRunning will be packed into the unused space of LinkedListElement. The generated test for the layout of SchedulerGroup will also fail as a result of this.

@emilio
Copy link
Contributor

emilio commented Sep 13, 2017

I believe the packing issue is #404.

@emilio
Copy link
Contributor

emilio commented Sep 13, 2017

Which is #380 itself.

@froydnj
Copy link
Contributor Author

froydnj commented Sep 13, 2017

Yeah, this is the same sort of issue as #380; not really obvious from #380's title, though. :)

@froydnj froydnj closed this as completed Sep 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants