rust: `overflow_assert!` macro
v3 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.
This patch set requires Alice's strncpy_from_user patch[1] linked below, because I use it as an example usecase for the overflow_assert! macro.
Dependent Patch [1]: https://lore.kernel.org/rust-for-linux/[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 | 33 +++++++++++++++++++++++++++++++++ rust/kernel/uaccess.rs | 4 ++-- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 rust/kernel/overflow_assert.rs
Signature · 1 line
2.50.0
PATCH v3 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 | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 rust/kernel/overflow_assert.rs
rust/kernel/lib.rs +1 -0
@@ -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;
rust/kernel/overflow_assert.rs new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Overflow assert. + +/// 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 fn f(x: i32) -> i32 { +/// x + 1 +/// } +/// overflow_assert!(f(40) < 42); +/// ``` +#[macro_export] +macro_rules! overflow_assert { + ($cond:expr) => { +if cfg! (CONFIG_RUST_OVERFLOW_CHECKS ) { + ::core::assert!( + $cond, +concat! ("overflow assertion failed: " ,stringify! ($cond)) + ); + } + }; +}
Signature · 1 line
2.50.0
Re: [PATCH v3 1/2] rust: kernel: create `overflow_assert!` macro
Quoted 37 lines · Antonio Hickey
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.
Co-developed-by Daniel Cote <[email protected]>Signed-off-by Daniel Cote <[email protected]>Signed-off-by Antonio Hickey <[email protected]>Suggested-by Miguel Ojeda <[email protected]>--- rust/kernel/lib.rs | 1 + rust/kernel/overflow_assert.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 rust/kernel/overflow_assert.rsrust/kernel/lib.rs +1
-0@@ -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;rust/kernel/overflow_assert.rs new file +0
-0
rust/kernel/overflow_assert.rs line 3
Quoted 3 lines
rust/kernel/overflow_assert.rs new file +3
-0@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Overflow assert.
s/assert/assertion/
AFAIK the standard library always uses assertion where a noun is needed, and assert where a verb is needed.
Quoted 16 lines
rust/kernel/overflow_assert.rs new file +16
-0+ +/// 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 fn f(x: i32) -> i32 { +/// x + 1
rust/kernel/overflow_assert.rs line 29
Quoted 10 lines
rust/kernel/overflow_assert.rs new file +10
-0+/// } +/// overflow_assert!(f(40) < 42); +/// ``` +#[macro_export] +macro_rules! overflow_assert { + ($cond:expr) => { +if cfg! (CONFIG_RUST_OVERFLOW_CHECKS ) { + ::core::assert!( + $cond, +concat! ("overflow assertion failed: " ,stringify! ($cond))
Can we still allow the caller to pass additional arguments to the macro, so that the overflowing value can be emitted? Alternatively if the expectation is that this macro is always used with a comparison operator perhaps we could have overflow_assert_lt and overflow_assert_le which provide panic messages containing the operand values?
Quoted 9 lines
rust/kernel/overflow_assert.rs new file +4
-1+ ); + } + }; +} --2.50.0
Re: [PATCH v3 1/2] rust: kernel: create `overflow_assert!` macro
Quoted 9 lines · Tamir Duberstein
Quoted 1 line · Antonio Hickey
+//! Overflow assert.
s/assert/assertion/
AFAIK the standard library always uses assertion where a noun is needed, and assert where a verb is needed.
Reasonable, I'll fix this verbage in my next version.
Quoted 24 lines
Quoted 24 lines
rust/kernel/overflow_assert.rs new file +24
-0+/// 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 fn f(x: i32) -> i32 { +/// x + 1 +/// } +/// overflow_assert!(f(40) < 42); +/// ``` +#[macro_export] +macro_rules! overflow_assert { + ($cond:expr) => { +if cfg! (CONFIG_RUST_OVERFLOW_CHECKS ) { + ::core::assert!( + $cond,
Quoted 9 lines
Quoted 1 line
+ concat!("overflow assertion failed: ", stringify!($cond))
Can we still allow the caller to pass additional arguments to the macro, so that the overflowing value can be emitted? Alternatively if the expectation is that this macro is always used with a comparison operator perhaps we could have
overflow_assert_ltandoverflow_assert_lewhich provide panic messages containing the operand values?
Me and Miguel discussed the overflow_assert_le and other variants in my previous v2 patch set[1]. We decided it would be best to just start with a more flexable general expression based variant of the macro for now, and consider other variants later.
I agree we should expand this into more specific variants, so it would document the intent of the assertions even more clearly.
[1] Link to Miguel's comment on a overflow_assert_le variant:
https://lore.kernel.org/all/CANiq72mvu54B=U+YCUmbFctj_wXgF5zjeE-BB-vHVnAP+3mPcQ@mail.gmail.com/
Thanks
Re: [PATCH v3 1/2] rust: kernel: create `overflow_assert!` macro
Quoted 53 lines · Antonio Hickey
Quoted 9 lines · Tamir Duberstein
Quoted 1 line · Antonio Hickey
+//! Overflow assert.
s/assert/assertion/
AFAIK the standard library always uses assertion where a noun is needed, and assert where a verb is needed.
Reasonable, I'll fix this verbage in my next version.
Quoted 24 lines
Quoted 24 lines
rust/kernel/overflow_assert.rs new file +24
-0+/// 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 fn f(x: i32) -> i32 { +/// x + 1 +/// } +/// overflow_assert!(f(40) < 42); +/// ``` +#[macro_export] +macro_rules! overflow_assert { + ($cond:expr) => { +if cfg! (CONFIG_RUST_OVERFLOW_CHECKS ) { + ::core::assert!( + $cond,Quoted 9 lines
Quoted 1 line
+ concat!("overflow assertion failed: ", stringify!($cond))
Can we still allow the caller to pass additional arguments to the macro, so that the overflowing value can be emitted? Alternatively if the expectation is that this macro is always used with a comparison operator perhaps we could have
overflow_assert_ltandoverflow_assert_lewhich provide panic messages containing the operand values?Me and Miguel discussed the
overflow_assert_leand other variants in my previous v2 patch set[1]. We decided it would be best to just start with a more flexable general expression based variant of the macro for now, and consider other variants later.
Quoted 5 lines
I agree we should expand this into more specific variants, so it would document the intent of the assertions even more clearly.
[1] Link to Miguel's comment on a
overflow_assert_levariant:
https://lore.kernel.org/all/CANiq72mvu54B=U+YCUmbFctj_wXgF5zjeE-BB-vHVnAP+3mPcQ@mail.gmail.com/
Ack, thanks for that. Still, I think the "any expression" version should allow the caller to supply a custom message.
Re: [PATCH v3 1/2] rust: kernel: create `overflow_assert!` macro
Quoted 3 lines · Tamir Duberstein
Ack, thanks for that. Still, I think the "any expression" version should allow the caller to supply a custom message.
Agreed, we should add that, like we did for static_assert! recently.
Cheers,
Miguel
PATCH v3 2/2 rust: uaccess: refactor to use `overflow_assert!`
In reply to Antonio HickeyUsing 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
@@ -9,6 +9,7 @@ bindings, error::Result, ffi::{c_char, c_void}, + overflow_assert, prelude::*, transmute::{AsBytes, FromBytes}, };
@@ -394,8 +395,7 @@ fn raw_strncpy_from_user(dst: &mut [MaybeUninit<u8>], src: UserPtr) -> Result<usreturn Err (Error ::from_errno (resas i32 )); } -#[cfg( CONFIG_RUST_OVERFLOW_CHECKS )] -assert! (res <= len); +overflow_assert! (res <= len);// GUARANTEES: `strncpy_from_user` was successful, so `dst` has contents in accordance with the // guarantees of this function.
Signature · 1 line
2.50.0