report.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //===-- report.cpp ----------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "report.h"
  9. #include "atomic_helpers.h"
  10. #include "string_utils.h"
  11. #include <stdarg.h>
  12. namespace scudo {
  13. class ScopedErrorReport {
  14. public:
  15. ScopedErrorReport() : Message() { Message.append("Scudo ERROR: "); }
  16. void append(const char *Format, ...) {
  17. va_list Args;
  18. va_start(Args, Format);
  19. Message.append(Format, Args);
  20. va_end(Args);
  21. }
  22. NORETURN ~ScopedErrorReport() {
  23. outputRaw(Message.data());
  24. setAbortMessage(Message.data());
  25. die();
  26. }
  27. private:
  28. ScopedString Message;
  29. };
  30. inline void NORETURN trap() { __builtin_trap(); }
  31. void NORETURN reportSoftRSSLimit(uptr RssLimitMb) {
  32. ScopedErrorReport Report;
  33. Report.append("Soft RSS limit of %zu MB exhausted, current RSS is %zu MB\n",
  34. RssLimitMb, GetRSS() >> 20);
  35. }
  36. void NORETURN reportHardRSSLimit(uptr RssLimitMb) {
  37. ScopedErrorReport Report;
  38. Report.append("Hard RSS limit of %zu MB exhausted, current RSS is %zu MB\n",
  39. RssLimitMb, GetRSS() >> 20);
  40. }
  41. // This could potentially be called recursively if a CHECK fails in the reports.
  42. void NORETURN reportCheckFailed(const char *File, int Line,
  43. const char *Condition, u64 Value1, u64 Value2) {
  44. static atomic_u32 NumberOfCalls;
  45. if (atomic_fetch_add(&NumberOfCalls, 1, memory_order_relaxed) > 2) {
  46. // TODO(kostyak): maybe sleep here?
  47. trap();
  48. }
  49. ScopedErrorReport Report;
  50. Report.append("CHECK failed @ %s:%d %s ((u64)op1=%llu, (u64)op2=%llu)\n",
  51. File, Line, Condition, Value1, Value2);
  52. }
  53. // Generic string fatal error message.
  54. void NORETURN reportError(const char *Message) {
  55. ScopedErrorReport Report;
  56. Report.append("%s\n", Message);
  57. }
  58. void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {
  59. ScopedErrorReport Report;
  60. Report.append("invalid value for %s option: '%s'\n", FlagType, Value);
  61. }
  62. // The checksum of a chunk header is invalid. This could be caused by an
  63. // {over,under}write of the header, a pointer that is not an actual chunk.
  64. void NORETURN reportHeaderCorruption(void *Ptr) {
  65. ScopedErrorReport Report;
  66. Report.append("corrupted chunk header at address %p\n", Ptr);
  67. }
  68. // Two threads have attempted to modify a chunk header at the same time. This is
  69. // symptomatic of a race-condition in the application code, or general lack of
  70. // proper locking.
  71. void NORETURN reportHeaderRace(void *Ptr) {
  72. ScopedErrorReport Report;
  73. Report.append("race on chunk header at address %p\n", Ptr);
  74. }
  75. // The allocator was compiled with parameters that conflict with field size
  76. // requirements.
  77. void NORETURN reportSanityCheckError(const char *Field) {
  78. ScopedErrorReport Report;
  79. Report.append("maximum possible %s doesn't fit in header\n", Field);
  80. }
  81. // We enforce a maximum alignment, to keep fields smaller and generally prevent
  82. // integer overflows, or unexpected corner cases.
  83. void NORETURN reportAlignmentTooBig(uptr Alignment, uptr MaxAlignment) {
  84. ScopedErrorReport Report;
  85. Report.append("invalid allocation alignment: %zu exceeds maximum supported "
  86. "alignment of %zu\n",
  87. Alignment, MaxAlignment);
  88. }
  89. // See above, we also enforce a maximum size.
  90. void NORETURN reportAllocationSizeTooBig(uptr UserSize, uptr TotalSize,
  91. uptr MaxSize) {
  92. ScopedErrorReport Report;
  93. Report.append("requested allocation size %zu (%zu after adjustments) exceeds "
  94. "maximum supported size of %zu\n",
  95. UserSize, TotalSize, MaxSize);
  96. }
  97. void NORETURN reportOutOfMemory(uptr RequestedSize) {
  98. ScopedErrorReport Report;
  99. Report.append("out of memory trying to allocate %zu bytes\n", RequestedSize);
  100. }
  101. static const char *stringifyAction(AllocatorAction Action) {
  102. switch (Action) {
  103. case AllocatorAction::Recycling:
  104. return "recycling";
  105. case AllocatorAction::Deallocating:
  106. return "deallocating";
  107. case AllocatorAction::Reallocating:
  108. return "reallocating";
  109. case AllocatorAction::Sizing:
  110. return "sizing";
  111. }
  112. return "<invalid action>";
  113. }
  114. // The chunk is not in a state congruent with the operation we want to perform.
  115. // This is usually the case with a double-free, a realloc of a freed pointer.
  116. void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr) {
  117. ScopedErrorReport Report;
  118. Report.append("invalid chunk state when %s address %p\n",
  119. stringifyAction(Action), Ptr);
  120. }
  121. void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) {
  122. ScopedErrorReport Report;
  123. Report.append("misaligned pointer when %s address %p\n",
  124. stringifyAction(Action), Ptr);
  125. }
  126. // The deallocation function used is at odds with the one used to allocate the
  127. // chunk (eg: new[]/delete or malloc/delete, and so on).
  128. void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
  129. u8 TypeA, u8 TypeB) {
  130. ScopedErrorReport Report;
  131. Report.append("allocation type mismatch when %s address %p (%d vs %d)\n",
  132. stringifyAction(Action), Ptr, TypeA, TypeB);
  133. }
  134. // The size specified to the delete operator does not match the one that was
  135. // passed to new when allocating the chunk.
  136. void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size,
  137. uptr ExpectedSize) {
  138. ScopedErrorReport Report;
  139. Report.append(
  140. "invalid sized delete when deallocating address %p (%zu vs %zu)\n", Ptr,
  141. Size, ExpectedSize);
  142. }
  143. void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment) {
  144. ScopedErrorReport Report;
  145. Report.append(
  146. "invalid allocation alignment: %zu, alignment must be a power of two\n",
  147. Alignment);
  148. }
  149. void NORETURN reportCallocOverflow(uptr Count, uptr Size) {
  150. ScopedErrorReport Report;
  151. Report.append("calloc parameters overflow: count * size (%zu * %zu) cannot "
  152. "be represented with type size_t\n",
  153. Count, Size);
  154. }
  155. void NORETURN reportInvalidPosixMemalignAlignment(uptr Alignment) {
  156. ScopedErrorReport Report;
  157. Report.append(
  158. "invalid alignment requested in posix_memalign: %zu, alignment must be a "
  159. "power of two and a multiple of sizeof(void *) == %zu\n",
  160. Alignment, sizeof(void *));
  161. }
  162. void NORETURN reportPvallocOverflow(uptr Size) {
  163. ScopedErrorReport Report;
  164. Report.append("pvalloc parameters overflow: size %zu rounded up to system "
  165. "page size %zu cannot be represented in type size_t\n",
  166. Size, getPageSizeCached());
  167. }
  168. void NORETURN reportInvalidAlignedAllocAlignment(uptr Alignment, uptr Size) {
  169. ScopedErrorReport Report;
  170. Report.append("invalid alignment requested in aligned_alloc: %zu, alignment "
  171. "must be a power of two and the requested size %zu must be a "
  172. "multiple of alignment\n",
  173. Alignment, Size);
  174. }
  175. } // namespace scudo