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