| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* Implementations of various class and method modifier attributes. */ |
| 8 |
|
| 9 |
#ifndef mozilla_Attributes_h |
| 10 |
#define mozilla_Attributes_h |
| 11 |
|
| 12 |
#include "mozilla/Compiler.h" |
| 13 |
|
| 14 |
/* |
| 15 |
* MOZ_ALWAYS_INLINE is a macro which expands to tell the compiler that the |
| 16 |
* method decorated with it must be inlined, even if the compiler thinks |
| 17 |
* otherwise. This is only a (much) stronger version of the inline hint: |
| 18 |
* compilers are not guaranteed to respect it (although they're much more likely |
| 19 |
* to do so). |
| 20 |
* |
| 21 |
* The MOZ_ALWAYS_INLINE_EVEN_DEBUG macro is yet stronger. It tells the |
| 22 |
* compiler to inline even in DEBUG builds. It should be used very rarely. |
| 23 |
*/ |
| 24 |
#if defined(_MSC_VER) |
| 25 |
# define MOZ_ALWAYS_INLINE_EVEN_DEBUG __forceinline |
| 26 |
#elif defined(__GNUC__) |
| 27 |
# define MOZ_ALWAYS_INLINE_EVEN_DEBUG __attribute__((always_inline)) inline |
| 28 |
#else |
| 29 |
# define MOZ_ALWAYS_INLINE_EVEN_DEBUG inline |
| 30 |
#endif |
| 31 |
|
| 32 |
#if !defined(DEBUG) |
| 33 |
# define MOZ_ALWAYS_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG |
| 34 |
#elif defined(_MSC_VER) && !defined(__cplusplus) |
| 35 |
# define MOZ_ALWAYS_INLINE __inline |
| 36 |
#else |
| 37 |
# define MOZ_ALWAYS_INLINE inline |
| 38 |
#endif |
| 39 |
|
| 40 |
#if defined(_MSC_VER) |
| 41 |
/* |
| 42 |
* g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality |
| 43 |
* without warnings (functionality used by the macros below). These modes are |
| 44 |
* detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or, more |
| 45 |
* standardly, by checking whether __cplusplus has a C++11 or greater value. |
| 46 |
* Current versions of g++ do not correctly set __cplusplus, so we check both |
| 47 |
* for forward compatibility. |
| 48 |
*/ |
| 49 |
# define MOZ_HAVE_NEVER_INLINE __declspec(noinline) |
| 50 |
# define MOZ_HAVE_NORETURN __declspec(noreturn) |
| 51 |
#elif defined(__clang__) |
| 52 |
/* |
| 53 |
* Per Clang documentation, "Note that marketing version numbers should not |
| 54 |
* be used to check for language features, as different vendors use different |
| 55 |
* numbering schemes. Instead, use the feature checking macros." |
| 56 |
*/ |
| 57 |
# ifndef __has_extension |
| 58 |
# define __has_extension \ |
| 59 |
__has_feature /* compatibility, for older versions of clang */ |
| 60 |
# endif |
| 61 |
# if __has_attribute(noinline) |
| 62 |
# define MOZ_HAVE_NEVER_INLINE __attribute__((noinline)) |
| 63 |
# endif |
| 64 |
# if __has_attribute(noreturn) |
| 65 |
# define MOZ_HAVE_NORETURN __attribute__((noreturn)) |
| 66 |
# endif |
| 67 |
#elif defined(__GNUC__) |
| 68 |
# define MOZ_HAVE_NEVER_INLINE __attribute__((noinline)) |
| 69 |
# define MOZ_HAVE_NORETURN __attribute__((noreturn)) |
| 70 |
# define MOZ_HAVE_NORETURN_PTR __attribute__((noreturn)) |
| 71 |
#endif |
| 72 |
|
| 73 |
#if defined(__clang__) |
| 74 |
# if __has_attribute(no_stack_protector) |
| 75 |
# define MOZ_HAVE_NO_STACK_PROTECTOR __attribute__((no_stack_protector)) |
| 76 |
# endif |
| 77 |
#elif defined(__GNUC__) |
| 78 |
# define MOZ_HAVE_NO_STACK_PROTECTOR __attribute__((no_stack_protector)) |
| 79 |
#endif |
| 80 |
|
| 81 |
/* if defined(__clang__) && __has_attribute (attr) may not be portable */ |
| 82 |
#if defined(__clang__) |
| 83 |
# define MOZ_HAS_CLANG_ATTRIBUTE(attr) __has_attribute(attr) |
| 84 |
#else |
| 85 |
# define MOZ_HAS_CLANG_ATTRIBUTE(attr) 0 |
| 86 |
#endif |
| 87 |
|
| 88 |
/* |
| 89 |
* When built with clang analyzer (a.k.a scan-build), define MOZ_HAVE_NORETURN |
| 90 |
* to mark some false positives |
| 91 |
*/ |
| 92 |
#ifdef __clang_analyzer__ |
| 93 |
# if __has_extension(attribute_analyzer_noreturn) |
| 94 |
# define MOZ_HAVE_ANALYZER_NORETURN __attribute__((analyzer_noreturn)) |
| 95 |
# endif |
| 96 |
#endif |
| 97 |
|
| 98 |
#if defined(__GNUC__) || MOZ_HAS_CLANG_ATTRIBUTE(no_profile_instrument_function) |
| 99 |
# define MOZ_NOPROFILE __attribute__((no_profile_instrument_function)) |
| 100 |
#else |
| 101 |
# define MOZ_NOPROFILE |
| 102 |
#endif |
| 103 |
|
| 104 |
#if defined(__GNUC__) || (MOZ_HAS_CLANG_ATTRIBUTE(no_instrument_function)) |
| 105 |
# define MOZ_NOINSTRUMENT __attribute__((no_instrument_function)) |
| 106 |
#else |
| 107 |
# define MOZ_NOINSTRUMENT |
| 108 |
#endif |
| 109 |
|
| 110 |
/* |
| 111 |
* MOZ_NAKED tells the compiler that the function only contains assembly and |
| 112 |
* that it should not try to inject code that may mess with the assembly in it. |
| 113 |
* |
| 114 |
* See https://github.com/llvm/llvm-project/issues/74573 for the interaction |
| 115 |
* between naked and no_profile_instrument_function. |
| 116 |
*/ |
| 117 |
#define MOZ_NAKED __attribute__((naked)) MOZ_NOPROFILE MOZ_NOINSTRUMENT |
| 118 |
|
| 119 |
/** |
| 120 |
* Per clang's documentation: |
| 121 |
* |
| 122 |
* If a statement is marked nomerge and contains call expressions, those call |
| 123 |
* expressions inside the statement will not be merged during optimization. This |
| 124 |
* attribute can be used to prevent the optimizer from obscuring the source |
| 125 |
* location of certain calls. |
| 126 |
* |
| 127 |
* This is useful to have clearer information on assertion failures. |
| 128 |
*/ |
| 129 |
#if MOZ_HAS_CLANG_ATTRIBUTE(nomerge) |
| 130 |
# define MOZ_NOMERGE __attribute__((nomerge)) |
| 131 |
#else |
| 132 |
# define MOZ_NOMERGE |
| 133 |
#endif |
| 134 |
|
| 135 |
/* |
| 136 |
* MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the |
| 137 |
* method decorated with it must never be inlined, even if the compiler would |
| 138 |
* otherwise choose to inline the method. Compilers aren't absolutely |
| 139 |
* guaranteed to support this, but most do. |
| 140 |
*/ |
| 141 |
#if defined(MOZ_HAVE_NEVER_INLINE) |
| 142 |
# define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE |
| 143 |
#else |
| 144 |
# define MOZ_NEVER_INLINE /* no support */ |
| 145 |
#endif |
| 146 |
|
| 147 |
/* |
| 148 |
* MOZ_NEVER_INLINE_DEBUG is a macro which expands to MOZ_NEVER_INLINE |
| 149 |
* in debug builds, and nothing in opt builds. |
| 150 |
*/ |
| 151 |
#if defined(DEBUG) |
| 152 |
# define MOZ_NEVER_INLINE_DEBUG MOZ_NEVER_INLINE |
| 153 |
#else |
| 154 |
# define MOZ_NEVER_INLINE_DEBUG /* don't inline in opt builds */ |
| 155 |
#endif |
| 156 |
/* |
| 157 |
* MOZ_NORETURN, specified at the start of a function declaration, indicates |
| 158 |
* that the given function does not return. (The function definition does not |
| 159 |
* need to be annotated.) |
| 160 |
* |
| 161 |
* MOZ_NORETURN void abort(const char* msg); |
| 162 |
* |
| 163 |
* This modifier permits the compiler to optimize code assuming a call to such a |
| 164 |
* function will never return. It also enables the compiler to avoid spurious |
| 165 |
* warnings about not initializing variables, or about any other seemingly-dodgy |
| 166 |
* operations performed after the function returns. |
| 167 |
* |
| 168 |
* There are two variants. The GCC version of NORETURN may be applied to a |
| 169 |
* function pointer, while for MSVC it may not. |
| 170 |
* |
| 171 |
* This modifier does not affect the corresponding function's linking behavior. |
| 172 |
*/ |
| 173 |
#if defined(MOZ_HAVE_NORETURN) |
| 174 |
# define MOZ_NORETURN MOZ_HAVE_NORETURN |
| 175 |
#else |
| 176 |
# define MOZ_NORETURN /* no support */ |
| 177 |
#endif |
| 178 |
#if defined(MOZ_HAVE_NORETURN_PTR) |
| 179 |
# define MOZ_NORETURN_PTR MOZ_HAVE_NORETURN_PTR |
| 180 |
#else |
| 181 |
# define MOZ_NORETURN_PTR /* no support */ |
| 182 |
#endif |
| 183 |
|
| 184 |
/** |
| 185 |
* MOZ_COLD tells the compiler that a function is "cold", meaning infrequently |
| 186 |
* executed. This may lead it to optimize for size more aggressively than speed, |
| 187 |
* or to allocate the body of the function in a distant part of the text segment |
| 188 |
* to help keep it from taking up unnecessary icache when it isn't in use. |
| 189 |
* |
| 190 |
* Place this attribute at the very beginning of a function definition. For |
| 191 |
* example, write |
| 192 |
* |
| 193 |
* MOZ_COLD int foo(); |
| 194 |
* |
| 195 |
* or |
| 196 |
* |
| 197 |
* MOZ_COLD int foo() { return 42; } |
| 198 |
*/ |
| 199 |
#if defined(__GNUC__) || defined(__clang__) |
| 200 |
# define MOZ_COLD __attribute__((cold)) |
| 201 |
#else |
| 202 |
# define MOZ_COLD |
| 203 |
#endif |
| 204 |
|
| 205 |
/** |
| 206 |
* MOZ_NONNULL tells the compiler that some of the arguments to a function are |
| 207 |
* known to be non-null. The arguments are a list of 1-based argument indexes |
| 208 |
* identifying arguments which are known to be non-null. |
| 209 |
* |
| 210 |
* Place this attribute at the very beginning of a function definition. For |
| 211 |
* example, write |
| 212 |
* |
| 213 |
* MOZ_NONNULL(1, 2) int foo(char *p, char *q); |
| 214 |
*/ |
| 215 |
#if defined(__GNUC__) || defined(__clang__) |
| 216 |
# define MOZ_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) |
| 217 |
#else |
| 218 |
# define MOZ_NONNULL(...) |
| 219 |
#endif |
| 220 |
|
| 221 |
/** |
| 222 |
* MOZ_NONNULL_RETURN tells the compiler that the function's return value is |
| 223 |
* guaranteed to be a non-null pointer, which may enable the compiler to |
| 224 |
* optimize better at call sites. |
| 225 |
* |
| 226 |
* Place this attribute at the end of a function declaration. For example, |
| 227 |
* |
| 228 |
* char* foo(char *p, char *q) MOZ_NONNULL_RETURN; |
| 229 |
*/ |
| 230 |
#if defined(__GNUC__) || defined(__clang__) |
| 231 |
# define MOZ_NONNULL_RETURN __attribute__((returns_nonnull)) |
| 232 |
#else |
| 233 |
# define MOZ_NONNULL_RETURN |
| 234 |
#endif |
| 235 |
|
| 236 |
/* |
| 237 |
* MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS, specified at the end of a function |
| 238 |
* declaration, indicates that for the purposes of static analysis, this |
| 239 |
* function does not return. (The function definition does not need to be |
| 240 |
* annotated.) |
| 241 |
* |
| 242 |
* MOZ_ReportCrash(const char* s, const char* file, int ln) |
| 243 |
* MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS |
| 244 |
* |
| 245 |
* Some static analyzers, like scan-build from clang, can use this information |
| 246 |
* to eliminate false positives. From the upstream documentation of scan-build: |
| 247 |
* "This attribute is useful for annotating assertion handlers that actually |
| 248 |
* can return, but for the purpose of using the analyzer we want to pretend |
| 249 |
* that such functions do not return." |
| 250 |
* |
| 251 |
*/ |
| 252 |
#if defined(MOZ_HAVE_ANALYZER_NORETURN) |
| 253 |
# define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS MOZ_HAVE_ANALYZER_NORETURN |
| 254 |
#else |
| 255 |
# define MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS /* no support */ |
| 256 |
#endif |
| 257 |
|
| 258 |
/* |
| 259 |
* MOZ_ASAN_IGNORE is a macro to tell AddressSanitizer (a compile-time |
| 260 |
* instrumentation shipped with Clang and GCC) to not instrument the annotated |
| 261 |
* function. Furthermore, it will prevent the compiler from inlining the |
| 262 |
* function because inlining currently breaks the blocklisting mechanism of |
| 263 |
* AddressSanitizer. |
| 264 |
*/ |
| 265 |
#if defined(__has_feature) |
| 266 |
# if __has_feature(address_sanitizer) |
| 267 |
# define MOZ_HAVE_ASAN_IGNORE |
| 268 |
# endif |
| 269 |
#elif defined(__GNUC__) |
| 270 |
# if defined(__SANITIZE_ADDRESS__) |
| 271 |
# define MOZ_HAVE_ASAN_IGNORE |
| 272 |
# endif |
| 273 |
#endif |
| 274 |
|
| 275 |
#if defined(MOZ_HAVE_ASAN_IGNORE) |
| 276 |
# define MOZ_ASAN_IGNORE MOZ_NEVER_INLINE __attribute__((no_sanitize_address)) |
| 277 |
#else |
| 278 |
# define MOZ_ASAN_IGNORE /* nothing */ |
| 279 |
#endif |
| 280 |
|
| 281 |
/* |
| 282 |
* MOZ_TSAN_IGNORE is a macro to tell ThreadSanitizer (a compile-time |
| 283 |
* instrumentation shipped with Clang) to not instrument the annotated function. |
| 284 |
* Furthermore, it will prevent the compiler from inlining the function because |
| 285 |
* inlining currently breaks the blocklisting mechanism of ThreadSanitizer. |
| 286 |
*/ |
| 287 |
#if defined(__has_feature) |
| 288 |
# if __has_feature(thread_sanitizer) |
| 289 |
# define MOZ_TSAN_IGNORE MOZ_NEVER_INLINE __attribute__((no_sanitize_thread)) |
| 290 |
# else |
| 291 |
# define MOZ_TSAN_IGNORE /* nothing */ |
| 292 |
# endif |
| 293 |
#else |
| 294 |
# define MOZ_TSAN_IGNORE /* nothing */ |
| 295 |
#endif |
| 296 |
|
| 297 |
#if defined(__has_attribute) |
| 298 |
# if __has_attribute(no_sanitize) |
| 299 |
# define MOZ_HAVE_NO_SANITIZE_ATTR |
| 300 |
# endif |
| 301 |
#endif |
| 302 |
|
| 303 |
#ifdef __clang__ |
| 304 |
# ifdef MOZ_HAVE_NO_SANITIZE_ATTR |
| 305 |
# define MOZ_HAVE_UNSIGNED_OVERFLOW_SANITIZE_ATTR |
| 306 |
# define MOZ_HAVE_SIGNED_OVERFLOW_SANITIZE_ATTR |
| 307 |
# endif |
| 308 |
#endif |
| 309 |
|
| 310 |
/* |
| 311 |
* MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW disables *un*signed integer overflow |
| 312 |
* checking on the function it annotates, in builds configured to perform it. |
| 313 |
* (Currently this is only Clang using -fsanitize=unsigned-integer-overflow, or |
| 314 |
* via --enable-unsigned-overflow-sanitizer in Mozilla's build system.) It has |
| 315 |
* no effect in other builds. |
| 316 |
* |
| 317 |
* Place this attribute at the very beginning of a function declaration. |
| 318 |
* |
| 319 |
* Unsigned integer overflow isn't *necessarily* a bug. It's well-defined in |
| 320 |
* C/C++, and code may reasonably depend upon it. For example, |
| 321 |
* |
| 322 |
* MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW inline bool |
| 323 |
* IsDecimal(char aChar) |
| 324 |
* { |
| 325 |
* // For chars less than '0', unsigned integer underflow occurs, to a value |
| 326 |
* // much greater than 10, so the overall test is false. |
| 327 |
* // For chars greater than '0', no overflow occurs, and only '0' to '9' |
| 328 |
* // pass the overall test. |
| 329 |
* return static_cast<unsigned int>(aChar) - '0' < 10; |
| 330 |
* } |
| 331 |
* |
| 332 |
* But even well-defined unsigned overflow often causes bugs when it occurs, so |
| 333 |
* it should be restricted to functions annotated with this attribute. |
| 334 |
* |
| 335 |
* The compiler instrumentation to detect unsigned integer overflow has costs |
| 336 |
* both at compile time and at runtime. Functions that are repeatedly inlined |
| 337 |
* at compile time will also implicitly inline the necessary instrumentation, |
| 338 |
* increasing compile time. Similarly, frequently-executed functions that |
| 339 |
* require large amounts of instrumentation will also notice significant runtime |
| 340 |
* slowdown to execute that instrumentation. Use this attribute to eliminate |
| 341 |
* those costs -- but only after carefully verifying that no overflow can occur. |
| 342 |
*/ |
| 343 |
#ifdef MOZ_HAVE_UNSIGNED_OVERFLOW_SANITIZE_ATTR |
| 344 |
# define MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW \ |
| 345 |
__attribute__((no_sanitize("unsigned-integer-overflow"))) |
| 346 |
#else |
| 347 |
# define MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW /* nothing */ |
| 348 |
#endif |
| 349 |
|
| 350 |
/* |
| 351 |
* MOZ_NO_SANITIZE_SIGNED_OVERFLOW disables *signed* integer overflow checking |
| 352 |
* on the function it annotates, in builds configured to perform it. (Currently |
| 353 |
* this is only Clang using -fsanitize=signed-integer-overflow, or via |
| 354 |
* --enable-signed-overflow-sanitizer in Mozilla's build system. GCC support |
| 355 |
* will probably be added in the future.) It has no effect in other builds. |
| 356 |
* |
| 357 |
* Place this attribute at the very beginning of a function declaration. |
| 358 |
* |
| 359 |
* Signed integer overflow is undefined behavior in C/C++: *anything* can happen |
| 360 |
* when it occurs. *Maybe* wraparound behavior will occur, but maybe also the |
| 361 |
* compiler will assume no overflow happens and will adversely optimize the rest |
| 362 |
* of your code. Code that contains signed integer overflow needs to be fixed. |
| 363 |
* |
| 364 |
* The compiler instrumentation to detect signed integer overflow has costs both |
| 365 |
* at compile time and at runtime. Functions that are repeatedly inlined at |
| 366 |
* compile time will also implicitly inline the necessary instrumentation, |
| 367 |
* increasing compile time. Similarly, frequently-executed functions that |
| 368 |
* require large amounts of instrumentation will also notice significant runtime |
| 369 |
* slowdown to execute that instrumentation. Use this attribute to eliminate |
| 370 |
* those costs -- but only after carefully verifying that no overflow can occur. |
| 371 |
*/ |
| 372 |
#ifdef MOZ_HAVE_SIGNED_OVERFLOW_SANITIZE_ATTR |
| 373 |
# define MOZ_NO_SANITIZE_SIGNED_OVERFLOW \ |
| 374 |
__attribute__((no_sanitize("signed-integer-overflow"))) |
| 375 |
#else |
| 376 |
# define MOZ_NO_SANITIZE_SIGNED_OVERFLOW /* nothing */ |
| 377 |
#endif |
| 378 |
|
| 379 |
#undef MOZ_HAVE_NO_SANITIZE_ATTR |
| 380 |
|
| 381 |
/** |
| 382 |
* MOZ_ALLOCATOR tells the compiler that the function it marks returns either a |
| 383 |
* "fresh", "pointer-free" block of memory, or nullptr. "Fresh" means that the |
| 384 |
* block is not pointed to by any other reachable pointer in the program. |
| 385 |
* "Pointer-free" means that the block contains no pointers to any valid object |
| 386 |
* in the program. It may be initialized with other (non-pointer) values. |
| 387 |
* |
| 388 |
* Placing this attribute on appropriate functions helps GCC analyze pointer |
| 389 |
* aliasing more accurately in their callers. |
| 390 |
* |
| 391 |
* GCC warns if a caller ignores the value returned by a function marked with |
| 392 |
* MOZ_ALLOCATOR: it is hard to imagine cases where dropping the value returned |
| 393 |
* by a function that meets the criteria above would be intentional. |
| 394 |
* |
| 395 |
* Place this attribute after the argument list and 'this' qualifiers of a |
| 396 |
* function definition. For example, write |
| 397 |
* |
| 398 |
* void *my_allocator(size_t) MOZ_ALLOCATOR; |
| 399 |
* |
| 400 |
* or |
| 401 |
* |
| 402 |
* void *my_allocator(size_t bytes) MOZ_ALLOCATOR { ... } |
| 403 |
*/ |
| 404 |
#if defined(__GNUC__) || defined(__clang__) |
| 405 |
# define MOZ_ALLOCATOR __attribute__((malloc, warn_unused_result)) |
| 406 |
# define MOZ_INFALLIBLE_ALLOCATOR \ |
| 407 |
__attribute__((malloc, warn_unused_result, returns_nonnull)) |
| 408 |
#else |
| 409 |
# define MOZ_ALLOCATOR |
| 410 |
# define MOZ_INFALLIBLE_ALLOCATOR |
| 411 |
#endif |
| 412 |
|
| 413 |
/** |
| 414 |
* MOZ_MAYBE_UNUSED suppresses compiler warnings about functions that are |
| 415 |
* never called (in this build configuration, at least). |
| 416 |
* |
| 417 |
* Place this attribute at the very beginning of a function declaration. For |
| 418 |
* example, write |
| 419 |
* |
| 420 |
* MOZ_MAYBE_UNUSED int foo(); |
| 421 |
* |
| 422 |
* or |
| 423 |
* |
| 424 |
* MOZ_MAYBE_UNUSED int foo() { return 42; } |
| 425 |
*/ |
| 426 |
#if defined(__GNUC__) || defined(__clang__) |
| 427 |
# define MOZ_MAYBE_UNUSED __attribute__((__unused__)) |
| 428 |
#elif defined(_MSC_VER) |
| 429 |
# define MOZ_MAYBE_UNUSED __pragma(warning(suppress : 4505)) |
| 430 |
#else |
| 431 |
# define MOZ_MAYBE_UNUSED |
| 432 |
#endif |
| 433 |
|
| 434 |
/* |
| 435 |
* MOZ_NO_STACK_PROTECTOR, specified at the start of a function declaration, |
| 436 |
* indicates that the given function should *NOT* be instrumented to detect |
| 437 |
* stack buffer overflows at runtime. (The function definition does not need to |
| 438 |
* be annotated.) |
| 439 |
* |
| 440 |
* MOZ_NO_STACK_PROTECTOR int foo(); |
| 441 |
* |
| 442 |
* Detecting stack buffer overflows at runtime is a security feature. This |
| 443 |
* modifier should thus only be used on functions which are provably exempt of |
| 444 |
* stack buffer overflows, for example because they do not use stack buffers. |
| 445 |
* |
| 446 |
* This modifier does not affect the corresponding function's linking behavior. |
| 447 |
*/ |
| 448 |
#if defined(MOZ_HAVE_NO_STACK_PROTECTOR) |
| 449 |
# define MOZ_NO_STACK_PROTECTOR MOZ_HAVE_NO_STACK_PROTECTOR |
| 450 |
#else |
| 451 |
# define MOZ_NO_STACK_PROTECTOR /* no support */ |
| 452 |
#endif |
| 453 |
|
| 454 |
/** |
| 455 |
* MOZ_GSL_OWNER indicates that objects of the type this annotation is attached |
| 456 |
* to own some kind of resources, generally memory. |
| 457 |
* |
| 458 |
* See: https://clang.llvm.org/docs/AttributeReference.html#owner |
| 459 |
*/ |
| 460 |
#if defined(__clang__) && defined(__has_cpp_attribute) |
| 461 |
# if __has_cpp_attribute(gsl::Owner) |
| 462 |
# define MOZ_GSL_OWNER [[gsl::Owner]] |
| 463 |
# else |
| 464 |
# define MOZ_GSL_OWNER /* nothing */ |
| 465 |
# endif |
| 466 |
#else |
| 467 |
# define MOZ_GSL_OWNER /* nothing */ |
| 468 |
#endif |
| 469 |
|
| 470 |
/** |
| 471 |
* MOZ_GSL_POINTER indicates that objects of the type this annotation is |
| 472 |
* attached to provide a non-owning view on some kind of resources, generally |
| 473 |
* memory. |
| 474 |
* |
| 475 |
* See: https://clang.llvm.org/docs/AttributeReference.html#pointer |
| 476 |
*/ |
| 477 |
#if defined(__clang__) && defined(__has_cpp_attribute) |
| 478 |
# if __has_cpp_attribute(gsl::Pointer) |
| 479 |
# define MOZ_GSL_POINTER [[gsl::Pointer]] |
| 480 |
# else |
| 481 |
# define MOZ_GSL_POINTER /* nothing */ |
| 482 |
# endif |
| 483 |
#else |
| 484 |
# define MOZ_GSL_POINTER /* nothing */ |
| 485 |
#endif |
| 486 |
|
| 487 |
/** |
| 488 |
* MOZ_LIFETIME_BOUND indicates that objects that are referred to by that |
| 489 |
* parameter may also be referred to by the return value of the annotated |
| 490 |
* function (or, for a parameter of a constructor, by the value of the |
| 491 |
* constructed object). |
| 492 |
* See: https://clang.llvm.org/docs/AttributeReference.html#lifetimebound |
| 493 |
*/ |
| 494 |
#if defined(__clang__) && defined(__has_cpp_attribute) |
| 495 |
# if __has_cpp_attribute(clang::lifetimebound) |
| 496 |
# define MOZ_LIFETIME_BOUND [[clang::lifetimebound]] |
| 497 |
# else |
| 498 |
# define MOZ_LIFETIME_BOUND /* nothing */ |
| 499 |
# endif |
| 500 |
#else |
| 501 |
# define MOZ_LIFETIME_BOUND /* nothing */ |
| 502 |
#endif |
| 503 |
|
| 504 |
/** |
| 505 |
* MOZ_LIFETIME_CAPTURE_BY(x) indicates that objects that are referred to |
| 506 |
* by that parameter may also be referred to by x. |
| 507 |
* See: https://clang.llvm.org/docs/AttributeReference.html#lifetime-capture-by |
| 508 |
*/ |
| 509 |
#if defined(__clang__) && defined(__has_cpp_attribute) |
| 510 |
# if __has_cpp_attribute(clang::lifetime_capture_by) |
| 511 |
# define MOZ_LIFETIME_CAPTURE_BY(x) [[clang::lifetime_capture_by(x)]] |
| 512 |
# else |
| 513 |
# define MOZ_LIFETIME_CAPTURE_BY(x) /* nothing */ |
| 514 |
# endif |
| 515 |
#else |
| 516 |
# define MOZ_LIFETIME_CAPTURE_BY(x) /* nothing */ |
| 517 |
#endif |
| 518 |
|
| 519 |
#ifdef __cplusplus |
| 520 |
|
| 521 |
/** |
| 522 |
* C++11 lets unions contain members that have non-trivial special member |
| 523 |
* functions (default/copy/move constructor, copy/move assignment operator, |
| 524 |
* destructor) if the user defines the corresponding functions on the union. |
| 525 |
* (Such user-defined functions must rely on external knowledge about which arm |
| 526 |
* is active to be safe. Be extra-careful defining these functions!) |
| 527 |
* |
| 528 |
* MSVC unfortunately warns/errors for this bog-standard C++11 pattern. Use |
| 529 |
* these macro-guards around such member functions to disable the warnings: |
| 530 |
* |
| 531 |
* union U |
| 532 |
* { |
| 533 |
* std::string s; |
| 534 |
* int x; |
| 535 |
* |
| 536 |
* MOZ_PUSH_DISABLE_NONTRIVIAL_UNION_WARNINGS |
| 537 |
* |
| 538 |
* // |U| must have a user-defined default constructor because |std::string| |
| 539 |
* // has a non-trivial default constructor. |
| 540 |
* U() ... { ... } |
| 541 |
* |
| 542 |
* // |U| must have a user-defined destructor because |std::string| has a |
| 543 |
* // non-trivial destructor. |
| 544 |
* ~U() { ... } |
| 545 |
* |
| 546 |
* MOZ_POP_DISABLE_NONTRIVIAL_UNION_WARNINGS |
| 547 |
* }; |
| 548 |
*/ |
| 549 |
# if defined(_MSC_VER) |
| 550 |
# define MOZ_PUSH_DISABLE_NONTRIVIAL_UNION_WARNINGS \ |
| 551 |
__pragma(warning(push)) __pragma(warning(disable : 4582)) \ |
| 552 |
__pragma(warning(disable : 4583)) |
| 553 |
# define MOZ_POP_DISABLE_NONTRIVIAL_UNION_WARNINGS __pragma(warning(pop)) |
| 554 |
# else |
| 555 |
# define MOZ_PUSH_DISABLE_NONTRIVIAL_UNION_WARNINGS /* nothing */ |
| 556 |
# define MOZ_POP_DISABLE_NONTRIVIAL_UNION_WARNINGS /* nothing */ |
| 557 |
# endif |
| 558 |
|
| 559 |
/* |
| 560 |
* The following macros are attributes that support the static analysis plugin |
| 561 |
* included with Mozilla, and will be implemented (when such support is enabled) |
| 562 |
* as C++11 attributes. Since such attributes are legal pretty much everywhere |
| 563 |
* and have subtly different semantics depending on their placement, the |
| 564 |
* following is a guide on where to place the attributes. |
| 565 |
* |
| 566 |
* Attributes that apply to a struct or class precede the name of the class: |
| 567 |
* (Note that this is different from the placement of final for classes!) |
| 568 |
* |
| 569 |
* class MOZ_CLASS_ATTRIBUTE SomeClass {}; |
| 570 |
* |
| 571 |
* Attributes that apply to functions follow the parentheses and const |
| 572 |
* qualifiers but precede final, override and the function body: |
| 573 |
* |
| 574 |
* void DeclaredFunction() MOZ_FUNCTION_ATTRIBUTE; |
| 575 |
* void SomeFunction() MOZ_FUNCTION_ATTRIBUTE {} |
| 576 |
* void PureFunction() const MOZ_FUNCTION_ATTRIBUTE = 0; |
| 577 |
* void OverriddenFunction() MOZ_FUNCTION_ATTIRBUTE override; |
| 578 |
* |
| 579 |
* Attributes that apply to variables or parameters follow the variable's name: |
| 580 |
* |
| 581 |
* int variable MOZ_VARIABLE_ATTRIBUTE; |
| 582 |
* |
| 583 |
* Attributes that apply to types follow the type name: |
| 584 |
* |
| 585 |
* typedef int MOZ_TYPE_ATTRIBUTE MagicInt; |
| 586 |
* int MOZ_TYPE_ATTRIBUTE someVariable; |
| 587 |
* int* MOZ_TYPE_ATTRIBUTE magicPtrInt; |
| 588 |
* int MOZ_TYPE_ATTRIBUTE* ptrToMagicInt; |
| 589 |
* |
| 590 |
* Attributes that apply to statements precede the statement: |
| 591 |
* |
| 592 |
* MOZ_IF_ATTRIBUTE if (x == 0) |
| 593 |
* MOZ_DO_ATTRIBUTE do { } while (0); |
| 594 |
* |
| 595 |
* Attributes that apply to labels precede the label: |
| 596 |
* |
| 597 |
* MOZ_LABEL_ATTRIBUTE target: |
| 598 |
* goto target; |
| 599 |
* MOZ_CASE_ATTRIBUTE case 5: |
| 600 |
* MOZ_DEFAULT_ATTRIBUTE default: |
| 601 |
* |
| 602 |
* The static analyses that are performed by the plugin are as follows: |
| 603 |
* |
| 604 |
* MOZ_CAN_RUN_SCRIPT: Applies to functions which can run script. Callers of |
| 605 |
* this function must also be marked as MOZ_CAN_RUN_SCRIPT, and all refcounted |
| 606 |
* arguments must be strongly held in the caller. Note that MOZ_CAN_RUN_SCRIPT |
| 607 |
* should only be applied to function declarations, not definitions. If you |
| 608 |
* need to apply it to a definition (eg because both are generated by a macro) |
| 609 |
* use MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION. |
| 610 |
* |
| 611 |
* MOZ_CAN_RUN_SCRIPT can be applied to XPIDL-generated declarations by |
| 612 |
* annotating the method or attribute as [can_run_script] in the .idl file. |
| 613 |
* |
| 614 |
* MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION: Same as MOZ_CAN_RUN_SCRIPT, but usable on |
| 615 |
* a definition. If the declaration is in a header file, users of that header |
| 616 |
* file may not see the annotation. |
| 617 |
* MOZ_CAN_RUN_SCRIPT_BOUNDARY: Applies to functions which need to call |
| 618 |
* MOZ_CAN_RUN_SCRIPT functions, but should not themselves be considered |
| 619 |
* MOZ_CAN_RUN_SCRIPT. This should generally be avoided but can be used in |
| 620 |
* two cases: |
| 621 |
* 1) As a temporary measure to limit the scope of changes when adding |
| 622 |
* MOZ_CAN_RUN_SCRIPT. Such a use must be accompanied by a follow-up bug |
| 623 |
* to replace the MOZ_CAN_RUN_SCRIPT_BOUNDARY with MOZ_CAN_RUN_SCRIPT and |
| 624 |
* a comment linking to that bug. |
| 625 |
* 2) If we can reason that the MOZ_CAN_RUN_SCRIPT callees of the function |
| 626 |
* do not in fact run script (for example, because their behavior depends |
| 627 |
* on arguments and we pass the arguments that don't allow script |
| 628 |
* execution). Such a use must be accompanied by a comment that explains |
| 629 |
* why it's OK to have the MOZ_CAN_RUN_SCRIPT_BOUNDARY, as well as |
| 630 |
* comments in the callee pointing out that if its behavior changes the |
| 631 |
* caller might need adjusting. And perhaps also a followup bug to |
| 632 |
* refactor things so the "script" and "no script" codepaths do not share |
| 633 |
* a chokepoint. |
| 634 |
* Importantly, any use MUST be accompanied by a comment explaining why it's |
| 635 |
* there, and should ideally have an action plan for getting rid of the |
| 636 |
* MOZ_CAN_RUN_SCRIPT_BOUNDARY annotation. |
| 637 |
* MOZ_MUST_OVERRIDE: Applies to all C++ member functions. All immediate |
| 638 |
* subclasses must provide an exact override of this method; if a subclass |
| 639 |
* does not override this method, the compiler will emit an error. This |
| 640 |
* attribute is not limited to virtual methods, so if it is applied to a |
| 641 |
* nonvirtual method and the subclass does not provide an equivalent |
| 642 |
* definition, the compiler will emit an error. |
| 643 |
* MOZ_STATIC_CLASS: Applies to all classes. Any class with this annotation is |
| 644 |
* expected to live in static memory, so it is a compile-time error to use |
| 645 |
* it, or an array of such objects, as the type of a variable declaration, or |
| 646 |
* as a temporary object, or as the type of a new expression (unless |
| 647 |
* placement new is being used). If a member of another class uses this |
| 648 |
* class, or if another class inherits from this class, then it is considered |
| 649 |
* to be a static class as well, although this attribute need not be provided |
| 650 |
* in such cases. |
| 651 |
* MOZ_STATIC_LOCAL_CLASS: Applies to all classes. Any class with this |
| 652 |
* annotation is expected to be a static local variable, so it is |
| 653 |
* a compile-time error to use it, or an array of such objects, or as a |
| 654 |
* temporary object, or as the type of a new expression. If another class |
| 655 |
* inherits from this class then it is considered to be a static local |
| 656 |
* class as well, although this attribute need not be provided in such cases. |
| 657 |
* It is also a compile-time error for any class with this annotation to have |
| 658 |
* a non-trivial destructor. |
| 659 |
* MOZ_STACK_CLASS: Applies to all classes. Any class with this annotation is |
| 660 |
* expected to live on the stack, so it is a compile-time error to use it, or |
| 661 |
* an array of such objects, as a global or static variable, or as the type of |
| 662 |
* a new expression (unless placement new is being used). If a member of |
| 663 |
* another class uses this class, or if another class inherits from this |
| 664 |
* class, then it is considered to be a stack class as well, although this |
| 665 |
* attribute need not be provided in such cases. |
| 666 |
* MOZ_NONHEAP_CLASS: Applies to all classes. Any class with this annotation is |
| 667 |
* expected to live on the stack or in static storage, so it is a compile-time |
| 668 |
* error to use it, or an array of such objects, as the type of a new |
| 669 |
* expression. If a member of another class uses this class, or if another |
| 670 |
* class inherits from this class, then it is considered to be a non-heap |
| 671 |
* class as well, although this attribute need not be provided in such cases. |
| 672 |
* MOZ_CONSTINIT: pre-C++20 equivalent to `constinit`. |
| 673 |
* MOZ_RUNINIT: Applies to global variables with runtime initialization. |
| 674 |
* MOZ_GLOBINIT: Applies to global variables with potential runtime |
| 675 |
* initialization (e.g. inside macro or when initialisation status depends on |
| 676 |
* template parameter). |
| 677 |
* MOZ_HEAP_CLASS: Applies to all classes. Any class with this annotation is |
| 678 |
* expected to live on the heap, so it is a compile-time error to use it, or |
| 679 |
* an array of such objects, as the type of a variable declaration, or as a |
| 680 |
* temporary object. If a member of another class uses this class, or if |
| 681 |
* another class inherits from this class, then it is considered to be a heap |
| 682 |
* class as well, although this attribute need not be provided in such cases. |
| 683 |
* MOZ_NON_TEMPORARY_CLASS: Applies to all classes. Any class with this |
| 684 |
* annotation is expected not to live in a temporary. If a member of another |
| 685 |
* class uses this class or if another class inherits from this class, then it |
| 686 |
* is considered to be a non-temporary class as well, although this attribute |
| 687 |
* need not be provided in such cases. |
| 688 |
* MOZ_TEMPORARY_CLASS: Applies to all classes. Any class with this annotation |
| 689 |
* is expected to only live in a temporary. If another class inherits from |
| 690 |
* this class, then it is considered to be a temporary class as well, although |
| 691 |
* this attribute need not be provided in such cases. |
| 692 |
* MOZ_RAII: Applies to all classes. Any class with this annotation is assumed |
| 693 |
* to be a RAII guard, which is expected to live on the stack in an automatic |
| 694 |
* allocation. It is prohibited from being allocated in a temporary, static |
| 695 |
* storage, or on the heap. This is a combination of MOZ_STACK_CLASS and |
| 696 |
* MOZ_NON_TEMPORARY_CLASS. |
| 697 |
* MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS: Applies to all classes that are |
| 698 |
* intended to prevent introducing static initializers. This attribute |
| 699 |
* currently makes it a compile-time error to instantiate these classes |
| 700 |
* anywhere other than at the global scope, or as a static member of a class. |
| 701 |
* In non-debug mode, it also prohibits non-trivial constructors and |
| 702 |
* destructors. |
| 703 |
* MOZ_TRIVIAL_CTOR_DTOR: Applies to all classes that must have both a trivial |
| 704 |
* or constexpr constructor and a trivial destructor. Setting this attribute |
| 705 |
* on a class makes it a compile-time error for that class to get a |
| 706 |
* non-trivial constructor or destructor for any reason. |
| 707 |
* MOZ_ALLOW_TEMPORARY: Applies to constructors. This indicates that using the |
| 708 |
* constructor is allowed in temporary expressions, if it would have otherwise |
| 709 |
* been forbidden by the type being a MOZ_NON_TEMPORARY_CLASS. Useful for |
| 710 |
* constructors like Maybe(Nothing). |
| 711 |
* MOZ_HEAP_ALLOCATOR: Applies to any function. This indicates that the return |
| 712 |
* value is allocated on the heap, and will as a result check such allocations |
| 713 |
* during MOZ_STACK_CLASS and MOZ_NONHEAP_CLASS annotation checking. |
| 714 |
* MOZ_IMPLICIT: Applies to constructors. Implicit conversion constructors |
| 715 |
* are disallowed by default unless they are marked as MOZ_IMPLICIT. This |
| 716 |
* attribute must be used for constructors which intend to provide implicit |
| 717 |
* conversions. |
| 718 |
* MOZ_IS_REFPTR: Applies to class declarations of ref pointer to mark them as |
| 719 |
* such for use with static-analysis. |
| 720 |
* A ref pointer is an object wrapping a pointer and automatically taking care |
| 721 |
* of its refcounting upon construction/destruction/transfer of ownership. |
| 722 |
* This annotation implies MOZ_IS_SMARTPTR_TO_REFCOUNTED. |
| 723 |
* MOZ_IS_SMARTPTR_TO_REFCOUNTED: Applies to class declarations of smart |
| 724 |
* pointers to ref counted classes to mark them as such for use with |
| 725 |
* static-analysis. |
| 726 |
* MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT: Applies to functions. Makes it a compile |
| 727 |
* time error to pass arithmetic expressions on variables to the function. |
| 728 |
* MOZ_OWNING_REF: Applies to declarations of pointers to reference counted |
| 729 |
* types. This attribute tells the compiler that the raw pointer is a strong |
| 730 |
* reference, where ownership through methods such as AddRef and Release is |
| 731 |
* managed manually. This can make the compiler ignore these pointers when |
| 732 |
* validating the usage of pointers otherwise. |
| 733 |
* |
| 734 |
* Example uses include owned pointers inside of unions, and pointers stored |
| 735 |
* in POD types where a using a smart pointer class would make the object |
| 736 |
* non-POD. |
| 737 |
* MOZ_NON_OWNING_REF: Applies to declarations of pointers to reference counted |
| 738 |
* types. This attribute tells the compiler that the raw pointer is a weak |
| 739 |
* reference, which is ensured to be valid by a guarantee that the reference |
| 740 |
* will be nulled before the pointer becomes invalid. This can make the |
| 741 |
* compiler ignore these pointers when validating the usage of pointers |
| 742 |
* otherwise. |
| 743 |
* |
| 744 |
* Examples include an mOwner pointer, which is nulled by the owning class's |
| 745 |
* destructor, and is null-checked before dereferencing. |
| 746 |
* MOZ_UNSAFE_REF: Applies to declarations of pointers to reference counted |
| 747 |
* types. Occasionally there are non-owning references which are valid, but |
| 748 |
* do not take the form of a MOZ_NON_OWNING_REF. Their safety may be |
| 749 |
* dependent on the behaviour of API consumers. The string argument passed |
| 750 |
* to this macro documents the safety conditions. This can make the compiler |
| 751 |
* ignore these pointers when validating the usage of pointers elsewhere. |
| 752 |
* |
| 753 |
* Examples include an nsAtom* member which is known at compile time to point |
| 754 |
* to a static atom which is valid throughout the lifetime of the program, or |
| 755 |
* an API which stores a pointer, but doesn't take ownership over it, instead |
| 756 |
* requiring the API consumer to correctly null the value before it becomes |
| 757 |
* invalid. |
| 758 |
* |
| 759 |
* Use of this annotation is discouraged when a strong reference or one of |
| 760 |
* the above two annotations can be used instead. |
| 761 |
* MOZ_NO_ADDREF_RELEASE_ON_RETURN: Applies to function declarations. Makes it |
| 762 |
* a compile time error to call AddRef or Release on the return value of a |
| 763 |
* function. This is intended to be used with operator->() of our smart |
| 764 |
* pointer classes to ensure that the refcount of an object wrapped in a |
| 765 |
* smart pointer is not manipulated directly. |
| 766 |
* MOZ_NEEDS_NO_VTABLE_TYPE: Applies to template class declarations. Makes it |
| 767 |
* a compile time error to instantiate this template with a type parameter |
| 768 |
* which has a VTable. |
| 769 |
* MOZ_NON_MEMMOVABLE: Applies to class declarations for types that are not safe |
| 770 |
* to be moved in memory using memmove(). |
| 771 |
* MOZ_NEEDS_MEMMOVABLE_TYPE: Applies to template class declarations where the |
| 772 |
* template arguments are required to be safe to move in memory using |
| 773 |
* memmove(). Passing MOZ_NON_MEMMOVABLE types to these templates is a |
| 774 |
* compile time error. |
| 775 |
* MOZ_NEEDS_MEMMOVABLE_MEMBERS: Applies to class declarations where each member |
| 776 |
* must be safe to move in memory using memmove(). MOZ_NON_MEMMOVABLE types |
| 777 |
* used in members of these classes are compile time errors. |
| 778 |
* MOZ_NO_DANGLING_ON_TEMPORARIES: Applies to method declarations which return |
| 779 |
* a pointer that is freed when the destructor of the class is called. This |
| 780 |
* prevents these methods from being called on temporaries of the class, |
| 781 |
* reducing risks of use-after-free. |
| 782 |
* This attribute cannot be applied to && methods. |
| 783 |
* In some cases, adding a deleted &&-qualified overload is too restrictive as |
| 784 |
* this method should still be callable as a non-escaping argument to another |
| 785 |
* function. This annotation can be used in those cases. |
| 786 |
* MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS: Applies to template class |
| 787 |
* declarations where an instance of the template should be considered, for |
| 788 |
* static analysis purposes, to inherit any type annotations (such as |
| 789 |
* MOZ_STACK_CLASS) from its template arguments. |
| 790 |
* MOZ_INIT_OUTSIDE_CTOR: Applies to class member declarations. Occasionally |
| 791 |
* there are class members that are not initialized in the constructor, |
| 792 |
* but logic elsewhere in the class ensures they are initialized prior to use. |
| 793 |
* Using this attribute on a member disables the check that this member must |
| 794 |
* be initialized in constructors via list-initialization, in the constructor |
| 795 |
* body, or via functions called from the constructor body. |
| 796 |
* MOZ_IS_CLASS_INIT: Applies to class method declarations. Occasionally the |
| 797 |
* constructor doesn't initialize all of the member variables and another |
| 798 |
* function is used to initialize the rest. This marker is used to make the |
| 799 |
* static analysis tool aware that the marked function is part of the |
| 800 |
* initialization process and to include the marked function in the scan |
| 801 |
* mechanism that determines which member variables still remain |
| 802 |
* uninitialized. |
| 803 |
* MOZ_NON_PARAM: Applies to types. Makes it compile time error to use the type |
| 804 |
* in parameter without pointer or reference. |
| 805 |
* MOZ_NON_AUTOABLE: Applies to class declarations. Makes it a compile time |
| 806 |
* error to use `auto` in place of this type in variable declarations. This |
| 807 |
* is intended to be used with types which are intended to be implicitly |
| 808 |
* constructed into other other types before being assigned to variables. |
| 809 |
* MOZ_REQUIRED_BASE_METHOD: Applies to virtual class method declarations. |
| 810 |
* Sometimes derived classes override methods that need to be called by their |
| 811 |
* overridden counterparts. This marker indicates that the marked method must |
| 812 |
* be called by the method that it overrides. |
| 813 |
* MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG: Applies to method declarations. |
| 814 |
* Callers of the annotated method must return from that function within the |
| 815 |
* calling block using an explicit `return` statement if the "this" value for |
| 816 |
* the call is a parameter of the caller. Only calls to Constructors, |
| 817 |
* references to local and member variables, and calls to functions or |
| 818 |
* methods marked as MOZ_MAY_CALL_AFTER_MUST_RETURN may be made after the |
| 819 |
* MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG call. |
| 820 |
* MOZ_MAY_CALL_AFTER_MUST_RETURN: Applies to function or method declarations. |
| 821 |
* Calls to these methods may be made in functions after calls a |
| 822 |
* MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG method. |
| 823 |
* MOZ_UNANNOTATED/MOZ_ANNOTATED: Applies to Mutexes/Monitors and variations on |
| 824 |
* them. MOZ_UNANNOTATED indicates that the Mutex/Monitor/etc hasn't been |
| 825 |
* examined and annotated using macros from mfbt/ThreadSafety -- |
| 826 |
* MOZ_GUARDED_BY()/REQUIRES()/etc. MOZ_ANNOTATED is used in rare cases to |
| 827 |
* indicate that is has been looked at, but it did not need any |
| 828 |
* MOZ_GUARDED_BY()/REQUIRES()/etc (and thus static analysis knows it can |
| 829 |
* ignore this Mutex/Monitor/etc) |
| 830 |
*/ |
| 831 |
|
| 832 |
// gcc emits a nuisance warning -Wignored-attributes because attributes do not |
| 833 |
// affect mangled names, and therefore template arguments do not propagate |
| 834 |
// their attributes. It is rare that this would affect anything in practice, |
| 835 |
// and most compilers are silent about it. Similarly, -Wattributes complains |
| 836 |
// about attributes being ignored during template instantiation. |
| 837 |
// |
| 838 |
// Be conservative and only suppress the warning when running in a |
| 839 |
// configuration where it would be emitted, namely when compiling with the |
| 840 |
// XGILL_PLUGIN for the rooting hazard analysis (which runs under gcc.) If we |
| 841 |
// end up wanting these attributes in general GCC builds, change this to |
| 842 |
// something like |
| 843 |
// |
| 844 |
// #if defined(__GNUC__) && ! defined(__clang__) |
| 845 |
// |
| 846 |
# ifdef XGILL_PLUGIN |
| 847 |
# pragma GCC diagnostic ignored "-Wignored-attributes" |
| 848 |
# pragma GCC diagnostic ignored "-Wattributes" |
| 849 |
# endif |
| 850 |
|
| 851 |
# if defined(MOZ_CLANG_PLUGIN) || defined(XGILL_PLUGIN) |
| 852 |
# define MOZ_CAN_RUN_SCRIPT __attribute__((annotate("moz_can_run_script"))) |
| 853 |
# define MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION \ |
| 854 |
__attribute__((annotate("moz_can_run_script"))) \ |
| 855 |
__attribute__((annotate("moz_can_run_script_for_definition"))) |
| 856 |
# define MOZ_CAN_RUN_SCRIPT_BOUNDARY \ |
| 857 |
__attribute__((annotate("moz_can_run_script_boundary"))) |
| 858 |
# define MOZ_MUST_OVERRIDE __attribute__((annotate("moz_must_override"))) |
| 859 |
# define MOZ_STATIC_CLASS __attribute__((annotate("moz_global_class"))) |
| 860 |
# define MOZ_STATIC_LOCAL_CLASS \ |
| 861 |
__attribute__((annotate("moz_static_local_class"))) \ |
| 862 |
__attribute__((annotate("moz_trivial_dtor"))) |
| 863 |
# define MOZ_STACK_CLASS __attribute__((annotate("moz_stack_class"))) |
| 864 |
# define MOZ_NONHEAP_CLASS __attribute__((annotate("moz_nonheap_class"))) |
| 865 |
# define MOZ_HEAP_CLASS __attribute__((annotate("moz_heap_class"))) |
| 866 |
# define MOZ_NON_TEMPORARY_CLASS \ |
| 867 |
__attribute__((annotate("moz_non_temporary_class"))) |
| 868 |
# define MOZ_TEMPORARY_CLASS __attribute__((annotate("moz_temporary_class"))) |
| 869 |
# define MOZ_TRIVIAL_CTOR_DTOR \ |
| 870 |
__attribute__((annotate("moz_trivial_ctor_dtor"))) |
| 871 |
# define MOZ_ALLOW_TEMPORARY __attribute__((annotate("moz_allow_temporary"))) |
| 872 |
# ifdef DEBUG |
| 873 |
/* in debug builds, these classes do have non-trivial constructors. */ |
| 874 |
# define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS \ |
| 875 |
__attribute__((annotate("moz_global_class"))) |
| 876 |
# else |
| 877 |
# define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS \ |
| 878 |
__attribute__((annotate("moz_global_class"))) MOZ_TRIVIAL_CTOR_DTOR |
| 879 |
# endif |
| 880 |
# define MOZ_IMPLICIT __attribute__((annotate("moz_implicit"))) |
| 881 |
# define MOZ_IS_SMARTPTR_TO_REFCOUNTED \ |
| 882 |
__attribute__((annotate("moz_is_smartptr_to_refcounted"))) |
| 883 |
# define MOZ_IS_REFPTR MOZ_IS_SMARTPTR_TO_REFCOUNTED |
| 884 |
# define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT \ |
| 885 |
__attribute__((annotate("moz_no_arith_expr_in_arg"))) |
| 886 |
# define MOZ_OWNING_REF __attribute__((annotate("moz_owning_ref"))) |
| 887 |
# define MOZ_NON_OWNING_REF __attribute__((annotate("moz_non_owning_ref"))) |
| 888 |
# define MOZ_UNSAFE_REF(reason) __attribute__((annotate("moz_unsafe_ref"))) |
| 889 |
# define MOZ_NO_ADDREF_RELEASE_ON_RETURN \ |
| 890 |
__attribute__((annotate("moz_no_addref_release_on_return"))) |
| 891 |
# define MOZ_NEEDS_NO_VTABLE_TYPE \ |
| 892 |
__attribute__((annotate("moz_needs_no_vtable_type"))) |
| 893 |
# define MOZ_NON_MEMMOVABLE __attribute__((annotate("moz_non_memmovable"))) |
| 894 |
# define MOZ_NEEDS_MEMMOVABLE_TYPE \ |
| 895 |
__attribute__((annotate("moz_needs_memmovable_type"))) |
| 896 |
# define MOZ_NEEDS_MEMMOVABLE_MEMBERS \ |
| 897 |
__attribute__((annotate("moz_needs_memmovable_members"))) |
| 898 |
# define MOZ_NO_DANGLING_ON_TEMPORARIES \ |
| 899 |
__attribute__((annotate("moz_no_dangling_on_temporaries"))) |
| 900 |
# define MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS \ |
| 901 |
__attribute__(( \ |
| 902 |
annotate("moz_inherit_type_annotations_from_template_args"))) |
| 903 |
# define MOZ_NON_AUTOABLE __attribute__((annotate("moz_non_autoable"))) |
| 904 |
# define MOZ_INIT_OUTSIDE_CTOR |
| 905 |
# define MOZ_IS_CLASS_INIT |
| 906 |
# define MOZ_NON_PARAM __attribute__((annotate("moz_non_param"))) |
| 907 |
# define MOZ_REQUIRED_BASE_METHOD \ |
| 908 |
__attribute__((annotate("moz_required_base_method"))) |
| 909 |
# define MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG \ |
| 910 |
__attribute__((annotate("moz_must_return_from_caller_if_this_is_arg"))) |
| 911 |
# define MOZ_MAY_CALL_AFTER_MUST_RETURN \ |
| 912 |
__attribute__((annotate("moz_may_call_after_must_return"))) |
| 913 |
# define MOZ_KNOWN_LIVE __attribute__((annotate("moz_known_live"))) |
| 914 |
# ifdef MOZ_CLANG_PLUGIN |
| 915 |
# define MOZ_UNANNOTATED __attribute__((annotate("moz_unannotated"))) |
| 916 |
# define MOZ_ANNOTATED __attribute__((annotate("moz_annotated"))) |
| 917 |
# define MOZ_RUNINIT __attribute__((annotate("moz_global_var"))) |
| 918 |
# define MOZ_GLOBINIT \ |
| 919 |
MOZ_RUNINIT __attribute__((annotate("moz_generated"))) |
| 920 |
# else |
| 921 |
# define MOZ_UNANNOTATED /* nothing */ |
| 922 |
# define MOZ_ANNOTATED /* nothing */ |
| 923 |
# define MOZ_RUNINIT /* nothing */ |
| 924 |
# define MOZ_GLOBINIT /* nothing */ |
| 925 |
# endif |
| 926 |
|
| 927 |
/* |
| 928 |
* It turns out that clang doesn't like void func() __attribute__ {} without a |
| 929 |
* warning, so use pragmas to disable the warning. |
| 930 |
*/ |
| 931 |
# ifdef __clang__ |
| 932 |
# define MOZ_HEAP_ALLOCATOR \ |
| 933 |
_Pragma("clang diagnostic push") \ |
| 934 |
_Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ |
| 935 |
__attribute__((annotate("moz_heap_allocator"))) _Pragma( \ |
| 936 |
"clang diagnostic pop") |
| 937 |
# else |
| 938 |
# define MOZ_HEAP_ALLOCATOR __attribute__((annotate("moz_heap_allocator"))) |
| 939 |
# endif |
| 940 |
# else |
| 941 |
# define MOZ_CAN_RUN_SCRIPT /* nothing */ |
| 942 |
# define MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION /* nothing */ |
| 943 |
# define MOZ_CAN_RUN_SCRIPT_BOUNDARY /* nothing */ |
| 944 |
# define MOZ_MUST_OVERRIDE /* nothing */ |
| 945 |
# define MOZ_STATIC_CLASS /* nothing */ |
| 946 |
# define MOZ_RUNINIT /* nothing */ |
| 947 |
# define MOZ_GLOBINIT /* nothing */ |
| 948 |
# define MOZ_STATIC_LOCAL_CLASS /* nothing */ |
| 949 |
# define MOZ_STACK_CLASS /* nothing */ |
| 950 |
# define MOZ_NONHEAP_CLASS /* nothing */ |
| 951 |
# define MOZ_HEAP_CLASS /* nothing */ |
| 952 |
# define MOZ_NON_TEMPORARY_CLASS /* nothing */ |
| 953 |
# define MOZ_TEMPORARY_CLASS /* nothing */ |
| 954 |
# define MOZ_TRIVIAL_CTOR_DTOR /* nothing */ |
| 955 |
# define MOZ_ALLOW_TEMPORARY /* nothing */ |
| 956 |
# define MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS /* nothing */ |
| 957 |
# define MOZ_IMPLICIT /* nothing */ |
| 958 |
# define MOZ_IS_SMARTPTR_TO_REFCOUNTED /* nothing */ |
| 959 |
# define MOZ_IS_REFPTR /* nothing */ |
| 960 |
# define MOZ_NO_ARITHMETIC_EXPR_IN_ARGUMENT /* nothing */ |
| 961 |
# define MOZ_HEAP_ALLOCATOR /* nothing */ |
| 962 |
# define MOZ_OWNING_REF /* nothing */ |
| 963 |
# define MOZ_NON_OWNING_REF /* nothing */ |
| 964 |
# define MOZ_UNSAFE_REF(reason) /* nothing */ |
| 965 |
# define MOZ_NO_ADDREF_RELEASE_ON_RETURN /* nothing */ |
| 966 |
# define MOZ_NEEDS_NO_VTABLE_TYPE /* nothing */ |
| 967 |
# define MOZ_NON_MEMMOVABLE /* nothing */ |
| 968 |
# define MOZ_NEEDS_MEMMOVABLE_TYPE /* nothing */ |
| 969 |
# define MOZ_NEEDS_MEMMOVABLE_MEMBERS /* nothing */ |
| 970 |
# define MOZ_NO_DANGLING_ON_TEMPORARIES /* nothing */ |
| 971 |
# define MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS /* nothing */ |
| 972 |
# define MOZ_INIT_OUTSIDE_CTOR /* nothing */ |
| 973 |
# define MOZ_IS_CLASS_INIT /* nothing */ |
| 974 |
# define MOZ_NON_PARAM /* nothing */ |
| 975 |
# define MOZ_NON_AUTOABLE /* nothing */ |
| 976 |
# define MOZ_REQUIRED_BASE_METHOD /* nothing */ |
| 977 |
# define MOZ_MUST_RETURN_FROM_CALLER_IF_THIS_IS_ARG /* nothing */ |
| 978 |
# define MOZ_MAY_CALL_AFTER_MUST_RETURN /* nothing */ |
| 979 |
# define MOZ_KNOWN_LIVE /* nothing */ |
| 980 |
# define MOZ_UNANNOTATED /* nothing */ |
| 981 |
# define MOZ_ANNOTATED /* nothing */ |
| 982 |
# endif /* defined(MOZ_CLANG_PLUGIN) || defined(XGILL_PLUGIN) */ |
| 983 |
|
| 984 |
# define MOZ_RAII MOZ_NON_TEMPORARY_CLASS MOZ_STACK_CLASS |
| 985 |
|
| 986 |
// XGILL_PLUGIN is used for the GC rooting hazard analysis, which compiles with |
| 987 |
// gcc. gcc has different rules governing __attribute__((...)) placement, so |
| 988 |
// some attributes will error out when used in the source code where clang |
| 989 |
// expects them to be. Remove the problematic annotations when needed. |
| 990 |
// |
| 991 |
// The placement of c++11 [[...]] attributes is more flexible and defined by a |
| 992 |
// spec, so it would be nice to switch to those for the problematic |
| 993 |
// cases. Unfortunately, the official spec provides *no* way to annotate a |
| 994 |
// lambda function, which is one source of the difficulty here. It appears that |
| 995 |
// this will be fixed in c++23: https://github.com/cplusplus/papers/issues/882 |
| 996 |
|
| 997 |
# ifdef XGILL_PLUGIN |
| 998 |
|
| 999 |
# undef MOZ_MUST_OVERRIDE |
| 1000 |
# undef MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION |
| 1001 |
# undef MOZ_CAN_RUN_SCRIPT |
| 1002 |
# undef MOZ_CAN_RUN_SCRIPT_BOUNDARY |
| 1003 |
# define MOZ_MUST_OVERRIDE /* nothing */ |
| 1004 |
# define MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION /* nothing */ |
| 1005 |
# define MOZ_CAN_RUN_SCRIPT /* nothing */ |
| 1006 |
# define MOZ_CAN_RUN_SCRIPT_BOUNDARY /* nothing */ |
| 1007 |
|
| 1008 |
# endif |
| 1009 |
|
| 1010 |
#endif /* __cplusplus */ |
| 1011 |
|
| 1012 |
/** |
| 1013 |
* Printf style formats. MOZ_FORMAT_PRINTF and MOZ_FORMAT_WPRINTF can be used |
| 1014 |
* to annotate a function or method that is "printf/wprintf-like"; this will let |
| 1015 |
* (some) compilers check that the arguments match the template string. |
| 1016 |
* |
| 1017 |
* This macro takes two arguments. The first argument is the argument |
| 1018 |
* number of the template string. The second argument is the argument |
| 1019 |
* number of the '...' argument holding the arguments. |
| 1020 |
* |
| 1021 |
* Argument numbers start at 1. Note that the implicit "this" |
| 1022 |
* argument of a non-static member function counts as an argument. |
| 1023 |
* |
| 1024 |
* So, for a simple case like: |
| 1025 |
* void print_something (int whatever, const char *fmt, ...); |
| 1026 |
* The corresponding annotation would be |
| 1027 |
* MOZ_FORMAT_PRINTF(2, 3) |
| 1028 |
* However, if "print_something" were a non-static member function, |
| 1029 |
* then the annotation would be: |
| 1030 |
* MOZ_FORMAT_PRINTF(3, 4) |
| 1031 |
* |
| 1032 |
* The second argument should be 0 for vprintf-like functions; that |
| 1033 |
* is, those taking a va_list argument. |
| 1034 |
* |
| 1035 |
* Note that the checking is limited to standards-conforming |
| 1036 |
* printf-likes, and in particular this should not be used for |
| 1037 |
* PR_snprintf and friends, which are "printf-like" but which assign |
| 1038 |
* different meanings to the various formats. |
| 1039 |
* |
| 1040 |
* MinGW requires special handling due to different format specifiers |
| 1041 |
* on different platforms. The macro __MINGW_PRINTF_FORMAT maps to |
| 1042 |
* either gnu_printf or ms_printf depending on where we are compiling |
| 1043 |
* to avoid warnings on format specifiers that are legal. |
| 1044 |
* |
| 1045 |
* At time of writing MinGW has no wide equivalent to __MINGW_PRINTF_FORMAT; |
| 1046 |
* therefore __MINGW_WPRINTF_FORMAT has been implemented following the same |
| 1047 |
* pattern seen in MinGW's source. |
| 1048 |
*/ |
| 1049 |
#ifdef __MINGW32__ |
| 1050 |
# define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck) \ |
| 1051 |
__attribute__((format(__MINGW_PRINTF_FORMAT, stringIndex, firstToCheck))) |
| 1052 |
# ifndef __MINGW_WPRINTF_FORMAT |
| 1053 |
# if defined(__clang__) |
| 1054 |
# define __MINGW_WPRINTF_FORMAT wprintf |
| 1055 |
# elif defined(_UCRT) || __USE_MINGW_ANSI_STDIO |
| 1056 |
# define __MINGW_WPRINTF_FORMAT gnu_wprintf |
| 1057 |
# else |
| 1058 |
# define __MINGW_WPRINTF_FORMAT ms_wprintf |
| 1059 |
# endif |
| 1060 |
# endif |
| 1061 |
# define MOZ_FORMAT_WPRINTF(stringIndex, firstToCheck) \ |
| 1062 |
__attribute__((format(__MINGW_WPRINTF_FORMAT, stringIndex, firstToCheck))) |
| 1063 |
#elif __GNUC__ || __clang__ |
| 1064 |
# define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck) \ |
| 1065 |
__attribute__((format(printf, stringIndex, firstToCheck))) |
| 1066 |
# define MOZ_FORMAT_WPRINTF(stringIndex, firstToCheck) \ |
| 1067 |
__attribute__((format(wprintf, stringIndex, firstToCheck))) |
| 1068 |
#else |
| 1069 |
# define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck) |
| 1070 |
# define MOZ_FORMAT_WPRINTF(stringIndex, firstToCheck) |
| 1071 |
#endif |
| 1072 |
|
| 1073 |
/** |
| 1074 |
* To manually declare an XPCOM ABI-compatible virtual function, the following |
| 1075 |
* macros can be used to handle the non-standard ABI used on Windows for COM |
| 1076 |
* compatibility. E.g.: |
| 1077 |
* |
| 1078 |
* virtual ReturnType MOZ_XPCOM_ABI foo(); |
| 1079 |
*/ |
| 1080 |
#if defined(XP_WIN) |
| 1081 |
# define MOZ_XPCOM_ABI __stdcall |
| 1082 |
#else |
| 1083 |
# define MOZ_XPCOM_ABI |
| 1084 |
#endif |
| 1085 |
|
| 1086 |
/** |
| 1087 |
* MSVC / clang-cl don't optimize empty bases correctly unless we explicitly |
| 1088 |
* tell it to, see: |
| 1089 |
* |
| 1090 |
* https://stackoverflow.com/questions/12701469/why-is-the-empty-base-class-optimization-ebo-is-not-working-in-msvc |
| 1091 |
* https://devblogs.microsoft.com/cppblog/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/ |
| 1092 |
*/ |
| 1093 |
#if defined(_MSC_VER) |
| 1094 |
# define MOZ_EMPTY_BASES __declspec(empty_bases) |
| 1095 |
#else |
| 1096 |
# define MOZ_EMPTY_BASES |
| 1097 |
#endif |
| 1098 |
|
| 1099 |
/** |
| 1100 |
* Pre- C++20 equivalent to constinit |
| 1101 |
*/ |
| 1102 |
#if defined(__cpp_constinit) |
| 1103 |
# define MOZ_CONSTINIT constinit |
| 1104 |
#elif defined(__clang__) |
| 1105 |
# define MOZ_CONSTINIT [[clang::require_constant_initialization]] |
| 1106 |
#elif MOZ_GCC_VERSION_AT_LEAST(10, 1, 0) |
| 1107 |
# define MOZ_CONSTINIT __constinit |
| 1108 |
#else |
| 1109 |
# define MOZ_CONSTINIT |
| 1110 |
#endif |
| 1111 |
|
| 1112 |
// XXX: GCC somehow does not allow attributes before lambda return types, while |
| 1113 |
// clang requires so. See also bug 1627007. |
| 1114 |
#ifdef __clang__ |
| 1115 |
# define MOZ_CAN_RUN_SCRIPT_BOUNDARY_LAMBDA MOZ_CAN_RUN_SCRIPT_BOUNDARY |
| 1116 |
#else |
| 1117 |
# define MOZ_CAN_RUN_SCRIPT_BOUNDARY_LAMBDA |
| 1118 |
#endif |
| 1119 |
|
| 1120 |
#endif /* mozilla_Attributes_h */ |