Skip to contentSkip to conversation

selftests: Fix asan linkage to work with clang

PATCH In review Ryan Roberts 2 participants 2 messages Apr 17, 2024 – Apr 19, 2024 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: Fix asan linkage to work with clang

Both fchmodat2 and openat2 statically link against libasan (for explanation of why, look at the fixes commit). This is done using -static-libasan. However, it turns out that clang uses a different option, -static-libsan. This caused clang to fail to compile the tests.

Fix this by using cc-option to figure out which variant to use.

While we are at it, stop passing helpers.h as a compilation unit. This was causing another clang error due to name conflict with helpers.c's object file. This header shouldn't be directly compiled anyway.

Fixes c652df8a4a9d ("selftests: link libasan statically for tests with -fsanitize=address")

---

Applies on v6.9-rc4.

Thanks,
Ryan

 tools/testing/selftests/fchmodat2/Makefile | 5 ++++-
 tools/testing/selftests/openat2/Makefile   | 7 +++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/fchmodat2/Makefile +4 -1
index 71ec34bf1501..a68bb5a00797 100644
@@ -1,6 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
+# We need this for the "cc-option" macro.
+include ../../../build/Build.include
 
-CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined -static-libasan $(KHDR_INCLUDES)
+CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined $(KHDR_INCLUDES)
+CFLAGS += $(call cc-option, -static-libasan) $(call cc-option, -static-libsan)
 TEST_GEN_PROGS := fchmodat2_test
 
 include ../lib.mk
tools/testing/selftests/openat2/Makefile +5 -2
index 254d676a2689..02af9b6ca5eb 100644
@@ -1,8 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
+# We need this for the "cc-option" macro.
+include ../../../build/Build.include
 
-CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined -static-libasan
+CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined
+CFLAGS += $(call cc-option, -static-libasan) $(call cc-option, -static-libsan)
 TEST_GEN_PROGS := openat2_test resolve_test rename_attack_test
 
 include ../lib.mk
 
-$(TEST_GEN_PROGS): helpers.c helpers.h
+$(TEST_GEN_PROGS): helpers.c

--
2.25.1

Reviewed-by on v1 1/1
#1 lore raw

Thanks for the fix.

Quoted 12 lines · Ryan Roberts

Both fchmodat2 and openat2 statically link against libasan (for explanation of why, look at the fixes commit). This is done using -static-libasan. However, it turns out that clang uses a different option, -static-libsan. This caused clang to fail to compile the tests.

Fix this by using cc-option to figure out which variant to use.

While we are at it, stop passing helpers.h as a compilation unit. This was causing another clang error due to name conflict with helpers.c's object file. This header shouldn't be directly compiled anyway.

Quoted 5 lines
Fixes c652df8a4a9d ("selftests: link libasan statically for tests with -fsanitize=address")
Quoted 48 lines

---

Applies on v6.9-rc4.

Thanks,
Ryan

 tools/testing/selftests/fchmodat2/Makefile | 5 ++++-
 tools/testing/selftests/openat2/Makefile   | 7 +++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/fchmodat2/Makefile +4 -1
index 71ec34bf1501..a68bb5a00797 100644
@@ -1,6 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
+# We need this for the "cc-option" macro.
+include ../../../build/Build.include
 
-CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined -static-libasan $(KHDR_INCLUDES)
+CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined $(KHDR_INCLUDES)
+CFLAGS += $(call cc-option, -static-libasan) $(call cc-option, -static-libsan)
 TEST_GEN_PROGS := fchmodat2_test
 
 include ../lib.mk
tools/testing/selftests/openat2/Makefile +5 -3
index 254d676a2689..02af9b6ca5eb 100644
@@ -1,8 +1,11 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
+# We need this for the "cc-option" macro.
+include ../../../build/Build.include
 
-CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined -static-libasan
+CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined
+CFLAGS += $(call cc-option, -static-libasan) $(call cc-option, -static-libsan)
 TEST_GEN_PROGS := openat2_test resolve_test rename_attack_test
 
 include ../lib.mk
 
-$(TEST_GEN_PROGS): helpers.c helpers.h
+$(TEST_GEN_PROGS): helpers.c
--

2.25.1

Signature · 2 lines
BR,
Muhammad Usama Anjum