* [PATCH 1/5] rust: fix size_t in bindgen prototypes of C builtins [not found] <20240913213041.395655-1-gary@garyguo.net> @ 2024-09-13 21:29 ` Gary Guo 2024-09-29 21:00 ` Trevor Gross 0 siblings, 1 reply; 3+ messages in thread From: Gary Guo @ 2024-09-13 21:29 UTC (permalink / raw) To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt Cc: rust-for-linux, linux-kernel, llvm Without `-fno-builtin`, for functions like memcpy/memmove (and many others), bindgen seems to be using the clang-provided prototype. This prototype is ABI-wise compatible, but the issue is that it does not have the same information as the source code w.r.t. typedefs. For example, bindgen generates the following: extern "C" { pub fn strlen(s: *const core::ffi::c_char) -> core::ffi::c_ulong; } note that the return type is `c_ulong` (i.e. unsigned long), despite the size_t-is-usize behavior (this is default, and we have not opted out from it using --no-size_t-is-usize). Similarly, memchr's size argument should be of type `__kernel_size_t`, but bindgen generates `c_ulong` directly. We want to ensure any `size_t` is translated to Rust `usize` so that we can avoid having them be different type on 32-bit and 64-bit architectures, and hence would require a lot of excessive type casts when calling FFI functions. I found that this bindgen behavior (which probably is caused by libclang) can be disabled by `-fno-builtin`. Using the flag for compiled code can result in less optimisation because compiler cannot assume about their properties anymore, but this should not affect bindgen. Signed-off-by: Gary Guo <gary@garyguo.net> --- rust/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rust/Makefile b/rust/Makefile index 4eae318f36ff7..863d87ad0ce68 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -265,7 +265,11 @@ else bindgen_c_flags_lto = $(bindgen_c_flags) endif -bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__ +# `-fno-builtin` is passed to avoid bindgen from using clang builtin prototypes +# for functions like `memcpy` -- if this flag is not passed, bindgen-generated +# prototypes use `c_ulong` or `c_uint` depending on architecture instead of +# generating `usize`. +bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__ quiet_cmd_bindgen = BINDGEN $@ cmd_bindgen = \ -- 2.44.1 ^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 1/5] rust: fix size_t in bindgen prototypes of C builtins 2024-09-13 21:29 ` [PATCH 1/5] rust: fix size_t in bindgen prototypes of C builtins Gary Guo @ 2024-09-29 21:00 ` Trevor Gross 2024-10-05 22:10 ` Gary Guo 0 siblings, 1 reply; 3+ messages in thread From: Trevor Gross @ 2024-09-29 21:00 UTC (permalink / raw) To: Gary Guo Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, rust-for-linux, linux-kernel, llvm On Fri, Sep 13, 2024 at 5:32 PM Gary Guo <gary@garyguo.net> wrote: > -bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__ > +# `-fno-builtin` is passed to avoid bindgen from using clang builtin prototypes > +# for functions like `memcpy` -- if this flag is not passed, bindgen-generated > +# prototypes use `c_ulong` or `c_uint` depending on architecture instead of > +# generating `usize`. > +bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__ I wonder how reliable this behavior is. Maybe bindgen could do a better job controlling this, is there an open issue? - Trevor ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/5] rust: fix size_t in bindgen prototypes of C builtins 2024-09-29 21:00 ` Trevor Gross @ 2024-10-05 22:10 ` Gary Guo 0 siblings, 0 replies; 3+ messages in thread From: Gary Guo @ 2024-10-05 22:10 UTC (permalink / raw) To: Trevor Gross Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng, Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl, Nathan Chancellor, Nick Desaulniers, Bill Wendling, Justin Stitt, rust-for-linux, linux-kernel, llvm On Sun, 29 Sep 2024 17:00:29 -0400 Trevor Gross <tmgross@umich.edu> wrote: > On Fri, Sep 13, 2024 at 5:32 PM Gary Guo <gary@garyguo.net> wrote: > > > -bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__ > > +# `-fno-builtin` is passed to avoid bindgen from using clang builtin prototypes > > +# for functions like `memcpy` -- if this flag is not passed, bindgen-generated > > +# prototypes use `c_ulong` or `c_uint` depending on architecture instead of > > +# generating `usize`. > > +bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__ > > I wonder how reliable this behavior is. Maybe bindgen could do a > better job controlling this, is there an open issue? > > - Trevor I didn't search for bindgen issues when made this change, but apparently this is indeed the suggested approach in https://github.com/rust-lang/rust-bindgen/issues/1770 Best, Gary ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-10-05 22:10 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- [not found] <20240913213041.395655-1-gary@garyguo.net> 2024-09-13 21:29 ` [PATCH 1/5] rust: fix size_t in bindgen prototypes of C builtins Gary Guo 2024-09-29 21:00 ` Trevor Gross 2024-10-05 22:10 ` Gary Guo
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).