Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 1 | //===-- asan_debugging.cc -------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // This file contains various functions that are generally useful to call when |
| 13 | // using a debugger (LLDB, GDB). |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "asan_allocator.h" |
| 17 | #include "asan_flags.h" |
| 18 | #include "asan_internal.h" |
| 19 | #include "asan_mapping.h" |
| 20 | #include "asan_report.h" |
| 21 | #include "asan_thread.h" |
| 22 | |
| 23 | namespace __asan { |
| 24 | |
| 25 | void GetInfoForStackVar(uptr addr, AddressDescription *descr, AsanThread *t) { |
| 26 | descr->name[0] = 0; |
| 27 | descr->region_address = 0; |
| 28 | descr->region_size = 0; |
| 29 | descr->region_kind = "stack"; |
| 30 | |
| 31 | AsanThread::StackFrameAccess access; |
| 32 | if (!t->GetStackFrameAccessByAddr(addr, &access)) |
| 33 | return; |
| 34 | InternalMmapVector<StackVarDescr> vars(16); |
| 35 | if (!ParseFrameDescription(access.frame_descr, &vars)) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | for (uptr i = 0; i < vars.size(); i++) { |
| 40 | if (access.offset <= vars[i].beg + vars[i].size) { |
| 41 | internal_strncat(descr->name, vars[i].name_pos, |
| 42 | Min(descr->name_size, vars[i].name_len)); |
| 43 | descr->region_address = addr - (access.offset - vars[i].beg); |
| 44 | descr->region_size = vars[i].size; |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void GetInfoForHeapAddress(uptr addr, AddressDescription *descr) { |
| 51 | AsanChunkView chunk = FindHeapChunkByAddress(addr); |
| 52 | |
| 53 | descr->name[0] = 0; |
| 54 | descr->region_address = 0; |
| 55 | descr->region_size = 0; |
| 56 | |
| 57 | if (!chunk.IsValid()) { |
| 58 | descr->region_kind = "heap-invalid"; |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | descr->region_address = chunk.Beg(); |
| 63 | descr->region_size = chunk.UsedSize(); |
| 64 | descr->region_kind = "heap"; |
| 65 | } |
| 66 | |
| 67 | void AsanLocateAddress(uptr addr, AddressDescription *descr) { |
| 68 | if (DescribeAddressIfShadow(addr, descr, /* print */ false)) { |
| 69 | return; |
| 70 | } |
| 71 | if (GetInfoForAddressIfGlobal(addr, descr)) { |
| 72 | return; |
| 73 | } |
| 74 | asanThreadRegistry().Lock(); |
| 75 | AsanThread *thread = FindThreadByStackAddress(addr); |
| 76 | asanThreadRegistry().Unlock(); |
| 77 | if (thread) { |
| 78 | GetInfoForStackVar(addr, descr, thread); |
| 79 | return; |
| 80 | } |
| 81 | GetInfoForHeapAddress(addr, descr); |
| 82 | } |
| 83 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 84 | static uptr AsanGetStack(uptr addr, uptr *trace, u32 size, u32 *thread_id, |
| 85 | bool alloc_stack) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 86 | AsanChunkView chunk = FindHeapChunkByAddress(addr); |
| 87 | if (!chunk.IsValid()) return 0; |
| 88 | |
| 89 | StackTrace stack(nullptr, 0); |
| 90 | if (alloc_stack) { |
| 91 | if (chunk.AllocTid() == kInvalidTid) return 0; |
| 92 | stack = chunk.GetAllocStack(); |
| 93 | if (thread_id) *thread_id = chunk.AllocTid(); |
| 94 | } else { |
| 95 | if (chunk.FreeTid() == kInvalidTid) return 0; |
| 96 | stack = chunk.GetFreeStack(); |
| 97 | if (thread_id) *thread_id = chunk.FreeTid(); |
| 98 | } |
| 99 | |
| 100 | if (trace && size) { |
| 101 | size = Min(size, Min(stack.size, kStackTraceMax)); |
| 102 | for (uptr i = 0; i < size; i++) |
| 103 | trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]); |
| 104 | |
| 105 | return size; |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 111 | } // namespace __asan |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 112 | |
| 113 | using namespace __asan; |
| 114 | |
| 115 | SANITIZER_INTERFACE_ATTRIBUTE |
| 116 | const char *__asan_locate_address(uptr addr, char *name, uptr name_size, |
| 117 | uptr *region_address, uptr *region_size) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 118 | AddressDescription descr = { name, name_size, 0, 0, nullptr }; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 119 | AsanLocateAddress(addr, &descr); |
| 120 | if (region_address) *region_address = descr.region_address; |
| 121 | if (region_size) *region_size = descr.region_size; |
| 122 | return descr.region_kind; |
| 123 | } |
| 124 | |
| 125 | SANITIZER_INTERFACE_ATTRIBUTE |
| 126 | uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) { |
| 127 | return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ true); |
| 128 | } |
| 129 | |
| 130 | SANITIZER_INTERFACE_ATTRIBUTE |
| 131 | uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) { |
| 132 | return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ false); |
| 133 | } |
| 134 | |
| 135 | SANITIZER_INTERFACE_ATTRIBUTE |
| 136 | void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset) { |
| 137 | if (shadow_scale) |
| 138 | *shadow_scale = SHADOW_SCALE; |
| 139 | if (shadow_offset) |
| 140 | *shadow_offset = SHADOW_OFFSET; |
| 141 | } |