xref: /aosp_15_r20/external/compiler-rt/lib/asan/asan_report.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- asan_report.cc ----------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of AddressSanitizer, an address sanity checker.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // This file contains error reporting code.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot 
15*7c3d14c8STreehugger Robot #include "asan_flags.h"
16*7c3d14c8STreehugger Robot #include "asan_internal.h"
17*7c3d14c8STreehugger Robot #include "asan_mapping.h"
18*7c3d14c8STreehugger Robot #include "asan_report.h"
19*7c3d14c8STreehugger Robot #include "asan_scariness_score.h"
20*7c3d14c8STreehugger Robot #include "asan_stack.h"
21*7c3d14c8STreehugger Robot #include "asan_thread.h"
22*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
23*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_flags.h"
24*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_report_decorator.h"
25*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_stackdepot.h"
26*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_symbolizer.h"
27*7c3d14c8STreehugger Robot 
28*7c3d14c8STreehugger Robot namespace __asan {
29*7c3d14c8STreehugger Robot 
30*7c3d14c8STreehugger Robot // -------------------- User-specified callbacks ----------------- {{{1
31*7c3d14c8STreehugger Robot static void (*error_report_callback)(const char*);
32*7c3d14c8STreehugger Robot static char *error_message_buffer = nullptr;
33*7c3d14c8STreehugger Robot static uptr error_message_buffer_pos = 0;
34*7c3d14c8STreehugger Robot static BlockingMutex error_message_buf_mutex(LINKER_INITIALIZED);
35*7c3d14c8STreehugger Robot static const unsigned kAsanBuggyPcPoolSize = 25;
36*7c3d14c8STreehugger Robot static __sanitizer::atomic_uintptr_t AsanBuggyPcPool[kAsanBuggyPcPoolSize];
37*7c3d14c8STreehugger Robot 
38*7c3d14c8STreehugger Robot struct ReportData {
39*7c3d14c8STreehugger Robot   uptr pc;
40*7c3d14c8STreehugger Robot   uptr sp;
41*7c3d14c8STreehugger Robot   uptr bp;
42*7c3d14c8STreehugger Robot   uptr addr;
43*7c3d14c8STreehugger Robot   bool is_write;
44*7c3d14c8STreehugger Robot   uptr access_size;
45*7c3d14c8STreehugger Robot   const char *description;
46*7c3d14c8STreehugger Robot };
47*7c3d14c8STreehugger Robot 
48*7c3d14c8STreehugger Robot static bool report_happened = false;
49*7c3d14c8STreehugger Robot static ReportData report_data = {};
50*7c3d14c8STreehugger Robot 
AppendToErrorMessageBuffer(const char * buffer)51*7c3d14c8STreehugger Robot void AppendToErrorMessageBuffer(const char *buffer) {
52*7c3d14c8STreehugger Robot   BlockingMutexLock l(&error_message_buf_mutex);
53*7c3d14c8STreehugger Robot   if (!error_message_buffer) {
54*7c3d14c8STreehugger Robot     error_message_buffer =
55*7c3d14c8STreehugger Robot       (char*)MmapOrDieQuietly(kErrorMessageBufferSize, __func__);
56*7c3d14c8STreehugger Robot     error_message_buffer_pos = 0;
57*7c3d14c8STreehugger Robot   }
58*7c3d14c8STreehugger Robot   uptr length = internal_strlen(buffer);
59*7c3d14c8STreehugger Robot   RAW_CHECK(kErrorMessageBufferSize >= error_message_buffer_pos);
60*7c3d14c8STreehugger Robot   uptr remaining = kErrorMessageBufferSize - error_message_buffer_pos;
61*7c3d14c8STreehugger Robot   internal_strncpy(error_message_buffer + error_message_buffer_pos,
62*7c3d14c8STreehugger Robot                    buffer, remaining);
63*7c3d14c8STreehugger Robot   error_message_buffer[kErrorMessageBufferSize - 1] = '\0';
64*7c3d14c8STreehugger Robot   // FIXME: reallocate the buffer instead of truncating the message.
65*7c3d14c8STreehugger Robot   error_message_buffer_pos += Min(remaining, length);
66*7c3d14c8STreehugger Robot }
67*7c3d14c8STreehugger Robot 
68*7c3d14c8STreehugger Robot // ---------------------- Decorator ------------------------------ {{{1
69*7c3d14c8STreehugger Robot class Decorator: public __sanitizer::SanitizerCommonDecorator {
70*7c3d14c8STreehugger Robot  public:
Decorator()71*7c3d14c8STreehugger Robot   Decorator() : SanitizerCommonDecorator() { }
Access()72*7c3d14c8STreehugger Robot   const char *Access()     { return Blue(); }
EndAccess()73*7c3d14c8STreehugger Robot   const char *EndAccess()  { return Default(); }
Location()74*7c3d14c8STreehugger Robot   const char *Location()   { return Green(); }
EndLocation()75*7c3d14c8STreehugger Robot   const char *EndLocation() { return Default(); }
Allocation()76*7c3d14c8STreehugger Robot   const char *Allocation()  { return Magenta(); }
EndAllocation()77*7c3d14c8STreehugger Robot   const char *EndAllocation()  { return Default(); }
78*7c3d14c8STreehugger Robot 
ShadowByte(u8 byte)79*7c3d14c8STreehugger Robot   const char *ShadowByte(u8 byte) {
80*7c3d14c8STreehugger Robot     switch (byte) {
81*7c3d14c8STreehugger Robot       case kAsanHeapLeftRedzoneMagic:
82*7c3d14c8STreehugger Robot       case kAsanHeapRightRedzoneMagic:
83*7c3d14c8STreehugger Robot       case kAsanArrayCookieMagic:
84*7c3d14c8STreehugger Robot         return Red();
85*7c3d14c8STreehugger Robot       case kAsanHeapFreeMagic:
86*7c3d14c8STreehugger Robot         return Magenta();
87*7c3d14c8STreehugger Robot       case kAsanStackLeftRedzoneMagic:
88*7c3d14c8STreehugger Robot       case kAsanStackMidRedzoneMagic:
89*7c3d14c8STreehugger Robot       case kAsanStackRightRedzoneMagic:
90*7c3d14c8STreehugger Robot       case kAsanStackPartialRedzoneMagic:
91*7c3d14c8STreehugger Robot         return Red();
92*7c3d14c8STreehugger Robot       case kAsanStackAfterReturnMagic:
93*7c3d14c8STreehugger Robot         return Magenta();
94*7c3d14c8STreehugger Robot       case kAsanInitializationOrderMagic:
95*7c3d14c8STreehugger Robot         return Cyan();
96*7c3d14c8STreehugger Robot       case kAsanUserPoisonedMemoryMagic:
97*7c3d14c8STreehugger Robot       case kAsanContiguousContainerOOBMagic:
98*7c3d14c8STreehugger Robot       case kAsanAllocaLeftMagic:
99*7c3d14c8STreehugger Robot       case kAsanAllocaRightMagic:
100*7c3d14c8STreehugger Robot         return Blue();
101*7c3d14c8STreehugger Robot       case kAsanStackUseAfterScopeMagic:
102*7c3d14c8STreehugger Robot         return Magenta();
103*7c3d14c8STreehugger Robot       case kAsanGlobalRedzoneMagic:
104*7c3d14c8STreehugger Robot         return Red();
105*7c3d14c8STreehugger Robot       case kAsanInternalHeapMagic:
106*7c3d14c8STreehugger Robot         return Yellow();
107*7c3d14c8STreehugger Robot       case kAsanIntraObjectRedzone:
108*7c3d14c8STreehugger Robot         return Yellow();
109*7c3d14c8STreehugger Robot       default:
110*7c3d14c8STreehugger Robot         return Default();
111*7c3d14c8STreehugger Robot     }
112*7c3d14c8STreehugger Robot   }
EndShadowByte()113*7c3d14c8STreehugger Robot   const char *EndShadowByte() { return Default(); }
MemoryByte()114*7c3d14c8STreehugger Robot   const char *MemoryByte() { return Magenta(); }
EndMemoryByte()115*7c3d14c8STreehugger Robot   const char *EndMemoryByte() { return Default(); }
116*7c3d14c8STreehugger Robot };
117*7c3d14c8STreehugger Robot 
118*7c3d14c8STreehugger Robot // ---------------------- Helper functions ----------------------- {{{1
119*7c3d14c8STreehugger Robot 
PrintMemoryByte(InternalScopedString * str,const char * before,u8 byte,bool in_shadow,const char * after="\\n")120*7c3d14c8STreehugger Robot static void PrintMemoryByte(InternalScopedString *str, const char *before,
121*7c3d14c8STreehugger Robot     u8 byte, bool in_shadow, const char *after = "\n") {
122*7c3d14c8STreehugger Robot   Decorator d;
123*7c3d14c8STreehugger Robot   str->append("%s%s%x%x%s%s", before,
124*7c3d14c8STreehugger Robot               in_shadow ? d.ShadowByte(byte) : d.MemoryByte(),
125*7c3d14c8STreehugger Robot               byte >> 4, byte & 15,
126*7c3d14c8STreehugger Robot               in_shadow ? d.EndShadowByte() : d.EndMemoryByte(), after);
127*7c3d14c8STreehugger Robot }
128*7c3d14c8STreehugger Robot 
PrintShadowByte(InternalScopedString * str,const char * before,u8 byte,const char * after="\\n")129*7c3d14c8STreehugger Robot static void PrintShadowByte(InternalScopedString *str, const char *before,
130*7c3d14c8STreehugger Robot     u8 byte, const char *after = "\n") {
131*7c3d14c8STreehugger Robot   PrintMemoryByte(str, before, byte, /*in_shadow*/true, after);
132*7c3d14c8STreehugger Robot }
133*7c3d14c8STreehugger Robot 
PrintShadowBytes(InternalScopedString * str,const char * before,u8 * bytes,u8 * guilty,uptr n)134*7c3d14c8STreehugger Robot static void PrintShadowBytes(InternalScopedString *str, const char *before,
135*7c3d14c8STreehugger Robot                              u8 *bytes, u8 *guilty, uptr n) {
136*7c3d14c8STreehugger Robot   Decorator d;
137*7c3d14c8STreehugger Robot   if (before) str->append("%s%p:", before, bytes);
138*7c3d14c8STreehugger Robot   for (uptr i = 0; i < n; i++) {
139*7c3d14c8STreehugger Robot     u8 *p = bytes + i;
140*7c3d14c8STreehugger Robot     const char *before =
141*7c3d14c8STreehugger Robot         p == guilty ? "[" : (p - 1 == guilty && i != 0) ? "" : " ";
142*7c3d14c8STreehugger Robot     const char *after = p == guilty ? "]" : "";
143*7c3d14c8STreehugger Robot     PrintShadowByte(str, before, *p, after);
144*7c3d14c8STreehugger Robot   }
145*7c3d14c8STreehugger Robot   str->append("\n");
146*7c3d14c8STreehugger Robot }
147*7c3d14c8STreehugger Robot 
PrintLegend(InternalScopedString * str)148*7c3d14c8STreehugger Robot static void PrintLegend(InternalScopedString *str) {
149*7c3d14c8STreehugger Robot   str->append(
150*7c3d14c8STreehugger Robot       "Shadow byte legend (one shadow byte represents %d "
151*7c3d14c8STreehugger Robot       "application bytes):\n",
152*7c3d14c8STreehugger Robot       (int)SHADOW_GRANULARITY);
153*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Addressable:           ", 0);
154*7c3d14c8STreehugger Robot   str->append("  Partially addressable: ");
155*7c3d14c8STreehugger Robot   for (u8 i = 1; i < SHADOW_GRANULARITY; i++) PrintShadowByte(str, "", i, " ");
156*7c3d14c8STreehugger Robot   str->append("\n");
157*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Heap left redzone:       ",
158*7c3d14c8STreehugger Robot                   kAsanHeapLeftRedzoneMagic);
159*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Heap right redzone:      ",
160*7c3d14c8STreehugger Robot                   kAsanHeapRightRedzoneMagic);
161*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Freed heap region:       ", kAsanHeapFreeMagic);
162*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Stack left redzone:      ",
163*7c3d14c8STreehugger Robot                   kAsanStackLeftRedzoneMagic);
164*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Stack mid redzone:       ",
165*7c3d14c8STreehugger Robot                   kAsanStackMidRedzoneMagic);
166*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Stack right redzone:     ",
167*7c3d14c8STreehugger Robot                   kAsanStackRightRedzoneMagic);
168*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Stack partial redzone:   ",
169*7c3d14c8STreehugger Robot                   kAsanStackPartialRedzoneMagic);
170*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Stack after return:      ",
171*7c3d14c8STreehugger Robot                   kAsanStackAfterReturnMagic);
172*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Stack use after scope:   ",
173*7c3d14c8STreehugger Robot                   kAsanStackUseAfterScopeMagic);
174*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Global redzone:          ", kAsanGlobalRedzoneMagic);
175*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Global init order:       ",
176*7c3d14c8STreehugger Robot                   kAsanInitializationOrderMagic);
177*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Poisoned by user:        ",
178*7c3d14c8STreehugger Robot                   kAsanUserPoisonedMemoryMagic);
179*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Container overflow:      ",
180*7c3d14c8STreehugger Robot                   kAsanContiguousContainerOOBMagic);
181*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Array cookie:            ",
182*7c3d14c8STreehugger Robot                   kAsanArrayCookieMagic);
183*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Intra object redzone:    ",
184*7c3d14c8STreehugger Robot                   kAsanIntraObjectRedzone);
185*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  ASan internal:           ", kAsanInternalHeapMagic);
186*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Left alloca redzone:     ", kAsanAllocaLeftMagic);
187*7c3d14c8STreehugger Robot   PrintShadowByte(str, "  Right alloca redzone:    ", kAsanAllocaRightMagic);
188*7c3d14c8STreehugger Robot }
189*7c3d14c8STreehugger Robot 
MaybeDumpInstructionBytes(uptr pc)190*7c3d14c8STreehugger Robot void MaybeDumpInstructionBytes(uptr pc) {
191*7c3d14c8STreehugger Robot   if (!flags()->dump_instruction_bytes || (pc < GetPageSizeCached()))
192*7c3d14c8STreehugger Robot     return;
193*7c3d14c8STreehugger Robot   InternalScopedString str(1024);
194*7c3d14c8STreehugger Robot   str.append("First 16 instruction bytes at pc: ");
195*7c3d14c8STreehugger Robot   if (IsAccessibleMemoryRange(pc, 16)) {
196*7c3d14c8STreehugger Robot     for (int i = 0; i < 16; ++i) {
197*7c3d14c8STreehugger Robot       PrintMemoryByte(&str, "", ((u8 *)pc)[i], /*in_shadow*/false, " ");
198*7c3d14c8STreehugger Robot     }
199*7c3d14c8STreehugger Robot     str.append("\n");
200*7c3d14c8STreehugger Robot   } else {
201*7c3d14c8STreehugger Robot     str.append("unaccessible\n");
202*7c3d14c8STreehugger Robot   }
203*7c3d14c8STreehugger Robot   Report("%s", str.data());
204*7c3d14c8STreehugger Robot }
205*7c3d14c8STreehugger Robot 
PrintShadowMemoryForAddress(uptr addr)206*7c3d14c8STreehugger Robot static void PrintShadowMemoryForAddress(uptr addr) {
207*7c3d14c8STreehugger Robot   if (!AddrIsInMem(addr)) return;
208*7c3d14c8STreehugger Robot   uptr shadow_addr = MemToShadow(addr);
209*7c3d14c8STreehugger Robot   const uptr n_bytes_per_row = 16;
210*7c3d14c8STreehugger Robot   uptr aligned_shadow = shadow_addr & ~(n_bytes_per_row - 1);
211*7c3d14c8STreehugger Robot   InternalScopedString str(4096 * 8);
212*7c3d14c8STreehugger Robot   str.append("Shadow bytes around the buggy address:\n");
213*7c3d14c8STreehugger Robot   for (int i = -5; i <= 5; i++) {
214*7c3d14c8STreehugger Robot     const char *prefix = (i == 0) ? "=>" : "  ";
215*7c3d14c8STreehugger Robot     PrintShadowBytes(&str, prefix, (u8 *)(aligned_shadow + i * n_bytes_per_row),
216*7c3d14c8STreehugger Robot                      (u8 *)shadow_addr, n_bytes_per_row);
217*7c3d14c8STreehugger Robot   }
218*7c3d14c8STreehugger Robot   if (flags()->print_legend) PrintLegend(&str);
219*7c3d14c8STreehugger Robot   Printf("%s", str.data());
220*7c3d14c8STreehugger Robot }
221*7c3d14c8STreehugger Robot 
PrintZoneForPointer(uptr ptr,uptr zone_ptr,const char * zone_name)222*7c3d14c8STreehugger Robot static void PrintZoneForPointer(uptr ptr, uptr zone_ptr,
223*7c3d14c8STreehugger Robot                                 const char *zone_name) {
224*7c3d14c8STreehugger Robot   if (zone_ptr) {
225*7c3d14c8STreehugger Robot     if (zone_name) {
226*7c3d14c8STreehugger Robot       Printf("malloc_zone_from_ptr(%p) = %p, which is %s\n",
227*7c3d14c8STreehugger Robot                  ptr, zone_ptr, zone_name);
228*7c3d14c8STreehugger Robot     } else {
229*7c3d14c8STreehugger Robot       Printf("malloc_zone_from_ptr(%p) = %p, which doesn't have a name\n",
230*7c3d14c8STreehugger Robot                  ptr, zone_ptr);
231*7c3d14c8STreehugger Robot     }
232*7c3d14c8STreehugger Robot   } else {
233*7c3d14c8STreehugger Robot     Printf("malloc_zone_from_ptr(%p) = 0\n", ptr);
234*7c3d14c8STreehugger Robot   }
235*7c3d14c8STreehugger Robot }
236*7c3d14c8STreehugger Robot 
DescribeThread(AsanThread * t)237*7c3d14c8STreehugger Robot static void DescribeThread(AsanThread *t) {
238*7c3d14c8STreehugger Robot   if (t)
239*7c3d14c8STreehugger Robot     DescribeThread(t->context());
240*7c3d14c8STreehugger Robot }
241*7c3d14c8STreehugger Robot 
242*7c3d14c8STreehugger Robot // ---------------------- Address Descriptions ------------------- {{{1
243*7c3d14c8STreehugger Robot 
IsASCII(unsigned char c)244*7c3d14c8STreehugger Robot static bool IsASCII(unsigned char c) {
245*7c3d14c8STreehugger Robot   return /*0x00 <= c &&*/ c <= 0x7F;
246*7c3d14c8STreehugger Robot }
247*7c3d14c8STreehugger Robot 
MaybeDemangleGlobalName(const char * name)248*7c3d14c8STreehugger Robot static const char *MaybeDemangleGlobalName(const char *name) {
249*7c3d14c8STreehugger Robot   // We can spoil names of globals with C linkage, so use an heuristic
250*7c3d14c8STreehugger Robot   // approach to check if the name should be demangled.
251*7c3d14c8STreehugger Robot   bool should_demangle = false;
252*7c3d14c8STreehugger Robot   if (name[0] == '_' && name[1] == 'Z')
253*7c3d14c8STreehugger Robot     should_demangle = true;
254*7c3d14c8STreehugger Robot   else if (SANITIZER_WINDOWS && name[0] == '\01' && name[1] == '?')
255*7c3d14c8STreehugger Robot     should_demangle = true;
256*7c3d14c8STreehugger Robot 
257*7c3d14c8STreehugger Robot   return should_demangle ? Symbolizer::GetOrInit()->Demangle(name) : name;
258*7c3d14c8STreehugger Robot }
259*7c3d14c8STreehugger Robot 
260*7c3d14c8STreehugger Robot // Check if the global is a zero-terminated ASCII string. If so, print it.
PrintGlobalNameIfASCII(InternalScopedString * str,const __asan_global & g)261*7c3d14c8STreehugger Robot static void PrintGlobalNameIfASCII(InternalScopedString *str,
262*7c3d14c8STreehugger Robot                                    const __asan_global &g) {
263*7c3d14c8STreehugger Robot   for (uptr p = g.beg; p < g.beg + g.size - 1; p++) {
264*7c3d14c8STreehugger Robot     unsigned char c = *(unsigned char*)p;
265*7c3d14c8STreehugger Robot     if (c == '\0' || !IsASCII(c)) return;
266*7c3d14c8STreehugger Robot   }
267*7c3d14c8STreehugger Robot   if (*(char*)(g.beg + g.size - 1) != '\0') return;
268*7c3d14c8STreehugger Robot   str->append("  '%s' is ascii string '%s'\n", MaybeDemangleGlobalName(g.name),
269*7c3d14c8STreehugger Robot               (char *)g.beg);
270*7c3d14c8STreehugger Robot }
271*7c3d14c8STreehugger Robot 
GlobalFilename(const __asan_global & g)272*7c3d14c8STreehugger Robot static const char *GlobalFilename(const __asan_global &g) {
273*7c3d14c8STreehugger Robot   const char *res = g.module_name;
274*7c3d14c8STreehugger Robot   // Prefer the filename from source location, if is available.
275*7c3d14c8STreehugger Robot   if (g.location)
276*7c3d14c8STreehugger Robot     res = g.location->filename;
277*7c3d14c8STreehugger Robot   CHECK(res);
278*7c3d14c8STreehugger Robot   return res;
279*7c3d14c8STreehugger Robot }
280*7c3d14c8STreehugger Robot 
PrintGlobalLocation(InternalScopedString * str,const __asan_global & g)281*7c3d14c8STreehugger Robot static void PrintGlobalLocation(InternalScopedString *str,
282*7c3d14c8STreehugger Robot                                 const __asan_global &g) {
283*7c3d14c8STreehugger Robot   str->append("%s", GlobalFilename(g));
284*7c3d14c8STreehugger Robot   if (!g.location)
285*7c3d14c8STreehugger Robot     return;
286*7c3d14c8STreehugger Robot   if (g.location->line_no)
287*7c3d14c8STreehugger Robot     str->append(":%d", g.location->line_no);
288*7c3d14c8STreehugger Robot   if (g.location->column_no)
289*7c3d14c8STreehugger Robot     str->append(":%d", g.location->column_no);
290*7c3d14c8STreehugger Robot }
291*7c3d14c8STreehugger Robot 
DescribeAddressRelativeToGlobal(uptr addr,uptr size,const __asan_global & g)292*7c3d14c8STreehugger Robot static void DescribeAddressRelativeToGlobal(uptr addr, uptr size,
293*7c3d14c8STreehugger Robot                                             const __asan_global &g) {
294*7c3d14c8STreehugger Robot   InternalScopedString str(4096);
295*7c3d14c8STreehugger Robot   Decorator d;
296*7c3d14c8STreehugger Robot   str.append("%s", d.Location());
297*7c3d14c8STreehugger Robot   if (addr < g.beg) {
298*7c3d14c8STreehugger Robot     str.append("%p is located %zd bytes to the left", (void *)addr,
299*7c3d14c8STreehugger Robot                g.beg - addr);
300*7c3d14c8STreehugger Robot   } else if (addr + size > g.beg + g.size) {
301*7c3d14c8STreehugger Robot     if (addr < g.beg + g.size)
302*7c3d14c8STreehugger Robot       addr = g.beg + g.size;
303*7c3d14c8STreehugger Robot     str.append("%p is located %zd bytes to the right", (void *)addr,
304*7c3d14c8STreehugger Robot                addr - (g.beg + g.size));
305*7c3d14c8STreehugger Robot   } else {
306*7c3d14c8STreehugger Robot     // Can it happen?
307*7c3d14c8STreehugger Robot     str.append("%p is located %zd bytes inside", (void *)addr, addr - g.beg);
308*7c3d14c8STreehugger Robot   }
309*7c3d14c8STreehugger Robot   str.append(" of global variable '%s' defined in '",
310*7c3d14c8STreehugger Robot              MaybeDemangleGlobalName(g.name));
311*7c3d14c8STreehugger Robot   PrintGlobalLocation(&str, g);
312*7c3d14c8STreehugger Robot   str.append("' (0x%zx) of size %zu\n", g.beg, g.size);
313*7c3d14c8STreehugger Robot   str.append("%s", d.EndLocation());
314*7c3d14c8STreehugger Robot   PrintGlobalNameIfASCII(&str, g);
315*7c3d14c8STreehugger Robot   Printf("%s", str.data());
316*7c3d14c8STreehugger Robot }
317*7c3d14c8STreehugger Robot 
DescribeAddressIfGlobal(uptr addr,uptr size,const char * bug_type)318*7c3d14c8STreehugger Robot static bool DescribeAddressIfGlobal(uptr addr, uptr size,
319*7c3d14c8STreehugger Robot                                     const char *bug_type) {
320*7c3d14c8STreehugger Robot   // Assume address is close to at most four globals.
321*7c3d14c8STreehugger Robot   const int kMaxGlobalsInReport = 4;
322*7c3d14c8STreehugger Robot   __asan_global globals[kMaxGlobalsInReport];
323*7c3d14c8STreehugger Robot   u32 reg_sites[kMaxGlobalsInReport];
324*7c3d14c8STreehugger Robot   int globals_num =
325*7c3d14c8STreehugger Robot       GetGlobalsForAddress(addr, globals, reg_sites, ARRAY_SIZE(globals));
326*7c3d14c8STreehugger Robot   if (globals_num == 0)
327*7c3d14c8STreehugger Robot     return false;
328*7c3d14c8STreehugger Robot   for (int i = 0; i < globals_num; i++) {
329*7c3d14c8STreehugger Robot     DescribeAddressRelativeToGlobal(addr, size, globals[i]);
330*7c3d14c8STreehugger Robot     if (0 == internal_strcmp(bug_type, "initialization-order-fiasco") &&
331*7c3d14c8STreehugger Robot         reg_sites[i]) {
332*7c3d14c8STreehugger Robot       Printf("  registered at:\n");
333*7c3d14c8STreehugger Robot       StackDepotGet(reg_sites[i]).Print();
334*7c3d14c8STreehugger Robot     }
335*7c3d14c8STreehugger Robot   }
336*7c3d14c8STreehugger Robot   return true;
337*7c3d14c8STreehugger Robot }
338*7c3d14c8STreehugger Robot 
DescribeAddressIfShadow(uptr addr,AddressDescription * descr,bool print)339*7c3d14c8STreehugger Robot bool DescribeAddressIfShadow(uptr addr, AddressDescription *descr, bool print) {
340*7c3d14c8STreehugger Robot   if (AddrIsInMem(addr))
341*7c3d14c8STreehugger Robot     return false;
342*7c3d14c8STreehugger Robot   const char *area_type = nullptr;
343*7c3d14c8STreehugger Robot   if (AddrIsInShadowGap(addr)) area_type = "shadow gap";
344*7c3d14c8STreehugger Robot   else if (AddrIsInHighShadow(addr)) area_type = "high shadow";
345*7c3d14c8STreehugger Robot   else if (AddrIsInLowShadow(addr)) area_type = "low shadow";
346*7c3d14c8STreehugger Robot   if (area_type != nullptr) {
347*7c3d14c8STreehugger Robot     if (print) {
348*7c3d14c8STreehugger Robot       Printf("Address %p is located in the %s area.\n", addr, area_type);
349*7c3d14c8STreehugger Robot     } else {
350*7c3d14c8STreehugger Robot       CHECK(descr);
351*7c3d14c8STreehugger Robot       descr->region_kind = area_type;
352*7c3d14c8STreehugger Robot     }
353*7c3d14c8STreehugger Robot     return true;
354*7c3d14c8STreehugger Robot   }
355*7c3d14c8STreehugger Robot   CHECK(0 && "Address is not in memory and not in shadow?");
356*7c3d14c8STreehugger Robot   return false;
357*7c3d14c8STreehugger Robot }
358*7c3d14c8STreehugger Robot 
359*7c3d14c8STreehugger Robot // Return " (thread_name) " or an empty string if the name is empty.
ThreadNameWithParenthesis(AsanThreadContext * t,char buff[],uptr buff_len)360*7c3d14c8STreehugger Robot const char *ThreadNameWithParenthesis(AsanThreadContext *t, char buff[],
361*7c3d14c8STreehugger Robot                                       uptr buff_len) {
362*7c3d14c8STreehugger Robot   const char *name = t->name;
363*7c3d14c8STreehugger Robot   if (name[0] == '\0') return "";
364*7c3d14c8STreehugger Robot   buff[0] = 0;
365*7c3d14c8STreehugger Robot   internal_strncat(buff, " (", 3);
366*7c3d14c8STreehugger Robot   internal_strncat(buff, name, buff_len - 4);
367*7c3d14c8STreehugger Robot   internal_strncat(buff, ")", 2);
368*7c3d14c8STreehugger Robot   return buff;
369*7c3d14c8STreehugger Robot }
370*7c3d14c8STreehugger Robot 
ThreadNameWithParenthesis(u32 tid,char buff[],uptr buff_len)371*7c3d14c8STreehugger Robot const char *ThreadNameWithParenthesis(u32 tid, char buff[],
372*7c3d14c8STreehugger Robot                                       uptr buff_len) {
373*7c3d14c8STreehugger Robot   if (tid == kInvalidTid) return "";
374*7c3d14c8STreehugger Robot   asanThreadRegistry().CheckLocked();
375*7c3d14c8STreehugger Robot   AsanThreadContext *t = GetThreadContextByTidLocked(tid);
376*7c3d14c8STreehugger Robot   return ThreadNameWithParenthesis(t, buff, buff_len);
377*7c3d14c8STreehugger Robot }
378*7c3d14c8STreehugger Robot 
PrintAccessAndVarIntersection(const StackVarDescr & var,uptr addr,uptr access_size,uptr prev_var_end,uptr next_var_beg)379*7c3d14c8STreehugger Robot static void PrintAccessAndVarIntersection(const StackVarDescr &var, uptr addr,
380*7c3d14c8STreehugger Robot                                           uptr access_size, uptr prev_var_end,
381*7c3d14c8STreehugger Robot                                           uptr next_var_beg) {
382*7c3d14c8STreehugger Robot   uptr var_end = var.beg + var.size;
383*7c3d14c8STreehugger Robot   uptr addr_end = addr + access_size;
384*7c3d14c8STreehugger Robot   const char *pos_descr = nullptr;
385*7c3d14c8STreehugger Robot   // If the variable [var.beg, var_end) is the nearest variable to the
386*7c3d14c8STreehugger Robot   // current memory access, indicate it in the log.
387*7c3d14c8STreehugger Robot   if (addr >= var.beg) {
388*7c3d14c8STreehugger Robot     if (addr_end <= var_end)
389*7c3d14c8STreehugger Robot       pos_descr = "is inside";  // May happen if this is a use-after-return.
390*7c3d14c8STreehugger Robot     else if (addr < var_end)
391*7c3d14c8STreehugger Robot       pos_descr = "partially overflows";
392*7c3d14c8STreehugger Robot     else if (addr_end <= next_var_beg &&
393*7c3d14c8STreehugger Robot              next_var_beg - addr_end >= addr - var_end)
394*7c3d14c8STreehugger Robot       pos_descr = "overflows";
395*7c3d14c8STreehugger Robot   } else {
396*7c3d14c8STreehugger Robot     if (addr_end > var.beg)
397*7c3d14c8STreehugger Robot       pos_descr = "partially underflows";
398*7c3d14c8STreehugger Robot     else if (addr >= prev_var_end &&
399*7c3d14c8STreehugger Robot              addr - prev_var_end >= var.beg - addr_end)
400*7c3d14c8STreehugger Robot       pos_descr = "underflows";
401*7c3d14c8STreehugger Robot   }
402*7c3d14c8STreehugger Robot   InternalScopedString str(1024);
403*7c3d14c8STreehugger Robot   str.append("    [%zd, %zd)", var.beg, var_end);
404*7c3d14c8STreehugger Robot   // Render variable name.
405*7c3d14c8STreehugger Robot   str.append(" '");
406*7c3d14c8STreehugger Robot   for (uptr i = 0; i < var.name_len; ++i) {
407*7c3d14c8STreehugger Robot     str.append("%c", var.name_pos[i]);
408*7c3d14c8STreehugger Robot   }
409*7c3d14c8STreehugger Robot   str.append("'");
410*7c3d14c8STreehugger Robot   if (pos_descr) {
411*7c3d14c8STreehugger Robot     Decorator d;
412*7c3d14c8STreehugger Robot     // FIXME: we may want to also print the size of the access here,
413*7c3d14c8STreehugger Robot     // but in case of accesses generated by memset it may be confusing.
414*7c3d14c8STreehugger Robot     str.append("%s <== Memory access at offset %zd %s this variable%s\n",
415*7c3d14c8STreehugger Robot                d.Location(), addr, pos_descr, d.EndLocation());
416*7c3d14c8STreehugger Robot   } else {
417*7c3d14c8STreehugger Robot     str.append("\n");
418*7c3d14c8STreehugger Robot   }
419*7c3d14c8STreehugger Robot   Printf("%s", str.data());
420*7c3d14c8STreehugger Robot }
421*7c3d14c8STreehugger Robot 
ParseFrameDescription(const char * frame_descr,InternalMmapVector<StackVarDescr> * vars)422*7c3d14c8STreehugger Robot bool ParseFrameDescription(const char *frame_descr,
423*7c3d14c8STreehugger Robot                            InternalMmapVector<StackVarDescr> *vars) {
424*7c3d14c8STreehugger Robot   CHECK(frame_descr);
425*7c3d14c8STreehugger Robot   char *p;
426*7c3d14c8STreehugger Robot   // This string is created by the compiler and has the following form:
427*7c3d14c8STreehugger Robot   // "n alloc_1 alloc_2 ... alloc_n"
428*7c3d14c8STreehugger Robot   // where alloc_i looks like "offset size len ObjectName".
429*7c3d14c8STreehugger Robot   uptr n_objects = (uptr)internal_simple_strtoll(frame_descr, &p, 10);
430*7c3d14c8STreehugger Robot   if (n_objects == 0)
431*7c3d14c8STreehugger Robot     return false;
432*7c3d14c8STreehugger Robot 
433*7c3d14c8STreehugger Robot   for (uptr i = 0; i < n_objects; i++) {
434*7c3d14c8STreehugger Robot     uptr beg  = (uptr)internal_simple_strtoll(p, &p, 10);
435*7c3d14c8STreehugger Robot     uptr size = (uptr)internal_simple_strtoll(p, &p, 10);
436*7c3d14c8STreehugger Robot     uptr len  = (uptr)internal_simple_strtoll(p, &p, 10);
437*7c3d14c8STreehugger Robot     if (beg == 0 || size == 0 || *p != ' ') {
438*7c3d14c8STreehugger Robot       return false;
439*7c3d14c8STreehugger Robot     }
440*7c3d14c8STreehugger Robot     p++;
441*7c3d14c8STreehugger Robot     StackVarDescr var = {beg, size, p, len};
442*7c3d14c8STreehugger Robot     vars->push_back(var);
443*7c3d14c8STreehugger Robot     p += len;
444*7c3d14c8STreehugger Robot   }
445*7c3d14c8STreehugger Robot 
446*7c3d14c8STreehugger Robot   return true;
447*7c3d14c8STreehugger Robot }
448*7c3d14c8STreehugger Robot 
DescribeAddressIfStack(uptr addr,uptr access_size)449*7c3d14c8STreehugger Robot bool DescribeAddressIfStack(uptr addr, uptr access_size) {
450*7c3d14c8STreehugger Robot   AsanThread *t = FindThreadByStackAddress(addr);
451*7c3d14c8STreehugger Robot   if (!t) return false;
452*7c3d14c8STreehugger Robot 
453*7c3d14c8STreehugger Robot   Decorator d;
454*7c3d14c8STreehugger Robot   char tname[128];
455*7c3d14c8STreehugger Robot   Printf("%s", d.Location());
456*7c3d14c8STreehugger Robot   Printf("Address %p is located in stack of thread T%d%s", addr, t->tid(),
457*7c3d14c8STreehugger Robot          ThreadNameWithParenthesis(t->tid(), tname, sizeof(tname)));
458*7c3d14c8STreehugger Robot 
459*7c3d14c8STreehugger Robot   // Try to fetch precise stack frame for this access.
460*7c3d14c8STreehugger Robot   AsanThread::StackFrameAccess access;
461*7c3d14c8STreehugger Robot   if (!t->GetStackFrameAccessByAddr(addr, &access)) {
462*7c3d14c8STreehugger Robot     Printf("%s\n", d.EndLocation());
463*7c3d14c8STreehugger Robot     return true;
464*7c3d14c8STreehugger Robot   }
465*7c3d14c8STreehugger Robot   Printf(" at offset %zu in frame%s\n", access.offset, d.EndLocation());
466*7c3d14c8STreehugger Robot 
467*7c3d14c8STreehugger Robot   // Now we print the frame where the alloca has happened.
468*7c3d14c8STreehugger Robot   // We print this frame as a stack trace with one element.
469*7c3d14c8STreehugger Robot   // The symbolizer may print more than one frame if inlining was involved.
470*7c3d14c8STreehugger Robot   // The frame numbers may be different than those in the stack trace printed
471*7c3d14c8STreehugger Robot   // previously. That's unfortunate, but I have no better solution,
472*7c3d14c8STreehugger Robot   // especially given that the alloca may be from entirely different place
473*7c3d14c8STreehugger Robot   // (e.g. use-after-scope, or different thread's stack).
474*7c3d14c8STreehugger Robot #if SANITIZER_PPC64V1
475*7c3d14c8STreehugger Robot   // On PowerPC64 ELFv1, the address of a function actually points to a
476*7c3d14c8STreehugger Robot   // three-doubleword data structure with the first field containing
477*7c3d14c8STreehugger Robot   // the address of the function's code.
478*7c3d14c8STreehugger Robot   access.frame_pc = *reinterpret_cast<uptr *>(access.frame_pc);
479*7c3d14c8STreehugger Robot #endif
480*7c3d14c8STreehugger Robot   access.frame_pc += 16;
481*7c3d14c8STreehugger Robot   Printf("%s", d.EndLocation());
482*7c3d14c8STreehugger Robot   StackTrace alloca_stack(&access.frame_pc, 1);
483*7c3d14c8STreehugger Robot   alloca_stack.Print();
484*7c3d14c8STreehugger Robot 
485*7c3d14c8STreehugger Robot   InternalMmapVector<StackVarDescr> vars(16);
486*7c3d14c8STreehugger Robot   if (!ParseFrameDescription(access.frame_descr, &vars)) {
487*7c3d14c8STreehugger Robot     Printf("AddressSanitizer can't parse the stack frame "
488*7c3d14c8STreehugger Robot            "descriptor: |%s|\n", access.frame_descr);
489*7c3d14c8STreehugger Robot     // 'addr' is a stack address, so return true even if we can't parse frame
490*7c3d14c8STreehugger Robot     return true;
491*7c3d14c8STreehugger Robot   }
492*7c3d14c8STreehugger Robot   uptr n_objects = vars.size();
493*7c3d14c8STreehugger Robot   // Report the number of stack objects.
494*7c3d14c8STreehugger Robot   Printf("  This frame has %zu object(s):\n", n_objects);
495*7c3d14c8STreehugger Robot 
496*7c3d14c8STreehugger Robot   // Report all objects in this frame.
497*7c3d14c8STreehugger Robot   for (uptr i = 0; i < n_objects; i++) {
498*7c3d14c8STreehugger Robot     uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0;
499*7c3d14c8STreehugger Robot     uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL);
500*7c3d14c8STreehugger Robot     PrintAccessAndVarIntersection(vars[i], access.offset, access_size,
501*7c3d14c8STreehugger Robot                                   prev_var_end, next_var_beg);
502*7c3d14c8STreehugger Robot   }
503*7c3d14c8STreehugger Robot   Printf("HINT: this may be a false positive if your program uses "
504*7c3d14c8STreehugger Robot          "some custom stack unwind mechanism or swapcontext\n");
505*7c3d14c8STreehugger Robot   if (SANITIZER_WINDOWS)
506*7c3d14c8STreehugger Robot     Printf("      (longjmp, SEH and C++ exceptions *are* supported)\n");
507*7c3d14c8STreehugger Robot   else
508*7c3d14c8STreehugger Robot     Printf("      (longjmp and C++ exceptions *are* supported)\n");
509*7c3d14c8STreehugger Robot 
510*7c3d14c8STreehugger Robot   DescribeThread(t);
511*7c3d14c8STreehugger Robot   return true;
512*7c3d14c8STreehugger Robot }
513*7c3d14c8STreehugger Robot 
DescribeAccessToHeapChunk(AsanChunkView chunk,uptr addr,uptr access_size)514*7c3d14c8STreehugger Robot static void DescribeAccessToHeapChunk(AsanChunkView chunk, uptr addr,
515*7c3d14c8STreehugger Robot                                       uptr access_size) {
516*7c3d14c8STreehugger Robot   sptr offset;
517*7c3d14c8STreehugger Robot   Decorator d;
518*7c3d14c8STreehugger Robot   InternalScopedString str(4096);
519*7c3d14c8STreehugger Robot   str.append("%s", d.Location());
520*7c3d14c8STreehugger Robot   if (chunk.AddrIsAtLeft(addr, access_size, &offset)) {
521*7c3d14c8STreehugger Robot     str.append("%p is located %zd bytes to the left of", (void *)addr, offset);
522*7c3d14c8STreehugger Robot   } else if (chunk.AddrIsAtRight(addr, access_size, &offset)) {
523*7c3d14c8STreehugger Robot     if (offset < 0) {
524*7c3d14c8STreehugger Robot       addr -= offset;
525*7c3d14c8STreehugger Robot       offset = 0;
526*7c3d14c8STreehugger Robot     }
527*7c3d14c8STreehugger Robot     str.append("%p is located %zd bytes to the right of", (void *)addr, offset);
528*7c3d14c8STreehugger Robot   } else if (chunk.AddrIsInside(addr, access_size, &offset)) {
529*7c3d14c8STreehugger Robot     str.append("%p is located %zd bytes inside of", (void*)addr, offset);
530*7c3d14c8STreehugger Robot   } else {
531*7c3d14c8STreehugger Robot     str.append("%p is located somewhere around (this is AddressSanitizer bug!)",
532*7c3d14c8STreehugger Robot                (void *)addr);
533*7c3d14c8STreehugger Robot   }
534*7c3d14c8STreehugger Robot   str.append(" %zu-byte region [%p,%p)\n", chunk.UsedSize(),
535*7c3d14c8STreehugger Robot              (void *)(chunk.Beg()), (void *)(chunk.End()));
536*7c3d14c8STreehugger Robot   str.append("%s", d.EndLocation());
537*7c3d14c8STreehugger Robot   Printf("%s", str.data());
538*7c3d14c8STreehugger Robot }
539*7c3d14c8STreehugger Robot 
DescribeHeapAddress(uptr addr,uptr access_size)540*7c3d14c8STreehugger Robot void DescribeHeapAddress(uptr addr, uptr access_size) {
541*7c3d14c8STreehugger Robot   AsanChunkView chunk = FindHeapChunkByAddress(addr);
542*7c3d14c8STreehugger Robot   if (!chunk.IsValid()) {
543*7c3d14c8STreehugger Robot     Printf("AddressSanitizer can not describe address in more detail "
544*7c3d14c8STreehugger Robot            "(wild memory access suspected).\n");
545*7c3d14c8STreehugger Robot     return;
546*7c3d14c8STreehugger Robot   }
547*7c3d14c8STreehugger Robot   DescribeAccessToHeapChunk(chunk, addr, access_size);
548*7c3d14c8STreehugger Robot   CHECK(chunk.AllocTid() != kInvalidTid);
549*7c3d14c8STreehugger Robot   asanThreadRegistry().CheckLocked();
550*7c3d14c8STreehugger Robot   AsanThreadContext *alloc_thread =
551*7c3d14c8STreehugger Robot       GetThreadContextByTidLocked(chunk.AllocTid());
552*7c3d14c8STreehugger Robot   StackTrace alloc_stack = chunk.GetAllocStack();
553*7c3d14c8STreehugger Robot   char tname[128];
554*7c3d14c8STreehugger Robot   Decorator d;
555*7c3d14c8STreehugger Robot   AsanThreadContext *free_thread = nullptr;
556*7c3d14c8STreehugger Robot   if (chunk.FreeTid() != kInvalidTid) {
557*7c3d14c8STreehugger Robot     free_thread = GetThreadContextByTidLocked(chunk.FreeTid());
558*7c3d14c8STreehugger Robot     Printf("%sfreed by thread T%d%s here:%s\n", d.Allocation(),
559*7c3d14c8STreehugger Robot            free_thread->tid,
560*7c3d14c8STreehugger Robot            ThreadNameWithParenthesis(free_thread, tname, sizeof(tname)),
561*7c3d14c8STreehugger Robot            d.EndAllocation());
562*7c3d14c8STreehugger Robot     StackTrace free_stack = chunk.GetFreeStack();
563*7c3d14c8STreehugger Robot     free_stack.Print();
564*7c3d14c8STreehugger Robot     Printf("%spreviously allocated by thread T%d%s here:%s\n",
565*7c3d14c8STreehugger Robot            d.Allocation(), alloc_thread->tid,
566*7c3d14c8STreehugger Robot            ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
567*7c3d14c8STreehugger Robot            d.EndAllocation());
568*7c3d14c8STreehugger Robot   } else {
569*7c3d14c8STreehugger Robot     Printf("%sallocated by thread T%d%s here:%s\n", d.Allocation(),
570*7c3d14c8STreehugger Robot            alloc_thread->tid,
571*7c3d14c8STreehugger Robot            ThreadNameWithParenthesis(alloc_thread, tname, sizeof(tname)),
572*7c3d14c8STreehugger Robot            d.EndAllocation());
573*7c3d14c8STreehugger Robot   }
574*7c3d14c8STreehugger Robot   alloc_stack.Print();
575*7c3d14c8STreehugger Robot   DescribeThread(GetCurrentThread());
576*7c3d14c8STreehugger Robot   if (free_thread)
577*7c3d14c8STreehugger Robot     DescribeThread(free_thread);
578*7c3d14c8STreehugger Robot   DescribeThread(alloc_thread);
579*7c3d14c8STreehugger Robot }
580*7c3d14c8STreehugger Robot 
DescribeAddress(uptr addr,uptr access_size,const char * bug_type)581*7c3d14c8STreehugger Robot static void DescribeAddress(uptr addr, uptr access_size, const char *bug_type) {
582*7c3d14c8STreehugger Robot   // Check if this is shadow or shadow gap.
583*7c3d14c8STreehugger Robot   if (DescribeAddressIfShadow(addr))
584*7c3d14c8STreehugger Robot     return;
585*7c3d14c8STreehugger Robot   CHECK(AddrIsInMem(addr));
586*7c3d14c8STreehugger Robot   if (DescribeAddressIfGlobal(addr, access_size, bug_type))
587*7c3d14c8STreehugger Robot     return;
588*7c3d14c8STreehugger Robot   if (DescribeAddressIfStack(addr, access_size))
589*7c3d14c8STreehugger Robot     return;
590*7c3d14c8STreehugger Robot   // Assume it is a heap address.
591*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, access_size);
592*7c3d14c8STreehugger Robot }
593*7c3d14c8STreehugger Robot 
594*7c3d14c8STreehugger Robot // ------------------- Thread description -------------------- {{{1
595*7c3d14c8STreehugger Robot 
DescribeThread(AsanThreadContext * context)596*7c3d14c8STreehugger Robot void DescribeThread(AsanThreadContext *context) {
597*7c3d14c8STreehugger Robot   CHECK(context);
598*7c3d14c8STreehugger Robot   asanThreadRegistry().CheckLocked();
599*7c3d14c8STreehugger Robot   // No need to announce the main thread.
600*7c3d14c8STreehugger Robot   if (context->tid == 0 || context->announced) {
601*7c3d14c8STreehugger Robot     return;
602*7c3d14c8STreehugger Robot   }
603*7c3d14c8STreehugger Robot   context->announced = true;
604*7c3d14c8STreehugger Robot   char tname[128];
605*7c3d14c8STreehugger Robot   InternalScopedString str(1024);
606*7c3d14c8STreehugger Robot   str.append("Thread T%d%s", context->tid,
607*7c3d14c8STreehugger Robot              ThreadNameWithParenthesis(context->tid, tname, sizeof(tname)));
608*7c3d14c8STreehugger Robot   if (context->parent_tid == kInvalidTid) {
609*7c3d14c8STreehugger Robot     str.append(" created by unknown thread\n");
610*7c3d14c8STreehugger Robot     Printf("%s", str.data());
611*7c3d14c8STreehugger Robot     return;
612*7c3d14c8STreehugger Robot   }
613*7c3d14c8STreehugger Robot   str.append(
614*7c3d14c8STreehugger Robot       " created by T%d%s here:\n", context->parent_tid,
615*7c3d14c8STreehugger Robot       ThreadNameWithParenthesis(context->parent_tid, tname, sizeof(tname)));
616*7c3d14c8STreehugger Robot   Printf("%s", str.data());
617*7c3d14c8STreehugger Robot   StackDepotGet(context->stack_id).Print();
618*7c3d14c8STreehugger Robot   // Recursively described parent thread if needed.
619*7c3d14c8STreehugger Robot   if (flags()->print_full_thread_history) {
620*7c3d14c8STreehugger Robot     AsanThreadContext *parent_context =
621*7c3d14c8STreehugger Robot         GetThreadContextByTidLocked(context->parent_tid);
622*7c3d14c8STreehugger Robot     DescribeThread(parent_context);
623*7c3d14c8STreehugger Robot   }
624*7c3d14c8STreehugger Robot }
625*7c3d14c8STreehugger Robot 
626*7c3d14c8STreehugger Robot // -------------------- Different kinds of reports ----------------- {{{1
627*7c3d14c8STreehugger Robot 
628*7c3d14c8STreehugger Robot // Use ScopedInErrorReport to run common actions just before and
629*7c3d14c8STreehugger Robot // immediately after printing error report.
630*7c3d14c8STreehugger Robot class ScopedInErrorReport {
631*7c3d14c8STreehugger Robot  public:
ScopedInErrorReport(ReportData * report=nullptr,bool fatal=false)632*7c3d14c8STreehugger Robot   explicit ScopedInErrorReport(ReportData *report = nullptr,
633*7c3d14c8STreehugger Robot                                bool fatal = false) {
634*7c3d14c8STreehugger Robot     halt_on_error_ = fatal || flags()->halt_on_error;
635*7c3d14c8STreehugger Robot 
636*7c3d14c8STreehugger Robot     if (lock_.TryLock()) {
637*7c3d14c8STreehugger Robot       StartReporting(report);
638*7c3d14c8STreehugger Robot       return;
639*7c3d14c8STreehugger Robot     }
640*7c3d14c8STreehugger Robot 
641*7c3d14c8STreehugger Robot     // ASan found two bugs in different threads simultaneously.
642*7c3d14c8STreehugger Robot 
643*7c3d14c8STreehugger Robot     u32 current_tid = GetCurrentTidOrInvalid();
644*7c3d14c8STreehugger Robot     if (reporting_thread_tid_ == current_tid ||
645*7c3d14c8STreehugger Robot         reporting_thread_tid_ == kInvalidTid) {
646*7c3d14c8STreehugger Robot       // This is either asynch signal or nested error during error reporting.
647*7c3d14c8STreehugger Robot       // Fail simple to avoid deadlocks in Report().
648*7c3d14c8STreehugger Robot 
649*7c3d14c8STreehugger Robot       // Can't use Report() here because of potential deadlocks
650*7c3d14c8STreehugger Robot       // in nested signal handlers.
651*7c3d14c8STreehugger Robot       const char msg[] = "AddressSanitizer: nested bug in the same thread, "
652*7c3d14c8STreehugger Robot                          "aborting.\n";
653*7c3d14c8STreehugger Robot       WriteToFile(kStderrFd, msg, sizeof(msg));
654*7c3d14c8STreehugger Robot 
655*7c3d14c8STreehugger Robot       internal__exit(common_flags()->exitcode);
656*7c3d14c8STreehugger Robot     }
657*7c3d14c8STreehugger Robot 
658*7c3d14c8STreehugger Robot     if (halt_on_error_) {
659*7c3d14c8STreehugger Robot       // Do not print more than one report, otherwise they will mix up.
660*7c3d14c8STreehugger Robot       // Error reporting functions shouldn't return at this situation, as
661*7c3d14c8STreehugger Robot       // they are effectively no-returns.
662*7c3d14c8STreehugger Robot 
663*7c3d14c8STreehugger Robot       Report("AddressSanitizer: while reporting a bug found another one. "
664*7c3d14c8STreehugger Robot              "Ignoring.\n");
665*7c3d14c8STreehugger Robot 
666*7c3d14c8STreehugger Robot       // Sleep long enough to make sure that the thread which started
667*7c3d14c8STreehugger Robot       // to print an error report will finish doing it.
668*7c3d14c8STreehugger Robot       SleepForSeconds(Max(100, flags()->sleep_before_dying + 1));
669*7c3d14c8STreehugger Robot 
670*7c3d14c8STreehugger Robot       // If we're still not dead for some reason, use raw _exit() instead of
671*7c3d14c8STreehugger Robot       // Die() to bypass any additional checks.
672*7c3d14c8STreehugger Robot       internal__exit(common_flags()->exitcode);
673*7c3d14c8STreehugger Robot     } else {
674*7c3d14c8STreehugger Robot       // The other thread will eventually finish reporting
675*7c3d14c8STreehugger Robot       // so it's safe to wait
676*7c3d14c8STreehugger Robot       lock_.Lock();
677*7c3d14c8STreehugger Robot     }
678*7c3d14c8STreehugger Robot 
679*7c3d14c8STreehugger Robot     StartReporting(report);
680*7c3d14c8STreehugger Robot   }
681*7c3d14c8STreehugger Robot 
~ScopedInErrorReport()682*7c3d14c8STreehugger Robot   ~ScopedInErrorReport() {
683*7c3d14c8STreehugger Robot     // Make sure the current thread is announced.
684*7c3d14c8STreehugger Robot     DescribeThread(GetCurrentThread());
685*7c3d14c8STreehugger Robot     // We may want to grab this lock again when printing stats.
686*7c3d14c8STreehugger Robot     asanThreadRegistry().Unlock();
687*7c3d14c8STreehugger Robot     // Print memory stats.
688*7c3d14c8STreehugger Robot     if (flags()->print_stats)
689*7c3d14c8STreehugger Robot       __asan_print_accumulated_stats();
690*7c3d14c8STreehugger Robot 
691*7c3d14c8STreehugger Robot     if (common_flags()->print_cmdline)
692*7c3d14c8STreehugger Robot       PrintCmdline();
693*7c3d14c8STreehugger Robot 
694*7c3d14c8STreehugger Robot     // Copy the message buffer so that we could start logging without holding a
695*7c3d14c8STreehugger Robot     // lock that gets aquired during printing.
696*7c3d14c8STreehugger Robot     InternalScopedBuffer<char> buffer_copy(kErrorMessageBufferSize);
697*7c3d14c8STreehugger Robot     {
698*7c3d14c8STreehugger Robot       BlockingMutexLock l(&error_message_buf_mutex);
699*7c3d14c8STreehugger Robot       internal_memcpy(buffer_copy.data(),
700*7c3d14c8STreehugger Robot                       error_message_buffer, kErrorMessageBufferSize);
701*7c3d14c8STreehugger Robot     }
702*7c3d14c8STreehugger Robot 
703*7c3d14c8STreehugger Robot     LogFullErrorReport(buffer_copy.data());
704*7c3d14c8STreehugger Robot 
705*7c3d14c8STreehugger Robot     if (error_report_callback) {
706*7c3d14c8STreehugger Robot       error_report_callback(buffer_copy.data());
707*7c3d14c8STreehugger Robot     }
708*7c3d14c8STreehugger Robot     CommonSanitizerReportMutex.Unlock();
709*7c3d14c8STreehugger Robot     reporting_thread_tid_ = kInvalidTid;
710*7c3d14c8STreehugger Robot     lock_.Unlock();
711*7c3d14c8STreehugger Robot     if (halt_on_error_) {
712*7c3d14c8STreehugger Robot       Report("ABORTING\n");
713*7c3d14c8STreehugger Robot       Die();
714*7c3d14c8STreehugger Robot     }
715*7c3d14c8STreehugger Robot   }
716*7c3d14c8STreehugger Robot 
717*7c3d14c8STreehugger Robot  private:
StartReporting(ReportData * report)718*7c3d14c8STreehugger Robot   void StartReporting(ReportData *report) {
719*7c3d14c8STreehugger Robot     if (report) report_data = *report;
720*7c3d14c8STreehugger Robot     report_happened = true;
721*7c3d14c8STreehugger Robot     ASAN_ON_ERROR();
722*7c3d14c8STreehugger Robot     // Make sure the registry and sanitizer report mutexes are locked while
723*7c3d14c8STreehugger Robot     // we're printing an error report.
724*7c3d14c8STreehugger Robot     // We can lock them only here to avoid self-deadlock in case of
725*7c3d14c8STreehugger Robot     // recursive reports.
726*7c3d14c8STreehugger Robot     asanThreadRegistry().Lock();
727*7c3d14c8STreehugger Robot     CommonSanitizerReportMutex.Lock();
728*7c3d14c8STreehugger Robot     reporting_thread_tid_ = GetCurrentTidOrInvalid();
729*7c3d14c8STreehugger Robot     Printf("===================================================="
730*7c3d14c8STreehugger Robot            "=============\n");
731*7c3d14c8STreehugger Robot   }
732*7c3d14c8STreehugger Robot 
733*7c3d14c8STreehugger Robot   static StaticSpinMutex lock_;
734*7c3d14c8STreehugger Robot   static u32 reporting_thread_tid_;
735*7c3d14c8STreehugger Robot   bool halt_on_error_;
736*7c3d14c8STreehugger Robot };
737*7c3d14c8STreehugger Robot 
738*7c3d14c8STreehugger Robot StaticSpinMutex ScopedInErrorReport::lock_;
739*7c3d14c8STreehugger Robot u32 ScopedInErrorReport::reporting_thread_tid_ = kInvalidTid;
740*7c3d14c8STreehugger Robot 
ReportStackOverflow(const SignalContext & sig)741*7c3d14c8STreehugger Robot void ReportStackOverflow(const SignalContext &sig) {
742*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report(/*report*/ nullptr, /*fatal*/ true);
743*7c3d14c8STreehugger Robot   Decorator d;
744*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
745*7c3d14c8STreehugger Robot   Report(
746*7c3d14c8STreehugger Robot       "ERROR: AddressSanitizer: stack-overflow on address %p"
747*7c3d14c8STreehugger Robot       " (pc %p bp %p sp %p T%d)\n",
748*7c3d14c8STreehugger Robot       (void *)sig.addr, (void *)sig.pc, (void *)sig.bp, (void *)sig.sp,
749*7c3d14c8STreehugger Robot       GetCurrentTidOrInvalid());
750*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
751*7c3d14c8STreehugger Robot   ScarinessScore::PrintSimple(10, "stack-overflow");
752*7c3d14c8STreehugger Robot   GET_STACK_TRACE_SIGNAL(sig);
753*7c3d14c8STreehugger Robot   stack.Print();
754*7c3d14c8STreehugger Robot   ReportErrorSummary("stack-overflow", &stack);
755*7c3d14c8STreehugger Robot }
756*7c3d14c8STreehugger Robot 
ReportDeadlySignal(const char * description,const SignalContext & sig)757*7c3d14c8STreehugger Robot void ReportDeadlySignal(const char *description, const SignalContext &sig) {
758*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report(/*report*/ nullptr, /*fatal*/ true);
759*7c3d14c8STreehugger Robot   Decorator d;
760*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
761*7c3d14c8STreehugger Robot   Report(
762*7c3d14c8STreehugger Robot       "ERROR: AddressSanitizer: %s on unknown address %p"
763*7c3d14c8STreehugger Robot       " (pc %p bp %p sp %p T%d)\n",
764*7c3d14c8STreehugger Robot       description, (void *)sig.addr, (void *)sig.pc, (void *)sig.bp,
765*7c3d14c8STreehugger Robot       (void *)sig.sp, GetCurrentTidOrInvalid());
766*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
767*7c3d14c8STreehugger Robot   ScarinessScore SS;
768*7c3d14c8STreehugger Robot   if (sig.pc < GetPageSizeCached())
769*7c3d14c8STreehugger Robot     Report("Hint: pc points to the zero page.\n");
770*7c3d14c8STreehugger Robot   if (sig.is_memory_access) {
771*7c3d14c8STreehugger Robot     const char *access_type =
772*7c3d14c8STreehugger Robot         sig.write_flag == SignalContext::WRITE
773*7c3d14c8STreehugger Robot             ? "WRITE"
774*7c3d14c8STreehugger Robot             : (sig.write_flag == SignalContext::READ ? "READ" : "UNKNOWN");
775*7c3d14c8STreehugger Robot     Report("The signal is caused by a %s memory access.\n", access_type);
776*7c3d14c8STreehugger Robot     if (sig.addr < GetPageSizeCached()) {
777*7c3d14c8STreehugger Robot       Report("Hint: address points to the zero page.\n");
778*7c3d14c8STreehugger Robot       SS.Scare(10, "null-deref");
779*7c3d14c8STreehugger Robot     } else if (sig.addr == sig.pc) {
780*7c3d14c8STreehugger Robot       SS.Scare(60, "wild-jump");
781*7c3d14c8STreehugger Robot     } else if (sig.write_flag == SignalContext::WRITE) {
782*7c3d14c8STreehugger Robot       SS.Scare(30, "wild-addr-write");
783*7c3d14c8STreehugger Robot     } else if (sig.write_flag == SignalContext::READ) {
784*7c3d14c8STreehugger Robot       SS.Scare(20, "wild-addr-read");
785*7c3d14c8STreehugger Robot     } else {
786*7c3d14c8STreehugger Robot       SS.Scare(25, "wild-addr");
787*7c3d14c8STreehugger Robot     }
788*7c3d14c8STreehugger Robot   } else {
789*7c3d14c8STreehugger Robot     SS.Scare(10, "signal");
790*7c3d14c8STreehugger Robot   }
791*7c3d14c8STreehugger Robot   SS.Print();
792*7c3d14c8STreehugger Robot   GET_STACK_TRACE_SIGNAL(sig);
793*7c3d14c8STreehugger Robot   stack.Print();
794*7c3d14c8STreehugger Robot   MaybeDumpInstructionBytes(sig.pc);
795*7c3d14c8STreehugger Robot   Printf("AddressSanitizer can not provide additional info.\n");
796*7c3d14c8STreehugger Robot   ReportErrorSummary(description, &stack);
797*7c3d14c8STreehugger Robot }
798*7c3d14c8STreehugger Robot 
ReportDoubleFree(uptr addr,BufferedStackTrace * free_stack)799*7c3d14c8STreehugger Robot void ReportDoubleFree(uptr addr, BufferedStackTrace *free_stack) {
800*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
801*7c3d14c8STreehugger Robot   Decorator d;
802*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
803*7c3d14c8STreehugger Robot   char tname[128];
804*7c3d14c8STreehugger Robot   u32 curr_tid = GetCurrentTidOrInvalid();
805*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: attempting double-free on %p in "
806*7c3d14c8STreehugger Robot          "thread T%d%s:\n",
807*7c3d14c8STreehugger Robot          addr, curr_tid,
808*7c3d14c8STreehugger Robot          ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
809*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
810*7c3d14c8STreehugger Robot   CHECK_GT(free_stack->size, 0);
811*7c3d14c8STreehugger Robot   ScarinessScore::PrintSimple(42, "double-free");
812*7c3d14c8STreehugger Robot   GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
813*7c3d14c8STreehugger Robot   stack.Print();
814*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, 1);
815*7c3d14c8STreehugger Robot   ReportErrorSummary("double-free", &stack);
816*7c3d14c8STreehugger Robot }
817*7c3d14c8STreehugger Robot 
ReportNewDeleteSizeMismatch(uptr addr,uptr alloc_size,uptr delete_size,BufferedStackTrace * free_stack)818*7c3d14c8STreehugger Robot void ReportNewDeleteSizeMismatch(uptr addr, uptr alloc_size, uptr delete_size,
819*7c3d14c8STreehugger Robot                                  BufferedStackTrace *free_stack) {
820*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
821*7c3d14c8STreehugger Robot   Decorator d;
822*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
823*7c3d14c8STreehugger Robot   char tname[128];
824*7c3d14c8STreehugger Robot   u32 curr_tid = GetCurrentTidOrInvalid();
825*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: new-delete-type-mismatch on %p in "
826*7c3d14c8STreehugger Robot          "thread T%d%s:\n",
827*7c3d14c8STreehugger Robot          addr, curr_tid,
828*7c3d14c8STreehugger Robot          ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
829*7c3d14c8STreehugger Robot   Printf("%s  object passed to delete has wrong type:\n", d.EndWarning());
830*7c3d14c8STreehugger Robot   Printf("  size of the allocated type:   %zd bytes;\n"
831*7c3d14c8STreehugger Robot          "  size of the deallocated type: %zd bytes.\n",
832*7c3d14c8STreehugger Robot          alloc_size, delete_size);
833*7c3d14c8STreehugger Robot   CHECK_GT(free_stack->size, 0);
834*7c3d14c8STreehugger Robot   ScarinessScore::PrintSimple(10, "new-delete-type-mismatch");
835*7c3d14c8STreehugger Robot   GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
836*7c3d14c8STreehugger Robot   stack.Print();
837*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, 1);
838*7c3d14c8STreehugger Robot   ReportErrorSummary("new-delete-type-mismatch", &stack);
839*7c3d14c8STreehugger Robot   Report("HINT: if you don't care about these errors you may set "
840*7c3d14c8STreehugger Robot          "ASAN_OPTIONS=new_delete_type_mismatch=0\n");
841*7c3d14c8STreehugger Robot }
842*7c3d14c8STreehugger Robot 
ReportFreeNotMalloced(uptr addr,BufferedStackTrace * free_stack)843*7c3d14c8STreehugger Robot void ReportFreeNotMalloced(uptr addr, BufferedStackTrace *free_stack) {
844*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
845*7c3d14c8STreehugger Robot   Decorator d;
846*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
847*7c3d14c8STreehugger Robot   char tname[128];
848*7c3d14c8STreehugger Robot   u32 curr_tid = GetCurrentTidOrInvalid();
849*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: attempting free on address "
850*7c3d14c8STreehugger Robot              "which was not malloc()-ed: %p in thread T%d%s\n", addr,
851*7c3d14c8STreehugger Robot          curr_tid, ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)));
852*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
853*7c3d14c8STreehugger Robot   CHECK_GT(free_stack->size, 0);
854*7c3d14c8STreehugger Robot   ScarinessScore::PrintSimple(40, "bad-free");
855*7c3d14c8STreehugger Robot   GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
856*7c3d14c8STreehugger Robot   stack.Print();
857*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, 1);
858*7c3d14c8STreehugger Robot   ReportErrorSummary("bad-free", &stack);
859*7c3d14c8STreehugger Robot }
860*7c3d14c8STreehugger Robot 
ReportAllocTypeMismatch(uptr addr,BufferedStackTrace * free_stack,AllocType alloc_type,AllocType dealloc_type)861*7c3d14c8STreehugger Robot void ReportAllocTypeMismatch(uptr addr, BufferedStackTrace *free_stack,
862*7c3d14c8STreehugger Robot                              AllocType alloc_type,
863*7c3d14c8STreehugger Robot                              AllocType dealloc_type) {
864*7c3d14c8STreehugger Robot   static const char *alloc_names[] =
865*7c3d14c8STreehugger Robot     {"INVALID", "malloc", "operator new", "operator new []"};
866*7c3d14c8STreehugger Robot   static const char *dealloc_names[] =
867*7c3d14c8STreehugger Robot     {"INVALID", "free", "operator delete", "operator delete []"};
868*7c3d14c8STreehugger Robot   CHECK_NE(alloc_type, dealloc_type);
869*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
870*7c3d14c8STreehugger Robot   Decorator d;
871*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
872*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: alloc-dealloc-mismatch (%s vs %s) on %p\n",
873*7c3d14c8STreehugger Robot         alloc_names[alloc_type], dealloc_names[dealloc_type], addr);
874*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
875*7c3d14c8STreehugger Robot   CHECK_GT(free_stack->size, 0);
876*7c3d14c8STreehugger Robot   ScarinessScore::PrintSimple(10, "alloc-dealloc-mismatch");
877*7c3d14c8STreehugger Robot   GET_STACK_TRACE_FATAL(free_stack->trace[0], free_stack->top_frame_bp);
878*7c3d14c8STreehugger Robot   stack.Print();
879*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, 1);
880*7c3d14c8STreehugger Robot   ReportErrorSummary("alloc-dealloc-mismatch", &stack);
881*7c3d14c8STreehugger Robot   Report("HINT: if you don't care about these errors you may set "
882*7c3d14c8STreehugger Robot          "ASAN_OPTIONS=alloc_dealloc_mismatch=0\n");
883*7c3d14c8STreehugger Robot }
884*7c3d14c8STreehugger Robot 
ReportMallocUsableSizeNotOwned(uptr addr,BufferedStackTrace * stack)885*7c3d14c8STreehugger Robot void ReportMallocUsableSizeNotOwned(uptr addr, BufferedStackTrace *stack) {
886*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
887*7c3d14c8STreehugger Robot   Decorator d;
888*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
889*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: attempting to call "
890*7c3d14c8STreehugger Robot              "malloc_usable_size() for pointer which is "
891*7c3d14c8STreehugger Robot              "not owned: %p\n", addr);
892*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
893*7c3d14c8STreehugger Robot   stack->Print();
894*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, 1);
895*7c3d14c8STreehugger Robot   ReportErrorSummary("bad-malloc_usable_size", stack);
896*7c3d14c8STreehugger Robot }
897*7c3d14c8STreehugger Robot 
ReportSanitizerGetAllocatedSizeNotOwned(uptr addr,BufferedStackTrace * stack)898*7c3d14c8STreehugger Robot void ReportSanitizerGetAllocatedSizeNotOwned(uptr addr,
899*7c3d14c8STreehugger Robot                                              BufferedStackTrace *stack) {
900*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
901*7c3d14c8STreehugger Robot   Decorator d;
902*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
903*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: attempting to call "
904*7c3d14c8STreehugger Robot              "__sanitizer_get_allocated_size() for pointer which is "
905*7c3d14c8STreehugger Robot              "not owned: %p\n", addr);
906*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
907*7c3d14c8STreehugger Robot   stack->Print();
908*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, 1);
909*7c3d14c8STreehugger Robot   ReportErrorSummary("bad-__sanitizer_get_allocated_size", stack);
910*7c3d14c8STreehugger Robot }
911*7c3d14c8STreehugger Robot 
ReportStringFunctionMemoryRangesOverlap(const char * function,const char * offset1,uptr length1,const char * offset2,uptr length2,BufferedStackTrace * stack)912*7c3d14c8STreehugger Robot void ReportStringFunctionMemoryRangesOverlap(const char *function,
913*7c3d14c8STreehugger Robot                                              const char *offset1, uptr length1,
914*7c3d14c8STreehugger Robot                                              const char *offset2, uptr length2,
915*7c3d14c8STreehugger Robot                                              BufferedStackTrace *stack) {
916*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
917*7c3d14c8STreehugger Robot   Decorator d;
918*7c3d14c8STreehugger Robot   char bug_type[100];
919*7c3d14c8STreehugger Robot   internal_snprintf(bug_type, sizeof(bug_type), "%s-param-overlap", function);
920*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
921*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: %s: "
922*7c3d14c8STreehugger Robot              "memory ranges [%p,%p) and [%p, %p) overlap\n", \
923*7c3d14c8STreehugger Robot              bug_type, offset1, offset1 + length1, offset2, offset2 + length2);
924*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
925*7c3d14c8STreehugger Robot   ScarinessScore::PrintSimple(10, bug_type);
926*7c3d14c8STreehugger Robot   stack->Print();
927*7c3d14c8STreehugger Robot   DescribeAddress((uptr)offset1, length1, bug_type);
928*7c3d14c8STreehugger Robot   DescribeAddress((uptr)offset2, length2, bug_type);
929*7c3d14c8STreehugger Robot   ReportErrorSummary(bug_type, stack);
930*7c3d14c8STreehugger Robot }
931*7c3d14c8STreehugger Robot 
ReportStringFunctionSizeOverflow(uptr offset,uptr size,BufferedStackTrace * stack)932*7c3d14c8STreehugger Robot void ReportStringFunctionSizeOverflow(uptr offset, uptr size,
933*7c3d14c8STreehugger Robot                                       BufferedStackTrace *stack) {
934*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
935*7c3d14c8STreehugger Robot   Decorator d;
936*7c3d14c8STreehugger Robot   const char *bug_type = "negative-size-param";
937*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
938*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: %s: (size=%zd)\n", bug_type, size);
939*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
940*7c3d14c8STreehugger Robot   ScarinessScore::PrintSimple(10, bug_type);
941*7c3d14c8STreehugger Robot   stack->Print();
942*7c3d14c8STreehugger Robot   DescribeAddress(offset, size, bug_type);
943*7c3d14c8STreehugger Robot   ReportErrorSummary(bug_type, stack);
944*7c3d14c8STreehugger Robot }
945*7c3d14c8STreehugger Robot 
ReportBadParamsToAnnotateContiguousContainer(uptr beg,uptr end,uptr old_mid,uptr new_mid,BufferedStackTrace * stack)946*7c3d14c8STreehugger Robot void ReportBadParamsToAnnotateContiguousContainer(uptr beg, uptr end,
947*7c3d14c8STreehugger Robot                                                   uptr old_mid, uptr new_mid,
948*7c3d14c8STreehugger Robot                                                   BufferedStackTrace *stack) {
949*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
950*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: bad parameters to "
951*7c3d14c8STreehugger Robot          "__sanitizer_annotate_contiguous_container:\n"
952*7c3d14c8STreehugger Robot          "      beg     : %p\n"
953*7c3d14c8STreehugger Robot          "      end     : %p\n"
954*7c3d14c8STreehugger Robot          "      old_mid : %p\n"
955*7c3d14c8STreehugger Robot          "      new_mid : %p\n",
956*7c3d14c8STreehugger Robot          beg, end, old_mid, new_mid);
957*7c3d14c8STreehugger Robot   uptr granularity = SHADOW_GRANULARITY;
958*7c3d14c8STreehugger Robot   if (!IsAligned(beg, granularity))
959*7c3d14c8STreehugger Robot     Report("ERROR: beg is not aligned by %d\n", granularity);
960*7c3d14c8STreehugger Robot   stack->Print();
961*7c3d14c8STreehugger Robot   ReportErrorSummary("bad-__sanitizer_annotate_contiguous_container", stack);
962*7c3d14c8STreehugger Robot }
963*7c3d14c8STreehugger Robot 
ReportODRViolation(const __asan_global * g1,u32 stack_id1,const __asan_global * g2,u32 stack_id2)964*7c3d14c8STreehugger Robot void ReportODRViolation(const __asan_global *g1, u32 stack_id1,
965*7c3d14c8STreehugger Robot                         const __asan_global *g2, u32 stack_id2) {
966*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
967*7c3d14c8STreehugger Robot   Decorator d;
968*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
969*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: odr-violation (%p):\n", g1->beg);
970*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
971*7c3d14c8STreehugger Robot   InternalScopedString g1_loc(256), g2_loc(256);
972*7c3d14c8STreehugger Robot   PrintGlobalLocation(&g1_loc, *g1);
973*7c3d14c8STreehugger Robot   PrintGlobalLocation(&g2_loc, *g2);
974*7c3d14c8STreehugger Robot   Printf("  [1] size=%zd '%s' %s\n", g1->size,
975*7c3d14c8STreehugger Robot          MaybeDemangleGlobalName(g1->name), g1_loc.data());
976*7c3d14c8STreehugger Robot   Printf("  [2] size=%zd '%s' %s\n", g2->size,
977*7c3d14c8STreehugger Robot          MaybeDemangleGlobalName(g2->name), g2_loc.data());
978*7c3d14c8STreehugger Robot   if (stack_id1 && stack_id2) {
979*7c3d14c8STreehugger Robot     Printf("These globals were registered at these points:\n");
980*7c3d14c8STreehugger Robot     Printf("  [1]:\n");
981*7c3d14c8STreehugger Robot     StackDepotGet(stack_id1).Print();
982*7c3d14c8STreehugger Robot     Printf("  [2]:\n");
983*7c3d14c8STreehugger Robot     StackDepotGet(stack_id2).Print();
984*7c3d14c8STreehugger Robot   }
985*7c3d14c8STreehugger Robot   Report("HINT: if you don't care about these errors you may set "
986*7c3d14c8STreehugger Robot          "ASAN_OPTIONS=detect_odr_violation=0\n");
987*7c3d14c8STreehugger Robot   InternalScopedString error_msg(256);
988*7c3d14c8STreehugger Robot   error_msg.append("odr-violation: global '%s' at %s",
989*7c3d14c8STreehugger Robot                    MaybeDemangleGlobalName(g1->name), g1_loc.data());
990*7c3d14c8STreehugger Robot   ReportErrorSummary(error_msg.data());
991*7c3d14c8STreehugger Robot }
992*7c3d14c8STreehugger Robot 
993*7c3d14c8STreehugger Robot // ----------------------- CheckForInvalidPointerPair ----------- {{{1
994*7c3d14c8STreehugger Robot static NOINLINE void
ReportInvalidPointerPair(uptr pc,uptr bp,uptr sp,uptr a1,uptr a2)995*7c3d14c8STreehugger Robot ReportInvalidPointerPair(uptr pc, uptr bp, uptr sp, uptr a1, uptr a2) {
996*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
997*7c3d14c8STreehugger Robot   const char *bug_type = "invalid-pointer-pair";
998*7c3d14c8STreehugger Robot   Decorator d;
999*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
1000*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: invalid-pointer-pair: %p %p\n", a1, a2);
1001*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
1002*7c3d14c8STreehugger Robot   GET_STACK_TRACE_FATAL(pc, bp);
1003*7c3d14c8STreehugger Robot   stack.Print();
1004*7c3d14c8STreehugger Robot   DescribeAddress(a1, 1, bug_type);
1005*7c3d14c8STreehugger Robot   DescribeAddress(a2, 1, bug_type);
1006*7c3d14c8STreehugger Robot   ReportErrorSummary(bug_type, &stack);
1007*7c3d14c8STreehugger Robot }
1008*7c3d14c8STreehugger Robot 
CheckForInvalidPointerPair(void * p1,void * p2)1009*7c3d14c8STreehugger Robot static INLINE void CheckForInvalidPointerPair(void *p1, void *p2) {
1010*7c3d14c8STreehugger Robot   if (!flags()->detect_invalid_pointer_pairs) return;
1011*7c3d14c8STreehugger Robot   uptr a1 = reinterpret_cast<uptr>(p1);
1012*7c3d14c8STreehugger Robot   uptr a2 = reinterpret_cast<uptr>(p2);
1013*7c3d14c8STreehugger Robot   AsanChunkView chunk1 = FindHeapChunkByAddress(a1);
1014*7c3d14c8STreehugger Robot   AsanChunkView chunk2 = FindHeapChunkByAddress(a2);
1015*7c3d14c8STreehugger Robot   bool valid1 = chunk1.IsAllocated();
1016*7c3d14c8STreehugger Robot   bool valid2 = chunk2.IsAllocated();
1017*7c3d14c8STreehugger Robot   if (!valid1 || !valid2 || !chunk1.Eq(chunk2)) {
1018*7c3d14c8STreehugger Robot     GET_CALLER_PC_BP_SP;
1019*7c3d14c8STreehugger Robot     return ReportInvalidPointerPair(pc, bp, sp, a1, a2);
1020*7c3d14c8STreehugger Robot   }
1021*7c3d14c8STreehugger Robot }
1022*7c3d14c8STreehugger Robot // ----------------------- Mac-specific reports ----------------- {{{1
1023*7c3d14c8STreehugger Robot 
ReportMacMzReallocUnknown(uptr addr,uptr zone_ptr,const char * zone_name,BufferedStackTrace * stack)1024*7c3d14c8STreehugger Robot void ReportMacMzReallocUnknown(uptr addr, uptr zone_ptr, const char *zone_name,
1025*7c3d14c8STreehugger Robot                                BufferedStackTrace *stack) {
1026*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report;
1027*7c3d14c8STreehugger Robot   Printf("mz_realloc(%p) -- attempting to realloc unallocated memory.\n"
1028*7c3d14c8STreehugger Robot              "This is an unrecoverable problem, exiting now.\n",
1029*7c3d14c8STreehugger Robot              addr);
1030*7c3d14c8STreehugger Robot   PrintZoneForPointer(addr, zone_ptr, zone_name);
1031*7c3d14c8STreehugger Robot   stack->Print();
1032*7c3d14c8STreehugger Robot   DescribeHeapAddress(addr, 1);
1033*7c3d14c8STreehugger Robot }
1034*7c3d14c8STreehugger Robot 
1035*7c3d14c8STreehugger Robot // -------------- SuppressErrorReport -------------- {{{1
1036*7c3d14c8STreehugger Robot // Avoid error reports duplicating for ASan recover mode.
SuppressErrorReport(uptr pc)1037*7c3d14c8STreehugger Robot static bool SuppressErrorReport(uptr pc) {
1038*7c3d14c8STreehugger Robot   if (!common_flags()->suppress_equal_pcs) return false;
1039*7c3d14c8STreehugger Robot   for (unsigned i = 0; i < kAsanBuggyPcPoolSize; i++) {
1040*7c3d14c8STreehugger Robot     uptr cmp = atomic_load_relaxed(&AsanBuggyPcPool[i]);
1041*7c3d14c8STreehugger Robot     if (cmp == 0 && atomic_compare_exchange_strong(&AsanBuggyPcPool[i], &cmp,
1042*7c3d14c8STreehugger Robot                                                    pc, memory_order_relaxed))
1043*7c3d14c8STreehugger Robot       return false;
1044*7c3d14c8STreehugger Robot     if (cmp == pc) return true;
1045*7c3d14c8STreehugger Robot   }
1046*7c3d14c8STreehugger Robot   Die();
1047*7c3d14c8STreehugger Robot }
1048*7c3d14c8STreehugger Robot 
PrintContainerOverflowHint()1049*7c3d14c8STreehugger Robot static void PrintContainerOverflowHint() {
1050*7c3d14c8STreehugger Robot   Printf("HINT: if you don't care about these errors you may set "
1051*7c3d14c8STreehugger Robot          "ASAN_OPTIONS=detect_container_overflow=0.\n"
1052*7c3d14c8STreehugger Robot          "If you suspect a false positive see also: "
1053*7c3d14c8STreehugger Robot          "https://github.com/google/sanitizers/wiki/"
1054*7c3d14c8STreehugger Robot          "AddressSanitizerContainerOverflow.\n");
1055*7c3d14c8STreehugger Robot }
1056*7c3d14c8STreehugger Robot 
AdjacentShadowValuesAreFullyPoisoned(u8 * s)1057*7c3d14c8STreehugger Robot static bool AdjacentShadowValuesAreFullyPoisoned(u8 *s) {
1058*7c3d14c8STreehugger Robot   return s[-1] > 127 && s[1] > 127;
1059*7c3d14c8STreehugger Robot }
1060*7c3d14c8STreehugger Robot 
ReportGenericError(uptr pc,uptr bp,uptr sp,uptr addr,bool is_write,uptr access_size,u32 exp,bool fatal)1061*7c3d14c8STreehugger Robot void ReportGenericError(uptr pc, uptr bp, uptr sp, uptr addr, bool is_write,
1062*7c3d14c8STreehugger Robot                         uptr access_size, u32 exp, bool fatal) {
1063*7c3d14c8STreehugger Robot   if (!fatal && SuppressErrorReport(pc)) return;
1064*7c3d14c8STreehugger Robot   ENABLE_FRAME_POINTER;
1065*7c3d14c8STreehugger Robot   ScarinessScore SS;
1066*7c3d14c8STreehugger Robot 
1067*7c3d14c8STreehugger Robot   if (access_size) {
1068*7c3d14c8STreehugger Robot     if (access_size <= 9) {
1069*7c3d14c8STreehugger Robot       char desr[] = "?-byte";
1070*7c3d14c8STreehugger Robot       desr[0] = '0' + access_size;
1071*7c3d14c8STreehugger Robot       SS.Scare(access_size + access_size / 2, desr);
1072*7c3d14c8STreehugger Robot     } else if (access_size >= 10) {
1073*7c3d14c8STreehugger Robot       SS.Scare(15, "multi-byte");
1074*7c3d14c8STreehugger Robot     }
1075*7c3d14c8STreehugger Robot     is_write ? SS.Scare(20, "write") : SS.Scare(1, "read");
1076*7c3d14c8STreehugger Robot   }
1077*7c3d14c8STreehugger Robot 
1078*7c3d14c8STreehugger Robot   // Optimization experiments.
1079*7c3d14c8STreehugger Robot   // The experiments can be used to evaluate potential optimizations that remove
1080*7c3d14c8STreehugger Robot   // instrumentation (assess false negatives). Instead of completely removing
1081*7c3d14c8STreehugger Robot   // some instrumentation, compiler can emit special calls into runtime
1082*7c3d14c8STreehugger Robot   // (e.g. __asan_report_exp_load1 instead of __asan_report_load1) and pass
1083*7c3d14c8STreehugger Robot   // mask of experiments (exp).
1084*7c3d14c8STreehugger Robot   // The reaction to a non-zero value of exp is to be defined.
1085*7c3d14c8STreehugger Robot   (void)exp;
1086*7c3d14c8STreehugger Robot 
1087*7c3d14c8STreehugger Robot   // Determine the error type.
1088*7c3d14c8STreehugger Robot   const char *bug_descr = "unknown-crash";
1089*7c3d14c8STreehugger Robot   u8 shadow_val = 0;
1090*7c3d14c8STreehugger Robot   if (AddrIsInMem(addr)) {
1091*7c3d14c8STreehugger Robot     u8 *shadow_addr = (u8*)MemToShadow(addr);
1092*7c3d14c8STreehugger Robot     // If we are accessing 16 bytes, look at the second shadow byte.
1093*7c3d14c8STreehugger Robot     if (*shadow_addr == 0 && access_size > SHADOW_GRANULARITY)
1094*7c3d14c8STreehugger Robot       shadow_addr++;
1095*7c3d14c8STreehugger Robot     // If we are in the partial right redzone, look at the next shadow byte.
1096*7c3d14c8STreehugger Robot     if (*shadow_addr > 0 && *shadow_addr < 128)
1097*7c3d14c8STreehugger Robot       shadow_addr++;
1098*7c3d14c8STreehugger Robot     bool far_from_bounds = false;
1099*7c3d14c8STreehugger Robot     shadow_val = *shadow_addr;
1100*7c3d14c8STreehugger Robot     int bug_type_score = 0;
1101*7c3d14c8STreehugger Robot     // For use-after-frees reads are almost as bad as writes.
1102*7c3d14c8STreehugger Robot     int read_after_free_bonus = 0;
1103*7c3d14c8STreehugger Robot     switch (shadow_val) {
1104*7c3d14c8STreehugger Robot       case kAsanHeapLeftRedzoneMagic:
1105*7c3d14c8STreehugger Robot       case kAsanHeapRightRedzoneMagic:
1106*7c3d14c8STreehugger Robot       case kAsanArrayCookieMagic:
1107*7c3d14c8STreehugger Robot         bug_descr = "heap-buffer-overflow";
1108*7c3d14c8STreehugger Robot         bug_type_score = 10;
1109*7c3d14c8STreehugger Robot         far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
1110*7c3d14c8STreehugger Robot         break;
1111*7c3d14c8STreehugger Robot       case kAsanHeapFreeMagic:
1112*7c3d14c8STreehugger Robot         bug_descr = "heap-use-after-free";
1113*7c3d14c8STreehugger Robot         bug_type_score = 20;
1114*7c3d14c8STreehugger Robot         if (!is_write) read_after_free_bonus = 18;
1115*7c3d14c8STreehugger Robot         break;
1116*7c3d14c8STreehugger Robot       case kAsanStackLeftRedzoneMagic:
1117*7c3d14c8STreehugger Robot         bug_descr = "stack-buffer-underflow";
1118*7c3d14c8STreehugger Robot         bug_type_score = 25;
1119*7c3d14c8STreehugger Robot         far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
1120*7c3d14c8STreehugger Robot         break;
1121*7c3d14c8STreehugger Robot       case kAsanInitializationOrderMagic:
1122*7c3d14c8STreehugger Robot         bug_descr = "initialization-order-fiasco";
1123*7c3d14c8STreehugger Robot         bug_type_score = 1;
1124*7c3d14c8STreehugger Robot         break;
1125*7c3d14c8STreehugger Robot       case kAsanStackMidRedzoneMagic:
1126*7c3d14c8STreehugger Robot       case kAsanStackRightRedzoneMagic:
1127*7c3d14c8STreehugger Robot       case kAsanStackPartialRedzoneMagic:
1128*7c3d14c8STreehugger Robot         bug_descr = "stack-buffer-overflow";
1129*7c3d14c8STreehugger Robot         bug_type_score = 25;
1130*7c3d14c8STreehugger Robot         far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
1131*7c3d14c8STreehugger Robot         break;
1132*7c3d14c8STreehugger Robot       case kAsanStackAfterReturnMagic:
1133*7c3d14c8STreehugger Robot         bug_descr = "stack-use-after-return";
1134*7c3d14c8STreehugger Robot         bug_type_score = 30;
1135*7c3d14c8STreehugger Robot         if (!is_write) read_after_free_bonus = 18;
1136*7c3d14c8STreehugger Robot         break;
1137*7c3d14c8STreehugger Robot       case kAsanUserPoisonedMemoryMagic:
1138*7c3d14c8STreehugger Robot         bug_descr = "use-after-poison";
1139*7c3d14c8STreehugger Robot         bug_type_score = 20;
1140*7c3d14c8STreehugger Robot         break;
1141*7c3d14c8STreehugger Robot       case kAsanContiguousContainerOOBMagic:
1142*7c3d14c8STreehugger Robot         bug_descr = "container-overflow";
1143*7c3d14c8STreehugger Robot         bug_type_score = 10;
1144*7c3d14c8STreehugger Robot         break;
1145*7c3d14c8STreehugger Robot       case kAsanStackUseAfterScopeMagic:
1146*7c3d14c8STreehugger Robot         bug_descr = "stack-use-after-scope";
1147*7c3d14c8STreehugger Robot         bug_type_score = 10;
1148*7c3d14c8STreehugger Robot         break;
1149*7c3d14c8STreehugger Robot       case kAsanGlobalRedzoneMagic:
1150*7c3d14c8STreehugger Robot         bug_descr = "global-buffer-overflow";
1151*7c3d14c8STreehugger Robot         bug_type_score = 10;
1152*7c3d14c8STreehugger Robot         far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
1153*7c3d14c8STreehugger Robot         break;
1154*7c3d14c8STreehugger Robot       case kAsanIntraObjectRedzone:
1155*7c3d14c8STreehugger Robot         bug_descr = "intra-object-overflow";
1156*7c3d14c8STreehugger Robot         bug_type_score = 10;
1157*7c3d14c8STreehugger Robot         break;
1158*7c3d14c8STreehugger Robot       case kAsanAllocaLeftMagic:
1159*7c3d14c8STreehugger Robot       case kAsanAllocaRightMagic:
1160*7c3d14c8STreehugger Robot         bug_descr = "dynamic-stack-buffer-overflow";
1161*7c3d14c8STreehugger Robot         bug_type_score = 25;
1162*7c3d14c8STreehugger Robot         far_from_bounds = AdjacentShadowValuesAreFullyPoisoned(shadow_addr);
1163*7c3d14c8STreehugger Robot         break;
1164*7c3d14c8STreehugger Robot     }
1165*7c3d14c8STreehugger Robot     SS.Scare(bug_type_score + read_after_free_bonus, bug_descr);
1166*7c3d14c8STreehugger Robot     if (far_from_bounds)
1167*7c3d14c8STreehugger Robot       SS.Scare(10, "far-from-bounds");
1168*7c3d14c8STreehugger Robot   }
1169*7c3d14c8STreehugger Robot 
1170*7c3d14c8STreehugger Robot   ReportData report = { pc, sp, bp, addr, (bool)is_write, access_size,
1171*7c3d14c8STreehugger Robot                         bug_descr };
1172*7c3d14c8STreehugger Robot   ScopedInErrorReport in_report(&report, fatal);
1173*7c3d14c8STreehugger Robot 
1174*7c3d14c8STreehugger Robot   Decorator d;
1175*7c3d14c8STreehugger Robot   Printf("%s", d.Warning());
1176*7c3d14c8STreehugger Robot   Report("ERROR: AddressSanitizer: %s on address "
1177*7c3d14c8STreehugger Robot              "%p at pc %p bp %p sp %p\n",
1178*7c3d14c8STreehugger Robot              bug_descr, (void*)addr, pc, bp, sp);
1179*7c3d14c8STreehugger Robot   Printf("%s", d.EndWarning());
1180*7c3d14c8STreehugger Robot 
1181*7c3d14c8STreehugger Robot   u32 curr_tid = GetCurrentTidOrInvalid();
1182*7c3d14c8STreehugger Robot   char tname[128];
1183*7c3d14c8STreehugger Robot   Printf("%s%s of size %zu at %p thread T%d%s%s\n",
1184*7c3d14c8STreehugger Robot          d.Access(),
1185*7c3d14c8STreehugger Robot          access_size ? (is_write ? "WRITE" : "READ") : "ACCESS",
1186*7c3d14c8STreehugger Robot          access_size, (void*)addr, curr_tid,
1187*7c3d14c8STreehugger Robot          ThreadNameWithParenthesis(curr_tid, tname, sizeof(tname)),
1188*7c3d14c8STreehugger Robot          d.EndAccess());
1189*7c3d14c8STreehugger Robot 
1190*7c3d14c8STreehugger Robot   SS.Print();
1191*7c3d14c8STreehugger Robot   GET_STACK_TRACE_FATAL(pc, bp);
1192*7c3d14c8STreehugger Robot   stack.Print();
1193*7c3d14c8STreehugger Robot 
1194*7c3d14c8STreehugger Robot   DescribeAddress(addr, access_size, bug_descr);
1195*7c3d14c8STreehugger Robot   if (shadow_val == kAsanContiguousContainerOOBMagic)
1196*7c3d14c8STreehugger Robot     PrintContainerOverflowHint();
1197*7c3d14c8STreehugger Robot   ReportErrorSummary(bug_descr, &stack);
1198*7c3d14c8STreehugger Robot   PrintShadowMemoryForAddress(addr);
1199*7c3d14c8STreehugger Robot }
1200*7c3d14c8STreehugger Robot 
1201*7c3d14c8STreehugger Robot }  // namespace __asan
1202*7c3d14c8STreehugger Robot 
1203*7c3d14c8STreehugger Robot // --------------------------- Interface --------------------- {{{1
1204*7c3d14c8STreehugger Robot using namespace __asan;  // NOLINT
1205*7c3d14c8STreehugger Robot 
__asan_report_error(uptr pc,uptr bp,uptr sp,uptr addr,int is_write,uptr access_size,u32 exp)1206*7c3d14c8STreehugger Robot void __asan_report_error(uptr pc, uptr bp, uptr sp, uptr addr, int is_write,
1207*7c3d14c8STreehugger Robot                          uptr access_size, u32 exp) {
1208*7c3d14c8STreehugger Robot   ENABLE_FRAME_POINTER;
1209*7c3d14c8STreehugger Robot   bool fatal = flags()->halt_on_error;
1210*7c3d14c8STreehugger Robot   ReportGenericError(pc, bp, sp, addr, is_write, access_size, exp, fatal);
1211*7c3d14c8STreehugger Robot }
1212*7c3d14c8STreehugger Robot 
__asan_set_error_report_callback(void (* callback)(const char *))1213*7c3d14c8STreehugger Robot void NOINLINE __asan_set_error_report_callback(void (*callback)(const char*)) {
1214*7c3d14c8STreehugger Robot   BlockingMutexLock l(&error_message_buf_mutex);
1215*7c3d14c8STreehugger Robot   error_report_callback = callback;
1216*7c3d14c8STreehugger Robot }
1217*7c3d14c8STreehugger Robot 
__asan_describe_address(uptr addr)1218*7c3d14c8STreehugger Robot void __asan_describe_address(uptr addr) {
1219*7c3d14c8STreehugger Robot   // Thread registry must be locked while we're describing an address.
1220*7c3d14c8STreehugger Robot   asanThreadRegistry().Lock();
1221*7c3d14c8STreehugger Robot   DescribeAddress(addr, 1, "");
1222*7c3d14c8STreehugger Robot   asanThreadRegistry().Unlock();
1223*7c3d14c8STreehugger Robot }
1224*7c3d14c8STreehugger Robot 
__asan_report_present()1225*7c3d14c8STreehugger Robot int __asan_report_present() {
1226*7c3d14c8STreehugger Robot   return report_happened ? 1 : 0;
1227*7c3d14c8STreehugger Robot }
1228*7c3d14c8STreehugger Robot 
__asan_get_report_pc()1229*7c3d14c8STreehugger Robot uptr __asan_get_report_pc() {
1230*7c3d14c8STreehugger Robot   return report_data.pc;
1231*7c3d14c8STreehugger Robot }
1232*7c3d14c8STreehugger Robot 
__asan_get_report_bp()1233*7c3d14c8STreehugger Robot uptr __asan_get_report_bp() {
1234*7c3d14c8STreehugger Robot   return report_data.bp;
1235*7c3d14c8STreehugger Robot }
1236*7c3d14c8STreehugger Robot 
__asan_get_report_sp()1237*7c3d14c8STreehugger Robot uptr __asan_get_report_sp() {
1238*7c3d14c8STreehugger Robot   return report_data.sp;
1239*7c3d14c8STreehugger Robot }
1240*7c3d14c8STreehugger Robot 
__asan_get_report_address()1241*7c3d14c8STreehugger Robot uptr __asan_get_report_address() {
1242*7c3d14c8STreehugger Robot   return report_data.addr;
1243*7c3d14c8STreehugger Robot }
1244*7c3d14c8STreehugger Robot 
__asan_get_report_access_type()1245*7c3d14c8STreehugger Robot int __asan_get_report_access_type() {
1246*7c3d14c8STreehugger Robot   return report_data.is_write ? 1 : 0;
1247*7c3d14c8STreehugger Robot }
1248*7c3d14c8STreehugger Robot 
__asan_get_report_access_size()1249*7c3d14c8STreehugger Robot uptr __asan_get_report_access_size() {
1250*7c3d14c8STreehugger Robot   return report_data.access_size;
1251*7c3d14c8STreehugger Robot }
1252*7c3d14c8STreehugger Robot 
__asan_get_report_description()1253*7c3d14c8STreehugger Robot const char *__asan_get_report_description() {
1254*7c3d14c8STreehugger Robot   return report_data.description;
1255*7c3d14c8STreehugger Robot }
1256*7c3d14c8STreehugger Robot 
1257*7c3d14c8STreehugger Robot extern "C" {
1258*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_ptr_sub(void * a,void * b)1259*7c3d14c8STreehugger Robot void __sanitizer_ptr_sub(void *a, void *b) {
1260*7c3d14c8STreehugger Robot   CheckForInvalidPointerPair(a, b);
1261*7c3d14c8STreehugger Robot }
1262*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
__sanitizer_ptr_cmp(void * a,void * b)1263*7c3d14c8STreehugger Robot void __sanitizer_ptr_cmp(void *a, void *b) {
1264*7c3d14c8STreehugger Robot   CheckForInvalidPointerPair(a, b);
1265*7c3d14c8STreehugger Robot }
1266*7c3d14c8STreehugger Robot } // extern "C"
1267*7c3d14c8STreehugger Robot 
1268*7c3d14c8STreehugger Robot #if !SANITIZER_SUPPORTS_WEAK_HOOKS
1269*7c3d14c8STreehugger Robot // Provide default implementation of __asan_on_error that does nothing
1270*7c3d14c8STreehugger Robot // and may be overriden by user.
1271*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
__asan_on_error()1272*7c3d14c8STreehugger Robot void __asan_on_error() {}
1273*7c3d14c8STreehugger Robot #endif
1274