[compiler-rt] r302218 - [ubsan] Implement __sanitizer_print_stack_trace for standalone UBSan runtime.

Alexander Potapenko via llvm-commits llvm-commits at lists.llvm.org
Fri May 5 02:02:29 PDT 2017


Author: glider
Date: Fri May  5 04:02:28 2017
New Revision: 302218

URL: http://llvm.org/viewvc/llvm-project?rev=302218&view=rev
Log:
[ubsan] Implement __sanitizer_print_stack_trace for standalone UBSan runtime.

Patch by Max Moroz, reviewed at https://reviews.llvm.org/D32542

Added:
    compiler-rt/trunk/lib/ubsan/ubsan_diag_standalone.cc
    compiler-rt/trunk/test/ubsan/TestCases/Misc/Linux/print_stack_trace.cc
Modified:
    compiler-rt/trunk/lib/ubsan/CMakeLists.txt

Modified: compiler-rt/trunk/lib/ubsan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/CMakeLists.txt?rev=302218&r1=302217&r2=302218&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/ubsan/CMakeLists.txt Fri May  5 04:02:28 2017
@@ -9,6 +9,7 @@ set(UBSAN_SOURCES
   )
 
 set(UBSAN_STANDALONE_SOURCES
+  ubsan_diag_standalone.cc
   ubsan_init_standalone.cc
   )
 

Added: compiler-rt/trunk/lib/ubsan/ubsan_diag_standalone.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_diag_standalone.cc?rev=302218&view=auto
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_diag_standalone.cc (added)
+++ compiler-rt/trunk/lib/ubsan/ubsan_diag_standalone.cc Fri May  5 04:02:28 2017
@@ -0,0 +1,37 @@
+//===-- ubsan_diag_standalone.cc ------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Diagnostic reporting for the standalone UBSan runtime.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ubsan_platform.h"
+#if CAN_SANITIZE_UB
+#include "ubsan_diag.h"
+
+using namespace __ubsan;
+
+extern "C" {
+SANITIZER_INTERFACE_ATTRIBUTE
+void __sanitizer_print_stack_trace() {
+  uptr top = 0;
+  uptr bottom = 0;
+  bool request_fast_unwind = common_flags()->fast_unwind_on_fatal;
+  if (request_fast_unwind)
+    __sanitizer::GetThreadStackTopAndBottom(false, &top, &bottom);
+
+  GET_REPORT_OPTIONS(false);
+  BufferedStackTrace stack;
+  stack.Unwind(kStackTraceMax, Opts.pc, Opts.bp, nullptr, top, bottom,
+               request_fast_unwind);
+  stack.Print();
+}
+} // extern "C"
+
+#endif  // CAN_SANITIZE_UB

Added: compiler-rt/trunk/test/ubsan/TestCases/Misc/Linux/print_stack_trace.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/Misc/Linux/print_stack_trace.cc?rev=302218&view=auto
==============================================================================
--- compiler-rt/trunk/test/ubsan/TestCases/Misc/Linux/print_stack_trace.cc (added)
+++ compiler-rt/trunk/test/ubsan/TestCases/Misc/Linux/print_stack_trace.cc Fri May  5 04:02:28 2017
@@ -0,0 +1,20 @@
+// RUN: %clangxx -fsanitize=undefined -O0 %s -o %t && UBSAN_OPTIONS=stack_trace_format=DEFAULT:fast_unwind_on_fatal=1 %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx -fsanitize=undefined -O0 %s -o %t && UBSAN_OPTIONS=stack_trace_format=DEFAULT:fast_unwind_on_fatal=0 %run %t 2>&1 | FileCheck %s
+
+// The test doesn't pass on Darwin in UBSan-TSan configuration, because TSan is
+// using the slow unwinder which is not supported on Darwin. The test should
+// be universal after landing of https://reviews.llvm.org/D32806.
+
+#include <sanitizer/common_interface_defs.h>
+
+static inline void FooBarBaz() {
+  __sanitizer_print_stack_trace();
+}
+
+int main() {
+  FooBarBaz();
+  return 0;
+}
+
+// CHECK: {{.*}} in FooBarBaz{{.*}}print_stack_trace.cc{{.*}}
+// CHECK: {{.*}} in main{{.*}}print_stack_trace.cc{{.*}}




More information about the llvm-commits mailing list