Skip to contentSkip to conversation

ubsan, kcsan: don't combine sanitizer with kcov

PATCH Applied Arnd Bergmann 5 participants 11 messages May 5, 2020 – May 22, 2020

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

PATCH 1/1 ubsan, kcsan: don't combine sanitizer with kcov

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

To avoid that case, add a Kconfig dependency. The dependency could go either way, disabling CONFIG_KCOV or CONFIG_UBSAN_BOUNDS when the other is set. I picked the second option here as this seems to have a smaller impact on the resulting kernel.

---
 lib/Kconfig.kcsan | 2 +-
 lib/Kconfig.ubsan | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
lib/Kconfig.kcsan +1 -1
index ea28245c6c1d..8f856c8828d5 100644
@@ -5,7 +5,7 @@ config HAVE_ARCH_KCSAN
 
 menuconfig KCSAN
 	bool "KCSAN: dynamic data race detector"
-	depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+	depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV
 	select STACKTRACE
 	help
 	  The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
lib/Kconfig.ubsan +1 -0
index 929211039bac..f98ef029553e 100644
@@ -29,6 +29,7 @@ config UBSAN_TRAP
 config UBSAN_BOUNDS
 	bool "Perform array index bounds checking"
 	default UBSAN
+	depends on !(CC_IS_CLANG && KCOV)
 	help
 	  This option enables detection of directly indexed out of bounds
 	  array accesses, where the array size is known at compile time.
Signature · 1 line
2.26.0
maintainer
#1 lore raw
Quoted 23 lines · Arnd Bergmann

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

To avoid that case, add a Kconfig dependency. The dependency could go either way, disabling CONFIG_KCOV or CONFIG_UBSAN_BOUNDS when the other is set. I picked the second option here as this seems to have a smaller impact on the resulting kernel.

---
 lib/Kconfig.kcsan | 2 +-
 lib/Kconfig.ubsan | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
lib/Kconfig.kcsan +0 -0
index ea28245c6c1d..8f856c8828d5 100644

lib/Kconfig.kcsan config HAVE_ARCH_KCSAN · line 8

Quoted 5 lines
lib/Kconfig.kcsan +1 -1
@@ -5,7 +5,7 @@ config HAVE_ARCH_KCSAN
 
 menuconfig KCSAN
        bool "KCSAN: dynamic data race detector"
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV

This also disables KCOV with GCC. Why does this not work with KCSAN?

This is a huge problem for us, since syzbot requires KCOV. In fact I've always been building KCSAN kernels with CONFIG_KCOV=y (with GCC or Clang) and cannot reproduce the problem.

lib/Kconfig.ubsan config UBSAN_TRAP · line 32

Quoted 12 lines
lib/Kconfig.kcsan +0 -0
        select STACKTRACE
        help
          The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
lib/Kconfig.ubsan +1 -0
index 929211039bac..f98ef029553e 100644
@@ -29,6 +29,7 @@ config UBSAN_TRAP
 config UBSAN_BOUNDS
        bool "Perform array index bounds checking"
        default UBSAN
+       depends on !(CC_IS_CLANG && KCOV)

Ditto, we really need KCOV for all sanitizers. I also just tried to reproduce the problem but can't.

Which version of clang is causing this? I'm currently using Clang 9. My guess is that we should not fix this by disallowing KCOV, but rather make Clang work with these configs.

Dmitry, can you comment?

Thanks,
-- Marco

Quoted 6 lines
lib/Kconfig.ubsan +0 -1
        help
          This option enables detection of directly indexed out of bounds
          array accesses, where the array size is known at compile time.
--

2.26.0

reviewer
#2 lore raw
Quoted 50 lines · Marco Elver
Quoted 22 lines

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

To avoid that case, add a Kconfig dependency. The dependency could go either way, disabling CONFIG_KCOV or CONFIG_UBSAN_BOUNDS when the other is set. I picked the second option here as this seems to have a smaller impact on the resulting kernel.

---
 lib/Kconfig.kcsan | 2 +-
 lib/Kconfig.ubsan | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
lib/Kconfig.kcsan +0 -0
index ea28245c6c1d..8f856c8828d5 100644

lib/Kconfig.kcsan config HAVE_ARCH_KCSAN · line 8

Quoted 5 lines
lib/Kconfig.kcsan +1 -1
@@ -5,7 +5,7 @@ config HAVE_ARCH_KCSAN
 
 menuconfig KCSAN
        bool "KCSAN: dynamic data race detector"
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV

