rust/kernel/lib.rs | 1 + rust/kernel/safety.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 rust/kernel/safety.rs
Introduce a new `safety` module containing `unsafe_precondition_assert!`
macro. It is a wrapper around `debug_assert!`, intended for validating
pre-conditions of unsafe function.
When `CONFIG_RUST_DEBUG_ASSERTIONS` flag is enabled, this macro performs
runtime checks to ensure that the preconditions for unsafe function hold.
Otherwise, the macro is a no-op.
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1162
Link: https://rust-for-linux.zulipchat.com/#narrow/channel/291566-Library/topic/.60unsafe_precondition_assert.60.20macro/with/528457452
Signed-off-by: Ritvik Gupta <ritvikfoss@gmail.com>
---
Changes in v2:
- Wrap `debug_assert!` internally instead of using `pr_err!` with `assert!` + `cfg!(debug_assertions)
- Print “unsafe precondition(s) violated” only on assertion failure (no longer always printed)
- Use `# Safety` section instead of comment in the example
- Rename module-level doc
- Link to v1: https://lore.kernel.org/rust-for-linux/20250716045957.39732-1-ritvikfoss@gmail.com/
---
rust/kernel/lib.rs | 1 +
rust/kernel/safety.rs | 52 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+)
create mode 100644 rust/kernel/safety.rs
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 11a6461e98da..7aab607dd879 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -104,6 +104,7 @@
pub mod print;
pub mod rbtree;
pub mod revocable;
+pub mod safety;
pub mod security;
pub mod seq_file;
pub mod sizes;
diff --git a/rust/kernel/safety.rs b/rust/kernel/safety.rs
new file mode 100644
index 000000000000..42f7763a044f
--- /dev/null
+++ b/rust/kernel/safety.rs
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Safety related APIs.
+
+/// Checks that preconditions of an unsafe function are followed.
+///
+/// The check is enabled at runtime if debug assertions (`CONFIG_RUST_DEBUG_ASSERTIONS`)
+/// are enabled. Otherwise, this macro is no-op.
+///
+/// # Examples
+///
+/// ```
+/// /// # Safety
+/// ///
+/// /// The caller must ensure that interpreting the bytes of `[T; N]` as `[U; N]` is valid.
+/// ///
+/// /// This requires:
+/// /// - `T` and `U` must have same size.
+/// /// - The bit pattern of `T` must be valid for `U`.
+/// /// - The alignment of `T` must be at least as strict as `U`.
+/// unsafe fn transmute_array<const N: usize, T: Copy, U: Copy>(input: [T; N]) -> [U; N] {
+/// unsafe_precondition_assert!(
+/// core::mem::size_of::<T>() == core::mem::size_of::<U>(),
+/// "src and dst must have the same size"
+/// );
+///
+/// unsafe_precondition_assert!(
+/// core::mem::align_of::<T>() >= core::mem::align_of::<U>(),
+/// "src alignment must be compatible with dst alignment"
+/// );
+///
+/// unsafe { core::mem::transmute_copy(&input) }
+/// }
+/// ```
+///
+/// # Panics
+///
+/// Panics if the expression is evaluated to `false` at runtime.
+///
+#[macro_export]
+macro_rules! unsafe_precondition_assert {
+ ($cond:expr $(,)?) => {
+ $crate::unsafe_precondition_assert!(@inner $cond, ::core::stringify!($cond))
+ };
+
+ ($cond:expr, $($arg:tt)+) => {
+ $crate::unsafe_precondition_assert!(@inner $cond, ::core::format_args!($($arg)+))
+ };
+
+ (@inner $cond:expr, $msg:expr) => {
+ ::core::debug_assert!($cond, "unsafe precondition(s) violated: {}", $msg) };
+}
base-commit: dff64b072708ffef23c117fa1ee1ea59eb417807
--
2.50.1
On Wed, 30 Jul 2025 22:41:28 +0530 Ritvik Gupta <ritvikfoss@gmail.com> wrote: > Introduce a new `safety` module containing `unsafe_precondition_assert!` > macro. It is a wrapper around `debug_assert!`, intended for validating > pre-conditions of unsafe function. > > When `CONFIG_RUST_DEBUG_ASSERTIONS` flag is enabled, this macro performs > runtime checks to ensure that the preconditions for unsafe function hold. > Otherwise, the macro is a no-op. This email should be include the Rust-for-Linux list. > > Suggested-by: Miguel Ojeda <ojeda@kernel.org> > Link: https://github.com/Rust-for-Linux/linux/issues/1162 > Link: https://rust-for-linux.zulipchat.com/#narrow/channel/291566-Library/topic/.60unsafe_precondition_assert.60.20macro/with/528457452 > Signed-off-by: Ritvik Gupta <ritvikfoss@gmail.com> > --- > > Changes in v2: > - Wrap `debug_assert!` internally instead of using `pr_err!` with `assert!` + `cfg!(debug_assertions) > - Print “unsafe precondition(s) violated” only on assertion failure (no longer always printed) > - Use `# Safety` section instead of comment in the example > - Rename module-level doc > - Link to v1: https://lore.kernel.org/rust-for-linux/20250716045957.39732-1-ritvikfoss@gmail.com/ > > --- > rust/kernel/lib.rs | 1 + > rust/kernel/safety.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 53 insertions(+) > create mode 100644 rust/kernel/safety.rs > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 11a6461e98da..7aab607dd879 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -104,6 +104,7 @@ > pub mod print; > pub mod rbtree; > pub mod revocable; > +pub mod safety; > pub mod security; > pub mod seq_file; > pub mod sizes; > diff --git a/rust/kernel/safety.rs b/rust/kernel/safety.rs > new file mode 100644 > index 000000000000..42f7763a044f > --- /dev/null > +++ b/rust/kernel/safety.rs > @@ -0,0 +1,52 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Safety related APIs. > + > +/// Checks that preconditions of an unsafe function are followed. > +/// > +/// The check is enabled at runtime if debug assertions (`CONFIG_RUST_DEBUG_ASSERTIONS`) > +/// are enabled. Otherwise, this macro is no-op. > +/// > +/// # Examples > +/// > +/// ``` > +/// /// # Safety > +/// /// > +/// /// The caller must ensure that interpreting the bytes of `[T; N]` as `[U; N]` is valid. > +/// /// > +/// /// This requires: > +/// /// - `T` and `U` must have same size. > +/// /// - The bit pattern of `T` must be valid for `U`. > +/// /// - The alignment of `T` must be at least as strict as `U`. > +/// unsafe fn transmute_array<const N: usize, T: Copy, U: Copy>(input: [T; N]) -> [U; N] { > +/// unsafe_precondition_assert!( > +/// core::mem::size_of::<T>() == core::mem::size_of::<U>(), > +/// "src and dst must have the same size" > +/// ); These can just be `const { assert!() }`? There's no reason for this to be runtime. Best, Gary > +/// > +/// unsafe_precondition_assert!( > +/// core::mem::align_of::<T>() >= core::mem::align_of::<U>(), > +/// "src alignment must be compatible with dst alignment" > +/// ); > +/// > +/// unsafe { core::mem::transmute_copy(&input) } > +/// } > +/// ``` > +/// > +/// # Panics > +/// > +/// Panics if the expression is evaluated to `false` at runtime. > +/// > +#[macro_export] > +macro_rules! unsafe_precondition_assert { > + ($cond:expr $(,)?) => { > + $crate::unsafe_precondition_assert!(@inner $cond, ::core::stringify!($cond)) > + }; > + > + ($cond:expr, $($arg:tt)+) => { > + $crate::unsafe_precondition_assert!(@inner $cond, ::core::format_args!($($arg)+)) > + }; > + > + (@inner $cond:expr, $msg:expr) => { > + ::core::debug_assert!($cond, "unsafe precondition(s) violated: {}", $msg) }; > +} > > base-commit: dff64b072708ffef23c117fa1ee1ea59eb417807
> This email should be include the Rust-for-Linux list. Apologies for oversight. Automated it to prevent in future :) > These can just be `const { assert!() }`? There's no reason for this to > be runtime. Yeah! `static_assert!` makes sense here. I'll add more relevant example in v3. Thanks for the feedback :)
© 2016 - 2025 Red Hat, Inc.