asan.h (asan_sanitize_allocas_p): Declare.
authorMaxim Ostapenko <m.ostapenko@samsung.com>
Thu, 6 Jul 2017 16:05:00 +0000 (16:05 +0000)
committerMaxim Ostapenko <chefmax@gcc.gnu.org>
Thu, 6 Jul 2017 16:05:00 +0000 (19:05 +0300)
gcc/
* asan.h (asan_sanitize_allocas_p): Declare.
* asan.c (asan_sanitize_allocas_p): New function.
(handle_builtin_stack_restore): Bail out if !asan_sanitize_allocas_p.
(handle_builtin_alloca): Likewise.
* cfgexpand.c (expand_used_vars): Do not add allocas unpoisoning stuff
if !asan_sanitize_allocas_p.
* params.def (asan-instrument-allocas): Add new option.
* params.h (ASAN_PROTECT_ALLOCAS): Define.
* opts.c (common_handle_option): Disable allocas sanitization for
KASan by default.

gcc/testsuite/
* c-c++-common/asan/kasan-alloca-1.c: New test.
* c-c++-common/asan/kasan-alloca-2.c: Likewise.

From-SVN: r250032

gcc/ChangeLog
gcc/asan.c
gcc/asan.h
gcc/cfgexpand.c
gcc/opts.c
gcc/params.def
gcc/params.h
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/asan/kasan-alloca-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/kasan-alloca-2.c [new file with mode: 0644]

index f5614b01dcd81e4a88e0057e39f72dcdbde3e948..e0fe1a63858657d5755209219c84b62238fb2602 100644 (file)
@@ -1,3 +1,16 @@
+2017-07-06  Maxim Ostapenko  <m.ostapenko@samsung.com>
+
+       * asan.h (asan_sanitize_allocas_p): Declare.
+       * asan.c (asan_sanitize_allocas_p): New function.
+       (handle_builtin_stack_restore): Bail out if !asan_sanitize_allocas_p.
+       (handle_builtin_alloca): Likewise.
+       * cfgexpand.c (expand_used_vars): Do not add allocas unpoisoning stuff
+       if !asan_sanitize_allocas_p.
+       * params.def (asan-instrument-allocas): Add new option.
+       * params.h (ASAN_PROTECT_ALLOCAS): Define.
+       * opts.c (common_handle_option): Disable allocas sanitization for
+       KASan by default.
+
 2017-07-06  Maxim Ostapenko  <m.ostapenko@samsung.com>
 
        * asan.c: Include gimple-fold.h.
index 252e59fcd4d7c4d127304a08365857b21eec5140..95004d734a5556c0b5ef938401b42a3ab4f5ae5d 100644 (file)
@@ -310,6 +310,12 @@ asan_sanitize_stack_p (void)
   return (sanitize_flags_p (SANITIZE_ADDRESS) && ASAN_STACK);
 }
 
+bool
+asan_sanitize_allocas_p (void)
+{
+  return (asan_sanitize_stack_p () && ASAN_PROTECT_ALLOCAS);
+}
+
 /* Checks whether section SEC should be sanitized.  */
 
 static bool
