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