Assertions.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. /* Implementations of runtime and static assertion macros for C and C++. */
  7. #ifndef mozilla_Assertions_h
  8. #define mozilla_Assertions_h
  9. #if defined(MOZILLA_INTERNAL_API) && defined(__cplusplus)
  10. #define MOZ_DUMP_ASSERTION_STACK
  11. #endif
  12. #include "mozilla/Attributes.h"
  13. #include "mozilla/Compiler.h"
  14. #include "mozilla/Likely.h"
  15. #include "mozilla/MacroArgs.h"
  16. #include "mozilla/StaticAnalysisFunctions.h"
  17. #include "mozilla/Types.h"
  18. #ifdef MOZ_DUMP_ASSERTION_STACK
  19. #include "nsTraceRefcnt.h"
  20. #endif
  21. #if defined(MOZ_HAS_MOZGLUE) || defined(MOZILLA_INTERNAL_API)
  22. /*
  23. * The crash reason set by MOZ_CRASH_ANNOTATE is consumed by the crash reporter
  24. * if present. It is declared here (and defined in Assertions.cpp) to make it
  25. * available to all code, even libraries that don't link with the crash reporter
  26. * directly.
  27. */
  28. MOZ_BEGIN_EXTERN_C
  29. extern MFBT_DATA const char* gMozCrashReason;
  30. MOZ_END_EXTERN_C
  31. static inline void
  32. AnnotateMozCrashReason(const char* reason)
  33. {
  34. gMozCrashReason = reason;
  35. }
  36. # define MOZ_CRASH_ANNOTATE(...) AnnotateMozCrashReason(__VA_ARGS__)
  37. #else
  38. # define MOZ_CRASH_ANNOTATE(...) do { /* nothing */ } while (0)
  39. #endif
  40. #include <stddef.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #ifdef WIN32
  44. /*
  45. * TerminateProcess and GetCurrentProcess are defined in <winbase.h>, which
  46. * further depends on <windef.h>. We hardcode these few definitions manually
  47. * because those headers clutter the global namespace with a significant
  48. * number of undesired macros and symbols.
  49. */
  50. # ifdef __cplusplus
  51. extern "C" {
  52. # endif
  53. __declspec(dllimport) int __stdcall
  54. TerminateProcess(void* hProcess, unsigned int uExitCode);
  55. __declspec(dllimport) void* __stdcall GetCurrentProcess(void);
  56. # ifdef __cplusplus
  57. }
  58. # endif
  59. #else
  60. # include <signal.h>
  61. #endif
  62. #ifdef ANDROID
  63. # include <android/log.h>
  64. #endif
  65. /*
  66. * MOZ_STATIC_ASSERT may be used to assert a condition *at compile time* in C.
  67. * In C++11, static_assert is provided by the compiler to the same effect.
  68. * This can be useful when you make certain assumptions about what must hold for
  69. * optimal, or even correct, behavior. For example, you might assert that the
  70. * size of a struct is a multiple of the target architecture's word size:
  71. *
  72. * struct S { ... };
  73. * // C
  74. * MOZ_STATIC_ASSERT(sizeof(S) % sizeof(size_t) == 0,
  75. * "S should be a multiple of word size for efficiency");
  76. * // C++11
  77. * static_assert(sizeof(S) % sizeof(size_t) == 0,
  78. * "S should be a multiple of word size for efficiency");
  79. *
  80. * This macro can be used in any location where both an extern declaration and a
  81. * typedef could be used.
  82. */
  83. #ifndef __cplusplus
  84. /*
  85. * Some of the definitions below create an otherwise-unused typedef. This
  86. * triggers compiler warnings with some versions of gcc, so mark the typedefs
  87. * as permissibly-unused to disable the warnings.
  88. */
  89. # if defined(__GNUC__)
  90. # define MOZ_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
  91. # else
  92. # define MOZ_STATIC_ASSERT_UNUSED_ATTRIBUTE /* nothing */
  93. # endif
  94. # define MOZ_STATIC_ASSERT_GLUE1(x, y) x##y
  95. # define MOZ_STATIC_ASSERT_GLUE(x, y) MOZ_STATIC_ASSERT_GLUE1(x, y)
  96. # if defined(__SUNPRO_CC)
  97. /*
  98. * The Sun Studio C++ compiler is buggy when declaring, inside a function,
  99. * another extern'd function with an array argument whose length contains a
  100. * sizeof, triggering the error message "sizeof expression not accepted as
  101. * size of array parameter". This bug (6688515, not public yet) would hit
  102. * defining moz_static_assert as a function, so we always define an extern
  103. * array for Sun Studio.
  104. *
  105. * We include the line number in the symbol name in a best-effort attempt
  106. * to avoid conflicts (see below).
  107. */
  108. # define MOZ_STATIC_ASSERT(cond, reason) \
  109. extern char MOZ_STATIC_ASSERT_GLUE(moz_static_assert, __LINE__)[(cond) ? 1 : -1]
  110. # elif defined(__COUNTER__)
  111. /*
  112. * If there was no preferred alternative, use a compiler-agnostic version.
  113. *
  114. * Note that the non-__COUNTER__ version has a bug in C++: it can't be used
  115. * in both |extern "C"| and normal C++ in the same translation unit. (Alas
  116. * |extern "C"| isn't allowed in a function.) The only affected compiler
  117. * we really care about is gcc 4.2. For that compiler and others like it,
  118. * we include the line number in the function name to do the best we can to
  119. * avoid conflicts. These should be rare: a conflict would require use of
  120. * MOZ_STATIC_ASSERT on the same line in separate files in the same
  121. * translation unit, *and* the uses would have to be in code with
  122. * different linkage, *and* the first observed use must be in C++-linkage
  123. * code.
  124. */
  125. # define MOZ_STATIC_ASSERT(cond, reason) \
  126. typedef int MOZ_STATIC_ASSERT_GLUE(moz_static_assert, __COUNTER__)[(cond) ? 1 : -1] MOZ_STATIC_ASSERT_UNUSED_ATTRIBUTE
  127. # else
  128. # define MOZ_STATIC_ASSERT(cond, reason) \
  129. extern void MOZ_STATIC_ASSERT_GLUE(moz_static_assert, __LINE__)(int arg[(cond) ? 1 : -1]) MOZ_STATIC_ASSERT_UNUSED_ATTRIBUTE
  130. # endif
  131. #define MOZ_STATIC_ASSERT_IF(cond, expr, reason) MOZ_STATIC_ASSERT(!(cond) || (expr), reason)
  132. #else
  133. #define MOZ_STATIC_ASSERT_IF(cond, expr, reason) static_assert(!(cond) || (expr), reason)
  134. #endif
  135. #ifdef __cplusplus
  136. extern "C" {
  137. #endif
  138. /*
  139. * Prints |aStr| as an assertion failure (using aFilename and aLine as the
  140. * location of the assertion) to the standard debug-output channel.
  141. *
  142. * Usually you should use MOZ_ASSERT or MOZ_CRASH instead of this method. This
  143. * method is primarily for internal use in this header, and only secondarily
  144. * for use in implementing release-build assertions.
  145. */
  146. static MOZ_COLD MOZ_ALWAYS_INLINE void
  147. MOZ_ReportAssertionFailure(const char* aStr, const char* aFilename, int aLine)
  148. MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS
  149. {
  150. #ifdef ANDROID
  151. __android_log_print(ANDROID_LOG_FATAL, "MOZ_Assert",
  152. "Assertion failure: %s, at %s:%d\n",
  153. aStr, aFilename, aLine);
  154. #else
  155. fprintf(stderr, "Assertion failure: %s, at %s:%d\n", aStr, aFilename, aLine);
  156. #if defined (MOZ_DUMP_ASSERTION_STACK)
  157. nsTraceRefcnt::WalkTheStack(stderr);
  158. #endif
  159. fflush(stderr);
  160. #endif
  161. }
  162. static MOZ_COLD MOZ_ALWAYS_INLINE void
  163. MOZ_ReportCrash(const char* aStr, const char* aFilename, int aLine)
  164. MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS
  165. {
  166. #ifdef ANDROID
  167. __android_log_print(ANDROID_LOG_FATAL, "MOZ_CRASH",
  168. "Hit MOZ_CRASH(%s) at %s:%d\n", aStr, aFilename, aLine);
  169. #else
  170. fprintf(stderr, "Hit MOZ_CRASH(%s) at %s:%d\n", aStr, aFilename, aLine);
  171. #if defined(MOZ_DUMP_ASSERTION_STACK)
  172. nsTraceRefcnt::WalkTheStack(stderr);
  173. #endif
  174. fflush(stderr);
  175. #endif
  176. }
  177. /**
  178. * MOZ_REALLY_CRASH is used in the implementation of MOZ_CRASH(). You should
  179. * call MOZ_CRASH instead.
  180. */
  181. #if defined(_MSC_VER)
  182. /*
  183. * On MSVC use the __debugbreak compiler intrinsic, which produces an inline
  184. * (not nested in a system function) breakpoint. This distinctively invokes
  185. * Breakpad without requiring system library symbols on all stack-processing
  186. * machines, as a nested breakpoint would require.
  187. *
  188. * We use TerminateProcess with the exit code aborting would generate
  189. * because we don't want to invoke atexit handlers, destructors, library
  190. * unload handlers, and so on when our process might be in a compromised
  191. * state.
  192. *
  193. * We don't use abort() because it'd cause Windows to annoyingly pop up the
  194. * process error dialog multiple times. See bug 345118 and bug 426163.
  195. *
  196. * We follow TerminateProcess() with a call to MOZ_NoReturn() so that the
  197. * compiler doesn't hassle us to provide a return statement after a
  198. * MOZ_REALLY_CRASH() call.
  199. *
  200. * (Technically these are Windows requirements, not MSVC requirements. But
  201. * practically you need MSVC for debugging, and we only ship builds created
  202. * by MSVC, so doing it this way reduces complexity.)
  203. */
  204. __declspec(noreturn) __inline void MOZ_NoReturn() {}
  205. # ifdef __cplusplus
  206. # define MOZ_REALLY_CRASH() \
  207. do { \
  208. ::__debugbreak(); \
  209. *((volatile int*) NULL) = __LINE__; \
  210. ::TerminateProcess(::GetCurrentProcess(), 3); \
  211. ::MOZ_NoReturn(); \
  212. } while (0)
  213. # else
  214. # define MOZ_REALLY_CRASH() \
  215. do { \
  216. __debugbreak(); \
  217. *((volatile int*) NULL) = __LINE__; \
  218. TerminateProcess(GetCurrentProcess(), 3); \
  219. MOZ_NoReturn(); \
  220. } while (0)
  221. # endif
  222. #else
  223. # ifdef __cplusplus
  224. # define MOZ_REALLY_CRASH() \
  225. do { \
  226. *((volatile int*) NULL) = __LINE__; \
  227. ::abort(); \
  228. } while (0)
  229. # else
  230. # define MOZ_REALLY_CRASH() \
  231. do { \
  232. *((volatile int*) NULL) = __LINE__; \
  233. abort(); \
  234. } while (0)
  235. # endif
  236. #endif
  237. /*
  238. * MOZ_CRASH([explanation-string]) crashes the program, plain and simple, in a
  239. * Breakpad-compatible way, in both debug and release builds.
  240. *
  241. * MOZ_CRASH is a good solution for "handling" failure cases when you're
  242. * unwilling or unable to handle them more cleanly -- for OOM, for likely memory
  243. * corruption, and so on. It's also a good solution if you need safe behavior
  244. * in release builds as well as debug builds. But if the failure is one that
  245. * should be debugged and fixed, MOZ_ASSERT is generally preferable.
  246. *
  247. * The optional explanation-string, if provided, must be a string literal
  248. * explaining why we're crashing. This argument is intended for use with
  249. * MOZ_CRASH() calls whose rationale is non-obvious; don't use it if it's
  250. * obvious why we're crashing.
  251. *
  252. * If we're a DEBUG build and we crash at a MOZ_CRASH which provides an
  253. * explanation-string, we print the string to stderr. Otherwise, we don't
  254. * print anything; this is because we want MOZ_CRASH to be 100% safe in release
  255. * builds, and it's hard to print to stderr safely when memory might have been
  256. * corrupted.
  257. */
  258. #ifndef DEBUG
  259. # define MOZ_CRASH(...) \
  260. do { \
  261. MOZ_CRASH_ANNOTATE("MOZ_CRASH(" __VA_ARGS__ ")"); \
  262. MOZ_REALLY_CRASH(); \
  263. } while (0)
  264. #else
  265. # define MOZ_CRASH(...) \
  266. do { \
  267. MOZ_ReportCrash("" __VA_ARGS__, __FILE__, __LINE__); \
  268. MOZ_CRASH_ANNOTATE("MOZ_CRASH(" __VA_ARGS__ ")"); \
  269. MOZ_REALLY_CRASH(); \
  270. } while (0)
  271. #endif
  272. #ifdef __cplusplus
  273. } /* extern "C" */
  274. #endif
  275. /*
  276. * MOZ_ASSERT(expr [, explanation-string]) asserts that |expr| must be truthy in
  277. * debug builds. If it is, execution continues. Otherwise, an error message
  278. * including the expression and the explanation-string (if provided) is printed,
  279. * an attempt is made to invoke any existing debugger, and execution halts.
  280. * MOZ_ASSERT is fatal: no recovery is possible. Do not assert a condition
  281. * which can correctly be falsy.
  282. *
  283. * The optional explanation-string, if provided, must be a string literal
  284. * explaining the assertion. It is intended for use with assertions whose
  285. * correctness or rationale is non-obvious, and for assertions where the "real"
  286. * condition being tested is best described prosaically. Don't provide an
  287. * explanation if it's not actually helpful.
  288. *
  289. * // No explanation needed: pointer arguments often must not be NULL.
  290. * MOZ_ASSERT(arg);
  291. *
  292. * // An explanation can be helpful to explain exactly how we know an
  293. * // assertion is valid.
  294. * MOZ_ASSERT(state == WAITING_FOR_RESPONSE,
  295. * "given that <thingA> and <thingB>, we must have...");
  296. *
  297. * // Or it might disambiguate multiple identical (save for their location)
  298. * // assertions of the same expression.
  299. * MOZ_ASSERT(getSlot(PRIMITIVE_THIS_SLOT).isUndefined(),
  300. * "we already set [[PrimitiveThis]] for this Boolean object");
  301. * MOZ_ASSERT(getSlot(PRIMITIVE_THIS_SLOT).isUndefined(),
  302. * "we already set [[PrimitiveThis]] for this String object");
  303. *
  304. * MOZ_ASSERT has no effect in non-debug builds. It is designed to catch bugs
  305. * *only* during debugging, not "in the field". If you want the latter, use
  306. * MOZ_RELEASE_ASSERT, which applies to non-debug builds as well.
  307. *
  308. * MOZ_DIAGNOSTIC_ASSERT works like MOZ_RELEASE_ASSERT in Nightly/Aurora and
  309. * MOZ_ASSERT in Beta/Release - use this when a condition is potentially rare
  310. * enough to require real user testing to hit, but is not security-sensitive.
  311. * This can cause user pain, so use it sparingly. If a MOZ_DIAGNOSTIC_ASSERT
  312. * is firing, it should promptly be converted to a MOZ_ASSERT while the failure
  313. * is being investigated, rather than letting users suffer.
  314. */
  315. /*
  316. * Implement MOZ_VALIDATE_ASSERT_CONDITION_TYPE, which is used to guard against
  317. * accidentally passing something unintended in lieu of an assertion condition.
  318. */
  319. #ifdef __cplusplus
  320. # include "mozilla/TypeTraits.h"
  321. namespace mozilla {
  322. namespace detail {
  323. template<typename T>
  324. struct AssertionConditionType
  325. {
  326. typedef typename RemoveReference<T>::Type ValueT;
  327. static_assert(!IsArray<ValueT>::value,
  328. "Expected boolean assertion condition, got an array or a "
  329. "string!");
  330. static_assert(!IsFunction<ValueT>::value,
  331. "Expected boolean assertion condition, got a function! Did "
  332. "you intend to call that function?");
  333. static_assert(!IsFloatingPoint<ValueT>::value,
  334. "It's often a bad idea to assert that a floating-point number "
  335. "is nonzero, because such assertions tend to intermittently "
  336. "fail. Shouldn't your code gracefully handle this case instead "
  337. "of asserting? Anyway, if you really want to do that, write an "
  338. "explicit boolean condition, like !!x or x!=0.");
  339. static const bool isValid = true;
  340. };
  341. } // namespace detail
  342. } // namespace mozilla
  343. # define MOZ_VALIDATE_ASSERT_CONDITION_TYPE(x) \
  344. static_assert(mozilla::detail::AssertionConditionType<decltype(x)>::isValid, \
  345. "invalid assertion condition")
  346. #else
  347. # define MOZ_VALIDATE_ASSERT_CONDITION_TYPE(x)
  348. #endif
  349. /* First the single-argument form. */
  350. #define MOZ_ASSERT_HELPER1(expr) \
  351. do { \
  352. MOZ_VALIDATE_ASSERT_CONDITION_TYPE(expr); \
  353. if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
  354. MOZ_ReportAssertionFailure(#expr, __FILE__, __LINE__); \
  355. MOZ_CRASH_ANNOTATE("MOZ_RELEASE_ASSERT(" #expr ")"); \
  356. MOZ_REALLY_CRASH(); \
  357. } \
  358. } while (0)
  359. /* Now the two-argument form. */
  360. #define MOZ_ASSERT_HELPER2(expr, explain) \
  361. do { \
  362. MOZ_VALIDATE_ASSERT_CONDITION_TYPE(expr); \
  363. if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
  364. MOZ_ReportAssertionFailure(#expr " (" explain ")", __FILE__, __LINE__); \
  365. MOZ_CRASH_ANNOTATE("MOZ_RELEASE_ASSERT(" #expr ") (" explain ")"); \
  366. MOZ_REALLY_CRASH(); \
  367. } \
  368. } while (0)
  369. #define MOZ_RELEASE_ASSERT_GLUE(a, b) a b
  370. #define MOZ_RELEASE_ASSERT(...) \
  371. MOZ_RELEASE_ASSERT_GLUE( \
  372. MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_ASSERT_HELPER, __VA_ARGS__), \
  373. (__VA_ARGS__))
  374. #ifdef DEBUG
  375. # define MOZ_ASSERT(...) MOZ_RELEASE_ASSERT(__VA_ARGS__)
  376. #else
  377. # define MOZ_ASSERT(...) do { } while (0)
  378. #endif /* DEBUG */
  379. #ifdef RELEASE_OR_BETA
  380. # define MOZ_DIAGNOSTIC_ASSERT MOZ_ASSERT
  381. #else
  382. # define MOZ_DIAGNOSTIC_ASSERT MOZ_RELEASE_ASSERT
  383. #endif
  384. /*
  385. * MOZ_ASSERT_IF(cond1, cond2) is equivalent to MOZ_ASSERT(cond2) if cond1 is
  386. * true.
  387. *
  388. * MOZ_ASSERT_IF(isPrime(num), num == 2 || isOdd(num));
  389. *
  390. * As with MOZ_ASSERT, MOZ_ASSERT_IF has effect only in debug builds. It is
  391. * designed to catch bugs during debugging, not "in the field".
  392. */
  393. #ifdef DEBUG
  394. # define MOZ_ASSERT_IF(cond, expr) \
  395. do { \
  396. if (cond) { \
  397. MOZ_ASSERT(expr); \
  398. } \
  399. } while (0)
  400. #else
  401. # define MOZ_ASSERT_IF(cond, expr) do { } while (0)
  402. #endif
  403. /*
  404. * MOZ_ASSUME_UNREACHABLE_MARKER() expands to an expression which states that
  405. * it is undefined behavior for execution to reach this point. No guarantees
  406. * are made about what will happen if this is reached at runtime. Most code
  407. * should use MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE because it has extra
  408. * asserts.
  409. */
  410. #if defined(__clang__) || defined(__GNUC__)
  411. # define MOZ_ASSUME_UNREACHABLE_MARKER() __builtin_unreachable()
  412. #elif defined(_MSC_VER)
  413. # define MOZ_ASSUME_UNREACHABLE_MARKER() __assume(0)
  414. #else
  415. # ifdef __cplusplus
  416. # define MOZ_ASSUME_UNREACHABLE_MARKER() ::abort()
  417. # else
  418. # define MOZ_ASSUME_UNREACHABLE_MARKER() abort()
  419. # endif
  420. #endif
  421. /*
  422. * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE([reason]) tells the compiler that it
  423. * can assume that the macro call cannot be reached during execution. This lets
  424. * the compiler generate better-optimized code under some circumstances, at the
  425. * expense of the program's behavior being undefined if control reaches the
  426. * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE.
  427. *
  428. * In Gecko, you probably should not use this macro outside of performance- or
  429. * size-critical code, because it's unsafe. If you don't care about code size
  430. * or performance, you should probably use MOZ_ASSERT or MOZ_CRASH.
  431. *
  432. * SpiderMonkey is a different beast, and there it's acceptable to use
  433. * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE more widely.
  434. *
  435. * Note that MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE is noreturn, so it's valid
  436. * not to return a value following a MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE
  437. * call.
  438. *
  439. * Example usage:
  440. *
  441. * enum ValueType {
  442. * VALUE_STRING,
  443. * VALUE_INT,
  444. * VALUE_FLOAT
  445. * };
  446. *
  447. * int ptrToInt(ValueType type, void* value) {
  448. * {
  449. * // We know for sure that type is either INT or FLOAT, and we want this
  450. * // code to run as quickly as possible.
  451. * switch (type) {
  452. * case VALUE_INT:
  453. * return *(int*) value;
  454. * case VALUE_FLOAT:
  455. * return (int) *(float*) value;
  456. * default:
  457. * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Unexpected ValueType");
  458. * }
  459. * }
  460. */
  461. /*
  462. * Unconditional assert in debug builds for (assumed) unreachable code paths
  463. * that have a safe return without crashing in release builds.
  464. */
  465. #define MOZ_ASSERT_UNREACHABLE(reason) \
  466. MOZ_ASSERT(false, "MOZ_ASSERT_UNREACHABLE: " reason)
  467. #define MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE(reason) \
  468. do { \
  469. MOZ_ASSERT_UNREACHABLE(reason); \
  470. MOZ_ASSUME_UNREACHABLE_MARKER(); \
  471. } while (0)
  472. /**
  473. * MOZ_FALLTHROUGH_ASSERT is an annotation to suppress compiler warnings about
  474. * switch cases that MOZ_ASSERT(false) (or its alias MOZ_ASSERT_UNREACHABLE) in
  475. * debug builds, but intentionally fall through in release builds to handle
  476. * unexpected values.
  477. *
  478. * Why do we need MOZ_FALLTHROUGH_ASSERT in addition to MOZ_FALLTHROUGH? In
  479. * release builds, the MOZ_ASSERT(false) will expand to `do { } while (0)`,
  480. * requiring a MOZ_FALLTHROUGH annotation to suppress a -Wimplicit-fallthrough
  481. * warning. In debug builds, the MOZ_ASSERT(false) will expand to something like
  482. * `if (true) { MOZ_CRASH(); }` and the MOZ_FALLTHROUGH annotation will cause
  483. * a -Wunreachable-code warning. The MOZ_FALLTHROUGH_ASSERT macro breaks this
  484. * warning stalemate.
  485. *
  486. * // Example before MOZ_FALLTHROUGH_ASSERT:
  487. * switch (foo) {
  488. * default:
  489. * // This case wants to assert in debug builds, fall through in release.
  490. * MOZ_ASSERT(false); // -Wimplicit-fallthrough warning in release builds!
  491. * MOZ_FALLTHROUGH; // but -Wunreachable-code warning in debug builds!
  492. * case 5:
  493. * return 5;
  494. * }
  495. *
  496. * // Example with MOZ_FALLTHROUGH_ASSERT:
  497. * switch (foo) {
  498. * default:
  499. * // This case asserts in debug builds, falls through in release.
  500. * MOZ_FALLTHROUGH_ASSERT("Unexpected foo value?!");
  501. * case 5:
  502. * return 5;
  503. * }
  504. */
  505. #ifdef DEBUG
  506. # define MOZ_FALLTHROUGH_ASSERT(reason) MOZ_CRASH("MOZ_FALLTHROUGH_ASSERT: " reason)
  507. #else
  508. # define MOZ_FALLTHROUGH_ASSERT(...) MOZ_FALLTHROUGH
  509. #endif
  510. /*
  511. * MOZ_ALWAYS_TRUE(expr) and MOZ_ALWAYS_FALSE(expr) always evaluate the provided
  512. * expression, in debug builds and in release builds both. Then, in debug
  513. * builds only, the value of the expression is asserted either true or false
  514. * using MOZ_ASSERT.
  515. */
  516. #ifdef DEBUG
  517. # define MOZ_ALWAYS_TRUE(expr) \
  518. do { \
  519. if ((expr)) { \
  520. /* Do nothing. */ \
  521. } else { \
  522. MOZ_ASSERT(false, #expr); \
  523. } \
  524. } while (0)
  525. # define MOZ_ALWAYS_FALSE(expr) \
  526. do { \
  527. if ((expr)) { \
  528. MOZ_ASSERT(false, #expr); \
  529. } else { \
  530. /* Do nothing. */ \
  531. } \
  532. } while (0)
  533. #else
  534. # define MOZ_ALWAYS_TRUE(expr) \
  535. do { \
  536. if ((expr)) { \
  537. /* Silence MOZ_MUST_USE. */ \
  538. } \
  539. } while (0)
  540. # define MOZ_ALWAYS_FALSE(expr) \
  541. do { \
  542. if ((expr)) { \
  543. /* Silence MOZ_MUST_USE. */ \
  544. } \
  545. } while (0)
  546. #endif
  547. #undef MOZ_DUMP_ASSERTION_STACK
  548. #undef MOZ_CRASH_CRASHREPORT
  549. #endif /* mozilla_Assertions_h */