rust-lang / rust-lang/rust-bindgen
bindings generated are not FFI-safe when std::string is returned by value
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Input C/C++ Header
// bindgen-flags: --opaque-type "std::.+" --enable-cxx-namespaces --allowlist-type "StdStringReturnedByValue"
namespace std {
template <typename> class allocator;
template <class> struct char_traits;
template <typename a, typename = char_traits<a>, typename = allocator<a>>
class basic_string;
typedef basic_string<char> string;
template <typename> class allocator {};
template <typename, typename, typename> class basic_string {
public:
struct {};
long b;
enum { c = 15 };
char d[c];
basic_string(const char *, const allocator<char> & = allocator<char>());
};
} // namespace std
struct StdStringReturnedByValue {
std::string value();
};
std::string StdStringReturnedByValue::value() {
std::string value("hello");
return value;
}
Bindgen Invocation
$ bindgen input.h --opaque-type "std::.+" --enable-cxx-namespaces --allowlist-type "StdStringReturnedByValue"
Actual Results
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]
#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
#[allow(unused_imports)]
use self::super::root;
pub mod std {
#[allow(unused_imports)]
use self::super::super::root;
pub type string = [u64; 4usize];
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct StdStringReturnedByValue {
pub _address: u8,
}
#[test]
fn bindgen_test_layout_StdStringReturnedByValue() {
assert_eq!(
::std::mem::size_of::<StdStringReturnedByValue>(),
1usize,
concat!("Size of: ", stringify!(StdStringReturnedByValue))
);
assert_eq!(
::std::mem::align_of::<StdStringReturnedByValue>(),
1usize,
concat!("Alignment of ", stringify!(StdStringReturnedByValue))
);
}
extern "C" {
#[link_name = "\u{1}_ZN24StdStringReturnedByValue5valueEv"]
pub fn StdStringReturnedByValue_value(
this: *mut root::StdStringReturnedByValue,
) -> root::std::string;
}
impl StdStringReturnedByValue {
#[inline]
pub unsafe fn value(&mut self) -> root::std::string {
StdStringReturnedByValue_value(self)
}
}
}
warning: `extern` block uses type `[u64; 4]`, which is not FFI-safe
--> /tmp/bindings.rs.aawdvy:41:14
|
41 | ) -> root::std::string;
| ^^^^^^^^^^^^^^^^^ not FFI-safe
|
= note: `#[warn(improper_ctypes)]` on by default
= help: consider passing a pointer to the array
= note: passing raw arrays by value is not FFI-safe
Expected Results
I expected no warnings to be emitted. tests pass fine but I would like the bindings generated to be FFI-safe.
Is there another set of flags that would do that for me?
In my actual project I'm wrapping an external library that returns std::string sometimes and I get these warnings there. I worked around it by replacing std::string with another struct with the same size and alignment but I don't like that solution.
/**
* <div rustbindgen="true" replaces="std::string"></div>
*/
struct basic_string_char {
uint64_t storage[4];
};
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with input.h and the shown bindgen invocation, then inspect how the generated extern declaration represents an opaque std::string return value. Done means generated bindings compile without the reported improper_ctypes warning while preserving the demonstrated return type behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100