aboutsummaryrefslogtreecommitdiff
path: root/lib/asan/asan_debugging.cc
blob: 7c3a8a73bd4ea14567c375ab740a37ce0f775cf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//===-- 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"
#include "asan_flags.h"
#include "asan_internal.h"
#include "asan_mapping.h"
#include "asan_report.h"
#include "asan_thread.h"

namespace __asan {

void GetInfoForStackVar(uptr addr, AddressDescription *descr, AsanThread *t) {
  descr->name[0] = 0;
  descr->region_address = 0;
  descr->region_size = 0;
  descr->region_kind = "stack";

  AsanThread::StackFrameAccess access;
  if (!t->GetStackFrameAccessByAddr(addr, &access))
    return;
  InternalMmapVector<StackVarDescr> vars(16);
  if (!ParseFrameDescription(access.frame_descr, &vars)) {
    return;
  }

  for (uptr i = 0; i < vars.size(); i++) {
    if (access.offset <= vars[i].beg + vars[i].size) {
      internal_strncat(descr->name, vars[i].name_pos,
                       Min(descr->name_size, vars[i].name_len));
      descr->region_address = addr - (access.offset - vars[i].beg);
      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) {
  if (DescribeAddressIfShadow(addr, descr, /* print */ false)) {
    return;
  }
  if (GetInfoForAddressIfGlobal(addr, descr)) {
    return;
  }
  asanThreadRegistry().Lock();
  AsanThread *thread = FindThreadByStackAddress(addr);
  asanThreadRegistry().Unlock();
  if (thread) {
    GetInfoForStackVar(addr, descr, thread);
    return;
  }
  GetInfoForHeapAddress(addr, descr);
}

static uptr AsanGetStack(uptr addr, uptr *trace, u32 size, u32 *thread_id,
                         bool alloc_stack) {
  AsanChunkView chunk = FindHeapChunkByAddress(addr);
  if (!chunk.IsValid()) return 0;

  StackTrace stack(nullptr, 0);
  if (alloc_stack) {
    if (chunk.AllocTid() == kInvalidTid) return 0;
    stack = chunk.GetAllocStack();
    if (thread_id) *thread_id = chunk.AllocTid();
  } else {
    if (chunk.FreeTid() == kInvalidTid) return 0;
    stack = chunk.GetFreeStack();
    if (thread_id) *thread_id = chunk.FreeTid();
  }

  if (trace && size) {
    size = Min(size, Min(stack.size, kStackTraceMax));
    for (uptr i = 0; i < size; i++)
      trace[i] = StackTrace::GetPreviousInstructionPc(stack.trace[i]);

    return size;
  }

  return 0;
}

} // namespace __asan

using namespace __asan;

SANITIZER_INTERFACE_ATTRIBUTE
const char *__asan_locate_address(uptr addr, char *name, uptr name_size,
                                  uptr *region_address, uptr *region_size) {
  AddressDescription descr = { name, name_size, 0, 0, nullptr };
  AsanLocateAddress(addr, &descr);
  if (region_address) *region_address = descr.region_address;
  if (region_size) *region_size = descr.region_size;
  return descr.region_kind;
}

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;
}