File: internal_symbolizer.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (187 lines) | stat: -rw-r--r-- 5,881 bytes parent folder | download | duplicates (13)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// RUN: %clangxx %s -o %t -g && %run %t 2>&1 | FileCheck %s

// REQUIRES: internal_symbolizer

#include <assert.h>
#include <dlfcn.h>
#include <link.h>
#include <sanitizer/hwasan_interface.h>
#include <sanitizer/msan_interface.h>
#include <string.h>

#include <string>
#include <vector>

extern "C" {
bool __sanitizer_symbolize_code(const char *ModuleName, uint64_t ModuleOffset,
                                char *Buffer, int MaxLength,
                                bool SymbolizeInlineFrames);
bool __sanitizer_symbolize_data(const char *ModuleName, uint64_t ModuleOffset,
                                char *Buffer, int MaxLength);
bool __sanitizer_symbolize_frame(const char *ModuleName, uint64_t ModuleOffset,
                                 char *Buffer, int MaxLength);
void __sanitizer_print_stack_trace();
bool __sanitizer_symbolize_demangle(const char *Name, char *Buffer,
                                    int MaxLength);
}

struct ScopedInSymbolizer {
#if defined(__has_feature)
#  if __has_feature(memory_sanitizer)
  ScopedInSymbolizer() { __msan_scoped_disable_interceptor_checks(); }
  ~ScopedInSymbolizer() { __msan_scoped_enable_interceptor_checks(); }
#  endif
#endif
};

struct FrameInfo {
  int line;
  std::string file;
  std::string function;
  void *address;
};

__attribute__((noinline)) void *GetPC() { return __builtin_return_address(0); }

__attribute__((always_inline)) FrameInfo InlineFunction() {
  void *address = GetPC();
  return {0, "", "",
          reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(address) - 1)};
}

__attribute__((noinline)) FrameInfo NoInlineFunction() {
  void *address = GetPC();
  return {0, "", "",
          reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(address) - 1)};
}

template <int N> struct A {
  template <class T> FrameInfo RecursiveTemplateFunction(const T &t);
};

template <int N>
template <class T>
__attribute__((noinline)) FrameInfo A<N>::RecursiveTemplateFunction(const T &) {
  std::vector<T> t;
  return A<N - 1>().RecursiveTemplateFunction(t);
}

template <>
template <class T>
__attribute__((noinline)) FrameInfo A<0>::RecursiveTemplateFunction(const T &) {
  return NoInlineFunction();
}

__attribute__((no_sanitize_memory)) std::pair<const char *, uint64_t>
GetModuleAndOffset(const void *address) {
  Dl_info di;
  link_map *lm = nullptr;
#if __has_feature(hwaddress_sanitizer)
  address = __hwasan_tag_pointer(address, 0);
#endif
  assert(
      dladdr1(address, &di, reinterpret_cast<void **>(&lm), RTLD_DL_LINKMAP));
  return {di.dli_fname, reinterpret_cast<uint64_t>(address) - lm->l_addr};
}

std::string Symbolize(FrameInfo frame) {
  auto modul_offset = GetModuleAndOffset(frame.address);
  char buffer[1024] = {};
  ScopedInSymbolizer in_symbolizer;
  assert(__sanitizer_symbolize_code(modul_offset.first, modul_offset.second,
                                    buffer, std::size(buffer), true));
  return buffer;
}

std::string GetRegex(const FrameInfo &frame) {
  return frame.function + "[^\\n]*\\n[^\\n]*" + frame.file + ":" +
         std::to_string(frame.line);
}

void TestInline() {
  auto frame = InlineFunction();
  fprintf(stderr, "%s: %s\n", __FUNCTION__, Symbolize(frame).c_str());
  // CHECK-LABEL: TestInline: InlineFunction()
  // CHECK-NEXT: internal_symbolizer.cpp:[[# @LINE - 58]]
  // CHECK-NEXT: TestInline()
  // CHECK-NEXT: internal_symbolizer.cpp:[[# @LINE - 5]]
}

void TestNoInline() {
  auto frame = NoInlineFunction();
  fprintf(stderr, "%s: %s\n", __FUNCTION__, Symbolize(frame).c_str());
  // CHECK-LABEL: TestNoInline: NoInlineFunction()
  // CHECK-NEXT: internal_symbolizer.cpp:[[# @LINE - 61]]
}

void TestLongFunctionNames() {
  auto frame = A<10>().RecursiveTemplateFunction(0);
  fprintf(stderr, "%s: %s\n", __FUNCTION__, Symbolize(frame).c_str());
  // CHECK-LABEL: TestLongFunctionNames: NoInlineFunction()
  // CHECK-NEXT: internal_symbolizer.cpp:[[# @LINE - 68]]
}

std::string SymbolizeStaticVar() {
  static int var = 1;
  auto modul_offset = GetModuleAndOffset(&var);
  char buffer[1024] = {};
  ScopedInSymbolizer in_symbolizer;
  assert(__sanitizer_symbolize_data(modul_offset.first, modul_offset.second,
                                    buffer, std::size(buffer)));
  return buffer;
}

void TestData() {
  fprintf(stderr, "%s: %s\n", __FUNCTION__, SymbolizeStaticVar().c_str());
  // CHECK-LABEL: TestData: SymbolizeStaticVar[abi:cxx11]()::var
  // CHECK-NEXT: {{[0-9]+ +[0-9]+}}
  // CHECK-NEXT: internal_symbolizer.cpp:[[# @LINE - 13]]
}

__attribute__((noinline)) std::string SymbolizeLocalVars(const void *pc) {
  auto modul_offset = GetModuleAndOffset(pc);
  char buffer[1024] = {};
  ScopedInSymbolizer in_symbolizer;
  __sanitizer_symbolize_frame(modul_offset.first, modul_offset.second, buffer,
                              std::size(buffer));
  return buffer;
}

__attribute__((
    noinline,
    no_sanitize_address /* Asan merges allocas destroying variable DI */)) void
TestFrame() {
  volatile int var = 1;
  void *address = GetPC();
  fprintf(stderr, "%s: %s\n", __FUNCTION__,
          SymbolizeLocalVars(address).c_str());
  // CHECK-LABEL: TestFrame: TestFrame
  // CHECK-NEXT: var
  // CHECK-NEXT: internal_symbolizer.cpp:[[# @LINE - 6]]
  // CHECK-NEXT: {{-?[0-9]+ +[0-9]+}}
  // CHECK-NEXT: TestFrame
  // CHECK-NEXT: address
  // CHECK-NEXT: internal_symbolizer.cpp:[[# @LINE - 9]]
  // CHECK-NEXT: {{-?[0-9]+ +[0-9]+}}
}

void TestDemangle() {
  char out[128];
  assert(!__sanitizer_symbolize_demangle("1A", out, sizeof(out)));

  const char name[] = "_Z3fooi";
  for (int i = 1; i < sizeof(out); ++i) {
    memset(out, 1, sizeof(out));
    assert(__sanitizer_symbolize_demangle(name, out, i) == (i > 8));
    assert(i < 9 || 0 == strncmp(out, "foo(int)", i - 1));
  }
}

int main() {
  TestInline();
  TestNoInline();
  TestLongFunctionNames();
  TestData();
  TestFrame();
  TestDemangle();
}