@@ -569,7 +575,7 @@ get_last_alloca_addr ()
 static void
 handle_builtin_stack_restore (gcall *call, gimple_stmt_iterator *iter)
 {
-  if (!iter)
+  if (!iter || !asan_sanitize_allocas_p ())
     return;
 
   tree last_alloca = get_last_alloca_addr ();
@@ -607,7 +613,7 @@ handle_builtin_stack_restore (gcall *call, gimple_stmt_iterator *iter)
 static void
 handle_builtin_alloca (gcall *call, gimple_stmt_iterator *iter)
 {
-  if (!iter)
+  if (!iter || !asan_sanitize_allocas_p ())
     return;
 
   gassign *g;
index 4e8120ef76164d8114645a68942d27815ba04e6d..c82d4d901e50a9242b5070206a4fe60027160aae 100644 (file)
@@ -108,6 +108,8 @@ extern void set_sanitized_sections (const char *);
 
 extern bool asan_sanitize_stack_p (void);
 
+extern bool asan_sanitize_allocas_p (void);
+
 /* Return TRUE if builtin with given FCODE will be intercepted by
    libasan.  */
 
index dd7277f4a73013ac9b3c25fb50b01c24febd95b4..c427a89bab085c232120f1c85d09def5a3981d2e 100644 (file)
@@ -2241,7 +2241,7 @@ expand_used_vars (void)
       expand_stack_vars (NULL, &data);
     }
 
-  if ((flag_sanitize & SANITIZE_ADDRESS) && cfun->calls_alloca)
+  if (asan_sanitize_allocas_p () && cfun->calls_alloca)
     var_end_seq = asan_emit_allocas_unpoison (virtual_stack_dynamic_rtx,
                                              virtual_stack_vars_rtx,
                                              var_end_seq);
index 7460c2be1b6a47f7e1e6df448d746e0e78324627..7555ed5543407d1e068010940a5a514b61fae5e8 100644 (file)
@@ -1909,6 +1909,9 @@ common_handle_option (struct gcc_options *opts,
                                 opts_set->x_param_values);
          maybe_set_param_value (PARAM_ASAN_STACK, 0, opts->x_param_values,
                                 opts_set->x_param_values);
+         maybe_set_param_value (PARAM_ASAN_PROTECT_ALLOCAS, 0,
+                                opts->x_param_values,
+                                opts_set->x_param_values);
          maybe_set_param_value (PARAM_ASAN_USE_AFTER_RETURN, 0,
                                 opts->x_param_values,
                                 opts_set->x_param_values);
index 6b07518a34bd9233d4438f28291d9701e102f621..805302bb93e94cf7a57db590c5335c6116b49946 100644 (file)
@@ -1142,6 +1142,11 @@ DEFPARAM (PARAM_ASAN_STACK,
          "Enable asan stack protection.",
          1, 0, 1)
 
+DEFPARAM (PARAM_ASAN_PROTECT_ALLOCAS,
+       "asan-instrument-allocas",
+       "Enable asan allocas/VLAs protection.",
+       1, 0, 1)
+
 DEFPARAM (PARAM_ASAN_GLOBALS,
          "asan-globals",
          "Enable asan globals protection.",
index 8b916607f163e3701a167405c8babb55c7ad2fb2..2188e1877e9e79bfdafa4b56eda1d8a546662a3e 100644 (file)
@@ -232,6 +232,8 @@ extern void init_param_values (int *params);
   PARAM_VALUE (PARAM_ALLOW_PACKED_STORE_DATA_RACES)
 #define ASAN_STACK \
   PARAM_VALUE (PARAM_ASAN_STACK)
+#define ASAN_PROTECT_ALLOCAS \
+  PARAM_VALUE (PARAM_ASAN_PROTECT_ALLOCAS)
 #define ASAN_GLOBALS \
   PARAM_VALUE (PARAM_ASAN_GLOBALS)
 #define ASAN_INSTRUMENT_READS \
index 886689f6c9c04b81cf4d37d3661d002d4a517b68..ac18d1ab2931a8d3e7edb5a32f2988e2e63e4174 100644 (file)
@@ -1,3 +1,8 @@
+2017-07-06  Maxim Ostapenko  <m.ostapenko@samsung.com>
+
+       * c-c++-common/asan/kasan-alloca-1.c: New test.
+       * c-c++-common/asan/kasan-alloca-2.c: Likewise.
+
 2017-07-06  Maxim Ostapenko  <m.ostapenko@samsung.com>
 
        * c-c++-common/asan/alloca_big_alignment.c: New test.
diff --git a/gcc/testsuite/c-c++-common/asan/kasan-alloca-1.c b/gcc/testsuite/c-c++-common/asan/kasan-alloca-1.c
new file mode 100644 (file)
index 0000000..518d190
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-fno-sanitize=address -fsanitize=kernel-address -fdump-tree-sanopt" } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O0" } } */
+
+void foo(int index, int len) {
+  char str[len];
+  str[index] = '1'; // BOOM
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin___asan_alloca_poison" "sanopt" } } */
+/* { dg-final { scan-tree-dump-not "__builtin___asan_allocas_unpoison" "sanopt" } } */
diff --git a/gcc/testsuite/c-c++-common/asan/kasan-alloca-2.c b/gcc/testsuite/c-c++-common/asan/kasan-alloca-2.c
new file mode 100644 (file)
index 0000000..e3999d9
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-fno-sanitize=address -fsanitize=kernel-address --param asan-instrument-allocas=1 --param asan-stack=1 -fdump-tree-sanopt" } */
+/* { dg-skip-if "" { *-*-* } { "*" } { "-O0" } } */
+
+void foo(int index, int len) {
+  char str[len];
+  str[index] = '1'; // BOOM
+}
+
+/* { dg-final { scan-tree-dump-times "__builtin___asan_alloca_poison" 1 "sanopt" } } */
+/* { dg-final { scan-tree-dump-times "__builtin___asan_allocas_unpoison" 1 "sanopt" } } */