Skip to contentSkip to conversation

rust: kernel: introduce `unsafe_precondition_assert!` macro

PATCH Superseded Ritvik Gupta 1 participant 1 message Aug 15, 2025 Rust

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 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 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 | 54 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 rust/kernel/safety.rs
rust/kernel/lib.rs +1 -0
index ed53169e795c..5eb301685ce8 100644
@@ -113,6 +113,7 @@
 pub mod rbtree;
 pub mod regulator;
 pub mod revocable;
+pub mod safety;
 pub mod security;
 pub mod seq_file;
 pub mod sizes;
rust/kernel/safety.rs new file +54 -0

54 lines not on this page · raw message

base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585

Signature · 1 line
2.50.1