[compiler-rt] r214833 - [UBSan] Allow to suppress reports from vptr checker for specified types.

Alexander Kornienko alexfh at google.com
Tue Aug 5 04:21:33 PDT 2014


On Tue, Aug 5, 2014 at 3:24 AM, Alexey Samsonov <vonosmas at gmail.com> wrote:

> Author: samsonov
> Date: Mon Aug  4 20:24:22 2014
> New Revision: 214833
>
> URL: http://llvm.org/viewvc/llvm-project?rev=214833&view=rev
> Log:
> [UBSan] Allow to suppress reports from vptr checker for specified types.
>
> Based on http://reviews.llvm.org/D4702 by Byoungyoung Lee!
>
> Modified:
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.cc
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.h
>
> compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc
>     compiler-rt/trunk/lib/ubsan/ubsan_diag.cc
>     compiler-rt/trunk/lib/ubsan/ubsan_diag.h
>     compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc
>     compiler-rt/trunk/lib/ubsan/ubsan_init.cc
>     compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/vptr.cpp
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.cc?rev=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.cc
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.cc Mon
> Aug  4 20:24:22 2014
> @@ -22,8 +22,8 @@
>  namespace __sanitizer {
>
>  static const char *const kTypeStrings[SuppressionTypeCount] = {
> -    "none",   "race", "mutex",           "thread",
> -    "signal", "leak", "called_from_lib", "deadlock"};
> +    "none", "race",            "mutex",    "thread",    "signal",
> +    "leak", "called_from_lib", "deadlock", "vptr_check"};
>
>  bool TemplateMatch(char *templ, const char *str) {
>    if (str == 0 || str[0] == 0)
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.h?rev=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.h
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_suppressions.h Mon
> Aug  4 20:24:22 2014
> @@ -27,6 +27,7 @@ enum SuppressionType {
>    SuppressionLeak,
>    SuppressionLib,
>    SuppressionDeadlock,
> +  SuppressionVptrCheck,
>    SuppressionTypeCount
>  };
>
>
> Modified:
> compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc?rev=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> ---
> compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc
> (original)
> +++
> compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_suppressions_test.cc
> Mon Aug  4 20:24:22 2014
> @@ -66,11 +66,13 @@ TEST(Suppressions, TypeStrings) {
>    CHECK(!internal_strcmp(SuppressionTypeString(SuppressionSignal),
> "signal"));
>    CHECK(!internal_strcmp(SuppressionTypeString(SuppressionLeak), "leak"));
>    CHECK(!internal_strcmp(SuppressionTypeString(SuppressionLib),
> -      "called_from_lib"));
> +                         "called_from_lib"));
>    CHECK(
>        !internal_strcmp(SuppressionTypeString(SuppressionDeadlock),
> "deadlock"));
> +  CHECK(!internal_strcmp(SuppressionTypeString(SuppressionVptrCheck),
> +                         "vptr_check"));
>    // Ensure this test is up-to-date when suppression types are added.
> -  CHECK_EQ(SuppressionTypeCount, 8);
> +  CHECK_EQ(9, SuppressionTypeCount);
>  }
>
>  class SuppressionContextTest : public ::testing::Test {
>
> 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=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/ubsan/ubsan_diag.cc (original)
> +++ compiler-rt/trunk/lib/ubsan/ubsan_diag.cc Mon Aug  4 20:24:22 2014
> @@ -312,3 +312,12 @@ ScopedReport::~ScopedReport() {
>    if (DieAfterReport)
>      Die();
>  }
> +
> +bool __ubsan::MatchSuppression(const char *Str, SuppressionType Type) {
> +  Suppression *s;
> +  // If .preinit_array is not used, it is possible that the UBSan runtime
> is not
> +  // initialized.
> +  if (!SANITIZER_CAN_USE_PREINIT_ARRAY)
> +    InitIfNecessary();
> +  return SuppressionContext::Get()->Match(Str, Type, &s);
> +}
>
> 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=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/ubsan/ubsan_diag.h (original)
> +++ compiler-rt/trunk/lib/ubsan/ubsan_diag.h Mon Aug  4 20:24:22 2014
> @@ -15,6 +15,7 @@
>
>  #include "ubsan_value.h"
>  #include "sanitizer_common/sanitizer_stacktrace.h"
> +#include "sanitizer_common/sanitizer_suppressions.h"
>
>  namespace __ubsan {
>
> @@ -218,6 +219,8 @@ public:
>    ~ScopedReport();
>  };
>
> +bool MatchSuppression(const char *Str, SuppressionType Type);
> +
>  } // namespace __ubsan
>
>  #endif // UBSAN_DIAG_H
>
> 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=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc (original)
> +++ compiler-rt/trunk/lib/ubsan/ubsan_handlers_cxx.cc Mon Aug  4 20:24:22
> 2014
> @@ -18,6 +18,7 @@
>  #include "ubsan_type_hash.h"
>
>  #include "sanitizer_common/sanitizer_common.h"
> +#include "sanitizer_common/sanitizer_suppressions.h"
>
>  using namespace __sanitizer;
>  using namespace __ubsan;
> @@ -33,6 +34,12 @@ static void HandleDynamicTypeCacheMiss(
>      // Just a cache miss. The type matches after all.
>      return;
>
> +  // Check if error report should be suppressed.
> +  DynamicTypeInfo DTI = getDynamicTypeInfo((void*)Pointer);
> +  if (DTI.isValid() &&
> +      MatchSuppression(DTI.getMostDerivedTypeName(),
> SuppressionVptrCheck))
> +    return;
> +
>    SourceLocation Loc = Data->Loc.acquire();
>    if (Loc.isDisabled())
>      return;
> @@ -44,23 +51,23 @@ static void HandleDynamicTypeCacheMiss(
>      << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer <<
> Data->Type;
>
>    // If possible, say what type it actually points to.
> -  DynamicTypeInfo DTI = getDynamicTypeInfo((void*)Pointer);
>    if (!DTI.isValid())
>      Diag(Pointer, DL_Note, "object has invalid vptr")
> -      << MangledName(DTI.getMostDerivedTypeName())
> -      << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
> +        << MangledName(DTI.getMostDerivedTypeName())
> +        << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
>    else if (!DTI.getOffset())
>      Diag(Pointer, DL_Note, "object is of type %0")
> -      << MangledName(DTI.getMostDerivedTypeName())
> -      << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0");
> +        << MangledName(DTI.getMostDerivedTypeName())
> +        << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0");
>    else
>      // FIXME: Find the type at the specified offset, and include that
>      //        in the note.
>      Diag(Pointer - DTI.getOffset(), DL_Note,
>           "object is base class subobject at offset %0 within object of
> type %1")
> -      << DTI.getOffset() << MangledName(DTI.getMostDerivedTypeName())
> -      << MangledName(DTI.getSubobjectTypeName())
> -      << Range(Pointer, Pointer + sizeof(uptr), "vptr for %2 base class
> of %1");
> +        << DTI.getOffset() << MangledName(DTI.getMostDerivedTypeName())
> +        << MangledName(DTI.getSubobjectTypeName())
> +        << Range(Pointer, Pointer + sizeof(uptr),
> +                 "vptr for %2 base class of %1");
>
>    MaybePrintStackTrace(pc, bp);
>  }
>
> Modified: compiler-rt/trunk/lib/ubsan/ubsan_init.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_init.cc?rev=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/ubsan/ubsan_init.cc (original)
> +++ compiler-rt/trunk/lib/ubsan/ubsan_init.cc Mon Aug  4 20:24:22 2014
> @@ -16,6 +16,7 @@
>  #include "sanitizer_common/sanitizer_common.h"
>  #include "sanitizer_common/sanitizer_libc.h"
>  #include "sanitizer_common/sanitizer_mutex.h"
> +#include "sanitizer_common/sanitizer_suppressions.h"
>  #include "sanitizer_common/sanitizer_symbolizer.h"
>
>  using namespace __ubsan;
> @@ -42,6 +43,7 @@ void __ubsan::InitIfNecessary() {
>    }
>    // Initialize UBSan-specific flags.
>    InitializeFlags();
> +  SuppressionContext::InitIfNecessary();
>    ubsan_inited = true;
>  }
>
>
> Modified: compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/vptr.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/vptr.cpp?rev=214833&r1=214832&r2=214833&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/vptr.cpp (original)
> +++ compiler-rt/trunk/test/ubsan/TestCases/TypeCheck/vptr.cpp Mon Aug  4
> 20:24:22 2014
> @@ -11,6 +11,19 @@
>  // RUN: UBSAN_OPTIONS=print_stacktrace=1 %run %t oU 2>&1 | FileCheck %s
> --check-prefix=CHECK-OFFSET --strict-whitespace
>  // RUN: UBSAN_OPTIONS=print_stacktrace=1 %run %t m0 2>&1 | FileCheck %s
> --check-prefix=CHECK-NULL-MEMBER --strict-whitespace
>
> +// RUN: %clangxx -fsanitize=vptr -fno-sanitize-recover -g %s -O3 -o %t
> +// RUN: (echo "vptr_check:S"; echo "vptr_check:T"; echo "vptr_check:U") >
> %t.supp
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.supp"
> UBSAN_OPTIONS=suppressions=%t.supp %run %t mS 2>&1
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.supp"
> UBSAN_OPTIONS=suppressions=%t.supp %run %t fS 2>&1
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.supp"
> UBSAN_OPTIONS=suppressions=%t.supp %run %t cS 2>&1
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.supp"
> UBSAN_OPTIONS=suppressions=%t.supp %run %t mV 2>&1
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.supp"
> UBSAN_OPTIONS=suppressions=%t.supp %run %t fV 2>&1
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.supp"
> UBSAN_OPTIONS=suppressions=%t.supp %run %t cV 2>&1
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.supp"
> UBSAN_OPTIONS=suppressions=%t.supp %run %t oU 2>&1
> +
> +// RUN: echo "vptr_check:S" > %t.loc-supp
> +// RUN: ASAN_OPTIONS="$ASAN_OPTIONS suppressions=%t.loc-supp"
> UBSAN_OPTIONS=suppressions=%t.loc-supp not %run %t x- 2>&1 | FileCheck %s
> --check-prefix=CHECK-LOC-SUPPRESS
>

This test doesn't do "export ASAN_OPTIONS=...", so using $ASAN_OPTIONS in
RUN: lines breaks when test runner checks for unset shell variables. I've
fixed this in r214855.


> +
>  // FIXME: This test produces linker errors on Darwin.
>  // XFAIL: darwin
>
> @@ -31,6 +44,8 @@ struct T : S {
>
>  struct U : S, T { virtual int v() { return 2; } };
>
> +struct V : S {};
> +
>  // Make p global so that lsan does not complain.
>  T *p = 0;
>
> @@ -84,6 +99,19 @@ int access_p(T *p, char type) {
>      {T &r = *p;}
>      break;
>
> +  case 'x':
> +    for (int i = 0; i < 2; i++) {
> +      // Check that the first iteration ("S") succeeds, while the second
> ("V") fails.
> +      p = reinterpret_cast<T*>((i == 0) ? new S : new V);
> +      // CHECK-LOC-SUPPRESS: vptr.cpp:[[@LINE+5]]:7: runtime error:
> member call on address [[PTR:0x[0-9a-f]*]] which does not point to an
> object of type 'T'
> +      // CHECK-LOC-SUPPRESS-NEXT: [[PTR]]: note: object is of type 'V'
> +      // CHECK-LOC-SUPPRESS-NEXT: {{^ .. .. .. ..  .. .. .. .. .. .. ..
> ..  }}
> +      // CHECK-LOC-SUPPRESS-NEXT: {{^
>  \^~~~~~~~~~~(~~~~~~~~~~~~)? *$}}
> +      // CHECK-LOC-SUPPRESS-NEXT: {{^              vptr for 'V'}}
> +      p->g();
> +    }
> +    return 0;
> +
>    case 'm':
>      // CHECK-MEMBER: vptr.cpp:[[@LINE+6]]:15: runtime error: member
> access within address [[PTR:0x[0-9a-f]*]] which does not point to an object
> of type 'T'
>      // CHECK-MEMBER-NEXT: [[PTR]]: note: object is of type
> [[DYN_TYPE:'S'|'U']]
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140805/de51152e/attachment.html>


More information about the llvm-commits mailing list