Message ID | 20170901202816.GR2323@tucnak |
---|---|
State | New |
Headers | show |
Series | Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981, take 2) | expand |
On September 1, 2017 10:28:16 PM GMT+02:00, Jakub Jelinek <jakub@redhat.com> wrote: >On Fri, Sep 01, 2017 at 07:10:51PM +0200, Richard Biener wrote: >> OK, I thought we have one. Can you add a helper for it please? >> replace_with_nop or so? I thought there's maybe replace_with_value >which >> handles null lhs by replacing with nop. (can't check, writing from >phone) > >Actually, you're right, replace_call_with_value does the right thing >when called on call without lhs (all these internal fns don't have >lhs), >and NULL_TREE val ensures we'd ICE if that ever wasn't the case. > >Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? OK. Richard. >2017-09-01 Jakub Jelinek <jakub@redhat.com> > > 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. > >--- gcc/gimple-fold.c.jj 2017-09-01 09:26:37.054748039 +0200 >+++ gcc/gimple-fold.c 2017-09-01 19:37:03.283795450 +0200 >@@ -3936,18 +3936,43 @@ gimple_fold_call (gimple_stmt_iterator * > 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); >--- gcc/testsuite/gcc.dg/ubsan/pr81981.c.jj 2017-09-01 >19:35:37.555782465 +0200 >+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c 2017-09-01 19:35:37.555782465 >+0200 >@@ -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" } */ >+} > > > Jakub
--- gcc/gimple-fold.c.jj 2017-09-01 09:26:37.054748039 +0200 +++ gcc/gimple-fold.c 2017-09-01 19:37:03.283795450 +0200 @@ -3936,18 +3936,43 @@ gimple_fold_call (gimple_stmt_iterator * 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); --- gcc/testsuite/gcc.dg/ubsan/pr81981.c.jj 2017-09-01 19:35:37.555782465 +0200 +++ gcc/testsuite/gcc.dg/ubsan/pr81981.c 2017-09-01 19:35:37.555782465 +0200 @@ -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" } */ +}