rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/1] rust: kernel: create `overflow_assert!`
@ 2025-05-04 16:43 Antonio Hickey
  2025-05-04 16:43 ` [PATCH 1/1] " Antonio Hickey
  2025-05-05  5:36 ` [PATCH 0/1] " Greg KH
  0 siblings, 2 replies; 4+ messages in thread
From: Antonio Hickey @ 2025-05-04 16:43 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich
  Cc: Antonio Hickey, rust-for-linux

This patch set introduces a macro for overflow assertions, the use of
this macro will avoid local `#ifdef`s by encapsulating the conditional
behavior to the macro. In addition this macro allows us to document
the intent of the assertion more clearly.

I also intend to add patches that add use of the assertion and 
transform any existing asserts that could make use of it. I just
wanted to get the ball rolling on the review of the macro itself
before getting started with these patches.

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>


Antonio Hickey (1):
  rust: kernel: create `overflow_assert!`

 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 42 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/1] rust: kernel: create `overflow_assert!`
  2025-05-04 16:43 [PATCH 0/1] rust: kernel: create `overflow_assert!` Antonio Hickey
@ 2025-05-04 16:43 ` Antonio Hickey
  2025-05-05  5:36 ` [PATCH 0/1] " Greg KH
  1 sibling, 0 replies; 4+ messages in thread
From: Antonio Hickey @ 2025-05-04 16:43 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich
  Cc: Antonio Hickey, linux-kernel, rust-for-linux

This commit creates a macro for overflow assertions, the use of this
macro will avoid local `#ifdef`s by encapsulating the conditional
behavior to the macro. In addition this macro allows us to document
the intent of the assertion more clearly.

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 | 42 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index de07aadd1ff5..feeb99fc4bbd 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -64,6 +64,7 @@
 #[cfg(CONFIG_NET)]
 pub mod net;
 pub mod of;
+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..42c274403498
--- /dev/null
+++ b/rust/kernel/overflow_assert.rs
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Overflow assert.
+
+/// Overflow assert (i.e. runtime bound check).
+///
+/// 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!(6, 5);
+///
+/// const X: u8 = 5;
+/// overflow_assert!(X + 1, 10);
+///
+/// const fn f(x: i32) -> i32 {
+///     x + 2
+/// }
+/// overflow_assert!(f(40), 42);
+/// ```
+#[macro_export]
+#[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
+macro_rules! overflow_assert {
+    ($x:expr, $y:expr) => {
+        core::assert!($x <= $y, "overflow assertion failed: {} > {}", $x, $y);
+    };
+}
+
+/// Disabled overflow assertion (no-op).
+///
+/// This macro exists to allow code using `overflow_assert!` to compile when
+/// `CONFIG_RUST_OVERFLOW_CHECKS` is **not** enabled. It expands to nothing
+/// so it performs no checks and emits no code.
+#[macro_export]
+#[cfg(not(CONFIG_RUST_OVERFLOW_CHECKS))]
+macro_rules! assert_no_overflow {
+    ($x:expr, $y:expr) => {};
+}
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 0/1] rust: kernel: create `overflow_assert!`
@ 2025-05-04 16:56 Antonio Hickey
  0 siblings, 0 replies; 4+ messages in thread
From: Antonio Hickey @ 2025-05-04 16:56 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich
  Cc: Antonio Hickey, rust-for-linux

This patch set introduces a macro for overflow assertions, the use of
this macro will avoid local `#ifdef`s by encapsulating the conditional
behavior to the macro. In addition this macro allows us to document
the intent of the assertion more clearly.

I also intend to add patches that add use of the assertion and 
transform any existing asserts that could make use of it. I just
wanted to get the ball rolling on the review of the macro itself
before getting started with these patches.

Changes in v2:
- Fixed example doc comment so it doesn't panic.
- Link to v1: https://lore.kernel.org/rust-for-linux/20250504164349.84149-1-contact@antoniohickey.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>

Antonio Hickey (1):
  rust: kernel: create `overflow_assert!`

 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 42 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs

-- 
2.49.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/1] rust: kernel: create `overflow_assert!`
  2025-05-04 16:43 [PATCH 0/1] rust: kernel: create `overflow_assert!` Antonio Hickey
  2025-05-04 16:43 ` [PATCH 1/1] " Antonio Hickey
@ 2025-05-05  5:36 ` Greg KH
  1 sibling, 0 replies; 4+ messages in thread
From: Greg KH @ 2025-05-05  5:36 UTC (permalink / raw)
  To: Antonio Hickey
  Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, rust-for-linux

On Sun, May 04, 2025 at 12:43:48PM -0400, Antonio Hickey wrote:
> This patch set introduces a macro for overflow assertions, the use of
> this macro will avoid local `#ifdef`s by encapsulating the conditional
> behavior to the macro. In addition this macro allows us to document
> the intent of the assertion more clearly.
> 
> I also intend to add patches that add use of the assertion and 
> transform any existing asserts that could make use of it. I just
> wanted to get the ball rolling on the review of the macro itself
> before getting started with these patches.

An example of how this will be used is best so that we can see if it
really does make sense to have such a macro :)

So an extra patch in the series doing this is very good to have.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-05-05  5:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-04 16:43 [PATCH 0/1] rust: kernel: create `overflow_assert!` Antonio Hickey
2025-05-04 16:43 ` [PATCH 1/1] " Antonio Hickey
2025-05-05  5:36 ` [PATCH 0/1] " Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2025-05-04 16:56 Antonio Hickey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).