This also disables KCOV with GCC. Why does this not work with KCSAN?

This is a huge problem for us, since syzbot requires KCOV. In fact I've always been building KCSAN kernels with CONFIG_KCOV=y (with GCC or Clang) and cannot reproduce the problem.

lib/Kconfig.ubsan config UBSAN_TRAP · line 32

Quoted 12 lines
lib/Kconfig.kcsan +0 -0
        select STACKTRACE
        help
          The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
lib/Kconfig.ubsan +1 -0
index 929211039bac..f98ef029553e 100644
@@ -29,6 +29,7 @@ config UBSAN_TRAP
 config UBSAN_BOUNDS
        bool "Perform array index bounds checking"
        default UBSAN
+       depends on !(CC_IS_CLANG && KCOV)

Ditto, we really need KCOV for all sanitizers. I also just tried to reproduce the problem but can't.

Quoted 5 lines

Which version of clang is causing this? I'm currently using Clang 9. My guess is that we should not fix this by disallowing KCOV, but rather make Clang work with these configs.

Dmitry, can you comment?

FWIW I can reproduce both with clang:

$ clang /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=bounds
clang-11: warning: argument unused during compilation:
'-fsanitize-coverage=trace-pc' [-Wunused-command-line-argument]
$ clang /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=thread
clang-11: warning: argument unused during compilation:
'-fsanitize-coverage=trace-pc' [-Wunused-command-line-argument]

