Do UBSAN sanitization just when current_function_decl != NULL_TREE (PR sanitize/81530).
authorMartin Liska <mliska@suse.cz>
Mon, 31 Jul 2017 08:53:00 +0000 (10:53 +0200)
committerMartin Liska <marxin@gcc.gnu.org>
Mon, 31 Jul 2017 08:53:00 +0000 (08:53 +0000)
2017-07-31  Martin Liska  <mliska@suse.cz>

PR sanitize/81530
* cp-gimplify.c (cp_genericize): Guard condition with flag_sanitize_p
also with current_function_decl non-null equality.
* cp-ubsan.c (cp_ubsan_instrument_vptr_p): Likewise.
* decl.c (compute_array_index_type): Likewise.
* init.c (finish_length_check): Likewise.
* typeck.c (cp_build_binary_op): Likewise.
2017-07-31  Martin Liska  <mliska@suse.cz>

PR sanitize/81530
* c-convert.c (convert): Guard condition with flag_sanitize_p
also with current_function_decl non-null equality.
* c-decl.c (grokdeclarator): Likewise.
* c-typeck.c (build_binary_op): Likewise.
2017-07-31  Martin Liska  <mliska@suse.cz>

PR sanitize/81530
* convert.c (convert_to_integer_1): Guard condition with flag_sanitize_p
also with current_function_decl non-null equality.
2017-07-31  Martin Liska  <mliska@suse.cz>

PR sanitize/81530
* c-ubsan.c (ubsan_maybe_instrument_array_ref):
Guard condition with flag_sanitize_p also with current_function_decl
non-null equality.
(ubsan_maybe_instrument_reference_or_call): Likewise.
2017-07-31  Martin Liska  <mliska@suse.cz>

PR sanitize/81530
* g++.dg/ubsan/pr81530.C: New test.

From-SVN: r250730

16 files changed:
gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-ubsan.c
gcc/c/ChangeLog
gcc/c/c-convert.c
gcc/c/c-decl.c
gcc/c/c-typeck.c
gcc/convert.c
gcc/cp/ChangeLog
gcc/cp/cp-gimplify.c
gcc/cp/cp-ubsan.c
gcc/cp/decl.c
gcc/cp/init.c
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ubsan/pr81530.C [new file with mode: 0644]

index 176847911b3660f522491ee4dffa27de9d0fbeee..7da2b65fdc2b9b2be5c8a135122a21aa8ee64ade 100644 (file)
@@ -1,3 +1,9 @@
+2017-07-31  Martin Liska  <mliska@suse.cz>
+
+       PR sanitize/81530
+       * convert.c (convert_to_integer_1): Guard condition with flag_sanitize_p
+       also with current_function_decl non-null equality.
+
 2017-07-31  Jakub Jelinek  <jakub@redhat.com>
 
        PR sanitizer/81604
index a9cb761c51731401df4aa82f6dda21ca742ae2e4..1b6d7cfb903eea16f9c1bc890748e40255f0aea7 100644 (file)
@@ -1,3 +1,11 @@
+2017-07-31  Martin Liska  <mliska@suse.cz>
+
+       PR sanitize/81530
+       * c-ubsan.c (ubsan_maybe_instrument_array_ref):
+       Guard condition with flag_sanitize_p also with current_function_decl
+       non-null equality.
+       (ubsan_maybe_instrument_reference_or_call): Likewise.
+
 2017-07-30  Uros Bizjak  <ubizjak@gmail.com>
 
        * c-format.c (asm_fprintf_char_table): Add 'z' to format_chars.
