Bug 1922904 - Fix bug 1780701 in a different approach. r=botond
[gecko.git] / mfbt / Assertions.cpp
blob7721677f1950db8c867c5cf3480b185fa2e7526d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/Assertions.h"
7 #include "mozilla/Atomics.h"
8 #include "mozilla/Sprintf.h"
10 #include <stdarg.h>
12 MOZ_BEGIN_EXTERN_C
15 * The crash reason is defined as a global variable here rather than in the
16 * crash reporter itself to make it available to all code, even libraries like
17 * JS that don't link with the crash reporter directly. This value will only
18 * be consumed if the crash reporter is used by the target application.
20 MFBT_DATA const char* gMozCrashReason = nullptr;
22 static char sPrintfCrashReason[sPrintfCrashReasonSize] = {};
24 // Accesses to this atomic are not included in web replay recordings, so that
25 // if we crash in an area where recorded events are not allowed the true reason
26 // for the crash is not obscured by a record/replay error.
27 static mozilla::Atomic<bool, mozilla::SequentiallyConsistent> sCrashing(false);
29 MFBT_API MOZ_COLD MOZ_NEVER_INLINE MOZ_FORMAT_PRINTF(1, 2) const
30 char* MOZ_CrashPrintf(const char* aFormat, ...) {
31 if (!sCrashing.compareExchange(false, true)) {
32 // In the unlikely event of a race condition, skip
33 // setting the crash reason and just crash safely.
34 MOZ_RELEASE_ASSERT(false);
36 va_list aArgs;
37 va_start(aArgs, aFormat);
38 int ret = VsprintfLiteral(sPrintfCrashReason, aFormat, aArgs);
39 va_end(aArgs);
40 MOZ_RELEASE_ASSERT(
41 ret >= 0 && size_t(ret) < sPrintfCrashReasonSize,
42 "Could not write the explanation string to the supplied buffer!");
43 return sPrintfCrashReason;
46 MOZ_END_EXTERN_C
48 MFBT_API MOZ_NORETURN MOZ_COLD void mozilla::detail::InvalidArrayIndex_CRASH(
49 size_t aIndex, size_t aLength) {
50 MOZ_CRASH_UNSAFE_PRINTF("ElementAt(aIndex = %zu, aLength = %zu)", aIndex,
51 aLength);