compiler_specific.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_COMPILER_SPECIFIC_H_
  5. #define BASE_COMPILER_SPECIFIC_H_
  6. #include "build/build_config.h"
  7. #if defined(COMPILER_MSVC) && !defined(__clang__)
  8. #error "Only clang-cl is supported on Windows, see https://crbug.com/988071"
  9. #endif
  10. // This is a wrapper around `__has_cpp_attribute`, which can be used to test for
  11. // the presence of an attribute. In case the compiler does not support this
  12. // macro it will simply evaluate to 0.
  13. //
  14. // References:
  15. // https://wg21.link/sd6#testing-for-the-presence-of-an-attribute-__has_cpp_attribute
  16. // https://wg21.link/cpp.cond#:__has_cpp_attribute
  17. #if defined(__has_cpp_attribute)
  18. #define HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
  19. #else
  20. #define HAS_CPP_ATTRIBUTE(x) 0
  21. #endif
  22. // A wrapper around `__has_attribute`, similar to HAS_CPP_ATTRIBUTE.
  23. #if defined(__has_attribute)
  24. #define HAS_ATTRIBUTE(x) __has_attribute(x)
  25. #else
  26. #define HAS_ATTRIBUTE(x) 0
  27. #endif
  28. // A wrapper around `__has_builtin`, similar to HAS_CPP_ATTRIBUTE.
  29. #if defined(__has_builtin)
  30. #define HAS_BUILTIN(x) __has_builtin(x)
  31. #else
  32. #define HAS_BUILTIN(x) 0
  33. #endif
  34. // Annotate a function indicating it should not be inlined.
  35. // Use like:
  36. // NOINLINE void DoStuff() { ... }
  37. #if defined(COMPILER_GCC) || defined(__clang__)
  38. #define NOINLINE __attribute__((noinline))
  39. #elif defined(COMPILER_MSVC)
  40. #define NOINLINE __declspec(noinline)
  41. #else
  42. #define NOINLINE
  43. #endif
  44. #if defined(COMPILER_GCC) && defined(NDEBUG)
  45. #define ALWAYS_INLINE inline __attribute__((__always_inline__))
  46. #elif defined(COMPILER_MSVC) && defined(NDEBUG)
  47. #define ALWAYS_INLINE __forceinline
  48. #else
  49. #define ALWAYS_INLINE inline
  50. #endif
  51. // Annotate a function indicating it should never be tail called. Useful to make
  52. // sure callers of the annotated function are never omitted from call-stacks.
  53. // To provide the complementary behavior (prevent the annotated function from
  54. // being omitted) look at NOINLINE. Also note that this doesn't prevent code
  55. // folding of multiple identical caller functions into a single signature. To
  56. // prevent code folding, see NO_CODE_FOLDING() in base/debug/alias.h.
  57. // Use like:
  58. // void NOT_TAIL_CALLED FooBar();
  59. #if defined(__clang__) && HAS_ATTRIBUTE(not_tail_called)
  60. #define NOT_TAIL_CALLED __attribute__((not_tail_called))
  61. #else
  62. #define NOT_TAIL_CALLED
  63. #endif
  64. // Specify memory alignment for structs, classes, etc.
  65. // Use like:
  66. // class ALIGNAS(16) MyClass { ... }
  67. // ALIGNAS(16) int array[4];
  68. //
  69. // In most places you can use the C++11 keyword "alignas", which is preferred.
  70. //
  71. // But compilers have trouble mixing __attribute__((...)) syntax with
  72. // alignas(...) syntax.
  73. //
  74. // Doesn't work in clang or gcc:
  75. // struct alignas(16) __attribute__((packed)) S { char c; };
  76. // Works in clang but not gcc:
  77. // struct __attribute__((packed)) alignas(16) S2 { char c; };
  78. // Works in clang and gcc:
  79. // struct alignas(16) S3 { char c; } __attribute__((packed));
  80. //
  81. // There are also some attributes that must be specified *before* a class
  82. // definition: visibility (used for exporting functions/classes) is one of
  83. // these attributes. This means that it is not possible to use alignas() with a
  84. // class that is marked as exported.
  85. #if defined(COMPILER_MSVC)
  86. #define ALIGNAS(byte_alignment) __declspec(align(byte_alignment))
  87. #elif defined(COMPILER_GCC)
  88. #define ALIGNAS(byte_alignment) __attribute__((aligned(byte_alignment)))
  89. #endif
  90. // In case the compiler supports it NO_UNIQUE_ADDRESS evaluates to the C++20
  91. // attribute [[no_unique_address]]. This allows annotating data members so that
  92. // they need not have an address distinct from all other non-static data members
  93. // of its class.
  94. //
  95. // References:
  96. // * https://en.cppreference.com/w/cpp/language/attributes/no_unique_address
  97. // * https://wg21.link/dcl.attr.nouniqueaddr
  98. #if HAS_CPP_ATTRIBUTE(no_unique_address)
  99. #define NO_UNIQUE_ADDRESS [[no_unique_address]]
  100. #else
  101. #define NO_UNIQUE_ADDRESS
  102. #endif
  103. // Tell the compiler a function is using a printf-style format string.
  104. // |format_param| is the one-based index of the format string parameter;
  105. // |dots_param| is the one-based index of the "..." parameter.
  106. // For v*printf functions (which take a va_list), pass 0 for dots_param.
  107. // (This is undocumented but matches what the system C headers do.)
  108. // For member functions, the implicit this parameter counts as index 1.
  109. #if defined(COMPILER_GCC) || defined(__clang__)
  110. #define PRINTF_FORMAT(format_param, dots_param) \
  111. __attribute__((format(printf, format_param, dots_param)))
  112. #else
  113. #define PRINTF_FORMAT(format_param, dots_param)
  114. #endif
  115. // WPRINTF_FORMAT is the same, but for wide format strings.
  116. // This doesn't appear to yet be implemented in any compiler.
  117. // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38308 .
  118. #define WPRINTF_FORMAT(format_param, dots_param)
  119. // If available, it would look like:
  120. // __attribute__((format(wprintf, format_param, dots_param)))
  121. // Sanitizers annotations.
  122. #if HAS_ATTRIBUTE(no_sanitize)
  123. #define NO_SANITIZE(what) __attribute__((no_sanitize(what)))
  124. #endif
  125. #if !defined(NO_SANITIZE)
  126. #define NO_SANITIZE(what)
  127. #endif
  128. // MemorySanitizer annotations.
  129. #if defined(MEMORY_SANITIZER) && !BUILDFLAG(IS_NACL)
  130. #include <sanitizer/msan_interface.h>
  131. // Mark a memory region fully initialized.
  132. // Use this to annotate code that deliberately reads uninitialized data, for
  133. // example a GC scavenging root set pointers from the stack.
  134. #define MSAN_UNPOISON(p, size) __msan_unpoison(p, size)
  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) \
  140. __msan_check_mem_is_initialized(p, size)
  141. #else // MEMORY_SANITIZER
  142. #define MSAN_UNPOISON(p, size)
  143. #define MSAN_CHECK_MEM_IS_INITIALIZED(p, size)
  144. #endif // MEMORY_SANITIZER
  145. // DISABLE_CFI_PERF -- Disable Control Flow Integrity for perf reasons.
  146. #if !defined(DISABLE_CFI_PERF)
  147. #if defined(__clang__) && defined(OFFICIAL_BUILD)
  148. #define DISABLE_CFI_PERF __attribute__((no_sanitize("cfi")))
  149. #else
  150. #define DISABLE_CFI_PERF
  151. #endif
  152. #endif
  153. // DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks.
  154. // Security Note: if you just need to allow calling of dlsym functions use
  155. // DISABLE_CFI_DLSYM.
  156. #if !defined(DISABLE_CFI_ICALL)
  157. #if BUILDFLAG(IS_WIN)
  158. // Windows also needs __declspec(guard(nocf)).
  159. #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall") __declspec(guard(nocf))
  160. #else
  161. #define DISABLE_CFI_ICALL NO_SANITIZE("cfi-icall")
  162. #endif
  163. #endif
  164. #if !defined(DISABLE_CFI_ICALL)
  165. #define DISABLE_CFI_ICALL
  166. #endif
  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
  178. #if !defined(DISABLE_CFI_DLSYM)
  179. #define DISABLE_CFI_DLSYM
  180. #endif
  181. // Macro useful for writing cross-platform function pointers.
  182. #if !defined(CDECL)
  183. #if BUILDFLAG(IS_WIN)
  184. #define CDECL __cdecl
  185. #else // BUILDFLAG(IS_WIN)
  186. #define CDECL
  187. #endif // BUILDFLAG(IS_WIN)
  188. #endif // !defined(CDECL)
  189. // Macro for hinting that an expression is likely to be false.
  190. #if !defined(UNLIKELY)
  191. #if defined(COMPILER_GCC) || defined(__clang__)
  192. #define UNLIKELY(x) __builtin_expect(!!(x), 0)
  193. #else
  194. #define UNLIKELY(x) (x)
  195. #endif // defined(COMPILER_GCC)
  196. #endif // !defined(UNLIKELY)
  197. #if !defined(LIKELY)
  198. #if defined(COMPILER_GCC) || defined(__clang__)
  199. #define LIKELY(x) __builtin_expect(!!(x), 1)
  200. #else
  201. #define LIKELY(x) (x)
  202. #endif // defined(COMPILER_GCC)
  203. #endif // !defined(LIKELY)
  204. // Compiler feature-detection.
  205. // clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
  206. #if defined(__has_feature)
  207. #define HAS_FEATURE(FEATURE) __has_feature(FEATURE)
  208. #else
  209. #define HAS_FEATURE(FEATURE) 0
  210. #endif
  211. #if defined(COMPILER_GCC)
  212. #define PRETTY_FUNCTION __PRETTY_FUNCTION__
  213. #elif defined(COMPILER_MSVC)
  214. #define PRETTY_FUNCTION __FUNCSIG__
  215. #else
  216. // See https://en.cppreference.com/w/c/language/function_definition#func
  217. #define PRETTY_FUNCTION __func__
  218. #endif
  219. #if !defined(CPU_ARM_NEON)
  220. #if defined(__arm__)
  221. #if !defined(__ARMEB__) && !defined(__ARM_EABI__) && !defined(__EABI__) && \
  222. !defined(__VFP_FP__) && !defined(_WIN32_WCE) && !defined(ANDROID)
  223. #error Chromium does not support middle endian architecture
  224. #endif
  225. #if defined(__ARM_NEON__)
  226. #define CPU_ARM_NEON 1
  227. #endif
  228. #endif // defined(__arm__)
  229. #endif // !defined(CPU_ARM_NEON)
  230. #if !defined(HAVE_MIPS_MSA_INTRINSICS)
  231. #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
  232. #define HAVE_MIPS_MSA_INTRINSICS 1
  233. #endif
  234. #endif
  235. #if defined(__clang__) && HAS_ATTRIBUTE(uninitialized)
  236. // Attribute "uninitialized" disables -ftrivial-auto-var-init=pattern for
  237. // the specified variable.
  238. // Library-wide alternative is
  239. // 'configs -= [ "//build/config/compiler:default_init_stack_vars" ]' in .gn
  240. // file.
  241. //
  242. // See "init_stack_vars" in build/config/compiler/BUILD.gn and
  243. // http://crbug.com/977230
  244. // "init_stack_vars" is enabled for non-official builds and we hope to enable it
  245. // in official build in 2020 as well. The flag writes fixed pattern into
  246. // uninitialized parts of all local variables. In rare cases such initialization
  247. // is undesirable and attribute can be used:
  248. // 1. Degraded performance
  249. // In most cases compiler is able to remove additional stores. E.g. if memory is
  250. // never accessed or properly initialized later. Preserved stores mostly will
  251. // not affect program performance. However if compiler failed on some
  252. // performance critical code we can get a visible regression in a benchmark.
  253. // 2. memset, memcpy calls
  254. // Compiler may replaces some memory writes with memset or memcpy calls. This is
  255. // not -ftrivial-auto-var-init specific, but it can happen more likely with the
  256. // flag. It can be a problem if code is not linked with C run-time library.
  257. //
  258. // Note: The flag is security risk mitigation feature. So in future the
  259. // attribute uses should be avoided when possible. However to enable this
  260. // mitigation on the most of the code we need to be less strict now and minimize
  261. // number of exceptions later. So if in doubt feel free to use attribute, but
  262. // please document the problem for someone who is going to cleanup it later.
  263. // E.g. platform, bot, benchmark or test name in patch description or next to
  264. // the attribute.
  265. #define STACK_UNINITIALIZED __attribute__((uninitialized))
  266. #else
  267. #define STACK_UNINITIALIZED
  268. #endif
  269. // Attribute "no_stack_protector" disables -fstack-protector for the specified
  270. // function.
  271. //
  272. // "stack_protector" is enabled on most POSIX builds. The flag adds a canary
  273. // to each stack frame, which on function return is checked against a reference
  274. // canary. If the canaries do not match, it's likely that a stack buffer
  275. // overflow has occurred, so immediately crashing will prevent exploitation in
  276. // many cases.
  277. //
  278. // In some cases it's desirable to remove this, e.g. on hot functions, or if
  279. // we have purposely changed the reference canary.
  280. #if defined(COMPILER_GCC) || defined(__clang__)
  281. #if HAS_ATTRIBUTE(__no_stack_protector__)
  282. #define NO_STACK_PROTECTOR __attribute__((__no_stack_protector__))
  283. #else
  284. #define NO_STACK_PROTECTOR __attribute__((__optimize__("-fno-stack-protector")))
  285. #endif
  286. #else
  287. #define NO_STACK_PROTECTOR
  288. #endif
  289. // The ANALYZER_ASSUME_TRUE(bool arg) macro adds compiler-specific hints
  290. // to Clang which control what code paths are statically analyzed,
  291. // and is meant to be used in conjunction with assert & assert-like functions.
  292. // The expression is passed straight through if analysis isn't enabled.
  293. //
  294. // ANALYZER_SKIP_THIS_PATH() suppresses static analysis for the current
  295. // codepath and any other branching codepaths that might follow.
  296. #if defined(__clang_analyzer__)
  297. inline constexpr bool AnalyzerNoReturn() __attribute__((analyzer_noreturn)) {
  298. return false;
  299. }
  300. inline constexpr bool AnalyzerAssumeTrue(bool arg) {
  301. // AnalyzerNoReturn() is invoked and analysis is terminated if |arg| is
  302. // false.
  303. return arg || AnalyzerNoReturn();
  304. }
  305. #define ANALYZER_ASSUME_TRUE(arg) ::AnalyzerAssumeTrue(!!(arg))
  306. #define ANALYZER_SKIP_THIS_PATH() static_cast<void>(::AnalyzerNoReturn())
  307. #else // !defined(__clang_analyzer__)
  308. #define ANALYZER_ASSUME_TRUE(arg) (arg)
  309. #define ANALYZER_SKIP_THIS_PATH()
  310. #endif // defined(__clang_analyzer__)
  311. // Use nomerge attribute to disable optimization of merging multiple same calls.
  312. #if defined(__clang__) && HAS_ATTRIBUTE(nomerge)
  313. #define NOMERGE [[clang::nomerge]]
  314. #else
  315. #define NOMERGE
  316. #endif
  317. // Marks a type as being eligible for the "trivial" ABI despite having a
  318. // non-trivial destructor or copy/move constructor. Such types can be relocated
  319. // after construction by simply copying their memory, which makes them eligible
  320. // to be passed in registers. The canonical example is std::unique_ptr.
  321. //
  322. // Use with caution; this has some subtle effects on constructor/destructor
  323. // ordering and will be very incorrect if the type relies on its address
  324. // remaining constant. When used as a function argument (by value), the value
  325. // may be constructed in the caller's stack frame, passed in a register, and
  326. // then used and destructed in the callee's stack frame. A similar thing can
  327. // occur when values are returned.
  328. //
  329. // TRIVIAL_ABI is not needed for types which have a trivial destructor and
  330. // copy/move constructors, such as base::TimeTicks and other POD.
  331. //
  332. // It is also not likely to be effective on types too large to be passed in one
  333. // or two registers on typical target ABIs.
  334. //
  335. // See also:
  336. // https://clang.llvm.org/docs/AttributeReference.html#trivial-abi
  337. // https://libcxx.llvm.org/docs/DesignDocs/UniquePtrTrivialAbi.html
  338. #if defined(__clang__) && HAS_ATTRIBUTE(trivial_abi)
  339. #define TRIVIAL_ABI [[clang::trivial_abi]]
  340. #else
  341. #define TRIVIAL_ABI
  342. #endif
  343. // Marks a member function as reinitializing a moved-from variable.
  344. // See also
  345. // https://clang.llvm.org/extra/clang-tidy/checks/bugprone-use-after-move.html#reinitialization
  346. #if defined(__clang__) && HAS_ATTRIBUTE(reinitializes)
  347. #define REINITIALIZES_AFTER_MOVE [[clang::reinitializes]]
  348. #else
  349. #define REINITIALIZES_AFTER_MOVE
  350. #endif
  351. // Requires constant initialization. See constinit in C++20. Allows to rely on a
  352. // variable being initialized before execution, and not requiring a global
  353. // constructor.
  354. #if HAS_ATTRIBUTE(require_constant_initialization)
  355. #define CONSTINIT __attribute__((require_constant_initialization))
  356. #endif
  357. #if !defined(CONSTINIT)
  358. #define CONSTINIT
  359. #endif
  360. #if defined(__clang__)
  361. #define GSL_OWNER [[gsl::Owner]]
  362. #define GSL_POINTER [[gsl::Pointer]]
  363. #else
  364. #define GSL_OWNER
  365. #define GSL_POINTER
  366. #endif
  367. #endif // BASE_COMPILER_SPECIFIC_H_