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