From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-08.mail-europe.com (mail-08.mail-europe.com [57.129.93.249]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B24B254B18 for ; Thu, 2 Jul 2026 02:18:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.129.93.249 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782958729; cv=none; b=bbvLwdwcDZUva3My+MpzgFa1+veAgvZb/xqKj4DXga9Qc4Pk4iva9lsKOoSSBJ2lGDYm1Y0csaT4LraHznLWVbxQqQSBftzReYKmNMtEysMz3+kYJLbjIUyICz+PfvJprirOJbL9lx5UvqXny2AN3WKxlGnJ2SNY7zC87cDPFXQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782958729; c=relaxed/simple; bh=u98X9Z9w1c6BzzpQp1M3iN9dahtRBhdFHn6doIpOHb8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ELPaYWU0vD0zcND3gd3t0/3ckQAxZ+H6h2PCk1e44DpH3jZ+gnpdR/tcLO9g40pZ6TwQ6Ky0aF9IesnCmYyuh16AGHAqLEf1riqLyaJpnu5sZkBtFgJw2QgwlKGywvi576BaKS89aUgMam+W5EMQ7f1IfzKgl8VtG2IxbZo/w7Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antoniohickey.com; spf=pass smtp.mailfrom=antoniohickey.com; arc=none smtp.client-ip=57.129.93.249 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=antoniohickey.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=antoniohickey.com Date: Thu, 02 Jul 2026 02:18:31 +0000 To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?Q?Onur_=C3=96zkan?= From: Antonio Hickey Cc: Antonio Hickey , Daniel Cote , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org Subject: [PATCH v6 1/2] rust: kernel: create `overflow_assert!` macro Message-ID: <20260702021811.758731-2-contact@antoniohickey.com> In-Reply-To: <20260702021811.758731-1-contact@antoniohickey.com> References: <20260702021811.758731-1-contact@antoniohickey.com> Feedback-ID: 144211621:user:proton X-Pm-Message-ID: d39059f06ab5ff992cbddd5fd875d8557c84aed5 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 Link: https://github.com/Rust-for-Linux/linux/issues/1159 Co-developed-by: Daniel Cote Signed-off-by: Daniel Cote Signed-off-by: Antonio Hickey --- 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.r= s 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 =3D 10; +/// let b: u32 =3D 5; +/// overflow_assert!(a >=3D 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 <=3D cap, "curr=3D{} > cap=3D{}", curr, cap)= ; +/// overflow_assert!(to_add <=3D cap - curr, "would exceed cap: {}+{} = > {}", curr, to_add, cap); +/// // ... then proceed to grow/append +/// } +/// ``` +#[macro_export] +macro_rules! overflow_assert { + ($($arg:tt)*) =3D> { + if cfg!(CONFIG_RUST_OVERFLOW_CHECKS) { + ::core::assert!($($arg)*); + } + }; +} --=20 2.54.0