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

Wrong size of class with virtual methods #3095

Open
kdanecki opened this issue Jan 17, 2025 · 0 comments
Open

Wrong size of class with virtual methods #3095

kdanecki opened this issue Jan 17, 2025 · 0 comments

Comments

@kdanecki
Copy link

input C++ header

class Base 
{
public:
      virtual void fun();
      int a;
};

class Derived : public Base
{
public:
      void fun() override;
      int x;
};

bindgen command

bindgen -o foo.rs foo.h -- -x c++

actual result

#[repr(C)]
pub struct Base__bindgen_vtable(::std::os::raw::c_void);
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Base {
    pub vtable_: *const Base__bindgen_vtable,
    pub a: ::std::os::raw::c_int,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of Base"][::std::mem::size_of::<Base>() - 16usize];
    ["Alignment of Base"][::std::mem::align_of::<Base>() - 8usize];
    ["Offset of field: Base::a"][::std::mem::offset_of!(Base, a) - 8usize];
};
unsafe extern "C" {
    #[link_name = "\u{1}_ZN4Base3funEv"]
    pub fn Base_fun(this: *mut ::std::os::raw::c_void);
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Derived {
    pub _base: Base,
    pub x: ::std::os::raw::c_int,
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
    ["Size of Derived"][::std::mem::size_of::<Derived>() - 16usize];
    ["Alignment of Derived"][::std::mem::align_of::<Derived>() - 8usize];
    ["Offset of field: Derived::x"][::std::mem::offset_of!(Derived, x) - 12usize];
};
unsafe extern "C" {
    #[link_name = "\u{1}_ZN7Derived3funEv"]
    pub fn Derived_fun(this: *mut ::std::os::raw::c_void);
}

compiler error

error[E0080]: evaluation of constant value failed
  --> foo.rs:29:5
   |
29 |     ["Size of Derived"][::std::mem::size_of::<Derived>() - 16usize];
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 8

description

When a class with virtual methods is derived, the fields of derived class can occupy padding of the Base class. But rust representation makes sizeof Derived == sizeof Base + sizeof all fields of derived.

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

1 participant