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
reportOutOfMemory(uptr RequestedSize)115 void NORETURN reportOutOfMemory(uptr RequestedSize) {
116 ScopedErrorReport Report;
117 Report.append("out of memory trying to allocate %zu bytes\n", RequestedSize);
118 }
119
stringifyAction(AllocatorAction Action)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.
reportInvalidChunkState(AllocatorAction Action,void * Ptr)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
reportMisalignedPointer(AllocatorAction Action,void * Ptr)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).
reportDeallocTypeMismatch(AllocatorAction Action,void * Ptr,u8 TypeA,u8 TypeB)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.
reportDeleteSizeMismatch(void * Ptr,uptr Size,uptr ExpectedSize)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
reportAlignmentNotPowerOfTwo(uptr Alignment)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
reportCallocOverflow(uptr Count,uptr Size)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
reportInvalidPosixMemalignAlignment(uptr Alignment)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
reportPvallocOverflow(uptr Size)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
reportInvalidAlignedAllocAlignment(uptr Alignment,uptr Size)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