Patchwork Add no_address_safety_analysis attribute (PR sanitizer/55435)

login
register
mail settings
Submitter Jakub Jelinek
Date Nov. 22, 2012, 11:07 a.m.
Message ID <20121122110748.GN2315@tucnak.redhat.com>
Download mbox | patch
Permalink /patch/201037/
State New
Headers show

Comments

Jakub Jelinek - Nov. 22, 2012, 11:07 a.m.
Hi!

This patch adds the no_address_safety_analysis attribute.  Tested on
x86_64-linux, ok for trunk?

2012-11-22  Jakub Jelinek  <jakub@redhat.com>

	PR sanitizer/55435
	* c-common.c (handle_no_address_safety_analysis_attribute): New
	function.
	(c_common_attribute_table): Add no_address_safety_analysis.

	* asan.c (gate_asan): Don't instrument functions with
	no_address_safety_analysis attribute.
	(gate_asan_O0): Use !optimize && gate_asan ().
	* doc/extend.texi (no_address_safety_analysis): Document new
	function attribute.

	* c-c++-common/asan/attrib-1.c: New test.


	Jakub
Konstantin Serebryany - Nov. 22, 2012, 6:58 p.m.
Looks good!
(not sure if I am a proper reviewer though)

--kcc

On Thu, Nov 22, 2012 at 3:07 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> Hi!
>
> This patch adds the no_address_safety_analysis attribute.  Tested on
> x86_64-linux, ok for trunk?
>
> 2012-11-22  Jakub Jelinek  <jakub@redhat.com>
>
>         PR sanitizer/55435
>         * c-common.c (handle_no_address_safety_analysis_attribute): New
>         function.
>         (c_common_attribute_table): Add no_address_safety_analysis.
>
>         * asan.c (gate_asan): Don't instrument functions with
>         no_address_safety_analysis attribute.
>         (gate_asan_O0): Use !optimize && gate_asan ().
>         * doc/extend.texi (no_address_safety_analysis): Document new
>         function attribute.
>
>         * c-c++-common/asan/attrib-1.c: New test.
>
> --- gcc/c-family/c-common.c.jj  2012-11-19 14:41:10.000000000 +0100
> +++ gcc/c-family/c-common.c     2012-11-22 11:51:48.053996443 +0100
> @@ -309,6 +309,8 @@ static tree handle_common_attribute (tre
>  static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
>  static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
>  static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
> +static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
> +                                                        int, bool *);
>  static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
>  static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
>  static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
> @@ -711,6 +713,10 @@ const struct attribute_spec c_common_att
>                               handle_cold_attribute, false },
>    { "hot",                    0, 0, true,  false, false,
>                               handle_hot_attribute, false },
> +  { "no_address_safety_analysis",
> +                             0, 0, true, false, false,
> +                             handle_no_address_safety_analysis_attribute,
> +                             false },
>    { "warning",               1, 1, true,  false, false,
>                               handle_error_attribute, false },
>    { "error",                 1, 1, true,  false, false,
> @@ -6477,6 +6483,22 @@ handle_cold_attribute (tree *node, tree
>      {
>        warning (OPT_Wattributes, "%qE attribute ignored", name);
>        *no_add_attrs = true;
> +    }
> +
> +  return NULL_TREE;
> +}
> +
> +/* Handle a "no_address_safety_analysis" attribute; arguments as in
> +   struct attribute_spec.handler.  */
> +
> +static tree
> +handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
> +                                            bool *no_add_attrs)
> +{
> +  if (TREE_CODE (*node) != FUNCTION_DECL)
> +    {
> +      warning (OPT_Wattributes, "%qE attribute ignored", name);
> +      *no_add_attrs = true;
>      }
>
>    return NULL_TREE;
> --- gcc/asan.c.jj       2012-11-19 14:41:20.000000000 +0100
> +++ gcc/asan.c  2012-11-22 11:35:47.310489415 +0100
> @@ -1587,7 +1587,9 @@ asan_instrument (void)
>  static bool
>  gate_asan (void)
>  {
> -  return flag_asan != 0;
> +  return flag_asan != 0
> +         && !lookup_attribute ("no_address_safety_analysis",
> +                               DECL_ATTRIBUTES (current_function_decl));
>  }
>
>  struct gimple_opt_pass pass_asan =
> @@ -1614,7 +1616,7 @@ struct gimple_opt_pass pass_asan =
>  static bool
>  gate_asan_O0 (void)
>  {
> -  return flag_asan != 0 && !optimize;
> +  return !optimize && gate_asan ();
>  }
>
>  struct gimple_opt_pass pass_asan_O0 =
> --- gcc/doc/extend.texi.jj      2012-11-19 14:41:08.000000000 +0100
> +++ gcc/doc/extend.texi 2012-11-22 11:52:20.913814831 +0100
> @@ -2130,10 +2130,11 @@ attributes are currently defined for fun
>  @code{weak}, @code{malloc}, @code{alias}, @code{ifunc},
>  @code{warn_unused_result}, @code{nonnull}, @code{gnu_inline},
>  @code{externally_visible}, @code{hot}, @code{cold}, @code{artificial},
> -@code{error} and @code{warning}.  Several other attributes are defined
> -for functions on particular target systems.  Other attributes,
> -including @code{section} are supported for variables declarations
> -(@pxref{Variable Attributes}) and for types (@pxref{Type Attributes}).
> +@code{no_address_safety_analysis}, @code{error} and @code{warning}.
> +Several other attributes are defined for functions on particular
> +target systems.  Other attributes, including @code{section} are
> +supported for variables declarations (@pxref{Variable Attributes})
> +and for types (@pxref{Type Attributes}).
>
>  GCC plugins may provide their own attributes.
>
> @@ -3416,6 +3417,12 @@ with computed goto or @code{asm goto}.
>  The @code{cold} attribute on labels is not implemented in GCC versions
>  earlier than 4.8.
>
> +@item no_address_safety_analysis
> +@cindex @code{no_address_safety_analysis} function attribute
> +The @code{no_address_safety_analysis} attribute on functions is used
> +to inform the compiler that it should not instrument memory accesses
> +in the function when compiling with the @option{-fsanitize=address} option.
> +
>  @item regparm (@var{number})
>  @cindex @code{regparm} attribute
>  @cindex functions that are passed arguments in registers on the 386
> --- gcc/testsuite/c-c++-common/asan/attrib-1.c.jj       2012-11-22 11:43:58.356674747 +0100
> +++ gcc/testsuite/c-c++-common/asan/attrib-1.c  2012-11-22 11:59:49.213328881 +0100
> @@ -0,0 +1,34 @@
> +/* PR sanitizer/55435 */
> +/* { dg-do compile } */
> +
> +__attribute__((no_address_safety_analysis)) int
> +f1 (int *p, int *q)
> +{
> +  *p = 42;
> +  return *q;
> +}
> +
> +void f2 (char *);
> +void f2 (char *) __attribute__((no_address_safety_analysis));
> +void f2 (char *) __attribute__((no_address_safety_analysis));
> +void f2 (char *);
> +
> +void
> +f2 (char *p)
> +{
> +  *p = 42;
> +}
> +
> +void f3 (short *);
> +__typeof (f3) f3  __attribute__((__no_address_safety_analysis__));
> +
> +void
> +f3 (short *p)
> +{
> +  *p = 42;
> +}
> +
> +int v __attribute__((no_address_safety_analysis)); /* { dg-warning "attribute ignored" } */
> +
> +/* { dg-final { scan-assembler-not "__asan_report_store" } } */
> +/* { dg-final { scan-assembler-not "__asan_report_load" } } */
>
>         Jakub

Patch

--- gcc/c-family/c-common.c.jj	2012-11-19 14:41:10.000000000 +0100
+++ gcc/c-family/c-common.c	2012-11-22 11:51:48.053996443 +0100
@@ -309,6 +309,8 @@  static tree handle_common_attribute (tre
 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
+static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
+							 int, bool *);
 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
@@ -711,6 +713,10 @@  const struct attribute_spec c_common_att
 			      handle_cold_attribute, false },
   { "hot",                    0, 0, true,  false, false,
 			      handle_hot_attribute, false },
