From patchwork Thu Aug 31 20:42:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 137347 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp486738vqu; Thu, 31 Aug 2023 13:43:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHC6yVKDVHsQRFNzKFV0HK2gvOrECPLLEn8t/PfNzA3NF/S9w+cZuW2hg3H+uoNfNLXRDR/ X-Received: by 2002:aa7:d80c:0:b0:523:38eb:395d with SMTP id v12-20020aa7d80c000000b0052338eb395dmr496251edq.9.1693514625509; Thu, 31 Aug 2023 13:43:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693514625; cv=none; d=google.com; s=arc-20160816; b=nOPPIKjbokW4pIavnakwkMEdBL335V5Z1doWlGAZWp/223W0CkUTw+LZVYiT4U2wYh EhG5/wVyWan3byW9O9/Saj+7IDquS1D+7mT80nDSMtUv5v6WOPG7BlIZV/ZI6cK8Huy3 668Qxvx7CbyHhKTiUIOizyzHw4o3ProcypNGxXaqR88yFBcW6wJ4A/IsugbnsXOtfyqW RAwoKhLfDPqH/Yi7+5n0t9vG/iAw7sA4ttAxKWgEtF4jJvpZrCNo95hS2TuFYIWLfUaP 7lWqg9U3YoaQTsA3S+6IwT1/TFpr4aXt+0lMmLjcdZzSx15yye0oSWjcxv9G/DoeGEAL +z4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=EZWcp3fUhzm+TK4RXyDqGXiejKNOswz82Sn9Lrmv8Ms=; fh=+IEfvAe+9BRgPHWhQEl2uIBTtAiiGDh1ExRZeB5JJoc=; b=F/22OmIXtqW+pNYlCJZeHSw0aTkNYaCbfkuWuS4KGdhAi8rP+vMHfXG9AZFV7ttwZc XPesHNOzWJACLocZ/lR0GjU16FNwBwJyfMu2gvp/5xy0mylvieUSsj64WgGWmVEdf570 LgiXoqltAvQa08QqI2xQrskRTEIIjPio8bvZ3SuBt6vuFkqXoNsH/zby569THNpsIFty H2PBG18xBvROn3Xgi8K0kADcxyS73NU8miSD4jYmQJqEtlYy+ehmnukSYQ134wbTTnHN xm7rn845tAsehDqKBvqIrlwJLJoYOB1DrO4uS+ESgtPg5zpcKhslbAFwhxf+fw/yyAUH girw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="n525T1/K"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y13-20020aa7c24d000000b00523d1aa2c0csi1601312edo.172.2023.08.31.13.43.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 13:43:45 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="n525T1/K"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5D8513858421 for ; Thu, 31 Aug 2023 20:43:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D8513858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693514623; bh=EZWcp3fUhzm+TK4RXyDqGXiejKNOswz82Sn9Lrmv8Ms=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=n525T1/KOrkuSX4nTf6dqHDjrmKp6jvq6UtrQ8Nt7pq2/MNI+J39UOD2VdtrM8ezU lJDFn75D1zPdI7Gr7Dt0CPnH96Ligm6DPGvv9op6WOE915FFvsmnCxRGwABn0gEySC +XispvUGITwqhl8IC8HnDg85EdopajC6rcBws8eg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id 4FBB23858D37; Thu, 31 Aug 2023 20:42:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4FBB23858D37 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [79.232.156.250] ([79.232.156.250]) by web-mail.gmx.net (3c-app-gmx-bap28.server.lan [172.19.172.98]) (via HTTP); Thu, 31 Aug 2023 22:42:55 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: runtime bounds-checking in presence of array constructors [PR31059] Date: Thu, 31 Aug 2023 22:42:55 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:LsdnHlCQJATi9Yxr7GIRNQEMp2YmZPugiJ4RVaJPFyuju/nFostAcobM7sPuF94JcC/nq OAxFwqhtToeSS7/Aovat21jwQSo0bNM3M6wpIizDfvUBl09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am-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775778791631904955 X-GMAIL-MSGID: 1775778791631904955 Dear all, gfortran's array bounds-checking code does a mostly reasonable job for array sections in expressions and assignments, but forgot the case that (rank-1) expressions can involve array constructors, which have a shape ;-) The attached patch walks over the loops generated by the scalarizer, checks for the presence of a constructor, and takes the first shape found as reference. (If several constructors are present, discrepancies in their shape seems to be already detected at compile time). For more details on what will be caught now see testcase. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 944a35909e8eeb79c92e398ae3f27e94708584e6 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Thu, 31 Aug 2023 22:19:58 +0200 Subject: [PATCH] Fortran: runtime bounds-checking in presence of array constructors [PR31059] gcc/fortran/ChangeLog: PR fortran/31059 * trans-array.cc (gfc_conv_ss_startstride): For array bounds checking, consider also array constructors in expressions, and use their shape. gcc/testsuite/ChangeLog: PR fortran/31059 * gfortran.dg/bounds_check_fail_5.f90: New test. --- gcc/fortran/trans-array.cc | 23 ++++++++++++++++ .../gfortran.dg/bounds_check_fail_5.f90 | 26 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/bounds_check_fail_5.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index 90a7d4e9aef..6ca58e98547 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -4740,6 +4740,29 @@ done: for (n = 0; n < loop->dimen; n++) size[n] = NULL_TREE; + /* If there is a constructor involved, derive size[] from its shape. */ + for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain) + { + gfc_ss_info *ss_info; + + ss_info = ss->info; + info = &ss_info->data.array; + + if (ss_info->type == GFC_SS_CONSTRUCTOR && info->shape) + { + for (n = 0; n < loop->dimen; n++) + { + if (size[n] == NULL) + { + gcc_assert (info->shape[n]); + size[n] = gfc_conv_mpz_to_tree (info->shape[n], + gfc_index_integer_kind); + } + } + break; + } + } + for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain) { stmtblock_t inner; diff --git a/gcc/testsuite/gfortran.dg/bounds_check_fail_5.f90 b/gcc/testsuite/gfortran.dg/bounds_check_fail_5.f90 new file mode 100644 index 00000000000..436cc96621d --- /dev/null +++ b/gcc/testsuite/gfortran.dg/bounds_check_fail_5.f90 @@ -0,0 +1,26 @@ +! { dg-do run } +! { dg-additional-options "-fcheck=bounds -g -fdump-tree-original" } +! { dg-output "At line 13 .*" } +! { dg-shouldfail "Array bound mismatch for dimension 1 of array 'ivec' (2/3)" } +! +! PR fortran/31059 - runtime bounds-checking in presence of array constructors + +program p + integer :: jvec(3) = [1,2,3] + integer, allocatable :: ivec(:), kvec(:), lvec(:), mvec(:), nvec(:) + ivec = [1,2] ! (re)allocation + kvec = [4,5,6] ! (re)allocation + ivec(:) = [4,5,6] ! runtime error (->dump) + ! not reached ... + print *, jvec + [1,2,3] ! OK & no check generated + print *, [4,5,6] + jvec ! OK & no check generated + print *, lvec + [1,2,3] ! check generated (->dump) + print *, [4,5,6] + mvec ! check generated (->dump) + nvec(:) = jvec ! check generated (->dump) +end + +! { dg-final { scan-tree-dump-times "Array bound mismatch " 4 "original" } } +! { dg-final { scan-tree-dump-times "Array bound mismatch .*ivec" 1 "original" } } +! { dg-final { scan-tree-dump-times "Array bound mismatch .*lvec" 1 "original" } } +! { dg-final { scan-tree-dump-times "Array bound mismatch .*mvec" 1 "original" } } +! { dg-final { scan-tree-dump-times "Array bound mismatch .*nvec" 1 "original" } } -- 2.35.3