Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | f1ea2fa | 2008-08-21 22:26:06 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_COMPILER_SPECIFIC_H_ |
| 6 | #define BASE_COMPILER_SPECIFIC_H_ |
| 7 | |
| 8 | #include "build/build_config.h" |
| 9 | |
Nico Weber | fb053cc | 2020-03-03 13:33:05 | [diff] [blame] | 10 | #if defined(COMPILER_MSVC) && !defined(__clang__) |
Nico Weber | 5979181 | 2019-07-27 04:02:11 | [diff] [blame] | 11 | #error "Only clang-cl is supported on Windows, see https://crbug.com/988071" |
| 12 | #endif |
| 13 | |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 14 | // A wrapper around `__has_attribute`, which can be used to test for |
Jan Wilken Dörrie | f8d479d | 2020-11-23 12:21:13 | [diff] [blame] | 15 | // the presence of an attribute. In case the compiler does not support this |
| 16 | // macro it will simply evaluate to 0. |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 17 | #if defined(__has_attribute) |
| 18 | #define HAS_ATTRIBUTE(x) __has_attribute(x) |
| 19 | #else |
| 20 | #define HAS_ATTRIBUTE(x) 0 |
| 21 | #endif |
| 22 | |
Jann Horn | 9e4b4855 | 2021-03-04 14:34:27 | [diff] [blame] | 23 | // A wrapper around `__has_builtin`, similar to HAS_CPP_ATTRIBUTE. |
| 24 | #if defined(__has_builtin) |
| 25 | #define HAS_BUILTIN(x) __has_builtin(x) |
| 26 | #else |
| 27 | #define HAS_BUILTIN(x) 0 |
| 28 | #endif |
| 29 | |
[email protected] | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 30 | // Annotate a function indicating it should not be inlined. |
| 31 | // Use like: |
| 32 | // NOINLINE void DoStuff() { ... } |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 33 | #if __has_cpp_attribute(gnu::noinline) |
| 34 | #define NOINLINE [[gnu::noinline]] |
| 35 | #elif __has_cpp_attribute(msvc::noinline) |
| 36 | #define NOINLINE [[msvc::noinline]] |
[email protected] | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 37 | #else |
[email protected] | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame] | 38 | #define NOINLINE |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 39 | #endif |
| 40 | |
Jose Dapena Paz | 7cc1b1d4 | 2023-11-08 18:37:28 | [diff] [blame] | 41 | // Annotate a function indicating it should not be optimized. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 42 | #if __has_cpp_attribute(clang::optnone) |
Jose Dapena Paz | 7cc1b1d4 | 2023-11-08 18:37:28 | [diff] [blame] | 43 | #define NOOPT [[clang::optnone]] |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 44 | #elif __has_cpp_attribute(gnu::optimize) |
| 45 | #define NOOPT [[gnu::optimize(0)]] |
Jose Dapena Paz | 7cc1b1d4 | 2023-11-08 18:37:28 | [diff] [blame] | 46 | #else |
| 47 | #define NOOPT |
| 48 | #endif |
| 49 | |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 50 | #if defined(NDEBUG) |
| 51 | #if __has_cpp_attribute(gnu::always_inline) |
| 52 | #define ALWAYS_INLINE [[gnu::always_inline]] inline |
| 53 | #elif defined(COMPILER_MSVC) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 54 | #define ALWAYS_INLINE __forceinline |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 55 | #endif |
| 56 | #endif |
| 57 | #if !defined(ALWAYS_INLINE) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 58 | #define ALWAYS_INLINE inline |
| 59 | #endif |
| 60 | |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 61 | // Annotate a function indicating it should never be tail called. Useful to make |
| 62 | // sure callers of the annotated function are never omitted from call-stacks. |
| 63 | // To provide the complementary behavior (prevent the annotated function from |
| 64 | // being omitted) look at NOINLINE. Also note that this doesn't prevent code |
| 65 | // folding of multiple identical caller functions into a single signature. To |
Bruce Dawson | 7915efd | 2021-01-27 18:07:58 | [diff] [blame] | 66 | // prevent code folding, see NO_CODE_FOLDING() in base/debug/alias.h. |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 67 | // Use like: |
Daniel Cheng | ddf1b22 | 2023-02-02 02:41:52 | [diff] [blame] | 68 | // NOT_TAIL_CALLED void FooBar(); |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 69 | #if __has_cpp_attribute(clang::not_tail_called) |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 70 | #define NOT_TAIL_CALLED [[clang::not_tail_called]] |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 71 | #else |
| 72 | #define NOT_TAIL_CALLED |
| 73 | #endif |
| 74 | |
mikt | 2a4fdf0 | 2024-07-09 18:47:57 | [diff] [blame] | 75 | // Annotate a function indicating it must be tail called. |
| 76 | // Can be used only on return statements, even for functions returning void. |
| 77 | // Caller and callee must have the same number of arguments and its types must |
| 78 | // be "similar". |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 79 | #if __has_cpp_attribute(clang::musttail) |
mikt | 2a4fdf0 | 2024-07-09 18:47:57 | [diff] [blame] | 80 | #define MUSTTAIL [[clang::musttail]] |
| 81 | #else |
| 82 | #define MUSTTAIL |
| 83 | #endif |
| 84 | |
Jan Wilken Dörrie | f8d479d | 2020-11-23 12:21:13 | [diff] [blame] | 85 | // In case the compiler supports it NO_UNIQUE_ADDRESS evaluates to the C++20 |
| 86 | // attribute [[no_unique_address]]. This allows annotating data members so that |
| 87 | // they need not have an address distinct from all other non-static data members |
| 88 | // of its class. |
| 89 | // |
| 90 | // References: |
| 91 | // * https://en.cppreference.com/w/cpp/language/attributes/no_unique_address |
| 92 | // * https://wg21.link/dcl.attr.nouniqueaddr |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 93 | // |
Peter Kasting | 8bc046d2 | 2023-11-14 00:38:03 | [diff] [blame] | 94 | // Unfortunately MSVC ignores [[no_unique_address]] (see |
| 95 | // https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/#msvc-extensions-and-abi), |
| 96 | // and clang-cl matches it for ABI compatibility reasons. We need to prefer |
| 97 | // [[msvc::no_unique_address]] when available if we actually want any effect. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 98 | #if __has_cpp_attribute(msvc::no_unique_address) |
Helmut Januschka | 13cd38b | 2023-12-22 03:31:47 | [diff] [blame] | 99 | #define NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 100 | #elif __has_cpp_attribute(no_unique_address) |
Jan Wilken Dörrie | f8d479d | 2020-11-23 12:21:13 | [diff] [blame] | 101 | #define NO_UNIQUE_ADDRESS [[no_unique_address]] |
| 102 | #else |
| 103 | #define NO_UNIQUE_ADDRESS |
| 104 | #endif |
| 105 | |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 106 | // Tells the compiler a function is using a printf-style format string. |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 107 | // |format_param| is the one-based index of the format string parameter; |
| 108 | // |dots_param| is the one-based index of the "..." parameter. |
| 109 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 110 | // (This is undocumented but matches what the system C headers do.) |
Nico Weber | fc7c8dd | 2019-02-28 21:28:44 | [diff] [blame] | 111 | // For member functions, the implicit this parameter counts as index 1. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 112 | #if __has_cpp_attribute(gnu::format) |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 113 | #define PRINTF_FORMAT(format_param, dots_param) \ |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 114 | [[gnu::format(printf, format_param, dots_param)]] |
[email protected] | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 115 | #else |
| 116 | #define PRINTF_FORMAT(format_param, dots_param) |
| 117 | #endif |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 118 | |
etienneb | 4e9250a | 2016-11-18 18:47:53 | [diff] [blame] | 119 | // Sanitizers annotations. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 120 | #if __has_cpp_attribute(clang::no_sanitize) |
| 121 | #define NO_SANITIZE(sanitizer) [[clang::no_sanitize(sanitizer)]] |
| 122 | #else |
| 123 | #define NO_SANITIZE(sanitizer) |
etienneb | 4e9250a | 2016-11-18 18:47:53 | [diff] [blame] | 124 | #endif |
| 125 | |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 126 | // MemorySanitizer annotations. |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 127 | #if defined(MEMORY_SANITIZER) && !BUILDFLAG(IS_NACL) |
[email protected] | eb82dfb | 2014-02-03 19:51:17 | [diff] [blame] | 128 | #include <sanitizer/msan_interface.h> |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 129 | |
| 130 | // Mark a memory region fully initialized. |
| 131 | // Use this to annotate code that deliberately reads uninitialized data, for |
| 132 | // example a GC scavenging root set pointers from the stack. |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 133 | #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame] | 134 | |
| 135 | // Check a memory region for initializedness, as if it was being used here. |
| 136 | // If any bits are uninitialized, crash with an MSan report. |
| 137 | // Use this to sanitize data which MSan won't be able to track, e.g. before |
| 138 | // passing data to another process via shared memory. |
| 139 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 140 | __msan_check_mem_is_initialized(p, size) |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 141 | #else // MEMORY_SANITIZER |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame] | 142 | #define MSAN_UNPOISON(p, size) |
| 143 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) |
[email protected] | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 144 | #endif // MEMORY_SANITIZER |
| 145 | |
krasin | 825ce48 | 2016-08-27 11:01:11 | [diff] [blame] | 146 | // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons. |
| 147 | #if !defined(DISABLE_CFI_PERF) |
krasin | 40f7c78 | 2016-09-22 19:04:27 | [diff] [blame] | 148 | #if defined(__clang__) && defined(OFFICIAL_BUILD) |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 149 | #define DISABLE_CFI_PERF NO_SANITIZE("cfi") |
krasin | 825ce48 | 2016-08-27 11:01:11 | [diff] [blame] | 150 | #else |
| 151 | #define DISABLE_CFI_PERF |
| 152 | #endif |
| 153 | #endif |
| 154 | |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 155 | // DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks. |
Alex Gough | 3657980 | 2022-07-25 20:20:46 | [diff] [blame] | 156 | // Security Note: if you just need to allow calling of dlsym functions use |
| 157 | // DISABLE_CFI_DLSYM. |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 158 | #if !defined(DISABLE_CFI_ICALL) |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 159 | #if BUILDFLAG(IS_WIN) |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 160 | // Windows also needs __declspec(guard(nocf)). |
| 161 | #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") __declspec(guard(nocf)) |
| 162 | #else |
| 163 | #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") |
| 164 | #endif |
| 165 | #endif |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 166 | |
Alex Gough | 3657980 | 2022-07-25 20:20:46 | [diff] [blame] | 167 | // DISABLE_CFI_DLSYM -- applies DISABLE_CFI_ICALL on platforms where dlsym |
| 168 | // functions must be called. Retains CFI checks on platforms where loaded |
| 169 | // modules participate in CFI (e.g. Windows). |
| 170 | #if !defined(DISABLE_CFI_DLSYM) |
| 171 | #if BUILDFLAG(IS_WIN) |
| 172 | // Windows modules register functions when loaded so can be checked by CFG. |
| 173 | #define DISABLE_CFI_DLSYM |
| 174 | #else |
| 175 | #define DISABLE_CFI_DLSYM DISABLE_CFI_ICALL |
| 176 | #endif |
| 177 | #endif |
Alex Gough | 3657980 | 2022-07-25 20:20:46 | [diff] [blame] | 178 | |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 179 | // Compiler feature-detection. |
jfb | a8dc9dd8 | 2016-04-06 20:20:31 | [diff] [blame] | 180 | // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension |
| 181 | #if defined(__has_feature) |
| 182 | #define HAS_FEATURE(FEATURE) __has_feature(FEATURE) |
| 183 | #else |
| 184 | #define HAS_FEATURE(FEATURE) 0 |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 185 | #endif |
| 186 | |
Alex Clarke | 23c6cf7 | 2018-11-21 13:22:27 | [diff] [blame] | 187 | #if defined(COMPILER_GCC) |
| 188 | #define PRETTY_FUNCTION __PRETTY_FUNCTION__ |
| 189 | #elif defined(COMPILER_MSVC) |
| 190 | #define PRETTY_FUNCTION __FUNCSIG__ |
| 191 | #else |
| 192 | // See https://en.cppreference.com/w/c/language/function_definition#func |
| 193 | #define PRETTY_FUNCTION __func__ |
| 194 | #endif |
| 195 | |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 196 | // Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for |
| 197 | // the specified variable. |
| 198 | // Library-wide alternative is |
| 199 | // 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn |
| 200 | // file. |
| 201 | // |
| 202 | // See "init_stack_vars" in build/config/compiler/BUILD.gn and |
| 203 | // http://crbug.com/977230 |
| 204 | // "init_stack_vars" is enabled for non-official builds and we hope to enable it |
| 205 | // in official build in 2020 as well. The flag writes fixed pattern into |
| 206 | // uninitialized parts of all local variables. In rare cases such initialization |
| 207 | // is undesirable and attribute can be used: |
| 208 | // 1. Degraded performance |
| 209 | // In most cases compiler is able to remove additional stores. E.g. if memory is |
| 210 | // never accessed or properly initialized later. Preserved stores mostly will |
| 211 | // not affect program performance. However if compiler failed on some |
| 212 | // performance critical code we can get a visible regression in a benchmark. |
| 213 | // 2. memset, memcpy calls |
| 214 | // Compiler may replaces some memory writes with memset or memcpy calls. This is |
| 215 | // not -ftrivial-auto-var-init specific, but it can happen more likely with the |
| 216 | // flag. It can be a problem if code is not linked with C run-time library. |
| 217 | // |
| 218 | // Note: The flag is security risk mitigation feature. So in future the |
| 219 | // attribute uses should be avoided when possible. However to enable this |
| 220 | // mitigation on the most of the code we need to be less strict now and minimize |
| 221 | // number of exceptions later. So if in doubt feel free to use attribute, but |
| 222 | // please document the problem for someone who is going to cleanup it later. |
| 223 | // E.g. platform, bot, benchmark or test name in patch description or next to |
| 224 | // the attribute. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 225 | #if __has_cpp_attribute(clang::uninitialized) |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 226 | #define STACK_UNINITIALIZED [[clang::uninitialized]] |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 227 | #elif __has_cpp_attribute(gnu::uninitialized) |
| 228 | #define STACK_UNINITIALIZED [[gnu::uninitialized]] |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 229 | #else |
| 230 | #define STACK_UNINITIALIZED |
| 231 | #endif |
| 232 | |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 233 | // Attribute "no_stack_protector" disables -fstack-protector for the specified |
| 234 | // function. |
| 235 | // |
| 236 | // "stack_protector" is enabled on most POSIX builds. The flag adds a canary |
| 237 | // to each stack frame, which on function return is checked against a reference |
| 238 | // canary. If the canaries do not match, it's likely that a stack buffer |
| 239 | // overflow has occurred, so immediately crashing will prevent exploitation in |
| 240 | // many cases. |
| 241 | // |
| 242 | // In some cases it's desirable to remove this, e.g. on hot functions, or if |
| 243 | // we have purposely changed the reference canary. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 244 | #if __has_cpp_attribute(gnu::no_stack_protector) |
| 245 | #define NO_STACK_PROTECTOR [[gnu::no_stack_protector]] |
| 246 | #elif __has_cpp_attribute(gnu::optimize) |
| 247 | #define NO_STACK_PROTECTOR [[gnu::optimize("-fno-stack-protector")]] |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 248 | #else |
| 249 | #define NO_STACK_PROTECTOR |
| 250 | #endif |
| 251 | |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 252 | // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current |
| 253 | // codepath and any other branching codepaths that might follow. |
| 254 | #if defined(__clang_analyzer__) |
| 255 | inline constexpr bool AnalyzerNoReturn() |
| 256 | #if HAS_ATTRIBUTE(analyzer_noreturn) |
| 257 | __attribute__((analyzer_noreturn)) |
| 258 | #endif |
| 259 | { |
| 260 | return false; |
| 261 | } |
| 262 | #define ANALYZER_SKIP_THIS_PATH() static_cast<void>(::AnalyzerNoReturn()) |
| 263 | #else |
| 264 | // The above definition would be safe even outside the analyzer, but defining |
| 265 | // the macro away entirely avoids the need for the optimizer to eliminate it. |
| 266 | #define ANALYZER_SKIP_THIS_PATH() |
| 267 | #endif |
| 268 | |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 269 | // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints |
| 270 | // to Clang which control what code paths are statically analyzed, |
| 271 | // and is meant to be used in conjunction with assert & assert-like functions. |
| 272 | // The expression is passed straight through if analysis isn't enabled. |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 273 | #if defined(__clang_analyzer__) |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 274 | inline constexpr bool AnalyzerAssumeTrue(bool arg) { |
| 275 | // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is |
| 276 | // false. |
| 277 | return arg || AnalyzerNoReturn(); |
| 278 | } |
George Burgess IV | a09d235d | 2020-04-17 13:32:50 | [diff] [blame] | 279 | #define ANALYZER_ASSUME_TRUE(arg) ::AnalyzerAssumeTrue(!!(arg)) |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 280 | #else |
| 281 | // Again, the above definition is safe, this is just simpler for the optimizer. |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 282 | #define ANALYZER_ASSUME_TRUE(arg) (arg) |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 283 | #endif |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 284 | |
Zequan Wu | 9909f14 | 2021-02-10 03:26:00 | [diff] [blame] | 285 | // Use nomerge attribute to disable optimization of merging multiple same calls. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 286 | #if __has_cpp_attribute(clang::nomerge) |
Zequan Wu | 9909f14 | 2021-02-10 03:26:00 | [diff] [blame] | 287 | #define NOMERGE [[clang::nomerge]] |
| 288 | #else |
| 289 | #define NOMERGE |
| 290 | #endif |
| 291 | |
Jeremy Roman | 810d98d | 2021-04-06 16:46:07 | [diff] [blame] | 292 | // Marks a type as being eligible for the "trivial" ABI despite having a |
| 293 | // non-trivial destructor or copy/move constructor. Such types can be relocated |
| 294 | // after construction by simply copying their memory, which makes them eligible |
| 295 | // to be passed in registers. The canonical example is std::unique_ptr. |
| 296 | // |
| 297 | // Use with caution; this has some subtle effects on constructor/destructor |
| 298 | // ordering and will be very incorrect if the type relies on its address |
| 299 | // remaining constant. When used as a function argument (by value), the value |
| 300 | // may be constructed in the caller's stack frame, passed in a register, and |
| 301 | // then used and destructed in the callee's stack frame. A similar thing can |
| 302 | // occur when values are returned. |
| 303 | // |
| 304 | // TRIVIAL_ABI is not needed for types which have a trivial destructor and |
| 305 | // copy/move constructors, such as base::TimeTicks and other POD. |
| 306 | // |
| 307 | // It is also not likely to be effective on types too large to be passed in one |
| 308 | // or two registers on typical target ABIs. |
| 309 | // |
| 310 | // See also: |
| 311 | // https://clang.llvm.org/docs/AttributeReference.html#trivial-abi |
| 312 | // https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 313 | #if __has_cpp_attribute(clang::trivial_abi) |
Jeremy Roman | 810d98d | 2021-04-06 16:46:07 | [diff] [blame] | 314 | #define TRIVIAL_ABI [[clang::trivial_abi]] |
| 315 | #else |
| 316 | #define TRIVIAL_ABI |
| 317 | #endif |
| 318 | |
Adam Rice | fb288d0 | 2023-10-13 08:36:21 | [diff] [blame] | 319 | // Detect whether a type is trivially relocatable, ie. a move-and-destroy |
| 320 | // sequence can replaced with memmove(). This can be used to optimise the |
| 321 | // implementation of containers. This is automatically true for types that were |
| 322 | // defined with TRIVIAL_ABI such as scoped_refptr. |
| 323 | // |
| 324 | // See also: |
| 325 | // https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p1144r8.html |
| 326 | // https://clang.llvm.org/docs/LanguageExtensions.html#:~:text=__is_trivially_relocatable |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 327 | #if HAS_BUILTIN(__is_trivially_relocatable) |
Adam Rice | fb288d0 | 2023-10-13 08:36:21 | [diff] [blame] | 328 | #define IS_TRIVIALLY_RELOCATABLE(t) __is_trivially_relocatable(t) |
| 329 | #else |
| 330 | #define IS_TRIVIALLY_RELOCATABLE(t) false |
| 331 | #endif |
| 332 | |
Lukasz Anforowicz | 3be38fbb | 2021-04-14 20:29:29 | [diff] [blame] | 333 | // Marks a member function as reinitializing a moved-from variable. |
| 334 | // See also |
Lei Zhang | dd1e6fe | 2024-02-01 08:51:35 | [diff] [blame] | 335 | // https://clang.llvm.org/extra/clang-tidy/checks/bugprone/use-after-move.html#reinitialization |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 336 | #if __has_cpp_attribute(clang::reinitializes) |
Lukasz Anforowicz | 3be38fbb | 2021-04-14 20:29:29 | [diff] [blame] | 337 | #define REINITIALIZES_AFTER_MOVE [[clang::reinitializes]] |
| 338 | #else |
| 339 | #define REINITIALIZES_AFTER_MOVE |
| 340 | #endif |
| 341 | |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 342 | #if __has_cpp_attribute(gsl::Owner) |
Daniel Cheng | 8ac305b | 2022-02-17 00:05:11 | [diff] [blame] | 343 | #define GSL_OWNER [[gsl::Owner]] |
danakj | ceb1702 | 2022-02-11 23:52:01 | [diff] [blame] | 344 | #else |
Jose Dapena Paz | 1183b14 | 2022-02-18 16:28:25 | [diff] [blame] | 345 | #define GSL_OWNER |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 346 | #endif |
| 347 | |
| 348 | #if __has_cpp_attribute(gsl::Pointer) |
| 349 | #define GSL_POINTER [[gsl::Pointer]] |
| 350 | #else |
danakj | ceb1702 | 2022-02-11 23:52:01 | [diff] [blame] | 351 | #define GSL_POINTER |
| 352 | #endif |
| 353 | |
Daniel Cheng | f2c0538 | 2022-09-16 02:51:42 | [diff] [blame] | 354 | // Adds the "logically_const" tag to a symbol's mangled name. The "Mutable |
| 355 | // Constants" check [1] detects instances of constants that aren't in .rodata, |
| 356 | // e.g. due to a missing `const`. Using this tag suppresses the check for this |
| 357 | // symbol, allowing it to live outside .rodata without a warning. |
| 358 | // |
| 359 | // [1]: |
| 360 | // https://crsrc.org/c/docs/speed/binary_size/android_binary_size_trybot.md#Mutable-Constants |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 361 | #if __has_cpp_attribute(gnu::abi_tag) |
Anthony Vallee-Dubois | 9dbbbda3 | 2022-08-26 01:25:31 | [diff] [blame] | 362 | #define LOGICALLY_CONST [[gnu::abi_tag("logically_const")]] |
| 363 | #else |
| 364 | #define LOGICALLY_CONST |
| 365 | #endif |
| 366 | |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 367 | // Disable `PRESERVE_MOST` outside AArch64/x64, where it's currently unsupported |
| 368 | // and thus may trigger warnings. |
| 369 | // |
| 370 | // Disable in component builds, since `_dl_runtime_resolve()` clobbers registers |
| 371 | // on platforms where it's used, and the component build is not perf-critical |
| 372 | // anyway; see https://github.com/llvm/llvm-project/issues/105588. |
| 373 | // |
| 374 | // Disable for Win ARM64 due to as-yet-uninvestigated crashes. |
| 375 | // TODO(crbug.com/42204008): Investigate, fix, and re-enable. |
| 376 | #if __has_cpp_attribute(clang::preserve_most) && \ |
| 377 | (defined(ARCH_CPU_ARM64) || defined(ARCH_CPU_X86_64)) && \ |
| 378 | !defined(COMPONENT_BUILD) && \ |
| 379 | !(BUILDFLAG(IS_WIN) && defined(ARCH_CPU_ARM64)) |
Anton Bikineev | 4d23e84 | 2023-06-14 10:46:19 | [diff] [blame] | 380 | // preserve_most clang's calling convention. Reduces register pressure for the |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 381 | // caller and as such can be used for cold calls. |
kxxt | 120045d | 2024-02-13 04:22:39 | [diff] [blame] | 382 | // Clang only supports preserve_most on X86-64 and AArch64 for now. |
Anton Bikineev | 4d23e84 | 2023-06-14 10:46:19 | [diff] [blame] | 383 | // See https://clang.llvm.org/docs/AttributeReference.html#preserve-most for |
| 384 | // more details. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 385 | #define PRESERVE_MOST [[clang::preserve_most]] |
Anton Bikineev | 4d23e84 | 2023-06-14 10:46:19 | [diff] [blame] | 386 | #else |
| 387 | #define PRESERVE_MOST |
| 388 | #endif |
| 389 | |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 390 | // Mark parameters or return types as having a lifetime attached to the class. |
| 391 | // |
| 392 | // When used to mark a method's pointer/reference parameter, the compiler is |
| 393 | // made aware that it will be stored internally in the class and the pointee |
| 394 | // must outlive the class. Typically used on constructor arguments. It should |
| 395 | // appear to the right of the parameter's variable name. |
| 396 | // |
| 397 | // Example: |
| 398 | // ``` |
| 399 | // struct S { |
| 400 | // S(int* p LIFETIME_BOUND) : ptr_(p) {} |
| 401 | // |
| 402 | // int* ptr_; |
| 403 | // }; |
| 404 | // ``` |
| 405 | // |
| 406 | // When used on a method with a return value, the compiler is made aware that |
| 407 | // the returned type is/has a pointer to the internals of the class, and must |
| 408 | // not outlive the class object. It should appear after any method qualifiers. |
| 409 | // |
| 410 | // Example: |
| 411 | // ``` |
| 412 | // struct S { |
| 413 | // int* GetPtr() const LIFETIME_BOUND { return i_; }; |
| 414 | // |
| 415 | // int i_; |
| 416 | // }; |
| 417 | // ``` |
| 418 | // |
| 419 | // This allows the compiler to warn in (a limited set of) cases where the |
| 420 | // pointer would otherwise be left dangling, especially in cases where the |
| 421 | // pointee would be a destroyed temporary. |
| 422 | // |
| 423 | // Docs: https://clang.llvm.org/docs/AttributeReference.html#lifetimebound |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 424 | #if __has_cpp_attribute(clang::lifetimebound) |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 425 | #define LIFETIME_BOUND [[clang::lifetimebound]] |
| 426 | #else |
| 427 | #define LIFETIME_BOUND |
| 428 | #endif |
| 429 | |
| 430 | // Mark a function as pure, meaning that it does not have side effects, meaning |
| 431 | // that it does not write anything external to the function's local variables |
| 432 | // and return value. |
| 433 | // |
| 434 | // WARNING: If this attribute is mis-used it will result in UB and |
| 435 | // miscompilation, as the optimizator may fold multiple calls into one and |
| 436 | // reorder them inappropriately. This shouldn't appear outside of key vocabulary |
| 437 | // types. It allows callers to work with the vocab type directly, and call its |
| 438 | // methods without having to worry about caching things into local variables in |
| 439 | // hot code. |
| 440 | // |
| 441 | // This attribute must not appear on functions that make use of function |
| 442 | // pointers, virtual methods, or methods of templates (including operators like |
| 443 | // comparison), as the "pure" function can not know what those functions do and |
| 444 | // can not guarantee there will never be sideeffects. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 445 | #if __has_cpp_attribute(gnu::pure) |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 446 | #define PURE_FUNCTION [[gnu::pure]] |
| 447 | #else |
| 448 | #define PURE_FUNCTION |
| 449 | #endif |
| 450 | |
danakj | 59f56d9 | 2024-02-01 15:31:35 | [diff] [blame] | 451 | // Functions should be marked with UNSAFE_BUFFER_USAGE when they lead to |
| 452 | // out-of-bounds bugs when called with incorrect inputs. |
| 453 | // |
| 454 | // Ideally such functions should be paired with a safer version that works with |
| 455 | // safe primitives like `base::span`. Otherwise, another safer coding pattern |
| 456 | // should be documented along side the use of `UNSAFE_BUFFER_USAGE`. |
| 457 | // |
| 458 | // All functions marked with UNSAFE_BUFFER_USAGE should come with a safety |
David Benjamin | 34f6c2d0 | 2024-04-16 17:43:54 | [diff] [blame] | 459 | // comment that explains the requirements of the function to prevent an |
| 460 | // out-of-bounds bug. For example: |
danakj | 59f56d9 | 2024-02-01 15:31:35 | [diff] [blame] | 461 | // ``` |
| 462 | // // Function to do things between `input` and `end`. |
| 463 | // // |
| 464 | // // # Safety |
| 465 | // // The `input` must point to an array with size at least 5. The `end` must |
| 466 | // // point within the same allocation of `input` and not come before `input`. |
| 467 | // ``` |
David Benjamin | 34f6c2d0 | 2024-04-16 17:43:54 | [diff] [blame] | 468 | // |
| 469 | // The requirements described in the safety comment must be sufficient to |
| 470 | // guarantee that the function never goes out of bounds. Annotating a function |
| 471 | // in this way means that all callers will be required to wrap the call in an |
| 472 | // `UNSAFE_BUFFERS()` macro (see below), with a comment justifying how it meets |
| 473 | // the requirements. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 474 | #if __has_cpp_attribute(clang::unsafe_buffer_usage) |
danakj | 59f56d9 | 2024-02-01 15:31:35 | [diff] [blame] | 475 | #define UNSAFE_BUFFER_USAGE [[clang::unsafe_buffer_usage]] |
| 476 | #else |
| 477 | #define UNSAFE_BUFFER_USAGE |
| 478 | #endif |
| 479 | |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 480 | // Test for `__clang__` directly, as there's no `__has_pragma` or similar (see |
| 481 | // https://github.com/llvm/llvm-project/issues/51887). |
| 482 | #if defined(__clang__) |
danakj | 59f56d9 | 2024-02-01 15:31:35 | [diff] [blame] | 483 | // UNSAFE_BUFFERS() wraps code that violates the -Wunsafe-buffer-usage warning, |
| 484 | // such as: |
| 485 | // - pointer arithmetic, |
| 486 | // - pointer subscripting, and |
| 487 | // - calls to functions annotated with UNSAFE_BUFFER_USAGE. |
| 488 | // |
David Benjamin | 34f6c2d0 | 2024-04-16 17:43:54 | [diff] [blame] | 489 | // This indicates code whose bounds correctness cannot be ensured |
| 490 | // systematically, and thus requires manual review. |
| 491 | // |
| 492 | // ** USE OF THIS MACRO SHOULD BE VERY RARE.** This should only be used when |
| 493 | // strictly necessary. Prefer to use `base::span` instead of pointers, or other |
| 494 | // safer coding patterns (like std containers) that avoid the opportunity for |
| 495 | // out-of-bounds bugs to creep into the code. Any use of UNSAFE_BUFFERS() can |
| 496 | // lead to a critical security bug if any assumptions are wrong, or ever become |
| 497 | // wrong in the future. |
danakj | 59f56d9 | 2024-02-01 15:31:35 | [diff] [blame] | 498 | // |
| 499 | // The macro should be used to wrap the minimum necessary code, to make it clear |
| 500 | // what is unsafe, and prevent accidentally opting extra things out of the |
| 501 | // warning. |
| 502 | // |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 503 | // All usage of UNSAFE_BUFFERS() *must* come with a `// SAFETY: ...` comment |
David Benjamin | 34f6c2d0 | 2024-04-16 17:43:54 | [diff] [blame] | 504 | // that explains how we have guaranteed that the pointer usage can never go |
| 505 | // out-of-bounds, or that the requirements of the UNSAFE_BUFFER_USAGE function |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 506 | // are met. The safety comment should allow the chrome security team to check |
| 507 | // that all requirements have been met, using only local invariants. Contact |
| 508 | // [email protected] to schedule such a review. |
| 509 | // |
| 510 | // Examples of local invariants include: |
David Benjamin | 34f6c2d0 | 2024-04-16 17:43:54 | [diff] [blame] | 511 | // - Runtime conditions or CHECKs near the UNSAFE_BUFFERS macros |
| 512 | // - Invariants guaranteed by types in the surrounding code |
| 513 | // - Invariants guaranteed by function calls in the surrounding code |
| 514 | // - Caller requirements, if the containing function is itself marked with |
| 515 | // UNSAFE_BUFFER_USAGE |
| 516 | // |
| 517 | // The last case should be an option of last resort. It is less safe and will |
| 518 | // require the caller also use the UNSAFE_BUFFERS() macro. Prefer directly |
| 519 | // capturing such invariants in types like `base::span`. |
| 520 | // |
| 521 | // Safety explanations may not rely on invariants that are not fully |
| 522 | // encapsulated close to the UNSAFE_BUFFERS() usage. Instead, use safer coding |
| 523 | // patterns or stronger invariants. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 524 | // |
danakj | 59f56d9 | 2024-02-01 15:31:35 | [diff] [blame] | 525 | // Formatting is off so that we can put each _Pragma on its own line, as |
| 526 | // recommended by the gcc docs. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 527 | // clang-format off |
danakj | 59f56d9 | 2024-02-01 15:31:35 | [diff] [blame] | 528 | #define UNSAFE_BUFFERS(...) \ |
| 529 | _Pragma("clang unsafe_buffer_usage begin") \ |
| 530 | __VA_ARGS__ \ |
| 531 | _Pragma("clang unsafe_buffer_usage end") |
| 532 | // clang-format on |
| 533 | #else |
| 534 | #define UNSAFE_BUFFERS(...) __VA_ARGS__ |
| 535 | #endif |
| 536 | |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 537 | // Line-level suppression of unsafe buffers warnings. This gives finer-grained |
| 538 | // control over opting out portions of code from buffer safety checks than the |
| 539 | // file-level pragma. It is used to indicate code that should be re-written for |
| 540 | // safety and makes such sections easy-to-find (contrast this with the |
| 541 | // UNSAFE_BUFFERS macro that indicates code that is expected to remain present |
| 542 | // and has been manually evaluated for safety). Use of this macro can increase |
| 543 | // the number of non-exempt files, and hence prevent new unsafe code from |
| 544 | // being written in them. |
| 545 | #define UNSAFE_TODO(...) UNSAFE_BUFFERS(__VA_ARGS__) |
| 546 | |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 547 | // Defines a condition for a function to be checked at compile time if the |
| 548 | // parameter's value is known at compile time. If the condition is failed, the |
| 549 | // function is omitted from the overload set resolution, much like `requires`. |
| 550 | // |
| 551 | // If the parameter is a runtime value, then the condition is unable to be |
| 552 | // checked and the function will be omitted from the overload set resolution. |
| 553 | // This ensures the function can only be called with values known at compile |
| 554 | // time. This is a clang extension. |
| 555 | // |
| 556 | // Example: |
| 557 | // ``` |
| 558 | // void f(int a) ENABLE_IF_ATTR(a > 0) {} |
| 559 | // f(1); // Ok. |
| 560 | // f(0); // Error: no valid f() found. |
| 561 | // ``` |
| 562 | // |
| 563 | // The `ENABLE_IF_ATTR` annotation is preferred over `consteval` with a check |
| 564 | // that breaks compile because metaprogramming does not observe such checks. So |
| 565 | // with `consteval`, the function looks callable to concepts/type_traits but is |
| 566 | // not and will fail to compile even though it reports it's usable. Whereas |
| 567 | // `ENABLE_IF_ATTR` interacts correctly with metaprogramming. This is especially |
| 568 | // painful for constructors. See also |
| 569 | // https://github.com/chromium/subspace/issues/266. |
Peter Kasting | c3dadb02 | 2024-09-17 22:44:54 | [diff] [blame^] | 570 | #if HAS_ATTRIBUTE(enable_if) |
danakj | c077a30e | 2024-03-22 19:25:36 | [diff] [blame] | 571 | #define ENABLE_IF_ATTR(cond, msg) __attribute__((enable_if(cond, msg))) |
| 572 | #else |
| 573 | #define ENABLE_IF_ATTR(cond, msg) |
| 574 | #endif |
| 575 | |
[email protected] | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 576 | #endif // BASE_COMPILER_SPECIFIC_H_ |