rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

`#pragma pack(1)` struct containing another packed struct is not packed itself.

Open reitermarkus opened this issue 3 years ago • 1 comments

Input C/C++ Header

#include <stdint.h>

#pragma pack(1)

typedef struct {
  uint64_t value;
} first_t;

typedef struct {
  first_t value;
} second_t;

#pragma pack()

Bindgen Invocation

$ bindgen input.h

Actual Results

#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct first_t {
    pub value: u64,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct second_t {
    pub value: first_t,
}

Expected Results

The second_t struct should be #[repr(C, packed)], not just #[repr(C)].

#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct first_t {
    pub value: u64,
}

#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
pub struct second_t {
    pub value: first_t,
}

reitermarkus avatar Apr 11 '22 19:04 reitermarkus

The way we detect packed structs with #pragma pack() is a bit hacky, and fails for this case:

https://github.com/rust-lang/rust-bindgen/blob/4d18f7606a2bc8a18b181b3e9639c9464a9dc896/src/ir/comp.rs#L1621-L1636

It's not particularly trivial to do better I think, but maybe libclang now exposes the relevant attributes?

emilio avatar Jun 05 '22 17:06 emilio