Cherry-pick libsanitizer pointer-pair tristate option.
authorMartin Liska <mliska@suse.cz>
Mon, 5 Feb 2018 11:01:50 +0000 (12:01 +0100)
committerMartin Liska <marxin@gcc.gnu.org>
Mon, 5 Feb 2018 11:01:50 +0000 (11:01 +0000)
2018-02-05  Martin Liska  <mliska@suse.cz>

* doc/invoke.texi: Cherry-pick upstream r323995.
2018-02-05  Martin Liska  <mliska@suse.cz>

* c-c++-common/asan/pointer-compare-1.c: Adjust ASAN_OPTIONS
options.
* c-c++-common/asan/pointer-compare-2.c: Likewise.
* c-c++-common/asan/pointer-subtract-1.c: Likewise.
* c-c++-common/asan/pointer-subtract-2.c: Likewise.
* c-c++-common/asan/pointer-subtract-3.c: Likewise.
* c-c++-common/asan/pointer-subtract-4.c: Likewise.
* c-c++-common/asan/pointer-compare-3.c: New test.
2018-02-05  Martin Liska  <mliska@suse.cz>

* asan/asan_flags.inc: Cherry-pick upstream r323995.
* asan/asan_report.cc (CheckForInvalidPointerPair):
Cherry-pick upstream r323995.

From-SVN: r257387

13 files changed:
gcc/ChangeLog
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/asan/pointer-compare-1.c
gcc/testsuite/c-c++-common/asan/pointer-compare-2.c
gcc/testsuite/c-c++-common/asan/pointer-compare-3.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/pointer-subtract-1.c
gcc/testsuite/c-c++-common/asan/pointer-subtract-2.c
gcc/testsuite/c-c++-common/asan/pointer-subtract-3.c
gcc/testsuite/c-c++-common/asan/pointer-subtract-4.c
libsanitizer/ChangeLog
libsanitizer/asan/asan_flags.inc
libsanitizer/asan/asan_report.cc

index 363c61774ff5abfecc2b4aa501d11c5cdf443340..e5a8c11b30390baeb9755e94d77b7e463a0515a8 100644 (file)
@@ -1,3 +1,7 @@
+2018-02-05  Martin Liska  <mliska@suse.cz>
+
+       * doc/invoke.texi: Cherry-pick upstream r323995.
+
 2018-02-05  Richard Sandiford  <richard.sandiford@linaro.org>
 
        * ira.c (ira_init_register_move_cost): Adjust comment.
index f3d93367640b4e48fffba58d05d352f682c42e9b..cf6d3ae5b990d32dcb97b82f4522f388c3217ea7 100644 (file)
@@ -11165,8 +11165,9 @@ The option must be combined with either @option{-fsanitize=kernel-address} or
 The option cannot be combined with @option{-fsanitize=thread}
 and/or @option{-fcheck-pointer-bounds}.
 Note: By default the check is disabled at run time.  To enable it,
-add @code{detect_invalid_pointer_pairs=1} to the environment variable
-@env{ASAN_OPTIONS}.
+add @code{detect_invalid_pointer_pairs=2} to the environment variable
+@env{ASAN_OPTIONS}. Using @code{detect_invalid_pointer_pairs=1} detects
+invalid operation only when both pointers are non-null.
 
 @item -fsanitize=pointer-subtract
 @opindex fsanitize=pointer-subtract
@@ -11176,8 +11177,9 @@ The option must be combined with either @option{-fsanitize=kernel-address} or
 The option cannot be combined with @option{-fsanitize=thread}
 and/or @option{-fcheck-pointer-bounds}.
 Note: By default the check is disabled at run time.  To enable it,
-add @code{detect_invalid_pointer_pairs=1} to the environment variable
-@env{ASAN_OPTIONS}.
+add @code{detect_invalid_pointer_pairs=2} to the environment variable
+@env{ASAN_OPTIONS}. Using @code{detect_invalid_pointer_pairs=1} detects
+invalid operation only when both pointers are non-null.
 
 @item -fsanitize=thread
 @opindex fsanitize=thread
index 3a879999e0ba13a49b94a0e0a1d47c2d848b15a0..074e6eadbd1803433bb32ac9553dafe3e845c244 100644 (file)
@@ -1,3 +1,14 @@
+2018-02-05  Martin Liska  <mliska@suse.cz>
+
+       * c-c++-common/asan/pointer-compare-1.c: Adjust ASAN_OPTIONS
+       options.
+       * c-c++-common/asan/pointer-compare-2.c: Likewise.
+       * c-c++-common/asan/pointer-subtract-1.c: Likewise.
+       * c-c++-common/asan/pointer-subtract-2.c: Likewise.
+       * c-c++-common/asan/pointer-subtract-3.c: Likewise.
+       * c-c++-common/asan/pointer-subtract-4.c: Likewise.
+       * c-c++-common/asan/pointer-compare-3.c: New test.
+
 2018-02-05  Richard Sandiford  <richard.sandiford@linaro.org>
 
        * gcc.target/aarch64/sve/vcond_4.c: Remove XFAILs.
