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. |
mmentovai@google.com | 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 | |
Jan Wilken Dörrie | f8d479d | 2020-11-23 12:21:13 | [diff] [blame] | 14 | // This is a wrapper around `__has_cpp_attribute`, which can be used to test for |
| 15 | // the presence of an attribute. In case the compiler does not support this |
| 16 | // macro it will simply evaluate to 0. |
| 17 | // |
| 18 | // References: |
| 19 | // https://wg21.link/sd6#testing-for-the-presence-of-an-attribute-__has_cpp_attribute |
| 20 | // https://wg21.link/cpp.cond#:__has_cpp_attribute |
| 21 | #if defined(__has_cpp_attribute) |
| 22 | #define HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) |
| 23 | #else |
| 24 | #define HAS_CPP_ATTRIBUTE(x) 0 |
| 25 | #endif |
| 26 | |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 27 | // A wrapper around `__has_attribute`, similar to HAS_CPP_ATTRIBUTE. |
| 28 | #if defined(__has_attribute) |
| 29 | #define HAS_ATTRIBUTE(x) __has_attribute(x) |
| 30 | #else |
| 31 | #define HAS_ATTRIBUTE(x) 0 |
| 32 | #endif |
| 33 | |
Jann Horn | 9e4b4855 | 2021-03-04 14:34:27 | [diff] [blame] | 34 | // A wrapper around `__has_builtin`, similar to HAS_CPP_ATTRIBUTE. |
| 35 | #if defined(__has_builtin) |
| 36 | #define HAS_BUILTIN(x) __has_builtin(x) |
| 37 | #else |
| 38 | #define HAS_BUILTIN(x) 0 |
| 39 | #endif |
| 40 | |
eroman@chromium.org | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 41 | // Annotate a function indicating it should not be inlined. |
| 42 | // Use like: |
| 43 | // NOINLINE void DoStuff() { ... } |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 44 | #if defined(__clang__) && HAS_ATTRIBUTE(noinline) |
| 45 | #define NOINLINE [[clang::noinline]] |
| 46 | #elif defined(COMPILER_GCC) && HAS_ATTRIBUTE(noinline) |
eroman@chromium.org | 2149cc62 | 2012-02-14 01:12:12 | [diff] [blame] | 47 | #define NOINLINE __attribute__((noinline)) |
| 48 | #elif defined(COMPILER_MSVC) |
| 49 | #define NOINLINE __declspec(noinline) |
| 50 | #else |
davemoore@chromium.org | 50795a0 | 2011-05-09 20:11:01 | [diff] [blame] | 51 | #define NOINLINE |
evan@chromium.org | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 52 | #endif |
| 53 | |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 54 | #if defined(__clang__) && defined(NDEBUG) && HAS_ATTRIBUTE(always_inline) |
| 55 | #define ALWAYS_INLINE [[clang::always_inline]] inline |
| 56 | #elif defined(COMPILER_GCC) && defined(NDEBUG) && HAS_ATTRIBUTE(always_inline) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 57 | #define ALWAYS_INLINE inline __attribute__((__always_inline__)) |
Ivan Krasin | 9c098a0d | 2018-08-05 03:57:57 | [diff] [blame] | 58 | #elif defined(COMPILER_MSVC) && defined(NDEBUG) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 59 | #define ALWAYS_INLINE __forceinline |
| 60 | #else |
| 61 | #define ALWAYS_INLINE inline |
| 62 | #endif |
| 63 | |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 64 | // Annotate a function indicating it should never be tail called. Useful to make |
| 65 | // sure callers of the annotated function are never omitted from call-stacks. |
| 66 | // To provide the complementary behavior (prevent the annotated function from |
| 67 | // being omitted) look at NOINLINE. Also note that this doesn't prevent code |
| 68 | // folding of multiple identical caller functions into a single signature. To |
Bruce Dawson | 7915efd | 2021-01-27 18:07:58 | [diff] [blame] | 69 | // prevent code folding, see NO_CODE_FOLDING() in base/debug/alias.h. |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 70 | // Use like: |
Daniel Cheng | ddf1b22 | 2023-02-02 02:41:52 | [diff] [blame] | 71 | // NOT_TAIL_CALLED void FooBar(); |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 72 | #if defined(__clang__) && HAS_ATTRIBUTE(not_tail_called) |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 73 | #define NOT_TAIL_CALLED [[clang::not_tail_called]] |
Olivier Li | 19d8925 | 2020-05-13 17:57:55 | [diff] [blame] | 74 | #else |
| 75 | #define NOT_TAIL_CALLED |
| 76 | #endif |
| 77 | |
jbates@chromium.org | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 78 | // Specify memory alignment for structs, classes, etc. |
| 79 | // Use like: |
| 80 | // class ALIGNAS(16) MyClass { ... } |
| 81 | // ALIGNAS(16) int array[4]; |
brettw | 16289b3e | 2017-06-13 21:58:40 | [diff] [blame] | 82 | // |
| 83 | // In most places you can use the C++11 keyword "alignas", which is preferred. |
| 84 | // |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 85 | // Historically, compilers had trouble mixing __attribute__((...)) syntax with |
| 86 | // alignas(...) syntax. However, at least Clang is very accepting nowadays. It |
| 87 | // may be that this macro can be removed entirely. |
| 88 | #if defined(__clang__) |
| 89 | #define ALIGNAS(byte_alignment) alignas(byte_alignment) |
| 90 | #elif defined(COMPILER_MSVC) |
jbates@chromium.org | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 91 | #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment)) |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 92 | #elif defined(COMPILER_GCC) && HAS_ATTRIBUTE(aligned) |
jbates@chromium.org | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame] | 93 | #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment))) |
| 94 | #endif |
| 95 | |
Jan Wilken Dörrie | f8d479d | 2020-11-23 12:21:13 | [diff] [blame] | 96 | // In case the compiler supports it NO_UNIQUE_ADDRESS evaluates to the C++20 |
| 97 | // attribute [[no_unique_address]]. This allows annotating data members so that |
| 98 | // they need not have an address distinct from all other non-static data members |
| 99 | // of its class. |
| 100 | // |
| 101 | // References: |
| 102 | // * https://en.cppreference.com/w/cpp/language/attributes/no_unique_address |
| 103 | // * https://wg21.link/dcl.attr.nouniqueaddr |
| 104 | #if HAS_CPP_ATTRIBUTE(no_unique_address) |
| 105 | #define NO_UNIQUE_ADDRESS [[no_unique_address]] |
| 106 | #else |
| 107 | #define NO_UNIQUE_ADDRESS |
| 108 | #endif |
| 109 | |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 110 | // Tells the compiler a function is using a printf-style format string. |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 111 | // |format_param| is the one-based index of the format string parameter; |
| 112 | // |dots_param| is the one-based index of the "..." parameter. |
| 113 | // For v*printf functions (which take a va_list), pass 0 for dots_param. |
| 114 | // (This is undocumented but matches what the system C headers do.) |
Nico Weber | fc7c8dd | 2019-02-28 21:28:44 | [diff] [blame] | 115 | // For member functions, the implicit this parameter counts as index 1. |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 116 | #if (defined(COMPILER_GCC) || defined(__clang__)) && HAS_ATTRIBUTE(format) |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 117 | #define PRINTF_FORMAT(format_param, dots_param) \ |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 118 | __attribute__((format(printf, format_param, dots_param))) |
evan@chromium.org | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 119 | #else |
| 120 | #define PRINTF_FORMAT(format_param, dots_param) |
| 121 | #endif |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 122 | |
| 123 | // WPRINTF_FORMAT is the same, but for wide format strings. |
evan@chromium.org | f5059510 | 2010-10-08 16:20:32 | [diff] [blame] | 124 | // This doesn't appear to yet be implemented in any compiler. |
evan@chromium.org | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 125 | // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 . |
| 126 | #define WPRINTF_FORMAT(format_param, dots_param) |
| 127 | // If available, it would look like: |
| 128 | // __attribute__((format(wprintf, format_param, dots_param))) |
| 129 | |
etienneb | 4e9250a | 2016-11-18 18:47:53 | [diff] [blame] | 130 | // Sanitizers annotations. |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 131 | #if HAS_ATTRIBUTE(no_sanitize) |
etienneb | 4e9250a | 2016-11-18 18:47:53 | [diff] [blame] | 132 | #define NO_SANITIZE(what) __attribute__((no_sanitize(what))) |
| 133 | #endif |
etienneb | 4e9250a | 2016-11-18 18:47:53 | [diff] [blame] | 134 | #if !defined(NO_SANITIZE) |
| 135 | #define NO_SANITIZE(what) |
| 136 | #endif |
| 137 | |
eugenis@chromium.org | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 138 | // MemorySanitizer annotations. |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 139 | #if defined(MEMORY_SANITIZER) && !BUILDFLAG(IS_NACL) |
earthdok@chromium.org | eb82dfb | 2014-02-03 19:51:17 | [diff] [blame] | 140 | #include <sanitizer/msan_interface.h> |
eugenis@chromium.org | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 141 | |
| 142 | // Mark a memory region fully initialized. |
| 143 | // Use this to annotate code that deliberately reads uninitialized data, for |
| 144 | // example a GC scavenging root set pointers from the stack. |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 145 | #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size) |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame] | 146 | |
| 147 | // Check a memory region for initializedness, as if it was being used here. |
| 148 | // If any bits are uninitialized, crash with an MSan report. |
| 149 | // Use this to sanitize data which MSan won't be able to track, e.g. before |
| 150 | // passing data to another process via shared memory. |
| 151 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) \ |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 152 | __msan_check_mem_is_initialized(p, size) |
eugenis@chromium.org | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 153 | #else // MEMORY_SANITIZER |
thestig | 1a42b407 | 2015-03-16 22:36:55 | [diff] [blame] | 154 | #define MSAN_UNPOISON(p, size) |
| 155 | #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size) |
eugenis@chromium.org | 75086be | 2013-03-20 21:18:22 | [diff] [blame] | 156 | #endif // MEMORY_SANITIZER |
| 157 | |
krasin | 825ce48 | 2016-08-27 11:01:11 | [diff] [blame] | 158 | // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons. |
| 159 | #if !defined(DISABLE_CFI_PERF) |
krasin | 40f7c78 | 2016-09-22 19:04:27 | [diff] [blame] | 160 | #if defined(__clang__) && defined(OFFICIAL_BUILD) |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 161 | #define DISABLE_CFI_PERF NO_SANITIZE("cfi") |
krasin | 825ce48 | 2016-08-27 11:01:11 | [diff] [blame] | 162 | #else |
| 163 | #define DISABLE_CFI_PERF |
| 164 | #endif |
| 165 | #endif |
| 166 | |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 167 | // DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks. |
Alex Gough | 3657980 | 2022-07-25 20:20:46 | [diff] [blame] | 168 | // Security Note: if you just need to allow calling of dlsym functions use |
| 169 | // DISABLE_CFI_DLSYM. |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 170 | #if !defined(DISABLE_CFI_ICALL) |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 171 | #if BUILDFLAG(IS_WIN) |
Will Harris | 9a033b0 | 2020-07-11 01:26:54 | [diff] [blame] | 172 | // Windows also needs __declspec(guard(nocf)). |
| 173 | #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") __declspec(guard(nocf)) |
| 174 | #else |
| 175 | #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") |
| 176 | #endif |
| 177 | #endif |
| 178 | #if !defined(DISABLE_CFI_ICALL) |
| 179 | #define DISABLE_CFI_ICALL |
| 180 | #endif |
| 181 | |
Alex Gough | 3657980 | 2022-07-25 20:20:46 | [diff] [blame] | 182 | // DISABLE_CFI_DLSYM -- applies DISABLE_CFI_ICALL on platforms where dlsym |
| 183 | // functions must be called. Retains CFI checks on platforms where loaded |
| 184 | // modules participate in CFI (e.g. Windows). |
| 185 | #if !defined(DISABLE_CFI_DLSYM) |
| 186 | #if BUILDFLAG(IS_WIN) |
| 187 | // Windows modules register functions when loaded so can be checked by CFG. |
| 188 | #define DISABLE_CFI_DLSYM |
| 189 | #else |
| 190 | #define DISABLE_CFI_DLSYM DISABLE_CFI_ICALL |
| 191 | #endif |
| 192 | #endif |
| 193 | #if !defined(DISABLE_CFI_DLSYM) |
| 194 | #define DISABLE_CFI_DLSYM |
| 195 | #endif |
| 196 | |
jochen@chromium.org | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 197 | // Macro useful for writing cross-platform function pointers. |
| 198 | #if !defined(CDECL) |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 199 | #if BUILDFLAG(IS_WIN) |
jochen@chromium.org | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 200 | #define CDECL __cdecl |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 201 | #else // BUILDFLAG(IS_WIN) |
jochen@chromium.org | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 202 | #define CDECL |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 203 | #endif // BUILDFLAG(IS_WIN) |
jochen@chromium.org | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 204 | #endif // !defined(CDECL) |
| 205 | |
skyostil@chromium.org | 2bc0c699 | 2014-02-13 16:11:04 | [diff] [blame] | 206 | // Macro for hinting that an expression is likely to be false. |
| 207 | #if !defined(UNLIKELY) |
Vladimir Levin | 6b77771 | 2017-09-09 00:12:05 | [diff] [blame] | 208 | #if defined(COMPILER_GCC) || defined(__clang__) |
skyostil@chromium.org | 2bc0c699 | 2014-02-13 16:11:04 | [diff] [blame] | 209 | #define UNLIKELY(x) __builtin_expect(!!(x), 0) |
| 210 | #else |
| 211 | #define UNLIKELY(x) (x) |
| 212 | #endif // defined(COMPILER_GCC) |
| 213 | #endif // !defined(UNLIKELY) |
| 214 | |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 215 | #if !defined(LIKELY) |
Vladimir Levin | 6b77771 | 2017-09-09 00:12:05 | [diff] [blame] | 216 | #if defined(COMPILER_GCC) || defined(__clang__) |
Chris Palmer | ad4cb83f | 2016-11-18 20:02:25 | [diff] [blame] | 217 | #define LIKELY(x) __builtin_expect(!!(x), 1) |
palmer | 58184a828 | 2016-11-08 19:15:39 | [diff] [blame] | 218 | #else |
| 219 | #define LIKELY(x) (x) |
| 220 | #endif // defined(COMPILER_GCC) |
| 221 | #endif // !defined(LIKELY) |
| 222 | |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 223 | // Compiler feature-detection. |
jfb | a8dc9dd8 | 2016-04-06 20:20:31 | [diff] [blame] | 224 | // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension |
| 225 | #if defined(__has_feature) |
| 226 | #define HAS_FEATURE(FEATURE) __has_feature(FEATURE) |
| 227 | #else |
| 228 | #define HAS_FEATURE(FEATURE) 0 |
jfb | d81c1ce | 2016-04-05 20:50:35 | [diff] [blame] | 229 | #endif |
| 230 | |
Alex Clarke | 23c6cf7 | 2018-11-21 13:22:27 | [diff] [blame] | 231 | #if defined(COMPILER_GCC) |
| 232 | #define PRETTY_FUNCTION __PRETTY_FUNCTION__ |
| 233 | #elif defined(COMPILER_MSVC) |
| 234 | #define PRETTY_FUNCTION __FUNCSIG__ |
| 235 | #else |
| 236 | // See https://en.cppreference.com/w/c/language/function_definition#func |
| 237 | #define PRETTY_FUNCTION __func__ |
| 238 | #endif |
| 239 | |
Henrique Ferreiro | 6daf71db | 2019-04-03 13:12:42 | [diff] [blame] | 240 | #if !defined(CPU_ARM_NEON) |
| 241 | #if defined(__arm__) |
| 242 | #if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \ |
| 243 | !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID) |
| 244 | #error Chromium does not support middle endian architecture |
| 245 | #endif |
| 246 | #if defined(__ARM_NEON__) |
| 247 | #define CPU_ARM_NEON 1 |
| 248 | #endif |
| 249 | #endif // defined(__arm__) |
| 250 | #endif // !defined(CPU_ARM_NEON) |
| 251 | |
| 252 | #if !defined(HAVE_MIPS_MSA_INTRINSICS) |
| 253 | #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5) |
| 254 | #define HAVE_MIPS_MSA_INTRINSICS 1 |
| 255 | #endif |
| 256 | #endif |
| 257 | |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 258 | #if defined(__clang__) && HAS_ATTRIBUTE(uninitialized) |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 259 | // Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for |
| 260 | // the specified variable. |
| 261 | // Library-wide alternative is |
| 262 | // 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn |
| 263 | // file. |
| 264 | // |
| 265 | // See "init_stack_vars" in build/config/compiler/BUILD.gn and |
| 266 | // http://crbug.com/977230 |
| 267 | // "init_stack_vars" is enabled for non-official builds and we hope to enable it |
| 268 | // in official build in 2020 as well. The flag writes fixed pattern into |
| 269 | // uninitialized parts of all local variables. In rare cases such initialization |
| 270 | // is undesirable and attribute can be used: |
| 271 | // 1. Degraded performance |
| 272 | // In most cases compiler is able to remove additional stores. E.g. if memory is |
| 273 | // never accessed or properly initialized later. Preserved stores mostly will |
| 274 | // not affect program performance. However if compiler failed on some |
| 275 | // performance critical code we can get a visible regression in a benchmark. |
| 276 | // 2. memset, memcpy calls |
| 277 | // Compiler may replaces some memory writes with memset or memcpy calls. This is |
| 278 | // not -ftrivial-auto-var-init specific, but it can happen more likely with the |
| 279 | // flag. It can be a problem if code is not linked with C run-time library. |
| 280 | // |
| 281 | // Note: The flag is security risk mitigation feature. So in future the |
| 282 | // attribute uses should be avoided when possible. However to enable this |
| 283 | // mitigation on the most of the code we need to be less strict now and minimize |
| 284 | // number of exceptions later. So if in doubt feel free to use attribute, but |
| 285 | // please document the problem for someone who is going to cleanup it later. |
| 286 | // E.g. platform, bot, benchmark or test name in patch description or next to |
| 287 | // the attribute. |
Peter Kasting | f541f778 | 2023-03-10 23:44:46 | [diff] [blame] | 288 | #define STACK_UNINITIALIZED [[clang::uninitialized]] |
Vitaly Buka | 2b79076 | 2019-12-20 21:11:48 | [diff] [blame] | 289 | #else |
| 290 | #define STACK_UNINITIALIZED |
| 291 | #endif |
| 292 | |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 293 | // Attribute "no_stack_protector" disables -fstack-protector for the specified |
| 294 | // function. |
| 295 | // |
| 296 | // "stack_protector" is enabled on most POSIX builds. The flag adds a canary |
| 297 | // to each stack frame, which on function return is checked against a reference |
| 298 | // canary. If the canaries do not match, it's likely that a stack buffer |
| 299 | // overflow has occurred, so immediately crashing will prevent exploitation in |
| 300 | // many cases. |
| 301 | // |
| 302 | // In some cases it's desirable to remove this, e.g. on hot functions, or if |
| 303 | // we have purposely changed the reference canary. |
| 304 | #if defined(COMPILER_GCC) || defined(__clang__) |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 305 | #if HAS_ATTRIBUTE(__no_stack_protector__) |
Stephan Hartmann | 4b456e7 | 2021-08-10 03:25:02 | [diff] [blame] | 306 | #define NO_STACK_PROTECTOR __attribute__((__no_stack_protector__)) |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 307 | #else |
Stephan Hartmann | 4b456e7 | 2021-08-10 03:25:02 | [diff] [blame] | 308 | #define NO_STACK_PROTECTOR __attribute__((__optimize__("-fno-stack-protector"))) |
| 309 | #endif |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 310 | #else |
| 311 | #define NO_STACK_PROTECTOR |
| 312 | #endif |
| 313 | |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 314 | // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints |
| 315 | // to Clang which control what code paths are statically analyzed, |
| 316 | // and is meant to be used in conjunction with assert & assert-like functions. |
| 317 | // The expression is passed straight through if analysis isn't enabled. |
| 318 | // |
| 319 | // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current |
| 320 | // codepath and any other branching codepaths that might follow. |
| 321 | #if defined(__clang_analyzer__) |
| 322 | |
| 323 | inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | inline constexpr bool AnalyzerAssumeTrue(bool arg) { |
| 328 | // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is |
| 329 | // false. |
| 330 | return arg || AnalyzerNoReturn(); |
| 331 | } |
| 332 | |
George Burgess IV | a09d235d | 2020-04-17 13:32:50 | [diff] [blame] | 333 | #define ANALYZER_ASSUME_TRUE(arg) ::AnalyzerAssumeTrue(!!(arg)) |
| 334 | #define ANALYZER_SKIP_THIS_PATH() static_cast<void>(::AnalyzerNoReturn()) |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 335 | |
| 336 | #else // !defined(__clang_analyzer__) |
| 337 | |
| 338 | #define ANALYZER_ASSUME_TRUE(arg) (arg) |
| 339 | #define ANALYZER_SKIP_THIS_PATH() |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 340 | |
| 341 | #endif // defined(__clang_analyzer__) |
| 342 | |
Zequan Wu | 9909f14 | 2021-02-10 03:26:00 | [diff] [blame] | 343 | // Use nomerge attribute to disable optimization of merging multiple same calls. |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 344 | #if defined(__clang__) && HAS_ATTRIBUTE(nomerge) |
Zequan Wu | 9909f14 | 2021-02-10 03:26:00 | [diff] [blame] | 345 | #define NOMERGE [[clang::nomerge]] |
| 346 | #else |
| 347 | #define NOMERGE |
| 348 | #endif |
| 349 | |
Jeremy Roman | 810d98d | 2021-04-06 16:46:07 | [diff] [blame] | 350 | // Marks a type as being eligible for the "trivial" ABI despite having a |
| 351 | // non-trivial destructor or copy/move constructor. Such types can be relocated |
| 352 | // after construction by simply copying their memory, which makes them eligible |
| 353 | // to be passed in registers. The canonical example is std::unique_ptr. |
| 354 | // |
| 355 | // Use with caution; this has some subtle effects on constructor/destructor |
| 356 | // ordering and will be very incorrect if the type relies on its address |
| 357 | // remaining constant. When used as a function argument (by value), the value |
| 358 | // may be constructed in the caller's stack frame, passed in a register, and |
| 359 | // then used and destructed in the callee's stack frame. A similar thing can |
| 360 | // occur when values are returned. |
| 361 | // |
| 362 | // TRIVIAL_ABI is not needed for types which have a trivial destructor and |
| 363 | // copy/move constructors, such as base::TimeTicks and other POD. |
| 364 | // |
| 365 | // It is also not likely to be effective on types too large to be passed in one |
| 366 | // or two registers on typical target ABIs. |
| 367 | // |
| 368 | // See also: |
| 369 | // https://clang.llvm.org/docs/AttributeReference.html#trivial-abi |
| 370 | // https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 371 | #if defined(__clang__) && HAS_ATTRIBUTE(trivial_abi) |
Jeremy Roman | 810d98d | 2021-04-06 16:46:07 | [diff] [blame] | 372 | #define TRIVIAL_ABI [[clang::trivial_abi]] |
| 373 | #else |
| 374 | #define TRIVIAL_ABI |
| 375 | #endif |
| 376 | |
Lukasz Anforowicz | 3be38fbb | 2021-04-14 20:29:29 | [diff] [blame] | 377 | // Marks a member function as reinitializing a moved-from variable. |
| 378 | // See also |
| 379 | // https://clang.llvm.org/extra/clang-tidy/checks/bugprone-use-after-move.html#reinitialization |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 380 | #if defined(__clang__) && HAS_ATTRIBUTE(reinitializes) |
Lukasz Anforowicz | 3be38fbb | 2021-04-14 20:29:29 | [diff] [blame] | 381 | #define REINITIALIZES_AFTER_MOVE [[clang::reinitializes]] |
| 382 | #else |
| 383 | #define REINITIALIZES_AFTER_MOVE |
| 384 | #endif |
| 385 | |
Benoit Lize | 5123edb | 2021-05-18 14:00:19 | [diff] [blame] | 386 | // Requires constant initialization. See constinit in C++20. Allows to rely on a |
| 387 | // variable being initialized before execution, and not requiring a global |
| 388 | // constructor. |
Peter Kasting | 64c67dd | 2022-05-12 18:11:51 | [diff] [blame] | 389 | #if HAS_ATTRIBUTE(require_constant_initialization) |
Benoit Lize | 5123edb | 2021-05-18 14:00:19 | [diff] [blame] | 390 | #define CONSTINIT __attribute__((require_constant_initialization)) |
Benoit Lize | 5123edb | 2021-05-18 14:00:19 | [diff] [blame] | 391 | #endif |
Anton Bikineev | 6beb710 | 2021-05-25 13:37:52 | [diff] [blame] | 392 | #if !defined(CONSTINIT) |
| 393 | #define CONSTINIT |
| 394 | #endif |
Benoit Lize | 5123edb | 2021-05-18 14:00:19 | [diff] [blame] | 395 | |
danakj | ceb1702 | 2022-02-11 23:52:01 | [diff] [blame] | 396 | #if defined(__clang__) |
Daniel Cheng | 8ac305b | 2022-02-17 00:05:11 | [diff] [blame] | 397 | #define GSL_OWNER [[gsl::Owner]] |
danakj | ceb1702 | 2022-02-11 23:52:01 | [diff] [blame] | 398 | #define GSL_POINTER [[gsl::Pointer]] |
| 399 | #else |
Jose Dapena Paz | 1183b14 | 2022-02-18 16:28:25 | [diff] [blame] | 400 | #define GSL_OWNER |
danakj | ceb1702 | 2022-02-11 23:52:01 | [diff] [blame] | 401 | #define GSL_POINTER |
| 402 | #endif |
| 403 | |
Daniel Cheng | f2c0538 | 2022-09-16 02:51:42 | [diff] [blame] | 404 | // Adds the "logically_const" tag to a symbol's mangled name. The "Mutable |
| 405 | // Constants" check [1] detects instances of constants that aren't in .rodata, |
| 406 | // e.g. due to a missing `const`. Using this tag suppresses the check for this |
| 407 | // symbol, allowing it to live outside .rodata without a warning. |
| 408 | // |
| 409 | // [1]: |
| 410 | // https://crsrc.org/c/docs/speed/binary_size/android_binary_size_trybot.md#Mutable-Constants |
Anthony Vallee-Dubois | 9dbbbda3 | 2022-08-26 01:25:31 | [diff] [blame] | 411 | #if defined(COMPILER_GCC) || defined(__clang__) |
| 412 | #define LOGICALLY_CONST [[gnu::abi_tag("logically_const")]] |
| 413 | #else |
| 414 | #define LOGICALLY_CONST |
| 415 | #endif |
| 416 | |
agl@chromium.org | dd9afc0b | 2008-11-21 23:58:09 | [diff] [blame] | 417 | #endif // BASE_COMPILER_SPECIFIC_H_ |