From patchwork Thu Sep 14 20:22:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 139820 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp604932vqi; Thu, 14 Sep 2023 13:23:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwbX1b/y3/F7XNywJZfOZ4Lv9h6VVo2EpbUGdTvP2YiB4GrnVIJWSByhhHjWQfuEP9zue4 X-Received: by 2002:a05:6512:3a91:b0:500:9796:fb6 with SMTP id q17-20020a0565123a9100b0050097960fb6mr6991642lfu.40.1694722988330; Thu, 14 Sep 2023 13:23:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694722988; cv=none; d=google.com; s=arc-20160816; b=ILtgkmjABqs/novMy/z4Oo/wFnIWDyR2VUsUmCXk32Qsnm+AKHvMAX2armNKlNv+zQ upbW2EspQHOJCvzZAWZvWgcC+Ym5YP91x1w4dVkiIobIKQEyyqUV4x6CTTViP64PMXG6 N3TIxJQ3r4Un/8wABxAf4gTi4yoJkxbQUl9azIiyhgt8tD1zq5yR6b5yZoniowJyxwEt XWYLB6BOt4MkthNFAvn+bPrd5+DmvPlifMXN2Sq9vjsJcn8YZzGJid0W3r5QYLyBUAs3 uFlsKV6TmMR30A7W9T4RLamp0puRVpYmFb5T4jXcLzOYufAUcIuavykLjlFdizXlnVGK tNsA== 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=qL6MA29AAnv4NMK0PCjY5IKwWethJloZ27GRU5XMM8c=; fh=+IEfvAe+9BRgPHWhQEl2uIBTtAiiGDh1ExRZeB5JJoc=; b=0KyV/zVOi3mBTSYOJbTNqBHA3GFok8JJ6xc70H8dM0wNo7TkGD7+jzRnrr2Ty5oNXr if8Bz6KIGkXFLLgeciuBLwMDy5p1Ep9TwtsE9jh+F16py3xeko420z44NENKzPLDtI4U 2+e2ajnB/p/arzDhSryeyUTJNGX/XU2kljvgCMyNeOlmPEhYcERTPFkyf0/7e0ubFGg0 5ZUk020rMD05UnOkT6kJkojiGwLRUV0PoezEqY0WFWelrVygS4lJm12yoZWwGcH8BWXS X3DxYy6kXB3lCkgsmfu2QqTPHoswhmyvjuiyr1izLYIM+yh7FXPQaYDrw8TpoZ7D+nmw GgHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Z5bLv1he; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w7-20020a170906130700b0099279210464si1925634ejb.420.2023.09.14.13.23.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 13:23:08 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Z5bLv1he; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 52A6D3858298 for ; Thu, 14 Sep 2023 20:23:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 52A6D3858298 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694722980; bh=qL6MA29AAnv4NMK0PCjY5IKwWethJloZ27GRU5XMM8c=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Z5bLv1heMkS8JiG5EZceuT6HSqK+32NLqRDLAxvzIO/rCZObPDfvYvcVx9wH+P7LT +cpDsJ7xJyQrYidnnjPNrKWfZjHQz3Q3JH64ZurHTWqK+InzVjwbM/Oh/ZYiXvDMjr RiYLf+XrP8EmKo26U+UVtpecnoI3L4AKIkn0m+8Y= 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.15.19]) by sourceware.org (Postfix) with ESMTPS id 9017A3858CDA; Thu, 14 Sep 2023 20:22:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9017A3858CDA X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.80.183] ([93.207.80.183]) by web-mail.gmx.net (3c-app-gmx-bs48.server.lan [172.19.170.101]) (via HTTP); Thu, 14 Sep 2023 22:22:14 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: improve bounds-checking for array sections [PR30802] Date: Thu, 14 Sep 2023 22:22:14 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:Chhqvux9JnESVjVQWAXZFGhZWSLpPUYsvo27PcOIL3LJccT8r5ELGGPtMtpt6U6IwwMXb XV+/FIEBpwdx+FhkH/gUErUXGUlqRdELcO8Vfqg2IcNy0+H2a+aT+76E+d6QLe7SfjL+K79xtzzp k0sYQRHxkaQS6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AYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, 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: 1777045852336661018 X-GMAIL-MSGID: 1777045852336661018 Dear all, array bounds checking was missing a few cases of array sections that are handled via gfc_conv_expr_descriptor. Bounds checking was done for the dimensions with ranges, but not for elemental dimensions. The attached patch implements that and fixes pr30802 and also pr97039, maybe a few more similar cases. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From bb2a765f56b440c8d086329f55c8ff0eaee2b97d Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Thu, 14 Sep 2023 22:08:26 +0200 Subject: [PATCH] Fortran: improve bounds-checking for array sections [PR30802] gcc/fortran/ChangeLog: PR fortran/30802 * trans-array.cc (trans_array_bound_check): Add optional argument COMPNAME for explicit specification of array component name. (array_bound_check_elemental): Helper function for generating bounds-checking code for elemental dimensions. (gfc_conv_expr_descriptor): Use bounds-checking also for elemental dimensions, i.e. those not handled by the scalarizer. gcc/testsuite/ChangeLog: PR fortran/30802 * gfortran.dg/bounds_check_fail_6.f90: New test. --- gcc/fortran/trans-array.cc | 72 ++++++++++++++++++- .../gfortran.dg/bounds_check_fail_6.f90 | 29 ++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/bounds_check_fail_6.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index 6ca58e98547..71123e37477 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -3452,7 +3452,8 @@ gfc_conv_array_ubound (tree descriptor, int dim) static tree trans_array_bound_check (gfc_se * se, gfc_ss *ss, tree index, int n, - locus * where, bool check_upper) + locus * where, bool check_upper, + const char *compname = NULL) { tree fault; tree tmp_lo, tmp_up; @@ -3474,6 +3475,10 @@ trans_array_bound_check (gfc_se * se, gfc_ss *ss, tree index, int n, if (VAR_P (descriptor)) name = IDENTIFIER_POINTER (DECL_NAME (descriptor)); + /* Use given (array component) name. */ + if (compname) + name = compname; + /* If upper bound is present, include both bounds in the error message. */ if (check_upper) { @@ -3524,6 +3529,67 @@ trans_array_bound_check (gfc_se * se, gfc_ss *ss, tree index, int n, } +/* Generate code for bounds checking for elemental dimensions. */ + +static void +array_bound_check_elemental (gfc_se * se, gfc_ss * ss, gfc_expr * expr) +{ + gfc_array_ref *ar; + gfc_ref *ref; + gfc_symbol *sym; + char *var_name = NULL; + size_t len; + int dim; + + if (!(gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)) + return; + + if (expr->expr_type == EXPR_VARIABLE) + { + sym = expr->symtree->n.sym; + len = strlen (sym->name) + 1; + + for (ref = expr->ref; ref; ref = ref->next) + if (ref->type == REF_COMPONENT) + len += 2 + strlen (ref->u.c.component->name); + + var_name = XALLOCAVEC (char, len); + strcpy (var_name, sym->name); + + for (ref = expr->ref; ref; ref = ref->next) + { + /* Append component name. */ + if (ref->type == REF_COMPONENT) + { + strcat (var_name, "%%"); + strcat (var_name, ref->u.c.component->name); + continue; + } + + if (ref->type == REF_ARRAY && ref->u.ar.dimen > 0) + { + ar = &ref->u.ar; + for (dim = 0; dim < ar->dimen; dim++) + { + if (ar->dimen_type[dim] == DIMEN_ELEMENT) + { + gfc_se indexse; + gfc_init_se (&indexse, NULL); + gfc_conv_expr_type (&indexse, ar->start[dim], + gfc_array_index_type); + trans_array_bound_check (se, ss, indexse.expr, dim, + &ar->where, + ar->as->type != AS_ASSUMED_SIZE + || dim < ar->dimen - 1, + var_name); + } + } + } + } + } +} + + /* Return the offset for an index. Performs bound checking for elemental dimensions. Single element references are processed separately. DIM is the array dimension, I is the loop dimension. */ @@ -7823,6 +7889,10 @@ gfc_conv_expr_descriptor (gfc_se *se, gfc_expr *expr) /* Setup the scalarizing loops and bounds. */ gfc_conv_ss_startstride (&loop); + /* Add bounds-checking for elemental dimensions. */ + if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) && !expr->no_bounds_check) + array_bound_check_elemental (se, ss, expr); + if (need_tmp) { if (expr->ts.type == BT_CHARACTER diff --git a/gcc/testsuite/gfortran.dg/bounds_check_fail_6.f90 b/gcc/testsuite/gfortran.dg/bounds_check_fail_6.f90 new file mode 100644 index 00000000000..90329131158 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/bounds_check_fail_6.f90 @@ -0,0 +1,29 @@ +! { dg-do run } +! { dg-additional-options "-fcheck=bounds -g -fdump-tree-original" } +! { dg-output "At line 18 .*" } +! { dg-shouldfail "dimension 3 of array 'u%z' outside of expected range" } +! +! PR fortran/30802 - improve bounds-checking for array sections + +program test + implicit none + integer :: k = 0 + integer, dimension(10,20,30) :: x = 42 + type t + real, dimension(10,20,30) :: z = 23 + end type t + type(t) :: u + + ! pr30802 + print *, u% z(1,:,k) ! runtime check only for dimension 3 + + ! pr97039 + call foo (x(k,:,k+1)) ! runtime checks for dimensions 1,3 +contains + subroutine foo (a) + integer, intent(in) :: a(:) + end subroutine foo +end program test + +! { dg-final { scan-tree-dump-times "'u%%z.' outside of expected range" 2 "original" } } +! { dg-final { scan-tree-dump-times "'x.' outside of expected range" 4 "original" } } -- 2.35.3