Skip to contentSkip to conversation

selftests: probe for sanitizer support in fchmodat2 and openat2

PATCH Applied Sasha Levin 2 participants 2 messages Nov 3 – Nov 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: probe for sanitizer support in fchmodat2 and openat2

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.

---
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(-)
tools/testing/selftests/fchmodat2/Makefile +13 -5
index 4373cea79b794..21f6b0b0cdc8f 100644
@@ -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
tools/testing/selftests/filesystems/openat2/Makefile +13 -5
index d848aac96bded..2f712dfcbf75c 100644
@@ -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
Signature · 1 line
2.53.0
maintainer Applied
#1 lore raw
Quoted 8 lines · Sasha Levin

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

[...]

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] selftests: probe for sanitizer support in fchmodat2 and openat2

      https://git.kernel.org/vfs/vfs/c/e00d0eb8e3e7