| 1 |
//===-- sanitizer_symbolizer_report.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 AddressSanitizer and other sanitizer run-time |
| 10 |
/// libraries and implements symbolized reports related functions. |
| 11 |
/// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "sanitizer_common.h" |
| 15 |
#include "sanitizer_file.h" |
| 16 |
#include "sanitizer_flags.h" |
| 17 |
#include "sanitizer_procmaps.h" |
| 18 |
#include "sanitizer_report_decorator.h" |
| 19 |
#include "sanitizer_stacktrace.h" |
| 20 |
#include "sanitizer_stacktrace_printer.h" |
| 21 |
#include "sanitizer_symbolizer.h" |
| 22 |
|
| 23 |
#if SANITIZER_POSIX |
| 24 |
# include "sanitizer_posix.h" |
| 25 |
# include <sys/mman.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
namespace __sanitizer { |
| 29 |
|
| 30 |
#if !SANITIZER_GO |
| 31 |
|
| 32 |
static bool FrameIsInternal(const SymbolizedStack *frame) { |
| 33 |
if (!frame) |
| 34 |
return true; |
| 35 |
const char *file = frame->info.file; |
| 36 |
const char *module = frame->info.module; |
| 37 |
// On Gentoo, the path is g++-*, so there's *not* a missing /. |
| 38 |
if (file && (internal_strstr(file, "/compiler-rt/lib/") || |
| 39 |
internal_strstr(file, "/include/c++/") || |
| 40 |
internal_strstr(file, "/include/g++"))) |
| 41 |
return true; |
| 42 |
if (file && internal_strstr(file, "\\compiler-rt\\lib\\")) |
| 43 |
return true; |
| 44 |
if (file && internal_strstr(file, "\\libsanitizer\\")) |
| 45 |
return true; |
| 46 |
if (module && (internal_strstr(module, "libclang_rt."))) |
| 47 |
return true; |
| 48 |
if (module && (internal_strstr(module, "clang_rt."))) |
| 49 |
return true; |
| 50 |
if (module && (internal_strstr(module, "libtsan.") |
| 51 |
|| internal_strstr(module, "libhwasan.") |
| 52 |
|| internal_strstr(module, "liblsan.") |
| 53 |
|| internal_strstr(module, "libasan.") |
| 54 |
|| internal_strstr(module, "libubsan."))) |
| 55 |
return true; |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
const SymbolizedStack *SkipInternalFrames(const SymbolizedStack *frames) { |
| 60 |
for (const SymbolizedStack *f = frames; f; f = f->next) |
| 61 |
if (!FrameIsInternal(f)) |
| 62 |
return f; |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
void ReportErrorSummary(const char *error_type, const AddressInfo &info, |
| 67 |
const char *alt_tool_name) { |
| 68 |
if (!common_flags()->print_summary) return; |
| 69 |
InternalScopedString buff; |
| 70 |
buff.AppendF("%s ", error_type); |
| 71 |
StackTracePrinter::GetOrInit()->RenderFrame( |
| 72 |
&buff, "%L %F", 0, info.address, &info, |
| 73 |
common_flags()->symbolize_vs_style, common_flags()->strip_path_prefix); |
| 74 |
ReportErrorSummary(buff.data(), alt_tool_name); |
| 75 |
} |
| 76 |
#endif |
| 77 |
|
| 78 |
#if !SANITIZER_FUCHSIA |
| 79 |
|
| 80 |
bool ReportFile::SupportsColors() { |
| 81 |
SpinMutexLock l(mu); |
| 82 |
ReopenIfNecessary(); |
| 83 |
return SupportsColoredOutput(fd); |
| 84 |
} |
| 85 |
|
| 86 |
static inline bool ReportSupportsColors() { |
| 87 |
return report_file.SupportsColors(); |
| 88 |
} |
| 89 |
|
| 90 |
#else // SANITIZER_FUCHSIA |
| 91 |
|
| 92 |
// Fuchsia's logs always go through post-processing that handles colorization. |
| 93 |
static inline bool ReportSupportsColors() { return true; } |
| 94 |
|
| 95 |
#endif // !SANITIZER_FUCHSIA |
| 96 |
|
| 97 |
bool ColorizeReports() { |
| 98 |
// FIXME: Add proper Windows support to AnsiColorDecorator and re-enable color |
| 99 |
// printing on Windows. |
| 100 |
if (SANITIZER_WINDOWS) |
| 101 |
return false; |
| 102 |
|
| 103 |
const char *flag = common_flags()->color; |
| 104 |
return internal_strcmp(flag, "always") == 0 || |
| 105 |
(internal_strcmp(flag, "auto") == 0 && ReportSupportsColors()); |
| 106 |
} |
| 107 |
|
| 108 |
void ReportErrorSummary(const char *error_type, const StackTrace *stack, |
| 109 |
const char *alt_tool_name) { |
| 110 |
#if !SANITIZER_GO |
| 111 |
if (!common_flags()->print_summary) |
| 112 |
return; |
| 113 |
|
| 114 |
// Find first non-internal stack frame. |
| 115 |
for (uptr i = 0; i < stack->size; ++i) { |
| 116 |
uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[i]); |
| 117 |
SymbolizedStackHolder symbolized_stack( |
| 118 |
Symbolizer::GetOrInit()->SymbolizePC(pc)); |
| 119 |
if (const SymbolizedStack *frame = symbolized_stack.get()) { |
| 120 |
if (const SymbolizedStack *summary_frame = SkipInternalFrames(frame)) { |
| 121 |
ReportErrorSummary(error_type, summary_frame->info, alt_tool_name); |
| 122 |
return; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Fallback to the top one. |
| 128 |
if (stack->size) { |
| 129 |
uptr pc = StackTrace::GetPreviousInstructionPc(stack->trace[0]); |
| 130 |
SymbolizedStackHolder symbolized_stack( |
| 131 |
Symbolizer::GetOrInit()->SymbolizePC(pc)); |
| 132 |
if (const SymbolizedStack *frame = symbolized_stack.get()) { |
| 133 |
ReportErrorSummary(error_type, frame->info, alt_tool_name); |
| 134 |
return; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// Fallback to a summary without location. |
| 139 |
ReportErrorSummary(error_type); |
| 140 |
#endif |
| 141 |
} |
| 142 |
|
| 143 |
void ReportMmapWriteExec(int prot, int flags) { |
| 144 |
#if SANITIZER_POSIX && (!SANITIZER_GO && !SANITIZER_ANDROID) |
| 145 |
int pflags = (PROT_WRITE | PROT_EXEC); |
| 146 |
if ((prot & pflags) != pflags) |
| 147 |
return; |
| 148 |
|
| 149 |
# if SANITIZER_APPLE && defined(MAP_JIT) |
| 150 |
if ((flags & MAP_JIT) == MAP_JIT) |
| 151 |
return; |
| 152 |
# endif |
| 153 |
|
| 154 |
ScopedErrorReportLock l; |
| 155 |
SanitizerCommonDecorator d; |
| 156 |
|
| 157 |
InternalMmapVector<BufferedStackTrace> stack_buffer(1); |
| 158 |
BufferedStackTrace *stack = stack_buffer.data(); |
| 159 |
stack->Reset(); |
| 160 |
uptr top = 0; |
| 161 |
uptr bottom = 0; |
| 162 |
GET_CALLER_PC_BP; |
| 163 |
bool fast = common_flags()->fast_unwind_on_fatal; |
| 164 |
if (StackTrace::WillUseFastUnwind(fast)) { |
| 165 |
GetThreadStackTopAndBottom(false, &top, &bottom); |
| 166 |
stack->Unwind(kStackTraceMax, pc, bp, nullptr, top, bottom, true); |
| 167 |
} else { |
| 168 |
stack->Unwind(kStackTraceMax, pc, 0, nullptr, 0, 0, false); |
| 169 |
} |
| 170 |
|
| 171 |
Printf("%s", d.Warning()); |
| 172 |
Report("WARNING: %s: writable-executable page usage\n", SanitizerToolName); |
| 173 |
Printf("%s", d.Default()); |
| 174 |
|
| 175 |
stack->Print(); |
| 176 |
ReportErrorSummary("w-and-x-usage", stack); |
| 177 |
#endif |
| 178 |
} |
| 179 |
|
| 180 |
#if !SANITIZER_FUCHSIA && !SANITIZER_GO |
| 181 |
void StartReportDeadlySignal() { |
| 182 |
// Write the first message using fd=2, just in case. |
| 183 |
// It may actually fail to write in case stderr is closed. |
| 184 |
CatastrophicErrorWrite(SanitizerToolName, internal_strlen(SanitizerToolName)); |
| 185 |
static const char kDeadlySignal[] = ":DEADLYSIGNAL\n"; |
| 186 |
CatastrophicErrorWrite(kDeadlySignal, sizeof(kDeadlySignal) - 1); |
| 187 |
} |
| 188 |
|
| 189 |
static void MaybeReportNonExecRegion(uptr pc) { |
| 190 |
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD |
| 191 |
MemoryMappingLayout proc_maps(/*cache_enabled*/ true); |
| 192 |
MemoryMappedSegment segment; |
| 193 |
while (proc_maps.Next(&segment)) { |
| 194 |
if (pc >= segment.start && pc < segment.end && !segment.IsExecutable()) |
| 195 |
Report("Hint: PC is at a non-executable region. Maybe a wild jump?\n"); |
| 196 |
} |
| 197 |
#endif |
| 198 |
} |
| 199 |
|
| 200 |
static void PrintMemoryByte(InternalScopedString *str, const char *before, |
| 201 |
u8 byte) { |
| 202 |
SanitizerCommonDecorator d; |
| 203 |
str->AppendF("%s%s%x%x%s ", before, d.MemoryByte(), byte >> 4, byte & 15, |
| 204 |
d.Default()); |
| 205 |
} |
| 206 |
|
| 207 |
static void MaybeDumpInstructionBytes(uptr pc) { |
| 208 |
if (!common_flags()->dump_instruction_bytes || (pc < GetPageSizeCached())) |
| 209 |
return; |
| 210 |
InternalScopedString str; |
| 211 |
str.AppendF("First 16 instruction bytes at pc: "); |
| 212 |
if (IsAccessibleMemoryRange(pc, 16)) { |
| 213 |
for (int i = 0; i < 16; ++i) { |
| 214 |
PrintMemoryByte(&str, "", ((u8 *)pc)[i]); |
| 215 |
} |
| 216 |
str.AppendF("\n"); |
| 217 |
} else { |
| 218 |
str.AppendF("unaccessible\n"); |
| 219 |
} |
| 220 |
Report("%s", str.data()); |
| 221 |
} |
| 222 |
|
| 223 |
static void MaybeDumpRegisters(void *context) { |
| 224 |
if (!common_flags()->dump_registers) return; |
| 225 |
SignalContext::DumpAllRegisters(context); |
| 226 |
} |
| 227 |
|
| 228 |
static void ReportStackOverflowImpl(const SignalContext &sig, u32 tid, |
| 229 |
UnwindSignalStackCallbackType unwind, |
| 230 |
const void *unwind_context) { |
| 231 |
SanitizerCommonDecorator d; |
| 232 |
Printf("%s", d.Warning()); |
| 233 |
static const char kDescription[] = "stack-overflow"; |
| 234 |
Report("ERROR: %s: %s on address %p (pc %p bp %p sp %p T%d)\n", |
| 235 |
SanitizerToolName, kDescription, (void *)sig.addr, (void *)sig.pc, |
| 236 |
(void *)sig.bp, (void *)sig.sp, tid); |
| 237 |
Printf("%s", d.Default()); |
| 238 |
// Avoid SEGVs in the unwinder when bp couldn't be determined. |
| 239 |
if (sig.bp) { |
| 240 |
InternalMmapVector<BufferedStackTrace> stack_buffer(1); |
| 241 |
BufferedStackTrace *stack = stack_buffer.data(); |
| 242 |
stack->Reset(); |
| 243 |
unwind(sig, unwind_context, stack); |
| 244 |
stack->Print(); |
| 245 |
ReportErrorSummary(kDescription, stack); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
static void ReportDeadlySignalImpl(const SignalContext &sig, u32 tid, |
| 250 |
UnwindSignalStackCallbackType unwind, |
| 251 |
const void *unwind_context) { |
| 252 |
SanitizerCommonDecorator d; |
| 253 |
Printf("%s", d.Warning()); |
| 254 |
const char *description = sig.Describe(); |
| 255 |
if (sig.is_memory_access && !sig.is_true_faulting_addr) |
| 256 |
Report("ERROR: %s: %s on unknown address (pc %p bp %p sp %p T%d)\n", |
| 257 |
SanitizerToolName, description, (void *)sig.pc, (void *)sig.bp, |
| 258 |
(void *)sig.sp, tid); |
| 259 |
else |
| 260 |
Report("ERROR: %s: %s on unknown address %p (pc %p bp %p sp %p T%d)\n", |
| 261 |
SanitizerToolName, description, (void *)sig.addr, (void *)sig.pc, |
| 262 |
(void *)sig.bp, (void *)sig.sp, tid); |
| 263 |
Printf("%s", d.Default()); |
| 264 |
if (sig.pc < GetPageSizeCached()) |
| 265 |
Report("Hint: pc points to the zero page.\n"); |
| 266 |
if (sig.is_memory_access) { |
| 267 |
const char *access_type = |
| 268 |
sig.write_flag == SignalContext::Write |
| 269 |
? "WRITE" |
| 270 |
: (sig.write_flag == SignalContext::Read ? "READ" : "UNKNOWN"); |
| 271 |
Report("The signal is caused by a %s memory access.\n", access_type); |
| 272 |
if (!sig.is_true_faulting_addr) |
| 273 |
Report("Hint: this fault was caused by a dereference of a high value " |
| 274 |
"address (see register values below). Disassemble the provided " |
| 275 |
"pc to learn which register was used.\n"); |
| 276 |
else if (sig.addr < GetPageSizeCached()) |
| 277 |
Report("Hint: address points to the zero page.\n"); |
| 278 |
} |
| 279 |
MaybeReportNonExecRegion(sig.pc); |
| 280 |
InternalMmapVector<BufferedStackTrace> stack_buffer(1); |
| 281 |
BufferedStackTrace *stack = stack_buffer.data(); |
| 282 |
stack->Reset(); |
| 283 |
unwind(sig, unwind_context, stack); |
| 284 |
stack->Print(); |
| 285 |
MaybeDumpInstructionBytes(sig.pc); |
| 286 |
MaybeDumpRegisters(sig.context); |
| 287 |
Printf("%s can not provide additional info.\n", SanitizerToolName); |
| 288 |
ReportErrorSummary(description, stack); |
| 289 |
} |
| 290 |
|
| 291 |
void ReportDeadlySignal(const SignalContext &sig, u32 tid, |
| 292 |
UnwindSignalStackCallbackType unwind, |
| 293 |
const void *unwind_context) { |
| 294 |
if (sig.IsStackOverflow()) |
| 295 |
ReportStackOverflowImpl(sig, tid, unwind, unwind_context); |
| 296 |
else |
| 297 |
ReportDeadlySignalImpl(sig, tid, unwind, unwind_context); |
| 298 |
} |
| 299 |
|
| 300 |
void HandleDeadlySignal(void *siginfo, void *context, u32 tid, |
| 301 |
UnwindSignalStackCallbackType unwind, |
| 302 |
const void *unwind_context) { |
| 303 |
StartReportDeadlySignal(); |
| 304 |
ScopedErrorReportLock rl; |
| 305 |
SignalContext sig(siginfo, context); |
| 306 |
ReportDeadlySignal(sig, tid, unwind, unwind_context); |
| 307 |
Report("ABORTING\n"); |
| 308 |
Die(); |
| 309 |
} |
| 310 |
|
| 311 |
#endif // !SANITIZER_FUCHSIA && !SANITIZER_GO |
| 312 |
|
| 313 |
atomic_uintptr_t ScopedErrorReportLock::reporting_thread_ = {0}; |
| 314 |
StaticSpinMutex ScopedErrorReportLock::mutex_; |
| 315 |
|
| 316 |
void ScopedErrorReportLock::Lock() { |
| 317 |
uptr current = GetThreadSelf(); |
| 318 |
for (;;) { |
| 319 |
uptr expected = 0; |
| 320 |
if (atomic_compare_exchange_strong(&reporting_thread_, &expected, current, |
| 321 |
memory_order_relaxed)) { |
| 322 |
// We've claimed reporting_thread so proceed. |
| 323 |
mutex_.Lock(); |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
if (expected == current) { |
| 328 |
// This is either asynch signal or nested error during error reporting. |
| 329 |
// Fail simple to avoid deadlocks in Report(). |
| 330 |
|
| 331 |
// Can't use Report() here because of potential deadlocks in nested |
| 332 |
// signal handlers. |
| 333 |
CatastrophicErrorWrite(SanitizerToolName, |
| 334 |
internal_strlen(SanitizerToolName)); |
| 335 |
static const char msg[] = ": nested bug in the same thread, aborting.\n"; |
| 336 |
CatastrophicErrorWrite(msg, sizeof(msg) - 1); |
| 337 |
|
| 338 |
internal__exit(common_flags()->exitcode); |
| 339 |
} |
| 340 |
|
| 341 |
internal_sched_yield(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
void ScopedErrorReportLock::Unlock() { |
| 346 |
mutex_.Unlock(); |
| 347 |
atomic_store_relaxed(&reporting_thread_, 0); |
| 348 |
} |
| 349 |
|
| 350 |
void ScopedErrorReportLock::CheckLocked() { mutex_.CheckLocked(); } |
| 351 |
|
| 352 |
} // namespace __sanitizer |