git: 3dc6188294dd - main - cdefs: Use __has_feature to gate the definition of __nosanitize*

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Sun, 20 Oct 2024 15:52:54 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=3dc6188294dd4f907f5f63cc3f1a79ea20dba99f

commit 3dc6188294dd4f907f5f63cc3f1a79ea20dba99f
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-10-19 13:55:12 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-10-20 14:25:30 +0000

    cdefs: Use __has_feature to gate the definition of __nosanitize*
    
    clang 12 does not implement the coverage sanitizer, and the build fails
    when __attribute__((no_sanitize("coverage"))) is used.
    
    Try to work around the problem by giving __nosanitize* a non-trivial
    definition only when the corresponding sanitizer is actually enabled in
    the build.
    
    Tested by reading disassembly of pmap_update_strided() and pmap_enter()
    in a kernel compiled with "options COVERAGE", and similar sanity checks
    for the other sanitizers.  I also test-booted KASAN and KMSAN kernels in
    amd64 bhyve.
    
    Suggested by:   jrtc27
    Reviewed by:    imp
    Fixes:          a78bacf3b0ec ("cdefs: Add __nosanitizecoverage")
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D47193
---
 sys/sys/cdefs.h | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index 353090db6995..6521a34595cb 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -712,21 +712,33 @@
  * GCC has the nosanitize attribute, but as a function attribute only, and
  * warns on use as a variable attribute.
  */
-#if __has_attribute(no_sanitize) && defined(__clang__)
+#if __has_feature(address_sanitizer) && defined(__clang__)
 #ifdef _KERNEL
-#define __nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
-#define __nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
+#define	__nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
 #else
-#define __nosanitizeaddress	__attribute__((no_sanitize("address")))
-#define __nosanitizememory	__attribute__((no_sanitize("memory")))
+#define	__nosanitizeaddress	__attribute__((no_sanitize("address")))
 #endif
-#define __nosanitizecoverage	__attribute__((no_sanitize("coverage")))
-#define __nosanitizethread	__attribute__((no_sanitize("thread")))
 #else
-#define __nosanitizeaddress
-#define __nosanitizecoverage
-#define __nosanitizememory
-#define __nosanitizethread
+#define	__nosanitizeaddress
+#endif
+#if __has_feature(coverage_sanitizer) && defined(__clang__)
+#define	__nosanitizecoverage	__attribute__((no_sanitize("coverage")))
+#else
+#define	__nosanitizecoverage
+#endif
+#if __has_feature(memory_sanitizer) && defined(__clang__)
+#ifdef _KERNEL
+#define	__nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
+#else
+#define	__nosanitizememory	__attribute__((no_sanitize("memory")))
+#endif
+#else
+#define	__nosanitizememory
+#endif
+#if __has_feature(thread_sanitizer) && defined(__clang__)
+#define	__nosanitizethread	__attribute__((no_sanitize("thread")))
+#else
+#define	__nosanitizethread
 #endif
 
 /*