This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2] Implement no_sanitize function attribute


On 06/08/2017 03:47 PM, Jakub Jelinek wrote:
Hi!

I'd still prefer to handle it with the flags infrastructure instead, but if
Richard wants to do it this way, then at least:

On Thu, Jun 08, 2017 at 03:30:49PM +0200, Martin Liška wrote:
+/* Return true when flag_sanitize & FLAG is non-zero.  If FN is non-null,
+   remove all flags mentioned in "no_sanitize_flags" of DECL_ATTRIBUTES.  */
+
+bool
+sanitize_flags_p (unsigned int flag, const_tree fn)
+{
+  unsigned int result_flags = flag_sanitize & flag;

This function really should be either inline, or partly inline, partly out
of line, to handle the common case (sanitization of something not enabled)
in the fast path.

Hello.

Having that inlined would be great, however we'll need to put it to tree.h
and thus we have to include "options.h" before tree.h in multiple source files.
Please take a look at partial patch.


And, it should have an early out,
   if (result_flags == 0)
     return false;

Good idea!


+
+  if (fn != NULL_TREE)
+    {
+      tree value = lookup_attribute ("no_sanitize_flags", DECL_ATTRIBUTES (fn));

The attribute, if it is internal only, should have spaces or similar
characters in its name, like "fn spec", "omp declare target" and many
others.

Done that.


+add_no_sanitize_value (tree node, unsigned int flags)
+{
+  tree attr = lookup_attribute ("no_sanitize_flags", DECL_ATTRIBUTES (node));
+  if (attr)
+    {
+      unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
+      flags |= old_value;
+    }
+
+  DECL_ATTRIBUTES (node)
+    = tree_cons (get_identifier ("no_sanitize_flags"),
+		 build_int_cst (unsigned_type_node, flags),
+		 DECL_ATTRIBUTES (node));

If there is a previous attribute already, can't you modify it in
place?  If not, as it could be perhaps shared? with other functions
somehow, at least you should avoid adding a new attribute if
(old_value | flags) == old_value.

Yep, we should definitely share, I'll add test-case for that.
I'm currently testing the incremental patch, may I install it
after regression tests?

Martin


	Jakub


>From f61cd244ef96d1a0dc278790a279b702ca8b63e5 Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Thu, 8 Jun 2017 16:39:33 +0200
Subject: [PATCH] Put it to tree.h header file.

---
 gcc/c-family/c-attribs.c                      | 17 +++++++++++------
 gcc/c-family/c-common.h                       |  1 +
 gcc/c-family/c-pretty-print.h                 |  1 +
 gcc/c-family/c-target-def.h                   |  1 +
 gcc/c-family/stub-objc.c                      |  1 +
 gcc/c/c-lang.c                                |  1 +
 gcc/c/c-objc-common.c                         |  1 +
 gcc/cp/tree.c                                 |  1 +
 gcc/debug.c                                   |  1 +
 gcc/fold-const-call.c                         |  2 +-
 gcc/fortran/convert.c                         |  1 +
 gcc/fortran/iresolve.c                        |  1 +
 gcc/fortran/target-memory.c                   |  1 +
 gcc/fortran/trans-const.c                     |  1 +
 gcc/fortran/trans-io.c                        |  2 +-
 gcc/ggc-tests.c                               |  1 +
 gcc/godump.c                                  |  1 +
 gcc/objc/objc-encoding.c                      |  2 +-
 gcc/objc/objc-map.c                           |  1 +
 gcc/realmpfr.c                                |  1 +
 gcc/selftest-run-tests.c                      |  1 +
 gcc/stringpool.c                              |  1 +
 gcc/substring-locations.c                     |  1 +
 gcc/testsuite/gcc.dg/asan/use-after-scope-4.c |  3 +++
 gcc/tree-diagnostic.c                         |  1 +
 gcc/tree-dump.c                               |  1 +
 gcc/tree-iterator.c                           |  1 +
 gcc/tree-ssa-scopedtables.c                   |  2 +-
 gcc/tree.c                                    | 18 ------------------
 gcc/tree.h                                    | 24 ++++++++++++++++++++----
 30 files changed, 60 insertions(+), 32 deletions(-)

diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c
index abb43d0d02c..98481b034c8 100644
--- a/gcc/c-family/c-attribs.c
+++ b/gcc/c-family/c-attribs.c
@@ -558,17 +558,22 @@ handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
 void
 add_no_sanitize_value (tree node, unsigned int flags)
 {
-  tree attr = lookup_attribute ("no_sanitize_flags", DECL_ATTRIBUTES (node));
+  tree attr = lookup_attribute ("sanitize no_flags", DECL_ATTRIBUTES (node));
   if (attr)
     {
       unsigned int old_value = tree_to_uhwi (TREE_VALUE (attr));
       flags |= old_value;
-    }
 
-  DECL_ATTRIBUTES (node)
-    = tree_cons (get_identifier ("no_sanitize_flags"),
-		 build_int_cst (unsigned_type_node, flags),
-		 DECL_ATTRIBUTES (node));
+      if (flags == old_value)
+	return;
+
+      TREE_VALUE (attr) = build_int_cst (unsigned_type_node, flags);
+    }
+  else
+    DECL_ATTRIBUTES (node)
+      = tree_cons (get_identifier ("sanitize no_flags"),
+		   build_int_cst (unsigned_type_node, flags),
+		   DECL_ATTRIBUTES (node));
 }
 
 /* Handle a "no_sanitize" attribute; arguments as in
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 1748c1979aa..9d19de2d21e 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "splay-tree.h"
 #include "cpplib.h"
 #include "alias.h"
+#include "options.h"
 #include "tree.h"
 #include "fold-const.h"
 
diff --git a/gcc/c-family/c-pretty-print.h b/gcc/c-family/c-pretty-print.h
index 86a4ae32639..38419d69e89 100644
--- a/gcc/c-family/c-pretty-print.h
+++ b/gcc/c-family/c-pretty-print.h
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #ifndef GCC_C_PRETTY_PRINTER
 #define GCC_C_PRETTY_PRINTER
 
+#include "options.h"
 #include "tree.h"
 #include "c-family/c-common.h"
 #include "pretty-print.h"
diff --git a/gcc/c-family/c-target-def.h b/gcc/c-family/c-target-def.h
index 781afbc78fe..cf46961039a 100644
--- a/gcc/c-family/c-target-def.h
+++ b/gcc/c-family/c-target-def.h
@@ -16,6 +16,7 @@
    <http://www.gnu.org/licenses/>.  */
 
 #include "c-family/c-target-hooks-def.h"
+#include "options.h"
 #include "tree.h"
 #include "c-family/c-common.h"
 #include "hooks.h"
diff --git a/gcc/c-family/stub-objc.c b/gcc/c-family/stub-objc.c
index 33dc2a1abd6..58780d17a70 100644
--- a/gcc/c-family/stub-objc.c
+++ b/gcc/c-family/stub-objc.c
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "c-objc.h"
 
diff --git a/gcc/c/c-lang.c b/gcc/c/c-lang.c
index 510b7e7de47..595433935b3 100644
--- a/gcc/c/c-lang.c
+++ b/gcc/c/c-lang.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "c-tree.h"
 #include "langhooks.h"
 #include "langhooks-def.h"
diff --git a/gcc/c/c-objc-common.c b/gcc/c/c-objc-common.c
index 05212b2cb8e..0ca096acba7 100644
--- a/gcc/c/c-objc-common.c
+++ b/gcc/c/c-objc-common.c
@@ -20,6 +20,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "c-tree.h"
 #include "intl.h"
 #include "c-family/c-pretty-print.h"
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index bb17278c611..80067dc5211 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "cp-tree.h"
 #include "gimple-expr.h"
diff --git a/gcc/debug.c b/gcc/debug.c
index 860f1e312b9..3a5f447fe86 100644
--- a/gcc/debug.c
+++ b/gcc/debug.c
@@ -18,6 +18,7 @@
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "debug.h"
 
diff --git a/gcc/fold-const-call.c b/gcc/fold-const-call.c
index 381cb7fd290..a4103f41754 100644
--- a/gcc/fold-const-call.c
+++ b/gcc/fold-const-call.c
@@ -21,9 +21,9 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "realmpfr.h"
+#include "options.h"
 #include "tree.h"
 #include "stor-layout.h"
-#include "options.h"
 #include "fold-const.h"
 #include "fold-const-call.h"
 #include "case-cfn-macros.h"
diff --git a/gcc/fortran/convert.c b/gcc/fortran/convert.c
index 35203235e8f..e3441b02a95 100644
--- a/gcc/fortran/convert.c
+++ b/gcc/fortran/convert.c
@@ -25,6 +25,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "fold-const.h"
 #include "convert.h"
diff --git a/gcc/fortran/iresolve.c b/gcc/fortran/iresolve.c
index b784ac339e9..0565f2fd9ed 100644
--- a/gcc/fortran/iresolve.c
+++ b/gcc/fortran/iresolve.c
@@ -29,6 +29,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "gfortran.h"
 #include "stringpool.h"
diff --git a/gcc/fortran/target-memory.c b/gcc/fortran/target-memory.c
index d239cf114e1..fe5c2f40ccc 100644
--- a/gcc/fortran/target-memory.c
+++ b/gcc/fortran/target-memory.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "gfortran.h"
 #include "trans.h"
diff --git a/gcc/fortran/trans-const.c b/gcc/fortran/trans-const.c
index 128d47d0fa3..ebd10fd24ca 100644
--- a/gcc/fortran/trans-const.c
+++ b/gcc/fortran/trans-const.c
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "gfortran.h"
 #include "trans.h"
diff --git a/gcc/fortran/trans-io.c b/gcc/fortran/trans-io.c
index c3c56f29623..64d068833ed 100644
--- a/gcc/fortran/trans-io.c
+++ b/gcc/fortran/trans-io.c
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "gfortran.h"
 #include "trans.h"
@@ -32,7 +33,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "trans-array.h"
 #include "trans-types.h"
 #include "trans-const.h"
-#include "options.h"
 
 /* Members of the ioparm structure.  */
 
diff --git a/gcc/ggc-tests.c b/gcc/ggc-tests.c
index cbb941d9573..c2dbfb36707 100644
--- a/gcc/ggc-tests.c
+++ b/gcc/ggc-tests.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tree-core.h"
+#include "options.h"
 #include "tree.h"
 #include "ggc-internal.h" /* (for ggc_force_collect).  */
 #include "selftest.h"
diff --git a/gcc/godump.c b/gcc/godump.c
index 4884deead80..c326c7bb6a2 100644
--- a/gcc/godump.c
+++ b/gcc/godump.c
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "wide-int-print.h"
 #include "diagnostic-core.h"
diff --git a/gcc/objc/objc-encoding.c b/gcc/objc/objc-encoding.c
index 2a2dfa51ba5..1ae6f8f992c 100644
--- a/gcc/objc/objc-encoding.c
+++ b/gcc/objc/objc-encoding.c
@@ -20,8 +20,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tree.h"
 #include "options.h"
+#include "tree.h"
 #include "stringpool.h"
 #include "stor-layout.h"
 
diff --git a/gcc/objc/objc-map.c b/gcc/objc/objc-map.c
index 58c902bbb17..0be5dd4a645 100644
--- a/gcc/objc/objc-map.c
+++ b/gcc/objc/objc-map.c
@@ -20,6 +20,7 @@ Boston, MA 02110-1301, USA.  */
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "objc-map.h"
 
diff --git a/gcc/realmpfr.c b/gcc/realmpfr.c
index 69439a4173d..1015958eb44 100644
--- a/gcc/realmpfr.c
+++ b/gcc/realmpfr.c
@@ -20,6 +20,7 @@
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "realmpfr.h"
 #include "stor-layout.h"
diff --git a/gcc/selftest-run-tests.c b/gcc/selftest-run-tests.c
index f62bc72b072..d182d1bf469 100644
--- a/gcc/selftest-run-tests.c
+++ b/gcc/selftest-run-tests.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "selftest.h"
+#include "options.h"
 #include "tree.h"
 #include "target.h"
 #include "langhooks.h"
diff --git a/gcc/stringpool.c b/gcc/stringpool.c
index 689327548e3..e5ec6bc97a5 100644
--- a/gcc/stringpool.c
+++ b/gcc/stringpool.c
@@ -28,6 +28,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 
 /* The "" allocated string.  */
diff --git a/gcc/substring-locations.c b/gcc/substring-locations.c
index 433023d9845..752a981cb17 100644
--- a/gcc/substring-locations.c
+++ b/gcc/substring-locations.c
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "coretypes.h"
 #include "diagnostic.h"
 #include "cpplib.h"
+#include "options.h"
 #include "tree.h"
 #include "langhooks.h"
 #include "substring-locations.h"
diff --git a/gcc/testsuite/gcc.dg/asan/use-after-scope-4.c b/gcc/testsuite/gcc.dg/asan/use-after-scope-4.c
index 781d70d6038..44dc79535d2 100644
--- a/gcc/testsuite/gcc.dg/asan/use-after-scope-4.c
+++ b/gcc/testsuite/gcc.dg/asan/use-after-scope-4.c
@@ -16,6 +16,9 @@ NAME (void) \
 
 void
 __attribute__((no_sanitize(("address"))))
+__attribute__((no_sanitize(("undefined"))))
+__attribute__((no_sanitize(("address"))))
+__attribute__((no_sanitize(("null"))))
 FN (fn1)
 
 void
diff --git a/gcc/tree-diagnostic.c b/gcc/tree-diagnostic.c
index 52b7e7f0bb4..27880ae3e92 100644
--- a/gcc/tree-diagnostic.c
+++ b/gcc/tree-diagnostic.c
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "diagnostic.h"
 #include "tree-pretty-print.h"
diff --git a/gcc/tree-dump.c b/gcc/tree-dump.c
index 347b33ab505..487eaf5e832 100644
--- a/gcc/tree-dump.c
+++ b/gcc/tree-dump.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "tree-pretty-print.h"
 #include "tree-dump.h"
diff --git a/gcc/tree-iterator.c b/gcc/tree-iterator.c
index c485413b5e5..b04e0a4c577 100644
--- a/gcc/tree-iterator.c
+++ b/gcc/tree-iterator.c
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "options.h"
 #include "tree.h"
 #include "tree-iterator.h"
 
diff --git a/gcc/tree-ssa-scopedtables.c b/gcc/tree-ssa-scopedtables.c
index 814f1c7b05b..3656268aa47 100644
--- a/gcc/tree-ssa-scopedtables.c
+++ b/gcc/tree-ssa-scopedtables.c
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "coretypes.h"
 #include "function.h"
 #include "basic-block.h"
+#include "options.h"
 #include "tree.h"
 #include "gimple.h"
 #include "tree-pass.h"
@@ -33,7 +34,6 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-eh.h"
 #include "internal-fn.h"
 #include "tree-dfa.h"
-#include "options.h"
 #include "params.h"
 
 static bool hashable_expr_equal_p (const struct hashable_expr *,
diff --git a/gcc/tree.c b/gcc/tree.c
index 8979819adf7..a58f9aaa69e 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -14442,24 +14442,6 @@ nonnull_arg_p (const_tree arg)
   return false;
 }
 
-/* Return true when flag_sanitize & FLAG is non-zero.  If FN is non-null,
-   remove all flags mentioned in "no_sanitize_flags" of DECL_ATTRIBUTES.  */
-
-bool
-sanitize_flags_p (unsigned int flag, const_tree fn)
-{
-  unsigned int result_flags = flag_sanitize & flag;
-
-  if (fn != NULL_TREE)
-    {
-      tree value = lookup_attribute ("no_sanitize_flags", DECL_ATTRIBUTES (fn));
-      if (value)
-	result_flags &= ~tree_to_uhwi (TREE_VALUE (value));
-    }
-
-  return result_flags;
-}
-
 /* Combine LOC and BLOCK to a combined adhoc loc, retaining any range
    information.  */
 
diff --git a/gcc/tree.h b/gcc/tree.h
index 22b9ec3f0e7..061ea0dd97c 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -4248,10 +4248,6 @@ extern tree merge_dllimport_decl_attributes (tree, tree);
 /* Handle a "dllimport" or "dllexport" attribute.  */
 extern tree handle_dll_attribute (tree *, tree, tree, int, bool *);
 
-
-extern bool sanitize_flags_p (unsigned int flag,
-			      const_tree fn = current_function_decl);
-
 /* Returns true iff CAND and BASE have equivalent language-specific
    qualifiers.  */
 
@@ -5513,4 +5509,24 @@ desired_pro_or_demotion_p (const_tree to_type, const_tree from_type)
   return to_type_precision <= TYPE_PRECISION (from_type);
 }
 
+/* Return true when flag_sanitize & FLAG is non-zero.  If FN is non-null,
+   remove all flags mentioned in "sanitize no_flags" of DECL_ATTRIBUTES.  */
+
+static inline bool
+sanitize_flags_p (unsigned int flag, const_tree fn = current_function_decl)
+{
+  unsigned int result_flags = flag_sanitize & flag;
+  if (result_flags == 0)
+    return false;
+
+  if (fn != NULL_TREE)
+    {
+      tree value = lookup_attribute ("sanitize no_flags", DECL_ATTRIBUTES (fn));
+      if (value)
+	result_flags &= ~tree_to_uhwi (TREE_VALUE (value));
+    }
+
+  return result_flags;
+}
+
 #endif  /* GCC_TREE_H  */
-- 
2.13.0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]