Skip to content

Commit 3c262d5

Browse files
committed
rust: helpers: Clarify comment on size_t = uintptr_t guard
Also fix the guard to not fail spriously on ARM. We just need the types to be ABI-compatible; we don't need C to be willing to implicitly convert between them. Signed-off-by: Geoffrey Thomas <[email protected]>
1 parent ac536cc commit 3c262d5

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

rust/helpers.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,24 @@ long rust_helper_ptr_err(__force const void *ptr)
117117
}
118118
EXPORT_SYMBOL_GPL(rust_helper_ptr_err);
119119

120-
#if !defined(CONFIG_ARM)
121-
// See https://github.com/rust-lang/rust-bindgen/issues/1671
122-
static_assert(__builtin_types_compatible_p(size_t, uintptr_t),
123-
"size_t must match uintptr_t, what architecture is this??");
124-
#endif
120+
/* We use bindgen's --size_t-is-usize option to bind the C size_t type
121+
* as the Rust usize type, so we can use it in contexts where Rust
122+
* expects a usize like slice (array) indices. usize is defined to be
123+
* the same as C's uintptr_t type (can hold any pointer) but not
124+
* necessarily the same as size_t (can hold the size of any single
125+
* object). Most modern platforms use the same concrete integer type for
126+
* both of them, but in case we find ourselves on a platform where
127+
* that's not true, fail early instead of risking ABI or
128+
* integer-overflow issues.
129+
*
130+
* If your platform fails this assertion, it means that you are in
131+
* danger of integer-overflow bugs (even if you attempt to remove
132+
* --size_t-is-usize). It may be easiest to change the kernel ABI on
133+
* your platform such that size_t matches uintptr_t (i.e., to increase
134+
* size_t, because uintptr_t has to be at least as big as size_t).
135+
*/
136+
static_assert(
137+
sizeof(size_t) == sizeof(uintptr_t) &&
138+
__alignof__(size_t) == __alignof__(uintptr_t),
139+
"Rust code expects C size_t to match Rust usize"
140+
);

0 commit comments

Comments
 (0)