mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro
       [not found] <20260702021811.758731-1-contact@antoniohickey.com>
@ 2026-07-02  2:18 ` Antonio Hickey
  2026-07-02 14:48   ` Gary Guo
  2026-07-02 14:50   ` Gary Guo
  2026-07-02  2:18 ` [PATCH v6 2/2] rust: uaccess: refactor to use `overflow_assert!` Antonio Hickey
  1 sibling, 2 replies; 4+ messages in thread
From: Antonio Hickey @ 2026-07-02  2:18 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: Antonio Hickey, Daniel Cote, 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 (like `#[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]`) to the macro.

In addition this macro allows us to document the intent of the assertion
more clearly.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1159
Co-developed-by: Daniel Cote <danielstonecote@gmail.com>
Signed-off-by: Daniel Cote <danielstonecote@gmail.com>
Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
---
 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 43 ++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs

diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 9512af7156df..3a562b517952 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -101,6 +101,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..e3e822edda73
--- /dev/null
+++ b/rust/kernel/overflow_assert.rs
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Overflow assertion.
+
+/// Asserts that a boolean expression is `true` at runtime if
+/// `CONFIG_RUST_OVERFLOW_CHECKS` is enabled.
+///
+/// This will invoke the [`panic!`] macro if the provided
+/// expression cannot be evaluated to `true` at runtime.
+///
+/// This assertion is intended only for extra validation within
+/// builds and environments where panics are acceptable. **Do not
+/// rely on `overflow_assert!` for checks that must *always* execute**
+/// (e.g. to prevent undefined behavior, perform access checks, etc).
+///
+/// # Examples
+///
+/// Basic boolean condition:
+///
+/// ```
+/// let a: u32 = 10;
+/// let b: u32 = 5;
+/// overflow_assert!(a >= b);
+/// ```
+///
+/// A guard before doing a size computation that could overflow:
+///
+/// ```
+/// fn reserve_for_concat(curr: usize, to_add: usize, cap: usize) {
+///     // If enabled, these will catch overflow errors early:
+///     overflow_assert!(curr <= cap, "curr={} > cap={}", curr, cap);
+///     overflow_assert!(to_add <= cap - curr, "would exceed cap: {}+{} > {}", curr, to_add, cap);
+///     // ... then proceed to grow/append
+/// }
+/// ```
+#[macro_export]
+macro_rules! overflow_assert {
+    ($($arg:tt)*) => {
+        if cfg!(CONFIG_RUST_OVERFLOW_CHECKS) {
+            ::core::assert!($($arg)*);
+        }
+    };
+}
-- 
2.54.0



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

* [PATCH v6 2/2] rust: uaccess: refactor to use `overflow_assert!`
       [not found] <20260702021811.758731-1-contact@antoniohickey.com>
  2026-07-02  2:18 ` [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro Antonio Hickey
@ 2026-07-02  2:18 ` Antonio Hickey
  1 sibling, 0 replies; 4+ messages in thread
From: Antonio Hickey @ 2026-07-02  2:18 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: Antonio Hickey, Daniel Cote, Tamir Duberstein, rust-for-linux,
	linux-kernel

Using the `overflow_assert!` macro here adds documentation to
the intent of the assertion, and avoids local `#ifdefs`s by
encapsulating the conditional behavior to the macro itself.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1159
Co-developed-by: Daniel Cote <danielstonecote@gmail.com>
Signed-off-by: Daniel Cote <danielstonecote@gmail.com>
Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
Reviewed-by: Tamir Duberstein <tamird@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/uaccess.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index 5f6c4d7a1a51..f04c913f23b6 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -11,6 +11,7 @@
     error::Result,
     ffi::{c_char, c_void},
     fs::file,
+    overflow_assert,
     prelude::*,
     ptr::KnownSize,
     transmute::{AsBytes, FromBytes},
@@ -655,8 +656,7 @@ fn raw_strncpy_from_user(dst: &mut [MaybeUninit<u8>], src: UserPtr) -> Result<us
         return Err(Error::from_errno(res as i32));
     }
 
-    #[cfg(CONFIG_RUST_OVERFLOW_CHECKS)]
-    assert!(res <= len);
+    overflow_assert!(res <= len, "strncpy_from_user wrote past dst buffer length");
 
     // GUARANTEES: `strncpy_from_user` was successful, so `dst` has contents in accordance with the
     // guarantees of this function.
-- 
2.54.0



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

