Skip to content

Commit b8d687c

Browse files
ritvikosojeda
authored andcommitted
rust: safety: 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 preconditions 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: #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> Reviewed-by: Benno Lossin <lossin@kernel.org> Link: https://patch.msgid.link/20251007215034.213779-1-ritvikfoss@gmail.com [ 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 ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 0e62e4f commit b8d687c

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub mod pwm;
135135
pub mod rbtree;
136136
pub mod regulator;
137137
pub mod revocable;
138+
pub mod safety;
138139
pub mod scatterlist;
139140
pub mod security;
140141
pub mod seq_file;

rust/kernel/safety.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Safety related APIs.
4+
5+
/// Checks that a precondition of an unsafe function is followed.
6+
///
7+
/// The check is enabled at runtime if debug assertions (`CONFIG_RUST_DEBUG_ASSERTIONS`)
8+
/// are enabled. Otherwise, this macro is a no-op.
9+
///
10+
/// # Examples
11+
///
12+
/// ```no_run
13+
/// use kernel::unsafe_precondition_assert;
14+
///
15+
/// struct RawBuffer<T: Copy, const N: usize> {
16+
/// data: [T; N],
17+
/// }
18+
///
19+
/// impl<T: Copy, const N: usize> RawBuffer<T, N> {
20+
/// /// # Safety
21+
/// ///
22+
/// /// The caller must ensure that `index` is less than `N`.
23+
/// unsafe fn set_unchecked(&mut self, index: usize, value: T) {
24+
/// unsafe_precondition_assert!(
25+
/// index < N,
26+
/// "RawBuffer::set_unchecked() requires index ({index}) < N ({N})"
27+
/// );
28+
///
29+
/// // SAFETY: By the safety requirements of this function, `index` is valid.
30+
/// unsafe {
31+
/// *self.data.get_unchecked_mut(index) = value;
32+
/// }
33+
/// }
34+
/// }
35+
/// ```
36+
///
37+
/// # Panics
38+
///
39+
/// Panics if the expression is evaluated to [`false`] at runtime.
40+
#[macro_export]
41+
macro_rules! unsafe_precondition_assert {
42+
($cond:expr $(,)?) => {
43+
$crate::unsafe_precondition_assert!(@inner $cond, ::core::stringify!($cond))
44+
};
45+
46+
($cond:expr, $($arg:tt)+) => {
47+
$crate::unsafe_precondition_assert!(@inner $cond, $crate::prelude::fmt!($($arg)+))
48+
};
49+
50+
(@inner $cond:expr, $msg:expr) => {
51+
::core::debug_assert!($cond, "unsafe precondition violated: {}", $msg)
52+
};
53+
}

0 commit comments

Comments
 (0)