From patchwork Thu May 25 15:28:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 70089 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B53F93856DC8 for ; Thu, 25 May 2023 15:29:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B53F93856DC8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685028557; bh=z9D7/mHeOL3LhRZG0mtHojRVDktfnur/SStlvz5AKPQ=; 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=HZTdqZJ7L1uFHnfs4KBDKWccJm9Tozp7soArtnn1SXReUYofw3YTWZ02rK3CS8yV/ k8xdrAciyUXKgiFPpR/1GOf0mOwpJVNfBjbwlhQd3LvPYGxnmYcX7G/1xlIJFzcrLy m9ZGzTVCRgc/6lRRh3rWg3izqfD26YDi64KPUdTE= 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 154403857702 for ; Thu, 25 May 2023 15:28:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 154403857702 Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34PExPJk001120; Thu, 25 May 2023 15:28:44 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 3qt9wxr4jf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 15:28:44 +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 34PFQH26023764; Thu, 25 May 2023 15:28:43 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2105.outbound.protection.outlook.com [104.47.70.105]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk8xaewh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 15:28:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lCT+aMLf96wnZBz9AwmZlrs/nxZQ+LMb7tVaEVeqytTOBdKeUvEhZCuPTYcBfpf3yJInvTu8w9m24i4QWreeOo3IV1BIkoW1VYLwVPapAj3kr0ijwVCj0tHHOEJFtbZCFpCyghGwfRvi9+6rKjG/Vgz0K0++aUQGR3s5pDyiCmihxGieXtKYqeTOnD4hfRs2q3CmM3FgsD3YWNZs/1lvG4sCx0jrTsRiMWkSdYJYP2Ravux8auSqa83GjOtTy/DTz2A7hbNkk2sMi6hMR3zY7onAOO6wwxvTRyRFWtOVP16vyDm9VVPhQO4Z9smI8LUeI2tp4M99SA/BLDgegARLow== 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=z9D7/mHeOL3LhRZG0mtHojRVDktfnur/SStlvz5AKPQ=; b=E5fMurxmJ+pPUaWn2m/KvS31hOyH9ifcP9lyqMvxH36GmSebggtJQ6ZB94QUJdGGGFxZ8j0LHICHo+mxZr+CTG3I5aRgxdpvRanxxlUdDgcc6/OgM0p6FjZlAcUhQkQtoVz+r+kkUwsd5Uo7S42bOyWTNrntUo9fR0oCKxzKgE9yzp13Y6dU8GO4VkVZDeficGmgQ1XMWxF/Sj71J7h0mdgNBVGHYpI4xmeK9MhE7sXUTQsvuxR5Hh6HKcGCM5Vh3W+ZjmTHaDv4FEZdjGVJ3plaOtEWblJ/ZYnfzZN1wrB5/0zXWxiMByS1fiZzr7/GweNFXvIfkeHlFXNxF0WYgA== 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 DS0PR10MB6871.namprd10.prod.outlook.com (2603:10b6:8:134::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.16; Thu, 25 May 2023 15:28:41 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa%7]) with mapi id 15.20.6411.028; Thu, 25 May 2023 15:28:41 +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: [V8][PATCH 1/2] Handle component_ref to a structre/union field including flexible array member [PR101832] Date: Thu, 25 May 2023 15:28:20 +0000 Message-Id: <20230525152821.2989599-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525152821.2989599-1-qing.zhao@oracle.com> References: <20230525152821.2989599-1-qing.zhao@oracle.com> X-ClientProxiedBy: DM6PR02CA0073.namprd02.prod.outlook.com (2603:10b6:5:1f4::14) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|DS0PR10MB6871:EE_ X-MS-Office365-Filtering-Correlation-Id: d529020a-423d-4697-9a79-08db5d34b841 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Prgw6d9oeMhvgNo3uTfIUgHt+QoT17iBA9BCAEjmrQRmv0GdjBT1XDfX84wLf5+UxZBUmWnA/VONsD8nnT2D8T2XyxwdTF3/bbCqC9oG/6Bx7JcPxcd0mEJv609UOG9TeaLrPnM8EKH7C+5gM354czhJHS46aqutf+NNR8jhssxxBQvb00dxc5PyJjYl/58Nv4jxD9ZJg3YgC5B6xeon8SZLumFTkpEyfmHMabWsRG/F40c0Bnbut562RycZFs1aXbw4aJ2uEnqrhjc++Pqa/F4sQIfFYjx5n5RZwrvEY10KljYADOfvtWwZlDDwk9t8VNcL4GRil/VSeH4WYLo6mxUPbmgZaziGUQjd5ZNZiuuAa0MZaJuSh/XysUHodAVGjhx2MSLEoV3ohNPBvpkqWOi3Mb15NXEAb8W5uD0C1mjq+9ahgiCJbIDT4py2sZm8kFpZgdS3iW9FrFHU1x8leTlVYzIldy4R2RwMTOxC+YP0nRhfy8Uo8/Qs/QgOjCcR9+k6Y/2nRLmjVXsk6/L9sGrdGXr/xIUyLZ9OwNfhP+U= 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)(376002)(346002)(366004)(396003)(39860400002)(136003)(451199021)(66946007)(478600001)(66476007)(8936002)(4326008)(66556008)(6666004)(41300700001)(8676002)(6486002)(5660300002)(83380400001)(2616005)(30864003)(2906002)(84970400001)(186003)(316002)(38100700002)(86362001)(36756003)(26005)(1076003)(6512007)(44832011)(107886003)(6506007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TpER/WoEFDeIebaZ3jreLeeZTvIZ6Oz6lp2sg+/zPWZIC4voHArAH46oImC/R2QaYbLZBBB6chJtlkCBA56rJy+NXJvCnKCMon8199a/G7ELNy8lOp+0XXr/7KuIhsLWz4xdnA4KtWUn7m1EQC+TRGENLvN41AcoN+xYrd2SyIA3khjWrUM4MNP8kTN+gz08vipmtj1dCxZQd3RlzGDsX/dMalbq90pdhGI88/GkYZcvSTlLTwjTKxcb9nZQKYbxqp3ItdPtldUlti5GzjGBqYNYwkhNxqG3yNwzqIonpV3C+bML6yPVkGviwWOnn2YZLZZ1nfJ/UPnoVy2jagd+lBVNffPFvmwMdS0Cr8cd9kbB9GFQW97L991LGwvVMKojC3hXpX+UqBcoFOI29d0kccBSXzOKU2Kh76/WXbolUgxs8ojHevPzyMW9biWjL5A5C5VExGgUgfqWa1YBBs5jfNzRed35R7TZXfr97G7vcSry0bajRTAmPDkexrvwgGFbzciLNkd0KLqU/K7Ef6F1I+dRK40a2k8qvDZ+N9i3+xIzWOLeptQtc+HqmVu6By5vEhdq57sDSLmYo/v8h9MhJAlydv0BRuenNO/DiN/gZu2jatuW92lh892uD0DdO5yUQCQ10yRzsDVZcX2fBCfb60Rwgg1bsBvSZ90fImstIKYU49iwp7BBmnt0TLGw2ZnpVRnopNgflIZHPiPe2vD55xBTwblLzE7zfQ20Qrh07FPqxhhHcmIgUdi3u62CRtEKCu+h3YoTJCXEq2Mw+gD1ViEvTkI5ogo1OJgQLCMfj0SuWsQiEmZrjclVVKTuC6OXmYBf8nxhcWtql4/FeTWjGLEfjGHobJ8pF2F+Ovu/7LluusKvyGLHLOCjOxh9m+SZsPbb30DZod+hHjYFtjM/ttU+3TmcYNPskkDvxb1LWp1DYtVrVofzFwVNqld1Ndj66Xuwa+yqVioAg7zoD/rELLRsVaH2vFCxqDern2kcmARLvtp1oqFyAbTFtnOdxOeuTHgOm/3vxVIQRITTWN0kO77E480uBxxCgnH0GKfE4g9/N9jJ0IBS428+/RPFeQTD+pqNW2iM0ZEyviGtR7+Z6rboVZg8DSwsy86ApErZ5bkAo0lmYK9xxApTtSIlZADZMhe3jViIdCMhO+o8fWf5qBh7HTmJLuw34LQFXyky8FqdZ0QcompeRV2Q+IpQ2Je/xsA0B4fY8d1vUF8HDxtZK3FQ7EFtX5P2sUGBjmZ8HNnvWYTjKa/7qEBagLB8JXf9NEbL45KesSrxK2mZuR7vwbp+CRsIH/bo5yuPRDGrkWXkwCUxxPrXXa5rgkPyFpx7QMFg72XRfVx5O2GogenHyGjE9Rv5TeCO+7tErqtl4pB87KHBDXOEOPvjkB5Ea3q+Ug9huLFMGczVV2HhLDBF/dUqgE2Rzc4XPleWU4LtPDsOBPPnyUC2moKflUuXNqA31Axj6v26J18TVDVHtkEkzNHrWIEGpjemW1AhRicssGhHf+7yPN2EETaZfDIxY/DQ34X+72rfJxbtKL5Z2A58rMD3R/whrTSohBOPNpj+001zBXFc7ltKZ5WuPtFmQCvi X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 469xH7v2OwHiMhm15F8BpBJ9X5RdZADGTnnNZjd8+xtSOQdMIRfbjgRSDCMbx632dnD8c3ELi4emtlDWdGT5VMYxe5NOwyZ8Zz9U+bAN1vtlXU6JdiietvXDSG6ZR29+4azRaXs4fZvEJvM/JCG+UN0qK33dD07T57CrzOIO+LNC/ti4kRC3aUDjR7vV5UMq6Bx/+UJM6d/pRzVXeNz/15pOhz03gzcEMXfOjJFquhyPNtUMWLlW0xxulvMRkWk1jfGxxKYtru9CVgBsAxzUIZlXsrzgqNCT33coHyn/qD1KleZvtOTev1JRQbgkZiphKsf0OOEa1jm1HqDqM9fDyWY+AU2FcpsNL7v3ZEPbOMui+wx4Fw0R8oITP9GN8tyu3ya3uiFxSuAC/gBUcTiUMVCESg+JMla3sHbeC7NWb+/1LODCrssXTpxuTJtWl9U4ZmD6q4/OIANMriflBsQ2VBbLXb3ndx+DcD51IP8UGDOW6e+cE6jS+qcmCxppfovWQ9A5W0IRiIHx3k/fIbPPJXnbDgKgaAJQyOhWkbDEkPa4II/ntvSvIbBEojJYTcqAJBHsR41VFD9hpCZtYToFiEuoFlsKkv4N58k4OtjPgkUY9Np/srWS1cWgIpiM1OA/S4XkwurEtTkxBKXpI5g4Of5yQouIuoDNoW7pBOSAhTCL+RFRNtELxoIwIx7SRfQBS8QHjrc0o6fFzq+sr9agLJfhrDAXSjNOJ9ChOUlcv93lTw/FTGbCzj/3TiPTmv1laHQ3jDcv2yWkvYkowksfTf6Rwqi4ye4loxCZPiOAa2Qxf2HLKmbn0Xm2mIsAx5z3MAMG/LYZCf3kdwECvRppMoYbd2mst4PACcRinlJTxjXFvYc5yo/MDyK1w+tZnuG11UXGd+Skg4HO8rMWePFcW+DxYl5bjALXeKtx3BhAHkfMIkos5FICSMnrBznAPIlF X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d529020a-423d-4697-9a79-08db5d34b841 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 15:28:41.5706 (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: iLI0VuTG0ebdasIHffRzdfIPVvPfwoM6TSvvo1iXdl5XRZRaDdKiLa26kTVQQ6xdxAH8M3ZGvXIfzF/YnDVLbw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6871 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-25_08,2023-05-25_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 suspectscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305250128 X-Proofpoint-GUID: h75LGx9TAHkjKxjLP1vXaW_jPOnD2Lv_ X-Proofpoint-ORIG-GUID: h75LGx9TAHkjKxjLP1vXaW_jPOnD2Lv_ 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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. --- 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 1af51c4acfc..e14f514cb6e 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9267,6 +9267,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 Thu May 25 15:28:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 70090 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F6AA3858D32 for ; Thu, 25 May 2023 16:01:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F6AA3858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685030512; bh=6bbxc4c1jouaMm52h8ANLMdugUP0IkQ0//pJ6002zB4=; 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=oX6yWVtlr1A1Z1AZ2KjNAHvqnwKf1QGxtY6flF6ZcAdYbkiKAHTRelNFjlBnz6VkH fH18nQB/Q44+QkYCiiZjPqx3zqn+wuukuDDPaFcz6JPoF+gDgxWszG9NrcfTpchjrM fxo7d5WQ5WLTFFJWl7iUt7ZE1ZHZusNdASqYr7Ao= 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 F29733858D32 for ; Thu, 25 May 2023 16:01:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F29733858D32 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 34PFt1iQ001711; Thu, 25 May 2023 16:01:18 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3qtaqwr0hr-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:01:17 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 34PEScHo015828; Thu, 25 May 2023 15:29:18 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2101.outbound.protection.outlook.com [104.47.55.101]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk6nag8k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 15:29:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lFjybhfDKcmsvjIHM5zCUv/1/8OnCJklzX6TsI3Fnq9aH0z8gdgOZDP7d/K+q8GAV7Fk0zqPzI4Hk4lG9faeUrbULDsoDQhkPgcONdmfNLocMXl5yKbPRjK7bMZHfVBMx9Zl4VSku16HQo+E16JVVvJEl0lcG7hSUHSSJXez432qvhB47J2mu6ChXLAbafB9jTTiJ/sP1Mox3bWT7q1zhSeNh3yKiy3jDG8kv0VFx5avgLmBzOocP/DSl16ixHiw8rBKrRoZk/7wgVYPp8x0sGPhbgchbL7jdJvdKtcmCIy0s3lfg3+7AgOIs/j6LHOjyao8a9UsC4aurwq5T3afmw== 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=6bbxc4c1jouaMm52h8ANLMdugUP0IkQ0//pJ6002zB4=; b=ice4ytTNqyWDBxRa8QTyWUG1HHPJ1yID/TIWqfGrxwmasY7VnvpzdzDI99Ld7e18qcsEzgMX/d89iFWUt1VFPir0PyMFqa9LqgqwXkofYz9Mo0l2CObgEUvcuWZsyeibcO0Y8ZcG57bVj7mRLawC1eBbJ2SRPOZEuaL54Izh4SnnfbABg1OcqGSlu5eccBMa6r3O4d1XpOf5nKOXxqN+SQqDnntRkHDXMbmiGp4MuwERxp8zHMHXCcrBOdU2pFflWtBOwzMISAJKWUcD3S1CEb8b4nzNCIDMpG73Az39ppUop6LZw70AExrRMwDbnABn78exCht9EmvqInG62qliZA== 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 SA1PR10MB5842.namprd10.prod.outlook.com (2603:10b6:806:22b::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.17; Thu, 25 May 2023 15:28:45 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::7aa1:df38:105a:21fa%7]) with mapi id 15.20.6411.028; Thu, 25 May 2023 15:28:44 +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: [V8][PATCH 2/2] Update documentation to clarify a GCC extension [PR77650] Date: Thu, 25 May 2023 15:28:21 +0000 Message-Id: <20230525152821.2989599-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525152821.2989599-1-qing.zhao@oracle.com> References: <20230525152821.2989599-1-qing.zhao@oracle.com> X-ClientProxiedBy: DM5PR07CA0063.namprd07.prod.outlook.com (2603:10b6:4:ad::28) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|SA1PR10MB5842:EE_ X-MS-Office365-Filtering-Correlation-Id: 2131938f-0363-43ee-582a-08db5d34ba3d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nNpSH/79EDTWjoNiuOOYRCaBohZ20RggFAVcZdD/iXBGkWlpNuTLm80+7hN3JJZFRJjb2sPS3+eOfLF1MxfV6NJ+Aeup0ugXa3Wq5DYSLjoK3v4l0zxi6EmhUOC39D4yMh4vhmaH/Wmej7xuAXnimTJxkK3oZqBoeolwnMVrQO/M7XpkPmC5r6PhRt+USoceVhgZE0ehbiFmGA9blJBpOrakNCMqOgqVaqcmr7znsyOrLBid4nwci5V9amCR4DdHw/5XXOR/aLw0rt2foJiknw8N4eiOndr0/H3/fbIQYwVpcePsOJmOdrTcCk7fOk+JODusrKy/NnLTVpLsBnGBCPsSWOW2NwiItBUhwiu3iL4nXSyr8mO3fttDRDHpEoYsyduaMaEZvJZNhOH19BmqQcMRqqgFA14Cc9SfUqNe+k0P/H7Cf6Z8OsbZb5Rc7ibQ01Qdi8BDh17JljIgqTZof+h/1RibplQqijPmTdEoQb26BSX8Bv5iK/sbmL3kjtEzKPLrozM7Xmrrg9OGbRX9taZKYs8lNFrHhXdaaBmaOno= 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)(366004)(136003)(346002)(39860400002)(376002)(396003)(451199021)(4326008)(8676002)(41300700001)(8936002)(316002)(6666004)(6486002)(84970400001)(66946007)(5660300002)(66556008)(66476007)(107886003)(478600001)(1076003)(26005)(6506007)(6512007)(44832011)(186003)(83380400001)(2616005)(2906002)(38100700002)(86362001)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: yEFjwsMuChUXwwyq920kG3Kezo0G+BdWRhKgug4JdPyBHvaRutGgPvg4ASOTu0Pflc6xZqEZNxeN/5+ck3gNqUjQHnE8ZtIHCfzuMg8ONTVcQ6AcfbGQAVDqb2Q3AFLkO3maoyrl7ENft9rO6FcRC47EchqyqvWR+7LF+mB1b3Lmm5mYdlcn8zV+rU+EM6Fetnz0K5TPOV8Tr6rUTPN7Lu/B8x8QgDGq9RdGUry4sPLhe0xF88IUgNdc1IlaNwX1sMGTSPVPOmurCkfcF2csNRPoukyqZmsjD4StqNJ9JQG6eWqQbn3UFXqkHLjhFbpyDO7xxFy/YW4zNEWLccNjEHJSRxkT7oBUJLHlQgdxUzdDfMzo6EdmX4j7jwB5jxrLCfu2kqCcGyWDI255scz4Z8baF+/2SESaO6Mi4x3TkoEKvPsFmQ+OzU2obqe/y5J82+7g17ejrKMEo7u2bTtjJKrEu7njuItuDH4Jqoxj8aA5xbzAGg5o4Ujmh2as1AY+73tCfqs3vQ1MPvLvlEg353vp0C+GUIownXO2mzNxhIrZpfOvOHWB171f0ezz9GRtv64hPGAEOd4YPtBn5SPKuo5HIMZbJVpBSrXxKRiUX5hLduLqsjHtroBJ23R3KeszYR6b/RsXLlLWnmMz05PhAzbeSNljMbaD4hyh75XSYGiyk8xrkriFg9DXD/vVsLosCmpfte0GZu+jw+Tflip48HPotpHAJeXvUatE4MywB2yeUvVFP8J3Or9M0hX/DE8dLJDwgUrF8TPQAmK8SwAvkk0utzz8Zzn8zNjv5jl0SiPiuek8iRrBZ8YTPC6APmr02QAa1IUlQuhavM6K4id35FBVPHg9JL4WMdXl7e7bdLHGDzsR0vEIliar/X0bdylQKE0K94VlPUkKpftiZdxf0eQuo8dDzPTFjTnpNhW12Sd5popk1wzQea5wPGoAqbk/7wrUdbptSvtivBAVeRGvSuPnNPmBqu9P6W511I1eYUen6yDTIeQGG4ZZk+XW2bMKEN7CaE4U/RHDYYibvdt1UjLrZ2aIqam1MnYoN/loHfYEbpbWadyFp6IrINAKU9zQ3m6waGtkItSKDTlDQe/M8tAmLbw2UImYCO6r0zTRwWa1dejPMCgQmTvLQjAdqKAyJIrKAI1lk98sS2a6ofw5IUaWAYXr2HlNjG0MCtNu2jysMnLirjOaSPqIceRjvIJWeL4c/M7daPE90Y+MyWb34Xi2d2rg6KSlnx+Ivu8VTFe9mxO7yJJ6OUeLIH3/cBCIBd9XBuMiFZU80Oa0DC5sqaKQS97Zra6+RYhGdyTdhsFnbRi0Z3TO7n4AGWZzEqu99hNNoK8M2ftbkJg6YTitWandVyfOLTESJlcCF38hBCCXpN0ad0BKlt0kidqwJ6GUtbFGdW2ENXdHqXUxyBCCerTuMuuQR3SfklGrm2TBZFx58FPlssPfIebYwjl2lwmIw8ZOaJUtoZZhAgG5XQ2FZ64qEymqy+M7svA3s069iIn3OEQGb3lU1N4mIWrhRzNDuFGz0U/BzNFPeYP61ooRa24YfYzwDJBwzdlDihqxTR7MwNgm+OsIrq93N0UBSbQE X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ITG1Nj0UGE0V8YsHKSCmKEjFTtKcQOn4Z3SLMsH7aYjQwpeut/SBw2rtKxyXcGPS2ChYinbZfQ24Hc4NwmxNB801fX5RcU5EYt+lLDpY8VtYihyWISMnGvfihOj08H4QdQGBy3RTHAz3S8Dkl+MN9UUlmbVw/GWxUZbo+OpMD4FmnKkL76qxO96IwKJSXN+AnHfQtH8UyITetEK5fG/2BoXbuD6Ni5cR8kfdLAU6RCXhjLZsG8Eq0crY5vP695DVCODNtfM8nyW100983fAOeMm8Kre8aO0ESkbD2SoPWmmV2yOJLOGRsGMERld13NJfraveE/R5g3oE7monwI4Vw5kNfpb86cLTP1s8at+axVeiejWrqM64e6Inc+vZ/vkZ1dtqB90kq8+ckJeY+1ceTnD1+a0EtmwY74SjFWETXyONvkARWx1Rq5OQ2w+JBgBlNaBawpqA5jwdghlLNjJCSVS2/ppqkU18u1ggpWvvgSdrv7AiKqgHkMt1m1nMsGZwMeOAIb6oWG4yEpO0cd3xbItYhr/A6Ee0E+Z4Q8a6fZ+WQpWXqcczN2j3l/BzzyILaZJc2ioVsO0mqquq1bARvF8KVwdvE50918BVJkDNTb6p7yPBxsP/k1OkJNhmc8kazledZtHX9tfFQVnl/TRWEcWtYx5akKgse5leFaJAzoOtIBeDAeVd9z8vkCUPLTicHmj912INI3oGHd89BdVIjHomTaofX9lURpCwHRNOwOnHcxPECw5sTcICZC39KplTOlO8Rkm1f0FJgtJMIeRI3SWhXl6u6OX0ywpNnKceHyl35vyObawj7uhagbj8Wngwzb3RGPHMAMNLhqbgHFrVcds3usB08Wa2DJV8TxTa/dhyLgx9ZwMv+RPsgVAGkz0xe3JOKsTPrbArGG5j6CD2WFx+nqBXG2rH6WzUQ2ctLv1pEOfyW3kyoiKoxwS7GT44 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2131938f-0363-43ee-582a-08db5d34ba3d X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 15:28:44.8998 (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: D9Du3Aa3M3G2nQzwnYH8W4Crr68s90W5kQXhmHLRuj5h/2DVH/WCD9ONHclmfwkYPJDbMtVGlTWYiA/KkyWGOQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5842 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-25_08,2023-05-25_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 bulkscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2304280000 definitions=main-2305250128 X-Proofpoint-ORIG-GUID: p018WchEvicDNfbOlltGooh-xQEM2kYC X-Proofpoint-GUID: p018WchEvicDNfbOlltGooh-xQEM2kYC 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 warning will be on by default starting from GCC 15. " 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 e14f514cb6e..ecd10ebb69c 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9278,6 +9278,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 f9d13b495ad..17ef80e75cc 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 warning +will be on by default starting from GCC 15. @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" } */