report.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.vappend(Format, Args);
  20. va_end(Args);
  21. }
  22. NORETURN ~ScopedErrorReport() { reportRawError(Message.data()); }
  23. private:
  24. ScopedString Message;
  25. };
  26. inline void NORETURN trap() { __builtin_trap(); }
  27. // This could potentially be called recursively if a CHECK fails in the reports.
  28. void NORETURN reportCheckFailed(const char *File, int Line,
  29. const char *Condition, u64 Value1, u64 Value2) {
  30. static atomic_u32 NumberOfCalls;
  31. if (atomic_fetch_add(&NumberOfCalls, 1, memory_order_relaxed) > 2) {
  32. // TODO(kostyak): maybe sleep here?
  33. trap();
  34. }
  35. ScopedErrorReport Report;
  36. Report.append("CHECK failed @ %s:%d %s ((u64)op1=%llu, (u64)op2=%llu)\n",
  37. File, Line, Condition, Value1, Value2);
  38. }
  39. // Generic string fatal error message.
  40. void NORETURN reportError(const char *Message) {
  41. ScopedErrorReport Report;
  42. Report.append("%s\n", Message);
  43. }
  44. // Generic fatal error message without ScopedString.
  45. void NORETURN reportRawError(const char *Message) {
  46. outputRaw(Message);
  47. setAbortMessage(Message);
  48. die();
  49. }
  50. void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) {
  51. ScopedErrorReport Report;
  52. Report.append("invalid value for %s option: '%s'\n", FlagType, Value);
  53. }
  54. // The checksum of a chunk header is invalid. This could be caused by an
  55. // {over,under}write of the header, a pointer that is not an actual chunk.
  56. void NORETURN reportHeaderCorruption(void *Ptr) {
  57. ScopedErrorReport Report;
  58. Report.append("corrupted chunk header at address %p\n", Ptr);
  59. }
  60. // The allocator was compiled with parameters that conflict with field size
  61. // requirements.
  62. void NORETURN reportSanityCheckError(const char *Field) {
  63. ScopedErrorReport Report;
  64. Report.append("maximum possible %s doesn't fit in header\n", Field);
  65. }
  66. // We enforce a maximum alignment, to keep fields smaller and generally prevent
  67. // integer overflows, or unexpected corner cases.
  68. void NORETURN reportAlignmentTooBig(uptr Alignment, uptr MaxAlignment) {
  69. ScopedErrorReport Report;
  70. Report.append("invalid allocation alignment: %zu exceeds maximum supported "
  71. "alignment of %zu\n",
  72. Alignment, MaxAlignment);
  73. }
  74. // See above, we also enforce a maximum size.
  75. void NORETURN reportAllocationSizeTooBig(uptr UserSize, uptr TotalSize,
  76. uptr MaxSize) {
  77. ScopedErrorReport Report;
  78. Report.append("requested allocation size %zu (%zu after adjustments) exceeds "
  79. "maximum supported size of %zu\n",
  80. UserSize, TotalSize, MaxSize);
  81. }
  82. void NORETURN reportOutOfBatchClass() {
  83. ScopedErrorReport Report;
  84. Report.append("BatchClass region is used up, can't hold any free block\n");
  85. }
  86. void NORETURN reportOutOfMemory(uptr RequestedSize) {
  87. ScopedErrorReport Report;
  88. Report.append("out of memory trying to allocate %zu bytes\n", RequestedSize);
  89. }
  90. static const char *stringifyAction(AllocatorAction Action) {
  91. switch (Action) {
  92. case AllocatorAction::Recycling:
  93. return "recycling";
  94. case AllocatorAction::Deallocating:
  95. return "deallocating";
  96. case AllocatorAction::Reallocating:
  97. return "reallocating";
  98. case AllocatorAction::Sizing:
  99. return "sizing";
  100. }
  101. return "<invalid action>";
  102. }
  103. // The chunk is not in a state congruent with the operation we want to perform.
  104. // This is usually the case with a double-free, a realloc of a freed pointer.
  105. void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr) {
  106. ScopedErrorReport Report;
  107. Report.append("invalid chunk state when %s address %p\n",
  108. stringifyAction(Action), Ptr);
  109. }
  110. void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) {
  111. ScopedErrorReport Report;
  112. Report.append("misaligned pointer when %s address %p\n",
  113. stringifyAction(Action), Ptr);
  114. }
  115. // The deallocation function used is at odds with the one used to allocate the
  116. // chunk (eg: new[]/delete or malloc/delete, and so on).
  117. void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
  118. u8 TypeA, u8 TypeB) {
  119. ScopedErrorReport Report;
  120. Report.append("allocation type mismatch when %s address %p (%d vs %d)\n",
  121. stringifyAction(Action), Ptr, TypeA, TypeB);
  122. }
  123. // The size specified to the delete operator does not match the one that was
  124. // passed to new when allocating the chunk.
  125. void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size,
  126. uptr ExpectedSize) {
  127. ScopedErrorReport Report;
  128. Report.append(
  129. "invalid sized delete when deallocating address %p (%zu vs %zu)\n", Ptr,
  130. Size, ExpectedSize);
  131. }
  132. void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment) {
  133. ScopedErrorReport Report;
  134. Report.append(
  135. "invalid allocation alignment: %zu, alignment must be a power of two\n",
  136. Alignment);
  137. }
  138. void NORETURN reportCallocOverflow(uptr Count, uptr Size) {
  139. ScopedErrorReport Report;
  140. Report.append("calloc parameters overflow: count * size (%zu * %zu) cannot "
  141. "be represented with type size_t\n",
  142. Count, Size);
  143. }
  144. void NORETURN reportInvalidPosixMemalignAlignment(uptr Alignment) {
  145. ScopedErrorReport Report;
  146. Report.append(
  147. "invalid alignment requested in posix_memalign: %zu, alignment must be a "
  148. "power of two and a multiple of sizeof(void *) == %zu\n",
  149. Alignment, sizeof(void *));
  150. }
  151. void NORETURN reportPvallocOverflow(uptr Size) {
  152. ScopedErrorReport Report;
  153. Report.append("pvalloc parameters overflow: size %zu rounded up to system "
  154. "page size %zu cannot be represented in type size_t\n",
  155. Size, getPageSizeCached());
  156. }
  157. void NORETURN reportInvalidAlignedAllocAlignment(uptr Alignment, uptr Size) {
  158. ScopedErrorReport Report;
  159. Report.append("invalid alignment requested in aligned_alloc: %zu, alignment "
  160. "must be a power of two and the requested size %zu must be a "
  161. "multiple of alignment\n",
  162. Alignment, Size);
  163. }
  164. } // namespace scudo