[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DDYZUHMIJSF3.1XF2MPW7D0Q6@kernel.org>
Date: Mon, 03 Nov 2025 11:39:36 +0100
From: "Benno Lossin" <lossin@...nel.org>
To: "Ritvik Gupta" <ritvikfoss@...il.com>, "Miguel Ojeda"
<ojeda@...nel.org>, "Alex Gaynor" <alex.gaynor@...il.com>, "Boqun Feng"
<boqun.feng@...il.com>, "Gary Guo" <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>, "Andreas
Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl" <aliceryhl@...gle.com>,
"Trevor Gross" <tmgross@...ch.edu>, "Danilo Krummrich" <dakr@...nel.org>
Cc: <skhan@...uxfoundation.org>, <linux-kernel@...r.kernel.org>,
<rust-for-linux@...r.kernel.org>
Subject: Re: [PATCH v6] rust: kernel: introduce
`unsafe_precondition_assert!` macro
On Tue Oct 7, 2025 at 11:50 PM CEST, Ritvik Gupta 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.
>
> Suggested-by: Miguel Ojeda <ojeda@...nel.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@...il.com>
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.
Reviewed-by: Benno Lossin <lossin@...nel.org>
One nit below (that Miguel can fix when picking the patch).
> +#[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
> + };
> +}
>
> base-commit: c746c3b5169831d7fb032a1051d8b45592ae8d78
Powered by blists - more mailing lists