build_assert.c 1.1 KB

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