Skip to contentSkip to conversation

rust: `overflow_assert!` macro

PATCH In review Antonio Hickey 2 participants 5 messages Jul 2 Rust

lore
Share & tools
lore
#0 lore raw

v6 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 v6:
- Fixed the order of tags.
- Improved the documentation of the overflow_assert! macro.
- Added missing review tags.
- Link to v5: https://lore.kernel.org/all/[email protected]/

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 | 43 ++++++++++++++++++++++++++++++++++
 rust/kernel/uaccess.rs         |  4 ++--
 3 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100644 rust/kernel/overflow_assert.rs
Signature · 1 line
2.54.0
#1 lore raw

PATCH v6 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 | 43 ++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs
rust/kernel/lib.rs +1 -0
index 9512af7156df..3a562b517952 100644
@@ -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;
rust/kernel/overflow_assert.rs new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.54.0
reviewer
#2 lore raw

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

rust/kernel/overflow_assert.rs

Quoted 31 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.

---
 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 43 ++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs
rust/kernel/lib.rs +1 -0
index 9512af7156df..3a562b517952 100644
@@ -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;
rust/kernel/overflow_assert.rs +0 -0

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

Quoted 18 lines

new file mode 100644
index 000000000000..e3e822edda73

rust/kernel/overflow_assert.rs new file +13 -0
@@ -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**

rust/kernel/overflow_assert.rs line 23

Quoted 10 lines
rust/kernel/overflow_assert.rs new file +10 -0
+/// (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

Quoted 20 lines
rust/kernel/overflow_assert.rs new file +20 -0
+/// ```
+///
+/// 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)*);
+        }
+    };
+}
reviewer
#3 lore raw

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

In reply to Antonio Hickey
Quoted 63 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.

---
 rust/kernel/lib.rs             |  1 +
 rust/kernel/overflow_assert.rs | 43 ++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 rust/kernel/overflow_assert.rs
rust/kernel/lib.rs +1 -0
index 9512af7156df..3a562b517952 100644
@@ -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;
rust/kernel/overflow_assert.rs new file +27 -0
new file mode 100644
index 000000000000..e3e822edda73
@@ -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:
+///

rust/kernel/overflow_assert.rs line 37

Quoted 10 lines
rust/kernel/overflow_assert.rs new file +10 -0
+/// ```
+/// 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/[email protected]
on the way to avoid this.

Best,
Gary

#4 lore raw

PATCH v6 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 5f6c4d7a1a51..f04c913f23b6 100644
@@ -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.
Signature · 1 line
2.54.0