index 2cc7395241ad4a668642c266de6b7c9dfbd32a44..cf67fe98bee84947c2cf8153618a0ad6438b2682 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do run } */
-/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=1:halt_on_error=0" } */
+/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=2:halt_on_error=0" } */
 /* { dg-options "-fsanitize=address,pointer-compare" } */
 
 volatile int v;
index 5539087e856467fb707efa2958902b5cc11e581e..d2142c8f1600903d9a672fb182e7c7d72a5eaba2 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do run } */
-/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=1 halt_on_error=1" } */
+/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=2 halt_on_error=1" } */
 /* { dg-options "-fsanitize=address,pointer-compare" } */
 
 volatile int v;
diff --git a/gcc/testsuite/c-c++-common/asan/pointer-compare-3.c b/gcc/testsuite/c-c++-common/asan/pointer-compare-3.c
new file mode 100644 (file)
index 0000000..7156e49
--- /dev/null
@@ -0,0 +1,39 @@
+/* { dg-do run } */
+/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=1:halt_on_error=1" } */
+/* { dg-options "-fsanitize=address,pointer-compare" } */
+
+int foo(char *p, char *q) {
+  return p <= q;
+}
+
+char global[8192] = {};
+char small_global[7] = {};
+
+int main() {
+  // Heap allocated memory.
+  char *p = (char *)__builtin_malloc(42);
+  int r = foo(p, 0);
+  __builtin_free(p);
+
+  p = (char *)__builtin_malloc(1024);
+  foo(0, p);
+  __builtin_free(p);
+
+  p = (char *)__builtin_malloc(4096);
+  foo(p, 0);
+  __builtin_free(p);
+
+  // Global variable.
+  foo(&global[0], 0);
+  foo(&global[1000], 0);
+
+  p = &small_global[0];
+  foo(p, 0);
+
+  // Stack variable.
+  char stack[10000];
+  foo(&stack[0], 0);
+  foo(0, &stack[9000]);
+
+  return 0;
+}
index 7cbef811d7b482fa3bb2a421fb589177aa007114..091a33f167d2a272745054a0f1ee77315a60c3d7 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do run } */
-/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=1 halt_on_error=0" } */
+/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=2 halt_on_error=0" } */
 /* { dg-options "-fsanitize=address,pointer-subtract" } */
 
 volatile __PTRDIFF_TYPE__ v;
index 6b65a16b559ca47f80a43f82da020b98326829e5..7ef106c03ce505fde5681d823d75dde15b731fc9 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do run } */
-/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=1 halt_on_error=1" } */
+/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=2 halt_on_error=1" } */
 /* { dg-options "-fsanitize=address,pointer-subtract" } */
 
 volatile __PTRDIFF_TYPE__ v;
index 5cbcda92503b8ad8afbfded74252bda2bb237c5a..7cae91b699108016da589b84ca634123e5711f50 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do run { target pthread_h } } */
-/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=1:halt_on_error=1" } */
+/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=2:halt_on_error=1" } */
 /* { dg-options "-fsanitize=address,pointer-subtract" } */
 /* { dg-additional-options "-pthread" { target pthread } } */
 
index 820f0aa64b02351d8192b30329f515050a641f30..af65c59512b8c3a5615894138935ceb66d6c6a10 100644 (file)
@@ -1,6 +1,6 @@
 /* { dg-do run { target pthread_h } } */
 /* { dg-shouldfail "asan" } */
-/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=1:halt_on_error=1" } */
+/* { dg-set-target-env-var ASAN_OPTIONS "detect_invalid_pointer_pairs=2:halt_on_error=1" } */
 /* { dg-options "-fsanitize=address,pointer-subtract" } */
 /* { dg-additional-options "-pthread" { target pthread } } */
 
index 0f2192aed4d553bcdec8222beabc38379fe7dfcd..5c95e45a8f262afec67319d3646317269a7d84c5 100644 (file)
@@ -1,3 +1,9 @@
+2018-02-05  Martin Liska  <mliska@suse.cz>
+
+       * asan/asan_flags.inc: Cherry-pick upstream r323995.
+       * asan/asan_report.cc (CheckForInvalidPointerPair):
+       Cherry-pick upstream r323995.
+
 2018-01-17  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 
        PR sanitizer/82825
index 3784f065be6bc4f07d984c03533da889e0a9e799..b4253e025243aa49d853c2d7c844e3c8781e2238 100644 (file)
@@ -134,9 +134,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 "
index 261ec1aece5dc544f5ec67f923b1edd126de6bd2..434aa734c8fec7169b6e6156a66f1f12af979f88 100644 (file)
@@ -340,7 +340,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<uptr>(p1);
   uptr a2 = reinterpret_cast<uptr>(p2);