From 3c80f4d9414bd21d5012486dbfcf1fc8f0af951b Mon Sep 17 00:00:00 2001 From: Alex Shlyapnikov Date: Thu, 1 Feb 2018 19:52:56 +0000 Subject: [PATCH] Make detect_invalid_pointer_pairs option to be tristate. Summary: With the change, one can choose not to report comparison (or subtraction) of a pointer with nullptr pointer. Reviewers: kcc, jakubjelinek, alekseyshl Reviewed By: alekseyshl Subscribers: kubamracek Differential Revision: https://reviews.llvm.org/D41479 llvm-svn: 323995 --- compiler-rt/lib/asan/asan_flags.inc | 6 +-- compiler-rt/lib/asan/asan_report.cc | 6 ++- .../Posix/invalid-pointer-pairs-threads.cc | 4 +- .../invalid-pointer-pairs-compare-errors.cc | 2 +- .../invalid-pointer-pairs-compare-null.cc | 42 +++++++++++++++++++ .../invalid-pointer-pairs-compare-success.cc | 2 +- .../invalid-pointer-pairs-subtract-errors.cc | 2 +- .../invalid-pointer-pairs-subtract-success.cc | 2 +- 8 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-null.cc diff --git a/compiler-rt/lib/asan/asan_flags.inc b/compiler-rt/lib/asan/asan_flags.inc index 00071d39f041..1663ae2a984d 100644 --- a/compiler-rt/lib/asan/asan_flags.inc +++ b/compiler-rt/lib/asan/asan_flags.inc @@ -136,9 +136,9 @@ ASAN_FLAG( "Android. ") ASAN_FLAG( int, detect_invalid_pointer_pairs, 0, - "If non-zero, try to detect operations like <, <=, >, >= and - on " - "invalid pointer pairs (e.g. when pointers belong to different objects). " - "The bigger the value the harder we try.") + "If >= 2, detect operations like <, <=, >, >= and - on invalid pointer " + "pairs (e.g. when pointers belong to different objects); " + "If == 1, detect invalid operations only when both pointers are non-null.") ASAN_FLAG( bool, detect_container_overflow, true, "If true, honor the container overflow annotations. See " diff --git a/compiler-rt/lib/asan/asan_report.cc b/compiler-rt/lib/asan/asan_report.cc index e3bc02994efd..3d0e88157a76 100644 --- a/compiler-rt/lib/asan/asan_report.cc +++ b/compiler-rt/lib/asan/asan_report.cc @@ -343,7 +343,11 @@ static bool IsInvalidPointerPair(uptr a1, uptr a2) { } static INLINE void CheckForInvalidPointerPair(void *p1, void *p2) { - if (!flags()->detect_invalid_pointer_pairs) return; + switch (flags()->detect_invalid_pointer_pairs) { + case 0 : return; + case 1 : if (p1 == nullptr || p2 == nullptr) return; break; + } + uptr a1 = reinterpret_cast(p1); uptr a2 = reinterpret_cast(p2); diff --git a/compiler-rt/test/asan/TestCases/Posix/invalid-pointer-pairs-threads.cc b/compiler-rt/test/asan/TestCases/Posix/invalid-pointer-pairs-threads.cc index 28be9b59117b..ee8a1c70cf03 100644 --- a/compiler-rt/test/asan/TestCases/Posix/invalid-pointer-pairs-threads.cc +++ b/compiler-rt/test/asan/TestCases/Posix/invalid-pointer-pairs-threads.cc @@ -1,7 +1,7 @@ // RUN: %clangxx_asan -O0 %s -pthread -o %t -mllvm -asan-detect-invalid-pointer-pair -// RUN: %env_asan_opts=detect_invalid_pointer_pairs=1 %run %t a 2>&1 | FileCheck %s -check-prefix=OK -allow-empty -// RUN: %env_asan_opts=detect_invalid_pointer_pairs=1 not %run %t b 2>&1 | FileCheck %s -check-prefix=B +// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2 %run %t a 2>&1 | FileCheck %s -check-prefix=OK -allow-empty +// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2 not %run %t b 2>&1 | FileCheck %s -check-prefix=B // pthread barriers are not available on OS X // UNSUPPORTED: darwin diff --git a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-errors.cc b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-errors.cc index 3c4f7d65eeb1..0690d40d2261 100644 --- a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-errors.cc +++ b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-errors.cc @@ -1,6 +1,6 @@ // RUN: %clangxx_asan -O0 %s -o %t -mllvm -asan-detect-invalid-pointer-pair -// RUN: %env_asan_opts=detect_invalid_pointer_pairs=1:halt_on_error=0 %run %t 2>&1 | FileCheck %s +// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2:halt_on_error=0 %run %t 2>&1 | FileCheck %s #include #include diff --git a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-null.cc b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-null.cc new file mode 100644 index 000000000000..dfc56616a4d6 --- /dev/null +++ b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-null.cc @@ -0,0 +1,42 @@ +// RUN: %clangxx_asan -O0 %s -o %t -mllvm -asan-detect-invalid-pointer-pair + +// RUN: %env_asan_opts=detect_invalid_pointer_pairs=1 %run %t + +#include +#include + +int foo(char *p, char *q) { + return p <= q; +} + +char global[8192] = {}; +char small_global[7] = {}; + +int main() { + // Heap allocated memory. + char *p = (char *)malloc(42); + int r = foo(p, nullptr); + free(p); + + p = (char *)malloc(1024); + foo(nullptr, p); + free(p); + + p = (char *)malloc(4096); + foo(p, nullptr); + free(p); + + // Global variable. + foo(&global[0], nullptr); + foo(&global[1000], nullptr); + + p = &small_global[0]; + foo(p, nullptr); + + // Stack variable. + char stack[10000]; + foo(&stack[0], nullptr); + foo(nullptr, &stack[9000]); + + return 0; +} diff --git a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-success.cc b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-success.cc index 565d39088340..d0d92265f002 100644 --- a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-success.cc +++ b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-compare-success.cc @@ -1,6 +1,6 @@ // RUN: %clangxx_asan -O0 %s -o %t -mllvm -asan-detect-invalid-pointer-pair -// RUN: %env_asan_opts=detect_invalid_pointer_pairs=1 %run %t +// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2 %run %t #include #include diff --git a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-errors.cc b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-errors.cc index 546f61f8184d..20aaebeb48e0 100644 --- a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-errors.cc +++ b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-errors.cc @@ -1,6 +1,6 @@ // RUN: %clangxx_asan -O0 %s -o %t -mllvm -asan-detect-invalid-pointer-pair -// RUN: %env_asan_opts=detect_invalid_pointer_pairs=1:halt_on_error=0 %run %t 2>&1 | FileCheck %s +// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2:halt_on_error=0 %run %t 2>&1 | FileCheck %s #include #include diff --git a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-success.cc b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-success.cc index de175a778c99..7ea120ed5c70 100644 --- a/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-success.cc +++ b/compiler-rt/test/asan/TestCases/invalid-pointer-pairs-subtract-success.cc @@ -1,6 +1,6 @@ // RUN: %clangxx_asan -O0 %s -o %t -mllvm -asan-detect-invalid-pointer-pair -// RUN: %env_asan_opts=detect_invalid_pointer_pairs=1 %run %t +// RUN: %env_asan_opts=detect_invalid_pointer_pairs=2 %run %t #include #include