re PR sanitizer/58411 (no_sanitize_undefined function attribute)
authorMarek Polacek <polacek@redhat.com>
Wed, 18 Sep 2013 10:01:40 +0000 (10:01 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 18 Sep 2013 10:01:40 +0000 (10:01 +0000)
2013-09-18  Marek Polacek  <polacek@redhat.com>

PR sanitizer/58411
* doc/extend.texi: Document no_sanitize_undefined attribute.
* builtins.c (fold_builtin_0): Don't sanitize function if it has the
no_sanitize_undefined attribute.

From-SVN: r202682

gcc/ChangeLog
gcc/builtins.c
gcc/c-family/ChangeLog
gcc/c-family/c-common.c
gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/doc/extend.texi
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/attrib-1.c [new file with mode: 0644]

index ea746bc884e91e4b326d509ffa70df095c41b61d..8460d0afd6422d89453bc7d8a58b7be449538b6a 100644 (file)
@@ -1,3 +1,10 @@
+2013-09-18  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/58411
+       * doc/extend.texi: Document no_sanitize_undefined attribute.
+       * builtins.c (fold_builtin_0): Don't sanitize function if it has the
+       no_sanitize_undefined attribute.
+
 2013-09-18  Nick Clifton  <nickc@redhat.com>
 
        * config/msp430/msp430.h (ASM_SPEC): Pass -md on to the assembler.
index 0ab6d9b5d7058c27c5ac38f597cdd3026d3bd5c5..d19ca68baba7fd6c1bc4170f3595c4c09baef1e8 100644 (file)
@@ -10313,7 +10313,10 @@ fold_builtin_0 (location_t loc, tree fndecl, bool ignore ATTRIBUTE_UNUSED)
       return fold_builtin_classify_type (NULL_TREE);
 
     case BUILT_IN_UNREACHABLE:
-      if (flag_sanitize & SANITIZE_UNREACHABLE)
+      if (flag_sanitize & SANITIZE_UNREACHABLE
+         && (current_function_decl == NULL
+             || !lookup_attribute ("no_sanitize_undefined",
+                                   DECL_ATTRIBUTES (current_function_decl))))
        return ubsan_instrument_unreachable (loc);
       break;
 
index 3061b4a896d9c6fd828230e770a6c7d3bace36e7..1772ba56b490604dd09963cbac3d3bb62acf3d46 100644 (file)
@@ -1,3 +1,10 @@
+2013-09-18  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/58411
+       * c-common.c (handle_no_sanitize_undefined_attribute): New function.
+       Declare it.
+       (struct attribute_spec c_common_att): Add no_sanitize_undefined.
+
 2013-09-14  Iain Sandoe  <iain@codesourcery.com>
 
        PR target/48094
index 62aa9fcec2bb0192715ed698320c6adb56068fe5..8ecb70cfa7cc320f06b83bb892daf15a986a6d4a 100644 (file)
@@ -311,6 +311,8 @@ static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
                                                  int, bool *);
 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
                                                         int, bool *);
+static tree handle_no_sanitize_undefined_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 *);
@@ -722,6 +724,9 @@ const struct attribute_spec c_common_attribute_table[] =
   { "no_sanitize_address",    0, 0, true, false, false,
                              handle_no_sanitize_address_attribute,
                              false },
+  { "no_sanitize_undefined",  0, 0, true, false, false,
+                             handle_no_sanitize_undefined_attribute,
+                             false },
   { "warning",               1, 1, true,  false, false,
                              handle_error_attribute, false },
   { "error",                 1, 1, true,  false, false,
@@ -6575,6 +6580,22 @@ handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
   return NULL_TREE;
 }
 
+/* Handle a "no_sanitize_undefined" attribute; arguments as in
+   struct attribute_spec.handler.  */
+
+static tree
+handle_no_sanitize_undefined_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;
+}
+
 /* Handle a "noinline" attribute; arguments as in
    struct attribute_spec.handler.  */
 
index 8b0cc2f517006a1a088bff16a851fa69d6ae96b5..59b71aa0e9d7dbab8272ac693849f787c95b6c9e 100644 (file)
@@ -1,3 +1,9 @@
+2013-09-18  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/58411
+       * c-typeck.c (build_binary_op): Don't sanitize function if it has the
+       no_sanitize_undefined attribute.
+
 2013-09-13  Kai Tietz  <ktietz@redhat.com>
 
        PR target/57848