with both my disto's 9.0.1 and fresher 11.0.0
(7b80cb7cf45faf462d6193cc41c2cb7ad556600d.

But both work with gcc

$ gcc /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=thread
$ gcc /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=bounds

Is it a known issue in clang?

Can we somehow disable it only for clang and not gcc?

This will immediately break KCSAN on syzbot as it enables KCSAN and KCOV:
https://syzkaller.appspot.com/upstream?manager=ci2-upstream-kcsan-gce

#3 lore raw
Quoted 7 lines · Dmitry Vyukov' via Clang Built Linux
Quoted 6 lines · Marco Elver
Quoted 6 lines

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

Quoted 6 lines
Quoted 6 lines

Review

Quoted 4 lines

menuconfig KCSAN
bool "KCSAN: dynamic data race detector"

(quoted diff) +1 -1
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV

This also disables KCOV with GCC. Why does this not work with KCSAN?

My mistake, this should be kept enabled for gcc. If we can get the combination to work in clang, that's something that should also get enabled.

Quoted 3 lines
Quoted 3 lines

This is a huge problem for us, since syzbot requires KCOV. In fact I've always been building KCSAN kernels with CONFIG_KCOV=y (with GCC or Clang) and cannot reproduce the problem.

I have some local patches that change the way we pick the warning options for each compiler, and enable more of the warnings that are normally disabled.

Maybe -Wunused-command-line-argument is disabled by default?
I only started seeing this problem recently. It's also possible that there are some other options that interact with it so only Kcov+FOO leads to KCSAN being ignored.

Quoted 27 lines
Quoted 8 lines

Ditto, we really need KCOV for all sanitizers. I also just tried to reproduce the problem but can't.

Which version of clang is causing this? I'm currently using Clang 9. My guess is that we should not fix this by disallowing KCOV, but rather make Clang work with these configs.

Dmitry, can you comment?

FWIW I can reproduce both with clang:

$ clang /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=bounds
clang-11: warning: argument unused during compilation:
'-fsanitize-coverage=trace-pc' [-Wunused-command-line-argument]
$ clang /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=thread
clang-11: warning: argument unused during compilation:
'-fsanitize-coverage=trace-pc' [-Wunused-command-line-argument]

with both my disto's 9.0.1 and fresher 11.0.0
(7b80cb7cf45faf462d6193cc41c2cb7ad556600d.

But both work with gcc

$ gcc /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=thread
$ gcc /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=bounds
Quoted 6 lines

Is it a known issue in clang?

Can we somehow disable it only for clang and not gcc?

This will immediately break KCSAN on syzbot as it enables KCSAN and KCOV:
https://syzkaller.appspot.com/upstream?manager=ci2-upstream-kcsan-gce

I can respin the patch with this fixup if you like:

lib/Kconfig.kcsan +1 -1
@@ -5,7 +5,7 @@ config HAVE_ARCH_KCSAN
 
 menuconfig KCSAN
        bool "KCSAN: dynamic data race detector"
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !(KCOV

&& CC_IS_CLANG)
select STACKTRACE
help
The Kernel Concurrency Sanitizer (KCSAN) is a dynamic

As you both say, the combination seems to be quite important, so maybe there is something else that can be to also enable it with clang.

Arnd

maintainer
#4 lore raw
Quoted 15 lines · Arnd Bergmann
Quoted 12 lines · Dmitry Vyukov' via Clang Built Linux
Quoted 11 lines · Marco Elver
Quoted 10 lines

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

menuconfig KCSAN
bool "KCSAN: dynamic data race detector"

(quoted diff) +1 -1
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV
Quoted 4 lines
Quoted 1 line
Quoted 1 line

This also disables KCOV with GCC. Why does this not work with KCSAN?

My mistake, this should be kept enabled for gcc. If we can get the combination to work in clang, that's something that should also get enabled.

See my suggestion below how we might dynamically determine if the combination is supported.

Quoted 11 lines
Quoted 3 lines
Quoted 3 lines

This is a huge problem for us, since syzbot requires KCOV. In fact I've always been building KCSAN kernels with CONFIG_KCOV=y (with GCC or Clang) and cannot reproduce the problem.

I have some local patches that change the way we pick the warning options for each compiler, and enable more of the warnings that are normally disabled.

Maybe -Wunused-command-line-argument is disabled by default?
I only started seeing this problem recently. It's also possible that there are some other options that interact with it so only Kcov+FOO leads to KCSAN being ignored.

I see. It certainly seems quite bad if one or the other option is effectively ignored.

Quoted 39 lines
Quoted 27 lines
Quoted 8 lines

Ditto, we really need KCOV for all sanitizers. I also just tried to reproduce the problem but can't.

Which version of clang is causing this? I'm currently using Clang 9. My guess is that we should not fix this by disallowing KCOV, but rather make Clang work with these configs.

Dmitry, can you comment?

FWIW I can reproduce both with clang:

$ clang /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=bounds
clang-11: warning: argument unused during compilation:
'-fsanitize-coverage=trace-pc' [-Wunused-command-line-argument]
$ clang /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=thread
clang-11: warning: argument unused during compilation:
'-fsanitize-coverage=trace-pc' [-Wunused-command-line-argument]

with both my disto's 9.0.1 and fresher 11.0.0
(7b80cb7cf45faf462d6193cc41c2cb7ad556600d.

But both work with gcc

$ gcc /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=thread
$ gcc /tmp/test.c -c -fsanitize-coverage=trace-pc -fsanitize=bounds
Quoted 6 lines

Is it a known issue in clang?

Can we somehow disable it only for clang and not gcc?

This will immediately break KCSAN on syzbot as it enables KCSAN and KCOV:
https://syzkaller.appspot.com/upstream?manager=ci2-upstream-kcsan-gce

I can respin the patch with this fixup if you like:

lib/Kconfig.kcsan config HAVE_ARCH_KCSAN · line 9

Quoted 6 lines
lib/Kconfig.kcsan +1 -1
@@ -5,7 +5,7 @@ config HAVE_ARCH_KCSAN
 
 menuconfig KCSAN
        bool "KCSAN: dynamic data race detector"
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !(KCOV
 && CC_IS_CLANG)

I wonder if we can just add this: depends on !(KCOV && !$(cc-option,-Werror -fsanitize=thread -fsanitize-coverage=trace-pc))

Similarly for UBSAN.

That way, once Clang supports this combination, we don't need another patch to fix it.

Thanks,
-- Marco

Quoted 8 lines
lib/Kconfig.kcsan +0 -0
        select STACKTRACE
        help

The Kernel Concurrency Sanitizer (KCSAN) is a dynamic

As you both say, the combination seems to be quite important, so maybe there is something else that can be to also enable it with clang.

Arnd

#5 lore raw
Quoted 13 lines · Marco Elver' via Clang Built Linux

lib/Kconfig.kcsan config HAVE_ARCH_KCSAN · line 9

Quoted 9 lines
lib/Kconfig.kcsan +1 -1
@@ -5,7 +5,7 @@ config HAVE_ARCH_KCSAN
 
 menuconfig KCSAN
        bool "KCSAN: dynamic data race detector"
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !(KCOV
 && CC_IS_CLANG)

I wonder if we can just add this: depends on !(KCOV && !$(cc-option,-Werror -fsanitize=thread -fsanitize-coverage=trace-pc))

Quoted 4 lines

Similarly for UBSAN.

That way, once Clang supports this combination, we don't need another patch to fix it.

Good idea. It probably get a little more complicated because kcov uses different flags depending on other options:

kcov-flags-$(CONFIG_CC_HAS_SANCOV_TRACE_PC)     += -fsanitize-coverage=trace-pc
kcov-flags-$(CONFIG_KCOV_ENABLE_COMPARISONS)    += -fsanitize-coverage=trace-cmp
kcov-flags-$(CONFIG_GCC_PLUGIN_SANCOV)          +=

-fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so

Do you have any preference on whether we should make KCSAN or KCOV conditional in this case? It may be easier to move the compiletime check into CONFIG_KCOV_ENABLE_COMPARISONS and CONFIG_CC_HAS_SANCOV_TRACE_PC.

Arnd

maintainer
#6 lore raw
Quoted 28 lines · Arnd Bergmann
Quoted 13 lines · Marco Elver' via Clang Built Linux

lib/Kconfig.kcsan config HAVE_ARCH_KCSAN · line 9

Quoted 9 lines
lib/Kconfig.kcsan +1 -1
@@ -5,7 +5,7 @@ config HAVE_ARCH_KCSAN
 
 menuconfig KCSAN
        bool "KCSAN: dynamic data race detector"
-       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !KCOV
+       depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN && !(KCOV
 && CC_IS_CLANG)

I wonder if we can just add this: depends on !(KCOV && !$(cc-option,-Werror -fsanitize=thread -fsanitize-coverage=trace-pc))

Quoted 4 lines

Similarly for UBSAN.

That way, once Clang supports this combination, we don't need another patch to fix it.

Good idea. It probably get a little more complicated because kcov uses different flags depending on other options:

kcov-flags-$(CONFIG_CC_HAS_SANCOV_TRACE_PC)     += -fsanitize-coverage=trace-pc
kcov-flags-$(CONFIG_KCOV_ENABLE_COMPARISONS)    += -fsanitize-coverage=trace-cmp
kcov-flags-$(CONFIG_GCC_PLUGIN_SANCOV)          +=
Quoted 6 lines

-fplugin=$(objtree)/scripts/gcc-plugins/sancov_plugin.so

Do you have any preference on whether we should make KCSAN or KCOV conditional in this case? It may be easier to move the compiletime check into CONFIG_KCOV_ENABLE_COMPARISONS and CONFIG_CC_HAS_SANCOV_TRACE_PC.

Whichever is easier. I think if we have a config that tries to set both, but then one gets silently disabled, it likely already breaks the usecase. It'd be nice if there was a way to warn about only one being selected so that a developer can then go back and choose the one they're most interested in (or change compiler).

Thanks,
-- Marco

#7 lore raw

PATCH v2 1/1 ubsan, kcsan: don't combine sanitizer with kcov on clang

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

To avoid the warning, check whether clang can handle this correctly or disallow ubsan and kcsan when kcov is enabled.

---
v2: this implements Marco's suggestion to check what the compiler actually supports, and references the bug report I now opened.

Let's wait for replies on that bug report before this gets applied, in case the feedback there changes the conclusion.

---
 lib/Kconfig.kcsan | 11 +++++++++++
 lib/Kconfig.ubsan | 11 +++++++++++
 2 files changed, 22 insertions(+)
lib/Kconfig.kcsan +11 -0
index ea28245c6c1d..a7276035ca0d 100644
@@ -3,9 +3,20 @@
 config HAVE_ARCH_KCSAN
 	bool
 
+config KCSAN_KCOV_BROKEN
+	def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+	depends on CC_IS_CLANG
+	depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=thread -fsanitize-coverage=trace-pc)
+	help
+	  Some versions of clang support either KCSAN and KCOV but not the
+	  combination of the two.
+	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+	  in newer releases.
+
 menuconfig KCSAN
 	bool "KCSAN: dynamic data race detector"
 	depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+	depends on !KCSAN_KCOV_BROKEN
 	select STACKTRACE
 	help
 	  The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
lib/Kconfig.ubsan +11 -0
index 929211039bac..a5ba2fd51823 100644
@@ -26,9 +26,20 @@ config UBSAN_TRAP
 	  the system. For some system builders this is an acceptable
 	  trade-off.
 
+config UBSAN_KCOV_BROKEN
+	def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+	depends on CC_IS_CLANG
+	depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=bounds -fsanitize-coverage=trace-pc)
+	help
+	  Some versions of clang support either UBSAN or KCOV but not the
+	  combination of the two.
+	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+	  in newer releases.
+
 config UBSAN_BOUNDS
 	bool "Perform array index bounds checking"
 	default UBSAN
+	depends on !UBSAN_KCOV_BROKEN
 	help
 	  This option enables detection of directly indexed out of bounds
 	  array accesses, where the array size is known at compile time.
Signature · 1 line
2.26.0
maintainer Acked-by on v2 1/1
#8 lore raw

Re: [PATCH] [v2] ubsan, kcsan: don't combine sanitizer with kcov on clang

Quoted 13 lines · Arnd Bergmann

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

To avoid the warning, check whether clang can handle this correctly or disallow ubsan and kcsan when kcov is enabled.

Quoted 6 lines

---
v2: this implements Marco's suggestion to check what the compiler actually supports, and references the bug report I now opened.

Let's wait for replies on that bug report before this gets applied, in case the feedback there changes the conclusion.

Waiting makes sense, if this is not very urgent.

Thank you!

Quoted 58 lines
---
 lib/Kconfig.kcsan | 11 +++++++++++
 lib/Kconfig.ubsan | 11 +++++++++++
 2 files changed, 22 insertions(+)
lib/Kconfig.kcsan +11 -0
index ea28245c6c1d..a7276035ca0d 100644
@@ -3,9 +3,20 @@
 config HAVE_ARCH_KCSAN
        bool
 
+config KCSAN_KCOV_BROKEN
+       def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+       depends on CC_IS_CLANG
+       depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=thread -fsanitize-coverage=trace-pc)
+       help
+         Some versions of clang support either KCSAN and KCOV but not the
+         combination of the two.
+         See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+         in newer releases.
+
 menuconfig KCSAN
        bool "KCSAN: dynamic data race detector"
        depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+       depends on !KCSAN_KCOV_BROKEN
        select STACKTRACE
        help
          The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
