2014-07-16 01:33:23 +08:00
|
|
|
//===-- asan_debugging.cc -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// This file contains various functions that are generally useful to call when
|
|
|
|
// using a debugger (LLDB, GDB).
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "asan_allocator.h"
|
2016-08-16 03:30:21 +08:00
|
|
|
#include "asan_descriptions.h"
|
2014-07-16 01:33:23 +08:00
|
|
|
#include "asan_flags.h"
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_mapping.h"
|
2014-09-27 03:15:32 +08:00
|
|
|
#include "asan_report.h"
|
2014-07-16 01:33:23 +08:00
|
|
|
#include "asan_thread.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2014-09-27 03:15:32 +08:00
|
|
|
void GetInfoForStackVar(uptr addr, AddressDescription *descr, AsanThread *t) {
|
|
|
|
descr->name[0] = 0;
|
|
|
|
descr->region_address = 0;
|
|
|
|
descr->region_size = 0;
|
|
|
|
descr->region_kind = "stack";
|
|
|
|
|
2014-10-02 05:13:00 +08:00
|
|
|
AsanThread::StackFrameAccess access;
|
|
|
|
if (!t->GetStackFrameAccessByAddr(addr, &access))
|
|
|
|
return;
|
2014-09-27 03:15:32 +08:00
|
|
|
InternalMmapVector<StackVarDescr> vars(16);
|
2014-10-02 05:13:00 +08:00
|
|
|
if (!ParseFrameDescription(access.frame_descr, &vars)) {
|
2014-09-27 03:15:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uptr i = 0; i < vars.size(); i++) {
|
2014-10-02 05:13:00 +08:00
|
|
|
if (access.offset <= vars[i].beg + vars[i].size) {
|
2014-09-27 03:15:32 +08:00
|
|
|
internal_strncat(descr->name, vars[i].name_pos,
|
|
|
|
Min(descr->name_size, vars[i].name_len));
|
2014-10-02 05:13:00 +08:00
|
|
|
descr->region_address = addr - (access.offset - vars[i].beg);
|
2014-09-27 03:15:32 +08:00
|
|
|
descr->region_size = vars[i].size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetInfoForHeapAddress(uptr addr, AddressDescription *descr) {
|
|
|
|
AsanChunkView chunk = FindHeapChunkByAddress(addr);
|
|
|
|
|
|
|
|
descr->name[0] = 0;
|
|
|
|
descr->region_address = 0;
|
|
|
|
descr->region_size = 0;
|
|
|
|
|
|
|
|
if (!chunk.IsValid()) {
|
|
|
|
descr->region_kind = "heap-invalid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
descr->region_address = chunk.Beg();
|
|
|
|
descr->region_size = chunk.UsedSize();
|
|
|
|
descr->region_kind = "heap";
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanLocateAddress(uptr addr, AddressDescription *descr) {
|
2016-08-16 03:30:21 +08:00
|
|
|
ShadowAddressDescription shadow_descr;
|
|
|
|
if (GetShadowAddressInformation(addr, &shadow_descr)) {
|
|
|
|
descr->region_kind = ShadowNames[shadow_descr.kind];
|
2014-09-27 03:15:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-08-18 03:52:12 +08:00
|
|
|
GlobalAddressDescription global_descr;
|
|
|
|
if (GetGlobalAddressInformation(addr, &global_descr)) {
|
|
|
|
descr->region_kind = "global";
|
|
|
|
auto &g = global_descr.globals[0];
|
|
|
|
internal_strlcpy(descr->name, g.name, descr->name_size);
|
|
|
|
descr->region_address = g.beg;
|
|
|
|
descr->region_size = g.size;
|
2014-09-27 03:15:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
asanThreadRegistry().Lock();
|
|
|
|
AsanThread *thread = FindThreadByStackAddress(addr);
|
|
|
|
asanThreadRegistry().Unlock();
|
|
|
|
if (thread) {
|
|
|
|
GetInfoForStackVar(addr, descr, thread);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GetInfoForHeapAddress(addr, descr);
|
|
|
|
}
|
|
|
|
|
2015-01-22 21:33:16 +08:00
|
|
|
static uptr AsanGetStack(uptr addr, uptr *trace, u32 size, u32 *thread_id,
|
|
|
|
bool alloc_stack) {
|
2014-07-16 01:33:23 +08:00
|
|
|
AsanChunkView chunk = FindHeapChunkByAddress(addr);
|
|
|
|
if (!chunk.IsValid()) return 0;
|
|
|
|
|
2014-10-26 11:35:14 +08:00
|
|
|
StackTrace stack(nullptr, 0);
|
2014-07-16 01:33:23 +08:00
|
|
|
if (alloc_stack) {
|
|
|
|
if (chunk.AllocTid() == kInvalidTid) return 0;
|
2014-10-26 11:35:14 +08:00
|
|
|
stack = chunk.GetAllocStack();
|
2014-07-16 01:33:23 +08:00
|
|
|
if (thread_id) *thread_id = chunk.AllocTid();
|
|
|
|
} else {
|
|
|
|
if (chunk.FreeTid() == kInvalidTid) return 0;
|
2014-10-26 11:35:14 +08:00
|
|
|
stack = chunk.GetFreeStack();
|
2014-07-16 01:33:23 +08:00
|
|
|
if (thread_id) *thread_id = chunk.FreeTid();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trace && size) {
|
2014-10-26 11:35:14 +08:00
|
|
|
size = Min(size, Min(stack.size, kStackTraceMax));
|
2014-07-16 01:33:23 +08:00
|
|
|
for (uptr i = 0; i < size; i++)
|
|
|
|
trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __asan
|
2014-07-16 01:33:23 +08:00
|
|
|
|
|
|
|
using namespace __asan;
|
|
|
|
|
2014-09-27 03:15:32 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
const char *__asan_locate_address(uptr addr, char *name, uptr name_size,
|
|
|
|
uptr *region_address, uptr *region_size) {
|
2015-10-01 08:22:21 +08:00
|
|
|
AddressDescription descr = { name, name_size, 0, 0, nullptr };
|
2014-09-27 03:15:32 +08:00
|
|
|
AsanLocateAddress(addr, &descr);
|
|
|
|
if (region_address) *region_address = descr.region_address;
|
|
|
|
if (region_size) *region_size = descr.region_size;
|
|
|
|
return descr.region_kind;
|
|
|
|
}
|
|
|
|
|
2014-07-16 01:33:23 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
|
|
|
|
return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {
|
|
|
|
return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset) {
|
|
|
|
if (shadow_scale)
|
|
|
|
*shadow_scale = SHADOW_SCALE;
|
|
|
|
if (shadow_offset)
|
|
|
|
*shadow_offset = SHADOW_OFFSET;
|
|
|
|
}
|