From: Shuah Khan <skhan@linuxfoundation.org>
To: Wake Liu <wakel@google.com>,
tglx@linutronix.de, kees@kernel.org, shuah@kernel.org,
nathan@kernel.org
Cc: luto@amacapital.net, wad@chromium.org,
nick.desaulniers+lkml@gmail.com, morbo@google.com,
justinstitt@google.com, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
Shuah Khan <skhan@linuxfoundation.org>
Subject: Re: [PATCH] kselftest/harness: Use helper to avoid zero-size memset warning
Date: Wed, 31 Dec 2025 13:31:20 -0700 [thread overview]
Message-ID: <3ee8406b-1789-4a18-8637-120fc0c62a99@linuxfoundation.org> (raw)
In-Reply-To: <20251224084120.249417-1-wakel@google.com>
On 12/24/25 01:41, Wake Liu wrote:
> When building kselftests with a toolchain that enables source
> fortification (e.g., Android's build environment, which uses
> -D_FORTIFY_SOURCE=3), a build failure occurs in tests that use an
> empty FIXTURE().
>
> The root cause is that an empty fixture struct results in
> `sizeof(self_private)` evaluating to 0. The compiler's fortification
> checks then detect the `memset()` call with a compile-time constant size
> of 0, issuing a `-Wuser-defined-warnings` which is promoted to an error
> by `-Werror`.
>
> An initial attempt to guard the call with `if (sizeof(self_private) > 0)`
> was insufficient. The compiler's static analysis is aggressive enough
> to flag the `memset(..., 0)` pattern before evaluating the conditional,
> thus still triggering the error.
>
> To resolve this robustly, this change introduces a `static inline`
> helper function, `__kselftest_memset_safe()`. This function wraps the
> size check and the `memset()` call. By replacing the direct `memset()`
> in the `__TEST_F_IMPL` macro with a call to this helper, we create an
> abstraction boundary. This prevents the compiler's static analyzer from
> "seeing" the problematic pattern at the macro expansion site, resolving
> the build failure.
>
> Build Context:
> Compiler: Android (14488419, +pgo, +bolt, +lto, +mlgo, based on r584948) clang version 22.0.0 (https://android.googlesource.com/toolchain/llvm-project 2d65e4108033380e6fe8e08b1f1826cd2bfb0c99)
> Relevant Options: -O2 -Wall -Werror -D_FORTIFY_SOURCE=3 -target i686-linux-android10000
>
> Test: m kselftest_futex_futex_requeue_pi
>
> Change-Id: If4fdfe6ffcbe9736fbd8f66b2453e8cbbb95e25e
I removed this before applying. In the future remove these
before sending the patch - running checkpatch.pl prompts
you to remove it.
> Signed-off-by: Wake Liu <wakel@google.com>
> ---
> tools/testing/selftests/kselftest_harness.h | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
> index 3f66e862e83eb..159cd6729af33 100644
> --- a/tools/testing/selftests/kselftest_harness.h
> +++ b/tools/testing/selftests/kselftest_harness.h
> @@ -70,6 +70,12 @@
>
> #include "kselftest.h"
>
> +static inline void __kselftest_memset_safe(void *s, int c, size_t n)
> +{
> + if (n > 0)
> + memset(s, c, n);
> +}
> +
> #define TEST_TIMEOUT_DEFAULT 30
>
> /* Utilities exposed to the test definitions */
> @@ -416,7 +422,7 @@
> self = mmap(NULL, sizeof(*self), PROT_READ | PROT_WRITE, \
> MAP_SHARED | MAP_ANONYMOUS, -1, 0); \
> } else { \
> - memset(&self_private, 0, sizeof(self_private)); \
> + __kselftest_memset_safe(&self_private, 0, sizeof(self_private)); \
> self = &self_private; \
> } \
> } \
Applied to linux-kselftest fixes branch for next rc.
thanks,
-- Shuah
prev parent reply other threads:[~2025-12-31 20:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <871plslxsw.ffs@tglx>
2025-12-24 8:41 ` [PATCH] kselftest/harness: Use helper to avoid zero-size memset warning Wake Liu
2025-12-31 20:31 ` Shuah Khan [this message]
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=3ee8406b-1789-4a18-8637-120fc0c62a99@linuxfoundation.org \
--to=skhan@linuxfoundation.org \
--cc=justinstitt@google.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=luto@amacapital.net \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=shuah@kernel.org \
--cc=tglx@linutronix.de \
--cc=wad@chromium.org \
--cc=wakel@google.com \
/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