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/. */
7 /* Implementations of runtime and static assertion macros for C and C++. */
9 #ifndef mozilla_Assertions_h
10 #define mozilla_Assertions_h
12 #if (defined(MOZ_HAS_MOZGLUE) || defined(MOZILLA_INTERNAL_API)) && \
14 # define MOZ_DUMP_ASSERTION_STACK
16 #if defined(XP_WIN) && (defined(DEBUG) || defined(FUZZING))
17 # define MOZ_BUFFER_STDERR
20 // It appears that this is sometimes compiled without XP_WIN
23 # define MOZ_GET_PID() _getpid()
24 #elif !defined(__wasi__)
26 # define MOZ_GET_PID() getpid()
28 // Prevent compiler warning
29 # define MOZ_GET_PID() -1
32 #include "mozilla/Attributes.h"
33 #include "mozilla/Compiler.h"
34 #include "mozilla/Fuzzing.h"
35 #include "mozilla/Likely.h"
36 #include "mozilla/MacroArgs.h"
37 #include "mozilla/StaticAnalysisFunctions.h"
38 #include "mozilla/Types.h"
39 #ifdef MOZ_DUMP_ASSERTION_STACK
40 # include "mozilla/StackWalk.h"
44 * The crash reason set by MOZ_CRASH_ANNOTATE is consumed by the crash reporter
45 * if present. It is declared here (and defined in Assertions.cpp) to make it
46 * available to all code, even libraries that don't link with the crash reporter
50 extern MFBT_DATA
const char* gMozCrashReason
;
53 #if defined(MOZ_HAS_MOZGLUE) || defined(MOZILLA_INTERNAL_API)
54 static inline void AnnotateMozCrashReason(const char* reason
) {
55 gMozCrashReason
= reason
;
56 // See bug 1681846, on 32-bit Android ARM the compiler removes the store to
57 // gMozCrashReason if this barrier is not present.
58 asm volatile("" ::: "memory");
60 # define MOZ_CRASH_ANNOTATE(...) AnnotateMozCrashReason(__VA_ARGS__)
62 # define MOZ_CRASH_ANNOTATE(...) \
72 * TerminateProcess and GetCurrentProcess are defined in <winbase.h>, which
73 * further depends on <windef.h>. We hardcode these few definitions manually
74 * because those headers clutter the global namespace with a significant
75 * number of undesired macros and symbols.
78 __declspec(dllimport
) int __stdcall
TerminateProcess(void* hProcess
,
79 unsigned int uExitCode
);
80 __declspec(dllimport
) void* __stdcall
GetCurrentProcess(void);
82 #elif defined(__wasi__)
84 * On Wasm/WASI platforms, we just call __builtin_trap().
90 # include <android/log.h>
95 #if defined(ANDROID) && defined(MOZ_DUMP_ASSERTION_STACK)
96 MOZ_MAYBE_UNUSED
static void MOZ_ReportAssertionFailurePrintFrame(
98 __android_log_print(ANDROID_LOG_FATAL
, "MOZ_Assert", "%s\n", aBuf
);
103 * Prints |aStr| as an assertion failure (using aFilename and aLine as the
104 * location of the assertion) to the standard debug-output channel.
106 * Usually you should use MOZ_ASSERT or MOZ_CRASH instead of this method. This
107 * method is primarily for internal use in this header, and only secondarily
108 * for use in implementing release-build assertions.
111 MOZ_MAYBE_UNUSED
static MOZ_COLD MOZ_NEVER_INLINE
void
112 MOZ_ReportAssertionFailure(const char* aStr
, const char* aFilename
,
113 int aLine
) MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS
{
114 MOZ_FUZZING_HANDLE_CRASH_EVENT4("MOZ_ASSERT", aFilename
, aLine
, aStr
);
116 __android_log_print(ANDROID_LOG_FATAL
, "MOZ_Assert",
117 "[%d] Assertion failure: %s, at %s:%d\n", MOZ_GET_PID(),
118 aStr
, aFilename
, aLine
);
119 # if defined(MOZ_DUMP_ASSERTION_STACK)
120 MozWalkTheStackWithWriter(MOZ_ReportAssertionFailurePrintFrame
, CallerPC(),
124 # if defined(MOZ_BUFFER_STDERR)
126 snprintf(msg
, sizeof(msg
) - 1, "[%d] Assertion failure: %s, at %s:%d\n",
127 MOZ_GET_PID(), aStr
, aFilename
, aLine
);
130 fprintf(stderr
, "[%d] Assertion failure: %s, at %s:%d\n", MOZ_GET_PID(), aStr
,
133 # if defined(MOZ_DUMP_ASSERTION_STACK)
134 MozWalkTheStack(stderr
, CallerPC(), /* aMaxFrames */ 0);
140 MOZ_MAYBE_UNUSED
static MOZ_COLD MOZ_NEVER_INLINE
void MOZ_ReportCrash(
141 const char* aStr
, const char* aFilename
,
142 int aLine
) MOZ_PRETEND_NORETURN_FOR_STATIC_ANALYSIS
{
144 __android_log_print(ANDROID_LOG_FATAL
, "MOZ_CRASH",
145 "[%d] Hit MOZ_CRASH(%s) at %s:%d\n", MOZ_GET_PID(), aStr
,
148 # if defined(MOZ_BUFFER_STDERR)
150 snprintf(msg
, sizeof(msg
) - 1, "[%d] Hit MOZ_CRASH(%s) at %s:%d\n",
151 MOZ_GET_PID(), aStr
, aFilename
, aLine
);
154 fprintf(stderr
, "[%d] Hit MOZ_CRASH(%s) at %s:%d\n", MOZ_GET_PID(), aStr
,
157 # if defined(MOZ_DUMP_ASSERTION_STACK)
158 MozWalkTheStack(stderr
, CallerPC(), /* aMaxFrames */ 0);
165 * MOZ_ASSUME_UNREACHABLE_MARKER() expands to an expression which states that
166 * it is undefined behavior for execution to reach this point. No guarantees
167 * are made about what will happen if this is reached at runtime. Most code
168 * should use MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE because it has extra
171 #if defined(__clang__) || defined(__GNUC__)
172 # define MOZ_ASSUME_UNREACHABLE_MARKER() __builtin_unreachable()
173 #elif defined(_MSC_VER)
174 # define MOZ_ASSUME_UNREACHABLE_MARKER() __assume(0)
177 # define MOZ_ASSUME_UNREACHABLE_MARKER() ::abort()
179 # define MOZ_ASSUME_UNREACHABLE_MARKER() abort()
184 * MOZ_REALLY_CRASH is used in the implementation of MOZ_CRASH(). You should
185 * call MOZ_CRASH instead.
187 #if defined(_MSC_VER)
189 * On MSVC use the __debugbreak compiler intrinsic, which produces an inline
190 * (not nested in a system function) breakpoint. This distinctively invokes
191 * Breakpad without requiring system library symbols on all stack-processing
192 * machines, as a nested breakpoint would require.
194 * We use __LINE__ to prevent the compiler from folding multiple crash sites
195 * together, which would make crash reports hard to understand.
197 * We use TerminateProcess with the exit code aborting would generate
198 * because we don't want to invoke atexit handlers, destructors, library
199 * unload handlers, and so on when our process might be in a compromised
202 * We don't use abort() because it'd cause Windows to annoyingly pop up the
203 * process error dialog multiple times. See bug 345118 and bug 426163.
205 * (Technically these are Windows requirements, not MSVC requirements. But
206 * practically you need MSVC for debugging, and we only ship builds created
207 * by MSVC, so doing it this way reduces complexity.)
210 MOZ_MAYBE_UNUSED
static MOZ_COLD MOZ_NORETURN MOZ_NEVER_INLINE
void
211 MOZ_NoReturn(int aLine
) {
212 *((volatile int*)NULL
) = aLine
;
213 TerminateProcess(GetCurrentProcess(), 3);
214 MOZ_ASSUME_UNREACHABLE_MARKER();
217 # define MOZ_REALLY_CRASH(line) \
220 MOZ_NoReturn(line); \
225 # define MOZ_REALLY_CRASH(line) __builtin_trap()
230 * MOZ_CRASH_WRITE_ADDR is the address to be used when performing a forced
231 * crash. NULL is preferred however if for some reason NULL cannot be used
232 * this makes choosing another value possible.
234 * In the case of UBSan certain checks, bounds specifically, cause the compiler
235 * to emit the 'ud2' instruction when storing to 0x0. This causes forced
236 * crashes to manifest as ILL (at an arbitrary address) instead of the expected
240 # define MOZ_CRASH_WRITE_ADDR 0x1
242 # define MOZ_CRASH_WRITE_ADDR NULL
246 # define MOZ_REALLY_CRASH(line) \
248 *((volatile int*)MOZ_CRASH_WRITE_ADDR) = line; /* NOLINT */ \
249 MOZ_NOMERGE ::abort(); \
252 # define MOZ_REALLY_CRASH(line) \
254 *((volatile int*)MOZ_CRASH_WRITE_ADDR) = line; /* NOLINT */ \
255 MOZ_NOMERGE abort(); \
261 * MOZ_CRASH([explanation-string]) crashes the program, plain and simple, in a
262 * Breakpad-compatible way, in both debug and release builds.
264 * MOZ_CRASH is a good solution for "handling" failure cases when you're
265 * unwilling or unable to handle them more cleanly -- for OOM, for likely memory
266 * corruption, and so on. It's also a good solution if you need safe behavior
267 * in release builds as well as debug builds. But if the failure is one that
268 * should be debugged and fixed, MOZ_ASSERT is generally preferable.
270 * The optional explanation-string, if provided, must be a string literal
271 * explaining why we're crashing. This argument is intended for use with
272 * MOZ_CRASH() calls whose rationale is non-obvious; don't use it if it's
273 * obvious why we're crashing.
275 * If we're a DEBUG build and we crash at a MOZ_CRASH which provides an
276 * explanation-string, we print the string to stderr. Otherwise, we don't
277 * print anything; this is because we want MOZ_CRASH to be 100% safe in release
278 * builds, and it's hard to print to stderr safely when memory might have been
281 #if !(defined(DEBUG) || defined(FUZZING))
282 # define MOZ_CRASH(...) \
284 MOZ_FUZZING_HANDLE_CRASH_EVENT4("MOZ_CRASH", __FILE__, __LINE__, NULL); \
285 MOZ_CRASH_ANNOTATE("MOZ_CRASH(" __VA_ARGS__ ")"); \
286 MOZ_REALLY_CRASH(__LINE__); \
289 # define MOZ_CRASH(...) \
291 MOZ_FUZZING_HANDLE_CRASH_EVENT4("MOZ_CRASH", __FILE__, __LINE__, NULL); \
292 MOZ_ReportCrash("" __VA_ARGS__, __FILE__, __LINE__); \
293 MOZ_CRASH_ANNOTATE("MOZ_CRASH(" __VA_ARGS__ ")"); \
294 MOZ_REALLY_CRASH(__LINE__); \
299 * MOZ_CRASH_UNSAFE(explanation-string) can be used if the explanation string
300 * cannot be a string literal (but no other processing needs to be done on it).
301 * A regular MOZ_CRASH() is preferred wherever possible, as passing arbitrary
302 * strings from a potentially compromised process is not without risk. If the
303 * string being passed is the result of a printf-style function, consider using
304 * MOZ_CRASH_UNSAFE_PRINTF instead.
306 * @note This macro causes data collection because crash strings are annotated
307 * to crash-stats and are publicly visible. Firefox data stewards must do data
308 * review on usages of this macro.
310 static MOZ_ALWAYS_INLINE_EVEN_DEBUG MOZ_COLD MOZ_NORETURN
void MOZ_Crash(
311 const char* aFilename
, int aLine
, const char* aReason
) {
312 MOZ_FUZZING_HANDLE_CRASH_EVENT4("MOZ_CRASH", aFilename
, aLine
, aReason
);
313 #if defined(DEBUG) || defined(FUZZING)
314 MOZ_ReportCrash(aReason
, aFilename
, aLine
);
316 MOZ_CRASH_ANNOTATE(aReason
);
317 MOZ_REALLY_CRASH(aLine
);
319 #define MOZ_CRASH_UNSAFE(reason) MOZ_Crash(__FILE__, __LINE__, reason)
321 static const size_t sPrintfMaxArgs
= 4;
322 static const size_t sPrintfCrashReasonSize
= 1024;
324 MFBT_API MOZ_COLD MOZ_NEVER_INLINE
MOZ_FORMAT_PRINTF(1, 2) const
325 char* MOZ_CrashPrintf(const char* aFormat
, ...);
328 * MOZ_CRASH_UNSAFE_PRINTF(format, arg1 [, args]) can be used when more
329 * information is desired than a string literal can supply. The caller provides
330 * a printf-style format string, which must be a string literal and between
331 * 1 and 4 additional arguments. A regular MOZ_CRASH() is preferred wherever
332 * possible, as passing arbitrary strings to printf from a potentially
333 * compromised process is not without risk.
335 * @note This macro causes data collection because crash strings are annotated
336 * to crash-stats and are publicly visible. Firefox data stewards must do data
337 * review on usages of this macro.
339 #define MOZ_CRASH_UNSAFE_PRINTF(format, ...) \
341 static_assert(MOZ_ARG_COUNT(__VA_ARGS__) > 0, \
342 "Did you forget arguments to MOZ_CRASH_UNSAFE_PRINTF? " \
343 "Or maybe you want MOZ_CRASH instead?"); \
344 static_assert(MOZ_ARG_COUNT(__VA_ARGS__) <= sPrintfMaxArgs, \
345 "Only up to 4 additional arguments are allowed!"); \
346 static_assert(sizeof(format) <= sPrintfCrashReasonSize, \
347 "The supplied format string is too long!"); \
348 MOZ_Crash(__FILE__, __LINE__, MOZ_CrashPrintf("" format, __VA_ARGS__)); \
354 * MOZ_ASSERT(expr [, explanation-string]) asserts that |expr| must be truthy in
355 * debug builds. If it is, execution continues. Otherwise, an error message
356 * including the expression and the explanation-string (if provided) is printed,
357 * an attempt is made to invoke any existing debugger, and execution halts.
358 * MOZ_ASSERT is fatal: no recovery is possible. Do not assert a condition
359 * which can correctly be falsy.
361 * The optional explanation-string, if provided, must be a string literal
362 * explaining the assertion. It is intended for use with assertions whose
363 * correctness or rationale is non-obvious, and for assertions where the "real"
364 * condition being tested is best described prosaically. Don't provide an
365 * explanation if it's not actually helpful.
367 * // No explanation needed: pointer arguments often must not be NULL.
370 * // An explanation can be helpful to explain exactly how we know an
371 * // assertion is valid.
372 * MOZ_ASSERT(state == WAITING_FOR_RESPONSE,
373 * "given that <thingA> and <thingB>, we must have...");
375 * // Or it might disambiguate multiple identical (save for their location)
376 * // assertions of the same expression.
377 * MOZ_ASSERT(getSlot(PRIMITIVE_THIS_SLOT).isUndefined(),
378 * "we already set [[PrimitiveThis]] for this Boolean object");
379 * MOZ_ASSERT(getSlot(PRIMITIVE_THIS_SLOT).isUndefined(),
380 * "we already set [[PrimitiveThis]] for this String object");
382 * MOZ_ASSERT has no effect in non-debug builds. It is designed to catch bugs
383 * *only* during debugging, not "in the field". If you want the latter, use
384 * MOZ_RELEASE_ASSERT, which applies to non-debug builds as well.
386 * MOZ_DIAGNOSTIC_ASSERT works like MOZ_RELEASE_ASSERT in Nightly and early beta
387 * and MOZ_ASSERT in late Beta and Release - use this when a condition is
388 * potentially rare enough to require real user testing to hit, but is not
389 * security-sensitive. This can cause user pain, so use it sparingly. If a
390 * MOZ_DIAGNOSTIC_ASSERT is firing, it should promptly be converted to a
391 * MOZ_ASSERT while the failure is being investigated, rather than letting users
394 * MOZ_DIAGNOSTIC_ASSERT_ENABLED is defined when MOZ_DIAGNOSTIC_ASSERT is like
395 * MOZ_RELEASE_ASSERT rather than MOZ_ASSERT.
399 * Implement MOZ_VALIDATE_ASSERT_CONDITION_TYPE, which is used to guard against
400 * accidentally passing something unintended in lieu of an assertion condition.
404 # include <type_traits>
408 template <typename T
>
409 struct AssertionConditionType
{
410 using ValueT
= std::remove_reference_t
<T
>;
411 static_assert(!std::is_array_v
<ValueT
>,
412 "Expected boolean assertion condition, got an array or a "
414 static_assert(!std::is_function_v
<ValueT
>,
415 "Expected boolean assertion condition, got a function! Did "
416 "you intend to call that function?");
417 static_assert(!std::is_floating_point_v
<ValueT
>,
418 "It's often a bad idea to assert that a floating-point number "
419 "is nonzero, because such assertions tend to intermittently "
420 "fail. Shouldn't your code gracefully handle this case instead "
421 "of asserting? Anyway, if you really want to do that, write an "
422 "explicit boolean condition, like !!x or x!=0.");
424 static const bool isValid
= true;
427 } // namespace detail
428 } // namespace mozilla
429 # define MOZ_VALIDATE_ASSERT_CONDITION_TYPE(x) \
431 mozilla::detail::AssertionConditionType<decltype(x)>::isValid, \
432 "invalid assertion condition")
434 # define MOZ_VALIDATE_ASSERT_CONDITION_TYPE(x)
437 #if defined(DEBUG) || defined(MOZ_ASAN)
438 # define MOZ_REPORT_ASSERTION_FAILURE(...) \
439 MOZ_ReportAssertionFailure(__VA_ARGS__)
441 # define MOZ_REPORT_ASSERTION_FAILURE(...) \
446 /* First the single-argument form. */
447 #define MOZ_ASSERT_HELPER1(kind, expr) \
449 MOZ_VALIDATE_ASSERT_CONDITION_TYPE(expr); \
450 if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
451 MOZ_FUZZING_HANDLE_CRASH_EVENT2(kind, #expr); \
452 MOZ_REPORT_ASSERTION_FAILURE(#expr, __FILE__, __LINE__); \
453 MOZ_CRASH_ANNOTATE(kind "(" #expr ")"); \
454 MOZ_REALLY_CRASH(__LINE__); \
457 /* Now the two-argument form. */
458 #define MOZ_ASSERT_HELPER2(kind, expr, explain) \
460 MOZ_VALIDATE_ASSERT_CONDITION_TYPE(expr); \
461 if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
462 MOZ_FUZZING_HANDLE_CRASH_EVENT2(kind, #expr); \
463 MOZ_REPORT_ASSERTION_FAILURE(#expr " (" explain ")", __FILE__, \
465 MOZ_CRASH_ANNOTATE(kind "(" #expr ") (" explain ")"); \
466 MOZ_REALLY_CRASH(__LINE__); \
470 #define MOZ_ASSERT_GLUE(a, b) a b
471 #define MOZ_RELEASE_ASSERT(...) \
473 MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_ASSERT_HELPER, __VA_ARGS__), \
474 ("MOZ_RELEASE_ASSERT", __VA_ARGS__))
477 # define MOZ_ASSERT(...) \
479 MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_ASSERT_HELPER, __VA_ARGS__), \
480 ("MOZ_ASSERT", __VA_ARGS__))
482 # define MOZ_ASSERT(...) \
487 #if defined(MOZ_DIAGNOSTIC_ASSERT_ENABLED)
488 # define MOZ_DIAGNOSTIC_ASSERT(...) \
490 MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_ASSERT_HELPER, __VA_ARGS__), \
491 ("MOZ_DIAGNOSTIC_ASSERT", __VA_ARGS__))
493 # define MOZ_DIAGNOSTIC_ASSERT(...) \
499 * MOZ_ASSERT_DEBUG_OR_FUZZING is like a MOZ_ASSERT but also enabled in builds
500 * that are non-DEBUG but FUZZING. This is useful for checks that are too
501 * expensive for Nightly in general but are still indicating potentially
503 * In fuzzing builds, the assert is rewritten to be a diagnostic assert because
504 * we already use this in other sensitive places and fuzzing automation is
505 * set to act on these under all circumstances.
508 # define MOZ_ASSERT_DEBUG_OR_FUZZING(...) MOZ_DIAGNOSTIC_ASSERT(__VA_ARGS__)
510 # define MOZ_ASSERT_DEBUG_OR_FUZZING(...) MOZ_ASSERT(__VA_ARGS__)
514 * MOZ_ASSERT_IF(cond1, cond2) is equivalent to MOZ_ASSERT(cond2) if cond1 is
517 * MOZ_ASSERT_IF(isPrime(num), num == 2 || isOdd(num));
519 * As with MOZ_ASSERT, MOZ_ASSERT_IF has effect only in debug builds. It is
520 * designed to catch bugs during debugging, not "in the field".
523 # define MOZ_ASSERT_IF(cond, expr) \
530 # define MOZ_ASSERT_IF(cond, expr) \
536 * MOZ_DIAGNOSTIC_ASSERT_IF is like MOZ_ASSERT_IF, but using
537 * MOZ_DIAGNOSTIC_ASSERT as the underlying assert.
539 * See the block comment for MOZ_DIAGNOSTIC_ASSERT above for more details on how
540 * diagnostic assertions work and how to use them.
542 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
543 # define MOZ_DIAGNOSTIC_ASSERT_IF(cond, expr) \
546 MOZ_DIAGNOSTIC_ASSERT(expr); \
550 # define MOZ_DIAGNOSTIC_ASSERT_IF(cond, expr) \
556 * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE([reason]) tells the compiler that it
557 * can assume that the macro call cannot be reached during execution. This lets
558 * the compiler generate better-optimized code under some circumstances, at the
559 * expense of the program's behavior being undefined if control reaches the
560 * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE.
562 * In Gecko, you probably should not use this macro outside of performance- or
563 * size-critical code, because it's unsafe. If you don't care about code size
564 * or performance, you should probably use MOZ_ASSERT or MOZ_CRASH.
566 * SpiderMonkey is a different beast, and there it's acceptable to use
567 * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE more widely.
569 * Note that MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE is noreturn, so it's valid
570 * not to return a value following a MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE
581 * int ptrToInt(ValueType type, void* value) {
583 * // We know for sure that type is either INT or FLOAT, and we want this
584 * // code to run as quickly as possible.
587 * return *(int*) value;
589 * return (int) *(float*) value;
591 * MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Unexpected ValueType");
597 * Unconditional assert in debug builds for (assumed) unreachable code paths
598 * that have a safe return without crashing in release builds.
600 #define MOZ_ASSERT_UNREACHABLE(reason) \
601 MOZ_ASSERT(false, "MOZ_ASSERT_UNREACHABLE: " reason)
603 #define MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE(reason) \
605 MOZ_ASSERT_UNREACHABLE(reason); \
606 MOZ_ASSUME_UNREACHABLE_MARKER(); \
610 * MOZ_FALLTHROUGH_ASSERT is an annotation to suppress compiler warnings about
611 * switch cases that MOZ_ASSERT(false) (or its alias MOZ_ASSERT_UNREACHABLE) in
612 * debug builds, but intentionally fall through in release builds to handle
615 * Why do we need MOZ_FALLTHROUGH_ASSERT in addition to [[fallthrough]]? In
616 * release builds, the MOZ_ASSERT(false) will expand to `do { } while (false)`,
617 * requiring a [[fallthrough]] annotation to suppress a -Wimplicit-fallthrough
618 * warning. In debug builds, the MOZ_ASSERT(false) will expand to something like
619 * `if (true) { MOZ_CRASH(); }` and the [[fallthrough]] annotation will cause
620 * a -Wunreachable-code warning. The MOZ_FALLTHROUGH_ASSERT macro breaks this
623 * // Example before MOZ_FALLTHROUGH_ASSERT:
626 * // This case wants to assert in debug builds, fall through in release.
627 * MOZ_ASSERT(false); // -Wimplicit-fallthrough warning in release builds!
628 * [[fallthrough]]; // but -Wunreachable-code warning in debug builds!
633 * // Example with MOZ_FALLTHROUGH_ASSERT:
636 * // This case asserts in debug builds, falls through in release.
637 * MOZ_FALLTHROUGH_ASSERT("Unexpected foo value?!");
643 # define MOZ_FALLTHROUGH_ASSERT(...) \
644 MOZ_CRASH("MOZ_FALLTHROUGH_ASSERT: " __VA_ARGS__)
646 # define MOZ_FALLTHROUGH_ASSERT(...) [[fallthrough]]
650 * MOZ_ALWAYS_TRUE(expr) and friends always evaluate the provided expression,
651 * in debug builds and in release builds both. Then, in debug builds and
652 * Nightly and early beta builds, the value of the expression is
653 * asserted either true or false using MOZ_DIAGNOSTIC_ASSERT.
655 #define MOZ_ALWAYS_TRUE(expr) \
657 if (MOZ_LIKELY(expr)) { \
658 /* Silence [[nodiscard]]. */ \
660 MOZ_DIAGNOSTIC_ASSERT(false, #expr); \
664 #define MOZ_ALWAYS_FALSE(expr) MOZ_ALWAYS_TRUE(!(expr))
665 #define MOZ_ALWAYS_OK(expr) MOZ_ALWAYS_TRUE((expr).isOk())
666 #define MOZ_ALWAYS_ERR(expr) MOZ_ALWAYS_TRUE((expr).isErr())
669 * These are disabled when fuzzing
672 # define MOZ_CRASH_UNLESS_FUZZING(...) \
675 # define MOZ_ASSERT_UNLESS_FUZZING(...) \
679 # define MOZ_CRASH_UNLESS_FUZZING(...) MOZ_CRASH(__VA_ARGS__)
680 # define MOZ_ASSERT_UNLESS_FUZZING(...) MOZ_ASSERT(__VA_ARGS__)
683 #undef MOZ_BUFFER_STDERR
684 #undef MOZ_CRASH_CRASHREPORT
685 #undef MOZ_DUMP_ASSERTION_STACK
688 * This is only used by Array and nsTArray classes, therefore it is not
689 * required when included from C code.
692 namespace mozilla::detail
{
693 MFBT_API MOZ_NORETURN MOZ_COLD
void InvalidArrayIndex_CRASH(size_t aIndex
,
695 } // namespace mozilla::detail
696 #endif // __cplusplus
699 * Provide a fake default value to be used when a value is required but none can
700 * sensibily be provided without adding undefined behavior or security issues.
702 * This function asserts and aborts if it ever executed.
707 * const Droid& lookFor;
708 * Trooper() : lookFor(MakeCompilerAssumeUnreachableFakeValue<
710 * // The class might be instantiated due to existing caller
711 * // but this never happens in practice.
718 template <typename T
>
719 static inline T
MakeCompilerAssumeUnreachableFakeValue() {
720 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE();
722 } // namespace mozilla
723 #endif // __cplusplus
725 #endif /* mozilla_Assertions_h */