diff mbox

Request to merge Undefined Behavior Sanitizer in (take 3)

Message ID 20130820083550.GB4968@redhat.com
State New
Headers show

Commit Message

Marek Polacek Aug. 20, 2013, 8:35 a.m. UTC
On Thu, Aug 15, 2013 at 05:08:48PM +0200, Marek Polacek wrote:
> One patch is not in yet, but that isn't anything important:
>    http://gcc.gnu.org/ml/gcc-patches/2013-08/msg00376.html

I've now commited this patch:

	Marek
diff mbox

Patch

diff --git a/gcc/ChangeLog.ubsan b/gcc/ChangeLog.ubsan
index 9040b68..695522e 100644
--- a/gcc/ChangeLog.ubsan
+++ b/gcc/ChangeLog.ubsan
@@ -1,3 +1,8 @@ 
+2013-08-20  Marek Polacek  <polacek@redhat.com>
+
+	* ubsan.c (is_ubsan_builtin_p): New function.
+	* ubsan.h: Declare it.
+
 2013-08-05  Marek Polacek  <polacek@redhat.com>
 
 	* ubsan.c (ubsan_source_location_type): Properly create
diff --git a/gcc/cp/ChangeLog.ubsan b/gcc/cp/ChangeLog.ubsan
index f37ce94..5674fc0 100644
--- a/gcc/cp/ChangeLog.ubsan
+++ b/gcc/cp/ChangeLog.ubsan
@@ -1,3 +1,7 @@ 
+2013-08-20  Marek Polacek  <polacek@redhat.com>
+
+	* error.c (dump_expr): Special-case ubsan builtins.
+
 2013-07-30  Marek Polacek  <polacek@redhat.com>
 
 	* typeck.c (cp_build_binary_op): Sanitize only when
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index 440169a..db50b5f 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -32,6 +32,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "tree-pretty-print.h"
 #include "pointer-set.h"
 #include "c-family/c-objc.h"
+#include "ubsan.h"
 
 #define pp_separate_with_comma(PP) pp_cxx_separate_with (PP, ',')
 #define pp_separate_with_semicolon(PP) pp_cxx_separate_with (PP, ';')
@@ -1972,6 +1973,12 @@  dump_expr (tree t, int flags)
 	      }
 	    skipfirst = true;
 	  }
+	if (flag_sanitize & SANITIZE_UNDEFINED
+	    && is_ubsan_builtin_p (fn))
+	  {
+	    pp_string (cxx_pp, M_("<ubsan routine call>"));
+	    break;
+	  }
 	dump_expr (fn, flags | TFF_EXPR_IN_PARENS);
 	dump_call_expr_args (t, flags, skipfirst);
       }
diff --git a/gcc/testsuite/ChangeLog.ubsan b/gcc/testsuite/ChangeLog.ubsan
index 2a62e05..9ba895e 100644
--- a/gcc/testsuite/ChangeLog.ubsan
+++ b/gcc/testsuite/ChangeLog.ubsan
@@ -1,3 +1,7 @@ 
+2013-08-20  Marek Polacek  <polacek@redhat.com>
+
+	* g++.dg/ubsan/div-by-zero-1.C: New test.
+
 2013-08-15  Marek Polacek  <polacek@redhat.com>
 
 	* c-c++-common/ubsan/save-expr-1.c: New test.
diff --git a/gcc/testsuite/g++.dg/ubsan/div-by-zero-1.C b/gcc/testsuite/g++.dg/ubsan/div-by-zero-1.C
new file mode 100644
index 0000000..d7d2c8f1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ubsan/div-by-zero-1.C
@@ -0,0 +1,10 @@ 
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=shift -w" } */
+
+void
+foo (int i)
+{
+  switch (i)
+  case 0 * (1 / 0): /* { dg-error "is not a constant expression" } */
+    ;
+}
diff --git a/gcc/ubsan.c b/gcc/ubsan.c
index 8135cc9..565758d 100644
--- a/gcc/ubsan.c
+++ b/gcc/ubsan.c
@@ -456,3 +456,13 @@  ubsan_instrument_unreachable (location_t loc)
   tree t = builtin_decl_explicit (BUILT_IN_UBSAN_HANDLE_BUILTIN_UNREACHABLE);
   return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data));
 }
+
+/* Return true if T is a call to a libubsan routine.  */
+
+bool
+is_ubsan_builtin_p (tree t)
+{
+  gcc_checking_assert (TREE_CODE (t) == FUNCTION_DECL);
+  return strncmp (IDENTIFIER_POINTER (DECL_NAME (t)),
+		  "__builtin___ubsan_", 18) == 0;
+}
diff --git a/gcc/ubsan.h b/gcc/ubsan.h
index abf4f5d..3553a6c 100644
--- a/gcc/ubsan.h
+++ b/gcc/ubsan.h
@@ -25,6 +25,7 @@  extern tree ubsan_instrument_unreachable (location_t);
 extern tree ubsan_create_data (const char *, location_t, ...);
 extern tree ubsan_type_descriptor (tree);
 extern tree ubsan_encode_value (tree);
+extern bool is_ubsan_builtin_p (tree);
 
 #endif  /* GCC_UBSAN_H  */