1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_COMPILER_TYPES_H
3 #error "Please don't include <linux/compiler-clang.h> directly, include <linux/compiler.h> instead."
4 #endif
5 
6 /* Compiler specific definitions for Clang compiler */
7 
8 /*
9  * Clang prior to 17 is being silly and considers many __cleanup() variables
10  * as unused (because they are, their sole purpose is to go out of scope).
11  *
12  * https://reviews.llvm.org/D152180
13  */
14 #undef __cleanup
15 #define __cleanup(func) __maybe_unused __attribute__((__cleanup__(func)))
16 
17 /* same as gcc, this was present in clang-2.6 so we can assume it works
18  * with any version that can compile the kernel
19  */
20 #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
21 
22 /* all clang versions usable with the kernel support KASAN ABI version 5 */
23 #define KASAN_ABI_VERSION 5
24 
25 /*
26  * Note: Checking __has_feature(*_sanitizer) is only true if the feature is
27  * enabled. Therefore it is not required to additionally check defined(CONFIG_*)
28  * to avoid adding redundant attributes in other configurations.
29  */
30 
31 #if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
32 /* Emulate GCC's __SANITIZE_ADDRESS__ flag */
33 #define __SANITIZE_ADDRESS__
34 #define __no_sanitize_address \
35 		__attribute__((no_sanitize("address", "hwaddress")))
36 #else
37 #define __no_sanitize_address
38 #endif
39 
40 #if __has_feature(thread_sanitizer)
41 /* emulate gcc's __SANITIZE_THREAD__ flag */
42 #define __SANITIZE_THREAD__
43 #define __no_sanitize_thread \
44 		__attribute__((no_sanitize("thread")))
45 #else
46 #define __no_sanitize_thread
47 #endif
48 
49 #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
50 #define __HAVE_BUILTIN_BSWAP32__
51 #define __HAVE_BUILTIN_BSWAP64__
52 #define __HAVE_BUILTIN_BSWAP16__
53 #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
54 
55 #if __has_feature(undefined_behavior_sanitizer)
56 /* GCC does not have __SANITIZE_UNDEFINED__ */
57 #define __no_sanitize_undefined \
58 		__attribute__((no_sanitize("undefined")))
59 #else
60 #define __no_sanitize_undefined
61 #endif
62 
63 #if __has_feature(memory_sanitizer)
64 #define __SANITIZE_MEMORY__
65 /*
66  * Unlike other sanitizers, KMSAN still inserts code into functions marked with
67  * no_sanitize("kernel-memory"). Using disable_sanitizer_instrumentation
68  * provides the behavior consistent with other __no_sanitize_ attributes,
69  * guaranteeing that __no_sanitize_memory functions remain uninstrumented.
70  */
71 #define __no_sanitize_memory __disable_sanitizer_instrumentation
72 
73 /*
74  * The __no_kmsan_checks attribute ensures that a function does not produce
75  * false positive reports by:
76  *  - initializing all local variables and memory stores in this function;
77  *  - skipping all shadow checks;
78  *  - passing initialized arguments to this function's callees.
79  */
80 #define __no_kmsan_checks __attribute__((no_sanitize("kernel-memory")))
81 #else
82 #define __no_sanitize_memory
83 #define __no_kmsan_checks
84 #endif
85 
86 /*
87  * Support for __has_feature(coverage_sanitizer) was added in Clang 13 together
88  * with no_sanitize("coverage"). Prior versions of Clang support coverage
89  * instrumentation, but cannot be queried for support by the preprocessor.
90  */
91 #if __has_feature(coverage_sanitizer)
92 #define __no_sanitize_coverage __attribute__((no_sanitize("coverage")))
93 #else
94 #define __no_sanitize_coverage
95 #endif
96 
97 #if __has_feature(shadow_call_stack)
98 # define __noscs	__attribute__((__no_sanitize__("shadow-call-stack")))
99 #endif
100 
101 #if __has_feature(kcfi)
102 /* Disable CFI checking inside a function. */
103 #define __nocfi		__attribute__((__no_sanitize__("kcfi")))
104 #endif
105 
106 /*
107  * Turn individual warnings and errors on and off locally, depending
108  * on version.
109  */
110 #define __diag_clang(version, severity, s) \
111 	__diag_clang_ ## version(__diag_clang_ ## severity s)
112 
113 /* Severity used in pragma directives */
114 #define __diag_clang_ignore	ignored
115 #define __diag_clang_warn	warning
116 #define __diag_clang_error	error
117 
118 #define __diag_str1(s)		#s
119 #define __diag_str(s)		__diag_str1(s)
120 #define __diag(s)		_Pragma(__diag_str(clang diagnostic s))
121 
122 #if CONFIG_CLANG_VERSION >= 110000
123 #define __diag_clang_11(s)	__diag(s)
124 #else
125 #define __diag_clang_11(s)
126 #endif
127 
128 #define __diag_ignore_all(option, comment) \
129 	__diag_clang(11, ignore, option)
130