[compiler-rt] r218135 - [UBSan] Optionally report summary in UBSan error reports.

Alexey Samsonov vonosmas at gmail.com
Fri Sep 19 11:33:46 PDT 2014


Author: samsonov
Date: Fri Sep 19 13:33:45 2014
New Revision: 218135

URL: http://llvm.org/viewvc/llvm-project?rev=218135&view=rev
Log:
[UBSan] Optionally report summary in UBSan error reports.

By default summary is not printed if UBSan is run in a standalone mode,
but is printed if it's combined with another sanitizer (like ASan).

Added:
    compiler-rt/trunk/test/ubsan/TestCases/Integer/summary.cpp
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/ubsan/ubsan_diag.cc
    compiler-rt/trunk/lib/ubsan/ubsan_diag.h
    compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc
    compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc
    compiler-rt/trunk/test/ubsan/lit.common.cfg

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=218135&r1=218134&r2=218135&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Fri Sep 19 13:33:45 2014
@@ -250,7 +250,7 @@ const int kMaxSummaryLength = 1024;
 // and pass it to __sanitizer_report_error_summary.
 void ReportErrorSummary(const char *error_message);
 // Same as above, but construct error_message as:
-//   error_type: file:line function
+//   error_type file:line function
 void ReportErrorSummary(const char *error_type, const char *file,
                         int line, const char *function);
 void ReportErrorSummary(const char *error_type, StackTrace *trace);

Modified: compiler-rt/trunk/lib/ubsan/ubsan_diag.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_diag.cc?rev=218135&r1=218134&r2=218135&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_diag.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_diag.cc Fri Sep 19 13:33:45 2014
@@ -38,6 +38,22 @@ static void MaybePrintStackTrace(uptr pc
   stack.Print();
 }
 
