diff options
author | Vitaly Buka <vitalybuka@google.com> | 2017-09-13 06:24:59 +0000 |
---|---|---|
committer | Vitaly Buka <vitalybuka@google.com> | 2017-09-13 06:24:59 +0000 |
commit | ecc44ecdbc07bfdd7c420e7b32d321bb7d8677b8 (patch) | |
tree | 4b6e3eb2dcb5a26f21db582599d64935643a6cff | |
parent | dfa8741c9693c344477c842a25ee0cb6a6f59fcd (diff) | |
download | bcm5719-llvm-ecc44ecdbc07bfdd7c420e7b32d321bb7d8677b8.tar.gz bcm5719-llvm-ecc44ecdbc07bfdd7c420e7b32d321bb7d8677b8.zip |
[compiler-rt] Move dump_instruction_bytes and dump_registers into sanitizer_common
Summary: Part of https://github.com/google/sanitizers/issues/637
Reviewers: eugenis, alekseyshl
Subscribers: kubamracek, llvm-commits, dberris
Differential Revision: https://reviews.llvm.org/D37766
llvm-svn: 313117
-rw-r--r-- | compiler-rt/lib/asan/asan_descriptions.h | 1 | ||||
-rw-r--r-- | compiler-rt/lib/asan/asan_errors.cc | 31 | ||||
-rw-r--r-- | compiler-rt/lib/asan/asan_flags.inc | 5 | ||||
-rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_common.h | 4 | ||||
-rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cc | 41 | ||||
-rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_flags.inc | 5 | ||||
-rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_report_decorator.h | 1 | ||||
-rw-r--r-- | compiler-rt/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc (renamed from compiler-rt/test/asan/TestCases/Posix/dump_instruction_bytes.cc) | 7 |
8 files changed, 55 insertions, 40 deletions
diff --git a/compiler-rt/lib/asan/asan_descriptions.h b/compiler-rt/lib/asan/asan_descriptions.h index 5161715dfcb..006a240ac13 100644 --- a/compiler-rt/lib/asan/asan_descriptions.h +++ b/compiler-rt/lib/asan/asan_descriptions.h @@ -69,7 +69,6 @@ class Decorator : public __sanitizer::SanitizerCommonDecorator { return Default(); } } - const char *MemoryByte() { return Magenta(); } }; enum ShadowKind : u8 { diff --git a/compiler-rt/lib/asan/asan_errors.cc b/compiler-rt/lib/asan/asan_errors.cc index 0f2a9e26f21..c9cf8f70bf1 100644 --- a/compiler-rt/lib/asan/asan_errors.cc +++ b/compiler-rt/lib/asan/asan_errors.cc @@ -38,37 +38,6 @@ void ErrorStackOverflow::Print() { ReportErrorSummary(scariness.GetDescription(), &stack); } -static void MaybeDumpInstructionBytes(uptr pc) { - if (!flags()->dump_instruction_bytes || (pc < GetPageSizeCached())) return; - InternalScopedString str(1024); - str.append("First 16 instruction bytes at pc: "); - if (IsAccessibleMemoryRange(pc, 16)) { - for (int i = 0; i < 16; ++i) { - PrintMemoryByte(&str, "", ((u8 *)pc)[i], /*in_shadow*/ false, " "); - } - str.append("\n"); - } else { - str.append("unaccessible\n"); - } - Report("%s", str.data()); -} - -static void MaybeDumpRegisters(void *context) { - if (!flags()->dump_registers) return; - SignalContext::DumpAllRegisters(context); -} - -static void MaybeReportNonExecRegion(uptr pc) { -#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD - MemoryMappingLayout proc_maps(/*cache_enabled*/ true); - MemoryMappedSegment segment; - while (proc_maps.Next(&segment)) { - if (pc >= segment.start && pc < segment.end && !segment.IsExecutable()) - Report("Hint: PC is at a non-executable region. Maybe a wild jump?\n"); - } -#endif -} - void ErrorDeadlySignal::Print() { Decorator d; Printf("%s", d.Warning()); diff --git a/compiler-rt/lib/asan/asan_flags.inc b/compiler-rt/lib/asan/asan_flags.inc index 19ebc696edc..00071d39f04 100644 --- a/compiler-rt/lib/asan/asan_flags.inc +++ b/compiler-rt/lib/asan/asan_flags.inc @@ -147,11 +147,6 @@ ASAN_FLAG(int, detect_odr_violation, 2, "If >=2, detect violation of One-Definition-Rule (ODR); " "If ==1, detect ODR-violation only if the two variables " "have different sizes") -ASAN_FLAG(bool, dump_instruction_bytes, false, - "If true, dump 16 bytes starting at the instruction that caused SEGV") -ASAN_FLAG(bool, dump_registers, true, - "If true, dump values of CPU registers when SEGV happens. Only " - "available on OS X for now.") ASAN_FLAG(const char *, suppressions, "", "Suppressions file name.") ASAN_FLAG(bool, halt_on_error, true, "Crash the program after printing the first error report " diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 483af36d738..d2021a0c9da 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -313,6 +313,10 @@ const char *DescribeSignalOrException(int signo); // Signal reporting. void StartReportDeadlySignal(); bool IsStackOverflow(int code, const SignalContext &sig); +// FIXME: Hide after moving more signal handling code into common. +void MaybeReportNonExecRegion(uptr pc); +void MaybeDumpInstructionBytes(uptr pc); +void MaybeDumpRegisters(void *context); // Alternative signal stack (POSIX-only). void SetAlternateSignalStack(); void UnsetAlternateSignalStack(); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cc b/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cc index b54503a6d88..cccb3be3e24 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cc @@ -16,6 +16,8 @@ #include "sanitizer_allocator_interface.h" #include "sanitizer_file.h" #include "sanitizer_flags.h" +#include "sanitizer_procmaps.h" +#include "sanitizer_report_decorator.h" #include "sanitizer_stackdepot.h" #include "sanitizer_stacktrace.h" #include "sanitizer_symbolizer.h" @@ -145,6 +147,45 @@ void BackgroundThread(void *arg) { } #endif +void MaybeReportNonExecRegion(uptr pc) { +#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD + MemoryMappingLayout proc_maps(/*cache_enabled*/ true); + MemoryMappedSegment segment; + while (proc_maps.Next(&segment)) { + if (pc >= segment.start && pc < segment.end && !segment.IsExecutable()) + Report("Hint: PC is at a non-executable region. Maybe a wild jump?\n"); + } +#endif +} + +static void PrintMemoryByte(InternalScopedString *str, const char *before, + u8 byte) { + SanitizerCommonDecorator d; + str->append("%s%s%x%x%s ", before, d.MemoryByte(), byte >> 4, byte & 15, + d.Default()); +} + +void MaybeDumpInstructionBytes(uptr pc) { + if (!common_flags()->dump_instruction_bytes || (pc < GetPageSizeCached())) + return; + InternalScopedString str(1024); + str.append("First 16 instruction bytes at pc: "); + if (IsAccessibleMemoryRange(pc, 16)) { + for (int i = 0; i < 16; ++i) { + PrintMemoryByte(&str, "", ((u8 *)pc)[i]); + } + str.append("\n"); + } else { + str.append("unaccessible\n"); + } + Report("%s", str.data()); +} + +void MaybeDumpRegisters(void *context) { + if (!common_flags()->dump_registers) return; + SignalContext::DumpAllRegisters(context); +} + void WriteToSyslog(const char *msg) { InternalScopedString msg_copy(kErrorMessageBufferSize); msg_copy.append("%s", msg); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc b/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc index 9f71861efcd..eab33ab4562 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_flags.inc @@ -229,3 +229,8 @@ COMMON_FLAG(bool, print_cmdline, false, "Print command line on crash " "(asan only).") COMMON_FLAG(bool, html_cov_report, false, "Generate html coverage report.") COMMON_FLAG(const char *, sancov_path, "sancov", "Sancov tool location.") +COMMON_FLAG(bool, dump_instruction_bytes, false, + "If true, dump 16 bytes starting at the instruction that caused SEGV") +COMMON_FLAG(bool, dump_registers, true, + "If true, dump values of CPU registers when SEGV happens. Only " + "available on OS X for now.") diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_report_decorator.h b/compiler-rt/lib/sanitizer_common/sanitizer_report_decorator.h index daa7f00a09b..060b58d3f2d 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_report_decorator.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_report_decorator.h @@ -28,6 +28,7 @@ class SanitizerCommonDecorator { const char *Bold() const { return ansi_ ? "\033[1m" : ""; } const char *Default() const { return ansi_ ? "\033[1m\033[0m" : ""; } const char *Warning() const { return Red(); } + const char *MemoryByte() { return Magenta(); } protected: const char *Black() const { return ansi_ ? "\033[1m\033[30m" : ""; } diff --git a/compiler-rt/test/asan/TestCases/Posix/dump_instruction_bytes.cc b/compiler-rt/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc index b5b38ff0819..408da6bb6ce 100644 --- a/compiler-rt/test/asan/TestCases/Posix/dump_instruction_bytes.cc +++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/dump_instruction_bytes.cc @@ -1,10 +1,11 @@ -// Check that ASan prints the faulting instruction bytes on +// Check that sanitizer prints the faulting instruction bytes on // dump_instruction_bytes=1 -// RUN: %clangxx_asan %s -o %t -// RUN: %env_asan_opts=dump_instruction_bytes=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-DUMP +// RUN: %clangxx %s -o %t +// RUN: %env_tool_opts=dump_instruction_bytes=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-DUMP // RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NODUMP // // REQUIRES: x86-target-arch +// XFAIL: lsan, msan, tsan, ubsan int main() { #if defined(__x86_64__) |