rust: kernel: introduce `unsafe_precondition_assert!` macro
PATCH v6 1/1 rust: kernel: introduce `unsafe_precondition_assert!` macro
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.
---
Changes in v6:
- Move variable names into format arguments in the doc example.
- Move closing brace ('}') to new line in the macro.
- Link to v5: https://lore.kernel.org/rust-for-linux/[email protected]/
Changes in v5:
- Change doc example
- Use re-exported kernel::prelude::fmt! instead of core::format_args!
- Link to v4: https://lore.kernel.org/rust-for-linux/[email protected]/
Changes in v4:
- Change doc example
- Add no_run attribute to the doc example
- Link to v3: https://lore.kernel.org/rust-for-linux/[email protected]/
Changes in v3:
- Change doc example
- Link to v2: https://lore.kernel.org/all/20250730181420.6979b4f1@eugeo/T/#m9cd35a8fc02a18bd03934c7ecdcffe8667b5fbbd
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/[email protected]/
--- rust/kernel/lib.rs | 1 + rust/kernel/safety.rs | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 rust/kernel/safety.rs
rust/kernel/lib.rs +1 -0
@@ -124,6 +124,7 @@pub mod rbtree;pub mod regulator;pub mod revocable; +pub mod safety;pub mod scatterlist;pub mod security;pub mod seq_file;
rust/kernel/safety.rs new file +53 -0
53 lines not on this page · raw message
base-commit: c746c3b5169831d7fb032a1051d8b45592ae8d78
Signature · 1 line
2.51.0
Quoted 4 lines · Ritvik Gupta
Introduce a new
safetymodule containingunsafe_precondition_assert!macro. It is a wrapper arounddebug_assert!, intended for validating pre-conditions of unsafe function.
Quoted 8 lines
When
CONFIG_RUST_DEBUG_ASSERTIONSflag is enabled, this macro performs runtime checks to ensure that the preconditions for unsafe function hold. Otherwise, the macro is a no-op.
I think it's a good idea to have something like this, not sure if we have the final version here, but we should merge something.
One nit below (that Miguel can fix when picking the patch).
rust/kernel/safety.rs line 51
Quoted 12 lines
rust/kernel/safety.rs new file +12
-0+#[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, $crate ::prelude::fmt!($($arg)+)) + }; + + (@inner $cond:expr, $msg:expr) => { + ::core::debug_assert!($cond,"unsafe precondition(s) violated: {}" , $msg)
This should be singular, since the name of the macro also is singular.
Cheers,
Benno
Quoted 4 lines
(quoted diff) +2
-0+ }; +}base-commit:
c746c3b5169831d7fb032a1051d8b45592ae8d78
Miguel Ojeda
AppliedQuoted 5 lines · Ritvik Gupta
Introduce a new
safetymodule containingunsafe_precondition_assert!macro. It is a wrapper arounddebug_assert!, intended for validating pre-conditions of unsafe function.
Quoted 8 lines
When
CONFIG_RUST_DEBUG_ASSERTIONSflag is enabled, this macro performs runtime checks to ensure that the preconditions for unsafe function hold. Otherwise, the macro is a no-op.
Applied to rust-next -- thanks everyone!
[ Added trailing periods, intra-doc link, "a" in "is a no-op" and ()
to function reference. Removed plural in assertion message and title of macro. Reworded slightly. - Miguel ]
Cheers,
Miguel