From nobody Mon Sep 28 23:58:44 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 47FD1346E6D; Sat, 15 Aug 2026 01:58:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786759121; cv=none; b=Ib28YUUBw7TViQTfhIq5iFTFOUwdhleefBOavgo3XOUNwl2hIbKaOqaYjhFjZd9Cpy3a/B6ZlRgnqh+lk3BgWjGJ/417JTs/8w2pE9Gfe7a2xQmaPh9fgvhrRPT8i8yPSbabY67K72QEWdb2+k7FTy8X2V8USedLBSpvxEotW/k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786759121; c=relaxed/simple; bh=qtPsJMNU7QlQ6OuxcXYUU2STv4bugoDWj89ZvxYousI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=cUAgnnXqVBAz7xRWDBvrWzZisgOKS8wtP9ylthfex4kmCZhPqMqUR/IIX/rU1WZV9K0TCdazbEfTut1FAd6CfuIXJLj0/V1rAktDsDLQfBARRKEr6JGq/4xyLNa9pWXwaPUBZ1keSr3gnd+pdcmeRAJnf0Rw6Ts+SyUKTJGw4MY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gEv1Y1xS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gEv1Y1xS" Received: by smtp.kernel.org (Postfix) with ESMTPS id C78AEC2BCB3; Sat, 15 Aug 2026 01:58:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786759120; bh=qtPsJMNU7QlQ6OuxcXYUU2STv4bugoDWj89ZvxYousI=; h=From:Date:Subject:To:Cc:Reply-To:From; b=gEv1Y1xSIfGsSftVVl9jAtSlkituED/FmX/E0/yuIHBoI77BM67b4PGS22DVfN+0A zP4YJVTaX+MaEz2SVZYipCaf7NaRCeRhamkiSf/Ng//2rR+kFlLOn5PGHVEqbx0NAD 0O/oGjbeURBTBZBM1vYcWiFjuBdcrWwm/pRtrDi+U0/v4xS1F30X92J79aQ9vsJXGh jauMuW7t4+c92mf9VOKzM9cWtOnSrtkZrKtoRhSGCdm6103n7gTJmddm4KNq1eQ2Js XRrx7UIES9R3hqE5YWZBNtUkJtiF5tpLp6gk+yAN0fV2G2T1ehFrz7cmPeeZ2FNQof m1FrSirjaC8ZQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1DD7C5CFDB; Sat, 15 Aug 2026 01:58:40 +0000 (UTC) From: Ke Sun via B4 Relay Date: Sat, 15 Aug 2026 09:57:33 +0800 Subject: [PATCH rust-next] rust: fmt: rework pointer formatting tests 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 Message-Id: <20260815-b4-hashedptr-followup-v1-1-83324702b4c9@kylinos.cn> X-B4-Tracking: v=1; b=H4sIAIzHf2oC/x3MQQrDIBBA0auEWXcgMdIpvUrpwtaxCkFlxqSBk LtXunyL/w9QlsQK9+EA4S1pKrljugzwji5/GJPvBjOa63ibLL4sRqeRfW2CoSxL+a4VDTnraaY wE0Fvq3BI+//7AFm1Yea9wfM8fxEsFqFyAAAA X-Change-ID: 20260814-b4-hashedptr-followup-27a4d737f377 To: Miguel Ojeda Cc: Gary Guo , Boqun Feng , =?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?= , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Ke Sun X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1786759119; l=5711; i=sunke@kylinos.cn; s=20260317; h=from:subject:message-id; bh=eAIhBRH7Q3/sEIY/K0BvhST20Ero40p/ar4U8ESK+t4=; b=D2DdMA4Qwa8xYmMUTGuUCAwRex/BTrfuMCZkw6l5YNDY4ru2bBAt6OTxxz5NLLOATdpY/RTgG GNaGgv/peAAD1edGIM/jUP0kr9owxXkuHQVvU4dgyZt3d719GlLUet7 X-Developer-Key: i=sunke@kylinos.cn; a=ed25519; pk=CHcwQp8GSoj25V/L1ZWNSQjWp9eSIb0s9LKr0Nm3WuE= X-Endpoint-Received: by B4 Relay for sunke@kylinos.cn/20260317 with auth_id=950 X-Original-From: Ke Sun Reply-To: sunke@kylinos.cn From: Ke Sun Assert formatted variants against the plain `{:p}` output instead of exact prefixes, and verify width, alignment and clamping also apply when `%p` falls back to the placeholder. Signed-off-by: Ke Sun --- This is a follow-up to the applied v15 series "rust: Add safe pointer formatting support" (on rust-next), addressing the review feedback on the KUnit tests. --- rust/kernel/fmt.rs | 81 +++++++++++++-------------------------------------= ---- 1 file changed, 19 insertions(+), 62 deletions(-) diff --git a/rust/kernel/fmt.rs b/rust/kernel/fmt.rs index 29582b053ab1f..b3902e02f1c1d 100644 --- a/rust/kernel/fmt.rs +++ b/rust/kernel/fmt.rs @@ -215,83 +215,40 @@ fn fmt(&self, f: &mut Formatter<'_>) -> Result { #[macros::kunit_tests(rust_kernel_fmt)] mod tests { use crate::{ - bindings, prelude::fmt, str::CString, // }; =20 #[cfg(CONFIG_64BIT)] - mod expected { - pub(super) const PTR_VALUE: usize =3D 0xffffffffdeadbeef; - pub(super) const PTR_VAL_NO_CRNG: &str =3D "(____ptrval____)"; - pub(super) const HASHED_PREFIX: &str =3D "0x00000000"; - pub(super) const RAW_POINTER: &str =3D "0xffffffffdeadbeef"; - pub(super) const PADDED_RIGHT: &str =3D " 0xffffffffdeadbeef"; - pub(super) const ZERO_PADDED: &str =3D "0x000000ffffffffdeadbeef"; - pub(super) const HASHED_PADDED_RIGHT_PREFIX: &str =3D " "; - pub(super) const HASHED_ZERO_PADDED_PREFIX: &str =3D "0x0000000000= 0000"; - pub(super) const CLAMPED: &str =3D "0x0000000000000ffffffffdeadbee= f"; - } + const PTR_VALUE: usize =3D 0xffffffffdeadbeef; =20 #[cfg(not(CONFIG_64BIT))] - mod expected { - pub(super) const PTR_VALUE: usize =3D 0xdeadbeef; - pub(super) const PTR_VAL_NO_CRNG: &str =3D "(ptrval)"; - pub(super) const HASHED_PREFIX: &str =3D "0x"; - pub(super) const RAW_POINTER: &str =3D "0xdeadbeef"; - pub(super) const PADDED_RIGHT: &str =3D " 0xdeadbeef"; - pub(super) const ZERO_PADDED: &str =3D "0x00000000000000deadbeef"; - pub(super) const HASHED_PADDED_RIGHT_PREFIX: &str =3D " = "; - pub(super) const HASHED_ZERO_PADDED_PREFIX: &str =3D "0x0000000000= 0000"; - pub(super) const CLAMPED: &str =3D "0x0000000000000000000000deadbe= ef"; - } + const PTR_VALUE: usize =3D 0xdeadbeef; =20 #[test] fn test_ptr_formatting() -> core::result::Result<(), crate::error::Err= or> { - let ptr: *const u8 =3D core::ptr::without_provenance(expected::PTR= _VALUE); + let ptr: *const u8 =3D core::ptr::without_provenance(PTR_VALUE); =20 - // SAFETY: `no_hash_pointers` is a global variable that is never c= oncurrently modified =E2=80=94 - // KUnit tests may run at boot (before `mark_readonly()`) or manua= lly afterwards (when the - // variable is read-only). Reading is always safe. - let no_hash =3D unsafe { bindings::no_hash_pointers }; + let cstr =3D CString::try_from_fmt(fmt!("{:p}", ptr))?; + let formatted =3D cstr.to_str()?; =20 - if no_hash { - let cstr =3D CString::try_from_fmt(fmt!("{:p}", ptr))?; - assert_eq!(cstr.to_str()?, expected::RAW_POINTER); + // If the RNG is not ready yet, `%p` falls back to `"(ptrval)"` / = `"(____ptrval____)"`. + let base =3D formatted.strip_prefix("0x").unwrap_or(formatted); =20 - let cstr =3D CString::try_from_fmt(fmt!("{:>24p}", ptr))?; - assert_eq!(cstr.to_str()?, expected::PADDED_RIGHT); + let cstr =3D CString::try_from_fmt(fmt!("{:>24p}", ptr))?; + let padded =3D cstr.to_str()?; + assert!(padded.ends_with(base)); + assert_eq!(padded.len(), 24); =20 - let cstr =3D CString::try_from_fmt(fmt!("{:024p}", ptr))?; - assert_eq!(cstr.to_str()?, expected::ZERO_PADDED); + let cstr =3D CString::try_from_fmt(fmt!("{:024p}", ptr))?; + let zero_padded =3D cstr.to_str()?; + assert!(zero_padded.ends_with(base)); + assert_eq!(zero_padded.len(), 24); =20 - let cstr =3D CString::try_from_fmt(fmt!("{:0100p}", ptr))?; - assert_eq!(cstr.to_str()?, expected::CLAMPED); - } else { - let cstr =3D CString::try_from_fmt(fmt!("{:p}", ptr))?; - let formatted =3D cstr.to_str()?; - // If the RNG is not yet ready, `%p` falls back to a placehold= er. - if formatted =3D=3D expected::PTR_VAL_NO_CRNG { - return Ok(()); - } - assert!(formatted.starts_with(expected::HASHED_PREFIX)); - assert_ne!(formatted, expected::RAW_POINTER); - - let cstr =3D CString::try_from_fmt(fmt!("{:>24p}", ptr))?; - assert!(cstr - .to_str()? - .starts_with(expected::HASHED_PADDED_RIGHT_PREFIX)); - - let cstr =3D CString::try_from_fmt(fmt!("{:024p}", ptr))?; - assert!(cstr - .to_str()? - .starts_with(expected::HASHED_ZERO_PADDED_PREFIX)); - - let cstr =3D CString::try_from_fmt(fmt!("{:0100p}", ptr))?; - let output =3D cstr.to_str()?; - assert!(output.starts_with("0x")); - assert!(!output[2..].chars().all(|c| c =3D=3D '0')); - } + let cstr =3D CString::try_from_fmt(fmt!("{:0100p}", ptr))?; + let clamped =3D cstr.to_str()?; + assert!(clamped.ends_with(base)); + assert_eq!(clamped.len(), 31); =20 Ok(()) } --- base-commit: be387b625933ee12ed230b1c2af5e14f946c3040 change-id: 20260814-b4-hashedptr-followup-27a4d737f377 Best regards, --=20 Ke Sun