index a072d19eda6b6a6908e635b3d322b42a2ed96924..541b53009c2bb64c76513dcae3e619a6e055bb40 100644 (file)
@@ -373,7 +373,8 @@ void
 ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one)
 {
   if (!ubsan_array_ref_instrumented_p (*expr_p)
-      && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT))
+      && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT)
+      && current_function_decl != NULL_TREE)
     {
       tree op0 = TREE_OPERAND (*expr_p, 0);
       tree op1 = TREE_OPERAND (*expr_p, 1);
@@ -393,7 +394,8 @@ static tree
 ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype,
                                          enum ubsan_null_ckind ckind)
 {
-  if (!sanitize_flags_p (SANITIZE_ALIGNMENT | SANITIZE_NULL))
+  if (!sanitize_flags_p (SANITIZE_ALIGNMENT | SANITIZE_NULL)
+      || current_function_decl == NULL_TREE)
     return NULL_TREE;
 
   tree type = TREE_TYPE (ptype);
index 4ce5360c0c1153fa9e094561666f65833e20f254..ab2a4c88ceebb502d9580a4fdfbac0e870ba9984 100644 (file)
@@ -1,3 +1,11 @@
+2017-07-31  Martin Liska  <mliska@suse.cz>
+
+       PR sanitize/81530
+       * c-convert.c (convert): Guard condition with flag_sanitize_p
+       also with current_function_decl non-null equality.
+       * c-decl.c (grokdeclarator): Likewise.
+       * c-typeck.c (build_binary_op): Likewise.
+
 2017-07-25  Marek Polacek  <polacek@redhat.com>
 
        * c-decl.c (grokfield): Remove local variable.
index 33c9143e354659eb6a8943974d22b325efc86100..bc649178f4c5d60eacf696fc3224e3808bf6a0ab 100644 (file)
@@ -108,6 +108,7 @@ convert (tree type, tree expr)
     case INTEGER_TYPE:
     case ENUMERAL_TYPE:
       if (sanitize_flags_p (SANITIZE_FLOAT_CAST)
+         && current_function_decl != NULL_TREE
          && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
          && COMPLETE_TYPE_P (type))
        {
index 12fbc18bb945b625335e16dd14cf84c3f08b1685..a54e12184342289890c613118dc7c02fefce6a76 100644 (file)
@@ -6052,6 +6052,7 @@ grokdeclarator (const struct c_declarator *declarator,
                    this_size_varies = size_varies = true;
                    warn_variable_length_array (name, size);
                    if (sanitize_flags_p (SANITIZE_VLA)
+                       && current_function_decl != NULL_TREE
                        && decl_context == NORMAL)
                      {
                        /* Evaluate the array size only once.  */
index 4d067e96dd3976bd22bce3731336bb5d1ba92b60..7451f3249fd1ac6cb030ce845f85acf9cc2eaef3 100644 (file)
@@ -11838,6 +11838,7 @@ build_binary_op (location_t location, enum tree_code code,
 
   if (sanitize_flags_p ((SANITIZE_SHIFT
                         | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
+      && current_function_decl != NULL_TREE
       && (doing_div_or_mod || doing_shift)
       && !require_constant_value)
     {
index 429f988cbde24f7ed8f3790967f6ee2f48b2afe1..58d8054a724cfa690a69047c0ff3878820c09cac 100644 (file)
@@ -938,7 +938,8 @@ convert_to_integer_1 (tree type, tree expr, bool dofold)
       return build1 (CONVERT_EXPR, type, expr);
 
     case REAL_TYPE:
-      if (sanitize_flags_p (SANITIZE_FLOAT_CAST))
+      if (sanitize_flags_p (SANITIZE_FLOAT_CAST)
+         && current_function_decl != NULL_TREE)
        {
          expr = save_expr (expr);
          tree check = ubsan_instrument_float_cast (loc, type, expr);
index 3d988a67b7757075b3a8fadffd76bc46308eeafb..6d9ff79c974981697fb92470b318a9fd10a451d9 100644 (file)
@@ -1,3 +1,13 @@
+2017-07-31  Martin Liska  <mliska@suse.cz>
+
+       PR sanitize/81530
+       * cp-gimplify.c (cp_genericize): Guard condition with flag_sanitize_p
+       also with current_function_decl non-null equality.
+       * cp-ubsan.c (cp_ubsan_instrument_vptr_p): Likewise.
+       * decl.c (compute_array_index_type): Likewise.
+       * init.c (finish_length_check): Likewise.
+       * typeck.c (cp_build_binary_op): Likewise.
+
 2017-07-29  Jakub Jelinek  <jakub@redhat.com>
 
        * cp-objcp-common.c (cp_decl_dwarf_attribute): Handle
index f010f6c63be10fbf08bda6a56ef83dbda72a3413..a9563b1a8cdff4dba169464290487bd0c67d3073 100644 (file)
@@ -1668,7 +1668,8 @@ cp_genericize (tree fndecl)
      walk_tree's hash functionality.  */
   cp_genericize_tree (&DECL_SAVED_TREE (fndecl), true);
 
-  if (sanitize_flags_p (SANITIZE_RETURN))
+  if (sanitize_flags_p (SANITIZE_RETURN)
+      && current_function_decl != NULL_TREE)
     cp_ubsan_maybe_instrument_return (fndecl);
 
   /* Do everything else.  */
index f00f870bd3ef94c4ef4e217b40ac24296c2ac2e7..3be607c0a42edaa0efd00c26232a271b2a6f8ef8 100644 (file)
@@ -36,6 +36,9 @@ cp_ubsan_instrument_vptr_p (tree type)
   if (!sanitize_flags_p (SANITIZE_VPTR))
     return false;
 
+  if (current_function_decl == NULL_TREE)
+    return false;
+
   if (type)
     {
       type = TYPE_MAIN_VARIANT (type);
index d98fab370d716001bc1d9b3a41a7e314df904c14..4ec38b82aa98f640a14bd7eccaa348e4011955d5 100644 (file)
@@ -9482,7 +9482,8 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
 
          stabilize_vla_size (itype);
 
-         if (sanitize_flags_p (SANITIZE_VLA))
+         if (sanitize_flags_p (SANITIZE_VLA)
+             && current_function_decl != NULL_TREE)
            {
              /* We have to add 1 -- in the ubsan routine we generate
                 LE_EXPR rather than LT_EXPR.  */
index 14335388a50d78b70cb78ab2c30d2945043d0b9d..3fe8f18b2a944cf3a827d1a0daea54fc31453228 100644 (file)
@@ -3910,7 +3910,8 @@ finish_length_check (tree atype, tree iterator, tree obase, unsigned n)
        }
       /* Don't check an array new when -fno-exceptions.  */
     }
-  else if (sanitize_flags_p (SANITIZE_BOUNDS))
+  else if (sanitize_flags_p (SANITIZE_BOUNDS)
+          && current_function_decl != NULL_TREE)
     {
       /* Make sure the last element of the initializer is in bounds. */
       finish_expr_stmt
index 316d57fb38c4d6df7be9cc805506e163d82b3010..3dc64045e1a7b44f73f3de8277f25ee67aec3f25 100644 (file)
@@ -5256,6 +5256,7 @@ cp_build_binary_op (location_t location,
 
   if (sanitize_flags_p ((SANITIZE_SHIFT
                         | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
+      && current_function_decl != NULL_TREE
       && !processing_template_decl
       && (doing_div_or_mod || doing_shift))
     {
index e84a715c22a05a361c93df102093f1d7218d273b..f0ef3ade89963c67d5db6eae2bfa98b0975357b1 100644 (file)
@@ -1,3 +1,8 @@
+2017-07-31  Martin Liska  <mliska@suse.cz>
+
+       PR sanitize/81530
+       * g++.dg/ubsan/pr81530.C: New test.
+
 2017-07-31  Jakub Jelinek  <jakub@redhat.com>
 
        PR sanitizer/81604
diff --git a/gcc/testsuite/g++.dg/ubsan/pr81530.C b/gcc/testsuite/g++.dg/ubsan/pr81530.C
new file mode 100644 (file)
index 0000000..e217246
--- /dev/null
@@ -0,0 +1,6 @@
+/* PR sanitizer/81530 */
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=undefined" } */
+
+int a[(long) 4e20]; /* { dg-error "overflow in constant expression" } */
+/* { dg-error "size of array .a. is too large" "" { target *-*-* } .-1 } */