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

C++ std::function types are translated into [u128; 3usize] #1724

Open
mayabyte opened this issue Feb 6, 2020 · 4 comments
Open

C++ std::function types are translated into [u128; 3usize] #1724

mayabyte opened this issue Feb 6, 2020 · 4 comments

Comments

@mayabyte
Copy link

mayabyte commented Feb 6, 2020

header.hpp:

using example_type = std::function<void()>;

bindgen invocation:

let bindings = bindgen::Builder::default()
        .header("header.hpp")
        .opaque_type("std::.*")
        .whitelist_type("example_type")
        .rust_target(bindgen::RustTarget::Nightly)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

Output binding:

pub type example_type = [u128; 3usize];

With just a plain typedef like this all is well, but in many situations bindgen also needs to use this type (possibly indirectly) in an extern "C" {...} block, resulting in warnings like this:

warning: `extern` block uses type `[u128; 3usize]`, which is not FFI-safe
    --> src/../bindings.rs:9772:18
     |
9772 |             root::example_type;
     |             ^^^^^^^^^^^^^^^^^^ not FFI-safe
     |
     = help: consider passing a pointer to the array
     = note: passing raw arrays by value is not FFI-safe

warning: `extern` block uses type `u128`, which is not FFI-safe
    --> src/../bindings.rs:1230:27
     |
1230 |                     this: *const root::example_type,
     |                           ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
     |
     = note: 128-bit integers don't currently have a known stable ABI

While not strictly an error, it makes me uneasy that function pointers may or may not be interpreted correctly between the C++ library I need to use and my own code.

Is there a way to get bindgen to generate, say, &[u64; 6usize] or something for std::functions instead? Is this intended behavior or unexpected?

I have a feeling this could be related to the use of .opaque_type("std::.*"), but whitelisting std::function doesn't seem to change anything and I'd greatly prefer not to generate bindings for all of std.

@emilio
Copy link
Contributor

emilio commented Mar 11, 2020

Yes, this is what opaque_type causes when used on this kind of stuff... That being said, it seems to me it's a rust bug to complain that *const root::example_type is not FFI-safe...

@elichai
Copy link
Contributor

elichai commented Mar 23, 2020

That being said, it seems to me it's a rust bug to complain that *const root::example_type is not FFI-safe...

rust-lang/rust#66220
Enjoy :)

@thomaseizinger
Copy link

Out of curiosity, why is this mapping happening? Does this array represent multiple pointers (context, parameters and return value?) or what does it mean semantically?

@jschwe
Copy link
Contributor

jschwe commented Jul 30, 2024

Out of curiosity, why is this mapping happening?

This related issue #2071 (comment) states that basically the example_type should not be translated by bindgen, but since bindgen does it anyway, the user should blocklist it manually.

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

5 participants