Thread (168 messages) 168 messages, 18 authors, 2026-01-21

Re: [PATCH RFC v3 5/8] rust: implement a test balloon via the "varint" subsystem

From: Ezekiel Newren <hidden>
Date: 2025-09-08 17:19:33

On Mon, Sep 8, 2025 at 8:13 AM Patrick Steinhardt [off-list ref] wrote:
+use std::os::raw::c_int;
+use std::os::raw::c_uchar;
I'd really rather avoid using C types in Rust, in favor of using Rust
types in C. I have written a commit that talks about why C should use
Rust primitive types and why Rust should avoid using C types, here:
https://lore.kernel.org/git/2a7d5b05c18d4a96f1905b7043d47c62d367cd2a.1757274320.git.gitgitgadget@gmail.com/ (local).
In my opinion, the type c_void is the only appropriate C type that
should be used on the Rust side, and should be used sparingly.

The std::os::raw::c_* directly inherits the problems of core::ffi,
which changes over time and seems to make a best guess at the correct
definition for a given platform/target. This probably isn't a problem
for all platforms that Rust supports currently, but can anyone say
that Rust got it right for all C compilers of all platforms/targets?

To give an example: c_long is defined in [1,2]

// Rust version 1.63.0
mod c_long_definition {
    cfg_if! {
        if #[cfg(all(target_pointer_width = "64", not(windows)))] {
            pub type c_long = i64;
            pub type NonZero_c_long = crate::num::NonZeroI64;
            pub type c_ulong = u64;
            pub type NonZero_c_ulong = crate::num::NonZeroU64;
        } else {
            // The minimal size of `long` in the C standard is 32 bits
            pub type c_long = i32;
            pub type NonZero_c_long = crate::num::NonZeroI32;
            pub type c_ulong = u32;
            pub type NonZero_c_ulong = crate::num::NonZeroU32;
        }
    }
}

// Rust version 1.89.0
mod c_long_definition {
    crate::cfg_select! {
        any(
            all(target_pointer_width = "64", not(windows)),
            // wasm32 Linux ABI uses 64-bit long
            all(target_arch = "wasm32", target_os = "linux")
        ) => {
            pub(super) type c_long = i64;
            pub(super) type c_ulong = u64;
        }
        _ => {
            // The minimal size of `long` in the C standard is 32 bits
            pub(super) type c_long = i32;
            pub(super) type c_ulong = u32;
        }
    }
}

[1] c_long in 1.63.0
https://doc.rust-lang.org/1.63.0/src/core/ffi/mod.rs.html#175-189:
[2] c_long in 1.89.0
https://doc.rust-lang.org/1.89.0/src/core/ffi/primitives.rs.html#135-151:
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help