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: |
21 | ScopedErrorReport() : Message() { Message.append(Format: "Scudo ERROR: "); } |
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 | } |
28 | NORETURN ~ScopedErrorReport() { reportRawError(Message: Message.data()); } |
29 | |
30 | private: |
31 | ScopedString Message; |
32 | }; |
33 | |
34 | inline void NORETURN trap() { __builtin_trap(); } |
35 | |
36 | // This could potentially be called recursively if a CHECK fails in the reports. |
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(A: &NumberOfCalls, V: 1, MO: memory_order_relaxed) > 2) { |
41 | // TODO(kostyak): maybe sleep here? |
42 | trap(); |
43 | } |
44 | ScopedErrorReport Report; |
45 | Report.append(Format: "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. |
50 | void NORETURN reportError(const char *Message) { |
51 | ScopedErrorReport Report; |
52 | Report.append(Format: "%s\n", Message); |
53 | } |
54 | |
55 | // Generic fatal error message without ScopedString. |
56 | void NORETURN reportRawError(const char *Message) { |
57 | outputRaw(Buffer: Message); |
58 | setAbortMessage(Message); |
59 | die(); |
60 | } |
61 | |
62 | void NORETURN reportInvalidFlag(const char *FlagType, const char *Value) { |
63 | ScopedErrorReport Report; |
64 | Report.append(Format: "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. |
69 | void NORETURN reportHeaderCorruption(void *Header, void *Ptr) { |
70 | ScopedErrorReport Report; |
71 | Report.append(Format: "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(Format: ": chunk header is zero and might indicate memory corruption " |
76 | "or a double free\n", |
77 | Ptr); |
78 | } else { |
79 | Report.append(Format: ": 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. |
85 | void NORETURN reportSanityCheckError(const char *Field) { |
86 | ScopedErrorReport Report; |
87 | Report.append(Format: "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. |
92 | void NORETURN reportAlignmentTooBig(uptr Alignment, uptr MaxAlignment) { |
93 | ScopedErrorReport Report; |
94 | Report.append(Format: "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. |
100 | void NORETURN reportAllocationSizeTooBig(uptr UserSize, uptr TotalSize, |
101 | uptr MaxSize) { |
102 | ScopedErrorReport Report; |
103 | Report.append(Format: "requested allocation size %zu (%zu after adjustments) exceeds " |
104 | "maximum supported size of %zu\n", |
105 | UserSize, TotalSize, MaxSize); |
106 | } |
107 | |
108 | void NORETURN reportOutOfBatchClass() { |
109 | ScopedErrorReport Report; |
110 | Report.append(Format: "BatchClass region is used up, can't hold any free block\n"); |
111 | } |
112 | |
113 | void NORETURN reportOutOfMemory(uptr RequestedSize) { |
114 | ScopedErrorReport Report; |
115 | Report.append(Format: "out of memory trying to allocate %zu bytes\n", RequestedSize); |
116 | } |
117 | |
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. |
134 | void NORETURN reportInvalidChunkState(AllocatorAction Action, void *Ptr) { |
135 | ScopedErrorReport Report; |
136 | Report.append(Format: "invalid chunk state when %s address %p\n", |
137 | stringifyAction(Action), Ptr); |
138 | } |
139 | |
140 | void NORETURN reportMisalignedPointer(AllocatorAction Action, void *Ptr) { |
141 | ScopedErrorReport Report; |
142 | Report.append(Format: "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). |
148 | void NORETURN reportDeallocTypeMismatch(AllocatorAction Action, void *Ptr, |
149 | u8 TypeA, u8 TypeB) { |
150 | ScopedErrorReport Report; |
151 | Report.append(Format: "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. |
157 | void NORETURN reportDeleteSizeMismatch(void *Ptr, uptr Size, |
158 | uptr ExpectedSize) { |
159 | ScopedErrorReport Report; |
160 | Report.append( |
161 | Format: "invalid sized delete when deallocating address %p (%zu vs %zu)\n", Ptr, |
162 | Size, ExpectedSize); |
163 | } |
164 | |
165 | void NORETURN reportAlignmentNotPowerOfTwo(uptr Alignment) { |
166 | ScopedErrorReport Report; |
167 | Report.append( |
168 | Format: "invalid allocation alignment: %zu, alignment must be a power of two\n", |
169 | Alignment); |
170 | } |
171 | |
172 | void NORETURN reportCallocOverflow(uptr Count, uptr Size) { |
173 | ScopedErrorReport Report; |
174 | Report.append(Format: "calloc parameters overflow: count * size (%zu * %zu) cannot " |
175 | "be represented with type size_t\n", |
176 | Count, Size); |
177 | } |
178 | |
179 | void NORETURN reportInvalidPosixMemalignAlignment(uptr Alignment) { |
180 | ScopedErrorReport Report; |
181 | Report.append( |
182 | Format: "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 | |
187 | void NORETURN reportPvallocOverflow(uptr Size) { |
188 | ScopedErrorReport Report; |
189 | Report.append(Format: "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 | |
194 | void NORETURN reportInvalidAlignedAllocAlignment(uptr Alignment, uptr Size) { |
195 | ScopedErrorReport Report; |
196 | Report.append(Format: "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 |
Definitions
- ScopedErrorReport
- ScopedErrorReport
- append
- ~ScopedErrorReport
- trap
- reportCheckFailed
- reportError
- reportRawError
- reportInvalidFlag
- reportHeaderCorruption
- reportSanityCheckError
- reportAlignmentTooBig
- reportAllocationSizeTooBig
- reportOutOfBatchClass
- reportOutOfMemory
- stringifyAction
- reportInvalidChunkState
- reportMisalignedPointer
- reportDeallocTypeMismatch
- reportDeleteSizeMismatch
- reportAlignmentNotPowerOfTwo
- reportCallocOverflow
- reportInvalidPosixMemalignAlignment
- reportPvallocOverflow
Learn to use CMake with our Intro Training
Find out more