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

on 32-bit platform (e.g. PI) including sys/param.h causes 'shift left with overflow error'. #739

Closed
noyez opened this issue Jun 8, 2017 · 5 comments
Assignees
Labels

Comments

@noyez
Copy link

noyez commented Jun 8, 2017

Input C/C++ Header

#include <sys/param.h>

Bindgen Invocation

        let bindings = bindgen::Builder::default()
            .no_unstable_rust()
            .header("use_sys_param.h")
            .generate()
            .expect("Unable to generate bindings");

Actual Results

pi@pirepeater:~/source/bindgen-test$ RUST_BACKTRACE=1 RUST_LOG=bindgen cargo build
   Compiling bindgen-test v0.1.0 (file:///home/pi/source/bindgen-test)
error: failed to run custom build command for `bindgen-test v0.1.0 (file:///home/pi/source/bindgen-test)`
process didn't exit successfully: `/home/pi/source/bindgen-test/target/debug/build/bindgen-test-d1b6d9223679bb31/build-script-build` (exit code: 101)
--- stderr
thread 'main' panicked at 'attempt to shift left with overflow', /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.25.3/src/ir/comp.rs:297
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:355
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:371
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:549
   5: std::panicking::begin_panic
             at /checkout/src/libstd/panicking.rs:511
   6: std::panicking::begin_panic_fmt
             at /checkout/src/libstd/panicking.rs:495
   7: rust_begin_unwind
             at /checkout/src/libstd/panicking.rs:471
   8: core::panicking::panic_fmt
             at /checkout/src/libcore/panicking.rs:69
   9: core::panicking::panic
             at /checkout/src/libcore/panicking.rs:49
  10: bindgen::ir::comp::Bitfield::mask
             at /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.25.3/src/ir/comp.rs:297
  11: <bindgen::ir::comp::Bitfield as bindgen::codegen::FieldCodegen<'a>>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:3017
  12: <bindgen::ir::comp::BitfieldUnit as bindgen::codegen::FieldCodegen<'a>>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:2933
  13: <bindgen::ir::comp::Field as bindgen::codegen::FieldCodegen<'a>>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:2095
  14: <bindgen::ir::comp::CompInfo as bindgen::codegen::CodeGenerator>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:3525
  15: <bindgen::ir::ty::Type as bindgen::codegen::CodeGenerator>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:1675
  16: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:1465
  17: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen::{{closure}}
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:1486
  18: <bindgen::ir::module::Module as bindgen::codegen::CodeGenerator>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:1510
  19: <bindgen::ir::item::Item as bindgen::codegen::CodeGenerator>::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:1451
  20: bindgen::codegen::codegen::{{closure}}
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:5925
  21: bindgen::ir::context::BindgenContext::gen
             at /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.25.3/src/ir/context.rs:662
  22: bindgen::codegen::codegen
             at ./target/debug/build/bindgen-bb02d811d9c942d5/out/codegen.rs:5903
  23: bindgen::Bindings::generate
             at /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.25.3/src/lib.rs:1075
  24: bindgen::Builder::generate
             at /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.25.3/src/lib.rs:778
  25: build_script_build::main
             at ./build.rs:33
  26: __rust_maybe_catch_panic
             at /checkout/src/libpanic_unwind/lib.rs:98
  27: std::rt::lang_start
             at /checkout/src/libstd/panicking.rs:433
             at /checkout/src/libstd/panic.rs:361
             at /checkout/src/libstd/rt.rs:57
  28: main
  29: __libc_start_main

Expected Results

On my 32-bit arm platforms (namely a Raspberry PI, and Beaglebone), I Expect it not to crash when running build program. I found this when working w/ a c-library, and narrowed it down the sys/param.h

There is no crash on 64-bit platforms.

RUST_LOG=bindgen Output

there was no useful output. I pasted everything in the Actual Results section.
@emilio
Copy link
Contributor

emilio commented Jun 8, 2017

Can you see, of the files that get recursively included, which one contains structs with bitfields?

That is computing a bitfield, that presumably is too wide for us to represent as a simple bitmask op in 32 bits?

Perhaps switching the bitfield mask to u64 would be the most reasonable thing to do.

Thanks for the report!

@noyez
Copy link
Author

noyez commented Jun 9, 2017

Sorry -- i probably should have tried to narrow it down a bit further before submitting. I finally found the offending struct from /usr/include/arm-linux-gnueabihf/sys/ucontext.h on my PI. The problem appears to be unsigned int types that are given 32 bits of a bit field.

Below is a code snippet that will trigger this error in bindgen.

// from: /usr/include/arm-linux-gnueabihf/sys/ucontext.h
//
struct _libc_fpstate
{
  struct
  {
    unsigned int sign1:1;
    unsigned int unused:15;
    unsigned int sign2:1;
    unsigned int exponent:14;
    unsigned int j:1;
    unsigned int mantissa1:31;
    unsigned int mantissa0:32;  /* casues overflow error */
  } fpregs[8];
  unsigned int fpsr:32;         /* casues overflow error */
  unsigned int fpcr:32;         /* casues overflow error */
  unsigned char ftype[8];
  unsigned int init_flag;
};

@emilio
Copy link
Contributor

emilio commented Jun 10, 2017

Well, that makes sense, thanks a lot for finding that out!

@emilio
Copy link
Contributor

emilio commented Jun 10, 2017

More minimized, and also reproducible in 64-bit:

#define POINTER_WIDTH (sizeof(void*) * 8)

struct Foo {
  unsigned long m_bitfield: POINTER_WIDTH;
};

@emilio emilio self-assigned this Jun 10, 2017
@emilio emilio added the bug label Jun 10, 2017
@emilio
Copy link
Contributor

emilio commented Jun 10, 2017

Fix at #742. Need to think about what the correct fix is for larger than-word-size bitfields. C++ truncates, but that shouldn't block that fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants