From patchwork Sat Feb 11 00:50:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 55663 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1260175wrn; Fri, 10 Feb 2023 16:51:36 -0800 (PST) X-Google-Smtp-Source: AK7set/HuYGMH6lPBLMXakPiDCUSXw28IKHSP18jh1gl4yGFM+Xqgo78Lizhhrlu+gvWmeUSFIg3 X-Received: by 2002:a50:cd44:0:b0:4aa:a4ea:cdc9 with SMTP id d4-20020a50cd44000000b004aaa4eacdc9mr22272393edj.16.1676076696828; Fri, 10 Feb 2023 16:51:36 -0800 (PST) Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bm2-20020a0564020b0200b004a2657551e4si6596275edb.287.2023.02.10.16.51.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Feb 2023 16:51:36 -0800 (PST) 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=bP4d5OpX; arc=fail (signature failed); 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 811C43858000 for ; Sat, 11 Feb 2023 00:51:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 811C43858000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676076695; bh=1oKS9t8b2ailpllC9KQi9JdASzX+GrxpUjWZYvl1YDU=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=bP4d5OpX5yc1yPxizgP3WEOzyBMhTE3hKRAs0lnBykbdMwfASKtvg426umnfDXTeO 04g5LB/boi2jjbzmH2SpHTGEUcdhX/KFPwblwpxMF76PKAOGPN++mSLmyzXOSvnN4e iXdlhpofnvuw0emMMWTtqAM/L9ECgz+eNQaGuDiM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 5DEA7385840C for ; Sat, 11 Feb 2023 00:50:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5DEA7385840C Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31B0OhM6012147; Sat, 11 Feb 2023 00:50:26 GMT Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3nhdy1ep5t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 11 Feb 2023 00:50:25 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 31AM5hQO036007; Sat, 11 Feb 2023 00:50:24 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2107.outbound.protection.outlook.com [104.47.58.107]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3nhdtha14e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 11 Feb 2023 00:50:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E3nr0FKzwT9hQprepKOz0atuPnK/v1zUFk4Vgeg62yDi8mm4n2btaBB5zWuAGEiyjZifpWQXoWJGt7eLodZlnPzxz7UN0MgSbPmxCtnhI1YbG1/jscUR+A9hf+1elVaOqa8anBg+Wo+XvxnwlJZNZw5432o6Azb/bZAlgH+Nz5W5ISFF849cuR+jEb1n+2MFqOq6HzxLmykKfj6bB6yHPcG8RE2j/fVJXXp33hJ+uoM3KCo2cyFbRpU3GlJM9U0dpfgTJR2jELWqzVOG9QQjdXh9mICAkyED/IT1Pm03cIpGNST+WLp439wCrN8kVqtiBvbt8VKykKfgKtr4OVcs0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1oKS9t8b2ailpllC9KQi9JdASzX+GrxpUjWZYvl1YDU=; b=ad5D8KcMjAkR9pxk89VeAhkWWk9xkKeXTZotmKxQ6EPGdK1d/VQ5QLm+FAvJfGUHADSKv2we1s061WDOiDGKrbDYukGnQGtZctKklWbueK53yYchjUdE3o4LSuxrYLFjzQoWcBy2335xgzLIiYap1Iw37Wf881qWqYEhP9V1EqaSNDotLXzD+pv3LY4/RcHdOdFcrFu4YZX4fP8r4Q++A4/B9VjU0LXyoqDSq4uP0y1t8fM9dTx7v9iZlnwtlwTxfzgKP98DEm4IEMuSpFMS3LRhYrI8TLsmnVPrxwna4uIs6/el5bV7JJ37E9fNVN1yuHsMtvHK9dv79le8z7xocw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by DS0PR10MB6176.namprd10.prod.outlook.com (2603:10b6:8:c0::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.7; Sat, 11 Feb 2023 00:50:22 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%5]) with mapi id 15.20.6111.007; Sat, 11 Feb 2023 00:50:22 +0000 To: joseph@codesourcery.com, rguenther@suse.de Cc: siddhesh@gotplt.org, keescook@chromium.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [v3][PATCH 1/2] Handle component_ref to a structre/union field including C99 FAM [PR101832] Date: Sat, 11 Feb 2023 00:50:12 +0000 Message-Id: <20230211005013.789161-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230211005013.789161-1-qing.zhao@oracle.com> References: <20230211005013.789161-1-qing.zhao@oracle.com> X-ClientProxiedBy: SA0PR11CA0071.namprd11.prod.outlook.com (2603:10b6:806:d2::16) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|DS0PR10MB6176:EE_ X-MS-Office365-Filtering-Correlation-Id: e67523b4-3ba8-40f7-9b6f-08db0bc9f485 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: i9tC8q4sFiKPMi48UFe4OsymiuCt9kjcAjhyNNMvOKxA19fIt7yYBhCUvLdJqn97MFKi2JYDMwuPrPEUYtv8miDPXkR4m2il40NVOSDuEW1ssfZl6uD6oqc8V3WlPUVCwX9fKUGfFn7Yn2Ldpsy8KmjCTKrqbUUMve64X229h2M+92QmwP3TuXXsLFVjgvq2uKx2H5r2HrFhTtmQaPIrBhZ1VeZSkNR+K9T8TM1sqpyDWd7Y5vi4f8oHbPe7CrNTr3+lPVJXRNOjQagYqoVc1n0NlBxjlkFBjbDmNFdERNqHpJ4GoDtxH1odnpUTP85V6vNy6PWp/N/pfEmu0N3mNOTPOZ6Edv3LbwfSZPCvRTj8D65c38l9wIUz2NeKJQL90i/ZLJgehLH/BxI09N6MWNR0dnOPY4jw3cQ3a3vpPcG0UebdPJRUeHeozOptBj5QqNYpIcbirXty89wwelEENfwvBzUpK2C4OuUFhZCElQsx2z4sefx9GBR6hNDXyMQxxrT5WQFydkfFwgWqj326Eb5Dbo2aOCTzeKOOAViXpGpUREpuhlRhxrLvwP/8A90CowI7SiJ5GORjo291utX6Kn/95RexkeN4WnH3Bdtd2WwOSX2tDH1/SCjT6/red1h7O7CEL7VDYi1g0cg9iWb4tmGxj1F7lpI2e9jfFY/GDQg= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR10MB4344.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(366004)(39860400002)(136003)(346002)(396003)(376002)(451199018)(86362001)(8676002)(36756003)(30864003)(44832011)(8936002)(41300700001)(5660300002)(84970400001)(4326008)(66476007)(66556008)(66946007)(38100700002)(316002)(83380400001)(6486002)(478600001)(2616005)(2906002)(26005)(1076003)(186003)(107886003)(6666004)(6512007)(6506007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Bl1wbqPNyp9M2z5HgsRrGpKvLtOWL/Xlypgcje4/skKUmgqcy+GtG1fyhlvv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T1Cxpjntenzm9+vcUufU709g77QBsBK/SXrRRagyFnPeDeDEdWdAwVzcytYt7I+8v/+u4gllmxto6w4w1Cf+unmxxV+bsHlL/Ans2/Dzo8pL1NubRFxRmHJLUenfxK3t7a5El3ZUH/ZpV+4Il8HdLFlnGCTiN5bQMkDQJJ/aRxnjJFxd/iJnD5FnhhYZwkxJryL0JQygwXkp4nO4z/tdwjotwyC7e6hG6ISEZusrGzhyNW6x3eCbeE1Z4kTk6NdYGQWjMs8yNHXeSKkb8FIA+h8WywH83YczsYAs+XDJRN3uKL+fHG2voK+vSAdVuTicUzvqxS1FRCoPL3i6r/v7/73foh7QqevO8w4bT4licUCXGJNv0PKdY X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e67523b4-3ba8-40f7-9b6f-08db0bc9f485 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Feb 2023 00:50:22.3660 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: CIn1zjne3vrGk2dugl4UXyC8loxULndHWecMJad1+FLiwfOAyLoahzf44oapuxsQV4PJGS0nizLj6KOzT20wpQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6176 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-10_17,2023-02-09_03,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 adultscore=0 malwarescore=0 mlxscore=0 phishscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302110004 X-Proofpoint-GUID: Eu_3yNVhxR2DahJiOrNcvZtTV9EZwT3d X-Proofpoint-ORIG-GUID: Eu_3yNVhxR2DahJiOrNcvZtTV9EZwT3d X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Qing Zhao via Gcc-patches From: Qing Zhao Reply-To: Qing Zhao Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757493798669838225?= X-GMAIL-MSGID: =?utf-8?q?1757493798669838225?= GCC extension accepts the case when a struct with a C99 flexible array member is embedded into another struct or union (possibly recursively). __builtin_object_size should treat such struct as flexible size. gcc/c/ChangeLog: PR tree-optimization/101832 * c-decl.cc (finish_struct): Set TYPE_INCLUDE_FLEXARRAY for struct/union type. gcc/cp/ChangeLog: PR tree-optimization/101832 * module.cc (trees_out::core_bools): Stream out new bit type_include_flexarray. (trees_in::core_bools): Stream in new bit type_include_flexarray. gcc/ChangeLog: PR tree-optimization/101832 * print-tree.cc (print_node): Print new bit type_include_flexarray. * tree-core.h (struct tree_type_common): New bit type_include_flexarray. * tree-object-size.cc (addr_object_size): Handle structure/union type when it has flexible size. * tree-streamer-in.cc (unpack_ts_type_common_value_fields): Stream in new bit type_include_flexarray. * tree-streamer-out.cc (pack_ts_type_common_value_fields): Stream out new bit type_include_flexarray. * tree.h (TYPE_INCLUDE_FLEXARRAY): New macro TYPE_INCLUDE_FLEXARRAY. gcc/testsuite/ChangeLog: PR tree-optimization/101832 * gcc.dg/builtin-object-size-pr101832.c: New test. --- gcc/c/c-decl.cc | 12 ++ gcc/cp/module.cc | 2 + gcc/print-tree.cc | 5 + .../gcc.dg/builtin-object-size-pr101832.c | 134 ++++++++++++++++++ gcc/tree-core.h | 4 +- gcc/tree-object-size.cc | 79 +++++++---- gcc/tree-streamer-in.cc | 1 + gcc/tree-streamer-out.cc | 1 + gcc/tree.h | 6 + 9 files changed, 215 insertions(+), 29 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 20e7d1855bf..741a37560b0 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9277,6 +9277,18 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */ DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x); + /* Set TYPE_INCLUDE_FLEXARRAY for the context of x, t + * when x is an array. */ + if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) + TYPE_INCLUDE_FLEXARRAY (t) = flexible_array_member_type_p (TREE_TYPE (x)) ; + /* Recursively set TYPE_INCLUDE_FLEXARRAY for the context of x, t + when x is the last field. */ + else if ((TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE + || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE) + && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x)) + && is_last_field) + TYPE_INCLUDE_FLEXARRAY (t) = true; + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field = true; diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index ac2fe66b080..c750361b704 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -5371,6 +5371,7 @@ trees_out::core_bools (tree t) WB (t->type_common.lang_flag_5); WB (t->type_common.lang_flag_6); WB (t->type_common.typeless_storage); + WB (t->type_common.type_include_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON)) @@ -5551,6 +5552,7 @@ trees_in::core_bools (tree t) RB (t->type_common.lang_flag_5); RB (t->type_common.lang_flag_6); RB (t->type_common.typeless_storage); + RB (t->type_common.type_include_flexarray); } if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON)) diff --git a/gcc/print-tree.cc b/gcc/print-tree.cc index 1f3afcbbc86..efacdb7686f 100644 --- a/gcc/print-tree.cc +++ b/gcc/print-tree.cc @@ -631,6 +631,11 @@ print_node (FILE *file, const char *prefix, tree node, int indent, && TYPE_CXX_ODR_P (node)) fputs (" cxx-odr-p", file); + if ((code == RECORD_TYPE + || code == UNION_TYPE) + && TYPE_INCLUDE_FLEXARRAY (node)) + fputs (" include-flexarray", file); + /* The transparent-union flag is used for different things in different nodes. */ if ((code == UNION_TYPE || code == RECORD_TYPE) diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c new file mode 100644 index 00000000000..60078e11634 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-object-size-pr101832.c @@ -0,0 +1,134 @@ +/* PR 101832: + GCC extension accepts the case when a struct with a C99 flexible array + member is embedded into another struct (possibly recursively). + __builtin_object_size will treat such struct as flexible size. + However, when a structure with non-C99 flexible array member, i.e, trailing + [0], [1], or [4], is embedded into anther struct, the stucture will not + be treated as flexible size. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + __builtin_printf ("ok: %s == %zd\n", #p, p); \ + else {\ + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + FAIL (); \ + } \ +} while (0); + + +struct A { + int n; + char data[]; +}; + +struct B { + int m; + struct A a; +}; + +struct C { + int q; + struct B b; +}; + +struct A0 { + int n; + char data[0]; +}; + +struct B0 { + int m; + struct A0 a; +}; + +struct C0 { + int q; + struct B0 b; +}; + +struct A1 { + int n; + char data[1]; +}; + +struct B1 { + int m; + struct A1 a; +}; + +struct C1 { + int q; + struct B1 b; +}; + +struct An { + int n; + char data[8]; +}; + +struct Bn { + int m; + struct An a; +}; + +struct Cn { + int q; + struct Bn b; +}; + +volatile void *magic1, *magic2; + +int main (int argc, char *argv[]) +{ + struct B *outer; + struct C *outest; + + /* Make sure optimization can't find some other object size. */ + outer = (void *)magic1; + outest = (void *)magic2; + + expect (__builtin_object_size (&outer->a, 1), -1); + expect (__builtin_object_size (&outest->b, 1), -1); + expect (__builtin_object_size (&outest->b.a, 1), -1); + + struct B0 *outer0; + struct C0 *outest0; + + /* Make sure optimization can't find some other object size. */ + outer0 = (void *)magic1; + outest0 = (void *)magic2; + + expect (__builtin_object_size (&outer0->a, 1), sizeof (outer0->a)); + expect (__builtin_object_size (&outest0->b, 1), sizeof (outest0->b)); + expect (__builtin_object_size (&outest0->b.a, 1), sizeof (outest0->b.a)); + + struct B1 *outer1; + struct C1 *outest1; + + /* Make sure optimization can't find some other object size. */ + outer1 = (void *)magic1; + outest1 = (void *)magic2; + + expect (__builtin_object_size (&outer1->a, 1), sizeof (outer1->a)); + expect (__builtin_object_size (&outest1->b, 1), sizeof (outest1->b)); + expect (__builtin_object_size (&outest1->b.a, 1), sizeof (outest1->b.a)); + + struct Bn *outern; + struct Cn *outestn; + + /* Make sure optimization can't find some other object size. */ + outern = (void *)magic1; + outestn = (void *)magic2; + + expect (__builtin_object_size (&outern->a, 1), sizeof (outern->a)); + expect (__builtin_object_size (&outestn->b, 1), sizeof (outestn->b)); + expect (__builtin_object_size (&outestn->b.a, 1), sizeof (outestn->b.a)); + + DONE (); + return 0; +} diff --git a/gcc/tree-core.h b/gcc/tree-core.h index acd8deea34e..705d5702b9c 100644 --- a/gcc/tree-core.h +++ b/gcc/tree-core.h @@ -1718,7 +1718,9 @@ struct GTY(()) tree_type_common { unsigned empty_flag : 1; unsigned indivisible_p : 1; unsigned no_named_args_stdarg_p : 1; - unsigned spare : 15; + /* TYPE_INCLUDE_FLEXARRAY flag for RECORD_TYPE and UNION_TYPE. */ + unsigned int type_include_flexarray : 1; + unsigned spare : 14; alias_set_type alias_set; tree pointer_to; diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9a936a91983..22b3c72ea6e 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -633,45 +633,68 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, v = NULL_TREE; break; case COMPONENT_REF: - if (TREE_CODE (TREE_TYPE (v)) != ARRAY_TYPE) + /* When the ref is not to an array, a record or a union, it + will not have flexible size, compute the object size + directly. */ + if ((TREE_CODE (TREE_TYPE (v)) != ARRAY_TYPE) + && (TREE_CODE (TREE_TYPE (v)) != RECORD_TYPE) + && (TREE_CODE (TREE_TYPE (v)) != UNION_TYPE)) { v = NULL_TREE; break; } - is_flexible_array_mem_ref = array_ref_flexible_size_p (v); - while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) - if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != UNION_TYPE - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != QUAL_UNION_TYPE) - break; - else - v = TREE_OPERAND (v, 0); - if (TREE_CODE (v) == COMPONENT_REF - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - == RECORD_TYPE) + if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE + || TREE_CODE (TREE_TYPE (v)) == UNION_TYPE) + /* if the record or union does not include a flexible array + recursively, compute the object size directly. */ { - /* compute object size only if v is not a - flexible array member. */ - if (!is_flexible_array_mem_ref) + if (!TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (v))) { v = NULL_TREE; break; } - v = TREE_OPERAND (v, 0); + else + v = TREE_OPERAND (v, 0); } - while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) - if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != UNION_TYPE - && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) - != QUAL_UNION_TYPE) - break; - else - v = TREE_OPERAND (v, 0); - if (v != pt_var) - v = NULL_TREE; else - v = pt_var; + { + /* Now the ref is to an array type. */ + is_flexible_array_mem_ref + = array_ref_flexible_size_p (v); + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != UNION_TYPE + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != QUAL_UNION_TYPE) + break; + else + v = TREE_OPERAND (v, 0); + if (TREE_CODE (v) == COMPONENT_REF + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + == RECORD_TYPE) + { + /* compute object size only if v is not a + flexible array member. */ + if (!is_flexible_array_mem_ref) + { + v = NULL_TREE; + break; + } + v = TREE_OPERAND (v, 0); + } + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != UNION_TYPE + && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) + != QUAL_UNION_TYPE) + break; + else + v = TREE_OPERAND (v, 0); + if (v != pt_var) + v = NULL_TREE; + else + v = pt_var; + } break; default: v = pt_var; diff --git a/gcc/tree-streamer-in.cc b/gcc/tree-streamer-in.cc index d4dc30f048f..c19ede0631d 100644 --- a/gcc/tree-streamer-in.cc +++ b/gcc/tree-streamer-in.cc @@ -390,6 +390,7 @@ unpack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) TYPE_TRANSPARENT_AGGR (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_FINAL_P (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_CXX_ODR_P (expr) = (unsigned) bp_unpack_value (bp, 1); + TYPE_INCLUDE_FLEXARRAY (expr) = (unsigned) bp_unpack_value (bp, 1); } else if (TREE_CODE (expr) == ARRAY_TYPE) TYPE_NONALIASED_COMPONENT (expr) = (unsigned) bp_unpack_value (bp, 1); diff --git a/gcc/tree-streamer-out.cc b/gcc/tree-streamer-out.cc index d107229da5c..73e4b4e547c 100644 --- a/gcc/tree-streamer-out.cc +++ b/gcc/tree-streamer-out.cc @@ -357,6 +357,7 @@ pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) bp_pack_value (bp, flag_wpa && TYPE_CANONICAL (expr) ? TYPE_CXX_ODR_P (TYPE_CANONICAL (expr)) : TYPE_CXX_ODR_P (expr), 1); + bp_pack_value (bp, TYPE_INCLUDE_FLEXARRAY (expr), 1); } else if (TREE_CODE (expr) == ARRAY_TYPE) bp_pack_value (bp, TYPE_NONALIASED_COMPONENT (expr), 1); diff --git a/gcc/tree.h b/gcc/tree.h index c656cd5b7bf..a27b9bb43bc 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -778,6 +778,12 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, #define TYPE_NO_NAMED_ARGS_STDARG_P(NODE) \ (TYPE_CHECK (NODE)->type_common.no_named_args_stdarg_p) +/* True if this RECORD_TYPE or UNION_TYPE includes a flexible array member + at the last field recursively. */ +#define TYPE_INCLUDE_FLEXARRAY(NODE) \ + (TYPE_CHECK (NODE)->type_common.type_include_flexarray) + + /* In an IDENTIFIER_NODE, this means that assemble_name was called with this string as an argument. */ #define TREE_SYMBOL_REFERENCED(NODE) \ From patchwork Sat Feb 11 00:50:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 55664 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1260491wrn; Fri, 10 Feb 2023 16:52:42 -0800 (PST) X-Google-Smtp-Source: AK7set9OtJaxjna9MpbaKKlH9JVlRb3zLw5Cs/AyRKGW/gXEyOeh7LQFFZaILZvMJix8Uh2be3DN X-Received: by 2002:a50:bb08:0:b0:4aa:b409:77aa with SMTP id y8-20020a50bb08000000b004aab40977aamr4010503ede.34.1676076762442; Fri, 10 Feb 2023 16:52:42 -0800 (PST) Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id l6-20020aa7c3c6000000b004ab250bced9si5202250edr.635.2023.02.10.16.52.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Feb 2023 16:52:42 -0800 (PST) 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="Pyn3ef/7"; arc=fail (signature failed); 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 415C0385841A for ; Sat, 11 Feb 2023 00:52:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 415C0385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676076761; bh=SNbA6qj+TAiFvaLAfRhaymPb4qFnVyLkPHkNsrfdqAY=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Pyn3ef/7AaMUfspkxA4UGTWvyVyRZeOtSc/K04T+8Ac0zPgHpKlBWsTGBVwAq8uWA 2T8ecTYnxEH8Ai5Hq1KwxsEuFnDfVocJALru8st7/B7iZmrVjl6z0CDXX7BfHA+GVm C63yLUfXKjbKMltZqs/lURWVpIebcV4fSxff/m08= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id E1C8B385840D for ; Sat, 11 Feb 2023 00:50:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E1C8B385840D Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31B0OGQ3011924; Sat, 11 Feb 2023 00:50:29 GMT Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3nhdy1ep5v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 11 Feb 2023 00:50:29 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 31AMGWap036215; Sat, 11 Feb 2023 00:50:27 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2107.outbound.protection.outlook.com [104.47.58.107]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3nhdtha15s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 11 Feb 2023 00:50:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GB7lxWwhTHt9bwBWxfPeJKtkqf+Z6bdtDtwqWy1jy/8VmtZw8fScfRTxdp3C19BRCzwo6zgtln+2W1u7DC59poXU86Ksb2mRSIZz+LrV+jLUJUB9u8V4ekvtpkgZIMqrhS7Zzfjyn/7IP5rLDIX9y8aYKEkrGmt2CatwIgeaWh8znxYXVdj9T7Bw0UhK4kmw4NFC0kU4aA+/M9tBptL07+9bgawGzmedxOx4PSc4hEWbo5p8T5WNPZEJUGU8dexFY+OfG6iydRV+gHWdi5Jvxy/3thkKwI5sbFXW1EOU4o6CJ1s2c2rTXdheSepyWW9cRiJ3GZSw1LwlV/BtxFkYGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SNbA6qj+TAiFvaLAfRhaymPb4qFnVyLkPHkNsrfdqAY=; b=KauQKGcPwjDdo2wF2CY0ZhdKH8+cTfL/hQeKd0Q96tn5AnyoeqEDZMLBHZUrswyj7M8E5IkgaaNZ8DHAlasm9UvwK1v9yWTaXIpEjvC50RbdSyWy+ODyhRe1qLrWwXPN+lJ5GI8xw+B8gk9xaGTiS4bQIddx7O4eKkBCYUyIAQ7OgjrrdzRd+jxJN+4c+TJVEVhIGprixAKPpqEWjBkLccIAw6LKLXzaGiWtwbqn1MizXTU1KKLhD06ucR/hGU6sViBOmkwiUKtxQeQAg4VXOZMvNhnMxD2LbC+Lh0D02ZujaLsToSdMSHTwjZLKb42s+e+2vDwv557fdc29kvx2qw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by DS0PR10MB6176.namprd10.prod.outlook.com (2603:10b6:8:c0::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.7; Sat, 11 Feb 2023 00:50:25 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7030:ed2e:20d0:cd5b%5]) with mapi id 15.20.6111.007; Sat, 11 Feb 2023 00:50:25 +0000 To: joseph@codesourcery.com, rguenther@suse.de Cc: siddhesh@gotplt.org, keescook@chromium.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [v3][PATCH 2/2] Update documentation to clarify a GCC extension (PR77650) Date: Sat, 11 Feb 2023 00:50:13 +0000 Message-Id: <20230211005013.789161-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230211005013.789161-1-qing.zhao@oracle.com> References: <20230211005013.789161-1-qing.zhao@oracle.com> X-ClientProxiedBy: SN7P222CA0009.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:124::34) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|DS0PR10MB6176:EE_ X-MS-Office365-Filtering-Correlation-Id: 3da5f4c4-cbf0-416f-44d0-08db0bc9f68b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bIlbZLMhHt020KofNZtQSlex/JSBC3lBN8MkEuN22G/CiduC/jAoUIzBwyQvxwjxTsDWfFITSv+KCuvSyFhFreqymEL4BHX7vswbkaVFtxBPFLT9I5H9lSokjwsP9SmHahpKmoxrDPDjcI3oSPMfaiRpP/MSKAltCLQvW/q98abDY4BJlQxJdQs4OeK7l5M9v4O6KNG0818Vkbe7SMwc+fae17Akdk4OUQZnOiyS7HQceOL1ctdKPSKexK3GYDaDq8186cl069ihnIXcPMIqrz5IM1K+dVHQvAOVX7Oi50z1+0p3Gz4d52hGvpZflRsl3hxn3AhI9Lplkr8mi/xb61Z8SMSI7F1SB0Aud1TvugxyJ4UnLk/bQ86/TRHOMLNKNvTxkzWFnXb2BdlohPbQt5p9Fo2XnX7KzIrA/QaYVZIJR2iIZuCHRbWi4ekuGoBQmthpP5z512hvS+Qr1i7tzswNR0mMwHgBjRt5mn/0OewargVTa7W1GDvc6T0xcqbuzEl1v7o4uv2L3jVBK2JNf1qydowD+yWrr6zFGDf2C8KfplmTt02w50CUb0CMGU6UztkuJthDW2rMy0dyplLsBUM3oWMivNEymUt/id8Tm/86cJGjPgkMy3Y1NKWvK+3JSN+3FGG+5dL+gOIn5a9sDuypflJSk3NC4CyZnyyGMDE= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR10MB4344.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(366004)(39860400002)(136003)(346002)(396003)(376002)(451199018)(86362001)(8676002)(36756003)(44832011)(8936002)(41300700001)(5660300002)(84970400001)(4326008)(66476007)(66556008)(66946007)(38100700002)(316002)(83380400001)(6486002)(478600001)(2616005)(2906002)(26005)(1076003)(186003)(107886003)(6666004)(6512007)(6506007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5tGCqh5s0IKPtT4lED8SrEwP0OtAFUZidIr5zYNk81h7ABTxOTJS/4xxPt/qzU1ro6B1rnyKB8UnnpcxQFYEFbWudP6s6JnQb8d21zL0KjMnxcIILtbgfYmj1HX18+cM6QRGRuDC1DBUBMblxRBmwMsNVm/wTtFfa213y8M4g9EislgUMcjd2L71z8u9YW4OOycK3yAAyeyyE81BjVAZbTt+BIJ8VMoqqPPBtWahorv7DJx0MMYkHrmnWHW4gJ1CLc/DOx82ZT/iu+gG+8YTMwEa+2fyF5jTgy6FNkzxGw8niA1ajMJTAxM95dNTrA3j4IxrAtsELse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5E7/bM0uU3pnEfITaCddzgGHiLaFeE89/rsO5xYwf4W6ROpEHINZ4AVrpnTbOoBdVO+zjNKgV2TVTes86GC//d2g X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3da5f4c4-cbf0-416f-44d0-08db0bc9f68b X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Feb 2023 00:50:25.7407 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 7s8eG/Xb2qcUQ55D9cDkZrlszPA50ybSb7qEeChvM8yAVaSkdMea/VofTbSGLEGLOawTGdIsL8yZJpTLcXQMqg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6176 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-10_17,2023-02-09_03,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 adultscore=0 malwarescore=0 mlxscore=0 phishscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302110004 X-Proofpoint-GUID: C-kL9A_nBQoYbYMJRZywnfLY5nTy6nTc X-Proofpoint-ORIG-GUID: C-kL9A_nBQoYbYMJRZywnfLY5nTy6nTc X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Qing Zhao via Gcc-patches From: Qing Zhao Reply-To: Qing Zhao Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757493867087615351?= X-GMAIL-MSGID: =?utf-8?q?1757493867087615351?= on structure with C99 flexible array member being nested in another structure. This is also fixed PR77650. " GCC extension accepts a structure containing a ISO C99 "flexible array member", or a union containing such a structure (possibly recursively) to be a member of a structure. There are three situations: * The structure with a C99 flexible array member is the last field of another structure, for example: struct flex { int length; char data[]; }; struct out_flex { int m; struct flex flex_data; }; In the above, 'flex_data.data[]' is considered as a flexible array too. * The structure with a C99 flexible array member is the field of another union, for example: struct flex1 { int length1; char data1[]; } struct flex2 { int length2; char data2[]; } union out_flex { struct flex1 flex_data1; struct flex2 flex_data2; } In the above, 'flex_data1.data1[]' or 'flex_data2.data2[]' is considered as flexible arrays too. * The structure with a C99 flexible array member is the middle field of another structure, for example: struct flex { int length; char data[]; }; struct mid_flex { int m; struct flex flex_data; int n; }; In the above, 'flex_data.data[]' is allowed to be extended flexibly to the padding. E.g, up to 4 elements. However, relying on space in struct padding is a bad programming practice, compilers do not handle such extension consistently, Any code relying on this behavior should be modified to ensure that flexible array members only end up at the ends of structures. Please use warning option '-Wgnu-variable-sized-type-not-at-end' to identify all such cases in the source code and modify them. This extension will be deprecated from gcc in the next release. " gcc/c-family/ChangeLog: PR c/77650 * c.opt: New option -Wgnu-variable-sized-type-not-at-end. gcc/c/ChangeLog: PR c/77650 * c-decl.cc (finish_struct): Issue warnings for new option. gcc/ChangeLog: PR c/77650 * doc/extend.texi: Document GCC extension on a structure containing a flexible array member to be a member of another structure. gcc/testsuite/ChangeLog: PR c/77650 * gcc.dg/variable-sized-type-flex-array.c: New test. --- gcc/c-family/c.opt | 5 ++ gcc/c/c-decl.cc | 7 +++ gcc/doc/extend.texi | 58 ++++++++++++++++++- .../gcc.dg/variable-sized-type-flex-array.c | 31 ++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index c0fea56a8f5..fd720538800 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -737,6 +737,11 @@ Wformat-truncation= C ObjC C++ LTO ObjC++ Joined RejectNegative UInteger Var(warn_format_trunc) Warning LangEnabledBy(C ObjC C++ LTO ObjC++,Wformat=, warn_format >= 1, 0) IntegerRange(0, 2) Warn about calls to snprintf and similar functions that truncate output. +Wgnu-variable-sized-type-not-at-end +C C++ Var(warn_variable_sized_type_not_at_end) Warning +Warn about structures or unions with C99 flexible array members are not +at the end of a structure. + Wif-not-aligned C ObjC C++ ObjC++ Var(warn_if_not_aligned) Init(1) Warning Warn when the field in a struct is not aligned. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 741a37560b0..041df4355da 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9289,6 +9289,13 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, && is_last_field) TYPE_INCLUDE_FLEXARRAY (t) = true; + if (warn_variable_sized_type_not_at_end + && !is_last_field + && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x))) + warning_at (DECL_SOURCE_LOCATION (x), + OPT_Wgnu_variable_sized_type_not_at_end, + "variable sized type not at the end of a struct"); + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field = true; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 5a026c4b48c..737228b35ac 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1748,7 +1748,63 @@ Flexible array members may only appear as the last member of a A structure containing a flexible array member, or a union containing such a structure (possibly recursively), may not be a member of a structure or an element of an array. (However, these uses are -permitted by GCC as extensions.) +permitted by GCC as extensions, see details below.) +@end itemize + +GCC extension accepts a structure containing a ISO C99 @dfn{flexible array +member}, or a union containing such a structure (possibly recursively) +to be a member of a structure. + +There are three situations: + +@itemize @bullet +@item +The structure with a C99 flexible array member is the last field of another +structure, for example: + +@smallexample +struct flex @{ int length; char data[]; @}; + +struct out_flex @{ int m; struct flex flex_data; @}; +@end smallexample + +In the above, @code{flex_data.data[]} is considered as a flexible array too. + +@item +The structure with a C99 flexible array member is the field of +another union, for example: + +@smallexample +struct flex1 @{ int length1; char data1[]; @} +struct flex2 @{ int length2; char data2[]; @} + +union out_flex @{ struct flex1 flex_data1; struct flex2 flex_data2; @} +@end smallexample + +In the above, @code{flex_data1.data1[]} or @code{flex_data2.data2[]} +is considered as flexible arrays too. + +@item +The structure with a C99 flexible array member is the middle field of another +structure, for example: + +@smallexample +struct flex @{ int length; char data[]; @}; + +struct mid_flex @{ int m; struct flex flex_data; int n; @}; +@end smallexample + +In the above, @code{flex_data.data[]} is allowed to be extended flexibly to +the padding. E.g, up to 4 elements. + +However, relying on space in struct padding is a bad programming practice, +compilers do not handle such extension consistently, Any code relying on +this behavior should be modified to ensure that flexible array members +only end up at the ends of structures. + +Please use warning option @option{-Wgnu-variable-sized-type-not-at-end} to +identify all such cases in the source code and modify them. This extension +will be deprecated from gcc in the next release. @end itemize Non-empty initialization of zero-length diff --git a/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c new file mode 100644 index 00000000000..e3f65c5ed07 --- /dev/null +++ b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c @@ -0,0 +1,31 @@ +/* Test for -Wgnu-variable-sized-type-not-at-end on structure/union with + C99 flexible array members being embedded into another structure. */ +/* { dg-do compile } */ +/* { dg-options "-Wgnu-variable-sized-type-not-at-end" } */ + +struct flex { int n; int data[]; }; +struct out_flex_end { int m; struct flex flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_mid { struct flex flex_data; int m; }; /* { dg-warning "variable sized type not at the end of a struct" } */ +/* since the warning has been issued for out_flex_mid, no need to + issue warning again when it is included in another structure/union. */ +struct outer_flex_mid { struct out_flex_mid out_flex_data; int p; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +union flex_union_mid { int a; struct outer_flex_mid b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ + + +struct flex0 { int n; int data[0]; }; +struct out_flex_end0 { int m; struct flex0 flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_mid0 { struct flex0 flex_data; int m; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct outer_flex_mid0 { struct out_flex_mid0 out_flex_data; int p; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +union flex_union_mid0 { int a; struct outer_flex_mid0 b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ + +struct flex1 { int n; int data[1]; }; +struct out_flex_end1 { int m; struct flex1 flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_mid1 { struct flex1 flex_data; int m; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct outer_flex_mid1 { struct out_flex_mid1 out_flex_data; int p; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +union flex_union_mid1 { int a; struct outer_flex_mid1 b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ + +struct flexn { int n; int data[8]; }; +struct out_flex_endn { int m; struct flexn flex_data; }; /* { dg-bogus "variable sized type not at the end of a struct" } */ +struct out_flex_midn { struct flexn flex_data; int m; }; /* { dg-bogus"variable sized type not at the end of a struct" } */ +struct outer_flex_midn { struct out_flex_midn out_flex_data; int p; }; /* { dg-bogus"variable sized type not at the end of a struct" } */ +union flex_union_midn { int a; struct outer_flex_midn b; }; /* { dg-bogus "variable sized type not at the end of a struct" } */