[{"id":1761649,"web_url":"http://patchwork.ozlabs.org/comment/1761649/","msgid":"<C026072F-D7E6-42FC-A44E-E4080575E585@suse.de>","list_archive_url":null,"date":"2017-09-01T12:32:43","subject":"Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)","submitter":{"id":4338,"url":"http://patchwork.ozlabs.org/api/people/4338/","name":"Richard Biener","email":"rguenther@suse.de"},"content":"On September 1, 2017 1:16:54 PM GMT+02:00, Jakub Jelinek <jakub@redhat.com> wrote:\n>Hi!\n>\n>This patch fixes the following testcase by folding some ubsan internal\n>fns\n>we'd either remove anyway during sanopt, or lower into if (cond)\n>do_something during sanopt where cond would be always false.\n>\n>Additionally, I've tried to clean up a little bit IFN_UBSAN_OBJECT_SIZE\n>handling by using variables for the call arguments that make it clear\n>what the arguments are.\n>\n>Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?\n\nI think there's a helper for replace - with-nop. \n\nRichard. \n\n>2017-09-01  Jakub Jelinek  <jakub@redhat.com>\n>\n>\tPR sanitizer/81981\n>\t* gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR\n>\tand UBSAN_BOUNDS internal calls.  Clean up IFN_UBSAN_OBJECT_SIZE\n>\thandling.\n>\n>\t* gcc.dg/ubsan/pr81981.c: New test.\n>\n>--- gcc/gimple-fold.c.jj\t2017-08-10 02:31:21.000000000 +0200\n>+++ gcc/gimple-fold.c\t2017-08-29 18:50:49.993673432 +0200\n>@@ -3938,11 +3938,23 @@ gimple_fold_call (gimple_stmt_iterator *\n> \t\t\t\t\tgimple_call_arg (stmt, 2));\n> \t  break;\n> \tcase IFN_UBSAN_OBJECT_SIZE:\n>-\t  if (integer_all_onesp (gimple_call_arg (stmt, 2))\n>-\t      || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST\n>-\t\t  && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST\n>-\t\t  && tree_int_cst_le (gimple_call_arg (stmt, 1),\n>-\t\t\t\t      gimple_call_arg (stmt, 2))))\n>+\t  {\n>+\t    tree offset = gimple_call_arg (stmt, 1);\n>+\t    tree objsize = gimple_call_arg (stmt, 2);\n>+\t    if (integer_all_onesp (objsize)\n>+\t\t|| (TREE_CODE (offset) == INTEGER_CST\n>+\t\t    && TREE_CODE (objsize) == INTEGER_CST\n>+\t\t    && tree_int_cst_le (offset, objsize)))\n>+\t      {\n>+\t\tgsi_replace (gsi, gimple_build_nop (), false);\n>+\t\tunlink_stmt_vdef (stmt);\n>+\t\trelease_defs (stmt);\n>+\t\treturn true;\n>+\t      }\n>+\t  }\n>+\t  break;\n>+\tcase IFN_UBSAN_PTR:\n>+\t  if (integer_zerop (gimple_call_arg (stmt, 1)))\n> \t    {\n> \t      gsi_replace (gsi, gimple_build_nop (), false);\n> \t      unlink_stmt_vdef (stmt);\n>@@ -3950,6 +3962,25 @@ gimple_fold_call (gimple_stmt_iterator *\n> \t      return true;\n> \t    }\n> \t  break;\n>+\tcase IFN_UBSAN_BOUNDS:\n>+\t  {\n>+\t    tree index = gimple_call_arg (stmt, 1);\n>+\t    tree bound = gimple_call_arg (stmt, 2);\n>+\t    if (TREE_CODE (index) == INTEGER_CST\n>+\t\t&& TREE_CODE (bound) == INTEGER_CST)\n>+\t      {\n>+\t\tindex = fold_convert (TREE_TYPE (bound), index);\n>+\t\tif (TREE_CODE (index) == INTEGER_CST\n>+\t\t    && tree_int_cst_le (index, bound))\n>+\t\t  {\n>+\t\t    gsi_replace (gsi, gimple_build_nop (), false);\n>+\t\t    unlink_stmt_vdef (stmt);\n>+\t\t    release_defs (stmt);\n>+\t\t    return true;\n>+\t\t  }\n>+\t      }\n>+\t  }\n>+\t  break;\n> \tcase IFN_GOACC_DIM_SIZE:\n> \tcase IFN_GOACC_DIM_POS:\n> \t  result = fold_internal_goacc_dim (stmt);\n>--- gcc/testsuite/gcc.dg/ubsan/pr81981.c.jj\t2017-08-29\n>18:54:33.826107761 +0200\n>+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c\t2017-08-29 18:55:36.721386827\n>+0200\n>@@ -0,0 +1,21 @@\n>+/* PR sanitizer/81981 */\n>+/* { dg-do compile } */\n>+/* { dg-options \"-O2 -Wmaybe-uninitialized -fsanitize=undefined\n>-ffat-lto-objects\" } */\n>+\n>+int v;\n>+\n>+int\n>+foo (int i)\n>+{\n>+  int t[1], u[1];\n>+  int n = 0;\n>+\n>+  if (i)\n>+    {\n>+      t[n] = i;\n>+      u[0] = i;\n>+    }\n>+\n>+  v = u[0];\t\t/* { dg-warning \"may be used uninitialized in this\n>function\" } */\n>+  return t[0];\t\t/* { dg-warning \"may be used uninitialized in this\n>function\" } */\n>+}\n>\n>\tJakub","headers":{"Return-Path":"<gcc-patches-return-461278-incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":["patchwork-incoming@bilbo.ozlabs.org","mailing list gcc-patches@gcc.gnu.org"],"Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org\n\t(client-ip=209.132.180.131; helo=sourceware.org;\n\tenvelope-from=gcc-patches-return-461278-incoming=patchwork.ozlabs.org@gcc.gnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (1024-bit key;\n\tunprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org\n\theader.b=\"y5QsyGAz\"; dkim-atps=neutral","sourceware.org; auth=none"],"Received":["from sourceware.org (server1.sourceware.org [209.132.180.131])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits)) (No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xkJYV5XrWz9s7c\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri,  1 Sep 2017 22:33:01 +1000 (AEST)","(qmail 60273 invoked by alias); 1 Sep 2017 12:32:53 -0000","(qmail 60224 invoked by uid 89); 1 Sep 2017 12:32:52 -0000","from mx2.suse.de (HELO mx1.suse.de) (195.135.220.15) by\n\tsourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP;\n\tFri, 01 Sep 2017 12:32:47 +0000","from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254])\tby\n\tmx1.suse.de (Postfix) with ESMTP id 68CB1ACB8;\n\tFri,  1 Sep 2017 12:32:45 +0000 (UTC)"],"DomainKey-Signature":"a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id\n\t:list-unsubscribe:list-archive:list-post:list-help:sender:date\n\t:in-reply-to:references:mime-version:content-type\n\t:content-transfer-encoding:subject:to:cc:from:message-id; q=dns;\n\ts=default; b=s5EzYyaVObJGIDZ2TeOAfz9ESZ7/iPK4FEzulXpCYaXo1ONaKl\n\tgbz6hvxnkYNp/D2dcL9nFHRifM4P8n74l/Ch5kpy8B9AnD13ZBuJbPfvcWk4rsRp\n\tQpFkGkOdvPz4wJk9EhhFD+f0RjBpQNn8CrQ52YY711StgxjjSdQX33WmE=","DKIM-Signature":"v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id\n\t:list-unsubscribe:list-archive:list-post:list-help:sender:date\n\t:in-reply-to:references:mime-version:content-type\n\t:content-transfer-encoding:subject:to:cc:from:message-id; s=\n\tdefault; bh=sR/mH8MKjTJsgkd4lJ54gAaPXyE=; b=y5QsyGAzMviEk/DiiJ3z\n\tuIE5HgDMl8x08HegXecYnmKRQ6TymFayjMa665q/EJSo46/QQIaYhC+qGbPTrAKG\n\tsxViq8rh7hUZtQF3VPGiPN17Umw/oMRuKo2QNKOCIDCtdU7cXjp/r46UV0+gwYYX\n\tpptLJTSD2PGGxO8MQEO4Dzc=","Mailing-List":"contact gcc-patches-help@gcc.gnu.org; run by ezmlm","Precedence":"bulk","List-Id":"<gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<mailto:gcc-patches-unsubscribe-incoming=patchwork.ozlabs.org@gcc.gnu.org>","List-Archive":"<http://gcc.gnu.org/ml/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-help@gcc.gnu.org>","Sender":"gcc-patches-owner@gcc.gnu.org","X-Virus-Found":"No","X-Spam-SWARE-Status":"No, score=-16.9 required=5.0 tests=BAYES_00, GIT_PATCH_1,\n\tGIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD,\n\tSPF_PASS autolearn=ham version=3.3.2 spammy=","X-HELO":"mx1.suse.de","Date":"Fri, 01 Sep 2017 14:32:43 +0200","User-Agent":"K-9 Mail for Android","In-Reply-To":"<20170901111654.GE2323@tucnak>","References":"<20170901111654.GE2323@tucnak>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","Subject":"Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)","To":"Jakub Jelinek <jakub@redhat.com>","CC":"gcc-patches@gcc.gnu.org","From":"Richard Biener <rguenther@suse.de>","Message-ID":"<C026072F-D7E6-42FC-A44E-E4080575E585@suse.de>"}},{"id":1761711,"web_url":"http://patchwork.ozlabs.org/comment/1761711/","msgid":"<20170901135328.GL2323@tucnak>","list_archive_url":null,"date":"2017-09-01T13:53:28","subject":"Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)","submitter":{"id":671,"url":"http://patchwork.ozlabs.org/api/people/671/","name":"Jakub Jelinek","email":"jakub@redhat.com"},"content":"On Fri, Sep 01, 2017 at 02:32:43PM +0200, Richard Biener wrote:\n> On September 1, 2017 1:16:54 PM GMT+02:00, Jakub Jelinek <jakub@redhat.com> wrote:\n> >Hi!\n> >\n> >This patch fixes the following testcase by folding some ubsan internal\n> >fns\n> >we'd either remove anyway during sanopt, or lower into if (cond)\n> >do_something during sanopt where cond would be always false.\n> >\n> >Additionally, I've tried to clean up a little bit IFN_UBSAN_OBJECT_SIZE\n> >handling by using variables for the call arguments that make it clear\n> >what the arguments are.\n> >\n> >Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?\n> \n> I think there's a helper for replace - with-nop. \n\nCan't find it.\ngimplify_and_update_call_from_tree has to add it, but I'd need\nto call it with something that gimplifies into empty sequence (void_node?)\nand it would still go through push_gimplify_context/gimplify_and_add/pop_gimplify_context\netc., which looks quite expensive.\n\n> >2017-09-01  Jakub Jelinek  <jakub@redhat.com>\n> >\n> >\tPR sanitizer/81981\n> >\t* gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR\n> >\tand UBSAN_BOUNDS internal calls.  Clean up IFN_UBSAN_OBJECT_SIZE\n> >\thandling.\n> >\n> >\t* gcc.dg/ubsan/pr81981.c: New test.\n> >\n> >--- gcc/gimple-fold.c.jj\t2017-08-10 02:31:21.000000000 +0200\n> >+++ gcc/gimple-fold.c\t2017-08-29 18:50:49.993673432 +0200\n> >@@ -3938,11 +3938,23 @@ gimple_fold_call (gimple_stmt_iterator *\n> > \t\t\t\t\tgimple_call_arg (stmt, 2));\n> > \t  break;\n> > \tcase IFN_UBSAN_OBJECT_SIZE:\n> >-\t  if (integer_all_onesp (gimple_call_arg (stmt, 2))\n> >-\t      || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST\n> >-\t\t  && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST\n> >-\t\t  && tree_int_cst_le (gimple_call_arg (stmt, 1),\n> >-\t\t\t\t      gimple_call_arg (stmt, 2))))\n> >+\t  {\n> >+\t    tree offset = gimple_call_arg (stmt, 1);\n> >+\t    tree objsize = gimple_call_arg (stmt, 2);\n> >+\t    if (integer_all_onesp (objsize)\n> >+\t\t|| (TREE_CODE (offset) == INTEGER_CST\n> >+\t\t    && TREE_CODE (objsize) == INTEGER_CST\n> >+\t\t    && tree_int_cst_le (offset, objsize)))\n> >+\t      {\n> >+\t\tgsi_replace (gsi, gimple_build_nop (), false);\n> >+\t\tunlink_stmt_vdef (stmt);\n> >+\t\trelease_defs (stmt);\n> >+\t\treturn true;\n> >+\t      }\n> >+\t  }\n> >+\t  break;\n> >+\tcase IFN_UBSAN_PTR:\n> >+\t  if (integer_zerop (gimple_call_arg (stmt, 1)))\n> > \t    {\n> > \t      gsi_replace (gsi, gimple_build_nop (), false);\n> > \t      unlink_stmt_vdef (stmt);\n> >@@ -3950,6 +3962,25 @@ gimple_fold_call (gimple_stmt_iterator *\n> > \t      return true;\n> > \t    }\n> > \t  break;\n> >+\tcase IFN_UBSAN_BOUNDS:\n> >+\t  {\n> >+\t    tree index = gimple_call_arg (stmt, 1);\n> >+\t    tree bound = gimple_call_arg (stmt, 2);\n> >+\t    if (TREE_CODE (index) == INTEGER_CST\n> >+\t\t&& TREE_CODE (bound) == INTEGER_CST)\n> >+\t      {\n> >+\t\tindex = fold_convert (TREE_TYPE (bound), index);\n> >+\t\tif (TREE_CODE (index) == INTEGER_CST\n> >+\t\t    && tree_int_cst_le (index, bound))\n> >+\t\t  {\n> >+\t\t    gsi_replace (gsi, gimple_build_nop (), false);\n> >+\t\t    unlink_stmt_vdef (stmt);\n> >+\t\t    release_defs (stmt);\n> >+\t\t    return true;\n> >+\t\t  }\n> >+\t      }\n> >+\t  }\n> >+\t  break;\n> > \tcase IFN_GOACC_DIM_SIZE:\n> > \tcase IFN_GOACC_DIM_POS:\n> > \t  result = fold_internal_goacc_dim (stmt);\n> >--- gcc/testsuite/gcc.dg/ubsan/pr81981.c.jj\t2017-08-29\n> >18:54:33.826107761 +0200\n> >+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c\t2017-08-29 18:55:36.721386827\n> >+0200\n> >@@ -0,0 +1,21 @@\n> >+/* PR sanitizer/81981 */\n> >+/* { dg-do compile } */\n> >+/* { dg-options \"-O2 -Wmaybe-uninitialized -fsanitize=undefined\n> >-ffat-lto-objects\" } */\n> >+\n> >+int v;\n> >+\n> >+int\n> >+foo (int i)\n> >+{\n> >+  int t[1], u[1];\n> >+  int n = 0;\n> >+\n> >+  if (i)\n> >+    {\n> >+      t[n] = i;\n> >+      u[0] = i;\n> >+    }\n> >+\n> >+  v = u[0];\t\t/* { dg-warning \"may be used uninitialized in this\n> >function\" } */\n> >+  return t[0];\t\t/* { dg-warning \"may be used uninitialized in this\n> >function\" } */\n> >+}\n> >\n> >\tJakub\n\n\tJakub","headers":{"Return-Path":"<gcc-patches-return-461298-incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":["patchwork-incoming@bilbo.ozlabs.org","mailing list gcc-patches@gcc.gnu.org"],"Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org\n\t(client-ip=209.132.180.131; helo=sourceware.org;\n\tenvelope-from=gcc-patches-return-461298-incoming=patchwork.ozlabs.org@gcc.gnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (1024-bit key;\n\tunprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org\n\theader.b=\"LDfTSeTN\"; dkim-atps=neutral","sourceware.org; auth=none","ext-mx02.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx02.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=jakub@redhat.com"],"Received":["from sourceware.org (server1.sourceware.org [209.132.180.131])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits)) (No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xkLLj2fZBz9s7F\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri,  1 Sep 2017 23:53:46 +1000 (AEST)","(qmail 42260 invoked by alias); 1 Sep 2017 13:53:39 -0000","(qmail 42246 invoked by uid 89); 1 Sep 2017 13:53:39 -0000","from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by\n\tsourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP;\n\tFri, 01 Sep 2017 13:53:33 +0000","from smtp.corp.redhat.com\n\t(int-mx06.intmail.prod.int.phx2.redhat.com\n\t[10.5.11.16])\t(using TLSv1.2 with cipher AECDH-AES256-SHA\n\t(256/256 bits))\t(No client certificate requested)\tby\n\tmx1.redhat.com (Postfix) with ESMTPS id 6F6E2806B8;\n\tFri,  1 Sep 2017 13:53:32 +0000 (UTC)","from tucnak.zalov.cz (ovpn-116-33.ams2.redhat.com\n\t[10.36.116.33])\tby smtp.corp.redhat.com (Postfix) with ESMTPS\n\tid EF3887B147; Fri,  1 Sep 2017 13:53:31 +0000 (UTC)","from tucnak.zalov.cz (localhost [127.0.0.1])\tby tucnak.zalov.cz\n\t(8.15.2/8.15.2) with ESMTP id v81DrTBY007870;\n\tFri, 1 Sep 2017 15:53:29 +0200","(from jakub@localhost)\tby tucnak.zalov.cz (8.15.2/8.15.2/Submit)\n\tid v81DrS3t007869; Fri, 1 Sep 2017 15:53:28 +0200"],"DomainKey-Signature":"a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id\n\t:list-unsubscribe:list-archive:list-post:list-help:sender:date\n\t:from:to:cc:subject:message-id:reply-to:references:mime-version\n\t:content-type:in-reply-to; q=dns; s=default; b=Jl9CrageNROrjQpSC\n\tkdyu5TITtyqplokT6XS4gzKyJJK5hcTHd/4yRbP0mSjZNay7dmTvJjTF+xTfSwfI\n\tL8YCuhQMjj993E7WbW+0d13OYlcJskR8s6ZbZfYZgkFOCyHXxnai0LKSSSsWkARM\n\tNZCGJGF7oOKdQCVLFm51zYWkm4=","DKIM-Signature":"v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id\n\t:list-unsubscribe:list-archive:list-post:list-help:sender:date\n\t:from:to:cc:subject:message-id:reply-to:references:mime-version\n\t:content-type:in-reply-to; s=default; bh=sPdcbMvoU+g+NJOGTI87obq\n\tjEI0=; b=LDfTSeTNxxQYdBVyfGHEDRKd745asyYf7ZfdKwQV5VnpXytcpiP66uV\n\tIOiaxp0D+DTxiGkfZj0R/iN1p5EIYzda9TEIMsC0tmDkCLuC4U83LAGFUm9TUBd4\n\t2/zha7sRQeydVuUh2kTiv+hJnSoGhXBbf4AXRwG4MGOZlebjmkOM=","Mailing-List":"contact gcc-patches-help@gcc.gnu.org; run by ezmlm","Precedence":"bulk","List-Id":"<gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<mailto:gcc-patches-unsubscribe-incoming=patchwork.ozlabs.org@gcc.gnu.org>","List-Archive":"<http://gcc.gnu.org/ml/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-help@gcc.gnu.org>","Sender":"gcc-patches-owner@gcc.gnu.org","X-Virus-Found":"No","X-Spam-SWARE-Status":"No, score=-15.9 required=5.0 tests=BAYES_00, GIT_PATCH_1,\n\tGIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY,\n\tRP_MATCHES_RCVD,\n\tSPF_HELO_PASS autolearn=ham version=3.3.2 spammy=","X-HELO":"mx1.redhat.com","DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 6F6E2806B8","Date":"Fri, 1 Sep 2017 15:53:28 +0200","From":"Jakub Jelinek <jakub@redhat.com>","To":"Richard Biener <rguenther@suse.de>","Cc":"gcc-patches@gcc.gnu.org","Subject":"Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)","Message-ID":"<20170901135328.GL2323@tucnak>","Reply-To":"Jakub Jelinek <jakub@redhat.com>","References":"<20170901111654.GE2323@tucnak>\n\t<C026072F-D7E6-42FC-A44E-E4080575E585@suse.de>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<C026072F-D7E6-42FC-A44E-E4080575E585@suse.de>","User-Agent":"Mutt/1.7.1 (2016-10-04)","X-IsSubscribed":"yes"}},{"id":1761838,"web_url":"http://patchwork.ozlabs.org/comment/1761838/","msgid":"<E8CEFE96-C8AA-4199-8C57-10BBD88BBBC4@suse.de>","list_archive_url":null,"date":"2017-09-01T17:10:51","subject":"Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)","submitter":{"id":4338,"url":"http://patchwork.ozlabs.org/api/people/4338/","name":"Richard Biener","email":"rguenther@suse.de"},"content":"On September 1, 2017 3:53:28 PM GMT+02:00, Jakub Jelinek <jakub@redhat.com> wrote:\n>On Fri, Sep 01, 2017 at 02:32:43PM +0200, Richard Biener wrote:\n>> On September 1, 2017 1:16:54 PM GMT+02:00, Jakub Jelinek\n><jakub@redhat.com> wrote:\n>> >Hi!\n>> >\n>> >This patch fixes the following testcase by folding some ubsan\n>internal\n>> >fns\n>> >we'd either remove anyway during sanopt, or lower into if (cond)\n>> >do_something during sanopt where cond would be always false.\n>> >\n>> >Additionally, I've tried to clean up a little bit\n>IFN_UBSAN_OBJECT_SIZE\n>> >handling by using variables for the call arguments that make it\n>clear\n>> >what the arguments are.\n>> >\n>> >Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?\n>> \n>> I think there's a helper for replace - with-nop. \n>\n>Can't find it.\n>gimplify_and_update_call_from_tree has to add it, but I'd need\n>to call it with something that gimplifies into empty sequence\n>(void_node?)\n>and it would still go through\n>push_gimplify_context/gimplify_and_add/pop_gimplify_context\n>etc., which looks quite expensive.\n\nOK, 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) \n\nRichard. \n\n>> >2017-09-01  Jakub Jelinek  <jakub@redhat.com>\n>> >\n>> >\tPR sanitizer/81981\n>> >\t* gimple-fold.c (gimple_fold_call): Optimize away useless UBSAN_PTR\n>> >\tand UBSAN_BOUNDS internal calls.  Clean up IFN_UBSAN_OBJECT_SIZE\n>> >\thandling.\n>> >\n>> >\t* gcc.dg/ubsan/pr81981.c: New test.\n>> >\n>> >--- gcc/gimple-fold.c.jj\t2017-08-10 02:31:21.000000000 +0200\n>> >+++ gcc/gimple-fold.c\t2017-08-29 18:50:49.993673432 +0200\n>> >@@ -3938,11 +3938,23 @@ gimple_fold_call (gimple_stmt_iterator *\n>> > \t\t\t\t\tgimple_call_arg (stmt, 2));\n>> > \t  break;\n>> > \tcase IFN_UBSAN_OBJECT_SIZE:\n>> >-\t  if (integer_all_onesp (gimple_call_arg (stmt, 2))\n>> >-\t      || (TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST\n>> >-\t\t  && TREE_CODE (gimple_call_arg (stmt, 2)) == INTEGER_CST\n>> >-\t\t  && tree_int_cst_le (gimple_call_arg (stmt, 1),\n>> >-\t\t\t\t      gimple_call_arg (stmt, 2))))\n>> >+\t  {\n>> >+\t    tree offset = gimple_call_arg (stmt, 1);\n>> >+\t    tree objsize = gimple_call_arg (stmt, 2);\n>> >+\t    if (integer_all_onesp (objsize)\n>> >+\t\t|| (TREE_CODE (offset) == INTEGER_CST\n>> >+\t\t    && TREE_CODE (objsize) == INTEGER_CST\n>> >+\t\t    && tree_int_cst_le (offset, objsize)))\n>> >+\t      {\n>> >+\t\tgsi_replace (gsi, gimple_build_nop (), false);\n>> >+\t\tunlink_stmt_vdef (stmt);\n>> >+\t\trelease_defs (stmt);\n>> >+\t\treturn true;\n>> >+\t      }\n>> >+\t  }\n>> >+\t  break;\n>> >+\tcase IFN_UBSAN_PTR:\n>> >+\t  if (integer_zerop (gimple_call_arg (stmt, 1)))\n>> > \t    {\n>> > \t      gsi_replace (gsi, gimple_build_nop (), false);\n>> > \t      unlink_stmt_vdef (stmt);\n>> >@@ -3950,6 +3962,25 @@ gimple_fold_call (gimple_stmt_iterator *\n>> > \t      return true;\n>> > \t    }\n>> > \t  break;\n>> >+\tcase IFN_UBSAN_BOUNDS:\n>> >+\t  {\n>> >+\t    tree index = gimple_call_arg (stmt, 1);\n>> >+\t    tree bound = gimple_call_arg (stmt, 2);\n>> >+\t    if (TREE_CODE (index) == INTEGER_CST\n>> >+\t\t&& TREE_CODE (bound) == INTEGER_CST)\n>> >+\t      {\n>> >+\t\tindex = fold_convert (TREE_TYPE (bound), index);\n>> >+\t\tif (TREE_CODE (index) == INTEGER_CST\n>> >+\t\t    && tree_int_cst_le (index, bound))\n>> >+\t\t  {\n>> >+\t\t    gsi_replace (gsi, gimple_build_nop (), false);\n>> >+\t\t    unlink_stmt_vdef (stmt);\n>> >+\t\t    release_defs (stmt);\n>> >+\t\t    return true;\n>> >+\t\t  }\n>> >+\t      }\n>> >+\t  }\n>> >+\t  break;\n>> > \tcase IFN_GOACC_DIM_SIZE:\n>> > \tcase IFN_GOACC_DIM_POS:\n>> > \t  result = fold_internal_goacc_dim (stmt);\n>> >--- gcc/testsuite/gcc.dg/ubsan/pr81981.c.jj\t2017-08-29\n>> >18:54:33.826107761 +0200\n>> >+++ gcc/testsuite/gcc.dg/ubsan/pr81981.c\t2017-08-29\n>18:55:36.721386827\n>> >+0200\n>> >@@ -0,0 +1,21 @@\n>> >+/* PR sanitizer/81981 */\n>> >+/* { dg-do compile } */\n>> >+/* { dg-options \"-O2 -Wmaybe-uninitialized -fsanitize=undefined\n>> >-ffat-lto-objects\" } */\n>> >+\n>> >+int v;\n>> >+\n>> >+int\n>> >+foo (int i)\n>> >+{\n>> >+  int t[1], u[1];\n>> >+  int n = 0;\n>> >+\n>> >+  if (i)\n>> >+    {\n>> >+      t[n] = i;\n>> >+      u[0] = i;\n>> >+    }\n>> >+\n>> >+  v = u[0];\t\t/* { dg-warning \"may be used uninitialized in this\n>> >function\" } */\n>> >+  return t[0];\t\t/* { dg-warning \"may be used uninitialized in this\n>> >function\" } */\n>> >+}\n>> >\n>> >\tJakub\n>\n>\tJakub","headers":{"Return-Path":"<gcc-patches-return-461313-incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":["patchwork-incoming@bilbo.ozlabs.org","mailing list gcc-patches@gcc.gnu.org"],"Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org\n\t(client-ip=209.132.180.131; helo=sourceware.org;\n\tenvelope-from=gcc-patches-return-461313-incoming=patchwork.ozlabs.org@gcc.gnu.org;\n\treceiver=<UNKNOWN>)","ozlabs.org; dkim=pass (1024-bit key;\n\tunprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org\n\theader.b=\"PoWmZEi8\"; dkim-atps=neutral","sourceware.org; auth=none"],"Received":["from sourceware.org (server1.sourceware.org [209.132.180.131])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits)) (No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xkQkP3GWzz9t2x\n\tfor <incoming@patchwork.ozlabs.org>;\n\tSat,  2 Sep 2017 03:11:09 +1000 (AEST)","(qmail 104377 invoked by alias); 1 Sep 2017 17:11:01 -0000","(qmail 104368 invoked by uid 89); 1 Sep 2017 17:11:01 -0000","from mx2.suse.de (HELO mx1.suse.de) (195.135.220.15) by\n\tsourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP;\n\tFri, 01 Sep 2017 17:10:56 +0000","from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254])\tby\n\tmx1.suse.de (Postfix) with ESMTP id B0CB0ABB0;\n\tFri,  1 Sep 2017 17:10:53 +0000 (UTC)"],"DomainKey-Signature":"a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id\n\t:list-unsubscribe:list-archive:list-post:list-help:sender:date\n\t:in-reply-to:references:mime-version:content-type\n\t:content-transfer-encoding:subject:to:cc:from:message-id; q=dns;\n\ts=default; b=ajPm/1YDJhSNQhvVYHaxQeZAvgcx7ER/9XPeULIr2pj38BjHrZ\n\tlnAcbQGNRolVGR0nzkL1j5/lfMHWH0RAARmrCM58NjvEPc4R0aROhYoq/chYFVuE\n\tjNXw0/d8sd76HP54/2s+5Jn5lQp27mL7RgyOKOpA4QLqApT5TnEGFGjUo=","DKIM-Signature":"v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id\n\t:list-unsubscribe:list-archive:list-post:list-help:sender:date\n\t:in-reply-to:references:mime-version:content-type\n\t:content-transfer-encoding:subject:to:cc:from:message-id; s=\n\tdefault; bh=BL0pA8GM5YhNUqRsQNgPBV+uD6c=; b=PoWmZEi8abeHM5QmiPxH\n\tVOCOy2ekOMmra33VSmGh7ET4ZW4hZr9XmenROdSks10Yg9WAc/pFW1fpwwlXyGAc\n\tWSI/M/Zdv77DKiE6D7oDKh5isobVBqjOR1wYNMBd/nPcMGfzaXfhrguZ4uzjqDgk\n\tS3XqQabwWw6zV+rs9XepHkU=","Mailing-List":"contact gcc-patches-help@gcc.gnu.org; run by ezmlm","Precedence":"bulk","List-Id":"<gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<mailto:gcc-patches-unsubscribe-incoming=patchwork.ozlabs.org@gcc.gnu.org>","List-Archive":"<http://gcc.gnu.org/ml/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-help@gcc.gnu.org>","Sender":"gcc-patches-owner@gcc.gnu.org","X-Virus-Found":"No","X-Spam-SWARE-Status":"No, score=-16.9 required=5.0 tests=BAYES_00, GIT_PATCH_1,\n\tGIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD,\n\tSPF_PASS autolearn=ham version=3.3.2 spammy=","X-HELO":"mx1.suse.de","Date":"Fri, 01 Sep 2017 19:10:51 +0200","User-Agent":"K-9 Mail for Android","In-Reply-To":"<20170901135328.GL2323@tucnak>","References":"<20170901111654.GE2323@tucnak>\n\t<C026072F-D7E6-42FC-A44E-E4080575E585@suse.de>\n\t<20170901135328.GL2323@tucnak>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","Subject":"Re: [PATCH] Add UBSAN_{PTR,BOUNDS} folding (PR sanitizer/81981)","To":"Jakub Jelinek <jakub@redhat.com>","CC":"gcc-patches@gcc.gnu.org","From":"Richard Biener <rguenther@suse.de>","Message-ID":"<E8CEFE96-C8AA-4199-8C57-10BBD88BBBC4@suse.de>"}}]