* Re: [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro
  2026-07-02  2:18 ` [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro Antonio Hickey
@ 2026-07-02 14:48   ` Gary Guo
  2026-07-02 14:50   ` Gary Guo
  1 sibling, 0 replies; 4+ messages in thread
From: Gary Guo @ 2026-07-02 14:48 UTC (permalink / raw)
  To: Antonio Hickey, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: Daniel Cote, linux-kernel, rust-for-linux

On Thu Jul 2, 2026 at 3:18 AM BST, Antonio Hickey wrote:
> 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.
>
> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1159
> Co-developed-by: Daniel Cote <danielstonecote@gmail.com>
> Signed-off-by: Daniel Cote <danielstonecote@gmail.com>
> Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
> ---
>  rust/kernel/lib.rs             |  1 +
>  rust/kernel/overflow_assert.rs | 43 ++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
>  create mode 100644 rust/kernel/overflow_assert.rs
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 9512af7156df..3a562b517952 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -101,6 +101,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

This probably should share a module with other runtime assertion related macros
instead of on its own.

> new file mode 100644
> index 000000000000..e3e822edda73
> --- /dev/null
> +++ b/rust/kernel/overflow_assert.rs
> @@ -0,0 +1,43 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Overflow assertion.
> +
> +/// Asserts that a boolean expression is `true` at runtime if
> +/// `CONFIG_RUST_OVERFLOW_CHECKS` is enabled.
> +///
> +/// This will invoke the [`panic!`] macro if the provided
> +/// expression cannot be evaluated to `true` at runtime.
> +///
> +/// This assertion is intended only for extra validation within
> +/// builds and environments where panics are acceptable. **Do not
> +/// rely on `overflow_assert!` for checks that must *always* execute**
> +/// (e.g. to prevent undefined behavior, perform access checks, etc).
> +///
> +/// # Examples
> +///
> +/// Basic boolean condition:
> +///
> +/// ```
> +/// let a: u32 = 10;
> +/// let b: u32 = 5;
> +/// overflow_assert!(a >= b);

The name "overflow_assert" feels a bit weird to me, somehow it hints to me that
this is asserting that something overflows, rather than just a flavour of
assert.

Not sure what's a better name, though.

Best,
Gary

> +/// ```
> +///
> +/// A guard before doing a size computation that could overflow:
> +///
> +/// ```
> +/// fn reserve_for_concat(curr: usize, to_add: usize, cap: usize) {
> +///     // If enabled, these will catch overflow errors early:
> +///     overflow_assert!(curr <= cap, "curr={} > cap={}", curr, cap);
> +///     overflow_assert!(to_add <= cap - curr, "would exceed cap: {}+{} > {}", curr, to_add, cap);
> +///     // ... then proceed to grow/append
> +/// }
> +/// ```
> +#[macro_export]
> +macro_rules! overflow_assert {
> +    ($($arg:tt)*) => {
> +        if cfg!(CONFIG_RUST_OVERFLOW_CHECKS) {
> +            ::core::assert!($($arg)*);
> +        }
> +    };
> +}



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

* Re: [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro
  2026-07-02  2:18 ` [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro Antonio Hickey
  2026-07-02 14:48   ` Gary Guo
@ 2026-07-02 14:50   ` Gary Guo
  1 sibling, 0 replies; 4+ messages in thread
From: Gary Guo @ 2026-07-02 14:50 UTC (permalink / raw)
  To: Antonio Hickey, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: Daniel Cote, linux-kernel, rust-for-linux

On Thu Jul 2, 2026 at 3:18 AM BST, Antonio Hickey wrote:
> 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.
>
> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1159
> Co-developed-by: Daniel Cote <danielstonecote@gmail.com>
> Signed-off-by: Daniel Cote <danielstonecote@gmail.com>
> Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
> ---
>  rust/kernel/lib.rs             |  1 +
>  rust/kernel/overflow_assert.rs | 43 ++++++++++++++++++++++++++++++++++
>  2 files changed, 44 insertions(+)
>  create mode 100644 rust/kernel/overflow_assert.rs
>
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index 9512af7156df..3a562b517952 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -101,6 +101,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..e3e822edda73
> --- /dev/null
> +++ b/rust/kernel/overflow_assert.rs
> @@ -0,0 +1,43 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Overflow assertion.
> +
> +/// Asserts that a boolean expression is `true` at runtime if
> +/// `CONFIG_RUST_OVERFLOW_CHECKS` is enabled.
> +///
> +/// This will invoke the [`panic!`] macro if the provided
> +/// expression cannot be evaluated to `true` at runtime.
> +///
> +/// This assertion is intended only for extra validation within
> +/// builds and environments where panics are acceptable. **Do not
> +/// rely on `overflow_assert!` for checks that must *always* execute**
> +/// (e.g. to prevent undefined behavior, perform access checks, etc).
> +///
> +/// # Examples
> +///
> +/// Basic boolean condition:
> +///
> +/// ```
> +/// let a: u32 = 10;
> +/// let b: u32 = 5;
> +/// overflow_assert!(a >= b);
> +/// ```
> +///
> +/// A guard before doing a size computation that could overflow:
> +///
> +/// ```
> +/// fn reserve_for_concat(curr: usize, to_add: usize, cap: usize) {
> +///     // If enabled, these will catch overflow errors early:
> +///     overflow_assert!(curr <= cap, "curr={} > cap={}", curr, cap);
> +///     overflow_assert!(to_add <= cap - curr, "would exceed cap: {}+{} > {}", curr, to_add, cap);
> +///     // ... then proceed to grow/append
> +/// }
> +/// ```
> +#[macro_export]
> +macro_rules! overflow_assert {

Also, please avoid adding new macros to the root of kernel crate.

See
https://lore.kernel.org/rust-for-linux/20260312174700.4016015-1-gary@kernel.org
on the way to avoid this.

Best,
Gary

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

end of thread, other threads:[~2026-07-02 14:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20260702021811.758731-1-contact@antoniohickey.com>
2026-07-02  2:18 ` [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro Antonio Hickey
2026-07-02 14:48   ` Gary Guo
2026-07-02 14:50   ` Gary Guo
2026-07-02  2:18 ` [PATCH v6 2/2] rust: uaccess: refactor to use `overflow_assert!` Antonio Hickey

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®