summaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authormpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2014-06-30 13:15:20 +0000
committermpolacek <mpolacek@138bc75d-0d04-0410-961f-82ee72b054a4>2014-06-30 13:15:20 +0000
commit7057506456ba18f080679b2fe55ec56ee90fd81c (patch)
treed9fbba8c55ca2e01e66ea39234d507306dac9bed /gcc
parent31dca860933170b72971877a25943d799ccdfc5c (diff)
downloadgcc-7057506456ba18f080679b2fe55ec56ee90fd81c.tar.gz
* convert.c (convert_to_integer): Don't instrument conversions if the
function has no_sanitize_undefined attribute. * ubsan.c: Don't run the ubsan pass if the function has no_sanitize_undefined attribute. c/ * c-decl.c (grokdeclarator): Don't instrument VLAs if the function has no_sanitize_undefined attribute. cp/ * cp-gimplify.c (cp_genericize): Don't instrument returns if the function has no_sanitize_undefined attribute. * decl.c (compute_array_index_type): Don't instrument VLAs if the function has no_sanitize_undefined attribute. testsuite/ * c-c++-common/ubsan/attrib-2.c: New test. * g++.dg/ubsan/return-3.C: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@212148 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog7
-rw-r--r--gcc/c/ChangeLog5
-rw-r--r--gcc/c/c-decl.c6
-rw-r--r--gcc/convert.c5
-rw-r--r--gcc/cp/ChangeLog7
-rw-r--r--gcc/cp/cp-gimplify.c7
-rw-r--r--gcc/cp/decl.c6
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/c-c++-common/ubsan/attrib-2.c71
-rw-r--r--gcc/testsuite/g++.dg/ubsan/return-3.C27
-rw-r--r--gcc/ubsan.c5
11 files changed, 145 insertions, 6 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index bae36686dbe..70f80885dcd 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@
+2014-06-30 Marek Polacek <polacek@redhat.com>
+
+ * convert.c (convert_to_integer): Don't instrument conversions if the
+ function has no_sanitize_undefined attribute.
+ * ubsan.c: Don't run the ubsan pass if the function has
+ no_sanitize_undefined attribute.
+
2014-06-30 Jakub Jelinek <jakub@redhat.com>
* doc/invoke.texi (-fsanitize=bounds): Move to the table with
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index 02a0f2f676a..6740d513d66 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,8 @@
+2014-06-30 Marek Polacek <polacek@redhat.com>
+
+ * c-decl.c (grokdeclarator): Don't instrument VLAs if the function
+ has no_sanitize_undefined attribute.
+
2014-06-30 Igor Zamyatin <igor.zamyatin@intel.com>
PR middle-end/57541
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index def10a2e2de..7c37edfd372 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -5505,7 +5505,11 @@ grokdeclarator (const struct c_declarator *declarator,
this_size_varies = size_varies = true;
warn_variable_length_array (name, size);
if (flag_sanitize & SANITIZE_VLA
- && decl_context == NORMAL)
+ && decl_context == NORMAL
+ && current_function_decl != NULL_TREE
+ && !lookup_attribute ("no_sanitize_undefined",
+ DECL_ATTRIBUTES
+ (current_function_decl)))
{
/* Evaluate the array size only once. */
size = c_save_expr (size);
diff --git a/gcc/convert.c b/gcc/convert.c
index 2d9600dd853..09bc555ee0e 100644
--- a/gcc/convert.c
+++ b/gcc/convert.c
@@ -847,7 +847,10 @@ convert_to_integer (tree type, tree expr)
return build1 (CONVERT_EXPR, type, expr);
case REAL_TYPE:
- if (flag_sanitize & SANITIZE_FLOAT_CAST)
+ if (flag_sanitize & SANITIZE_FLOAT_CAST
+ && current_function_decl != NULL_TREE
+ && !lookup_attribute ("no_sanitize_undefined",
+ DECL_ATTRIBUTES (current_function_decl)))
{
expr = save_expr (expr);
tree check = ubsan_instrument_float_cast (loc, type, expr);
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index edf79e7d526..6d5b43f193d 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,10 @@
+2014-06-30 Marek Polacek <polacek@redhat.com>
+
+ * cp-gimplify.c (cp_genericize): Don't instrument returns if the
+ function has no_sanitize_undefined attribute.
+ * decl.c (compute_array_index_type): Don't instrument VLAs if the
+ function has no_sanitize_undefined attribute.
+
2014-06-30 Igor Zamyatin <igor.zamyatin@intel.com>
PR middle-end/57541
diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 296bd5f2443..a35177bdbda 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -1221,7 +1221,7 @@ cp_genericize_tree (tree* t_p)
/* If a function that should end with a return in non-void
function doesn't obviously end with return, add ubsan
- instrmentation code to verify it at runtime. */
+ instrumentation code to verify it at runtime. */
static void
cp_ubsan_maybe_instrument_return (tree fndecl)
@@ -1334,7 +1334,10 @@ cp_genericize (tree fndecl)
walk_tree's hash functionality. */
cp_genericize_tree (&DECL_SAVED_TREE (fndecl));
- if (flag_sanitize & SANITIZE_RETURN)
+ if (flag_sanitize & SANITIZE_RETURN
+ && current_function_decl != NULL_TREE
+ && !lookup_attribute ("no_sanitize_undefined",
+ DECL_ATTRIBUTES (current_function_decl)))
cp_ubsan_maybe_instrument_return (fndecl);
/* Do everything else. */
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index d548f61fa51..6902bb053e2 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -8469,7 +8469,11 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
throw_bad_array_length (), void_node);
finish_expr_stmt (comp);
}
- else if (flag_sanitize & SANITIZE_VLA)
+ else if (flag_sanitize & SANITIZE_VLA
+ && current_function_decl != NULL_TREE
+ && !lookup_attribute ("no_sanitize_undefined",
+ DECL_ATTRIBUTES
+ (current_function_decl)))
{
/* From C++1y onwards, we throw an exception on a negative
length size of an array; see above. */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index c2ed2736f15..4191a9728cf 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2014-06-30 Marek Polacek <polacek@redhat.com>
+
+ * c-c++-common/ubsan/attrib-2.c: New test.
+ * g++.dg/ubsan/return-3.C: New test.
+
2014-06-30 Igor Zamyatin <igor.zamyatin@intel.com>
PR middle-end/57541
diff --git a/gcc/testsuite/c-c++-common/ubsan/attrib-2.c b/gcc/testsuite/c-c++-common/ubsan/attrib-2.c
new file mode 100644
index 00000000000..71f2e58ea67
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/ubsan/attrib-2.c
@@ -0,0 +1,71 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=undefined -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow" } */
+
+/* Test that we don't instrument functions marked with
+ no_sanitize_undefined attribute. */
+
+#ifndef __cplusplus
+#define bool _Bool
+#endif
+enum A { B = -3, C = 2 } a;
+bool b;
+
+__attribute__((no_sanitize_undefined))
+static void
+vla_bound (void)
+{
+ int i = -1;
+ volatile int a[i];
+}
+
+__attribute__((no_sanitize_undefined))
+static void
+si_overflow (void)
+{
+ int x = 123, y = 267;
+ volatile int z1 = x + y;
+ volatile int z2 = x - y;
+ volatile int z3 = x * y;
+ volatile int z4 = x / y;
+}
+
+__attribute__((no_sanitize_undefined))
+static void
+null (int *p)
+{
+ *p = 42;
+}
+
+__attribute__((no_sanitize_undefined))
+static void
+retrn (int *p)
+{
+ *p = 42;
+}
+
+__attribute__((no_sanitize_undefined))
+static enum A
+bool_enum (bool *p)
+{
+ *p = b;
+ return a;
+}
+
+__attribute__((no_sanitize_undefined))
+static void
+float_zero (void)
+{
+ volatile float a = 4.2f, b = 0.0f, c;
+ c = a / b;
+}
+
+__attribute__((no_sanitize_undefined))
+static void
+float_cast (void)
+{
+ volatile double d = 300;
+ volatile signed char c;
+ c = d;
+}
+
+/* { dg-final { scan-assembler-not "__ubsan_handle" } } */
diff --git a/gcc/testsuite/g++.dg/ubsan/return-3.C b/gcc/testsuite/g++.dg/ubsan/return-3.C
new file mode 100644
index 00000000000..95f345deb32
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ubsan/return-3.C
@@ -0,0 +1,27 @@
+// { dg-do compile }
+// { dg-options "-fsanitize=return" }
+
+struct S { S (); ~S (); };
+
+S::S () {}
+S::~S () {}
+
+__attribute__((no_sanitize_undefined))
+int
+foo (int x)
+{
+ S a;
+ {
+ S b;
+ if (x)
+ return 1;
+ }
+}
+
+int
+main ()
+{
+ foo (0);
+}
+
+// { dg-final { scan-assembler-not "__ubsan_handle" } }
diff --git a/gcc/ubsan.c b/gcc/ubsan.c
index 5e1c3e7c9ed..11d2a889cc8 100644
--- a/gcc/ubsan.c
+++ b/gcc/ubsan.c
@@ -1148,7 +1148,10 @@ public:
virtual bool gate (function *)
{
return flag_sanitize & (SANITIZE_NULL | SANITIZE_SI_OVERFLOW
- | SANITIZE_BOOL | SANITIZE_ENUM);
+ | SANITIZE_BOOL | SANITIZE_ENUM)
+ && current_function_decl != NULL_TREE
+ && !lookup_attribute ("no_sanitize_undefined",
+ DECL_ATTRIBUTES (current_function_decl));
}
virtual unsigned int execute (function *);