[compiler-rt] r226878 - Fix/workaround for OS X truncated stacktraces taken by external tools

Kuba Brecka kuba.brecka at gmail.com
Thu Jan 22 15:36:47 PST 2015


Author: kuba.brecka
Date: Thu Jan 22 17:36:47 2015
New Revision: 226878

URL: http://llvm.org/viewvc/llvm-project?rev=226878&view=rev
Log:
Fix/workaround for OS X truncated stacktraces taken by external tools

This patch is a proposed solution for https://code.google.com/p/address-sanitizer/issues/detail?id=375:

When the stacktraces are captured and printed by ASan itself, they are fine, but when the program has already printed the report (or is just printing it), capturing a stacktrace via other means is broken. "Other means" include OS X CrashReporter, debuggers or calling backtrace() within the program. For example calling backtrace() from a sanitizer_set_death_callback function prints a very truncated stacktrace.

Reviewed at http://reviews.llvm.org/D7103


Added:
    compiler-rt/trunk/test/asan/TestCases/Darwin/crashlog-stacktraces.c
Modified:
    compiler-rt/trunk/lib/asan/asan_mac.cc
    compiler-rt/trunk/lib/asan/asan_report.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h

Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=226878&r1=226877&r2=226878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Thu Jan 22 17:36:47 2015
@@ -374,13 +374,6 @@ void dispatch_source_set_event_handler(d
     work(); \
   }
 
-// Forces the compiler to generate a frame pointer in the function.
-#define ENABLE_FRAME_POINTER                                       \
-  do {                                                             \
-    volatile uptr enable_fp;                                       \
-    enable_fp = GET_CURRENT_FRAME();                               \
-  } while (0)
-
 INTERCEPTOR(void, dispatch_async,
             dispatch_queue_t dq, void(^work)(void)) {
   ENABLE_FRAME_POINTER;

Modified: compiler-rt/trunk/lib/asan/asan_report.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_report.cc?rev=226878&r1=226877&r2=226878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_report.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_report.cc Thu Jan 22 17:36:47 2015
@@ -937,6 +937,8 @@ using namespace __asan;  // NOLINT
 
 void __asan_report_error(uptr pc, uptr bp, uptr sp, uptr addr, int is_write,
                          uptr access_size) {
+  ENABLE_FRAME_POINTER;
+  
   // Determine the error type.
   const char *bug_descr = "unknown-crash";
   if (AddrIsInMem(addr)) {

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h?rev=226878&r1=226877&r2=226878&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h Thu Jan 22 17:36:47 2015
@@ -325,4 +325,11 @@ extern "C" void* _ReturnAddress(void);
     } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
   }
 
+// Forces the compiler to generate a frame pointer in the function.
+#define ENABLE_FRAME_POINTER                                       \
+  do {                                                             \
+    volatile uptr enable_fp;                                       \
+    enable_fp = GET_CURRENT_FRAME();                               \
+  } while (0)
+
 #endif  // SANITIZER_DEFS_H

Added: compiler-rt/trunk/test/asan/TestCases/Darwin/crashlog-stacktraces.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Darwin/crashlog-stacktraces.c?rev=226878&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Darwin/crashlog-stacktraces.c (added)
+++ compiler-rt/trunk/test/asan/TestCases/Darwin/crashlog-stacktraces.c Thu Jan 22 17:36:47 2015
@@ -0,0 +1,43 @@
+// RUN: %clang_asan -O0 %s -o %t
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+#include <execinfo.h>
+#include <sanitizer/common_interface_defs.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+void death_function() {
+  fprintf(stderr, "DEATH CALLBACK\n");
+
+  void* callstack[128];
+  int i, frames = backtrace(callstack, 128);
+  char** strs = backtrace_symbols(callstack, frames);
+  for (i = 0; i < frames; ++i) {
+    fprintf(stderr, "%s\n", strs[i]);
+  }
+  free(strs);
+
+  fprintf(stderr, "END OF BACKTRACE\n");
+}
+
+int fault_function() {
+  char *x = (char*)malloc(10 * sizeof(char));
+  free(x);
+  return x[5];  // BOOM
+}
+
+int main() {
+  __sanitizer_set_death_callback(death_function);
+  fault_function();
+  return 0;
+}
+
+// CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
+// CHECK: {{READ of size 1 at 0x.* thread T0}}
+// CHECK: {{    #0 0x.* in fault_function}}
+
+// CHECK: DEATH CALLBACK
+// CHECK: death_function
+// CHECK: fault_function
+// CHECK: main
+// CHECK: END OF BACKTRACE





More information about the llvm-commits mailing list