lib/Kconfig.ubsan +11 -1
index 929211039bac..a5ba2fd51823 100644
@@ -26,9 +26,20 @@ config UBSAN_TRAP
          the system. For some system builders this is an acceptable
          trade-off.
 
+config UBSAN_KCOV_BROKEN
+       def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+       depends on CC_IS_CLANG
+       depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=bounds -fsanitize-coverage=trace-pc)
+       help
+         Some versions of clang support either UBSAN or KCOV but not the
+         combination of the two.
+         See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+         in newer releases.
+
 config UBSAN_BOUNDS
        bool "Perform array index bounds checking"
        default UBSAN
+       depends on !UBSAN_KCOV_BROKEN
        help
          This option enables detection of directly indexed out of bounds
          array accesses, where the array size is known at compile time.
--

2.26.0

#9 lore raw

Re: [PATCH] [v2] ubsan, kcsan: don't combine sanitizer with kcov on clang

In reply to Arnd Bergmann
Quoted 6 lines · Arnd Bergmann

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

Quoted 6 lines

To avoid the warning, check whether clang can handle this correctly or disallow ubsan and kcsan when kcov is enabled.

Applied for v5.9 and pushed, thank you!

Thanx, Paul

Quoted 64 lines

---
v2: this implements Marco's suggestion to check what the compiler actually supports, and references the bug report I now opened.