index e52533ecd6d3b35054e0df9b0571d8d3e28f9c59..7dc5527fc7c63d6fe81e3bb356841f946cf4f98c 100644 (file)
@@ -10498,6 +10498,8 @@ build_binary_op (location_t location, enum tree_code code,
 
   if (flag_sanitize & SANITIZE_UNDEFINED
       && current_function_decl != 0
+      && !lookup_attribute ("no_sanitize_undefined",
+                           DECL_ATTRIBUTES (current_function_decl))
       && (doing_div_or_mod || doing_shift))
     {
       /* OP0 and/or OP1 might have side-effects.  */
index 7e2c13beb5e6273738a2fa1bd05e53d91e3ac5e7..c16d682459da2ac7aeddf2794ce2591322dbde9a 100644 (file)
@@ -1,3 +1,9 @@
+2013-09-18  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/58411
+       * typeck.c (cp_build_binary_op): Don't sanitize function if it has the
+       no_sanitize_undefined attribute.
+
 2013-09-17  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/58435
index 6c48f242dd762eddd5ac80c3b794bed7d482be45..f7d6208022fd73416d2775b27f05ec3426df60c2 100644 (file)
@@ -4887,6 +4887,8 @@ cp_build_binary_op (location_t location,
   if ((flag_sanitize & SANITIZE_UNDEFINED)
       && !processing_template_decl
       && current_function_decl != 0
+      && !lookup_attribute ("no_sanitize_undefined",
+                           DECL_ATTRIBUTES (current_function_decl))
       && (doing_div_or_mod || doing_shift))
     {
       /* OP0 and/or OP1 might have side-effects.  */
index cb0306b72b99414763d914093d67bbd1f13aa05c..1d0dfbe985d4a860df8cbc2e0666d3fb46369bfb 100644 (file)
@@ -2136,6 +2136,7 @@ attributes are currently defined for functions on all targets:
 @code{warn_unused_result}, @code{nonnull}, @code{gnu_inline},
 @code{externally_visible}, @code{hot}, @code{cold}, @code{artificial},
 @code{no_sanitize_address}, @code{no_address_safety_analysis},
+@code{no_sanitize_undefined},
 @code{error} and @code{warning}.
 Several other attributes are defined for functions on particular
 target systems.  Other attributes, including @code{section} are
@@ -3500,6 +3501,12 @@ The @code{no_address_safety_analysis} is a deprecated alias of the
 @code{no_sanitize_address} attribute, new code should use
 @code{no_sanitize_address}.
 
+@item no_sanitize_undefined
+@cindex @code{no_sanitize_undefined} function attribute
+The @code{no_sanitize_undefined} attribute on functions is used
+to inform the compiler that it should not check for undefined behavior
+in the function when compiling with the @option{-fsanitize=undefined} option.
+
 @item regparm (@var{number})
 @cindex @code{regparm} attribute
 @cindex functions that are passed arguments in registers on the 386
index 796e14343c22827ff14dd30a4bb40f70bafc4de8..2b1cad23bc8ec9f0c77374748bddc219332ec707 100644 (file)
@@ -1,3 +1,8 @@
+2013-09-18  Marek Polacek  <polacek@redhat.com>
+
+       PR sanitizer/58411
+       * c-c++-common/ubsan/attrib-1.c: New test.
+
 2013-09-17  Cong Hou  <congh@google.com>
 
        * gcc.dg/vect/vect-reduc-dot-s16c.c: Add a test case with dot product 
diff --git a/gcc/testsuite/c-c++-common/ubsan/attrib-1.c b/gcc/testsuite/c-c++-common/ubsan/attrib-1.c
new file mode 100644 (file)
index 0000000..2e9141c
--- /dev/null
@@ -0,0 +1,33 @@
+/* PR sanitizer/58411 */
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=undefined -w" } */
+
+__attribute__((no_sanitize_undefined)) int
+f1 (int i)
+{
+  return 16 << i;
+}
+
+int f2 (int i);
+int f2 (int i) __attribute__((no_sanitize_undefined));
+int f2 (int i) __attribute__((no_sanitize_undefined));
+int f2 (int i);
+
+int
+f2 (int i)
+{
+  return 1 / i;
+}
+
+void f3 (void);
+__typeof (f3) f3  __attribute__((__no_sanitize_undefined__));
+
+void
+f3 (void)
+{
+  __builtin_unreachable ();
+}
+
+/* { dg-final { scan-assembler-not "__ubsan_handle_shift_out_of_bounds" } } */
+/* { dg-final { scan-assembler-not "__ubsan_handle_divrem_overflow" } } */
+/* { dg-final { scan-assembler-not "__ubsan_handle_builtin_unreachable" } } */