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)
This commit is contained in:
Mark Johnston 2024-10-19 13:55:12 +00:00
parent 7d3f9eb168
commit b240f05f13

View file

@ -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
/*