Skip to content

Incorrect Size when C++ std::string in struct #1808

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

Closed
JonBoyleCoding opened this issue Jun 20, 2020 · 2 comments
Closed

Incorrect Size when C++ std::string in struct #1808

JonBoyleCoding opened this issue Jun 20, 2020 · 2 comments

Comments

@JonBoyleCoding
Copy link

JonBoyleCoding commented Jun 20, 2020

Hi, when trying to create bindings to a struct that contains a C++ std::string, the tests on those particular structs fail with an incorrect size.

I'm using bindgen version 0.54.0 and using clang version 10.0.0 provided via linuxbrew.

Is this something that:

  • should work and it's a setup issue on my end?
  • something that's not supported?
  • something that I can continue using and not worry about?
  • something that there is a decent workaround for?
  • something I haven't thought of?

I would note the library I am wanting to use bindgen on isn't able to be changed.

Thanks

Input C/C++ Header

#ifndef BINDGEN_TEST_TEST_STRUCT_H
#define BINDGEN_TEST_TEST_STRUCT_H

#include <string>

struct JStructString {
    std::string mystring;
};

#endif //BINDGEN_TEST_TEST_STRUCT_H

Bindgen Invocation

    let bindings = bindgen::Builder::default()
        .header("test_struct.h")
        .clang_args(&["-x", "c++"])
        .whitelist_type("JStructString")
        .enable_cxx_namespaces()
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

Actual Results

test root::__bindgen_test_layout_basic_string_open0_char_char_traits_open1_char_close1_allocator_open1_char_close1_close0_instantiation ... FAILED
test root::bindgen_test_layout_JStructString ... FAILED

failures:

---- root::__bindgen_test_layout_basic_string_open0_char_char_traits_open1_char_close1_allocator_open1_char_close1_close0_instantiation stdout ----
thread 'root::__bindgen_test_layout_basic_string_open0_char_char_traits_open1_char_close1_allocator_open1_char_close1_close0_instantiation' panicked at 'assertion failed: `(left == right)`
  left: `16`,
 right: `32`: Size of template specialization: root :: std :: basic_string < :: std :: os :: raw :: c_char >', /data1/Programming/bindgen_test/target/debug/build/bindgen_test-50b60b0d3abdf93c/out/bindings.rs:451:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:200
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:211
   6: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:477
   7: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:384
   8: std::panicking::begin_panic_fmt
             at src/libstd/panicking.rs:339
   9: bindgen_test::root::__bindgen_test_layout_basic_string_open0_char_char_traits_open1_char_close1_allocator_open1_char_close1_close0_instantiation
             at ./target/debug/build/bindgen_test-50b60b0d3abdf93c/out/bindings.rs:451
  10: bindgen_test::root::__bindgen_test_layout_basic_string_open0_char_char_traits_open1_char_close1_allocator_open1_char_close1_close0_instantiation::{{closure}}
             at ./target/debug/build/bindgen_test-50b60b0d3abdf93c/out/bindings.rs:449
  11: core::ops::function::FnOnce::call_once
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libcore/ops/function.rs:235
  12: <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/liballoc/boxed.rs:787
  13: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:80
  14: std::panicking::try
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libstd/panicking.rs:275
  15: std::panic::catch_unwind
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libstd/panic.rs:394
  16: test::run_test::run_test_inner::{{closure}}
             at src/libtest/lib.rs:1408
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

---- root::bindgen_test_layout_JStructString stdout ----
thread 'root::bindgen_test_layout_JStructString' panicked at 'assertion failed: `(left == right)`
  left: `16`,
 right: `32`: Size of: JStructString', /data1/Programming/bindgen_test/target/debug/build/bindgen_test-50b60b0d3abdf93c/out/bindings.rs:427:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:200
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:211
   6: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:477
   7: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:384
   8: std::panicking::begin_panic_fmt
             at src/libstd/panicking.rs:339
   9: bindgen_test::root::bindgen_test_layout_JStructString
             at ./target/debug/build/bindgen_test-50b60b0d3abdf93c/out/bindings.rs:427
  10: bindgen_test::root::bindgen_test_layout_JStructString::{{closure}}
             at ./target/debug/build/bindgen_test-50b60b0d3abdf93c/out/bindings.rs:426
  11: core::ops::function::FnOnce::call_once
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libcore/ops/function.rs:235
  12: <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/liballoc/boxed.rs:787
  13: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:80
  14: std::panicking::try
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libstd/panicking.rs:275
  15: std::panic::catch_unwind
             at /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libstd/panic.rs:394
  16: test::run_test::run_test_inner::{{closure}}
             at src/libtest/lib.rs:1408
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@emilio
Copy link
Contributor

emilio commented Jun 21, 2020

Yeah, there are known issues with the STL (which is very unfortunate, I know). Generally it's recommended to mark the stl as blacklisted or opaque, so that the types have the right layout :/

@emilio
Copy link
Contributor

emilio commented Jun 21, 2020

Closing as a duplicate of #738.

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

2 participants