From patchwork Tue May 30 18:30:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1787853 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) 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=wggQd7/b; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QW1Dr6qpLz20Pc for ; Wed, 31 May 2023 04:31:08 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EF807385702F for ; Tue, 30 May 2023 18:31:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EF807385702F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685471467; bh=5HURD/vXDWnYX26ixFfZphVhOi+YMV+vO/xMHOQywQU=; 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=wggQd7/bCEBCn9yfbQKEmpROlAdF/w3ymy029OSAttKmcd8N06iJGgRrTJ0RmmyqH KMb12JSUVdw7urKoPdhN08e/HTnGTumQrNv6oxCieNukdqdPie66HyBDWeaF/vo+X2 tuAWn3YEOBKuuLJoLpv2x5aHdsi5k8DsJ1mn3xR8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id DC6503858D28 for ; Tue, 30 May 2023 18:30:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC6503858D28 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34UE45Rf017607; Tue, 30 May 2023 18:30:42 GMT Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3qvhj4ugnn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 May 2023 18:30:42 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 34UHaoVn019830; Tue, 30 May 2023 18:30:41 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2101.outbound.protection.outlook.com [104.47.55.101]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3qu8a4hqp9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 May 2023 18:30:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f7GB2zjp/rR5+XsclqQ0TCF9czbaSp8MspGV+rfRoebq5zmTezjRg3/Mwh4kQEyBE2a6za6149ZAmk858zVPmSmgyKxTOr2ePhgIQR1wRy0GSJTKA7Bl7MF08DW/PsbMU5OiY2gm7gUo3Ygbvw5Ysg1t3K0hwOs/CgwiTYbNkcqyNQrrlr0XylTOzZEDVSgdm4v5YFxDjXXFvoya5JYOR2niDy6O8zFHAIGskp0SNCQc3isQjXucA3st1bK15K8fz8n7I7Qb+jC7H+85Q1E6WCun3KkF++UIhwBURcG9gMHX0zXB8y9jHtwu5hm+W9/jgTxBMiFzJAZuxZYq9S653g== 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=5HURD/vXDWnYX26ixFfZphVhOi+YMV+vO/xMHOQywQU=; b=fgdfXL6jwkQG7Crd2sh0VCNgcCCkiPgbC6CkSiRlaSJYgQw/jg38qbAbuodwXikY1GtLsthoLWRM/8nR7dvyo0Iqk2NcNSwUppZPgf4+BFzRat6WfBN81glQNJMLjyoAguwQb5RlGL5z33rA7LDfTtFcvWsyv1OLsiKo3FNO2WA7fisXbfiLO61MjpJml3615NqWObFvUxOYcLgHG2egCMy2VWn7kvf+FOGlQOT1UjSD9Rz6hytoQkmN1XgKaXRRBHOuOyZD+u9C+3jTeTwPPS85952dDYUG9C0psdYGOPlxw3NEh0AmeZtST6s9vEncclAMWNKt92YYua9zgFbU/g== 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 PH7PR10MB7033.namprd10.prod.outlook.com (2603:10b6:510:276::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.23; Tue, 30 May 2023 18:30:38 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::346b:de36:41d7:b1e4]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::346b:de36:41d7:b1e4%4]) with mapi id 15.20.6433.022; Tue, 30 May 2023 18:30:38 +0000 To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, siddhesh@gotplt.org, uecker@tugraz.at, Qing Zhao Subject: [V9][PATCH 1/2] Handle component_ref to a structre/union field including flexible array member [PR101832] Date: Tue, 30 May 2023 18:30:28 +0000 Message-Id: <20230530183029.1156862-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230530183029.1156862-1-qing.zhao@oracle.com> References: <20230530183029.1156862-1-qing.zhao@oracle.com> X-ClientProxiedBy: SN7P220CA0028.NAMP220.PROD.OUTLOOK.COM (2603:10b6:806:123::33) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|PH7PR10MB7033:EE_ X-MS-Office365-Filtering-Correlation-Id: 3dbca034-63ae-4c2d-04c8-08db613bf75e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sSgbU4bXJf/91ow2sH+RFyHNuI+M6+uqDm8kNBuNQcf68i/gbhzwaW9IHevYw92RpT5J7UrdxP/HtEVmnB9Dq6BCDO1IAyPeU7XQ1OvsJo7QCLcndK51uEdgqGxjkmuBVmI51696M2OdA2kxPB/FzTg/K43ZJD38lZR23hkjQH2c8DGQAA+eFPp9scAteH3zmYWUh/x7HPz0FEbRrMx5XrBfJCj7HFyK6KGjV98ylT29FJewTqvkvbq740uQNmMUtN2m5R9AKF5M6R7ddjtVS4ukHJfrF14Zf2ue9jd0tq36iyzapiFpyCcY9oDaEzI9oP9d9Op4kszBNjnRtbwngEZbZ/B2z2SOqCurR5MwErimBc/Ytp5P9+cEvrQQ0z2NMmYVJU0F+o1Ua03twZsjk78JZphv0YWDsd/cvjHteS9DZNJC4Y9tbJwWwkc6GGey1Af9fh90n4j2QobprSAVskmFhmZ3LFE/6QXloYSapVGqfWnaQslOmnBulDCT2uSOPAgpWuDnjkVaAL29szhqp+HBUhdP8aDFn9WBLRoOIqs= 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:(13230028)(39860400002)(366004)(396003)(346002)(136003)(376002)(451199021)(478600001)(38100700002)(66946007)(66476007)(66556008)(6486002)(41300700001)(84970400001)(8676002)(8936002)(5660300002)(86362001)(107886003)(44832011)(26005)(2906002)(30864003)(1076003)(6506007)(186003)(6512007)(83380400001)(4326008)(6666004)(316002)(2616005)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CeircMlvBRrPC1/5TgcD7O3mshuZqcQqhDJ343pDC2N8SjoSyxh6/n5qfbfBiWTMmrITvqfC3zIUs73PtiWHsqoZZHulbFNZu4Nf9eDlR2q5Hzon+NSLu6GxYHDa8bxgZjZya2MpDWyXXXQHLUZZCq3fKL78DrqHidyoVZQmBha8zMuKahIi5a9804XM72VPLtNzfUPvNO4BceCsXn5rqxxqt3FYTsKedS359ZwiT87OGGNRvCoG5UqoS/7pxfFU0XFrVf2b9S3q/WhQt52ltDWYl66PQ967WJLzOQGSiwsuk8z0otvnVu0etEMHARWU3ZGXS5w6vnvPqJ7aWDvPBMH9IKCuvUetJswyZ16a3K7J5f3AInKkrobt6aAGWZmitopr1Su236tT0Jzk/DEENbe+dKzt8ck4wcfYu01q6ROsO4lnnVunYEkQFcg2XDAn1bkRm8E6HNbd6nKATwOHSrd7ebKsAnsv9BzaXSLaoCRuPoL8KFfW+K3bLGh9X5FQhwCZvzPWgyIP0ZuhdwPfKytr8cdcajuglcmTGmYPwGvzkvCj34bcFxVyRF2WyAUZRKnQqxoAP078noKhTG2c7g1uSZUqYzUhc+zxDs1X675vStQ3oHrNlLbc+LeVpewuFNXw72D+7dvreMWy3iL8xapKpfResUxol1WEJ24yxn9blANEZU9DmYNSFFRLBBjsnWHxMJrFX9xTAnEafXwTtdCKYIeC9pn7NWNmzy8OdD6uTc+REQFiyFOGbXrrCNbh+woyfz6kX8w+OTFYH72u4sjsGzmorkCDO0DsClR9vh7my2LBsuqHCLysZc+CmmxlFcoa5ojYpsY9IkuxBHvK1P1U47iDI6NsBJiBCDIw3kokGXV5cW0H2bNhDuLWzG/rNHL6D5LOWFQMT3Nm8IwZdDcqjjkUIhBHkAAmK3X6hS6I2U424wz3Ome7hf27IRgwOycxBQwgjVf+iq45/Tq0/pKOA4Cea3OWTaq72kHtEFyqx2qh5xeZFPRbDsFL/vtc4wiYtMT39/EZryMmFS35u923Z3bwTOveQEAVFNUUKYmcJD0zhD+DkQ7XWppBupGNlGOYWy2VgniiEE7lryH5L25gjSxrVBcMSoCbz9z88bTNYA0nKGc4zp+u5A1QAKHY8iX/a2TnX5sIfZX2C+sBYldtVD/Rf9qy8fZMt3QzQUkkUi+y3gvDv8eQRsLa/639nujG6wZmrllcekeP2KVBLyb6re150ZjWaPYFH5tspt4whWOjFZxr/TFa9J7u3aSlLvCtYbPLvW/wCep7uVJ5cHnStzAw2C309fgC8fZBr8xnV2dFDU0kgh/zlVbAImff7X8u/u+8fqvT/bhxZUCkcoqFSxBJWx2BKaXyitHGtSNsBCsPp/C763B4CYx0YnX7ngEZrNja04G9u7Xnfa/m4VaP2dwezdyB503PPI4XREJ0gg8xGqJUMPoKIeL8EHR6VgtVvgDggUKet1IdxZzB89yWEloebyMhV0F0Gm3dD1X2jj9qAhkInWRNbMA6yKBRnuaeXNecNtzAFNG93M4ufj+ZZZBj/KPQlV5laDy7jTDSA16yzHeEFA7wqx8Rfxga X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: HhsXuYrs+tSiiWejAdgoC8V7K07p3cBCw1cnA8MdoLMAc0AY4hlKzPZ2GyfHS+6qy9WSEj0ttH8WNOckNVzwpKEYdtDkStRopvaZmMpxjkngLaVz/XX6uIkAvlqU+xXuX+8kNsA1gkKsJWieYTGk/F01UXr1DZ+JuSML88Bpnh4i9fclDUGFjKiKG5M7u0UPMAnS/XiiLEQoL920VxXhm8550KeHg1YOB9Ui9MshmEu6K4rF5oXljiH4J1beNg48KP0O4/4wNzJX8tlLsbtxsV5Vi1otxlpaJLYC3v3C7oUKRZanTXMOFsqMCKsA0glhPAUQ3Qc09HcAjCxxWJflAyD1zWhP69LyHVJTF2mofAXRMW16JIwPI5cQf59ncLbB2ZS2DTnD6qhVXaNw5wlSCChXVgR3tNMZIxezUdrOooBw1M4antCrydC944wnBl06jBTpCguZxMfbYyQlNLBqJJZVYLuiSaKOMHHdYeueOETNxnk7ZEsX8rO068AcsgKGFqkfs2S4tylswibfzqRf22whvz7eftqbBURJgNZ/vaj7fj4dNgzZBYY50+4MUD0IxIp5ol5Vn+vuL9ATm8YeSI55nS/Hmsl1yvLkB37x0Gau5wPgYRJlGUvPt6XlDR97HFN5Mm5gAx6KvyGbwnfh+BQS3edV6MpJhYGOkIFKxhE0Uhh5xA2+gWL9oYaAlB7X8vE13bxAbVdumdoQXoSgknLJ1jyjScmVKsq9+znwuEFtAv8b+GLTnCQLXMnOgba2wpC60OTFnA8f4EubG1p8Ja4xB5SNA72Fz37FlCHPNdlwD+a0k8elxPT03yPqb8V71iplZIC1PyJPef4vDUj7ZodzlY8HXYTy2Pxjw8dUl9dl/QFd55XrDGLYw6J07MCnbcVE3tpg+Z6obE5bknYveI3Cv6w+LkF+X79p3LCuQq+ffmziWI16BqzKGYBrDsAU X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3dbca034-63ae-4c2d-04c8-08db613bf75e X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2023 18:30:38.5874 (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: +rR0Bq6InoJ7U3Ytx3SIomGA46iGNk+wJ2681et98CzTd65PZDDMdmDj2nE5ORlMHEQPzDXZpUvkrSi8HlAs1Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB7033 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-05-30_14,2023-05-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 malwarescore=0 phishscore=0 bulkscore=0 adultscore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305300148 X-Proofpoint-GUID: upOFhZev05gchGYx99NGywCamW-5urlK X-Proofpoint-ORIG-GUID: upOFhZev05gchGYx99NGywCamW-5urlK 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Richard or Jakub, could you please review this patch and see whether it's Okay to commit? thanks a lot. Qing =================================== GCC extension accepts the case when a struct with a C99 flexible array member is embedded into another struct or union (possibly recursively) as the last field. __builtin_object_size should treat such struct as flexible size. gcc/c/ChangeLog: PR tree-optimization/101832 * c-decl.cc (finish_struct): Set TYPE_INCLUDES_FLEXARRAY for struct/union type. gcc/lto/ChangeLog: PR tree-optimization/101832 * lto-common.cc (compare_tree_sccs_1): Compare bit TYPE_NO_NAMED_ARGS_STDARG_P or TYPE_INCLUDES_FLEXARRAY properly for its corresponding type. gcc/ChangeLog: PR tree-optimization/101832 * print-tree.cc (print_node): Print new bit type_includes_flexarray. * tree-core.h (struct tree_type_common): Use bit no_named_args_stdarg_p as type_includes_flexarray for RECORD_TYPE or UNION_TYPE. * 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 bit no_named_args_stdarg_p properly for its corresponding type. * tree-streamer-out.cc (pack_ts_type_common_value_fields): Stream out bit no_named_args_stdarg_p properly for its corresponding type. * tree.h (TYPE_INCLUDES_FLEXARRAY): New macro TYPE_INCLUDES_FLEXARRAY. gcc/testsuite/ChangeLog: PR tree-optimization/101832 * gcc.dg/builtin-object-size-pr101832.c: New test. change TYPE_INCLUDES_FLEXARRAY to TYPE_INCLUDES_FLEXARRAY --- gcc/c/c-decl.cc | 11 ++ gcc/lto/lto-common.cc | 5 +- gcc/print-tree.cc | 5 + .../gcc.dg/builtin-object-size-pr101832.c | 134 ++++++++++++++++++ gcc/tree-core.h | 2 + gcc/tree-object-size.cc | 23 ++- gcc/tree-streamer-in.cc | 5 +- gcc/tree-streamer-out.cc | 5 +- gcc/tree.h | 7 +- 9 files changed, 192 insertions(+), 5 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 b5b491cf2da..0c718151f6d 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9282,6 +9282,17 @@ 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_INCLUDES_FLEXARRAY for the context of x, t. + when x is an array and is the last field. */ + if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) + TYPE_INCLUDES_FLEXARRAY (t) + = is_last_field && flexible_array_member_type_p (TREE_TYPE (x)); + /* Recursively set TYPE_INCLUDES_FLEXARRAY for the context of x, t + when x is an union or record and is the last field. */ + else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) + TYPE_INCLUDES_FLEXARRAY (t) + = is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)); + if (DECL_NAME (x) || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))) saw_named_field = true; diff --git a/gcc/lto/lto-common.cc b/gcc/lto/lto-common.cc index 537570204b3..f6b85bbc6f7 100644 --- a/gcc/lto/lto-common.cc +++ b/gcc/lto/lto-common.cc @@ -1275,7 +1275,10 @@ compare_tree_sccs_1 (tree t1, tree t2, tree **map) if (AGGREGATE_TYPE_P (t1)) compare_values (TYPE_TYPELESS_STORAGE); compare_values (TYPE_EMPTY_P); - compare_values (TYPE_NO_NAMED_ARGS_STDARG_P); + if (FUNC_OR_METHOD_TYPE_P (t1)) + compare_values (TYPE_NO_NAMED_ARGS_STDARG_P); + if (RECORD_OR_UNION_TYPE_P (t1)) + compare_values (TYPE_INCLUDES_FLEXARRAY); compare_values (TYPE_PACKED); compare_values (TYPE_RESTRICT); compare_values (TYPE_USER_ALIGN); diff --git a/gcc/print-tree.cc b/gcc/print-tree.cc index ccecd3dc6a7..62451b6cf4e 100644 --- a/gcc/print-tree.cc +++ b/gcc/print-tree.cc @@ -632,6 +632,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_INCLUDES_FLEXARRAY (node)) + fputs (" includes-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 9d44c04bf03..b2f619c5efc 100644 --- a/gcc/tree-core.h +++ b/gcc/tree-core.h @@ -1712,6 +1712,8 @@ struct GTY(()) tree_type_common { unsigned typeless_storage : 1; unsigned empty_flag : 1; unsigned indivisible_p : 1; + /* TYPE_NO_NAMED_ARGS_STDARG_P for a stdarg function. + Or TYPE_INCLUDES_FLEXARRAY for RECORD_TYPE and UNION_TYPE. */ unsigned no_named_args_stdarg_p : 1; unsigned spare : 1; diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9a936a91983..a62af050056 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -633,11 +633,32 @@ 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 aggregate type, i.e, an array, + a record or a union, it will not have flexible size, + compute the object size directly. */ + if (!AGGREGATE_TYPE_P (TREE_TYPE (v))) { v = NULL_TREE; break; } + /* if the ref is to a record or union type, but the type + does not include a flexible array recursively, compute + the object size directly. */ + if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (v))) + { + if (!TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (v))) + { + v = NULL_TREE; + break; + } + else + { + v = TREE_OPERAND (v, 0); + break; + } + } + /* Now the ref is to an array type. */ + gcc_assert (TREE_CODE (TREE_TYPE (v)) == 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))) diff --git a/gcc/tree-streamer-in.cc b/gcc/tree-streamer-in.cc index c803800862c..be2bdbb7699 100644 --- a/gcc/tree-streamer-in.cc +++ b/gcc/tree-streamer-in.cc @@ -386,7 +386,10 @@ unpack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) if (AGGREGATE_TYPE_P (expr)) TYPE_TYPELESS_STORAGE (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_EMPTY_P (expr) = (unsigned) bp_unpack_value (bp, 1); - TYPE_NO_NAMED_ARGS_STDARG_P (expr) = (unsigned) bp_unpack_value (bp, 1); + if (FUNC_OR_METHOD_TYPE_P (expr)) + TYPE_NO_NAMED_ARGS_STDARG_P (expr) = (unsigned) bp_unpack_value (bp, 1); + if (RECORD_OR_UNION_TYPE_P (expr)) + TYPE_INCLUDES_FLEXARRAY (expr) = (unsigned) bp_unpack_value (bp, 1); TYPE_PRECISION (expr) = bp_unpack_var_len_unsigned (bp); SET_TYPE_ALIGN (expr, bp_unpack_var_len_unsigned (bp)); #ifdef ACCEL_COMPILER diff --git a/gcc/tree-streamer-out.cc b/gcc/tree-streamer-out.cc index 5751f77273b..6d4a9d90da6 100644 --- a/gcc/tree-streamer-out.cc +++ b/gcc/tree-streamer-out.cc @@ -355,7 +355,10 @@ pack_ts_type_common_value_fields (struct bitpack_d *bp, tree expr) if (AGGREGATE_TYPE_P (expr)) bp_pack_value (bp, TYPE_TYPELESS_STORAGE (expr), 1); bp_pack_value (bp, TYPE_EMPTY_P (expr), 1); - bp_pack_value (bp, TYPE_NO_NAMED_ARGS_STDARG_P (expr), 1); + if (FUNC_OR_METHOD_TYPE_P (expr)) + bp_pack_value (bp, TYPE_NO_NAMED_ARGS_STDARG_P (expr), 1); + if (RECORD_OR_UNION_TYPE_P (expr)) + bp_pack_value (bp, TYPE_INCLUDES_FLEXARRAY (expr), 1); bp_pack_var_len_unsigned (bp, TYPE_PRECISION (expr)); bp_pack_var_len_unsigned (bp, TYPE_ALIGN (expr)); } diff --git a/gcc/tree.h b/gcc/tree.h index 0b72663e6a1..d4755317a4c 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -786,7 +786,12 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, (...) prototype, where arguments can be accessed with va_start and va_arg), as opposed to an unprototyped function. */ #define TYPE_NO_NAMED_ARGS_STDARG_P(NODE) \ - (TYPE_CHECK (NODE)->type_common.no_named_args_stdarg_p) + (FUNC_OR_METHOD_CHECK (NODE)->type_common.no_named_args_stdarg_p) + +/* True if this RECORD_TYPE or UNION_TYPE includes a flexible array member + as the last field recursively. */ +#define TYPE_INCLUDES_FLEXARRAY(NODE) \ + (RECORD_OR_UNION_CHECK (NODE)->type_common.no_named_args_stdarg_p) /* In an IDENTIFIER_NODE, this means that assemble_name was called with this string as an argument. */ From patchwork Tue May 30 18:30:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1787854 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) 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=aaspOr7C; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QW1F15XwMz20Pc for ; Wed, 31 May 2023 04:31:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C25143857735 for ; Tue, 30 May 2023 18:31:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C25143857735 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685471475; bh=rCNz3PNsfalh1mAWWZQgHbEwbsJp4Gq3jY9sdbJ8RV4=; 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=aaspOr7Czjm/ZuF+B55+B5ULwneyGgAw/YuR02XkI1YqiW9VGzt4+EqhBgo6wadPE 3P+VZt4D1oauCtvoWcBcSTHeJ2m4+0K/1XkytuUlOXtpSeKHVOgRFiSJO0+utIup+f grrKcJ4DRQO/5cxrHqoISIGcxOs80hUCr4/J6XuI= 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 D17263857707 for ; Tue, 30 May 2023 18:30:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D17263857707 Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34UE4jMo021703; Tue, 30 May 2023 18:30:48 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 3qvhjh3gqh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 May 2023 18:30:48 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 34UI0uJt026089; Tue, 30 May 2023 18:30:46 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2040.outbound.protection.outlook.com [104.47.73.40]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qu8aatq7d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 30 May 2023 18:30:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZkcxFG/rS0I7EtvJvzHZMS4yGPzW7zQ8vwDA+oSir53bdadqjQX9jJVdOYdyHAs7Vg+rrhPBWsC80dSIUIZkYLyQel7fKug/5J5qf3HPS2KYaOATlSS0uCrjhhOOuXz90DCJK7es1GGESURODLKaE2oEJK1WGD6B8F0zbAXd1f5QCTmA5yyf4P5gywGpLmHr0laLB6fJf2JOUPuu5GuzBpvyAK+/lSxDNIhusydXklXjxhnfuJOT/Fp+aYvC29lfcjbFSkWzwQ3f9g60A3d5XVRhWGS5X5ygfe0/9mPbFd7ivDLwIMztDmVVWMmCpys5o1cqXhmdKIUjIy24R2OBww== 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=rCNz3PNsfalh1mAWWZQgHbEwbsJp4Gq3jY9sdbJ8RV4=; b=KZlkgaUvj0wW+8tQdNdF8b2pvrPpyY0+bzafNCz9l3KpBEZuUStnMdQ9vQLObjzj2Eth5UvR46GM9k+xmsQIJE0f9SPM8JdRKHP2VMYm5CWTFEgJHxDBhRb2uCC50JtVhs1p/2O/D2c4yx3xEOEctbqch2MRJ4bWZrvTZG7DXMPuzTeQlWISk1fTfcPo+3q9+oU3alB/jDt7M4LiuTI8Bm7rWeMFXVbypEefu3H3J8mPd7WBmorDJuO0eKgIvp8OutTBMFi3pg/+OJFdHqkHwRkKLuHWxV5bvcFpS4+PBWGRZVBSm4oSSmZ58t/eSyTiz0t+JVoS1M7TZWDHpbfYcw== 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 PH7PR10MB7033.namprd10.prod.outlook.com (2603:10b6:510:276::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.23; Tue, 30 May 2023 18:30:43 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::346b:de36:41d7:b1e4]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::346b:de36:41d7:b1e4%4]) with mapi id 15.20.6433.022; Tue, 30 May 2023 18:30:43 +0000 To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, siddhesh@gotplt.org, uecker@tugraz.at, Qing Zhao Subject: [V9][PATCH 2/2] Update documentation to clarify a GCC extension [PR77650] Date: Tue, 30 May 2023 18:30:29 +0000 Message-Id: <20230530183029.1156862-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230530183029.1156862-1-qing.zhao@oracle.com> References: <20230530183029.1156862-1-qing.zhao@oracle.com> X-ClientProxiedBy: DM6PR21CA0016.namprd21.prod.outlook.com (2603:10b6:5:174::26) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|PH7PR10MB7033:EE_ X-MS-Office365-Filtering-Correlation-Id: dd611da6-9871-4aa5-c507-08db613bfa26 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A0Dgq9Lbzl92ERa/Y+vJ5jVtrvmDMH7Y8nnjZ52dzx2Mc7QB0Wd0HsA6WYTudHEX1+o50+U6/Vm9xH2iyXbBhmDExS5R95bzTLzA9UYSLFFgnC9tBxoJ80RA2HPQ2i7l3gO1bDeAwxCp27Ca54eQlLwLHfYNNRFgE6z0zk2n3VP6GOORa05NXa+hiQtx8YWlZ5qU7QPpYy3E3OmIS3a+knBQvqKGtfAIr1yGiioabiRBEZd931cT/IML4LqwZtVOhAsarUyMkzOkyC4YiAOgWBvgI6PIJdevHZ3yjtRPSzKKJBMuGp6KCo2LaY1Y9IHlwNJ4K5T+UxkZc3Ps4qjtq+yxtZ7sG2JZgFcxpLr2nN+4r4J7qAvSCUOxzMVUf+lc8HRC9a5JUBoQN723XDT8OMFCZXp66ybzxKEumZpt8GuWCC7OkzQzqosP1U5n6xzM9fIaQ2Eyi0r4R/JMRac1egQDu1JeddHE6EUp3CBUAUJCQz/nBx+4cHaVewRWaUgpjqsrYm1HkzLbpX8voPaMqzOnC2XZdyYNJx1WXtFKcOQ= 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:(13230028)(39860400002)(366004)(396003)(346002)(136003)(376002)(451199021)(478600001)(38100700002)(66946007)(66476007)(66556008)(6486002)(41300700001)(84970400001)(8676002)(8936002)(5660300002)(86362001)(107886003)(44832011)(26005)(2906002)(1076003)(6506007)(186003)(6512007)(83380400001)(4326008)(6666004)(316002)(2616005)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xB7ppOuS5swqbKJ7+tBGcoXF31kD2fnpwn8v2LTlTpRkqBg/ORFp1CR5nrnl2Kz0twqVFL5Bt7vvQEZzmiVqxRjA/pGIJSLy8DpTr7p/mGOB3vNXqvbQJeQjHV3/ooTZfcXiW9f/MZK9yUviONdac75xsG7Qmj/dKAPdEinvCvBG/2nYRRtnOIoF6y/giAZFJloMBiv5Y5AHHrisQv6DomhNLtLIQOgFn9BkcfItjObR+NmLyo9gJPhDjEJo1DUukY6XJ8nxXv2+NZZ0bkqQ/go78vaZs+ydYg0x+lOBbBHT8b6eV0MP8LTO3Deq4sANCa5cQLkTVBMwueOdp/tNLSIx17bsdZvQf0zAgZsiuI61F+MWX7TMl+mvVySFJBlutpSi9TZb41gFhkjGVHSkZO5/T+oQnI2OdY7UwWvQ7MwhfNL9i8x7zkMZ8/BoMVKZ4x9cTT+hEhjSl1En2TQb8QsOyILgihIOYQ4cGMtiWcr8LL8d8bstLp5e690TCRFR4bf1KIrALU1FPiO3Fj5aY7V+NdOgK4iP0/7K32rQeOu6nMth8OvhKnETzEuY6AUhuRxcsT9SH5R4EUSl/Q30krno2iAE7D7BpwtFqBv7EynWPvEvuPKsz1FCkbnoE9UkA/dM4Rf34eP7bcye4iiFPeG8XQ8HLEQRI31f3AeEwK8eOMH8o8M1mLgo2gJ1ibM7Ttz4ANiNXRepNeUqmCj+L1L/bEelkfz2/5tK7IAjjdjNXiOAuZE5ECdX+768Yse6vNCKK70XmakIjpu2g6meK8cJbbLX26krLudQAxIr5qoP2EfR2vnHchCOtgEctZV+V6QnEJh1tJQWZBc1zgtjDOM8fmvJzymFS386VIj4/O7oTWybfGh76ydo66LfEznP1zWKu9yDkdgiaYOhcInLglUkA+u9T0fUu6Cem/X0Q+4eXV1FWWqRVfCxQImJrf5/kNJGLYsMECnA7+p6GcYcHdoVy6WeRAtMHBsWNaJ21wer3lSAKZDVFeOJjqL8yGcezk0oji/fVNpgFPlIP6ZfjcWgdIlN5leAesWpwe0Gj+Ls/5TlFPTJ5/Pka9NZtcidPIkJQa1QAUgTB4LnpYxwB8JxRn13yPwRgvR7yy1Prh7Kjj86HWleCJx7UBFY67hnVRz6YEzKS7PotfGu/l3f+O0/3CS2EN1kKfSsXsmbq/rgCPvtZz3vt2D8cBBcMQl22qriDX+tGEEjA4cvyqoF7zKDNFg9ctFaGh9k7dCmBn2bEAmZotkI3MJ+MX49Yj38AP8d0jzcgZ7vzWZ1+XtfqrUYePUpiNnNWOGPvYx+Yw324Z45A3gyaqb0dmyn7714zedd6tYr058EJs6Yx6+1eLJ5B4ajA744B6o06A11jkdhPQmNKeq1NDeiyIQTAGmtrSkm979Abl7fVSNUblIUnsxm5tGnWzeUsjioKZrP/vvTLKqSs7Ll3W/lVvllbhBHgSwJZl50Dlvz930xIgLNsUbEGeDy5VXS5ESFcGi+gj/in46Pz127p3Y4RjnlEVjVIJqYYWzxz7HJx3oJJ5HP++peqzeY5BgX2aw78cvdR+doyITvJzoY1Ox27L1zFLEo X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: QcG79RJL0QDoGbtiJmezHXZNo4y6DjPECJg0qEiF9vMTWv4kZ7MqLR81JLcD1QvXrWDBKs/O4GNM4noFwtZtRLtq8jtd1lV1ntoB4XsQPXoKySyOsOZR8jXcQVliqNOoxkCZ+xBleLlISLuz8eQcqJurDQ5XGYczs/slXgnw5VZBNbHy5+S8FHkqtDD40QD5hPplSgwHRIlQ5hDtvCtoFzGAju7N9GojVpPXoMlLdU/XSbUxds1x2A945KBmnmu8Ooi4NW0NOeyZlUa3HPgZ7FT2TkzPibDWJj9lDP1XSWBG85iyQF45vZMRBNvJpNj0mnZbQQslqaWbpCLOKJ/cwaOkLHpRaXW1Y4kuFwEk8EvUePXKS8kqCgmEcHUvBFpIW5B7937EjNWNhf7o6n7xPxIHy5UPCjOKO8FfPpHQi3v1omx/mQzXUdFG2Vta1H92WK00HEOWa43APRTqGi1hOB+P3ea83Vo80e1sTu5TW1RWFhMh0azvFCiG1er4mYJc/93bobgJHk9V4fIu6PwR1riV5SUbztfyCJYPhfK7YBBKXlBDfTfkVzYCSsjDnTn/2cEV8fx9U6XbHplhft+nfKNJwVksJ3VCWNvaUaoa0Cn7jOrhDfCFn/8TCFIm3jiftByXHWG3PYBzh9FH2QswoWxvrOnBbwPTcyVLmtfSa/tcPrnbPpetrLhtc059GNyqnDHNykj5He8P2zEra9ZeJuSAmRxYlNuXr/IGvjZlFO0OBk0Nv6lGs8Kb/NRLCtpNSsnaKY1bc0yhy75fKy84ADGYcVPWummXKHr+ES6Uuk53jwdrNK3rnONEsQpJA6AtJeI+ZsULYuBjsnf9yL0WoEPkVT8c/nR6XLXvvVwcnXlSL4NJkW/kWUdL6Y8R6c99nddIDvy9zV2qJF1xZnjo+OUsan35jZqeNn28JsPwu/lE31N/z5xnSj1kdqJirsOg X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: dd611da6-9871-4aa5-c507-08db613bfa26 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2023 18:30:43.2734 (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: 4gFELrH/g6Y0QEEXMz2jV4CzCokzFLTB4csKm/JgcUbkU1Xul5aYQUBvuNzKm/ZrUTYOqB/ksjqzWHmNre+GUg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB7033 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-05-30_14,2023-05-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 spamscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305300148 X-Proofpoint-GUID: a1QGZhtYJAyJLxx0A44FIBRTFR3ls1NG X-Proofpoint-ORIG-GUID: a1QGZhtYJAyJLxx0A44FIBRTFR3ls1NG 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Joseph, could you please review this patch and see whether it's Okay for commit now? thanks a lot for all your comments and suggestions for this patch. Qing. ====================================== on a structure with a C99 flexible array member being nested in another structure. "The GCC extension accepts a structure containing an ISO C99 "flexible array member", or a union containing such a structure (possibly recursively) to be a member of a structure. There are two situations: * A structure containing a C99 flexible array member, or a union containing such a structure, is the last field of another structure, for example: struct flex { int length; char data[]; }; union union_flex { int others; struct flex f; }; struct out_flex_struct { int m; struct flex flex_data; }; struct out_flex_union { int n; union union_flex flex_data; }; In the above, both 'out_flex_struct.flex_data.data[]' and 'out_flex_union.flex_data.f.data[]' are considered as flexible arrays too. * A structure containing a C99 flexible array member, or a union containing such a structure, is not the last 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, accessing a member of the array 'mid_flex.flex_data.data[]' might have undefined behavior. Compilers do not handle such a case consistently, Any code relying on this case should be modified to ensure that flexible array members only end up at the ends of structures. Please use the warning option '-Wflex-array-member-not-at-end' to identify all such cases in the source code and modify them. This extension is now deprecated. " gcc/c-family/ChangeLog: * c.opt: New option -Wflex-array-member-not-at-end. gcc/c/ChangeLog: * c-decl.cc (finish_struct): Issue warnings for new option. gcc/ChangeLog: * doc/extend.texi: Document GCC extension on a structure containing a flexible array member to be a member of another structure. gcc/testsuite/ChangeLog: * gcc.dg/variable-sized-type-flex-array.c: New test. --- gcc/c-family/c.opt | 5 +++ gcc/c/c-decl.cc | 9 ++++ gcc/doc/extend.texi | 44 ++++++++++++++++++- .../gcc.dg/variable-sized-type-flex-array.c | 31 +++++++++++++ 4 files changed, 88 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 3333cddeece..c26d9801b63 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. +Wflex-array-member-not-at-end +C C++ Var(warn_flex_array_member_not_at_end) Warning +Warn when a structure containing a C99 flexible array member as the last +field is not at the end of another 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 0c718151f6d..a1a8e9bd341 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9293,6 +9293,15 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, TYPE_INCLUDES_FLEXARRAY (t) = is_last_field && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x)); + if (warn_flex_array_member_not_at_end + && !is_last_field + && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)) + && TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (x))) + warning_at (DECL_SOURCE_LOCATION (x), + OPT_Wflex_array_member_not_at_end, + "structure containing a flexible array member" + " is not at the end of another structure"); + 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 ed8b9c8a87b..aa1ecb6968e 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1751,7 +1751,49 @@ 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 + +The GCC extension accepts a structure containing an ISO C99 @dfn{flexible array +member}, or a union containing such a structure (possibly recursively) +to be a member of a structure. + +There are two situations: + +@itemize @bullet +@item +A structure containing a C99 flexible array member, or a union containing +such a structure, is the last field of another structure, for example: + +@smallexample +struct flex @{ int length; char data[]; @}; +union union_flex @{ int others; struct flex f; @}; + +struct out_flex_struct @{ int m; struct flex flex_data; @}; +struct out_flex_union @{ int n; union union_flex flex_data; @}; +@end smallexample + +In the above, both @code{out_flex_struct.flex_data.data[]} and +@code{out_flex_union.flex_data.f.data[]} are considered as flexible arrays too. + +@item +A structure containing a C99 flexible array member, or a union containing +such a structure, is not the last 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, accessing a member of the array @code{mid_flex.flex_data.data[]} +might have undefined behavior. Compilers do not handle such a case +consistently. Any code relying on this case should be modified to ensure +that flexible array members only end up at the ends of structures. + +Please use the warning option @option{-Wflex-array-member-not-at-end} to +identify all such cases in the source code and modify them. This extension +is now deprecated. @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..3924937bad4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/variable-sized-type-flex-array.c @@ -0,0 +1,31 @@ +/* Test for -Wflex-array-member-not-at-end on structure/union with + C99 flexible array members being embedded into another structure. */ +/* { dg-do compile } */ +/* { dg-options "-Wflex-array-member-not-at-end" } */ + +struct flex { int n; int data[]; }; +struct out_flex_end { int m; struct flex flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid { struct flex flex_data; int m; }; /* { dg-warning "structure containing a flexible array member is not at the end of another structure" } */ +/* 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 "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid { int a; struct outer_flex_mid b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + + +struct flex0 { int n; int data[0]; }; +struct out_flex_end0 { int m; struct flex0 flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid0 { struct flex0 flex_data; int m; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_mid0 { struct out_flex_mid0 out_flex_data; int p; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid0 { int a; struct outer_flex_mid0 b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + +struct flex1 { int n; int data[1]; }; +struct out_flex_end1 { int m; struct flex1 flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_mid1 { struct flex1 flex_data; int m; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_mid1 { struct out_flex_mid1 out_flex_data; int p; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_mid1 { int a; struct outer_flex_mid1 b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ + +struct flexn { int n; int data[8]; }; +struct out_flex_endn { int m; struct flexn flex_data; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */ +struct out_flex_midn { struct flexn flex_data; int m; }; /* { dg-bogus"structure containing a flexible array member is not at the end of another structure" } */ +struct outer_flex_midn { struct out_flex_midn out_flex_data; int p; }; /* { dg-bogus"structure containing a flexible array member is not at the end of another structure" } */ +union flex_union_midn { int a; struct outer_flex_midn b; }; /* { dg-bogus "structure containing a flexible array member is not at the end of another structure" } */