Assertions.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/. */
  5. #include "mozilla/Assertions.h"
  6. #include "mozilla/Atomics.h"
  7. #include <stdarg.h>
  8. MOZ_BEGIN_EXTERN_C
  9. /*
  10. * The crash reason is defined as a global variable here rather than in the
  11. * crash reporter itself to make it available to all code, even libraries like
  12. * JS that don't link with the crash reporter directly. This value will only
  13. * be consumed if the crash reporter is used by the target application.
  14. */
  15. MFBT_DATA const char* gMozCrashReason = nullptr;
  16. #ifndef DEBUG
  17. MFBT_API MOZ_COLD MOZ_NORETURN MOZ_NEVER_INLINE void
  18. MOZ_CrashOOL(int aLine, const char* aReason)
  19. #else
  20. MFBT_API MOZ_COLD MOZ_NORETURN MOZ_NEVER_INLINE void
  21. MOZ_CrashOOL(const char* aFilename, int aLine, const char* aReason)
  22. #endif
  23. {
  24. #ifdef DEBUG
  25. MOZ_ReportCrash(aReason, aFilename, aLine);
  26. #endif
  27. MOZ_CRASH_ANNOTATE(aReason);
  28. MOZ_REALLY_CRASH(aLine);
  29. }
  30. static char sPrintfCrashReason[sPrintfCrashReasonSize] = {};
  31. static mozilla::Atomic<bool> sCrashing(false);
  32. #ifndef DEBUG
  33. MFBT_API MOZ_COLD MOZ_NORETURN MOZ_NEVER_INLINE MOZ_FORMAT_PRINTF(2, 3) void
  34. MOZ_CrashPrintf(int aLine, const char* aFormat, ...)
  35. #else
  36. MFBT_API MOZ_COLD MOZ_NORETURN MOZ_NEVER_INLINE MOZ_FORMAT_PRINTF(3, 4) void
  37. MOZ_CrashPrintf(const char* aFilename, int aLine, const char* aFormat, ...)
  38. #endif
  39. {
  40. if (!sCrashing.compareExchange(false, true)) {
  41. // In the unlikely event of a race condition, skip
  42. // setting the crash reason and just crash safely.
  43. MOZ_REALLY_CRASH(aLine);
  44. }
  45. va_list aArgs;
  46. va_start(aArgs, aFormat);
  47. int ret = vsnprintf(sPrintfCrashReason, sPrintfCrashReasonSize,
  48. aFormat, aArgs);
  49. va_end(aArgs);
  50. MOZ_RELEASE_ASSERT(ret >= 0 && size_t(ret) < sPrintfCrashReasonSize,
  51. "Could not write the explanation string to the supplied buffer!");
  52. #ifdef DEBUG
  53. MOZ_ReportCrash(sPrintfCrashReason, aFilename, aLine);
  54. #endif
  55. MOZ_CRASH_ANNOTATE(sPrintfCrashReason);
  56. MOZ_REALLY_CRASH(aLine);
  57. }
  58. MOZ_END_EXTERN_C