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 A9C70156F45; Sat, 15 Aug 2026 05:02:11 +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=1786770131; cv=none; b=FyovfSw2pTIofWlAl5Xngd0VWdI/VB+XsOtzNvNYJ9A+hHBl4cKJaLTyj10uzklYOuQfou1gMSCXA+hviH2+GOyM0uS7DtikZesNBMyWU1eFwm3St1Key5QYIMH1aqCJnSAhbwm9hQB+na58txfkgoKUIfQpehSEEAiWefrxOWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786770131; c=relaxed/simple; bh=gKr1C7dvD8KfZzdbeyurTP9rArSW+LY8tX4oYaAXeTo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=sAHRgaG19b8XbVnFDsc0Z6k68GebcgsZqEU7EqhMnOgbZVlW+60QHwC6Iv1SMly7MpHHFgxHRao5seKYnVFr7ui+fK+q7mAXqs6Ad0gECFzJPsgMy3nXPeTXaIhZo4iS+SWTSqt2s+GzLyyi+YZTAKLSW8fQLC+CNNbna9PcaJ4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e1+4BPuh; 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="e1+4BPuh" Received: by smtp.kernel.org (Postfix) with ESMTPS id 2F1CCC19425; Sat, 15 Aug 2026 05:02:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1786770131; bh=gKr1C7dvD8KfZzdbeyurTP9rArSW+LY8tX4oYaAXeTo=; h=From:Date:Subject:To:Cc:Reply-To:From; b=e1+4BPuhbDOkHuBu32CPaZnXoechrL3AXk5eZa4nZYA7Bx1KQArL+YyqePYRzelV5 TBBqVA93Wyrm453BoJ15KZS7oyD4wWhppK84jQwpq3STX+ctmMA7dLCxeFnEbPZpzg t8pve0lXpt6+HVkT0JTW8FT4l7b2hcuOiadR9/e2aW8/wejsIJ4d6kzQX8DqN9zwCU KC03vDljeQfZsxS+7RuGcNiFKfczMXH4AkxIc9xhMZrpDj0pBsZVtl5GaPTCKnsW9h DcOBfwb91daozDqgzHDCO2VW7FOTGduHhM78Iy/fcMkfotkX5L7tchD1WTF71ubZy5 f4ceIpfs5W5fw== 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 18C99C5CFC1; Sat, 15 Aug 2026 05:02:11 +0000 (UTC) From: Ke Sun via B4 Relay Date: Sat, 15 Aug 2026 13:00:31 +0800 Subject: [PATCH rust-next v2] 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-v2-1-6cf592ad1022@kylinos.cn> X-B4-Tracking: v=1; b=H4sIAG7yf2oC/3WNyw6CMBREf4XctdfQhxZd+R+GBY9ibyRt0xaEE P7dhrh1eTIzZzaIOpCOcC82CHqmSM5m4KcCOtPYl0bqMwMv+bWsmMRWommi0b1PAQc3ju4zeeS qkb0SahBKQd76oAdaDu8TwhQTWr0kqHNkKCYX1uNxZkfhJ7/8kc8MGVZCcKlK3sru9nivI1kXz 52Fet/3L532JeXGAAAA 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=1786770129; l=6669; i=sunke@kylinos.cn; s=20260317; h=from:subject:message-id; bh=qtnzn0XwheOjLSxI3U7X4nlXPdrx6AGWoWdmY9ulHzs=; b=uimAqKU6ONr2K3w/gu+yItFgjOGOseKjcOuKQccHfe0MxfB1w377WBT7+ptRBPnkrvld3Ss/2 LHtVS8bmRT/Djg6s8g8lSI5DOvhN7xyRerlm1isnhcIlPZXFZBJrmCt 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 `test_ptr_formatting` depended on the `no_hash_pointers` setting and skipped the remaining checks when the CRNG was not ready. Assert the formatted variants against the plain `{:p}` output instead, and cover the placeholder path. 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. --- Changes in v2: - Fix the KUnit failure when the CRNG is not ready: `%p` emits the placeholder at a fixed length, ignoring width and zero-padding. - Link to v1: https://lore.kernel.org/r/20260815-b4-hashedptr-followup-v1-1= -83324702b4c9@kylinos.cn --- rust/kernel/fmt.rs | 98 ++++++++++++++++++++------------------------------= ---- 1 file changed, 36 insertions(+), 62 deletions(-) diff --git a/rust/kernel/fmt.rs b/rust/kernel/fmt.rs index 29582b053ab1f..cbdca7183d54b 100644 --- a/rust/kernel/fmt.rs +++ b/rust/kernel/fmt.rs @@ -215,83 +215,57 @@ fn fmt(&self, f: &mut Formatter<'_>) -> Result { #[macros::kunit_tests(rust_kernel_fmt)] mod tests { use crate::{ - bindings, prelude::fmt, str::CString, // }; =20 + // Placeholder used when the CRNG is not ready. #[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_VAL_NO_CRNG: &str =3D "(____ptrval____)"; =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_VAL_NO_CRNG: &str =3D "(ptrval)"; + + #[cfg(CONFIG_64BIT)] + const PTR_VALUE: usize =3D 0xffffffffdeadbeef; + + #[cfg(not(CONFIG_64BIT))] + 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 CRNG is not ready yet, `%p` emits the placeholder at a f= ixed + // length: `f.pad` still applies, zero-padding does not (see `ptr_= to_id()`). + let (crng_ready, expected_suffix) =3D if let Some(suffix) =3D form= atted.strip_prefix("0x") { + (true, suffix) + } else { + (false, formatted) + }; + let (zero_padded_len, clamped_len) =3D if crng_ready { + (24, 31) + } else { + (PTR_VAL_NO_CRNG.len(), PTR_VAL_NO_CRNG.len()) + }; =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(expected_suffix)); + 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(expected_suffix)); + assert_eq!(zero_padded.len(), zero_padded_len); =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(expected_suffix)); + assert_eq!(clamped.len(), clamped_len); =20 Ok(()) } --- base-commit: be387b625933ee12ed230b1c2af5e14f946c3040 change-id: 20260814-b4-hashedptr-followup-27a4d737f377 Best regards, --=20 Ke Sun