rust-lang / rust-lang/rust-bindgen
Wrong ABI used for small C++ classes on Linux.
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
#include <cstdint>
struct Foo {
size_t data;
};
struct Bar {
size_t data;
~Bar() { data = 0; }
};
Foo MakeFoo(); // { return Foo(); }
Bar MakeBar(); // { return Bar(); }
Bindgen Invocation
bindgen::Builder::default()
.clang_args(&["-x","c++", "-std=c++11"])
.header("input.h")
.generate()
.unwrap()
Actual Results
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Foo {
pub data: usize,
}
impl Clone for Foo {
fn clone(&self) -> Self { *self }
}
#[repr(C)]
#[derive(Debug)]
pub struct Bar {
pub data: usize,
}
extern "C" {
#[link_name = "_ZN3BarD1Ev"]
pub fn Bar_Bar_destructor(this: *mut Bar);
}
impl Bar {
#[inline]
pub unsafe fn destruct(&mut self) { Bar_Bar_destructor(self) }
}
extern "C" {
#[link_name = "_Z7MakeFoov"]
pub fn MakeFoo() -> Foo;
}
extern "C" {
#[link_name = "_Z7MakeBarv"]
pub fn MakeBar() -> Bar;
}
Expected Results
Not sure...
Discussion
MakeFoo and MakeBar look very similar, however in C++ they use different ABIs, at least on Linux.
Here's a quote from System V AMD64 ABI spec (page 20):
If a C++ object has either a non-trivial copy constructor or a non-trivial
destructor 11, it is passed by invisible reference (the object is replaced in the
parameter list by a pointer that has class POINTER) 12
This means that while Foo get returned by-value in the rax register, Bar must be returned by-pointer to a caller allocated memory. To Rust compiler, however, these look identical, so it expects both to be returned in rax.
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 with the supplied C++ header and the bindgen::Builder invocation; inspect how generated return types represent trivial Foo versus non-trivial Bar under the Linux System V AMD64 ABI. Done means generated bindings use the correct return convention for both classes and include regression coverage for the mismatch.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100