* [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)
@ 2017-09-01 11:17 Jakub Jelinek
2017-09-01 12:32 ` Richard Biener
0 siblings, 1 reply; 6+ messages in thread
From: Jakub Jelinek @ 2017-09-01 11:17 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
Hi!
This patch fixes the following testcase by folding some ubsan internal fns
we'd either remove anyway during sanopt, or lower into if (cond)
do_something during sanopt where cond would be always false.
Additionally, I've tried to clean up a little bit IFN_UBSAN_OBJECT_SIZE
handling by using variables for the call arguments that make it clear
what the arguments are.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
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.
* gcc.dg/ubsan/pr81981.c: New test.
--- gcc/gimple-fold.c.jj 2017-08-10 02:31:21.000000000 +0200
+++ gcc/gimple-fold.c 2017-08-29 18:50:49.993673432 +0200
@@ -3938,11 +3938,23 @@ 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)))
+ {
+ gsi_replace (gsi, gimple_build_nop (), false);
+ unlink_stmt_vdef (stmt);
+ release_defs (stmt);
+ 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);
@@ -3950,6 +3962,25 @@ gimple_fold_call (gimple_stmt_iterator *
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))
+ {
+ gsi_replace (gsi, gimple_build_nop (), false);
+ unlink_stmt_vdef (stmt);
+ release_defs (stmt);
+ 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-08-29 18:54:33.826107761 +0200
+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c 2017-08-29 18:55:36.721386827 +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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)
2017-09-01 11:17 [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981) Jakub Jelinek
@ 2017-09-01 12:32 ` Richard Biener
2017-09-01 13:53 ` Jakub Jelinek
0 siblings, 1 reply; 6+ messages in thread
From: Richard Biener @ 2017-09-01 12:32 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On September 1, 2017 1:16:54 PM GMT+02:00, Jakub Jelinek <jakub@redhat.com> wrote:
>Hi!
>
>This patch fixes the following testcase by folding some ubsan internal
>fns
>we'd either remove anyway during sanopt, or lower into if (cond)
>do_something during sanopt where cond would be always false.
>
>Additionally, I've tried to clean up a little bit IFN_UBSAN_OBJECT_SIZE
>handling by using variables for the call arguments that make it clear
>what the arguments are.
>
>Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
I think there's a helper for replace - with-nop.
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.
>
> * gcc.dg/ubsan/pr81981.c: New test.
>
>--- gcc/gimple-fold.c.jj 2017-08-10 02:31:21.000000000 +0200
>+++ gcc/gimple-fold.c 2017-08-29 18:50:49.993673432 +0200
>@@ -3938,11 +3938,23 @@ 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)))
>+ {
>+ gsi_replace (gsi, gimple_build_nop (), false);
>+ unlink_stmt_vdef (stmt);
>+ release_defs (stmt);
>+ 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);
>@@ -3950,6 +3962,25 @@ gimple_fold_call (gimple_stmt_iterator *
> 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))
>+ {
>+ gsi_replace (gsi, gimple_build_nop (), false);
>+ unlink_stmt_vdef (stmt);
>+ release_defs (stmt);
>+ 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-08-29
>18:54:33.826107761 +0200
>+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c 2017-08-29 18:55:36.721386827
>+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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)
2017-09-01 12:32 ` Richard Biener
@ 2017-09-01 13:53 ` Jakub Jelinek
2017-09-01 17:11 ` Richard Biener
0 siblings, 1 reply; 6+ messages in thread
From: Jakub Jelinek @ 2017-09-01 13:53 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
On Fri, Sep 01, 2017 at 02:32:43PM +0200, Richard Biener wrote:
> On September 1, 2017 1:16:54 PM GMT+02:00, Jakub Jelinek <jakub@redhat.com> wrote:
> >Hi!
> >
> >This patch fixes the following testcase by folding some ubsan internal
> >fns
> >we'd either remove anyway during sanopt, or lower into if (cond)
> >do_something during sanopt where cond would be always false.
> >
> >Additionally, I've tried to clean up a little bit IFN_UBSAN_OBJECT_SIZE
> >handling by using variables for the call arguments that make it clear
> >what the arguments are.
> >
> >Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
> I think there's a helper for replace - with-nop.
Can't find it.
gimplify_and_update_call_from_tree has to add it, but I'd need
to call it with something that gimplifies into empty sequence (void_node?)
and it would still go through push_gimplify_context/gimplify_and_add/pop_gimplify_context
etc., which looks quite expensive.
> >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.
> >
> > * gcc.dg/ubsan/pr81981.c: New test.
> >
> >--- gcc/gimple-fold.c.jj 2017-08-10 02:31:21.000000000 +0200
> >+++ gcc/gimple-fold.c 2017-08-29 18:50:49.993673432 +0200
> >@@ -3938,11 +3938,23 @@ 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)))
> >+ {
> >+ gsi_replace (gsi, gimple_build_nop (), false);
> >+ unlink_stmt_vdef (stmt);
> >+ release_defs (stmt);
> >+ 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);
> >@@ -3950,6 +3962,25 @@ gimple_fold_call (gimple_stmt_iterator *
> > 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))
> >+ {
> >+ gsi_replace (gsi, gimple_build_nop (), false);
> >+ unlink_stmt_vdef (stmt);
> >+ release_defs (stmt);
> >+ 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-08-29
> >18:54:33.826107761 +0200
> >+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c 2017-08-29 18:55:36.721386827
> >+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
Jakub
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)
2017-09-01 13:53 ` Jakub Jelinek
@ 2017-09-01 17:11 ` Richard Biener
2017-09-01 20:28 ` [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981, take 2) Jakub Jelinek
0 siblings, 1 reply; 6+ messages in thread
From: Richard Biener @ 2017-09-01 17:11 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On September 1, 2017 3:53:28 PM GMT+02:00, Jakub Jelinek <jakub@redhat.com> wrote:
>On Fri, Sep 01, 2017 at 02:32:43PM +0200, Richard Biener wrote:
>> On September 1, 2017 1:16:54 PM GMT+02:00, Jakub Jelinek
><jakub@redhat.com> wrote:
>> >Hi!
>> >
>> >This patch fixes the following testcase by folding some ubsan
>internal
>> >fns
>> >we'd either remove anyway during sanopt, or lower into if (cond)
>> >do_something during sanopt where cond would be always false.
>> >
>> >Additionally, I've tried to clean up a little bit
>IFN_UBSAN_OBJECT_SIZE
>> >handling by using variables for the call arguments that make it
>clear
>> >what the arguments are.
>> >
>> >Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>>
>> I think there's a helper for replace - with-nop.
>
>Can't find it.
>gimplify_and_update_call_from_tree has to add it, but I'd need
>to call it with something that gimplifies into empty sequence
>(void_node?)
>and it would still go through
>push_gimplify_context/gimplify_and_add/pop_gimplify_context
>etc., which looks quite expensive.
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)
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.
>> >
>> > * gcc.dg/ubsan/pr81981.c: New test.
>> >
>> >--- gcc/gimple-fold.c.jj 2017-08-10 02:31:21.000000000 +0200
>> >+++ gcc/gimple-fold.c 2017-08-29 18:50:49.993673432 +0200
>> >@@ -3938,11 +3938,23 @@ 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)))
>> >+ {
>> >+ gsi_replace (gsi, gimple_build_nop (), false);
>> >+ unlink_stmt_vdef (stmt);
>> >+ release_defs (stmt);
>> >+ 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);
>> >@@ -3950,6 +3962,25 @@ gimple_fold_call (gimple_stmt_iterator *
>> > 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))
>> >+ {
>> >+ gsi_replace (gsi, gimple_build_nop (), false);
>> >+ unlink_stmt_vdef (stmt);
>> >+ release_defs (stmt);
>> >+ 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-08-29
>> >18:54:33.826107761 +0200
>> >+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c 2017-08-29
>18:55:36.721386827
>> >+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
>
> Jakub
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981, take 2)
2017-09-01 17:11 ` Richard Biener
@ 2017-09-01 20:28 ` Jakub Jelinek
2017-09-02 11:51 ` Richard Biener
0 siblings, 1 reply; 6+ messages in thread
From: Jakub Jelinek @ 2017-09-01 20:28 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
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?
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981, take 2)
2017-09-01 20:28 ` [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981, take 2) Jakub Jelinek
@ 2017-09-02 11:51 ` Richard Biener
0 siblings, 0 replies; 6+ messages in thread
From: Richard Biener @ 2017-09-02 11:51 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2017-09-02 11:51 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-01 11:17 [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981) Jakub Jelinek
2017-09-01 12:32 ` Richard Biener
2017-09-01 13:53 ` Jakub Jelinek
2017-09-01 17:11 ` Richard Biener
2017-09-01 20:28 ` [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981, take 2) Jakub Jelinek
2017-09-02 11:51 ` Richard Biener
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).