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:
ScopedErrorReport()20 ScopedErrorReport() : Message() { Message.append("Scudo ERROR: "); }
append(const char * Format,...)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 }
~ScopedErrorReport()27 NORETURN ~ScopedErrorReport() {
28 outputRaw(Message.data());
29 setAbortMessage(Message.data());
30 die();
31 }
32
33 private:
34 ScopedString Message;
35 };
36
trap()37 inline void NORETURN trap() { __builtin_trap(); }
38
reportSoftRSSLimit(uptr RssLimitMb)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
reportHardRSSLimit(uptr RssLimitMb)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.
reportCheckFailed(const char * File,int Line,const char * Condition,u64 Value1,u64 Value2)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.
reportError(const char * Message)65 void NORETURN reportError(const char *Message) {
66 ScopedErrorReport Report;
67 Report.append("%s\n", Message);
68 }
69
reportInvalidFlag(const char * FlagType,const char * Value)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.
reportHeaderCorruption(void * Ptr)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.
reportHeaderRace(void * Ptr)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.
reportSanityCheckError(const char * Field)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.
reportAlignmentTooBig(uptr Alignment,uptr MaxAlignment)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.
reportAllocationSizeTooBig(uptr UserSize,uptr TotalSize,uptr MaxSize)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
reportOutOfBatchClass()115 void NORETURN reportOutOfBatchClass() {
116 ScopedErrorReport Report;
117 Report.append("BatchClass region is used up, can't hold any free block\n");
118 }
119
reportOutOfMemory(uptr RequestedSize)120 void NORETURN reportOutOfMemory(uptr RequestedSize) {
121 ScopedErrorReport Report;
122 Report.append("out of memory trying to allocate %zu bytes\n", RequestedSize);
123 }
124
stringifyAction(AllocatorAction Action)125 static const char *stringifyAction(AllocatorAction Action) {
126 switch (Action) {
127 case AllocatorAction::Recycling:
128 return "recycling";
129 case AllocatorAction::Deallocating:
130 return "deallocating";
131 case AllocatorAction::Reallocating:
132 return "reallocating";
133 case AllocatorAction::Sizing:
134 return "sizing";
135 }
136 return "<invalid action>";
137 }
138
139 // The chunk is not in a state congruent with the operation we want to perform.
140 // This is usually the case with a double-free, a realloc of a freed pointer.
reportInvalidChunkState(AllocatorAction Action,void * Ptr)141 void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr) {
142 ScopedErrorReport Report;
143 Report.append("invalid chunk state when %s address %p\n",
144 stringifyAction(Action), Ptr);
145 }
146
reportMisalignedPointer(AllocatorAction Action,void * Ptr)147 void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) {
148 ScopedErrorReport Report;
149 Report.append("misaligned pointer when %s address %p\n",
150 stringifyAction(Action), Ptr);
151 }
152
153 // The deallocation function used is at odds with the one used to allocate the
154 // chunk (eg: new[]/delete or malloc/delete, and so on).
reportDeallocTypeMismatch(AllocatorAction Action,void * Ptr,u8 TypeA,u8 TypeB)155 void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr,
156 u8 TypeA, u8 TypeB) {
157 ScopedErrorReport Report;
158 Report.append("allocation type mismatch when %s address %p (%d vs %d)\n",
159 stringifyAction(Action), Ptr, TypeA, TypeB);
160 }
161
162 // The size specified to the delete operator does not match the one that was
163 // passed to new when allocating the chunk.
reportDeleteSizeMismatch(void * Ptr,uptr Size,uptr ExpectedSize)164 void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size,
165 uptr ExpectedSize) {
166 ScopedErrorReport Report;
167 Report.append(
168 "invalid sized delete when deallocating address %p (%zu vs %zu)\n", Ptr,
169 Size, ExpectedSize);
170 }
171
reportAlignmentNotPowerOfTwo(uptr Alignment)172 void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment) {
173 ScopedErrorReport Report;
174 Report.append(
175 "invalid allocation alignment: %zu, alignment must be a power of two\n",
176 Alignment);
177 }
178
reportCallocOverflow(uptr Count,uptr Size)179 void NORETURN reportCallocOverflow(uptr Count, uptr Size) {
180 ScopedErrorReport Report;
181 Report.append("calloc parameters overflow: count * size (%zu * %zu) cannot "
182 "be represented with type size_t\n",
183 Count, Size);
184 }
185
reportInvalidPosixMemalignAlignment(uptr Alignment)186 void NORETURN reportInvalidPosixMemalignAlignment(uptr Alignment) {
187 ScopedErrorReport Report;
188 Report.append(
189 "invalid alignment requested in posix_memalign: %zu, alignment must be a "
190 "power of two and a multiple of sizeof(void *) == %zu\n",
191 Alignment, sizeof(void *));
192 }
193
reportPvallocOverflow(uptr Size)194 void NORETURN reportPvallocOverflow(uptr Size) {
195 ScopedErrorReport Report;
196 Report.append("pvalloc parameters overflow: size %zu rounded up to system "
197 "page size %zu cannot be represented in type size_t\n",
198 Size, getPageSizeCached());
199 }
200
reportInvalidAlignedAllocAlignment(uptr Alignment,uptr Size)201 void NORETURN reportInvalidAlignedAllocAlignment(uptr Alignment, uptr Size) {
202 ScopedErrorReport Report;
203 Report.append("invalid alignment requested in aligned_alloc: %zu, alignment "
204 "must be a power of two and the requested size %zu must be a "
205 "multiple of alignment\n",
206 Alignment, Size);
207 }
208
209 } // namespace scudo
210