+  { "no_address_safety_analysis",
+			      0, 0, true, false, false,
+			      handle_no_address_safety_analysis_attribute,
+			      false },
   { "warning",		      1, 1, true,  false, false,
 			      handle_error_attribute, false },
   { "error",		      1, 1, true,  false, false,
@@ -6477,6 +6483,22 @@  handle_cold_attribute (tree *node, tree
     {
       warning (OPT_Wattributes, "%qE attribute ignored", name);
       *no_add_attrs = true;
+    }
+
+  return NULL_TREE;
+}
+
+/* Handle a "no_address_safety_analysis" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
+					     bool *no_add_attrs)
+{
+  if (TREE_CODE (*node) != FUNCTION_DECL)
+    {
+      warning (OPT_Wattributes, "%qE attribute ignored", name);
+      *no_add_attrs = true;
     }
 
   return NULL_TREE;
--- gcc/asan.c.jj	2012-11-19 14:41:20.000000000 +0100
+++ gcc/asan.c	2012-11-22 11:35:47.310489415 +0100
@@ -1587,7 +1587,9 @@  asan_instrument (void)
 static bool
 gate_asan (void)
 {
-  return flag_asan != 0;
+  return flag_asan != 0
+	  && !lookup_attribute ("no_address_safety_analysis",
+				DECL_ATTRIBUTES (current_function_decl));
 }
 
 struct gimple_opt_pass pass_asan =
@@ -1614,7 +1616,7 @@  struct gimple_opt_pass pass_asan =
 static bool
 gate_asan_O0 (void)
 {
-  return flag_asan != 0 && !optimize;
+  return !optimize && gate_asan ();
 }
 
 struct gimple_opt_pass pass_asan_O0 =
--- gcc/doc/extend.texi.jj	2012-11-19 14:41:08.000000000 +0100
+++ gcc/doc/extend.texi	2012-11-22 11:52:20.913814831 +0100
@@ -2130,10 +2130,11 @@  attributes are currently defined for fun
 @code{weak}, @code{malloc}, @code{alias}, @code{ifunc},
 @code{warn_unused_result}, @code{nonnull}, @code{gnu_inline},
 @code{externally_visible}, @code{hot}, @code{cold}, @code{artificial},
-@code{error} and @code{warning}.  Several other attributes are defined
-for functions on particular target systems.  Other attributes,
-including @code{section} are supported for variables declarations
-(@pxref{Variable Attributes}) and for types (@pxref{Type Attributes}).
+@code{no_address_safety_analysis}, @code{error} and @code{warning}.
+Several other attributes are defined for functions on particular
+target systems.  Other attributes, including @code{section} are
+supported for variables declarations (@pxref{Variable Attributes})
+and for types (@pxref{Type Attributes}).
 
 GCC plugins may provide their own attributes.
 
@@ -3416,6 +3417,12 @@  with computed goto or @code{asm goto}.
 The @code{cold} attribute on labels is not implemented in GCC versions
 earlier than 4.8.
 
+@item no_address_safety_analysis
+@cindex @code{no_address_safety_analysis} function attribute
+The @code{no_address_safety_analysis} attribute on functions is used
+to inform the compiler that it should not instrument memory accesses
+in the function when compiling with the @option{-fsanitize=address} option.
+
 @item regparm (@var{number})
 @cindex @code{regparm} attribute
 @cindex functions that are passed arguments in registers on the 386
--- gcc/testsuite/c-c++-common/asan/attrib-1.c.jj	2012-11-22 11:43:58.356674747 +0100
+++ gcc/testsuite/c-c++-common/asan/attrib-1.c	2012-11-22 11:59:49.213328881 +0100
@@ -0,0 +1,34 @@ 
+/* PR sanitizer/55435 */
+/* { dg-do compile } */
+
+__attribute__((no_address_safety_analysis)) int
+f1 (int *p, int *q)
+{
+  *p = 42;
+  return *q;
+}
+
+void f2 (char *);
+void f2 (char *) __attribute__((no_address_safety_analysis));
+void f2 (char *) __attribute__((no_address_safety_analysis));
+void f2 (char *);
+
+void
+f2 (char *p)
+{
+  *p = 42;
+}
+
+void f3 (short *);
+__typeof (f3) f3  __attribute__((__no_address_safety_analysis__));
+
+void
+f3 (short *p)
+{
+  *p = 42;
+}
+
+int v __attribute__((no_address_safety_analysis)); /* { dg-warning "attribute ignored" } */
+
+/* { dg-final { scan-assembler-not "__asan_report_store" } } */
+/* { dg-final { scan-assembler-not "__asan_report_load" } } */