diff options
-rw-r--r-- | compiler-rt/lib/msan/msan.cc | 11 | ||||
-rw-r--r-- | compiler-rt/lib/msan/msan.h | 13 | ||||
-rw-r--r-- | compiler-rt/test/msan/check-handler.cc | 16 |
3 files changed, 38 insertions, 2 deletions
diff --git a/compiler-rt/lib/msan/msan.cc b/compiler-rt/lib/msan/msan.cc index e6226ba7670..227f35ae1ab 100644 --- a/compiler-rt/lib/msan/msan.cc +++ b/compiler-rt/lib/msan/msan.cc @@ -379,6 +379,14 @@ static void MsanOnDeadlySignal(int signo, void *siginfo, void *context) { HandleDeadlySignal(siginfo, context, GetTid(), &OnStackUnwind, nullptr); } +static void MsanCheckFailed(const char *file, int line, const char *cond, + u64 v1, u64 v2) { + Report("MemorySanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", file, + line, cond, (uptr)v1, (uptr)v2); + PRINT_CURRENT_STACK_CHECK(); + Die(); +} + void __msan_init() { CHECK(!msan_init_is_running); if (msan_inited) return; @@ -391,6 +399,9 @@ void __msan_init() { CacheBinaryName(); InitializeFlags(); + // Install tool-specific callbacks in sanitizer_common. + SetCheckFailedCallback(MsanCheckFailed); + __sanitizer_set_report_path(common_flags()->log_path); InitializeInterceptors(); diff --git a/compiler-rt/lib/msan/msan.h b/compiler-rt/lib/msan/msan.h index cbae444127e..ad1b8649574 100644 --- a/compiler-rt/lib/msan/msan.h +++ b/compiler-rt/lib/msan/msan.h @@ -356,14 +356,23 @@ const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1; common_flags()->fast_unwind_on_malloc); \ } +#define GET_STORE_STACK_TRACE \ + GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) + #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \ BufferedStackTrace stack; \ if (msan_inited) \ GetStackTrace(&stack, kStackTraceMax, pc, bp, nullptr, \ common_flags()->fast_unwind_on_fatal) -#define GET_STORE_STACK_TRACE \ - GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) +#define GET_FATAL_STACK_TRACE_HERE \ + GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME()) + +#define PRINT_CURRENT_STACK_CHECK() \ + { \ + GET_FATAL_STACK_TRACE_HERE; \ + stack.Print(); \ + } class ScopedThreadLocalStateBackup { public: diff --git a/compiler-rt/test/msan/check-handler.cc b/compiler-rt/test/msan/check-handler.cc new file mode 100644 index 00000000000..4721f8c3068 --- /dev/null +++ b/compiler-rt/test/msan/check-handler.cc @@ -0,0 +1,16 @@ +// RUN: %clangxx_msan -O0 -g %s -o %t && not %run %t 2>&1 | FileCheck %s + +// Verify that CHECK handler prints a stack on CHECK fail. + +#include <stdlib.h> + +int main(void) { + // Allocate chunk from the secondary allocator to trigger CHECK(IsALigned()) + // in its free() path. + void *p = malloc(8 << 20); + free(reinterpret_cast<char*>(p) + 1); + // CHECK: MemorySanitizer: bad pointer + // CHECK: MemorySanitizer CHECK failed + // CHECK: #0 + return 0; +} |