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