[PATCH] -f{no-sanitize,{,no-}sanitize-recover}=all support
Jakub Jelinek
jakub@redhat.com
Mon Jan 5 17:40:00 GMT 2015
On Tue, Dec 23, 2014 at 04:47:06PM -0800, Alexey Samsonov wrote:
> Right, -fsanitize=all produces an error, while -fsanitize-recover=all
> enables recovery for all
> sanitizers, -fno-sanitize-recover=all disables recovery for everything.
Ok, here is a GCC patch for that.
-fno-sanitize=all is supported as a way to turn off all the sanitizers,
-f{,no-}sanitize-recover=all is supported too, and one can mix all
with other sanitizer names.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2015-01-05 Jakub Jelinek <jakub@redhat.com>
* opts.c (common_handle_option): Add support for
-fno-sanitize=all and -f{,no-}sanitize-recover=all.
* c-c++-common/asan/sanitize-all-1.c: New test.
* c-c++-common/ubsan/sanitize-all-1.c: New test.
* c-c++-common/ubsan/sanitize-all-2.c: New test.
* c-c++-common/ubsan/sanitize-all-3.c: New test.
* c-c++-common/ubsan/sanitize-all-4.c: New test.
--- gcc/opts.c.jj 2015-01-05 15:37:15.155023474 +0100
+++ gcc/opts.c 2015-01-05 16:05:18.659760976 +0100
@@ -1588,6 +1588,7 @@ common_handle_option (struct gcc_options
sizeof "returns-nonnull-attribute" - 1 },
{ "object-size", SANITIZE_OBJECT_SIZE,
sizeof "object-size" - 1 },
+ { "all", ~0, sizeof "all" - 1 },
{ NULL, 0, 0 }
};
const char *comma;
@@ -1611,7 +1612,15 @@ common_handle_option (struct gcc_options
&& memcmp (p, spec[i].name, len) == 0)
{
/* Handle both -fsanitize and -fno-sanitize cases. */
- if (value)
+ if (value && spec[i].flag == ~0U)
+ {
+ if (code == OPT_fsanitize_)
+ error_at (loc, "-fsanitize=all option is not valid");
+ else
+ *flag |= ~(SANITIZE_USER_ADDRESS | SANITIZE_THREAD
+ | SANITIZE_LEAK);
+ }
+ else if (value)
*flag |= spec[i].flag;
else
*flag &= ~spec[i].flag;
--- gcc/testsuite/c-c++-common/asan/sanitize-all-1.c.jj 2015-01-05 15:41:43.515357551 +0100
+++ gcc/testsuite/c-c++-common/asan/sanitize-all-1.c 2015-01-05 15:41:43.515357551 +0100
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-fno-sanitize=all" } */
+
+volatile int ten = 10;
+
+int main() {
+ volatile char x[10];
+ x[ten];
+ return 0;
+}
+
+/* { dg-final { scan-assembler-not "__asan_load" } } */
--- gcc/testsuite/c-c++-common/ubsan/sanitize-all-1.c.jj 2015-01-05 15:41:43.515357551 +0100
+++ gcc/testsuite/c-c++-common/ubsan/sanitize-all-1.c 2015-01-05 15:41:43.515357551 +0100
@@ -0,0 +1,8 @@
+/* Test -f*sanitize*=all */
+/* { dg-do compile } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O2" } } */
+/* { dg-options "-fsanitize=all" } */
+
+int i;
+
+/* { dg-error "-fsanitize=all option is not valid" "" { target *-*-* } 0 } */
--- gcc/testsuite/c-c++-common/ubsan/sanitize-all-2.c.jj 2015-01-05 15:41:43.516357533 +0100
+++ gcc/testsuite/c-c++-common/ubsan/sanitize-all-2.c 2015-01-05 17:46:57.634523012 +0100
@@ -0,0 +1,41 @@
+/* Test -f*sanitize*=all */
+/* { dg-do run } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O2" } } */
+/* { dg-skip-if "" { *-*-* } { "-flto -fno-fat-lto-objects" } } */
+/* { dg-options "-fsanitize=undefined,float-cast-overflow,float-divide-by-zero -fno-sanitize=all -fdump-tree-optimized" } */
+
+int a[4];
+
+int
+f1 (int x, int y, int z)
+{
+ return a[x] + (1 << y) + (100 / z);
+}
+
+char *
+f2 (int x)
+{
+ char *p = (char *) __builtin_calloc (64, 1);
+ p[x] = 3;
+ return p;
+}
+
+int
+f3 (int x, int *y, double z, double w)
+{
+ int a[*y];
+ if (x)
+ __builtin_unreachable ();
+ asm volatile ("" : : "r" (&a[0]));
+ return z / w;
+}
+
+int
+main ()
+{
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "__ubsan_" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "UBSAN_CHECK_" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
--- gcc/testsuite/c-c++-common/ubsan/sanitize-all-3.c.jj 2015-01-05 15:41:43.516357533 +0100
+++ gcc/testsuite/c-c++-common/ubsan/sanitize-all-3.c 2015-01-05 17:47:03.768418951 +0100
@@ -0,0 +1,42 @@
+/* Test -f*sanitize*=all */
+/* { dg-do run } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O2" } } */
+/* { dg-skip-if "" { *-*-* } { "-flto -fno-fat-lto-objects" } } */
+/* { dg-options "-fsanitize=undefined -fsanitize-recover=all -fdump-tree-optimized" } */
+
+int a[4];
+
+int
+f1 (int x, int y, int z)
+{
+ return a[x] + (1 << y) + (100 / z);
+}
+
+char *
+f2 (int x)
+{
+ char *p = (char *) __builtin_calloc (64, 1);
+ p[x] = 3;
+ return p;
+}
+
+int
+f3 (int x, int *y, double z, double w)
+{
+ int a[*y];
+ if (x)
+ __builtin_unreachable ();
+ asm volatile ("" : : "r" (&a[0]));
+ return z / w;
+}
+
+int
+main ()
+{
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump "__ubsan_" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__ubsan_\[a-z_\]*_abort" "optimized" } } */
+/* { dg-final { scan-tree-dump "UBSAN_CHECK_" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
--- gcc/testsuite/c-c++-common/ubsan/sanitize-all-4.c.jj 2015-01-05 15:41:43.516357533 +0100
+++ gcc/testsuite/c-c++-common/ubsan/sanitize-all-4.c 2015-01-05 17:47:09.969313754 +0100
@@ -0,0 +1,42 @@
+/* Test -f*sanitize*=all */
+/* { dg-do run } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O2" } } */
+/* { dg-skip-if "" { *-*-* } { "-flto -fno-fat-lto-objects" } } */
+/* { dg-options "-fsanitize=undefined -fno-sanitize-recover=all -fdump-tree-optimized" } */
+
+int a[4];
+
+int
+f1 (int x, int y, int z)
+{
+ return a[x] + (1 << y) + (100 / z);
+}
+
+char *
+f2 (int x)
+{
+ char *p = (char *) __builtin_calloc (64, 1);
+ p[x] = 3;
+ return p;
+}
+
+int
+f3 (int x, int *y, double z, double w)
+{
+ int a[*y];
+ if (x)
+ __builtin_unreachable ();
+ asm volatile ("" : : "r" (&a[0]));
+ return z / w;
+}
+
+int
+main ()
+{
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump "__ubsan_\[a-z_\]*_abort" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__ubsan_\[a-z_\]*\[^et\] " "optimized" } } */
+/* { dg-final { scan-tree-dump "UBSAN_CHECK_" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
Jakub
More information about the Gcc-patches
mailing list