aboutsummaryrefslogtreecommitdiffstats
path: root/rust/helpers/build_assert.c
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <[email protected]>2024-09-27 18:36:52 +0000
committerArnaldo Carvalho de Melo <[email protected]>2024-09-27 18:36:52 +0000
commit52c996d3f40b40f87ef9dc80596903309682acc3 (patch)
treecccf9d5d20463b6930054e6f083f778f7ebe487a /rust/helpers/build_assert.c
parentperf symbol: Set binary_type of dso when loading (diff)
parentMerge tag 'mm-hotfixes-stable-2024-09-27-09-45' of git://git.kernel.org/pub/s... (diff)
downloadkernel-52c996d3f40b40f87ef9dc80596903309682acc3.tar.gz
kernel-52c996d3f40b40f87ef9dc80596903309682acc3.zip
Merge remote-tracking branch 'torvalds/master' into perf-tools
To pick up changes in other trees that may affect perf, such as libbpf and in general the header files that perf has copies of, so that we can do the sync with the kernel sources. Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
Diffstat (limited to 'rust/helpers/build_assert.c')
-rw-r--r--rust/helpers/build_assert.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/rust/helpers/build_assert.c b/rust/helpers/build_assert.c
new file mode 100644
index 000000000000..6a54b2680b14
--- /dev/null
+++ b/rust/helpers/build_assert.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/build_bug.h>
+
+/*
+ * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
+ * use it in contexts where Rust expects a `usize` like slice (array) indices.
+ * `usize` is defined to be the same as C's `uintptr_t` type (can hold any
+ * pointer) but not necessarily the same as `size_t` (can hold the size of any
+ * single object). Most modern platforms use the same concrete integer type for
+ * both of them, but in case we find ourselves on a platform where
+ * that's not true, fail early instead of risking ABI or
+ * integer-overflow issues.
+ *
+ * If your platform fails this assertion, it means that you are in
+ * danger of integer-overflow bugs (even if you attempt to add
+ * `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
+ * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
+ * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
+ */
+static_assert(
+ sizeof(size_t) == sizeof(uintptr_t) &&
+ __alignof__(size_t) == __alignof__(uintptr_t),
+ "Rust code expects C `size_t` to match Rust `usize`"
+);