From patchwork Mon Mar 18 19:03:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1058053 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-498052-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="WLwr0VD6"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="gdbWlaqe"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44NQZZ1HRPz9s3q for ; Tue, 19 Mar 2019 06:03:47 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=IILAX4OEkLYw9st8fhVaqQmNrzn39L7aWe6ZAKA1KEmpXx7YHF IoCc+WgLfB61iC7HBGhVlhBE3qL+VagJrCQsrudXtsKGFM5TmkJf+XmKwtpHMX3b onQ5D917OaPgJ7yDwasaPJ0NZAq7Bp81ZvT8ZyeiOMFPDSRkeb6QrF0uU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=YFsow8/U2LKWMhYFCCSsb4VOpcM=; b=WLwr0VD6yV9pWdWgT33q cTCubn5T2iDQlcnTLb3q8kMsooUnbSB3Td1IJ5FkGwbip1PMoxmg3oF9m9Q6ERfj Sci4nVTxYhyLdLIPvBhpGZRQBp0qb9qmBe4x22+zYQ6jT5I4x55HBrlPtxXhDuaU /PkYmpnoX1YvjqqO9UpoU70= Received: (qmail 27908 invoked by alias); 18 Mar 2019 19:03:40 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 27894 invoked by uid 89); 18 Mar 2019 19:03:40 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-9.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=unusual, spurious, location_t X-HELO: mail-qk1-f180.google.com Received: from mail-qk1-f180.google.com (HELO mail-qk1-f180.google.com) (209.85.222.180) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 18 Mar 2019 19:03:38 +0000 Received: by mail-qk1-f180.google.com with SMTP id m65so2729353qkl.11 for ; Mon, 18 Mar 2019 12:03:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=YyfXZhSW+PitFct3m4xNgI4OKvnE0y2XVgwRZOw8pAA=; b=gdbWlaqeaVl19fkW1psZoLYxE+0CP03ndWrzKB9bz+LoejjGqhgzHPTJZGuLuIgQfR txDqzYK7BgP8osneKZqTUFQoBmcjDXAATtY/xF/KVgZvD/yZxyJk91+LTS/9XLFWloPt sY6XiQ/HT7RtIRpHb6pVwFVqEi6VD1ri3IMXQDZDuEJdMHfbGxaWTumLM6WBiKvOCMmz 99coQO9cOgtZsyxdcgUg4oQ6iBkeHcMW9pmY2UOi2hQnqV8s8oN/w/i2IQxpFkxqCEqL cFoCUVZC+cdMIbyTSeP+lLmOf5DlC5q35RA35yc4KgH34fosB6dlc7NGwqTXyW/wUNQ1 8s3Q== Received: from [192.168.0.41] (75-166-119-163.hlrn.qwest.net. [75.166.119.163]) by smtp.gmail.com with ESMTPSA id n3sm6215053qkf.93.2019.03.18.12.03.35 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Mar 2019 12:03:35 -0700 (PDT) To: "gcc-patches@gcc.gnu.org" From: Martin Sebor Subject: [PATCH] have -Warray-bounds treat [max, min] ranges same as anti-ranges (PR 89720) Message-ID: <24a09062-adeb-a7cf-5f59-f0015505d481@gmail.com> Date: Mon, 18 Mar 2019 13:03:34 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 X-IsSubscribed: yes I the -Warray-bounds enhancement committed at the beginning of the GCC 9 window I tried to correctly handle offsets in MEM_REFs in the [max, min] kind of a range after converting from sizetype to ptrdiff_type, but I did it wrong. The bug results in false positives in some unusual use cases that I didn't consider at the time. The attached patch removes this incorrect handling and uses the conservative anti-range handling for these cases instead. Martin PS Is there some technical reason why pointer offsets are represented as the unsigned sizetype when they can be signed? PR tree-optimization/89720 - Spurious -Warray-bounds warning on a range with max < min gcc/ChangeLog: PR tree-optimization/89720 * tree-vrp.c (vrp_prop::check_mem_ref): Treat range with max < min more conservatively, the same as anti-range. gcc/testsuite/ChangeLog: PR tree-optimization/89720 * gcc.dg/Warray-bounds-42.c: New test. Index: gcc/tree-vrp.c =================================================================== --- gcc/tree-vrp.c (revision 269767) +++ gcc/tree-vrp.c (working copy) @@ -4546,9 +4546,9 @@ vrp_prop::check_mem_ref (location_t location, tree const value_range *vr = NULL; /* Determine the offsets and increment OFFRANGE for the bounds of each. - The loop computes the the range of the final offset for expressions - such as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs - in some range. */ + The loop computes the range of the final offset for expressions such + as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs in + some range. */ while (TREE_CODE (arg) == SSA_NAME) { gimple *def = SSA_NAME_DEF_STMT (arg); @@ -4583,26 +4583,21 @@ vrp_prop::check_mem_ref (location_t location, tree if (vr->kind () == VR_RANGE) { - if (tree_int_cst_lt (vr->min (), vr->max ())) + offset_int min + = wi::to_offset (fold_convert (ptrdiff_type_node, vr->min ())); + offset_int max + = wi::to_offset (fold_convert (ptrdiff_type_node, vr->max ())); + if (min < max) { - offset_int min - = wi::to_offset (fold_convert (ptrdiff_type_node, vr->min ())); - offset_int max - = wi::to_offset (fold_convert (ptrdiff_type_node, vr->max ())); - if (min < max) - { - offrange[0] += min; - offrange[1] += max; - } - else - { - offrange[0] += max; - offrange[1] += min; - } + offrange[0] += min; + offrange[1] += max; } else { - /* Conservatively add [-MAXOBJSIZE -1, MAXOBJSIZE] + /* When MIN >= MAX, the offset is effectively in a union + of two ranges: [-MAXOBJSIZE -1, MAX] and [MIN, MAXOBJSIZE]. + Since there is no way to represent such a range across + additions, conservatively add [-MAXOBJSIZE -1, MAXOBJSIZE] to OFFRANGE. */ offrange[0] += arrbounds[0]; offrange[1] += arrbounds[1]; Index: gcc/testsuite/gcc.dg/Warray-bounds-42.c =================================================================== --- gcc/testsuite/gcc.dg/Warray-bounds-42.c (nonexistent) +++ gcc/testsuite/gcc.dg/Warray-bounds-42.c (working copy) @@ -0,0 +1,26 @@ +/* PR tree-optimization/89720 - Spurious -Warray-bounds warning on a range + with max < min + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +void f (char*, int); + +#if __SIZEOF_POINTER__ == 8 + +void g (__INT64_TYPE__ i) +{ + char a[65536] = ""; + char *p = a + (i & (__INT64_TYPE__)0xffffffff3fffffffLL); + f (p, *(p - 6)); /* { dg-bogus "\\\[-Warray-bounds" } */ +} + +#elif __SIZEOF_POINTER__ == 4 + +void h (__INT32_TYPE__ i) +{ + char a[65536] = ""; + char *p = a + (i & (__INT32_TYPE__)0x8fffffffLL); + f (p, *(p - 6)); /* { dg-bogus "\\\[-Warray-bounds" } */ +} + +#endif