+static void MaybeReportErrorSummary(Location Loc) {
+  if (!common_flags()->print_summary)
+    return;
+  // Don't try to unwind the stack trace in UBSan summaries: just use the
+  // provided location.
+  if (Loc.isSourceLocation()) {
+    SourceLocation SLoc = Loc.getSourceLocation();
+    if (!SLoc.isInvalid()) {
+      ReportErrorSummary("runtime-error", SLoc.getFilename(), SLoc.getLine(),
+                         "");
+      return;
+    }
+  }
+  ReportErrorSummary("runtime-error");
+}
+
 namespace {
 class Decorator : public SanitizerCommonDecorator {
  public:
@@ -315,13 +331,15 @@ Diag::~Diag() {
                         NumRanges, Args);
 }
 
-ScopedReport::ScopedReport(ReportOptions Opts) : Opts(Opts) {
+ScopedReport::ScopedReport(ReportOptions Opts, Location SummaryLoc)
+    : Opts(Opts), SummaryLoc(SummaryLoc) {
   InitIfNecessary();
   CommonSanitizerReportMutex.Lock();
 }
 
 ScopedReport::~ScopedReport() {
   MaybePrintStackTrace(Opts.pc, Opts.bp);
+  MaybeReportErrorSummary(SummaryLoc);
   CommonSanitizerReportMutex.Unlock();
   if (Opts.DieAfterReport || flags()->halt_on_error)
     Die();

Modified: compiler-rt/trunk/lib/ubsan/ubsan_diag.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_diag.h?rev=218135&r1=218134&r2=218135&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_diag.h (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_diag.h Fri Sep 19 13:33:45 2014
@@ -223,9 +223,10 @@ struct ReportOptions {
 /// different sanitizers won't be mixed.
 class ScopedReport {
   ReportOptions Opts;
+  Location SummaryLoc;
 
 public:
-  ScopedReport(ReportOptions Opts);
+  ScopedReport(ReportOptions Opts, Location SummaryLoc);
   ~ScopedReport();
 };
 

Modified: compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc?rev=218135&r1=218134&r2=218135&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_handlers.cc Fri Sep 19 13:33:45 2014
@@ -43,11 +43,11 @@ static void handleTypeMismatchImpl(TypeM
   if (ignoreReport(Loc.getSourceLocation(), Opts))
     return;
 
-  ScopedReport R(Opts);
-
   if (Data->Loc.isInvalid())
     Loc = FallbackLoc;
 
+  ScopedReport R(Opts, Loc);
+
   if (!Pointer)
     Diag(Loc, DL_Error, "%0 null pointer of type %1")
       << TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
@@ -85,7 +85,7 @@ static void handleIntegerOverflowImpl(Ov
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Diag(Loc, DL_Error, "%0 integer overflow: "
                       "%1 %2 %3 cannot be represented in type %4")
@@ -114,7 +114,7 @@ static void handleNegateOverflowImpl(Ove
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   if (Data->Type.isSignedIntegerTy())
     Diag(Loc, DL_Error,
@@ -145,7 +145,7 @@ static void handleDivremOverflowImpl(Ove
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Value LHSVal(Data->Type, LHS);
   Value RHSVal(Data->Type, RHS);
@@ -177,7 +177,7 @@ static void handleShiftOutOfBoundsImpl(S
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Value LHSVal(Data->LHSType, LHS);
   Value RHSVal(Data->RHSType, RHS);
@@ -216,7 +216,7 @@ static void handleOutOfBoundsImpl(OutOfB
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Value IndexVal(Data->IndexType, Index);
   Diag(Loc, DL_Error, "index %0 out of bounds for type %1")
@@ -237,7 +237,7 @@ void __ubsan::__ubsan_handle_out_of_boun
 
 static void handleBuiltinUnreachableImpl(UnreachableData *Data,
                                          ReportOptions Opts) {
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Data->Loc);
   Diag(Data->Loc, DL_Error, "execution reached a __builtin_unreachable() call");
 }
 
@@ -248,7 +248,7 @@ void __ubsan::__ubsan_handle_builtin_unr
 }
 
 static void handleMissingReturnImpl(UnreachableData *Data, ReportOptions Opts) {
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Data->Loc);
   Diag(Data->Loc, DL_Error,
        "execution reached the end of a value-returning function "
        "without returning a value");
@@ -266,7 +266,7 @@ static void handleVLABoundNotPositive(VL
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Diag(Loc, DL_Error, "variable length array bound evaluates to "
                       "non-positive value %0")
@@ -288,11 +288,12 @@ void __ubsan::__ubsan_handle_vla_bound_n
 static void handleFloatCastOverflow(FloatCastOverflowData *Data,
                                     ValueHandle From, ReportOptions Opts) {
   // TODO: Add deduplication once a SourceLocation is generated for this check.
-  ScopedReport R(Opts);
+  Location Loc = getCallerLocation();
+  ScopedReport R(Opts, Loc);
 
-  Diag(getCallerLocation(), DL_Error,
+  Diag(Loc, DL_Error,
        "value %0 is outside the range of representable values of type %2")
-    << Value(Data->FromType, From) << Data->FromType << Data->ToType;
+      << Value(Data->FromType, From) << Data->FromType << Data->ToType;
 }
 
 void __ubsan::__ubsan_handle_float_cast_overflow(FloatCastOverflowData *Data,
@@ -314,7 +315,7 @@ static void handleLoadInvalidValue(Inval
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Diag(Loc, DL_Error,
        "load of value %0, which is not a valid value for type %1")
@@ -340,7 +341,7 @@ static void handleFunctionTypeMismatch(F
 
   Location Loc = getFunctionLocation(Function, &FName);
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Diag(Data->Loc, DL_Error,
        "call to function %0 through pointer to incorrect function type %1")
@@ -367,7 +368,7 @@ static void handleNonNullReturn(NonNullR
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Diag(Loc, DL_Error, "null pointer returned from function declared to never "
                       "return null");
@@ -391,7 +392,7 @@ static void handleNonNullArg(NonNullArgD
   if (ignoreReport(Loc, Opts))
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Diag(Loc, DL_Error, "null pointer passed as argument %0, which is declared to "
        "never be null") << Data->ArgIndex;

Modified: compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc?rev=218135&r1=218134&r2=218135&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc Fri Sep 19 13:33:45 2014
@@ -44,7 +44,7 @@ static void HandleDynamicTypeCacheMiss(
   if (Loc.isDisabled())
     return;
 
-  ScopedReport R(Opts);
+  ScopedReport R(Opts, Loc);
 
   Diag(Loc, DL_Error,
        "%0 address %1 which does not point to an object of type %2")

Added: compiler-rt/trunk/test/ubsan/TestCases/Integer/summary.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/Integer/summary.cpp?rev=218135&view=auto
==============================================================================
--- compiler-rt/trunk/test/ubsan/TestCases/Integer/summary.cpp (added)
+++ compiler-rt/trunk/test/ubsan/TestCases/Integer/summary.cpp Fri Sep 19 13:33:45 2014
@@ -0,0 +1,10 @@
+// RUN: %clangxx -fsanitize=integer %s -o %t && %t 2>&1 | FileCheck %s
+// REQUIRES: ubsan-asan
+
+#include <stdint.h>
+
+int main() {
+  (void)(uint64_t(10000000000000000000ull) + uint64_t(9000000000000000000ull));
+  // CHECK: SUMMARY: AddressSanitizer: runtime-error {{.*}}summary.cpp:[[@LINE-1]]
+  return 0;
+}

Modified: compiler-rt/trunk/test/ubsan/lit.common.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/lit.common.cfg?rev=218135&r1=218134&r2=218135&view=diff
==============================================================================
--- compiler-rt/trunk/test/ubsan/lit.common.cfg (original)
+++ compiler-rt/trunk/test/ubsan/lit.common.cfg Fri Sep 19 13:33:45 2014
@@ -18,9 +18,11 @@ config.test_source_root = os.path.dirnam
 ubsan_lit_test_mode = get_required_attr(config, 'ubsan_lit_test_mode')
 if ubsan_lit_test_mode == "Standalone":
   config.name = 'UndefinedBehaviorSanitizer-Standalone'
+  config.available_features.add("ubsan-standalone")
   clang_ubsan_cflags = []
 elif ubsan_lit_test_mode == "AddressSanitizer":
   config.name = 'UndefinedBehaviorSanitizer-AddressSanitizer'
+  config.available_features.add("ubsan-asan")
   clang_ubsan_cflags = ["-fsanitize=address"]
   config.environment['ASAN_OPTIONS'] = 'detect_leaks=0'
 else:





More information about the llvm-commits mailing list