mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: Kees Cook <kees@kernel.org>, Shuah Khan <shuah@kernel.org>
Cc: "Andy Lutomirski" <luto@amacapital.net>,
	"Will Drewry" <wad@chromium.org>,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] selftests: harness: print pointers through uintptr_t
Date: Tue,  3 Nov 2026 09:44:59 -0500	[thread overview]
Message-ID: <20261103144459.194046-1-sashal@kernel.org> (raw)

__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


                 reply	other threads:[~2026-11-03 14:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261103144459.194046-1-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=shuah@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    --cc=wad@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®