From: Sasha Levin <sashal@kernel.org>
To: Shuah Khan <shuah@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>,
Aleksa Sarai <aleksa@amutable.com>,
John Hubbard <jhubbard@nvidia.com>,
Nathan Chancellor <nathan@kernel.org>,
linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org,
llvm@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH] selftests: probe for sanitizer support in fchmodat2 and openat2
Date: Tue, 3 Nov 2026 09:27:56 -0500 [thread overview]
Message-ID: <20261103142756.132111-1-sashal@kernel.org> (raw)
The fchmodat2 and openat2 tests are always built with -fsanitize=address
and -fsanitize=undefined. Toolchains without the sanitizer runtimes,
such as clang without the compiler-rt package, cannot link them and the
tests fail to build:
ld: cannot find /usr/lib/llvm-21/lib/clang/21/lib/x86_64-unknown-linux-gnu/libclang_rt.asan_static.a: No such file or directory
The same happens with USERLDFLAGS=-static, which AddressSanitizer does
not support. KernelCI's clang builds fail this way on every
architecture.
Link an empty program with the sanitizer flags first, and only add them
when that works. With the runtimes available the tests are built as
before.
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
Tested on x86_64 with gcc 16 and clang 21, building both suites with
O=: with the runtimes installed the binaries still link ASan as before.
With clang pointed at a resource directory without the runtimes, and
with gcc and USERLDFLAGS=-static, the build failed before and now
builds all five programs without the sanitizers. fchmodat2_test and the
openat2 tests give the same results from both builds.
tools/testing/selftests/fchmodat2/Makefile | 18 +++++++++++++-----
.../selftests/filesystems/openat2/Makefile | 18 +++++++++++++-----
2 files changed, 26 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/fchmodat2/Makefile b/tools/testing/selftests/fchmodat2/Makefile
index 4373cea79b794..21f6b0b0cdc8f 100644
--- a/tools/testing/selftests/fchmodat2/Makefile
+++ b/tools/testing/selftests/fchmodat2/Makefile
@@ -1,15 +1,23 @@
# SPDX-License-Identifier: GPL-2.0-or-later
-CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined $(KHDR_INCLUDES)
+CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
+
+TEST_GEN_PROGS := fchmodat2_test
+
+include ../lib.mk
# gcc requires -static-libasan in order to ensure that Address Sanitizer's
# library is the first one loaded. However, clang already statically links the
# Address Sanitizer if -fsanitize is specified. Therefore, simply omit
# -static-libasan for clang builds.
+SANITIZER_CFLAGS := -fsanitize=address -fsanitize=undefined
ifeq ($(LLVM),)
- CFLAGS += -static-libasan
+ SANITIZER_CFLAGS += -static-libasan
endif
-TEST_GEN_PROGS := fchmodat2_test
-
-include ../lib.mk
+# Build without the sanitizers when the toolchain cannot link them.
+ifeq ($(shell echo 'int main(void) { return 0; }' | $(CC) $(CFLAGS) \
+ $(SANITIZER_CFLAGS) $(LDFLAGS) -x c - -o /dev/null >/dev/null 2>&1 && \
+ echo y),y)
+CFLAGS += $(SANITIZER_CFLAGS)
+endif
diff --git a/tools/testing/selftests/filesystems/openat2/Makefile b/tools/testing/selftests/filesystems/openat2/Makefile
index d848aac96bded..2f712dfcbf75c 100644
--- a/tools/testing/selftests/filesystems/openat2/Makefile
+++ b/tools/testing/selftests/filesystems/openat2/Makefile
@@ -1,17 +1,25 @@
# SPDX-License-Identifier: GPL-2.0-or-later
CFLAGS += $(KHDR_INCLUDES)
-CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined $(TOOLS_INCLUDES)
+CFLAGS += -Wall -O2 -g $(TOOLS_INCLUDES)
TEST_GEN_PROGS := openat2_test resolve_test rename_attack_test emptypath_test
+LOCAL_HDRS += helpers.h
+
+include ../../lib.mk
+
# gcc requires -static-libasan in order to ensure that Address Sanitizer's
# library is the first one loaded. However, clang already statically links the
# Address Sanitizer if -fsanitize is specified. Therefore, simply omit
# -static-libasan for clang builds.
+SANITIZER_CFLAGS := -fsanitize=address -fsanitize=undefined
ifeq ($(LLVM),)
- CFLAGS += -static-libasan
+ SANITIZER_CFLAGS += -static-libasan
endif
-LOCAL_HDRS += helpers.h
-
-include ../../lib.mk
+# Build without the sanitizers when the toolchain cannot link them.
+ifeq ($(shell echo 'int main(void) { return 0; }' | $(CC) $(CFLAGS) \
+ $(SANITIZER_CFLAGS) $(LDFLAGS) -x c - -o /dev/null >/dev/null 2>&1 && \
+ echo y),y)
+CFLAGS += $(SANITIZER_CFLAGS)
+endif
--
2.53.0
reply other threads:[~2026-11-03 14:27 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=20261103142756.132111-1-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=aleksa@amutable.com \
--cc=brauner@kernel.org \
--cc=jhubbard@nvidia.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=nathan@kernel.org \
--cc=shuah@kernel.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®