Skip to contentSkip to conversation

rust: `overflow_assert!` macro

PATCH Superseded Antonio Hickey 2 participants 7 messages Aug 19, 2025 – Jun 27 Rust

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover rust: `overflow_assert!` macro

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

Changes in v5:
- Refactored the overflow_assert! macro to forward all tokens
to the assert!, allowing full message formatting and parity
with debug_assert!.
- Improved the documentation of the overflow_assert! macro.
- Updated example use patch to also use a custom message.
- Link to v4: https://lore.kernel.org/all/[email protected]/

Changes in v4:
- Added the ability for callers to supply a custom message to the
overflow assertion.
- Small change to verbage in documentation.
- Link to v3: https://lore.kernel.org/all/[email protected]/

Changes in v3:
- Made the overflow_assert! macro take in expressions instead of
values.
- Fixed path in macro to fully qualify.
- Refactor macro to make the conditional compilation as local as
possible.
- Refactor macro to utilize the cfg! macro like the standard library
does to reduce boilerplate.
- Added an example use case of the overflow_assert! macro
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/

Changes in v2:
- Fixed example doc comment so it doesn't panic.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

Antonio Hickey (2):
rust: kernel: create overflow_assert! macro
rust: uaccess: refactor to use overflow_assert!

 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 44 ++++++++++++++++++++++++++++++++++
 rust/kernel/uaccess.rs         |  4 ++--
 3 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 rust/kernel/overflow_assert.rs
Signature · 1 line
2.50.1
#1 lore raw

PATCH v5 1/2 rust: kernel: create `overflow_assert!` macro

This commit creates a macro for overflow assertions, the use of this macro will avoid local #ifdefs 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.

---
 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 44 ++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs
rust/kernel/lib.rs +1 -0
index ed53169e795c..901e54a509a3 100644
@@ -103,6 +103,7 @@
 pub mod of;
 #[cfg(CONFIG_PM_OPP)]
 pub mod opp;
+pub mod overflow_assert;
 pub mod page;
 #[cfg(CONFIG_PCI)]
 pub mod pci;
rust/kernel/overflow_assert.rs new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH v5 1/2] rust: kernel: create `overflow_assert!` macro

Hi Antonio,

Someone asked about this old issue in GitHub -- let me take the chance to give a couple comments to resurrect this series...

Quoted 2 lines · Antonio Hickey

It seems you didn't reorder the tags as mentioned in v4? Was there a reason for that?

rust/kernel/overflow_assert.rs line 11

Quoted 2 lines
rust/kernel/overflow_assert.rs new file +2 -0
+/// This macro only has effect when `CONFIG_RUST_OVERFLOW_CHECKS`
+/// is enabled, otherwise it expands to a no-op.

This is not exactly true anymore, no? i.e. it was true back before you changed the approach to use cfg!, but now some code will be emitted, even if discarded later.

I would just add " if CONFIG_RUST_OVERFLOW_CHECKS is enabled." in the title (i.e. first paragraph), and remove this paragraph here.

Which is good, because it also means the important bit (i.e. the CONFIG_ part that decides when it is enabled) is in the title, not buried in the third paragraph.

If you can fix that and send a rebased v6, then it would be good (and you will get a Sashiko review by doing so, too! :)

Thanks!

Cheers,
Miguel

#3 lore raw

Re: [PATCH v5 1/2] rust: kernel: create `overflow_assert!` macro

Quoted 5 lines

Hi Antonio,

Someone asked about this old issue in GitHub -- let me take the chance to give a couple comments to resurrect this series...

Hey Miguel,

I've been getting back up to speed on this patch series.

Quoted 5 lines
Quoted 1 line · Antonio Hickey

It seems you didn't reorder the tags as mentioned in v4? Was there a reason for that?

I don't see any valid reason other than just naively missing this on my end, but I'll make sure my v6 reorders the tags as mentioned.

rust/kernel/overflow_assert.rs line 36

Quoted 9 lines
rust/kernel/overflow_assert.rs new file +9 -0
+/// A guard before doing a size computation that could overflow:
+/// ```
+/// fn reserve_for_concat(curr: usize, to_add: usize, cap: usize) {
+///     // If enabled, catch obvious overflow logic 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
+/// }
+/// ```

As I'm re-reviewing my patch for a v6, what are your thought's on this example in the doc's of the macro?

My main worry is the placeholder comments make the example seem incomplete, or are these kind of partial function examples normal?

Cheers,

Antonio Hickey

#4 lore raw

Re: [PATCH v5 1/2] rust: kernel: create `overflow_assert!` macro

Quoted 3 lines · Antonio Hickey

I don't see any valid reason other than just naively missing this on my end, but I'll make sure my v6 reorders the tags as mentioned.

No worries :) I was just curious (i.e. sometimes when reviewing what changed, it is hard to say if there was a reason why something did not happen).

Quoted 5 lines

As I'm re-reviewing my patch for a v6, what are your thought's on this example in the doc's of the macro?

My main worry is the placeholder comments make the example seem incomplete, or are these kind of partial function examples normal?

I think it is fine, i.e. we aim for clarity, and I think the "proceed to grow/append" is clear enough.

Of course, if you can think of a better example that is both complete but small enough (i.e. without a lot of boilerplate), that is also good.

Perhaps add a newline before that comment, so that it is even clearer that it would be code that go after the early checks. Also the first comment could perhaps be

    // If enabled, these will catch overflow errors early:"

(Maybe -- not a native speaker)

Thanks for coming back to this!

Cheers,
Miguel

#5 lore raw

PATCH v5 2/2 rust: uaccess: refactor to use `overflow_assert!`

In reply to Antonio Hickey

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

---
 rust/kernel/uaccess.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
rust/kernel/uaccess.rs +2 -2
index a8fb4764185a..cbf201ebd426 100644
@@ -9,6 +9,7 @@
     bindings,
     error::Result,
     ffi::{c_char, c_void},
+    overflow_assert,
     prelude::*,
     transmute::{AsBytes, FromBytes},
 };
@@ -499,8 +500,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.
Signature · 1 line
2.50.1
#6 lore raw

Re: [PATCH v5 2/2] rust: uaccess: refactor to use `overflow_assert!`

Quoted 10 lines · Antonio Hickey

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

You didn't pick the review tags you were given in v4 -- was there a reason for that? (e.g. one usual reason is that the patch changed a lot, but I don't think that is the case here?)

Cheers,
Miguel