From patchwork Tue Mar 28 15:49:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 67036 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 3E5A4385828E for ; Tue, 28 Mar 2023 16:02:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E5A4385828E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680019378; bh=vjPH9UImP6g8tFlt6t10CLXPnrP9chg8D5QcLcCSKuY=; 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=tXLtFG0CWkF+1DbL7AQ5BX0PwpUWU/pa0EwNmwSQyjXBXr3c39PjdhymNB9LSpSoa DEQS2Zz9Mhupse0jeraJ/AkoSuWr/Ba2Yw3n9ErDQjvyYWoyN7H1eloZg5e8U/ZLrp TWgrGRwMQJEwKiPwxxkR7OkyDrgUJkAtdSNKYd7A= 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 16F433858D39 for ; Tue, 28 Mar 2023 16:02:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 16F433858D39 Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32SFxZ0R006584; Tue, 28 Mar 2023 16:02:19 GMT Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3pm3byg0ca-15 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Mar 2023 16:02:19 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 32SFLKU5005499; Tue, 28 Mar 2023 15:49:55 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2176.outbound.protection.outlook.com [104.47.57.176]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3phqd6kah1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Mar 2023 15:49:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=erUl00kHpAUf5jVbI2v/edzw5lsI6ivPOjHVjOVwGo2lfwmvHfF+67v1UgnFcgB+s9/+yewLC3ji9rw/qF/7DfrnE1rzKw3C4RzzbD3CJkyZBevXJb0TflAmO28zhiDZkBw5tmTK6bQtCONiXhPJe1NBU1lCdW2P5gN/e40qR9/UyD/aD0sDL/Ge2AJ16J8JKX1mNGwvNq8IvoSkdr9DPQTqBaGTfUD8qR8oik5CUXkMl0RIEMjveh224JZBB7NeCwsq5GeHRVdnTRJ8TbHXHs3IxG5gpZFrDuMXOod/CVX6fEcu6VbVBVZjSgZlvoJI9WZLbBa282YPUtgNi5k29w== 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=vjPH9UImP6g8tFlt6t10CLXPnrP9chg8D5QcLcCSKuY=; b=MihEUvJLlMTvgiK4/+efSMjo0F1mlZCnzn9QlPk6MV2U8ricSMj0nM+DZMmWQ6Mw92WeUPw0Svaah706QimRAYjnwcq0YKrA+GavsOc9iGEkMJcOV/V+abp7+kAm5+Wn4pT6+gIsxQvMkwBA8dftvL3Ypftkokswjuk6LMrh9LRRw+yuvzxqnmiT5qem1soQBUbjkRRwCKjiiF2/wUBJtS7TlSw2l324AQOQ5FKJW8YEoBvcjj6ZSfz0Moiz8bldSUCbFxNh20L71rSOipMypIcmOquOheV1/UTQOh3uGz5/a88EKHuYqZ037IxkgUkkHKJQVwHqgAyLqRjZNyoc8g== 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 CY8PR10MB6563.namprd10.prod.outlook.com (2603:10b6:930:59::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Tue, 28 Mar 2023 15:49:53 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::a33f:73da:f5e2:6c0a]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::a33f:73da:f5e2:6c0a%5]) with mapi id 15.20.6222.033; Tue, 28 Mar 2023 15:49:53 +0000 To: jakub@redhat.com, joseph@codesourcery.com Cc: richard.guenther@gmail.com, keescook@chromium.org, siddhesh@gotplt.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [V6][PATCH 1/2] Handle component_ref to a structre/union field including flexible array member [PR101832] Date: Tue, 28 Mar 2023 15:49:43 +0000 Message-Id: <20230328154944.3946619-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230328154944.3946619-1-qing.zhao@oracle.com> References: <20230328154944.3946619-1-qing.zhao@oracle.com> X-ClientProxiedBy: SN7P222CA0018.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:124::27) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|CY8PR10MB6563:EE_ X-MS-Office365-Filtering-Correlation-Id: bdbf8664-aaf5-44fd-7ff5-08db2fa411f5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1p7dVnjRW+ubc/+NtMOEfN082fJZ7w6WLvbwoU4EL3yx9hH+U8m/QUzPKaII/CeAgLgVEPh4CpSfmf6LD5qbEecIUWLqgNqRkDFTeMiXqCqzutJ47OtZnBa6cscYT9yyZnkNelwSnXAWc/MDKToC3Zvi3xFxauBBrTce6cHcymuzLEiBA8ZHMQH0r4X2e+2vKS0N+ENXJolXx7sIHxb5/27JaNwAavH4upKaVC9NvJ4P5eV///J/NsvRgHeKRJ9rDr4FnJneG5xR/aCFycebhgDCM6ENsW5jzLWBuP3hjwyCu/a7rlpggqwiLjvDuCujN2VImLAaoH13L3Hiv1V0hqo8dg7zUiz3VIZHjlqiXp0nC9YtG9gEfVa5FOWa2AZ4eOAB7FxD2wj3QRJEoMx10eXt+hzrMqfBVYjeSiyE3Tv+yGdo3Ry2Y1XIUGfM/ELkGC+riMbkgeixzbYZ+wzEZaa0E/tJJ/eov9UvQwrI/1jLn2VQ8wrQgrOAY/MBdO+gPbrn6yQnFvmletCbqGcvHUSZnMSMlVIBqcGbD4G1S9VE/rChBcT1PDVk63ekDlRCBmXQacIRqM6OiMuP7QHbeg== 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)(39860400002)(136003)(346002)(396003)(366004)(451199021)(478600001)(30864003)(26005)(6666004)(38100700002)(86362001)(84970400001)(6512007)(316002)(6486002)(5660300002)(6506007)(8936002)(36756003)(44832011)(186003)(2906002)(41300700001)(66476007)(83380400001)(1076003)(8676002)(107886003)(66556008)(66946007)(4326008)(2616005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?/AZ0OHCTFK5iMOB2vYOIFpSV9gKr?= =?utf-8?q?Tdbkhz4nscahVSGGias1tlmcjoCkZtecvRZW6NO0ksYx5L9CdJdn89KacSwecG25E?= =?utf-8?q?BRerdYc1x+p82qTJPaiqeiC6rVaNLHRQe4GMLQyk1hWLRXQFSOwLwR5wfIrlZQeQo?= =?utf-8?q?9KsfWOfqnOi86DImtUMXWNwc87Wyiy2qIXmLilvZ2qIQ2e3Rhbb/h9OiA3ciM1ums?= =?utf-8?q?07sIUjjd+O8KBxOZNFOHgrmZsFmou8ng38kH1g873FsU0xGF1SGW/+kLJrFpYyhVd?= =?utf-8?q?BxMP1NcPhjQyZuK8FxSI8Yan4+g+fuK2ZNQBwex4S1a846uglJUmC4T8JrnhtTFZR?= =?utf-8?q?4DX+z4BC7Sg1K4i9cR/7i8XdXZgRMITlKafUW1qwEuBHwbkbwT/c7swS9UV8FdTCM?= =?utf-8?q?RmVp4S/BUaf9kTKWPUXQNEkQ3/u4Wh0o+B9kiDroUcLNtJww0Um4PAlJk1L0isVJE?= =?utf-8?q?X9/ih9eRFHHtQZfJKHiCFKtpccOj6Qg5JtRJcIdYxfBtOABEkP0a75044MVSPt5AI?= =?utf-8?q?FSdRsU8SAaRhreecnrZRlF2DWFZacMAIcfTejkmHimBZAQfO7DQggn/YwHAFZWRv0?= =?utf-8?q?Ez2K/gGk1c6AxF+wbgJxCA0dHZF9zwOgefr5g6EB7I0Aia9YvcRecVIkzne6Vwo4H?= =?utf-8?q?1csGv6F9rwngpvVy3oU2L3JbVECgfvW/JMuCPPXx5GX6v4Sbt/174MmglZKEkwYN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MS-Exchange-AntiSpam-ExternalHop-MessageData-0: cRPbcuS8WFanr5Wd6jUGNC2F2n3RTTsHcsGXoYgOeYYPy82BDTnWEriAfZONkYC+V9MixIYxfk63AK6CYMSBGqEDI40IskPnOYSM/6Q9K9HsuzoFgn3ca3KntiHTskdDZ7iSZnvVGyCZ8UuLJSJ5ZsUNXXTXvHSsm3WjDFeUgQaE1EmTC7ifNSrqLheKFi/TNT7Lk55PJrKssegvqKdadQxU4W1edJjh1/64laeo1zeAQVN3G9q9scERMElH9s9CKOnqwIhj6nOm6/S2Oc3YQcCObX0QMc8q3Pbsp3M7JUoXp6fFdfZlwockbaKbtUVCADyqECzEtGOvlhw24Bc4+jdkU2bcFgFx/1tm0g0hKq3EiTBF3+DoTcxPu56A2bj/6fpQ4cNEyIRXPmtEdhqakf6ewhGZe5VRGIcKVKIwpiqdELhWl/XUdcPd58ZCptlUAqQKMI2cVsvMHWeLtgqGOBu5kgBBseM4l0YAunmqBvI+AJbKzOT+HVBXM3f2xIkSNLaHVgOssBTZY1o9ne7zKIyslvYEUGecu7pcVJQ+kCrt3fzVx5yTdh/w7UkpHLUbcF7SxjVfxTxOA0zAEtvOcrJHiMHwriKsNpY7g28l3rdq0sgtFAJIKUnMW+dOASc3h3CeSj169+zlr5ytnPRiiU7UraZiy/h5tkDhmLd/94aUUsvcpGA1PWLsnwAWNrt34YiDLC9W91i6YI/vcr+GODTpmAXcN0UwWR4hp2/raM0os8bLWwPArGYU7Bhyu4lJvjdf71dfTO0DceEmGCJtJPw97KsGp755DjZ4viHj7X/CSu6kcT6YYsPJUgRh+vTer/JzAppuxmKikCu033B0ul5NVlRNFCJFqe4vv0owE9+rS1+sPmTe7GhqbzolKVc4+xH1Z7Oz4sfrSG4wOSF6BoHzVVg3CznJ1+5Ev1NGlTQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: bdbf8664-aaf5-44fd-7ff5-08db2fa411f5 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Mar 2023 15:49:53.0226 (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: w/ZJ0Kmh9XarIt6A455niRFQrU+R2b9wsfpymChYP79cUntY/oLSofMQP4qqZ99Xd0+2dFLgV6vMlencn4GkSA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR10MB6563 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-24_11,2023-03-28_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 phishscore=0 suspectscore=0 malwarescore=0 spamscore=0 mlxscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2303280123 X-Proofpoint-GUID: xh_l1BZsuykmeqpAXdL68mBFPh1ih6gP X-Proofpoint-ORIG-GUID: xh_l1BZsuykmeqpAXdL68mBFPh1ih6gP 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, KAM_SHORT, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" the C front-end has been approved by Joseph. Jacub, could you please eview the middle end part of the changes of this patch? The major change is in tree-object-size.cc (addr_object_size). (To use the new TYPE_INCLUDE_FLEXARRAY info). This patch is to fix PR101832(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101832), and is needed for Linux Kernel security. It’s better to be put into GCC13. Thanks a lot! Qing ========== GCC extension accepts the case when a struct with a flexible array member is embedded into another struct or union (possibly recursively). __builtin_object_size should treat such struct as flexible size per -fstrict-flex-arrays. gcc/c/ChangeLog: PR tree-optimization/101832 * c-decl.cc (finish_struct): Set TYPE_INCLUDE_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_INCLUDE_FLEXARRAY properly for its corresponding type. gcc/ChangeLog: PR tree-optimization/101832 * print-tree.cc (print_node): Print new bit type_include_flexarray. * tree-core.h (struct tree_type_common): Use bit no_named_args_stdarg_p as type_include_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_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 | 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 e537d33f398..14c54809b9d 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9258,6 +9258,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_INCLUDE_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_INCLUDE_FLEXARRAY (t) + = is_last_field && flexible_array_member_type_p (TREE_TYPE (x)); + /* Recursively set TYPE_INCLUDE_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_INCLUDE_FLEXARRAY (t) + = is_last_field && TYPE_INCLUDE_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 882dd8971a4..9dde7118266 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_INCLUDE_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 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 fd2be57b78c..83482537a6d 100644 --- a/gcc/tree-core.h +++ b/gcc/tree-core.h @@ -1717,6 +1717,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_INCLUDE_FLEXARRAY for RECORD_TYPE and UNION_TYPE. */ unsigned no_named_args_stdarg_p : 1; unsigned spare : 15; diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9a936a91983..1619d144ecd 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_INCLUDE_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 d4dc30f048f..56add06a70a 100644 --- a/gcc/tree-streamer-in.cc +++ b/gcc/tree-streamer-in.cc @@ -398,7 +398,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_INCLUDE_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 d107229da5c..4a1d481015b 100644 --- a/gcc/tree-streamer-out.cc +++ b/gcc/tree-streamer-out.cc @@ -365,7 +365,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_INCLUDE_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 abcdb5638d4..3ab3a3a0cc7 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 + at the last field recursively. */ +#define TYPE_INCLUDE_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 Mar 28 15:49:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 67034 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 9B42638708BE for ; Tue, 28 Mar 2023 15:51:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B42638708BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680018709; bh=4nGAwciXM4v+9GRD2uQQPqWgZ3/rcDUIpCzUCQVThd4=; 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=T/mWt56wB916dttTKxA2L2kseeI93jL17CGe5kxrL0A0QTSHxnnIzDWXgMwrCJrjn 39iAU9cxH5IpWKCZpQS4GQD7AbcVlhztNGVRGLtGYZdPcWQ59qEx0+oxkjFmpR3+2U 8wPVkbKAHJnbC6ObuYnl44Iok0n8acV6W9xe67qg= 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 3AD0A38708F5 for ; Tue, 28 Mar 2023 15:50:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3AD0A38708F5 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 32SFo4XP023514; Tue, 28 Mar 2023 15:50:04 GMT Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3pm37g000n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Mar 2023 15:50:03 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 32SFLZOP033061; Tue, 28 Mar 2023 15:50:02 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2048.outbound.protection.outlook.com [104.47.73.48]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3phqdebxxp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 Mar 2023 15:50:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T4GcbvSjEtvSpGTAJm+3Xclt6YwilEWGFL4xZDJ3HGyDQH0cRfGDPf/n2wYxR1B0vh7RHzALvX7VjW/N9fOpgFGJ8YfWlxyIPzmM4iBbUvq2YHhsCkgC6oQVQmFqZlez1iQZtJ5DhPYN85sYIfi74dV4enAqomjj1p3xBbMLLMqAR5hD5nzsF3ehFuByFx41oxjMXbrkhzQ6+Ln8bQSENbNwIyHDvREgZ/+pz32ddNLdZRZSCee70f6zvF2hPRb+IhSo7tQgtimII6Ks1c/qhLvxwubX6j/TkcIW5Uzqesd1yduMdxzO3sUj9VnBywox47/JmQGXIO0+Z17UFxD67A== 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=4nGAwciXM4v+9GRD2uQQPqWgZ3/rcDUIpCzUCQVThd4=; b=YoEnq14pD+XE3xfNt1Fu+DaiF1Ko8oc9D/IjRw2HHDZNaiGb4xf6tfwsL6Z3NQnExg0AycBtKKy4rlebMicujg2pYIYOuY5P5yzJDc9kyt/ssNuGTBuyRi9FUFg3EoflILuVRop0lf2q0cFvi/6hfDQCCn9UKHtulo1Pmf0HRW0J5VXDgTXTDe4DH+v5qujvCHgF2E3coVMKxKJpDpIaK/RvwtGiHYslC6s/tKKegv/bUc3xoXRTenxuqnuLZX4JdnbFYT5gaeyptjXPrTWfSPG1oxTBsk1unOK1Peymc20PHy4Tf2VibRVAdbNXxVnBz2WsbvL9tQj1PKVS7rvZSg== 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 CO6PR10MB5619.namprd10.prod.outlook.com (2603:10b6:303:14a::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Tue, 28 Mar 2023 15:49:58 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::a33f:73da:f5e2:6c0a]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::a33f:73da:f5e2:6c0a%5]) with mapi id 15.20.6222.033; Tue, 28 Mar 2023 15:49:56 +0000 To: jakub@redhat.com, joseph@codesourcery.com Cc: richard.guenther@gmail.com, keescook@chromium.org, siddhesh@gotplt.org, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH 2/2] Update documentation to clarify a GCC extension Date: Tue, 28 Mar 2023 15:49:44 +0000 Message-Id: <20230328154944.3946619-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230328154944.3946619-1-qing.zhao@oracle.com> References: <20230328154944.3946619-1-qing.zhao@oracle.com> X-ClientProxiedBy: SA0PR11CA0036.namprd11.prod.outlook.com (2603:10b6:806:d0::11) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|CO6PR10MB5619:EE_ X-MS-Office365-Filtering-Correlation-Id: b302a5b9-1633-40c6-eb08-08db2fa413f3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SMLMI/eDz8jJwFg3tc7Bbz8wjnHd0zYPLJSl8oX7gTL86ZlESahPRhTDAS3Bp+LxEkm8aKuF3jgkEbngwY4jM9/FJTqbcTwat5ySToilHB0Dn8exeAd5AXOC/4WsoQHhnLfiRELmzfle6siUPaANkt6oHHDHOaCc3p6xR99XwGT2vQCfY8b3wphnb7xidYmAU3dze5ad+FUiPbhR/sJbe/6wwCbxRijapQhCzxZGxRdgBz01bfrC109/RYxcJK1K+MtFxPUKa4AzHljECZmH6j/KDjkzWxyLbSMNR1jKaoZURyO0kxZPlZ4ItJSpkafqFZWNrjoBP3vn5wlG6e8dVNcWHLEPDvyAhW3c1ael4tAtZ63Jf6haHGez6FWPbSnKNzGDuiBCrdYsxPywJBhAC1UmFyNZtJBdz+vPY/OnnECMUn3xFgpU1HqJV3pA7YTmDPMisQFd/L8wpM54w1SkfYjJ8KzdJQh/wtFjBXQtQPKpYr8a37s7BPtpTws7rqtTWQZn/J8JXX5bn8yPbLrHpJTomIT3qZ4onnKYpNNDK8A= 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)(136003)(366004)(396003)(376002)(39860400002)(346002)(451199021)(36756003)(86362001)(186003)(2616005)(83380400001)(1076003)(6506007)(26005)(44832011)(6512007)(84970400001)(107886003)(8936002)(5660300002)(6666004)(41300700001)(6486002)(4326008)(8676002)(2906002)(38100700002)(66946007)(316002)(66476007)(66556008)(478600001); DIR:OUT; SFP:1101; X-MS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c6vtjbbdqmV+Uq8slZS3LnO4Oom84ZMI0HhpWjyHyJtE7ugXBovpwFK6o3/PzrIUKpuPzFRdehbB6oK52xlFkEof4DICUj3RGRvNiqL0sCkwll5755QRJU4m2A6ZVWd1lcCNaKpFYkoNlFMxjYWkV9BEyYrjyhRGBuIj9jk/WT66+pmq0VRceB48Uvj5hZH9zjMz1atVf4taKB/ty8sw643iHDktFe066vF/bSSgi5w+BWY6E7b5ll6tdM1BWAwcQKx84UiMo7rp8UWd8Zxf47KqaIfy2KSzqjP/Wb5B5plYy0e2COFspUALj6ECspUK9Ktnb40dNECS5uL1Lu62e5aEamBoUuxbTqasvB0xY4TkjXe08HSk5Ja9EoJiBOL1LXnwFP2oP/qQDvEq6GaRFPbv4NX/EUD1xj+Q3LuoKp9V/ByuYD1V68861cgPaVDdsBG46TjZYkqQrgIZ949s0sDXQyU6Ne2peedNcMnGeQ2oNzkmwEmSWoo8bKzr/JGMwM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b302a5b9-1633-40c6-eb08-08db2fa413f3 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Mar 2023 15:49:56.0716 (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: nEFD1XQHsBAc9tOk7W06KsaESn80CgamPqUEGzwYS16awnELUOlJj7e8oKPARroYzaXuzdMvLascNG3Aw1oGRw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR10MB5619 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-03-24_11,2023-03-28_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 bulkscore=0 spamscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2303280123 X-Proofpoint-GUID: xjpJ2pMuz1Agmaa3zd5vvkMHsj1fUPEK X-Proofpoint-ORIG-GUID: xjpJ2pMuz1Agmaa3zd5vvkMHsj1fUPEK 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" on a structure with a C99 flexible array member being nested in another structure. (PR77650) "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 or a union with a C99 flexible array member 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 or a union 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, 'mid_flex.flex_data.data[]' has undefined behavior. Compilers do not handle such case consistently, Any code relying on such case should be modified to ensure that flexible array members only end up at the ends of structures. Please use 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 14. " 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 | 45 ++++++++++++++++++- .../gcc.dg/variable-sized-type-flex-array.c | 31 +++++++++++++ 4 files changed, 89 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 14c54809b9d..92304fd9c8f 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9269,6 +9269,15 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, TYPE_INCLUDE_FLEXARRAY (t) = is_last_field && TYPE_INCLUDE_FLEXARRAY (TREE_TYPE (x)); + if (warn_flex_array_member_not_at_end + && !is_last_field + && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)) + && TYPE_INCLUDE_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 3adb67aa47a..ef46423339e 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -1748,7 +1748,50 @@ 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 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 or a union with a C99 flexible array member 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 or a union 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{mid_flex.flex_data.data[]} has undefined behavior. +Compilers do not handle such case consistently, Any code relying on +such case should be modified to ensure that flexible array members +only end up at the ends of structures. + +Please use 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 staring from GCC 14. @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" } */