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

Debug trait is derived when a struct contains __bindgen_padding #648

Closed
leeopop opened this issue Apr 20, 2017 · 4 comments
Closed

Debug trait is derived when a struct contains __bindgen_padding #648

leeopop opened this issue Apr 20, 2017 · 4 comments

Comments

@leeopop
Copy link

leeopop commented Apr 20, 2017

Input C/C++ Header

/** The data structure associated with each crypto device. */
struct rte_cryptodev {
        dequeue_pkt_burst_t dequeue_burst;
        /**< Pointer to PMD receive function. */
        enqueue_pkt_burst_t enqueue_burst;
        /**< Pointer to PMD transmit function. */

        const struct rte_cryptodev_driver *driver;
        /**< Driver for this device */
        struct rte_cryptodev_data *data;
        /**< Pointer to device data */
        struct rte_cryptodev_ops *dev_ops;
        /**< Functions exported by PMD */
        uint64_t feature_flags;
        /**< Supported features */
        struct rte_device *device;
        /**< Backing device */

        enum rte_cryptodev_type dev_type;
        /**< Crypto device type */

        struct rte_cryptodev_cb_list link_intr_cbs;
        /**< User application callback for interrupts if present */

        __extension__
        uint8_t attached : 1;
        /**< Flag indicating the device is attached */
} __rte_cache_aligned;

__rte_cache_aligned is 64byte alignment.

Bindgen Invokation

    bindgen::builder()
        .header(header_path.to_str().unwrap())
        .no_unstable_rust()
        .clang_arg(format!("-I{}", dpdk_include_path.to_str().unwrap()))
        .clang_arg(format!("-I{}", c_include_path.to_str().unwrap()))
        .clang_arg("-imacros")
        .clang_arg(dpdk_config_path.to_str().unwrap())
        .clang_arg("-march=native")
        .generate()
        .unwrap()
        .write_to_file(target_path)
        .ok();

Actual Results

#[repr(C)]
#[derive(Debug, Copy)]
pub struct rte_cryptodev {
    pub dequeue_burst: dequeue_pkt_burst_t,
    pub enqueue_burst: enqueue_pkt_burst_t,
    pub driver: *const rte_cryptodev_driver,
    pub data: *mut rte_cryptodev_data,
    pub dev_ops: *mut rte_cryptodev_ops,
    pub feature_flags: u64,
    pub device: *mut rte_device,
    pub dev_type: rte_cryptodev_type,
    pub link_intr_cbs: rte_cryptodev_cb_list,
    pub _bitfield_1: u8,
    pub __bindgen_padding_0: [u8; 47usize],
}
   Compiling bindgen v0.23.1 (https://github.com/servo/rust-bindgen.git?rev=f775f1f#f775f1f2)
   Compiling rust-dpdk v0.0.1-dev (file:///home/leeopop/git/rust-dpdk)
error[E0277]: the trait bound `[u8; 47]: std::fmt::Debug` is not satisfied
     --> /home/leeopop/git/rust-dpdk/src/dpdk.rs:18040:5
      |
18040 |     pub __bindgen_padding_0: [u8; 47usize],
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::fmt::Debug` is not implemented for `[u8; 47]`
      |
      = note: `[u8; 47]` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
      = note: required because of the requirements on the impl of `std::fmt::Debug` for `&[u8; 47]`
      = note: required for the cast to the object type `std::fmt::Debug`

Expected Results

This structure contains [u8; 47] which does not implement Debug trait.
By the way, does [u8; 47] implements Copy trait?
struct rte_cryptodev should not derive Debug and Copy trait also.

#458

@leeopop leeopop changed the title Copy trait is derived when a struct contains __bindgen_padding Debug trait is derived when a struct contains __bindgen_padding Apr 20, 2017
@fitzgen
Copy link
Member

fitzgen commented Apr 20, 2017

Thanks for the bug report!

@fitzgen
Copy link
Member

fitzgen commented Jul 21, 2017

@leeopop both bitfields and deriving debug have undergone significant changes since you reported this; are you still able to reproduce on master?

If so, the best way to help resolve this bug would be to get us a standalone test case we can reproduce ourselves with: https://github.com/servo/rust-bindgen/blob/master/CONTRIBUTING.md#using-creduce-to-minimize-test-cases

Thanks!

@ghost
Copy link

ghost commented Jul 26, 2017

Hi !

I used creduce and get this result:

struct {
  void *schedule;
  unsigned enqueue;
  short *enqueue_burst;
  unsigned enqueue_new_burst;
  short *enqueue_forward_burst;
  short *dequeue;
  short dequeue_burst;
  int *data;
  int *dev_ops;
  char attached
} __attribute__((__aligned__(64)));

Once I run bindgen, I get

error[E0277]: the trait bound `[u8; 55]: std::fmt::Debug` is not satisfied
  --> src/lib.rs:16:5
   |
16 |     pub __bindgen_padding_0: [u8; 55usize],
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `[u8; 55]` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
   |
   = help: the trait `std::fmt::Debug` is not implemented for `[u8; 55]`
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `&[u8; 55]`
   = note: required for the cast to the object type `std::fmt::Debug

edit:
bindgen used: 0.28.0 (built on 167e84c)
rustc 1.21.0-nightly (c417ee9ae 2017-07-25)

@fitzgen
Copy link
Member

fitzgen commented Jul 26, 2017

Thanks! This is very helpful! Will dig into it.

fitzgen added a commit to fitzgen/rust-bindgen that referenced this issue Jul 26, 2017
When there is large enough alignment that we might generate padding which has
more members that `RUST_DERIVE_IN_ARRAY_LIMIT`, we can break our ability to
derive traits. This commit solves this issue conservatively: there are cases
where we leave a derive on the table, because in order to know that we could add
that derive, we would need to compute padding before we determine whether we can
derive.

Fixes rust-lang#648
tmfink pushed a commit to tmfink/rust-bindgen that referenced this issue Aug 4, 2017
When there is large enough alignment that we might generate padding which has
more members that `RUST_DERIVE_IN_ARRAY_LIMIT`, we can break our ability to
derive traits. This commit solves this issue conservatively: there are cases
where we leave a derive on the table, because in order to know that we could add
that derive, we would need to compute padding before we determine whether we can
derive.

Fixes rust-lang#648
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants