xref: /linux-6.15/rust/helpers/build_assert.c (revision 87634653)
1*87634653SAndreas Hindborg // SPDX-License-Identifier: GPL-2.0
2*87634653SAndreas Hindborg 
3*87634653SAndreas Hindborg #include <linux/build_bug.h>
4*87634653SAndreas Hindborg 
5*87634653SAndreas Hindborg /*
6*87634653SAndreas Hindborg  * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
7*87634653SAndreas Hindborg  * use it in contexts where Rust expects a `usize` like slice (array) indices.
8*87634653SAndreas Hindborg  * `usize` is defined to be the same as C's `uintptr_t` type (can hold any
9*87634653SAndreas Hindborg  * pointer) but not necessarily the same as `size_t` (can hold the size of any
10*87634653SAndreas Hindborg  * single object). Most modern platforms use the same concrete integer type for
11*87634653SAndreas Hindborg  * both of them, but in case we find ourselves on a platform where
12*87634653SAndreas Hindborg  * that's not true, fail early instead of risking ABI or
13*87634653SAndreas Hindborg  * integer-overflow issues.
14*87634653SAndreas Hindborg  *
15*87634653SAndreas Hindborg  * If your platform fails this assertion, it means that you are in
16*87634653SAndreas Hindborg  * danger of integer-overflow bugs (even if you attempt to add
17*87634653SAndreas Hindborg  * `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
18*87634653SAndreas Hindborg  * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
19*87634653SAndreas Hindborg  * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
20*87634653SAndreas Hindborg  */
21*87634653SAndreas Hindborg static_assert(
22*87634653SAndreas Hindborg 	sizeof(size_t) == sizeof(uintptr_t) &&
23*87634653SAndreas Hindborg 	__alignof__(size_t) == __alignof__(uintptr_t),
24*87634653SAndreas Hindborg 	"Rust code expects C `size_t` to match Rust `usize`"
25*87634653SAndreas Hindborg );
26