rust-lang / rust-lang/rust-bindgen

Padding not added if we use a typedef to a wrongly-sized replacement type

Open
#3,158 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Reproduction:

/**
* <div rustbindgen="true" replaces="std::string">
*/
class CxxString {
    char* ptr;
};
#include <string>
#include <cstdint>
typedef std::string my_string;
struct A {
   my_string a;
};
struct B {
   std::string a;
};

Command:

cargo run -- test.hpp --no-layout-tests --allowlist-type A --allowlist-type B --enable-cxx-namespaces

Bindgen version 20aa65a0b9edfd5f8ab3e038197da5cb2c52ff18 (today's main).

Rust generated:

/* automatically generated by rust-bindgen 0.71.1 */

#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
    #[allow(unused_imports)]
    use self::super::root;
    pub mod std {
        #[allow(unused_imports)]
        use self::super::super::root;
        #[doc = " <div rustbindgen=\"true\" replaces=\"std::string\">"]
        #[repr(C)]
        #[derive(Debug, Copy, Clone)]
        pub struct string {
            pub ptr: *mut ::std::os::raw::c_char,
        }
    }
    pub type my_string = root::std::string;
    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    pub struct A {
        pub a: root::my_string,
    }
    #[repr(C)]
    #[derive(Debug, Copy, Clone)]
    pub struct B {
        pub a: root::std::string,
        pub __bindgen_padding_0: [u64; 2usize],
    }
}

Note that struct B contains padding, but struct A doesn't. This means the size of struct A is wrong.

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 issue with the test.hpp example and the shown cargo run command, comparing the generated layouts for A and B. Start by tracing bindgen's handling of typedef aliases and replacement types in layout generation; done means struct A receives the padding needed to match its C++ size.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.