xref: /openbsd-src/gnu/llvm/compiler-rt/lib/asan/asan_errors.cpp (revision 3cab2bb3f667058bece8e38b12449a63a9d73c4b)
1*3cab2bb3Spatrick //===-- asan_errors.cpp -----------------------------------------*- C++ -*-===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick //
9*3cab2bb3Spatrick // This file is a part of AddressSanitizer, an address sanity checker.
10*3cab2bb3Spatrick //
11*3cab2bb3Spatrick // ASan implementation for error structures.
12*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
13*3cab2bb3Spatrick 
14*3cab2bb3Spatrick #include "asan_errors.h"
15*3cab2bb3Spatrick #include "asan_descriptions.h"
16*3cab2bb3Spatrick #include "asan_mapping.h"
17*3cab2bb3Spatrick #include "asan_report.h"
18*3cab2bb3Spatrick #include "asan_stack.h"
19*3cab2bb3Spatrick #include "sanitizer_common/sanitizer_stackdepot.h"
20*3cab2bb3Spatrick 
21*3cab2bb3Spatrick namespace __asan {
22*3cab2bb3Spatrick 
23*3cab2bb3Spatrick static void OnStackUnwind(const SignalContext &sig,
24*3cab2bb3Spatrick                           const void *callback_context,
25*3cab2bb3Spatrick                           BufferedStackTrace *stack) {
26*3cab2bb3Spatrick   bool fast = common_flags()->fast_unwind_on_fatal;
27*3cab2bb3Spatrick #if SANITIZER_FREEBSD || SANITIZER_NETBSD
28*3cab2bb3Spatrick   // On FreeBSD the slow unwinding that leverages _Unwind_Backtrace()
29*3cab2bb3Spatrick   // yields the call stack of the signal's handler and not of the code
30*3cab2bb3Spatrick   // that raised the signal (as it does on Linux).
31*3cab2bb3Spatrick   fast = true;
32*3cab2bb3Spatrick #endif
33*3cab2bb3Spatrick   // Tests and maybe some users expect that scariness is going to be printed
34*3cab2bb3Spatrick   // just before the stack. As only asan has scariness score we have no
35*3cab2bb3Spatrick   // corresponding code in the sanitizer_common and we use this callback to
36*3cab2bb3Spatrick   // print it.
37*3cab2bb3Spatrick   static_cast<const ScarinessScoreBase *>(callback_context)->Print();
38*3cab2bb3Spatrick   stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
39*3cab2bb3Spatrick                 fast);
40*3cab2bb3Spatrick }
41*3cab2bb3Spatrick 
42*3cab2bb3Spatrick void ErrorDeadlySignal::Print() {
43*3cab2bb3Spatrick   ReportDeadlySignal(signal, tid, &OnStackUnwind, &scariness);
44*3cab2bb3Spatrick }
45*3cab2bb3Spatrick 
46*3cab2bb3Spatrick void ErrorDoubleFree::Print() {
47*3cab2bb3Spatrick   Decorator d;
48*3cab2bb3Spatrick   Printf("%s", d.Error());
49*3cab2bb3Spatrick   Report(
50*3cab2bb3Spatrick       "ERROR: AddressSanitizer: attempting %s on %p in thread %s:\n",
51*3cab2bb3Spatrick       scariness.GetDescription(), addr_description.addr,
52*3cab2bb3Spatrick       AsanThreadIdAndName(tid).c_str());
53*3cab2bb3Spatrick   Printf("%s", d.Default());
54*3cab2bb3Spatrick   scariness.Print();
55*3cab2bb3Spatrick   GET_STACK_TRACE_FATAL(second_free_stack->trace[0],
56*3cab2bb3Spatrick                         second_free_stack->top_frame_bp);
57*3cab2bb3Spatrick   stack.Print();
58*3cab2bb3Spatrick   addr_description.Print();
59*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), &stack);
60*3cab2bb3Spatrick }
61*3cab2bb3Spatrick 
62*3cab2bb3Spatrick void ErrorNewDeleteTypeMismatch::Print() {
63*3cab2bb3Spatrick   Decorator d;
64*3cab2bb3Spatrick   Printf("%s", d.Error());
65*3cab2bb3Spatrick   Report(
66*3cab2bb3Spatrick       "ERROR: AddressSanitizer: %s on %p in thread %s:\n",
67*3cab2bb3Spatrick       scariness.GetDescription(), addr_description.addr,
68*3cab2bb3Spatrick       AsanThreadIdAndName(tid).c_str());
69*3cab2bb3Spatrick   Printf("%s  object passed to delete has wrong type:\n", d.Default());
70*3cab2bb3Spatrick   if (delete_size != 0) {
71*3cab2bb3Spatrick     Printf(
72*3cab2bb3Spatrick         "  size of the allocated type:   %zd bytes;\n"
73*3cab2bb3Spatrick         "  size of the deallocated type: %zd bytes.\n",
74*3cab2bb3Spatrick         addr_description.chunk_access.chunk_size, delete_size);
75*3cab2bb3Spatrick   }
76*3cab2bb3Spatrick   const uptr user_alignment =
77*3cab2bb3Spatrick       addr_description.chunk_access.user_requested_alignment;
78*3cab2bb3Spatrick   if (delete_alignment != user_alignment) {
79*3cab2bb3Spatrick     char user_alignment_str[32];
80*3cab2bb3Spatrick     char delete_alignment_str[32];
81*3cab2bb3Spatrick     internal_snprintf(user_alignment_str, sizeof(user_alignment_str),
82*3cab2bb3Spatrick                       "%zd bytes", user_alignment);
83*3cab2bb3Spatrick     internal_snprintf(delete_alignment_str, sizeof(delete_alignment_str),
84*3cab2bb3Spatrick                       "%zd bytes", delete_alignment);
85*3cab2bb3Spatrick     static const char *kDefaultAlignment = "default-aligned";
86*3cab2bb3Spatrick     Printf(
87*3cab2bb3Spatrick         "  alignment of the allocated type:   %s;\n"
88*3cab2bb3Spatrick         "  alignment of the deallocated type: %s.\n",
89*3cab2bb3Spatrick         user_alignment > 0 ? user_alignment_str : kDefaultAlignment,
90*3cab2bb3Spatrick         delete_alignment > 0 ? delete_alignment_str : kDefaultAlignment);
91*3cab2bb3Spatrick   }
92*3cab2bb3Spatrick   CHECK_GT(free_stack->size, 0);
93*3cab2bb3Spatrick   scariness.Print();
94*3cab2bb3Spatrick   GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
95*3cab2bb3Spatrick   stack.Print();
96*3cab2bb3Spatrick   addr_description.Print();
97*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), &stack);
98*3cab2bb3Spatrick   Report(
99*3cab2bb3Spatrick       "HINT: if you don't care about these errors you may set "
100*3cab2bb3Spatrick       "ASAN_OPTIONS=new_delete_type_mismatch=0\n");
101*3cab2bb3Spatrick }
102*3cab2bb3Spatrick 
103*3cab2bb3Spatrick void ErrorFreeNotMalloced::Print() {
104*3cab2bb3Spatrick   Decorator d;
105*3cab2bb3Spatrick   Printf("%s", d.Error());
106*3cab2bb3Spatrick   Report(
107*3cab2bb3Spatrick       "ERROR: AddressSanitizer: attempting free on address "
108*3cab2bb3Spatrick       "which was not malloc()-ed: %p in thread %s\n",
109*3cab2bb3Spatrick       addr_description.Address(), AsanThreadIdAndName(tid).c_str());
110*3cab2bb3Spatrick   Printf("%s", d.Default());
111*3cab2bb3Spatrick   CHECK_GT(free_stack->size, 0);
112*3cab2bb3Spatrick   scariness.Print();
113*3cab2bb3Spatrick   GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
114*3cab2bb3Spatrick   stack.Print();
115*3cab2bb3Spatrick   addr_description.Print();
116*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), &stack);
117*3cab2bb3Spatrick }
118*3cab2bb3Spatrick 
119*3cab2bb3Spatrick void ErrorAllocTypeMismatch::Print() {
120*3cab2bb3Spatrick   static const char *alloc_names[] = {"INVALID", "malloc", "operator new",
121*3cab2bb3Spatrick                                       "operator new []"};
122*3cab2bb3Spatrick   static const char *dealloc_names[] = {"INVALID", "free", "operator delete",
123*3cab2bb3Spatrick                                         "operator delete []"};
124*3cab2bb3Spatrick   CHECK_NE(alloc_type, dealloc_type);
125*3cab2bb3Spatrick   Decorator d;
126*3cab2bb3Spatrick   Printf("%s", d.Error());
127*3cab2bb3Spatrick   Report("ERROR: AddressSanitizer: %s (%s vs %s) on %p\n",
128*3cab2bb3Spatrick          scariness.GetDescription(), alloc_names[alloc_type],
129*3cab2bb3Spatrick          dealloc_names[dealloc_type], addr_description.Address());
130*3cab2bb3Spatrick   Printf("%s", d.Default());
131*3cab2bb3Spatrick   CHECK_GT(dealloc_stack->size, 0);
132*3cab2bb3Spatrick   scariness.Print();
133*3cab2bb3Spatrick   GET_STACK_TRACE_FATAL(dealloc_stack->trace[0], dealloc_stack->top_frame_bp);
134*3cab2bb3Spatrick   stack.Print();
135*3cab2bb3Spatrick   addr_description.Print();
136*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), &stack);
137*3cab2bb3Spatrick   Report(
138*3cab2bb3Spatrick       "HINT: if you don't care about these errors you may set "
139*3cab2bb3Spatrick       "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
140*3cab2bb3Spatrick }
141*3cab2bb3Spatrick 
142*3cab2bb3Spatrick void ErrorMallocUsableSizeNotOwned::Print() {
143*3cab2bb3Spatrick   Decorator d;
144*3cab2bb3Spatrick   Printf("%s", d.Error());
145*3cab2bb3Spatrick   Report(
146*3cab2bb3Spatrick       "ERROR: AddressSanitizer: attempting to call malloc_usable_size() for "
147*3cab2bb3Spatrick       "pointer which is not owned: %p\n",
148*3cab2bb3Spatrick       addr_description.Address());
149*3cab2bb3Spatrick   Printf("%s", d.Default());
150*3cab2bb3Spatrick   stack->Print();
151*3cab2bb3Spatrick   addr_description.Print();
152*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
153*3cab2bb3Spatrick }
154*3cab2bb3Spatrick 
155*3cab2bb3Spatrick void ErrorSanitizerGetAllocatedSizeNotOwned::Print() {
156*3cab2bb3Spatrick   Decorator d;
157*3cab2bb3Spatrick   Printf("%s", d.Error());
158*3cab2bb3Spatrick   Report(
159*3cab2bb3Spatrick       "ERROR: AddressSanitizer: attempting to call "
160*3cab2bb3Spatrick       "__sanitizer_get_allocated_size() for pointer which is not owned: %p\n",
161*3cab2bb3Spatrick       addr_description.Address());
162*3cab2bb3Spatrick   Printf("%s", d.Default());
163*3cab2bb3Spatrick   stack->Print();
164*3cab2bb3Spatrick   addr_description.Print();
165*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
166*3cab2bb3Spatrick }
167*3cab2bb3Spatrick 
168*3cab2bb3Spatrick void ErrorCallocOverflow::Print() {
169*3cab2bb3Spatrick   Decorator d;
170*3cab2bb3Spatrick   Printf("%s", d.Error());
171*3cab2bb3Spatrick   Report(
172*3cab2bb3Spatrick       "ERROR: AddressSanitizer: calloc parameters overflow: count * size "
173*3cab2bb3Spatrick       "(%zd * %zd) cannot be represented in type size_t (thread %s)\n",
174*3cab2bb3Spatrick       count, size, AsanThreadIdAndName(tid).c_str());
175*3cab2bb3Spatrick   Printf("%s", d.Default());
176*3cab2bb3Spatrick   stack->Print();
177*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
178*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
179*3cab2bb3Spatrick }
180*3cab2bb3Spatrick 
181*3cab2bb3Spatrick void ErrorReallocArrayOverflow::Print() {
182*3cab2bb3Spatrick   Decorator d;
183*3cab2bb3Spatrick   Printf("%s", d.Error());
184*3cab2bb3Spatrick   Report(
185*3cab2bb3Spatrick       "ERROR: AddressSanitizer: reallocarray parameters overflow: count * size "
186*3cab2bb3Spatrick       "(%zd * %zd) cannot be represented in type size_t (thread %s)\n",
187*3cab2bb3Spatrick       count, size, AsanThreadIdAndName(tid).c_str());
188*3cab2bb3Spatrick   Printf("%s", d.Default());
189*3cab2bb3Spatrick   stack->Print();
190*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
191*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
192*3cab2bb3Spatrick }
193*3cab2bb3Spatrick 
194*3cab2bb3Spatrick void ErrorPvallocOverflow::Print() {
195*3cab2bb3Spatrick   Decorator d;
196*3cab2bb3Spatrick   Printf("%s", d.Error());
197*3cab2bb3Spatrick   Report(
198*3cab2bb3Spatrick       "ERROR: AddressSanitizer: pvalloc parameters overflow: size 0x%zx "
199*3cab2bb3Spatrick       "rounded up to system page size 0x%zx cannot be represented in type "
200*3cab2bb3Spatrick       "size_t (thread %s)\n",
201*3cab2bb3Spatrick       size, GetPageSizeCached(), AsanThreadIdAndName(tid).c_str());
202*3cab2bb3Spatrick   Printf("%s", d.Default());
203*3cab2bb3Spatrick   stack->Print();
204*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
205*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
206*3cab2bb3Spatrick }
207*3cab2bb3Spatrick 
208*3cab2bb3Spatrick void ErrorInvalidAllocationAlignment::Print() {
209*3cab2bb3Spatrick   Decorator d;
210*3cab2bb3Spatrick   Printf("%s", d.Error());
211*3cab2bb3Spatrick   Report(
212*3cab2bb3Spatrick       "ERROR: AddressSanitizer: invalid allocation alignment: %zd, "
213*3cab2bb3Spatrick       "alignment must be a power of two (thread %s)\n",
214*3cab2bb3Spatrick       alignment, AsanThreadIdAndName(tid).c_str());
215*3cab2bb3Spatrick   Printf("%s", d.Default());
216*3cab2bb3Spatrick   stack->Print();
217*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
218*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
219*3cab2bb3Spatrick }
220*3cab2bb3Spatrick 
221*3cab2bb3Spatrick void ErrorInvalidAlignedAllocAlignment::Print() {
222*3cab2bb3Spatrick   Decorator d;
223*3cab2bb3Spatrick   Printf("%s", d.Error());
224*3cab2bb3Spatrick #if SANITIZER_POSIX
225*3cab2bb3Spatrick   Report("ERROR: AddressSanitizer: invalid alignment requested in "
226*3cab2bb3Spatrick          "aligned_alloc: %zd, alignment must be a power of two and the "
227*3cab2bb3Spatrick          "requested size 0x%zx must be a multiple of alignment "
228*3cab2bb3Spatrick          "(thread %s)\n", alignment, size, AsanThreadIdAndName(tid).c_str());
229*3cab2bb3Spatrick #else
230*3cab2bb3Spatrick   Report("ERROR: AddressSanitizer: invalid alignment requested in "
231*3cab2bb3Spatrick          "aligned_alloc: %zd, the requested size 0x%zx must be a multiple of "
232*3cab2bb3Spatrick          "alignment (thread %s)\n", alignment, size,
233*3cab2bb3Spatrick          AsanThreadIdAndName(tid).c_str());
234*3cab2bb3Spatrick #endif
235*3cab2bb3Spatrick   Printf("%s", d.Default());
236*3cab2bb3Spatrick   stack->Print();
237*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
238*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
239*3cab2bb3Spatrick }
240*3cab2bb3Spatrick 
241*3cab2bb3Spatrick void ErrorInvalidPosixMemalignAlignment::Print() {
242*3cab2bb3Spatrick   Decorator d;
243*3cab2bb3Spatrick   Printf("%s", d.Error());
244*3cab2bb3Spatrick   Report(
245*3cab2bb3Spatrick       "ERROR: AddressSanitizer: invalid alignment requested in posix_memalign: "
246*3cab2bb3Spatrick       "%zd, alignment must be a power of two and a multiple of sizeof(void*) "
247*3cab2bb3Spatrick       "== %zd (thread %s)\n",
248*3cab2bb3Spatrick       alignment, sizeof(void *), AsanThreadIdAndName(tid).c_str());
249*3cab2bb3Spatrick   Printf("%s", d.Default());
250*3cab2bb3Spatrick   stack->Print();
251*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
252*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
253*3cab2bb3Spatrick }
254*3cab2bb3Spatrick 
255*3cab2bb3Spatrick void ErrorAllocationSizeTooBig::Print() {
256*3cab2bb3Spatrick   Decorator d;
257*3cab2bb3Spatrick   Printf("%s", d.Error());
258*3cab2bb3Spatrick   Report(
259*3cab2bb3Spatrick       "ERROR: AddressSanitizer: requested allocation size 0x%zx (0x%zx after "
260*3cab2bb3Spatrick       "adjustments for alignment, red zones etc.) exceeds maximum supported "
261*3cab2bb3Spatrick       "size of 0x%zx (thread %s)\n",
262*3cab2bb3Spatrick       user_size, total_size, max_size, AsanThreadIdAndName(tid).c_str());
263*3cab2bb3Spatrick   Printf("%s", d.Default());
264*3cab2bb3Spatrick   stack->Print();
265*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
266*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
267*3cab2bb3Spatrick }
268*3cab2bb3Spatrick 
269*3cab2bb3Spatrick void ErrorRssLimitExceeded::Print() {
270*3cab2bb3Spatrick   Decorator d;
271*3cab2bb3Spatrick   Printf("%s", d.Error());
272*3cab2bb3Spatrick   Report(
273*3cab2bb3Spatrick       "ERROR: AddressSanitizer: specified RSS limit exceeded, currently set to "
274*3cab2bb3Spatrick       "soft_rss_limit_mb=%zd\n", common_flags()->soft_rss_limit_mb);
275*3cab2bb3Spatrick   Printf("%s", d.Default());
276*3cab2bb3Spatrick   stack->Print();
277*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
278*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
279*3cab2bb3Spatrick }
280*3cab2bb3Spatrick 
281*3cab2bb3Spatrick void ErrorOutOfMemory::Print() {
282*3cab2bb3Spatrick   Decorator d;
283*3cab2bb3Spatrick   Printf("%s", d.Error());
284*3cab2bb3Spatrick   Report(
285*3cab2bb3Spatrick       "ERROR: AddressSanitizer: allocator is out of memory trying to allocate "
286*3cab2bb3Spatrick       "0x%zx bytes\n", requested_size);
287*3cab2bb3Spatrick   Printf("%s", d.Default());
288*3cab2bb3Spatrick   stack->Print();
289*3cab2bb3Spatrick   PrintHintAllocatorCannotReturnNull();
290*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
291*3cab2bb3Spatrick }
292*3cab2bb3Spatrick 
293*3cab2bb3Spatrick void ErrorStringFunctionMemoryRangesOverlap::Print() {
294*3cab2bb3Spatrick   Decorator d;
295*3cab2bb3Spatrick   char bug_type[100];
296*3cab2bb3Spatrick   internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
297*3cab2bb3Spatrick   Printf("%s", d.Error());
298*3cab2bb3Spatrick   Report(
299*3cab2bb3Spatrick       "ERROR: AddressSanitizer: %s: memory ranges [%p,%p) and [%p, %p) "
300*3cab2bb3Spatrick       "overlap\n",
301*3cab2bb3Spatrick       bug_type, addr1_description.Address(),
302*3cab2bb3Spatrick       addr1_description.Address() + length1, addr2_description.Address(),
303*3cab2bb3Spatrick       addr2_description.Address() + length2);
304*3cab2bb3Spatrick   Printf("%s", d.Default());
305*3cab2bb3Spatrick   scariness.Print();
306*3cab2bb3Spatrick   stack->Print();
307*3cab2bb3Spatrick   addr1_description.Print();
308*3cab2bb3Spatrick   addr2_description.Print();
309*3cab2bb3Spatrick   ReportErrorSummary(bug_type, stack);
310*3cab2bb3Spatrick }
311*3cab2bb3Spatrick 
312*3cab2bb3Spatrick void ErrorStringFunctionSizeOverflow::Print() {
313*3cab2bb3Spatrick   Decorator d;
314*3cab2bb3Spatrick   Printf("%s", d.Error());
315*3cab2bb3Spatrick   Report("ERROR: AddressSanitizer: %s: (size=%zd)\n",
316*3cab2bb3Spatrick          scariness.GetDescription(), size);
317*3cab2bb3Spatrick   Printf("%s", d.Default());
318*3cab2bb3Spatrick   scariness.Print();
319*3cab2bb3Spatrick   stack->Print();
320*3cab2bb3Spatrick   addr_description.Print();
321*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
322*3cab2bb3Spatrick }
323*3cab2bb3Spatrick 
324*3cab2bb3Spatrick void ErrorBadParamsToAnnotateContiguousContainer::Print() {
325*3cab2bb3Spatrick   Report(
326*3cab2bb3Spatrick       "ERROR: AddressSanitizer: bad parameters to "
327*3cab2bb3Spatrick       "__sanitizer_annotate_contiguous_container:\n"
328*3cab2bb3Spatrick       "      beg     : %p\n"
329*3cab2bb3Spatrick       "      end     : %p\n"
330*3cab2bb3Spatrick       "      old_mid : %p\n"
331*3cab2bb3Spatrick       "      new_mid : %p\n",
332*3cab2bb3Spatrick       beg, end, old_mid, new_mid);
333*3cab2bb3Spatrick   uptr granularity = SHADOW_GRANULARITY;
334*3cab2bb3Spatrick   if (!IsAligned(beg, granularity))
335*3cab2bb3Spatrick     Report("ERROR: beg is not aligned by %d\n", granularity);
336*3cab2bb3Spatrick   stack->Print();
337*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), stack);
338*3cab2bb3Spatrick }
339*3cab2bb3Spatrick 
340*3cab2bb3Spatrick void ErrorODRViolation::Print() {
341*3cab2bb3Spatrick   Decorator d;
342*3cab2bb3Spatrick   Printf("%s", d.Error());
343*3cab2bb3Spatrick   Report("ERROR: AddressSanitizer: %s (%p):\n", scariness.GetDescription(),
344*3cab2bb3Spatrick          global1.beg);
345*3cab2bb3Spatrick   Printf("%s", d.Default());
346*3cab2bb3Spatrick   InternalScopedString g1_loc(256), g2_loc(256);
347*3cab2bb3Spatrick   PrintGlobalLocation(&g1_loc, global1);
348*3cab2bb3Spatrick   PrintGlobalLocation(&g2_loc, global2);
349*3cab2bb3Spatrick   Printf("  [1] size=%zd '%s' %s\n", global1.size,
350*3cab2bb3Spatrick          MaybeDemangleGlobalName(global1.name), g1_loc.data());
351*3cab2bb3Spatrick   Printf("  [2] size=%zd '%s' %s\n", global2.size,
352*3cab2bb3Spatrick          MaybeDemangleGlobalName(global2.name), g2_loc.data());
353*3cab2bb3Spatrick   if (stack_id1 && stack_id2) {
354*3cab2bb3Spatrick     Printf("These globals were registered at these points:\n");
355*3cab2bb3Spatrick     Printf("  [1]:\n");
356*3cab2bb3Spatrick     StackDepotGet(stack_id1).Print();
357*3cab2bb3Spatrick     Printf("  [2]:\n");
358*3cab2bb3Spatrick     StackDepotGet(stack_id2).Print();
359*3cab2bb3Spatrick   }
360*3cab2bb3Spatrick   Report(
361*3cab2bb3Spatrick       "HINT: if you don't care about these errors you may set "
362*3cab2bb3Spatrick       "ASAN_OPTIONS=detect_odr_violation=0\n");
363*3cab2bb3Spatrick   InternalScopedString error_msg(256);
364*3cab2bb3Spatrick   error_msg.append("%s: global '%s' at %s", scariness.GetDescription(),
365*3cab2bb3Spatrick                    MaybeDemangleGlobalName(global1.name), g1_loc.data());
366*3cab2bb3Spatrick   ReportErrorSummary(error_msg.data());
367*3cab2bb3Spatrick }
368*3cab2bb3Spatrick 
369*3cab2bb3Spatrick void ErrorInvalidPointerPair::Print() {
370*3cab2bb3Spatrick   Decorator d;
371*3cab2bb3Spatrick   Printf("%s", d.Error());
372*3cab2bb3Spatrick   Report("ERROR: AddressSanitizer: %s: %p %p\n", scariness.GetDescription(),
373*3cab2bb3Spatrick          addr1_description.Address(), addr2_description.Address());
374*3cab2bb3Spatrick   Printf("%s", d.Default());
375*3cab2bb3Spatrick   GET_STACK_TRACE_FATAL(pc, bp);
376*3cab2bb3Spatrick   stack.Print();
377*3cab2bb3Spatrick   addr1_description.Print();
378*3cab2bb3Spatrick   addr2_description.Print();
379*3cab2bb3Spatrick   ReportErrorSummary(scariness.GetDescription(), &stack);
380*3cab2bb3Spatrick }
381*3cab2bb3Spatrick 
382*3cab2bb3Spatrick static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {
383*3cab2bb3Spatrick   return s[-1] > 127 && s[1] > 127;
384*3cab2bb3Spatrick }
385*3cab2bb3Spatrick 
386*3cab2bb3Spatrick ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
387*3cab2bb3Spatrick                            bool is_write_, uptr access_size_)
388*3cab2bb3Spatrick     : ErrorBase(tid),
389*3cab2bb3Spatrick       addr_description(addr, access_size_, /*shouldLockThreadRegistry=*/false),
390*3cab2bb3Spatrick       pc(pc_),
391*3cab2bb3Spatrick       bp(bp_),
392*3cab2bb3Spatrick       sp(sp_),
393*3cab2bb3Spatrick       access_size(access_size_),
394*3cab2bb3Spatrick       is_write(is_write_),
395*3cab2bb3Spatrick       shadow_val(0) {
396*3cab2bb3Spatrick   scariness.Clear();
397*3cab2bb3Spatrick   if (access_size) {
398*3cab2bb3Spatrick     if (access_size <= 9) {
399*3cab2bb3Spatrick       char desr[] = "?-byte";
400*3cab2bb3Spatrick       desr[0] = '0' + access_size;
401*3cab2bb3Spatrick       scariness.Scare(access_size + access_size / 2, desr);
402*3cab2bb3Spatrick     } else if (access_size >= 10) {
403*3cab2bb3Spatrick       scariness.Scare(15, "multi-byte");
404*3cab2bb3Spatrick     }
405*3cab2bb3Spatrick     is_write ? scariness.Scare(20, "write") : scariness.Scare(1, "read");
406*3cab2bb3Spatrick 
407*3cab2bb3Spatrick     // Determine the error type.
408*3cab2bb3Spatrick     bug_descr = "unknown-crash";
409*3cab2bb3Spatrick     if (AddrIsInMem(addr)) {
410*3cab2bb3Spatrick       u8 *shadow_addr = (u8 *)MemToShadow(addr);
411*3cab2bb3Spatrick       // If we are accessing 16 bytes, look at the second shadow byte.
412*3cab2bb3Spatrick       if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY) shadow_addr++;
413*3cab2bb3Spatrick       // If we are in the partial right redzone, look at the next shadow byte.
414*3cab2bb3Spatrick       if (*shadow_addr > 0 && *shadow_addr < 128) shadow_addr++;
415*3cab2bb3Spatrick       bool far_from_bounds = false;
416*3cab2bb3Spatrick       shadow_val = *shadow_addr;
417*3cab2bb3Spatrick       int bug_type_score = 0;
418*3cab2bb3Spatrick       // For use-after-frees reads are almost as bad as writes.
419*3cab2bb3Spatrick       int read_after_free_bonus = 0;
420*3cab2bb3Spatrick       switch (shadow_val) {
421*3cab2bb3Spatrick         case kAsanHeapLeftRedzoneMagic:
422*3cab2bb3Spatrick         case kAsanArrayCookieMagic:
423*3cab2bb3Spatrick           bug_descr = "heap-buffer-overflow";
424*3cab2bb3Spatrick           bug_type_score = 10;
425*3cab2bb3Spatrick           far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
426*3cab2bb3Spatrick           break;
427*3cab2bb3Spatrick         case kAsanHeapFreeMagic:
428*3cab2bb3Spatrick           bug_descr = "heap-use-after-free";
429*3cab2bb3Spatrick           bug_type_score = 20;
430*3cab2bb3Spatrick           if (!is_write) read_after_free_bonus = 18;
431*3cab2bb3Spatrick           break;
432*3cab2bb3Spatrick         case kAsanStackLeftRedzoneMagic:
433*3cab2bb3Spatrick           bug_descr = "stack-buffer-underflow";
434*3cab2bb3Spatrick           bug_type_score = 25;
435*3cab2bb3Spatrick           far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
436*3cab2bb3Spatrick           break;
437*3cab2bb3Spatrick         case kAsanInitializationOrderMagic:
438*3cab2bb3Spatrick           bug_descr = "initialization-order-fiasco";
439*3cab2bb3Spatrick           bug_type_score = 1;
440*3cab2bb3Spatrick           break;
441*3cab2bb3Spatrick         case kAsanStackMidRedzoneMagic:
442*3cab2bb3Spatrick         case kAsanStackRightRedzoneMagic:
443*3cab2bb3Spatrick           bug_descr = "stack-buffer-overflow";
444*3cab2bb3Spatrick           bug_type_score = 25;
445*3cab2bb3Spatrick           far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
446*3cab2bb3Spatrick           break;
447*3cab2bb3Spatrick         case kAsanStackAfterReturnMagic:
448*3cab2bb3Spatrick           bug_descr = "stack-use-after-return";
449*3cab2bb3Spatrick           bug_type_score = 30;
450*3cab2bb3Spatrick           if (!is_write) read_after_free_bonus = 18;
451*3cab2bb3Spatrick           break;
452*3cab2bb3Spatrick         case kAsanUserPoisonedMemoryMagic:
453*3cab2bb3Spatrick           bug_descr = "use-after-poison";
454*3cab2bb3Spatrick           bug_type_score = 20;
455*3cab2bb3Spatrick           break;
456*3cab2bb3Spatrick         case kAsanContiguousContainerOOBMagic:
457*3cab2bb3Spatrick           bug_descr = "container-overflow";
458*3cab2bb3Spatrick           bug_type_score = 10;
459*3cab2bb3Spatrick           break;
460*3cab2bb3Spatrick         case kAsanStackUseAfterScopeMagic:
461*3cab2bb3Spatrick           bug_descr = "stack-use-after-scope";
462*3cab2bb3Spatrick           bug_type_score = 10;
463*3cab2bb3Spatrick           break;
464*3cab2bb3Spatrick         case kAsanGlobalRedzoneMagic:
465*3cab2bb3Spatrick           bug_descr = "global-buffer-overflow";
466*3cab2bb3Spatrick           bug_type_score = 10;
467*3cab2bb3Spatrick           far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
468*3cab2bb3Spatrick           break;
469*3cab2bb3Spatrick         case kAsanIntraObjectRedzone:
470*3cab2bb3Spatrick           bug_descr = "intra-object-overflow";
471*3cab2bb3Spatrick           bug_type_score = 10;
472*3cab2bb3Spatrick           break;
473*3cab2bb3Spatrick         case kAsanAllocaLeftMagic:
474*3cab2bb3Spatrick         case kAsanAllocaRightMagic:
475*3cab2bb3Spatrick           bug_descr = "dynamic-stack-buffer-overflow";
476*3cab2bb3Spatrick           bug_type_score = 25;
477*3cab2bb3Spatrick           far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
478*3cab2bb3Spatrick           break;
479*3cab2bb3Spatrick       }
480*3cab2bb3Spatrick       scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
481*3cab2bb3Spatrick       if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
482*3cab2bb3Spatrick     }
483*3cab2bb3Spatrick   }
484*3cab2bb3Spatrick }
485*3cab2bb3Spatrick 
486*3cab2bb3Spatrick static void PrintContainerOverflowHint() {
487*3cab2bb3Spatrick   Printf("HINT: if you don't care about these errors you may set "
488*3cab2bb3Spatrick          "ASAN_OPTIONS=detect_container_overflow=0.\n"
489*3cab2bb3Spatrick          "If you suspect a false positive see also: "
490*3cab2bb3Spatrick          "https://github.com/google/sanitizers/wiki/"
491*3cab2bb3Spatrick          "AddressSanitizerContainerOverflow.\n");
492*3cab2bb3Spatrick }
493*3cab2bb3Spatrick 
494*3cab2bb3Spatrick static void PrintShadowByte(InternalScopedString *str, const char *before,
495*3cab2bb3Spatrick     u8 byte, const char *after = "\n") {
496*3cab2bb3Spatrick   PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
497*3cab2bb3Spatrick }
498*3cab2bb3Spatrick 
499*3cab2bb3Spatrick static void PrintLegend(InternalScopedString *str) {
500*3cab2bb3Spatrick   str->append(
501*3cab2bb3Spatrick       "Shadow byte legend (one shadow byte represents %d "
502*3cab2bb3Spatrick       "application bytes):\n",
503*3cab2bb3Spatrick       (int)SHADOW_GRANULARITY);
504*3cab2bb3Spatrick   PrintShadowByte(str, "  Addressable:           ", 0);
505*3cab2bb3Spatrick   str->append("  Partially addressable: ");
506*3cab2bb3Spatrick   for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " ");
507*3cab2bb3Spatrick   str->append("\n");
508*3cab2bb3Spatrick   PrintShadowByte(str, "  Heap left redzone:       ",
509*3cab2bb3Spatrick                   kAsanHeapLeftRedzoneMagic);
510*3cab2bb3Spatrick   PrintShadowByte(str, "  Freed heap region:       ", kAsanHeapFreeMagic);
511*3cab2bb3Spatrick   PrintShadowByte(str, "  Stack left redzone:      ",
512*3cab2bb3Spatrick                   kAsanStackLeftRedzoneMagic);
513*3cab2bb3Spatrick   PrintShadowByte(str, "  Stack mid redzone:       ",
514*3cab2bb3Spatrick                   kAsanStackMidRedzoneMagic);
515*3cab2bb3Spatrick   PrintShadowByte(str, "  Stack right redzone:     ",
516*3cab2bb3Spatrick                   kAsanStackRightRedzoneMagic);
517*3cab2bb3Spatrick   PrintShadowByte(str, "  Stack after return:      ",
518*3cab2bb3Spatrick                   kAsanStackAfterReturnMagic);
519*3cab2bb3Spatrick   PrintShadowByte(str, "  Stack use after scope:   ",
520*3cab2bb3Spatrick                   kAsanStackUseAfterScopeMagic);
521*3cab2bb3Spatrick   PrintShadowByte(str, "  Global redzone:          ", kAsanGlobalRedzoneMagic);
522*3cab2bb3Spatrick   PrintShadowByte(str, "  Global init order:       ",
523*3cab2bb3Spatrick                   kAsanInitializationOrderMagic);
524*3cab2bb3Spatrick   PrintShadowByte(str, "  Poisoned by user:        ",
525*3cab2bb3Spatrick                   kAsanUserPoisonedMemoryMagic);
526*3cab2bb3Spatrick   PrintShadowByte(str, "  Container overflow:      ",
527*3cab2bb3Spatrick                   kAsanContiguousContainerOOBMagic);
528*3cab2bb3Spatrick   PrintShadowByte(str, "  Array cookie:            ",
529*3cab2bb3Spatrick                   kAsanArrayCookieMagic);
530*3cab2bb3Spatrick   PrintShadowByte(str, "  Intra object redzone:    ",
531*3cab2bb3Spatrick                   kAsanIntraObjectRedzone);
532*3cab2bb3Spatrick   PrintShadowByte(str, "  ASan internal:           ", kAsanInternalHeapMagic);
533*3cab2bb3Spatrick   PrintShadowByte(str, "  Left alloca redzone:     ", kAsanAllocaLeftMagic);
534*3cab2bb3Spatrick   PrintShadowByte(str, "  Right alloca redzone:    ", kAsanAllocaRightMagic);
535*3cab2bb3Spatrick   PrintShadowByte(str, "  Shadow gap:              ", kAsanShadowGap);
536*3cab2bb3Spatrick }
537*3cab2bb3Spatrick 
538*3cab2bb3Spatrick static void PrintShadowBytes(InternalScopedString *str, const char *before,
539*3cab2bb3Spatrick                              u8 *bytes, u8 *guilty, uptr n) {
540*3cab2bb3Spatrick   Decorator d;
541*3cab2bb3Spatrick   if (before) str->append("%s%p:", before, bytes);
542*3cab2bb3Spatrick   for (uptr i = 0; i < n; i++) {
543*3cab2bb3Spatrick     u8 *p = bytes + i;
544*3cab2bb3Spatrick     const char *before =
545*3cab2bb3Spatrick         p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
546*3cab2bb3Spatrick     const char *after = p == guilty ? "]" : "";
547*3cab2bb3Spatrick     PrintShadowByte(str, before, *p, after);
548*3cab2bb3Spatrick   }
549*3cab2bb3Spatrick   str->append("\n");
550*3cab2bb3Spatrick }
551*3cab2bb3Spatrick 
552*3cab2bb3Spatrick static void PrintShadowMemoryForAddress(uptr addr) {
553*3cab2bb3Spatrick   if (!AddrIsInMem(addr)) return;
554*3cab2bb3Spatrick   uptr shadow_addr = MemToShadow(addr);
555*3cab2bb3Spatrick   const uptr n_bytes_per_row = 16;
556*3cab2bb3Spatrick   uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
557*3cab2bb3Spatrick   InternalScopedString str(4096 * 8);
558*3cab2bb3Spatrick   str.append("Shadow bytes around the buggy address:\n");
559*3cab2bb3Spatrick   for (int i = -5; i <= 5; i++) {
560*3cab2bb3Spatrick     uptr row_shadow_addr = aligned_shadow + i * n_bytes_per_row;
561*3cab2bb3Spatrick     // Skip rows that would be outside the shadow range. This can happen when
562*3cab2bb3Spatrick     // the user address is near the bottom, top, or shadow gap of the address
563*3cab2bb3Spatrick     // space.
564*3cab2bb3Spatrick     if (!AddrIsInShadow(row_shadow_addr)) continue;
565*3cab2bb3Spatrick     const char *prefix = (i == 0) ? "=>" : "  ";
566*3cab2bb3Spatrick     PrintShadowBytes(&str, prefix, (u8 *)row_shadow_addr, (u8 *)shadow_addr,
567*3cab2bb3Spatrick                      n_bytes_per_row);
568*3cab2bb3Spatrick   }
569*3cab2bb3Spatrick   if (flags()->print_legend) PrintLegend(&str);
570*3cab2bb3Spatrick   Printf("%s", str.data());
571*3cab2bb3Spatrick }
572*3cab2bb3Spatrick 
573*3cab2bb3Spatrick void ErrorGeneric::Print() {
574*3cab2bb3Spatrick   Decorator d;
575*3cab2bb3Spatrick   Printf("%s", d.Error());
576*3cab2bb3Spatrick   uptr addr = addr_description.Address();
577*3cab2bb3Spatrick   Report("ERROR: AddressSanitizer: %s on address %p at pc %p bp %p sp %p\n",
578*3cab2bb3Spatrick          bug_descr, (void *)addr, pc, bp, sp);
579*3cab2bb3Spatrick   Printf("%s", d.Default());
580*3cab2bb3Spatrick 
581*3cab2bb3Spatrick   Printf("%s%s of size %zu at %p thread %s%s\n", d.Access(),
582*3cab2bb3Spatrick          access_size ? (is_write ? "WRITE" : "READ") : "ACCESS", access_size,
583*3cab2bb3Spatrick          (void *)addr, AsanThreadIdAndName(tid).c_str(), d.Default());
584*3cab2bb3Spatrick 
585*3cab2bb3Spatrick   scariness.Print();
586*3cab2bb3Spatrick   GET_STACK_TRACE_FATAL(pc, bp);
587*3cab2bb3Spatrick   stack.Print();
588*3cab2bb3Spatrick 
589*3cab2bb3Spatrick   // Pass bug_descr because we have a special case for
590*3cab2bb3Spatrick   // initialization-order-fiasco
591*3cab2bb3Spatrick   addr_description.Print(bug_descr);
592*3cab2bb3Spatrick   if (shadow_val == kAsanContiguousContainerOOBMagic)
593*3cab2bb3Spatrick     PrintContainerOverflowHint();
594*3cab2bb3Spatrick   ReportErrorSummary(bug_descr, &stack);
595*3cab2bb3Spatrick   PrintShadowMemoryForAddress(addr);
596*3cab2bb3Spatrick }
597*3cab2bb3Spatrick 
598*3cab2bb3Spatrick }  // namespace __asan
599