Skip to contentSkip to conversation

rust: kernel: create `overflow_assert!`

PATCH Superseded Antonio Hickey 2 participants 5 messages May 4, 2025 – May 6, 2025 Rust

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover rust: kernel: create `overflow_assert!`

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 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/[email protected]/

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
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/1 rust: kernel: create `overflow_assert!`

This commit creates 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 allows us to document the intent of the assertion more clearly.

---
 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 42 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs
rust/kernel/lib.rs +1 -0
index de07aadd1ff5..feeb99fc4bbd 100644
@@ -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;
rust/kernel/overflow_assert.rs new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.49.0
#2 lore raw
Quoted 2 lines · Antonio Hickey

+/// Overflow assert (i.e. runtime bound check).

Probably this paragraph can be removed, since we have the explanation below, i.e. the first line/title is enough.

Quoted 1 line

+ core::assert!($x <= $y, "overflow assertion failed: {} > {}", $x, $y);

Interesting!

I was thinking of just allowing any expression here. I can see the value in printing the values, but it makes it a bit harder to remember the comparison and is less flexible -- I can imagine we will have other operators being used, or even things that are not tested via an operator.

Perhaps we could have two macros, one for the "any expression" and another for the _le case, like the normal assert has the _eq version. Though we probably want to see the use cases first before deciding on particular variants, so I would just go for the general one first only.

Speaking of use cases -- could you please add a sample second patch with this macro being used somewhere? e.g. see the linked email in the issue (it does not necessarily need to be a "real patch" that applies to existing code, although that would be nice).

Also, please fully qualify the path (::core) since we are in a macro.

Quoted 1 line

+macro_rules! assert_no_overflow {

Shouldn't this be the same name? Does it work?

By the way, would it be possible to have the cfg attribute inside the macro, so that we share the docs and so on? i.e. to make the conditional compilation as local as possible even here :)

i.e. as it is currently done, we can have mistake like not sharing the "signature", and the generated docs will not have the actual docs of the macro if it is disabled.

We could also consider using the cfg! macro to force it to be always valid code -- not sure about whether we want that in all cases though. The standard library does that, though, so perhaps we should do the same for consistency. If someone really wants to skip it, they can always do it on the caller side.

Thanks for the patch!

Cheers,
Miguel

#3 lore raw
Quoted 5 lines · Miguel Ojeda

I was thinking of just allowing any expression here. I can see the value in printing the values, but it makes it a bit harder to remember the comparison and is less flexible -- I can imagine we will have other operators being used, or even things that are not tested via an operator.

Oh wow yea this could be so much more flexible allowing any expression, didn't even think of this now my approach feels so naive haha.

Quoted 7 lines

By the way, would it be possible to have the cfg attribute inside the macro, so that we share the docs and so on? i.e. to make the conditional compilation as local as possible even here :)

i.e. as it is currently done, we can have mistake like not sharing the "signature", and the generated docs will not have the actual docs of the macro if it is disabled.

I originally thought we couldn't do conditional compilation in macros, but I'm looking into it now and it seems possible, I'll have to test this.

Quoted 5 lines

We could also consider using the cfg! macro to force it to be always valid code -- not sure about whether we want that in all cases though. The standard library does that, though, so perhaps we should do the same for consistency. If someone really wants to skip it, they can always do it on the caller side.

I do like the idea utilizing the cfg! macro, it's cleaner as it reduces the empty boilerplate currently required. I do wonder though could that potentially cause warnings like unreachable code or unused variables?

#4 lore raw
Quoted 5 lines · Antonio Hickey

I do like the idea utilizing the cfg! macro, it's cleaner as it reduces the empty boilerplate currently required. I do wonder though could that potentially cause warnings like unreachable code or unused variables?

Hmm... Isn't it the other way around? i.e. #[cfg] is the one that completely removes code and thus could introduce cases where no other code references something.

As for unreachable, I don't think that triggers with an if false { ... } (it does with e.g. code after a diverging call).

The debug_assert! macro works with cfg!, so unless the compiler does something custom on linting for it, I think it should work for us here too.

Thanks!

Cheers,
Miguel