Let's wait for replies on that bug report before this gets applied, in case the feedback there changes the conclusion.

---
 lib/Kconfig.kcsan | 11 +++++++++++
 lib/Kconfig.ubsan | 11 +++++++++++
 2 files changed, 22 insertions(+)
lib/Kconfig.kcsan +11 -0
index ea28245c6c1d..a7276035ca0d 100644
@@ -3,9 +3,20 @@
 config HAVE_ARCH_KCSAN
 	bool
 
+config KCSAN_KCOV_BROKEN
+	def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+	depends on CC_IS_CLANG
+	depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=thread -fsanitize-coverage=trace-pc)
+	help
+	  Some versions of clang support either KCSAN and KCOV but not the
+	  combination of the two.
+	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+	  in newer releases.
+
 menuconfig KCSAN
 	bool "KCSAN: dynamic data race detector"
 	depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+	depends on !KCSAN_KCOV_BROKEN
 	select STACKTRACE
 	help
 	  The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
lib/Kconfig.ubsan +11 -0
index 929211039bac..a5ba2fd51823 100644
@@ -26,9 +26,20 @@ config UBSAN_TRAP
 	  the system. For some system builders this is an acceptable
 	  trade-off.
 
+config UBSAN_KCOV_BROKEN
+	def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+	depends on CC_IS_CLANG
+	depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=bounds -fsanitize-coverage=trace-pc)
+	help
+	  Some versions of clang support either UBSAN or KCOV but not the
+	  combination of the two.
+	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+	  in newer releases.
+
 config UBSAN_BOUNDS
 	bool "Perform array index bounds checking"
 	default UBSAN
