public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Martin Liška" <mliska@suse.cz>
To: gcc-patches@gcc.gnu.org
Cc: Jakub Jelinek <jakub@redhat.com>, arnd@linaro.org
Subject: [PATCH] Come up with --param asan-stack-small-redzone (PR sanitizer/81715).
Date: Tue, 25 Sep 2018 09:05:00 -0000	[thread overview]
Message-ID: <e603cb5f-c598-f599-90df-33773a1bb357@suse.cz> (raw)

[-- Attachment #1: Type: text/plain, Size: 1441 bytes --]

Hi.

As requested in PR81715, GCC emits bigger middle redzones for small variables.
It's analyzed in following comment: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715#c28

For now I'm suggesting to shrink shadow memory for variables <= 16B to 32B (including variable storage).
LLVM is more aggressive as they allocate just 16B of shadow memory for variables <= 4B. That would
require bigger code refactoring in asan.c and I would like to avoid that.

For detailed statistics of Linux Kernel, please take a look here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81715#c29

I'm testing the patch now.

Thoughts?

gcc/ChangeLog:

2018-09-25  Martin Liska  <mliska@suse.cz>

	* cfgexpand.c (expand_stack_vars): Smaller middle redzones
	if requested.
	* doc/invoke.texi: New param.
	* params.def (PARAM_ASAN_STACK_SMALL_REDZONE): Likewise.
	* params.h (ASAN_STACK_SMALL_REDZONE): Likewise.

gcc/testsuite/ChangeLog:

2018-09-25  Martin Liska  <mliska@suse.cz>

	* c-c++-common/asan/asan-stack-small.c: New test.
---
 gcc/cfgexpand.c                               | 13 +++++++--
 gcc/doc/invoke.texi                           |  3 ++
 gcc/params.def                                |  6 ++++
 gcc/params.h                                  |  2 ++
 .../c-c++-common/asan/asan-stack-small.c      | 29 +++++++++++++++++++
 5 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/asan/asan-stack-small.c



[-- Attachment #2: 0001-Come-up-with-param-asan-stack-small-redzone-PR-sanit.patch --]
[-- Type: text/x-patch, Size: 3206 bytes --]

diff --git a/gcc/cfgexpand.c b/gcc/cfgexpand.c
index 35ca276e4ad..c735e9ca2a3 100644
--- a/gcc/cfgexpand.c
+++ b/gcc/cfgexpand.c
@@ -1128,9 +1128,18 @@ expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
 					MAX (alignb, ASAN_RED_ZONE_SIZE),
 					!FRAME_GROWS_DOWNWARD);
 	      tree repr_decl = NULL_TREE;
+	      poly_uint64 size = stack_vars[i].size;
+	      /* For small variables shrink middle redzone (including
+	       * variable store) just to ASAN_RED_ZONE_SIZE.  */
+	      if (ASAN_STACK_SMALL_REDZONE
+		  && i != n - 1
+		  && (stack_vars[i].size.to_constant ()
+		      <= (ASAN_RED_ZONE_SIZE / 2)))
+		;
+	      else
+		size += ASAN_RED_ZONE_SIZE;
 	      offset
-		= alloc_stack_frame_space (stack_vars[i].size
-					   + ASAN_RED_ZONE_SIZE,
+		= alloc_stack_frame_space (size,
 					   MAX (alignb, ASAN_RED_ZONE_SIZE));
 
 	      data->asan_vec.safe_push (prev_offset);
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 7ef4e7a449b..1c55887b9cd 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -11313,6 +11313,9 @@ Enable buffer overflow detection for stack objects.  This kind of
 protection is enabled by default when using @option{-fsanitize=address}.
 To disable stack protection use @option{--param asan-stack=0} option.
 
+@item asan-stack-small-redzone
+Emit smaller middle redzone for variables smaller or equal to 16 bytes.
+
 @item asan-instrument-reads
 Enable buffer overflow detection for memory reads.  This kind of
 protection is enabled by default when using @option{-fsanitize=address}.
diff --git a/gcc/params.def b/gcc/params.def
index 9f0697327d4..f1fafb4002d 100644
--- a/gcc/params.def
+++ b/gcc/params.def
@@ -1201,6 +1201,12 @@ DEFPARAM (PARAM_ASAN_STACK,
          "Enable asan stack protection.",
          1, 0, 1)
 
+DEFPARAM (PARAM_ASAN_STACK_SMALL_REDZONE,
+	  "asan-stack-small-redzone",
+	  "Emit smaller middle redzone for variables smaller "
+	  "or equal to 16 bytes.",
+	  0, 0, 1)
+
 DEFPARAM (PARAM_ASAN_PROTECT_ALLOCAS,
 	"asan-instrument-allocas",
 	"Enable asan allocas/VLAs protection.",
diff --git a/gcc/params.h b/gcc/params.h
index 8aa960a904e..42df40e66fd 100644
--- a/gcc/params.h
+++ b/gcc/params.h
@@ -236,6 +236,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_STACK_SMALL_REDZONE \
+  PARAM_VALUE (PARAM_ASAN_STACK_SMALL_REDZONE)
 #define ASAN_PROTECT_ALLOCAS \
   PARAM_VALUE (PARAM_ASAN_PROTECT_ALLOCAS)
 #define ASAN_GLOBALS \
diff --git a/gcc/testsuite/c-c++-common/asan/asan-stack-small.c b/gcc/testsuite/c-c++-common/asan/asan-stack-small.c
new file mode 100644
index 00000000000..dc08edaad71
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/asan/asan-stack-small.c
@@ -0,0 +1,29 @@
+/* { dg-do run } */
+/* { dg-options "--param asan-stack-small-redzone=1" } */
+
+char *pa;
+char *pb;
+char *pc;
+
+void access (volatile char *ptr)
+{
+  *ptr = 'x';
+}
+
+int main (int argc, char **argv)
+{
+  char a;
+  char b;
+  char c;
+
+  pa = &a;
+  pb = &b;
+  pc = &c;
+
+  access (pb);
+  access (pc);
+  // access 'b' here
+  access (pa + 32);
+
+  return 0;
+}


             reply	other threads:[~2018-09-25  9:05 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-25  9:05 Martin Liška [this message]
2018-09-25  9:49 ` Jakub Jelinek
2018-09-25 10:17   ` Martin Liška
2018-09-25 10:25     ` Martin Liška
2018-09-25 12:19     ` Jakub Jelinek
2018-09-25 15:40       ` Martin Liška
2018-09-25 15:55         ` Jakub Jelinek
2018-09-26  9:34           ` Martin Liška
2018-10-09  8:53             ` Martin Liška
2018-10-23  9:17               ` Martin Liška
2018-11-13  7:50                 ` Martin Liška
2018-11-28 12:01             ` Jakub Jelinek
2018-11-29 15:03               ` Martin Liška
2018-11-29 15:17                 ` Jakub Jelinek
2018-11-29 16:37                   ` Martin Liška
2018-11-29 16:50                     ` Jakub Jelinek
2018-11-30 11:44                       ` Martin Liška
2018-11-30 12:01                         ` Jakub Jelinek
2018-12-01  7:36                         ` [PATCH] Partial fix for asan on big endian targets (PR sanitizer/88289) Jakub Jelinek
2018-12-02 12:46                           ` [committed] Fix ICE in asan_clear_shadow (PR sanitizer/88291) Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e603cb5f-c598-f599-90df-33773a1bb357@suse.cz \
    --to=mliska@suse.cz \
    --cc=arnd@linaro.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).