Skip to content

bindgen is not aware of std::atomic #2595

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
nwoods-cimpress opened this issue Aug 2, 2023 · 1 comment
Closed

bindgen is not aware of std::atomic #2595

nwoods-cimpress opened this issue Aug 2, 2023 · 1 comment

Comments

@nwoods-cimpress
Copy link

Input C/C++ Header

#include <atomic>

struct StructWithAtomics
{
	std::atomic<uint32_t>	m_x;
	uint32_t		m_a, m_b, m_c;
};

StructWithAtomics frob();

Bindgen Invocation

$ bindgen foo.h --allowlist-function=frob --no-layout-tests -- -x c++

Actual Results

/* automatically generated by rust-bindgen 0.59.1 */

#[repr(C)]
#[derive(Debug)]
pub struct std_atomic<_Tp> {
    pub _M_i: _Tp,
    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<_Tp>>,
}
pub type std_atomic_value_type<_Tp> = _Tp;
pub type __uint32_t = ::std::os::raw::c_uint;
#[repr(C)]
#[derive(Debug)]
pub struct StructWithAtomics {
    pub m_x: std_atomic<u32>,
    pub m_a: u32,
    pub m_b: u32,
    pub m_c: u32,
}
extern "C" {
    #[link_name = "\u{1}_Z4frobv"]
    pub fn frob() -> StructWithAtomics;
}

With the System V AMD64 ABI (used on x86-64 pretty much everywhere except for Windows), this can actually create a calling convention mismatch because the bindings generated by Rust will expect StructWithAtomics to be returned in RAX/RDX, whereas the C++ code will actually use a hidden return parameter. This can be demonstrated here: https://godbolt.org/z/4ErhEjb1v

Expected Results

/* automatically generated by rust-bindgen 0.59.1 */

#[repr(C)]
#[derive(Debug)]
pub struct StructWithAtomics {
    pub m_x: std::sync::atomic::AtomicU32,
    pub m_a: u32,
    pub m_b: u32,
    pub m_c: u32,
}
extern "C" {
    #[link_name = "\u{1}_Z4frobv"]
    pub fn frob() -> StructWithAtomics;
}
@pvdrz
Copy link
Contributor

pvdrz commented Aug 8, 2023

duplicated of #2151

@pvdrz pvdrz closed this as completed Aug 8, 2023
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