mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] selftests: harness: print pointers through uintptr_t
@ 2026-11-03 14:44 Sasha Levin
  0 siblings, 0 replies; only message in thread
From: Sasha Levin @ 2026-11-03 14:44 UTC (permalink / raw)
  To: Kees Cook, Shuah Khan
  Cc: Andy Lutomirski, Will Drewry, Thomas Weißschuh,
	linux-kselftest, linux-kernel

__EXPECT() casts both operands to intmax_t or uintmax_t to print them
when an assertion fails. When an operand is a pointer, as in
ASSERT_NE(ptr, MAP_FAILED) or ASSERT_NULL(ptr), gcc warns about the cast
to an integer of a different size on 32-bit architectures, once for
each case of the signedness switch. A 32-bit arm kselftest build in
KernelCI has over 4000 of these warnings, and a suite that builds with
-Werror fails:

  kselftest_harness.h:797:49: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]

Convert pointers to uintptr_t before widening them. Other types are
cast as before, so the printed values do not change.

Signed-off-by: Sasha Levin <sashal@kernel.org>
---
The -Werror case is the configfs test, which I've also sent a separate
fix for. Testing:

- A test that fails assertions on pointers, signed and unsigned integers
  of each width, bool, an enum and a double prints the same values
  before and after with gcc and clang on x86_64 and with clang on i386.
  The harness selftest still matches its expected output on all three.

- Building alsa, filesystems, iommu, landlock, lsm, mm, nfsd, pidfd and
  seccomp with gcc -m32 goes from 3221 of these warnings to none from
  the harness. 53 remain, from casts in the tests themselves.

- Full native gcc and clang builds of the selftests (without bpf,
  sched_ext and hid) produce the same binaries and warnings as before.

 tools/testing/selftests/kselftest_harness.h | 25 ++++++++++++++-------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index 29a19bc870846..a4404699a6dee 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -762,6 +762,15 @@ static inline void __kselftest_memset_safe(void *s, int c, size_t n)
 
 #define is_signed_var(var)	(!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
 
+/*
+ * Pointers go through uintptr_t before they are widened for printing, as
+ * casting them straight to an integer of a different size warns on 32-bit.
+ * 5 is the pointer type class of __builtin_classify_type().
+ */
+#define __printable_var(var) \
+	__builtin_choose_expr(__builtin_classify_type(var) == 5, \
+			      (uintptr_t)(var), (var))
+
 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
 	/* Avoid multiple evaluation of the cases */ \
 	__typeof__(_expected) __exp = (_expected); \
@@ -770,32 +779,32 @@ static inline void __kselftest_memset_safe(void *s, int c, size_t n)
 		/* Report with actual signedness to avoid weird output. */ \
 		switch (is_signed_var(__exp) * 2 + is_signed_var(__seen)) { \
 		case 0: { \
-			uintmax_t __exp_print = (uintmax_t)__exp; \
-			uintmax_t __seen_print = (uintmax_t)__seen; \
+			uintmax_t __exp_print = (uintmax_t)__printable_var(__exp); \
+			uintmax_t __seen_print = (uintmax_t)__printable_var(__seen); \
 			__TH_LOG("Expected %s (%ju) %s %s (%ju)", \
 				 _expected_str, __exp_print, #_t, \
 				 _seen_str, __seen_print); \
 			break; \
 			} \
 		case 1: { \
-			uintmax_t __exp_print = (uintmax_t)__exp; \
-			intmax_t  __seen_print = (intmax_t)__seen; \
+			uintmax_t __exp_print = (uintmax_t)__printable_var(__exp); \
+			intmax_t  __seen_print = (intmax_t)__printable_var(__seen); \
 			__TH_LOG("Expected %s (%ju) %s %s (%jd)", \
 				 _expected_str, __exp_print, #_t, \
 				 _seen_str, __seen_print); \
 			break; \
 			} \
 		case 2: { \
-			intmax_t  __exp_print = (intmax_t)__exp; \
-			uintmax_t __seen_print = (uintmax_t)__seen; \
+			intmax_t  __exp_print = (intmax_t)__printable_var(__exp); \
+			uintmax_t __seen_print = (uintmax_t)__printable_var(__seen); \
 			__TH_LOG("Expected %s (%jd) %s %s (%ju)", \
 				 _expected_str, __exp_print, #_t, \
 				 _seen_str, __seen_print); \
 			break; \
 			} \
 		case 3: { \
-			intmax_t  __exp_print = (intmax_t)__exp; \
-			intmax_t  __seen_print = (intmax_t)__seen; \
+			intmax_t  __exp_print = (intmax_t)__printable_var(__exp); \
+			intmax_t  __seen_print = (intmax_t)__printable_var(__seen); \
 			__TH_LOG("Expected %s (%jd) %s %s (%jd)", \
 				 _expected_str, __exp_print, #_t, \
 				 _seen_str, __seen_print); \
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-03 14:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 14:44 [PATCH] selftests: harness: print pointers through uintptr_t Sasha Levin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®