From ca1150f0129abd2b0b52ad0c701a6bd7e0a1fc76 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Mon, 4 Sep 2017 10:11:44 +0200 Subject: [PATCH] re PR sanitizer/81981 (-fsanitize=undefined makes a -Wmaybe-uninitialized warning disappear) PR sanitizer/81981 * gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR and UBSAN_BOUNDS internal calls. Clean up IFN_UBSAN_OBJECT_SIZE handling. Use replace_call_with_value with NULL instead of gsi_replace, unlink_stmt_vdef and release_defs. * gcc.dg/ubsan/pr81981.c: New test. From-SVN: r251641 --- gcc/ChangeLog | 6 ++++ gcc/gimple-fold.c | 41 ++++++++++++++++++++++------ gcc/testsuite/ChangeLog | 3 ++ gcc/testsuite/gcc.dg/ubsan/pr81981.c | 21 ++++++++++++++ 4 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/ubsan/pr81981.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4d1c041acaa..50d5e26f96a 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,11 @@ 2017-09-04 Jakub Jelinek + PR sanitizer/81981 + * gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR + and UBSAN_BOUNDS internal calls. Clean up IFN_UBSAN_OBJECT_SIZE + handling. Use replace_call_with_value with NULL instead of + gsi_replace, unlink_stmt_vdef and release_defs. + * gdbhooks.py (OptMachineModePrinter.to_string): Use 8 spaces instead of tab. diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index 367b35c5c9b..8366e4b50b8 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -3936,18 +3936,43 @@ gimple_fold_call (gimple_stmt_iterator *gsi, bool inplace) gimple_call_arg (stmt, 2)); break; case IFN_UBSAN_OBJECT_SIZE: - if (integer_all_onesp (gimple_call_arg (stmt, 2)) - || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST - && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST - && tree_int_cst_le (gimple_call_arg (stmt, 1), - gimple_call_arg (stmt, 2)))) + { + tree offset = gimple_call_arg (stmt, 1); + tree objsize = gimple_call_arg (stmt, 2); + if (integer_all_onesp (objsize) + || (TREE_CODE (offset) == INTEGER_CST + && TREE_CODE (objsize) == INTEGER_CST + && tree_int_cst_le (offset, objsize))) + { + replace_call_with_value (gsi, NULL_TREE); + return true; + } + } + break; + case IFN_UBSAN_PTR: + if (integer_zerop (gimple_call_arg (stmt, 1))) { - gsi_replace (gsi, gimple_build_nop (), false); - unlink_stmt_vdef (stmt); - release_defs (stmt); + replace_call_with_value (gsi, NULL_TREE); return true; } break; + case IFN_UBSAN_BOUNDS: + { + tree index = gimple_call_arg (stmt, 1); + tree bound = gimple_call_arg (stmt, 2); + if (TREE_CODE (index) == INTEGER_CST + && TREE_CODE (bound) == INTEGER_CST) + { + index = fold_convert (TREE_TYPE (bound), index); + if (TREE_CODE (index) == INTEGER_CST + && tree_int_cst_le (index, bound)) + { + replace_call_with_value (gsi, NULL_TREE); + return true; + } + } + } + break; case IFN_GOACC_DIM_SIZE: case IFN_GOACC_DIM_POS: result = fold_internal_goacc_dim (stmt); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 38294c6e9f4..cd7ac1d84e7 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2017-09-04 Jakub Jelinek + PR sanitizer/81981 + * gcc.dg/ubsan/pr81981.c: New test. + PR tree-optimization/70043 PR testsuite/82093 * gfortran.dg/vect/pr70043.f90 (fn1): Start loop from 1 instead of 0. diff --git a/gcc/testsuite/gcc.dg/ubsan/pr81981.c b/gcc/testsuite/gcc.dg/ubsan/pr81981.c new file mode 100644 index 00000000000..b2636d4c934 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/pr81981.c @@ -0,0 +1,21 @@ +/* PR sanitizer/81981 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wmaybe-uninitialized -fsanitize=undefined -ffat-lto-objects" } */ + +int v; + +int +foo (int i) +{ + int t[1], u[1]; + int n = 0; + + if (i) + { + t[n] = i; + u[0] = i; + } + + v = u[0]; /* { dg-warning "may be used uninitialized in this function" } */ + return t[0]; /* { dg-warning "may be used uninitialized in this function" } */ +} -- 2.30.2