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: 1828506 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=n/U++oAL; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RcCmY1SBPz1yfm for ; Fri, 1 Sep 2023 06:43:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 29592385840B for ; Thu, 31 Aug 2023 20:43:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29592385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693514600; bh=EZWcp3fUhzm+TK4RXyDqGXiejKNOswz82Sn9Lrmv8Ms=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=n/U++oALfZ9GTUPva04oGFXz7pbKfYx/9AaLUP7rumlI2vjEumYX09p43bK/Z8wZd PdFZJs45amJ1tobXdOavsEiYievsaTNiRHgrNip4uedkZq4llsmf1q890VEvDWaC7m YLSJCww0MGkvJgv+ieSuLp+EV5d21zXZIB49yBk8= 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/Aovat21jwQSo0bNM3M6wpIizDfvUBl09l0udT52a68osYfcmionpnGrBY/s9D aauWwj2MOjQ4uLw3KBG8xEn4/gR2b1SdylLBCRRjd9mt9fjcXQyW5LhS0OR79o1Cdxa4Bt48sr0y VquTZxLSbc/TvqWytpPmEcLyfOiyzNM5F+objJsdGvseDbJe6R4OnGS6nhLJmln76gfmGgT3fEga SM= UI-OutboundReport: notjunk:1;M01:P0:QtdXehkSwMU=;vO5YKqCkRuMk1Wfc4YYhoeWcAZF Y8Dmzhpcl5/ekTQg1LPYWNs87bJkpKis9z19ihZMKn6Z3QjUXjkoOLFHryQUmSEouoEaqPngB ib3mOMpl+uBjGaZzq8i9IhI582LpwnrPhCprtnU/i0fvBWFvovnz1IlVagbd8OdfICc77AwPM iknsntY+EFkkNYoj3Iscn2opdY8T3Z0t689/ELR5+xWT4c2P9mCjH5K2+TFcpJ4XkVC72Q/mn Y9MA51VYLzK/lA4jU2+Q68fhh+60XkLrAeYir8EiHBCA7UBRkZStey4pDt9heBAm0XAtiQMV4 G+TSBaY5J8J5t3Vh3evRKNUaDCSzIEUvJMlFK5YaVS359CIV3pOYCqqXhBYMGVxMe3vT3Evnh Oer/GAwvK1mM52aWeIpGBPNxxc2ig+bLd2VgWxatyHS9s0mP9CIcfEr1BnvM8nT7zNThHyWeE gaildFe4ybrcKcvFSEN7cpPKJ0+rAwBmq3ruEKh5m9eZL+AEup39ILhn88HsSiRR58kX7xgxR PEpRHBxDiwM43oLsscBOxc+6brGq1vi96GVmp2LoHqqoy4563rMDaIk+ZQo8VUy+iH85TPJwL nbJPAVXxy0Q+bxUpBDXw/TT1gXqWojZGcHe3QYzCIH9P7sXkoMpVjxgdqwj+rUPtb2kkN+xdI s3r7uZgBP3uA2VPKHfrF/x/Kf2LoH3jWXp1XuDH4Us87byjFFz9od5qJKln9oVLs/93/0dwZB cOLA00slrl4VZXOWZn7XPxS5dA9Newt9q5p+GjAlmI4+ppmZLDRdE0CWAxu0+wFqOxfTt0UMa CPqIsLAmY3KLH8fjuPZHP/AA== X-Spam-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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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