diff options
author | Martin Sebor <msebor@redhat.com> | 2017-12-16 23:58:34 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2017-12-16 16:58:34 -0700 |
commit | cc8bea0aeeeeb6ed8046ede1a577ee681da2ca6a (patch) | |
tree | 8ab48ffcd2c1abce152c9686d64db508a4f40a96 /gcc/gimple-fold.c | |
parent | d43568222a4564e22a6ffd370481e11ba031b318 (diff) | |
download | gcc-cc8bea0aeeeeb6ed8046ede1a577ee681da2ca6a.zip gcc-cc8bea0aeeeeb6ed8046ede1a577ee681da2ca6a.tar.gz gcc-cc8bea0aeeeeb6ed8046ede1a577ee681da2ca6a.tar.bz2 |
PR tree-optimization/78918 - missing -Wrestrict on memcpy copying over self
gcc/c-family/ChangeLog:
PR tree-optimization/78918
* c-common.c (check_function_restrict): Avoid checking built-ins.
* c.opt (-Wrestrict): Include in -Wall.
gcc/ChangeLog:
PR tree-optimization/78918
* Makefile.in (OBJS): Add gimple-ssa-warn-restrict.o.
* builtins.c (check_sizes): Rename...
(check_access): ...to this. Rename function arguments for clarity.
(check_memop_sizes): Adjust names.
(expand_builtin_memchr, expand_builtin_memcpy): Same.
(expand_builtin_memmove, expand_builtin_mempcpy): Same.
(expand_builtin_strcat, expand_builtin_stpncpy): Same.
(check_strncat_sizes, expand_builtin_strncat): Same.
(expand_builtin_strncpy, expand_builtin_memset): Same.
(expand_builtin_bzero, expand_builtin_memcmp): Same.
(expand_builtin_memory_chk, maybe_emit_chk_warning): Same.
(maybe_emit_sprintf_chk_warning): Same.
(expand_builtin_strcpy): Adjust.
(expand_builtin_stpcpy): Same.
(expand_builtin_with_bounds): Detect out-of-bounds accesses
in pointer-checking forms of memcpy, memmove, and mempcpy.
(gcall_to_tree_minimal, max_object_size): Define new functions.
* builtins.h (max_object_size): Declare.
* calls.c (alloc_max_size): Call max_object_size instead of
hardcoding ssizetype limit.
(get_size_range): Handle new argument.
* calls.h (get_size_range): Add a new argument.
* cfgexpand.c (expand_call_stmt): Propagate no-warning bit.
* doc/invoke.texi (-Wrestrict): Adjust, add example.
* gimple-fold.c (gimple_fold_builtin_memory_op): Detect overlapping
operations.
(gimple_fold_builtin_memory_chk): Same.
(gimple_fold_builtin_stxcpy_chk): New function.
* gimple-ssa-warn-restrict.c: New source.
* gimple-ssa-warn-restrict.h: New header.
* gimple.c (gimple_build_call_from_tree): Propagate location.
* passes.def (pass_warn_restrict): Add new pass.
* tree-pass.h (make_pass_warn_restrict): Declare.
* tree-ssa-strlen.c (handle_builtin_strcpy): Detect overlapping
operations.
(handle_builtin_strcat): Same.
(strlen_optimize_stmt): Rename...
(strlen_check_and_optimize_stmt): ...to this. Handle strncat,
stpncpy, strncpy, and their checking forms.
gcc/testsuite/ChangeLog:
PR tree-optimization/78918
* c-c++-common/Warray-bounds.c: New test.
* c-c++-common/Warray-bounds-2.c: New test.
* c-c++-common/Warray-bounds-3.c: New test.
* c-c++-common/Warray-bounds-4.c: New test.
* c-c++-common/Warray-bounds-5.c: New test.
* c-c++-common/Wrestrict-2.c: New test.
* c-c++-common/Wrestrict.c: New test.
* c-c++-common/Wrestrict.s: New test.
* c-c++-common/Wsizeof-pointer-memaccess1.c: Adjust
* c-c++-common/Wsizeof-pointer-memaccess2.c: Same.
* g++.dg/torture/Wsizeof-pointer-memaccess1.C: Same.
* g++.dg/torture/Wsizeof-pointer-memaccess2.C: Same.
* gcc.dg/range.h: New header.
* gcc.dg/memcpy-6.c: New test.
* gcc.dg/pr69172.c: Adjust.
* gcc.dg/pr79223.c: Same.
* gcc.dg/pr81345.c: Adjust.
* gcc.dg/Wobjsize-1.c: Same.
* gcc.dg/Wrestrict-2.c: New test.
* gcc.dg/Wrestrict.c: New test.
* gcc.dg/Wsizeof-pointer-memaccess1.c: Adjust.
* gcc.dg/builtin-stpncpy.c: Same.
* gcc.dg/builtin-stringop-chk-1.c: Same.
* gcc.target/i386/chkp-stropt-17.c: New test.
* gcc.dg/torture/Wsizeof-pointer-memaccess1.c: Adjust.
From-SVN: r255755
Diffstat (limited to 'gcc/gimple-fold.c')
-rw-r--r-- | gcc/gimple-fold.c | 67 |
1 files changed, 58 insertions, 9 deletions
diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index 403fbb8..87ce3d8 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "ssa.h" #include "cgraph.h" #include "gimple-pretty-print.h" +#include "gimple-ssa-warn-restrict.h" #include "fold-const.h" #include "stmt.h" #include "expr.h" @@ -663,13 +664,12 @@ size_must_be_zero_p (tree size) return wi::eq_p (min, wone) && wi::geu_p (max, ssize_max); } -/* Fold function call to builtin mem{{,p}cpy,move}. Return - false if no simplification can be made. - If ENDP is 0, return DEST (like memcpy). - If ENDP is 1, return DEST+LEN (like mempcpy). - If ENDP is 2, return DEST+LEN-1 (like stpcpy). - If ENDP is 3, return DEST, additionally *SRC and *DEST may overlap - (memmove). */ +/* Fold function call to builtin mem{{,p}cpy,move}. Try to detect and + diagnose (otherwise undefined) overlapping copies without preventing + folding. When folded, GCC guarantees that overlapping memcpy has + the same semantics as memmove. Call to the library memcpy need not + provide the same guarantee. Return false if no simplification can + be made. */ static bool gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, @@ -681,6 +681,12 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, tree destvar, srcvar; location_t loc = gimple_location (stmt); + tree func = gimple_call_fndecl (stmt); + bool nowarn = gimple_no_warning_p (stmt); + bool check_overlap = (DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE + && DECL_FUNCTION_CODE (func) != BUILT_IN_MEMMOVE_CHK + && !nowarn); + /* If the LEN parameter is a constant zero or in range where the only valid value is zero, return DEST. */ if (size_must_be_zero_p (len)) @@ -704,6 +710,15 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, DEST{,+LEN,+LEN-1}. */ if (operand_equal_p (src, dest, 0)) { + /* Avoid diagnosing exact overlap in calls to __builtin_memcpy. + It's safe and may even be emitted by GCC itself (see bug + 32667). However, diagnose it in explicit calls to the memcpy + function. */ + if (check_overlap && *IDENTIFIER_POINTER (DECL_NAME (func)) != '_') + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + unlink_stmt_vdef (stmt); if (gimple_vdef (stmt) && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME) release_ssa_name (gimple_vdef (stmt)); @@ -753,6 +768,13 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, unsigned ilen = tree_to_uhwi (len); if (pow2p_hwi (ilen)) { + /* Detect invalid bounds and overlapping copies and issue + either -Warray-bounds or -Wrestrict. */ + if (!nowarn + && check_bounds_or_overlap (as_a <gcall *>(stmt), + dest, src, len, len)) + gimple_set_no_warning (stmt, true); + scalar_int_mode mode; tree type = lang_hooks.types.type_for_size (ilen * 8, 1); if (type @@ -1025,6 +1047,11 @@ gimple_fold_builtin_memory_op (gimple_stmt_iterator *gsi, } } + /* Detect invalid bounds and overlapping copies and issue either + -Warray-bounds or -Wrestrict. */ + if (!nowarn) + check_bounds_or_overlap (as_a <gcall *>(stmt), dest, src, len, len); + gimple *new_stmt; if (is_gimple_reg_type (TREE_TYPE (srcvar))) { @@ -1418,7 +1445,7 @@ get_range_strlen (tree arg, tree length[2], bitmap *visited, int type, tree op3 = gimple_assign_rhs3 (def_stmt); return get_range_strlen (op2, length, visited, type, fuzzy, flexp) && get_range_strlen (op3, length, visited, type, fuzzy, flexp); - } + } return false; case GIMPLE_PHI: @@ -1510,12 +1537,19 @@ static bool gimple_fold_builtin_strcpy (gimple_stmt_iterator *gsi, tree dest, tree src) { - location_t loc = gimple_location (gsi_stmt (*gsi)); + gimple *stmt = gsi_stmt (*gsi); + location_t loc = gimple_location (stmt); tree fn; /* If SRC and DEST are the same (and not volatile), return DEST. */ if (operand_equal_p (src, dest, 0)) { + tree func = gimple_call_fndecl (stmt); + + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + replace_call_with_value (gsi, dest); return true; } @@ -2416,6 +2450,15 @@ gimple_fold_builtin_memory_chk (gimple_stmt_iterator *gsi, (resp. DEST+LEN for __mempcpy_chk). */ if (fcode != BUILT_IN_MEMSET_CHK && operand_equal_p (src, dest, 0)) { + if (fcode != BUILT_IN_MEMMOVE && fcode != BUILT_IN_MEMMOVE_CHK) + { + tree func = gimple_call_fndecl (stmt); + + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + } + if (fcode != BUILT_IN_MEMPCPY_CHK) { replace_call_with_value (gsi, dest); @@ -2517,6 +2560,12 @@ gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterator *gsi, /* If SRC and DEST are the same (and not volatile), return DEST. */ if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) { + tree func = gimple_call_fndecl (stmt); + + warning_at (loc, OPT_Wrestrict, + "%qD source argument is the same as destination", + func); + replace_call_with_value (gsi, dest); return true; } |