+	depends on !UBSAN_KCOV_BROKEN
 	help
 	  This option enables detection of directly indexed out of bounds
 	  array accesses, where the array size is known at compile time.
Signature · 1 line
2.26.0
merge bot Applied
#10 lore raw

[tip: locking/kcsan] ubsan, kcsan: Don't combine sanitizer with kcov on clang

In reply to Arnd Bergmann
The following commit has been merged into the locking/kcsan branch of tip:

The following commit has been merged into the locking/kcsan branch of tip:

Commit-ID:     e87c5783e9e43ec8bd5c0a1cf7cfa4add33603b0
Gitweb:        https://git.kernel.org/tip/e87c5783e9e43ec8bd5c0a1cf7cfa4add33603b0
Author:        Arnd Bergmann <[email protected]>
AuthorDate:    Thu, 21 May 2020 16:20:37 +02:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Fri, 22 May 2020 14:34:26 +02:00

ubsan, kcsan: Don't combine sanitizer with kcov on clang

Clang does not allow -fsanitize-coverage=trace-{pc,cmp} together with -fsanitize=bounds or with ubsan:

clang: error: argument unused during compilation: '-fsanitize-coverage=trace-pc' [-Werror,-Wunused-command-line-argument]
clang: error: argument unused during compilation: '-fsanitize-coverage=trace-cmp' [-Werror,-Wunused-command-line-argument]

To avoid the warning, check whether clang can handle this correctly or disallow ubsan and kcsan when kcov is enabled.

---
 lib/Kconfig.kcsan | 11 +++++++++++
 lib/Kconfig.ubsan | 11 +++++++++++
 2 files changed, 22 insertions(+)
lib/Kconfig.kcsan +11 -0
index 689b6b8..b5d88ac 100644
@@ -3,9 +3,20 @@
 config HAVE_ARCH_KCSAN
 	bool
 
+config KCSAN_KCOV_BROKEN
+	def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+	depends on CC_IS_CLANG
+	depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=thread -fsanitize-coverage=trace-pc)
+	help
+	  Some versions of clang support either KCSAN and KCOV but not the
+	  combination of the two.
+	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+	  in newer releases.
+
 menuconfig KCSAN
 	bool "KCSAN: dynamic data race detector"
 	depends on HAVE_ARCH_KCSAN && DEBUG_KERNEL && !KASAN
+	depends on !KCSAN_KCOV_BROKEN
 	select STACKTRACE
 	help
 	  The Kernel Concurrency Sanitizer (KCSAN) is a dynamic
lib/Kconfig.ubsan +11 -0
index 48469c9..3baea77 100644
@@ -26,9 +26,20 @@ config UBSAN_TRAP
 	  the system. For some system builders this is an acceptable
 	  trade-off.
 
+config UBSAN_KCOV_BROKEN
+	def_bool KCOV && CC_HAS_SANCOV_TRACE_PC
+	depends on CC_IS_CLANG
+	depends on !$(cc-option,-Werror=unused-command-line-argument -fsanitize=bounds -fsanitize-coverage=trace-pc)
+	help
+	  Some versions of clang support either UBSAN or KCOV but not the
+	  combination of the two.
+	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
+	  in newer releases.
+
 config UBSAN_BOUNDS
 	bool "Perform array index bounds checking"
 	default UBSAN
+	depends on !UBSAN_KCOV_BROKEN
 	help
 	  This option enables detection of directly indexed out of bounds
 	  array accesses, where the array size is known at compile time.