llvm-project
147 строк · 4.8 Кб
1//===-- asan_debugging.cpp ------------------------------------------------===//
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// This file contains various functions that are generally useful to call when
12// using a debugger (LLDB, GDB).
13//===----------------------------------------------------------------------===//
14
15#include "asan_allocator.h"16#include "asan_descriptions.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#include "sanitizer_common/sanitizer_stackdepot.h"23
24namespace {25using namespace __asan;26
27static void FindInfoForStackVar(uptr addr, const char *frame_descr, uptr offset,28char *name, uptr name_size,29uptr *region_address, uptr *region_size) {30InternalMmapVector<StackVarDescr> vars;31vars.reserve(16);32if (!ParseFrameDescription(frame_descr, &vars)) {33return;34}35
36for (uptr i = 0; i < vars.size(); i++) {37if (offset <= vars[i].beg + vars[i].size) {38// We use name_len + 1 because strlcpy will guarantee a \0 at the end, so39// if we're limiting the copy due to name_len, we add 1 to ensure we copy40// the whole name and then terminate with '\0'.41internal_strlcpy(name, vars[i].name_pos,42Min(name_size, vars[i].name_len + 1));43*region_address = addr - (offset - vars[i].beg);44*region_size = vars[i].size;45return;46}47}48}
49
50uptr AsanGetStack(uptr addr, uptr *trace, u32 size, u32 *thread_id,51bool alloc_stack) {52AsanChunkView chunk = FindHeapChunkByAddress(addr);53if (!chunk.IsValid()) return 0;54
55StackTrace stack(nullptr, 0);56if (alloc_stack) {57if (chunk.AllocTid() == kInvalidTid) return 0;58stack = StackDepotGet(chunk.GetAllocStackId());59if (thread_id) *thread_id = chunk.AllocTid();60} else {61if (chunk.FreeTid() == kInvalidTid) return 0;62stack = StackDepotGet(chunk.GetFreeStackId());63if (thread_id) *thread_id = chunk.FreeTid();64}65
66if (trace && size) {67size = Min(size, Min(stack.size, kStackTraceMax));68for (uptr i = 0; i < size; i++)69trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]);70
71return size;72}73
74return 0;75}
76
77} // namespace78
79SANITIZER_INTERFACE_ATTRIBUTE
80const char *__asan_locate_address(uptr addr, char *name, uptr name_size,81uptr *region_address_ptr,82uptr *region_size_ptr) {83AddressDescription descr(addr);84uptr region_address = 0;85uptr region_size = 0;86const char *region_kind = nullptr;87if (name && name_size > 0) name[0] = 0;88
89if (auto shadow = descr.AsShadow()) {90// region_{address,size} are already 091switch (shadow->kind) {92case kShadowKindLow:93region_kind = "low shadow";94break;95case kShadowKindGap:96region_kind = "shadow gap";97break;98case kShadowKindHigh:99region_kind = "high shadow";100break;101}102} else if (auto heap = descr.AsHeap()) {103region_kind = "heap";104region_address = heap->chunk_access.chunk_begin;105region_size = heap->chunk_access.chunk_size;106} else if (auto stack = descr.AsStack()) {107region_kind = "stack";108if (!stack->frame_descr) {109// region_{address,size} are already 0110} else {111FindInfoForStackVar(addr, stack->frame_descr, stack->offset, name,112name_size, ®ion_address, ®ion_size);113}114} else if (auto global = descr.AsGlobal()) {115region_kind = "global";116auto &g = global->globals[0];117internal_strlcpy(name, g.name, name_size);118region_address = g.beg;119region_size = g.size;120} else {121// region_{address,size} are already 0122region_kind = "heap-invalid";123}124
125CHECK(region_kind);126if (region_address_ptr) *region_address_ptr = region_address;127if (region_size_ptr) *region_size_ptr = region_size;128return region_kind;129}
130
131SANITIZER_INTERFACE_ATTRIBUTE
132uptr __asan_get_alloc_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {133return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ true);134}
135
136SANITIZER_INTERFACE_ATTRIBUTE
137uptr __asan_get_free_stack(uptr addr, uptr *trace, uptr size, u32 *thread_id) {138return AsanGetStack(addr, trace, size, thread_id, /* alloc_stack */ false);139}
140
141SANITIZER_INTERFACE_ATTRIBUTE
142void __asan_get_shadow_mapping(uptr *shadow_scale, uptr *shadow_offset) {143if (shadow_scale)144*shadow_scale = ASAN_SHADOW_SCALE;145if (shadow_offset)146*shadow_offset = ASAN_SHADOW_OFFSET;147}
148