xref: /llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace_printer.cpp (revision 74b0ac571b5facee3c8038d21ed71d7a29ee1098)
1 //===-- sanitizer_common.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 shared between sanitizers' run-time libraries.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "sanitizer_stacktrace_printer.h"
14 
15 #include "sanitizer_file.h"
16 #include "sanitizer_flags.h"
17 #include "sanitizer_fuchsia.h"
18 
19 namespace __sanitizer {
20 
21 const char *StripFunctionName(const char *function) {
22   if (!common_flags()->demangle)
23     return function;
24   if (!function)
25     return nullptr;
26   auto try_strip = [function](const char *prefix) -> const char * {
27     const uptr prefix_len = internal_strlen(prefix);
28     if (!internal_strncmp(function, prefix, prefix_len))
29       return function + prefix_len;
30     return nullptr;
31   };
32   if (SANITIZER_APPLE) {
33     if (const char *s = try_strip("wrap_"))
34       return s;
35   } else if (SANITIZER_WINDOWS) {
36     if (const char *s = try_strip("__asan_wrap_"))
37       return s;
38   } else {
39     if (const char *s = try_strip("___interceptor_"))
40       return s;
41     if (const char *s = try_strip("__interceptor_"))
42       return s;
43   }
44   return function;
45 }
46 
47 // sanitizer_symbolizer_markup.cpp implements these differently.
48 #if !SANITIZER_SYMBOLIZER_MARKUP
49 
50 static const char *DemangleFunctionName(const char *function) {
51   if (!common_flags()->demangle)
52     return function;
53   if (!function)
54     return nullptr;
55 
56   // NetBSD uses indirection for old threading functions for historical reasons
57   // The mangled names are internal implementation detail and should not be
58   // exposed even in backtraces.
59 #if SANITIZER_NETBSD
60   if (!internal_strcmp(function, "__libc_mutex_init"))
61     return "pthread_mutex_init";
62   if (!internal_strcmp(function, "__libc_mutex_lock"))
63     return "pthread_mutex_lock";
64   if (!internal_strcmp(function, "__libc_mutex_trylock"))
65     return "pthread_mutex_trylock";
66   if (!internal_strcmp(function, "__libc_mutex_unlock"))
67     return "pthread_mutex_unlock";
68   if (!internal_strcmp(function, "__libc_mutex_destroy"))
69     return "pthread_mutex_destroy";
70   if (!internal_strcmp(function, "__libc_mutexattr_init"))
71     return "pthread_mutexattr_init";
72   if (!internal_strcmp(function, "__libc_mutexattr_settype"))
73     return "pthread_mutexattr_settype";
74   if (!internal_strcmp(function, "__libc_mutexattr_destroy"))
75     return "pthread_mutexattr_destroy";
76   if (!internal_strcmp(function, "__libc_cond_init"))
77     return "pthread_cond_init";
78   if (!internal_strcmp(function, "__libc_cond_signal"))
79     return "pthread_cond_signal";
80   if (!internal_strcmp(function, "__libc_cond_broadcast"))
81     return "pthread_cond_broadcast";
82   if (!internal_strcmp(function, "__libc_cond_wait"))
83     return "pthread_cond_wait";
84   if (!internal_strcmp(function, "__libc_cond_timedwait"))
85     return "pthread_cond_timedwait";
86   if (!internal_strcmp(function, "__libc_cond_destroy"))
87     return "pthread_cond_destroy";
88   if (!internal_strcmp(function, "__libc_rwlock_init"))
89     return "pthread_rwlock_init";
90   if (!internal_strcmp(function, "__libc_rwlock_rdlock"))
91     return "pthread_rwlock_rdlock";
92   if (!internal_strcmp(function, "__libc_rwlock_wrlock"))
93     return "pthread_rwlock_wrlock";
94   if (!internal_strcmp(function, "__libc_rwlock_tryrdlock"))
95     return "pthread_rwlock_tryrdlock";
96   if (!internal_strcmp(function, "__libc_rwlock_trywrlock"))
97     return "pthread_rwlock_trywrlock";
98   if (!internal_strcmp(function, "__libc_rwlock_unlock"))
99     return "pthread_rwlock_unlock";
100   if (!internal_strcmp(function, "__libc_rwlock_destroy"))
101     return "pthread_rwlock_destroy";
102   if (!internal_strcmp(function, "__libc_thr_keycreate"))
103     return "pthread_key_create";
104   if (!internal_strcmp(function, "__libc_thr_setspecific"))
105     return "pthread_setspecific";
106   if (!internal_strcmp(function, "__libc_thr_getspecific"))
107     return "pthread_getspecific";
108   if (!internal_strcmp(function, "__libc_thr_keydelete"))
109     return "pthread_key_delete";
110   if (!internal_strcmp(function, "__libc_thr_once"))
111     return "pthread_once";
112   if (!internal_strcmp(function, "__libc_thr_self"))
113     return "pthread_self";
114   if (!internal_strcmp(function, "__libc_thr_exit"))
115     return "pthread_exit";
116   if (!internal_strcmp(function, "__libc_thr_setcancelstate"))
117     return "pthread_setcancelstate";
118   if (!internal_strcmp(function, "__libc_thr_equal"))
119     return "pthread_equal";
120   if (!internal_strcmp(function, "__libc_thr_curcpu"))
121     return "pthread_curcpu_np";
122   if (!internal_strcmp(function, "__libc_thr_sigsetmask"))
123     return "pthread_sigmask";
124 #endif
125 
126   return function;
127 }
128 
129 static void MaybeBuildIdToBuffer(const AddressInfo &info, bool PrefixSpace,
130                                  InternalScopedString *buffer) {
131   if (info.uuid_size) {
132     if (PrefixSpace)
133       buffer->append(" ");
134     buffer->append("(BuildId: ");
135     for (uptr i = 0; i < info.uuid_size; ++i) {
136       buffer->append("%02x", info.uuid[i]);
137     }
138     buffer->append(")");
139   }
140 }
141 
142 static const char kDefaultFormat[] = "    #%n %p %F %L";
143 
144 void RenderFrame(InternalScopedString *buffer, const char *format, int frame_no,
145                  uptr address, const AddressInfo *info, bool vs_style,
146                  const char *strip_path_prefix) {
147   // info will be null in the case where symbolization is not needed for the
148   // given format. This ensures that the code below will get a hard failure
149   // rather than print incorrect information in case RenderNeedsSymbolization
150   // ever ends up out of sync with this function. If non-null, the addresses
151   // should match.
152   CHECK(!info || address == info->address);
153   if (0 == internal_strcmp(format, "DEFAULT"))
154     format = kDefaultFormat;
155   for (const char *p = format; *p != '\0'; p++) {
156     if (*p != '%') {
157       buffer->append("%c", *p);
158       continue;
159     }
160     p++;
161     switch (*p) {
162     case '%':
163       buffer->append("%%");
164       break;
165     // Frame number and all fields of AddressInfo structure.
166     case 'n':
167       buffer->append("%u", frame_no);
168       break;
169     case 'p':
170       buffer->append("0x%zx", address);
171       break;
172     case 'm':
173       buffer->append("%s", StripPathPrefix(info->module, strip_path_prefix));
174       break;
175     case 'o':
176       buffer->append("0x%zx", info->module_offset);
177       break;
178     case 'b':
179       MaybeBuildIdToBuffer(*info, /*PrefixSpace=*/false, buffer);
180       break;
181     case 'f':
182       buffer->append("%s",
183                      DemangleFunctionName(StripFunctionName(info->function)));
184       break;
185     case 'q':
186       buffer->append("0x%zx", info->function_offset != AddressInfo::kUnknown
187                                   ? info->function_offset
188                                   : 0x0);
189       break;
190     case 's':
191       buffer->append("%s", StripPathPrefix(info->file, strip_path_prefix));
192       break;
193     case 'l':
194       buffer->append("%d", info->line);
195       break;
196     case 'c':
197       buffer->append("%d", info->column);
198       break;
199     // Smarter special cases.
200     case 'F':
201       // Function name and offset, if file is unknown.
202       if (info->function) {
203         buffer->append("in %s",
204                        DemangleFunctionName(StripFunctionName(info->function)));
205         if (!info->file && info->function_offset != AddressInfo::kUnknown)
206           buffer->append("+0x%zx", info->function_offset);
207       }
208       break;
209     case 'S':
210       // File/line information.
211       RenderSourceLocation(buffer, info->file, info->line, info->column,
212                            vs_style, strip_path_prefix);
213       break;
214     case 'L':
215       // Source location, or module location.
216       if (info->file) {
217         RenderSourceLocation(buffer, info->file, info->line, info->column,
218                              vs_style, strip_path_prefix);
219       } else if (info->module) {
220         RenderModuleLocation(buffer, info->module, info->module_offset,
221                              info->module_arch, strip_path_prefix);
222 
223         MaybeBuildIdToBuffer(*info, /*PrefixSpace=*/true, buffer);
224       } else {
225         buffer->append("(<unknown module>)");
226       }
227       break;
228     case 'M':
229       // Module basename and offset, or PC.
230       if (address & kExternalPCBit) {
231         // There PCs are not meaningful.
232       } else if (info->module) {
233         // Always strip the module name for %M.
234         RenderModuleLocation(buffer, StripModuleName(info->module),
235                              info->module_offset, info->module_arch, "");
236         MaybeBuildIdToBuffer(*info, /*PrefixSpace=*/true, buffer);
237       } else {
238         buffer->append("(%p)", (void *)address);
239       }
240       break;
241     default:
242       Report("Unsupported specifier in stack frame format: %c (%p)!\n", *p,
243              (void *)p);
244       Die();
245     }
246   }
247 }
248 
249 bool RenderNeedsSymbolization(const char *format) {
250   if (0 == internal_strcmp(format, "DEFAULT"))
251     format = kDefaultFormat;
252   for (const char *p = format; *p != '\0'; p++) {
253     if (*p != '%')
254       continue;
255     p++;
256     switch (*p) {
257       case '%':
258         break;
259       case 'n':
260         // frame_no
261         break;
262       case 'p':
263         // address
264         break;
265       default:
266         return true;
267     }
268   }
269   return false;
270 }
271 
272 void RenderData(InternalScopedString *buffer, const char *format,
273                 const DataInfo *DI, const char *strip_path_prefix) {
274   for (const char *p = format; *p != '\0'; p++) {
275     if (*p != '%') {
276       buffer->append("%c", *p);
277       continue;
278     }
279     p++;
280     switch (*p) {
281       case '%':
282         buffer->append("%%");
283         break;
284       case 's':
285         buffer->append("%s", StripPathPrefix(DI->file, strip_path_prefix));
286         break;
287       case 'l':
288         buffer->append("%zu", DI->line);
289         break;
290       case 'g':
291         buffer->append("%s", DI->name);
292         break;
293       default:
294         Report("Unsupported specifier in stack frame format: %c (%p)!\n", *p,
295                (void *)p);
296         Die();
297     }
298   }
299 }
300 
301 #endif  // !SANITIZER_SYMBOLIZER_MARKUP
302 
303 void RenderSourceLocation(InternalScopedString *buffer, const char *file,
304                           int line, int column, bool vs_style,
305                           const char *strip_path_prefix) {
306   if (vs_style && line > 0) {
307     buffer->append("%s(%d", StripPathPrefix(file, strip_path_prefix), line);
308     if (column > 0)
309       buffer->append(",%d", column);
310     buffer->append(")");
311     return;
312   }
313 
314   buffer->append("%s", StripPathPrefix(file, strip_path_prefix));
315   if (line > 0) {
316     buffer->append(":%d", line);
317     if (column > 0)
318       buffer->append(":%d", column);
319   }
320 }
321 
322 void RenderModuleLocation(InternalScopedString *buffer, const char *module,
323                           uptr offset, ModuleArch arch,
324                           const char *strip_path_prefix) {
325   buffer->append("(%s", StripPathPrefix(module, strip_path_prefix));
326   if (arch != kModuleArchUnknown) {
327     buffer->append(":%s", ModuleArchToString(arch));
328   }
329   buffer->append("+0x%zx)", offset);
330 }
331 
332 } // namespace __sanitizer
333