git: b240f05f13c9 - stable/14 - cdefs: Use __has_feature to gate the definition of __nosanitize*

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 28 Oct 2024 16:59:45 UTC
The branch stable/14 has been updated by markj:

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

commit b240f05f13c9e68fd48e716cd0d5fa2e77aefa11
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-10-19 13:55:12 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-10-28 16:39:50 +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
    
    (cherry picked from commit 3dc6188294dd4f907f5f63cc3f1a79ea20dba99f)
---
 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 4fa4decd7652..1481bc1d89d1 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -872,21 +872,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
 
 /*