Debian Codegraph @

libsanitizer/asan/asan_descriptions.cpp

raw
1 //===-- asan_descriptions.cpp -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // ASan functions for getting information about an address and/or printing it.
12 //===----------------------------------------------------------------------===//
13
14 #include "asan_descriptions.h"
15 #include "asan_mapping.h"
16 #include "asan_report.h"
17 #include "asan_stack.h"
18 #include "sanitizer_common/sanitizer_stackdepot.h"
19
20 namespace __asan {
21
22 AsanThreadIdAndName::AsanThreadIdAndName(AsanThreadContext *t) {
23 if (!t) {
24 internal_snprintf(name, sizeof(name), "T-1");
25 return;
26 }
27 int len = internal_snprintf(name, sizeof(name), "T%llu", t->unique_id);
28 CHECK(((unsigned int)len) < sizeof(name));
29 if (internal_strlen(t->name))
30 internal_snprintf(&name[len], sizeof(name) - len, " (%s)", t->name);
31 }
32
33 AsanThreadIdAndName::AsanThreadIdAndName(u32 tid)
34 : AsanThreadIdAndName(
35 tid == kInvalidTid ? nullptr : GetThreadContextByTidLocked(tid)) {
36 asanThreadRegistry().CheckLocked();
37 }
38
39 void DescribeThread(AsanThreadContext *context) {
40 CHECK(context);
41 asanThreadRegistry().CheckLocked();
42 // No need to announce the main thread.
43 if (context->tid == kMainTid || context->announced) {
44 return;
45 }
46 context->announced = true;
47
48 AsanThreadContext *parent_context =
49 context->parent_tid == kInvalidTid
50 ? nullptr
51 : GetThreadContextByTidLocked(context->parent_tid);
52
53 // `context->parent_tid` may point to reused slot. Check `unique_id` which
54 // is always smaller for the parent, always greater for a new user.
55 if (context->unique_id <= parent_context->unique_id)
56 parent_context = nullptr;
57
58 InternalScopedString str;
59 str.AppendF("Thread %s", AsanThreadIdAndName(context).c_str());
60 if (!parent_context) {
61 str.Append(" created by unknown thread\n");
62 Printf("%s", str.data());
63 return;
64 }
65 str.AppendF(" created by %s here:\n",
66 AsanThreadIdAndName(context->parent_tid).c_str());
67 Printf("%s", str.data());
68 StackDepotGet(context->stack_id).Print();
69 // Recursively described parent thread if needed.
70 if (flags()->print_full_thread_history)
71 DescribeThread(parent_context);
72 }
73
74 // Shadow descriptions
75 static bool GetShadowKind(uptr addr, ShadowKind *shadow_kind) {
76 CHECK(!AddrIsInMem(addr));
77 if (AddrIsInShadowGap(addr)) {
78 *shadow_kind = kShadowKindGap;
79 } else if (AddrIsInHighShadow(addr)) {
80 *shadow_kind = kShadowKindHigh;
81 } else if (AddrIsInLowShadow(addr)) {
82 *shadow_kind = kShadowKindLow;
83 } else {
84 return false;
85 }
86 return true;
87 }
88
89 bool DescribeAddressIfShadow(uptr addr) {
90 ShadowAddressDescription descr;
91 if (!GetShadowAddressInformation(addr, &descr)) return false;
92 descr.Print();
93 return true;
94 }
95
96 bool GetShadowAddressInformation(uptr addr, ShadowAddressDescription *descr) {
97 if (AddrIsInMem(addr)) return false;
98 ShadowKind shadow_kind;
99 if (!GetShadowKind(addr, &shadow_kind)) return false;
100 if (shadow_kind != kShadowKindGap) descr->shadow_byte = *(u8 *)addr;
101 descr->addr = addr;
102 descr->kind = shadow_kind;
103 return true;
104 }
105
106 // Heap descriptions
107 static void GetAccessToHeapChunkInformation(ChunkAccess *descr,
108 AsanChunkView chunk, uptr addr,
109 uptr access_size) {
110 descr->bad_addr = addr;
111 if (chunk.AddrIsAtLeft(addr, access_size, &descr->offset)) {
112 descr->access_type = kAccessTypeLeft;
113 } else if (chunk.AddrIsAtRight(addr, access_size, &descr->offset)) {
114 descr->access_type = kAccessTypeRight;
115 if (descr->offset < 0) {
116 descr->bad_addr -= descr->offset;
117 descr->offset = 0;
118 }
119 } else if (chunk.AddrIsInside(addr, access_size, &descr->offset)) {
120 descr->access_type = kAccessTypeInside;
121 } else {
122 descr->access_type = kAccessTypeUnknown;
123 }
124 descr->chunk_begin = chunk.Beg();
125 descr->chunk_size = chunk.UsedSize();
126 descr->user_requested_alignment = chunk.UserRequestedAlignment();
127 descr->alloc_type = chunk.GetAllocType();
128 }
129
130 static void PrintHeapChunkAccess(uptr addr, const ChunkAccess &descr) {
131 Decorator d;
132 InternalScopedString str;
133 str.Append(d.Location());
134 switch (descr.access_type) {
135 case kAccessTypeLeft:
136 str.AppendF("%p is located %zd bytes before", (void *)descr.bad_addr,
137 descr.offset);
138 break;
139 case kAccessTypeRight:
140 str.AppendF("%p is located %zd bytes after", (void *)descr.bad_addr,
141 descr.offset);
142 break;
143 case kAccessTypeInside:
144 str.AppendF("%p is located %zd bytes inside of", (void *)descr.bad_addr,
145 descr.offset);
146 break;
147 case kAccessTypeUnknown:
148 str.AppendF(
149 "%p is located somewhere around (this is AddressSanitizer bug!)",
150 (void *)descr.bad_addr);
151 }
152 str.AppendF(" %zu-byte region [%p,%p)\n", descr.chunk_size,
153 (void *)descr.chunk_begin,
154 (void *)(descr.chunk_begin + descr.chunk_size));
155 str.Append(d.Default());
156 Printf("%s", str.data());
157 }
158
159 bool GetHeapAddressInformation(uptr addr, uptr access_size,
160 HeapAddressDescription *descr) {
161 AsanChunkView chunk = FindHeapChunkByAddress(addr);
162 if (!chunk.IsValid()) {
163 return false;
164 }
165 descr->addr = addr;
166 GetAccessToHeapChunkInformation(&descr->chunk_access, chunk, addr,
167 access_size);
168 CHECK_NE(chunk.AllocTid(), kInvalidTid);
169 descr->alloc_tid = chunk.AllocTid();
170 descr->alloc_stack_id = chunk.GetAllocStackId();
171 descr->free_tid = chunk.FreeTid();
172 if (descr->free_tid != kInvalidTid)
173 descr->free_stack_id = chunk.GetFreeStackId();
174 return true;
175 }
176
177 static StackTrace GetStackTraceFromId(u32 id) {
178 CHECK(id);
179 StackTrace res = StackDepotGet(id);
180 CHECK(res.trace);
181 return res;
182 }
183
184 bool DescribeAddressIfHeap(uptr addr, uptr access_size) {
185 HeapAddressDescription descr;
186 if (!GetHeapAddressInformation(addr, access_size, &descr)) {
187 Printf(
188 "AddressSanitizer can not describe address in more detail "
189 "(wild memory access suspected).\n");
190 return false;
191 }
192 descr.Print();
193 return true;
194 }
195
196 // Stack descriptions
197 bool GetStackAddressInformation(uptr addr, uptr access_size,
198 StackAddressDescription *descr) {
199 AsanThread *t = FindThreadByStackAddress(addr);
200 if (!t) return false;
201
202 descr->addr = addr;
203 descr->tid = t->tid();
204 // Try to fetch precise stack frame for this access.
205 AsanThread::StackFrameAccess access;
206 if (!t->GetStackFrameAccessByAddr(addr, &access)) {
207 descr->frame_descr = nullptr;
208 return true;
209 }
210
211 descr->offset = access.offset;
212 descr->access_size = access_size;
213 descr->frame_pc = access.frame_pc;
214 descr->frame_descr = access.frame_descr;
215
216 #if SANITIZER_PPC64V1
217 // On PowerPC64 ELFv1, the address of a function actually points to a
218 // three-doubleword data structure with the first field containing
219 // the address of the function's code.
220 descr->frame_pc = *reinterpret_cast<uptr *>(descr->frame_pc);
221 #endif
222 descr->frame_pc += 16;
223
224 return true;
225 }
226
227 static void PrintAccessAndVarIntersection(const StackVarDescr &var, uptr addr,
228 uptr access_size, uptr prev_var_end,
229 uptr next_var_beg) {
230 uptr var_end = var.beg + var.size;
231 uptr addr_end = addr + access_size;
232 const char *pos_descr = nullptr;
233 // If the variable [var.beg, var_end) is the nearest variable to the
234 // current memory access, indicate it in the log.
235 if (addr >= var.beg) {
236 if (addr_end <= var_end)
237 pos_descr = "is inside"; // May happen if this is a use-after-return.
238 else if (addr < var_end)
239 pos_descr = "partially overflows";
240 else if (addr_end <= next_var_beg &&
241 next_var_beg - addr_end >= addr - var_end)
242 pos_descr = "overflows";
243 } else {
244 if (addr_end > var.beg)
245 pos_descr = "partially underflows";
246 else if (addr >= prev_var_end && addr - prev_var_end >= var.beg - addr_end)
247 pos_descr = "underflows";
248 }
249 InternalScopedString str;
250 str.AppendF(" [%zd, %zd)", var.beg, var_end);
251 // Render variable name.
252 str.Append(" '");
253 for (uptr i = 0; i < var.name_len; ++i) {
254 str.AppendF("%c", var.name_pos[i]);
255 }
256 str.Append("'");
257 if (var.line > 0) {
258 str.AppendF(" (line %zd)", var.line);
259 }
260 if (pos_descr) {
261 Decorator d;
262 // FIXME: we may want to also print the size of the access here,
263 // but in case of accesses generated by memset it may be confusing.
264 str.AppendF("%s <== Memory access at offset %zd %s this variable%s\n",
265 d.Location(), addr, pos_descr, d.Default());
266 } else {
267 str.Append("\n");
268 }
269 Printf("%s", str.data());
270 }
271
272 bool DescribeAddressIfStack(uptr addr, uptr access_size) {
273 StackAddressDescription descr;
274 if (!GetStackAddressInformation(addr, access_size, &descr)) return false;
275 descr.Print();
276 return true;
277 }
278
279 // Global descriptions
280 static void DescribeAddressRelativeToGlobal(uptr addr, uptr access_size,
281 const __asan_global &g) {
282 InternalScopedString str;
283 Decorator d;
284 str.Append(d.Location());
285 if (addr < g.beg) {
286 str.AppendF("%p is located %zd bytes before", (void *)addr, g.beg - addr);
287 } else if (addr + access_size > g.beg + g.size) {
288 if (addr < g.beg + g.size) addr = g.beg + g.size;
289 str.AppendF("%p is located %zd bytes after", (void *)addr,
290 addr - (g.beg + g.size));
291 } else {
292 // Can it happen?
293 str.AppendF("%p is located %zd bytes inside of", (void *)addr,
294 addr - g.beg);
295 }
296 str.AppendF(" global variable '%s' defined in '",
297 MaybeDemangleGlobalName(g.name));
298 PrintGlobalLocation(&str, g, /*print_module_name=*/false);
299 str.AppendF("' (%p) of size %zu\n", (void *)g.beg, g.size);
300 str.Append(d.Default());
301 PrintGlobalNameIfASCII(&str, g);
302 Printf("%s", str.data());
303 }
304
305 bool GetGlobalAddressInformation(uptr addr, uptr access_size,
306 GlobalAddressDescription *descr) {
307 descr->addr = addr;
308 int globals_num = GetGlobalsForAddress(addr, descr->globals, descr->reg_sites,
309 ARRAY_SIZE(descr->globals));
310 descr->size = globals_num;
311 descr->access_size = access_size;
312 return globals_num != 0;
313 }
314
315 bool DescribeAddressIfGlobal(uptr addr, uptr access_size,
316 const char *bug_type) {
317 GlobalAddressDescription descr;
318 if (!GetGlobalAddressInformation(addr, access_size, &descr)) return false;
319
320 descr.Print(bug_type);
321 return true;
322 }
323
324 void ShadowAddressDescription::Print() const {
325 Printf("Address %p is located in the %s area.\n", (void *)addr,
326 ShadowNames[kind]);
327 }
328
329 void GlobalAddressDescription::Print(const char *bug_type) const {
330 for (int i = 0; i < size; i++) {
331 DescribeAddressRelativeToGlobal(addr, access_size, globals[i]);
332 if (bug_type &&
333 0 == internal_strcmp(bug_type, "initialization-order-fiasco") &&
334 reg_sites[i]) {
335 Printf(" registered at:\n");
336 StackDepotGet(reg_sites[i]).Print();
337 }
338 }
339 }
340
341 bool GlobalAddressDescription::PointsInsideTheSameVariable(
342 const GlobalAddressDescription &other) const {
343 if (size == 0 || other.size == 0) return false;
344
345 for (uptr i = 0; i < size; i++) {
346 const __asan_global &a = globals[i];
347 for (uptr j = 0; j < other.size; j++) {
348 const __asan_global &b = other.globals[j];
349 if (a.beg == b.beg &&
350 a.beg <= addr &&
351 b.beg <= other.addr &&
352 (addr + access_size) < (a.beg + a.size) &&
353 (other.addr + other.access_size) < (b.beg + b.size))
354 return true;
355 }
356 }
357
358 return false;
359 }
360
361 void StackAddressDescription::Print() const {
362 Decorator d;
363 Printf("%s", d.Location());
364 Printf("Address %p is located in stack of thread %s", (void *)addr,
365 AsanThreadIdAndName(tid).c_str());
366
367 if (!frame_descr) {
368 Printf("%s\n", d.Default());
369 return;
370 }
371 Printf(" at offset %zu in frame%s\n", offset, d.Default());
372
373 // Now we print the frame where the alloca has happened.
374 // We print this frame as a stack trace with one element.
375 // The symbolizer may print more than one frame if inlining was involved.
376 // The frame numbers may be different than those in the stack trace printed
377 // previously. That's unfortunate, but I have no better solution,
378 // especially given that the alloca may be from entirely different place
379 // (e.g. use-after-scope, or different thread's stack).
380 Printf("%s", d.Default());
381 StackTrace alloca_stack(&frame_pc, 1);
382 alloca_stack.Print();
383
384 InternalMmapVector<StackVarDescr> vars;
385 vars.reserve(16);
386 if (!ParseFrameDescription(frame_descr, &vars)) {
387 Printf(
388 "AddressSanitizer can't parse the stack frame "
389 "descriptor: |%s|\n",
390 frame_descr);
391 // 'addr' is a stack address, so return true even if we can't parse frame
392 return;
393 }
394 uptr n_objects = vars.size();
395 // Report the number of stack objects.
396 Printf(" This frame has %zu object(s):\n", n_objects);
397
398 // Report all objects in this frame.
399 for (uptr i = 0; i < n_objects; i++) {
400 uptr prev_var_end = i ? vars[i - 1].beg + vars[i - 1].size : 0;
401 uptr next_var_beg = i + 1 < n_objects ? vars[i + 1].beg : ~(0UL);
402 PrintAccessAndVarIntersection(vars[i], offset, access_size, prev_var_end,
403 next_var_beg);
404 }
405 Printf(
406 "HINT: this may be a false positive if your program uses "
407 "some custom stack unwind mechanism, swapcontext or vfork\n");
408 if (SANITIZER_WINDOWS)
409 Printf(" (longjmp, SEH and C++ exceptions *are* supported)\n");
410 else
411 Printf(" (longjmp and C++ exceptions *are* supported)\n");
412
413 DescribeThread(GetThreadContextByTidLocked(tid));
414 }
415
416 void HeapAddressDescription::Print() const {
417 PrintHeapChunkAccess(addr, chunk_access);
418
419 asanThreadRegistry().CheckLocked();
420 AsanThreadContext *alloc_thread = GetThreadContextByTidLocked(alloc_tid);
421 StackTrace alloc_stack = GetStackTraceFromId(alloc_stack_id);
422
423 Decorator d;
424 AsanThreadContext *free_thread = nullptr;
425 if (free_tid != kInvalidTid) {
426 free_thread = GetThreadContextByTidLocked(free_tid);
427 Printf("%sfreed by thread %s here:%s\n", d.Allocation(),
428 AsanThreadIdAndName(free_thread).c_str(), d.Default());
429 StackTrace free_stack = GetStackTraceFromId(free_stack_id);
430 free_stack.Print();
431 Printf("%spreviously allocated by thread %s here:%s\n", d.Allocation(),
432 AsanThreadIdAndName(alloc_thread).c_str(), d.Default());
433 } else {
434 Printf("%sallocated by thread %s here:%s\n", d.Allocation(),
435 AsanThreadIdAndName(alloc_thread).c_str(), d.Default());
436 }
437 alloc_stack.Print();
438 DescribeThread(GetCurrentThread());
439 if (free_thread) DescribeThread(free_thread);
440 DescribeThread(alloc_thread);
441 }
442
443 AddressDescription::AddressDescription(uptr addr, uptr access_size,
444 bool shouldLockThreadRegistry) {
445 if (GetShadowAddressInformation(addr, &data.shadow)) {
446 data.kind = kAddressKindShadow;
447 return;
448 }
449 if (GetHeapAddressInformation(addr, access_size, &data.heap)) {
450 data.kind = kAddressKindHeap;
451 return;
452 }
453
454 bool isStackMemory = false;
455 if (shouldLockThreadRegistry) {
456 ThreadRegistryLock l(&asanThreadRegistry());
457 isStackMemory = GetStackAddressInformation(addr, access_size, &data.stack);
458 } else {
459 isStackMemory = GetStackAddressInformation(addr, access_size, &data.stack);
460 }
461 if (isStackMemory) {
462 data.kind = kAddressKindStack;
463 return;
464 }
465
466 if (GetGlobalAddressInformation(addr, access_size, &data.global)) {
467 data.kind = kAddressKindGlobal;
468 return;
469 }
470 data.kind = kAddressKindWild;
471 data.wild.addr = addr;
472 data.wild.access_size = access_size;
473 }
474
475 void WildAddressDescription::Print() const {
476 Printf("Address %p is a wild pointer inside of access range of size %p.\n",
477 (void *)addr, (void *)access_size);
478 }
479
480 void PrintAddressDescription(uptr addr, uptr access_size,
481 const char *bug_type) {
482 ShadowAddressDescription shadow_descr;
483 if (GetShadowAddressInformation(addr, &shadow_descr)) {
484 shadow_descr.Print();
485 return;
486 }
487
488 GlobalAddressDescription global_descr;
489 if (GetGlobalAddressInformation(addr, access_size, &global_descr)) {
490 global_descr.Print(bug_type);
491 return;
492 }
493
494 StackAddressDescription stack_descr;
495 if (GetStackAddressInformation(addr, access_size, &stack_descr)) {
496 stack_descr.Print();
497 return;
498 }
499
500 HeapAddressDescription heap_descr;
501 if (GetHeapAddressInformation(addr, access_size, &heap_descr)) {
502 heap_descr.Print();
503 return;
504 }
505
506 // We exhausted our possibilities. Bail out.
507 Printf(
508 "AddressSanitizer can not describe address in more detail "
509 "(wild memory access suspected).\n");
510 }
511 } // namespace __asan