From: Antonio Hickey <contact@antoniohickey.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: Antonio Hickey <contact@antoniohickey.com>,
Daniel Cote <danielstonecote@gmail.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: [PATCH v4 1/2] rust: kernel: create `overflow_assert!` macro
Date: Sun, 29 Jun 2025 02:43:35 +0000 [thread overview]
Message-ID: <20250629024310.97937-2-contact@antoniohickey.com> (raw)
In-Reply-To: <20250629024310.97937-1-contact@antoniohickey.com>
This commit creates a macro for overflow assertions, the use of this
macro will avoid local `#ifdef`s by encapsulating the conditional
behavior (like `#[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]`) to the macro.
In addition this macro allows us to document the intent of the assertion
more clearly.
Co-developed-by: Daniel Cote <danielstonecote@gmail.com>
Signed-off-by: Daniel Cote <danielstonecote@gmail.com>
Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
Link: https://github.com/Rust-for-Linux/linux/issues/1159
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
---
rust/kernel/lib.rs | 1 +
rust/kernel/overflow_assert.rs | 32 ++++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+)
create mode 100644 rust/kernel/overflow_assert.rs
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 6b4774b2b1c3..e395adb6b293 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -92,6 +92,7 @@
pub mod of;
#[cfg(CONFIG_PM_OPP)]
pub mod opp;
+pub mod overflow_assert;
pub mod page;
#[cfg(CONFIG_PCI)]
pub mod pci;
diff --git a/rust/kernel/overflow_assert.rs b/rust/kernel/overflow_assert.rs
new file mode 100644
index 000000000000..5164339813cb
--- /dev/null
+++ b/rust/kernel/overflow_assert.rs
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Overflow assertion.
+
+/// Verifies at runtime that an expression is within an expected bound.
+///
+/// This macro is only active when `CONFIG_RUST_OVERFLOW_CHECKS` is enabled.
+///
+/// # Examples
+///
+/// ```
+/// overflow_assert!(3 <= 10);
+/// overflow_assert!(5 <= 5);
+///
+/// const X: u8 = 5;
+/// overflow_assert!(X + 3 < 10);
+///
+/// const MAX: i32 = 42;
+/// const fn f(x: i32) -> i32 {
+/// x + 1
+/// }
+/// overflow_assert!(f(40) < MAX);
+/// overflow_assert!(f(40) < MAX, "f(x) must not overflow the max value.");
+/// ```
+#[macro_export]
+macro_rules! overflow_assert {
+ ($cond:expr $(,$arg:literal)?) => {
+ if cfg!(CONFIG_RUST_OVERFLOW_CHECKS) {
+ ::core::assert!($cond $(,$arg)?);
+ }
+ };
+}
--
2.50.0
next parent reply other threads:[~2025-06-29 2:43 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20250629024310.97937-1-contact@antoniohickey.com>
2025-06-29 2:43 ` Antonio Hickey [this message]
2025-06-29 16:04 ` Tamir Duberstein
2025-07-20 16:17 ` Antonio Hickey
2025-07-20 16:23 ` Tamir Duberstein
2025-07-20 16:58 ` Miguel Ojeda
2025-06-29 2:43 ` [PATCH v4 2/2] rust: uaccess: refactor to use `overflow_assert!` Antonio Hickey
2025-06-29 19:01 ` Tamir Duberstein
2025-07-21 11:28 ` Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250629024310.97937-2-contact@antoniohickey.com \
--to=contact@antoniohickey.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=danielstonecote@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®