From patchwork Mon Mar 11 20:16:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1054860 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-497712-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="FhG98dlk"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="KDstvBYj"; 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 44J8XF43zPz9ryj for ; Tue, 12 Mar 2019 07:16:53 +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=OatPjHy0qds6DIt4ts2LQG0+1chmbssTlmkbp+qLj5nmhmM1LV srTubkbTtx+jGI3rkKkxZQtPVmRkGxbcKNMQ9zTmw1Tr153BiM0a1HT9izwc+NSz fj9/B0gfyqKUE3qy7HgYC4TqU2Ctny/fAh+7TfK/fCsUTSIaOEQKhSUI4= 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=fVak+LPjhnZ4ZX0AhcHsfvPEqLM=; b=FhG98dlk2CG4YKCmrsSN fOI8kAd1qfmjEklRidVRMnO+tDGtQOjPIx1ItYfOiYqXX/wvkoiQ94LxdbxlFPWj rxq4ogxdzadlbUtKFqT/1PU9ZJ2pUhwzHxUN0v5VxsExgl4z7xEenqaqHUoe6nV9 86jc+jgi5vc786PfKNBx94g= Received: (qmail 120284 invoked by alias); 11 Mar 2019 20:16:46 -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 120276 invoked by uid 89); 11 Mar 2019 20:16:46 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-9.0 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=HX-Languages-Length:2611 X-HELO: mail-qt1-f169.google.com Received: from mail-qt1-f169.google.com (HELO mail-qt1-f169.google.com) (209.85.160.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 11 Mar 2019 20:16:44 +0000 Received: by mail-qt1-f169.google.com with SMTP id h39so76775qte.2 for ; Mon, 11 Mar 2019 13:16:44 -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=ea18a5hH+Hv9RVwxOgyrteU9KHD76zEI5uJ5zLLQSk8=; b=KDstvBYjBwu5K6KR/CCmiao1CDckJLfIyw/uemiyQs7iKbR/73srhoQ9xmi7ozi22P RyyLVyKmpwIrYKwvQQphwRsznS6ucjdwiiVlhfWYiPTz1GSbqaMMUBlkGmlWYwHYbg57 g85D2X3ceMiT/SiGUrfAepIHvAT1dqFNwodfz/EXgLj+Y5T0GvJ65ndo4TK3qByi1Jl7 ad/+OoJFyIKA1KrmVcAAi7KOH5/JBFoIMJjD+u8wofKipZz8q+v0niZC0cMUiUlQH8J2 t+mVLmv+LV8nKmy3dx+MZ9BXaeBFfIjiYlGGrgQZ34y+38NSch41HicOcJIT+dk+75dJ eRxw== Received: from [192.168.0.106] (174-16-104-92.hlrn.qwest.net. [174.16.104.92]) by smtp.gmail.com with ESMTPSA id 202sm1894358qkn.27.2019.03.11.13.16.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Mar 2019 13:16:41 -0700 (PDT) To: "gcc-patches@gcc.gnu.org" From: Martin Sebor Subject: [PATCH] avoid assuming every type has a size (PR 89662) Message-ID: <49192c12-c10b-49d0-9eed-23cc81691fa2@gmail.com> Date: Mon, 11 Mar 2019 14:16:40 -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 A -Warray-bounds enhancement committed last year into GCC 9 introduced an assumption that the MEM_REF type argument has a size. The test case submitted in PR89662 does pointer addition on void*, in which the MEM_REF type is void*, which breaks the assumption. The attached change removes this assumption and considers such types to have the size of 1. (The result is used to scale the offset in diagnostics after it has been determined to be out of bounds.) Martin PR tree-optimization/89662 - -Warray-bounds ICE on void* arithmetic gcc/ChangeLog: PR tree-optimization/89662 * tree-vrp.c (vrp_prop::check_mem_ref): Avoid assuming every type has a size. gcc/testsuite/ChangeLog: PR tree-optimization/89662 * gcc.dg/Warray-bounds-41.c: New test. Index: gcc/tree-vrp.c =================================================================== --- gcc/tree-vrp.c (revision 269445) +++ gcc/tree-vrp.c (working copy) @@ -4718,13 +4718,16 @@ vrp_prop::check_mem_ref (location_t location, tree { /* Extract the element type out of MEM_REF and use its size to compute the index to print in the diagnostic; arrays - in MEM_REF don't mean anything. */ + in MEM_REF don't mean anything. A type with no size like + void is as good as having a size of 1. */ tree type = TREE_TYPE (ref); while (TREE_CODE (type) == ARRAY_TYPE) type = TREE_TYPE (type); - tree size = TYPE_SIZE_UNIT (type); - offrange[0] = offrange[0] / wi::to_offset (size); - offrange[1] = offrange[1] / wi::to_offset (size); + if (tree size = TYPE_SIZE_UNIT (type)) + { + offrange[0] = offrange[0] / wi::to_offset (size); + offrange[1] = offrange[1] / wi::to_offset (size); + } } else { Index: gcc/testsuite/gcc.dg/Warray-bounds-41.c =================================================================== --- gcc/testsuite/gcc.dg/Warray-bounds-41.c (nonexistent) +++ gcc/testsuite/gcc.dg/Warray-bounds-41.c (working copy) @@ -0,0 +1,33 @@ +/* PR tree-optimization/89662- -Warray-bounds ICE on void* arithmetic + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +void* vptr (void *c) +{ + return c; +} + +void sink (void*); + +void test_vptr_arith_vla_cst (void) +{ + int n = 1; + char c[n]; + sink (vptr (c) - 1); /* { dg-warning "\\\[-Warray-bounds" } */ +} + +void test_vptr_arith_vla_range (int n) +{ + if (n < 1 || 4 < n) + return; + + char c[n]; + sink (vptr (c) - 1); /* { dg-warning "\\\[-Warray-bounds" "pr82608" { xfail *-*-* } } */ +} + +void test_vptr_arith_vla_var (int n) +{ + char c[n]; + sink (vptr (c) - 1); /* { dg-warning "\\\[-Warray-bounds" "pr82608" { xfail *-*-* } } */ +} +