From patchwork Thu May 25 16:14:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 70091 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 4CEA438555A9 for ; Thu, 25 May 2023 16:15:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CEA438555A9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685031346; bh=9YlfsMla0gcP75BzBj+zsz44iYPjc18CeFarUyCO4hw=; 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=pqLf6MVfjCr5keJmDOGOecdO18tFKEsTsyVZdg2/sFQM4IzuKdS5tvCTtMe7CD4YR RjIjeMlxy3xLrHghGcVYDXUTqgleh3jTaiGYofafWop2JsotTMPAIKTm8Oojow4DUg 5xWoSw/cnr00PtGZ8UjMtrZWtvhkDfkOYAhOgiI0= 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 885A2385842A for ; Thu, 25 May 2023 16:15:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 885A2385842A 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 34PGEmvL031880; Thu, 25 May 2023 16:15:09 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 3qtb0y803m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:08 +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 34PFwihk015873; Thu, 25 May 2023 16:15:07 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2171.outbound.protection.outlook.com [104.47.56.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk6nccmp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:07 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Mf4eusveSlVkWsOYoZ9nWMyNofV4ksqMYDn8IRgxJ2XQGhI2cIA0me28J+IplO4RUu7hXJAL8WBYnb6IKYwJxnXl0ZLa2FbzyAx/aibHK6R4XoryhXn276l1FtfoN4zXazwlotJKNgjOqotxo1gXyF05J1UgwuwWQrMPJXx1HVVlxLIutocig4cJbPIPZnoeXOlw188ntAOY/rXbZpZeTGsAG9hp/Ji5N6FcmzT0DcscV5bcYzHvTLo4Dx765LPjUH9JyIWgQoTndVtb87L3gufoloRLEO9dWIFP7u1qQLCNcBXeF2tNBTqcWMH1Zoj1KB7VLxw+lY09oTi8qWv1bA== 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=9YlfsMla0gcP75BzBj+zsz44iYPjc18CeFarUyCO4hw=; b=cZPi2dduLnPRNL07HZXzIXCwANo8s32X7RYitOCzd3g5wU6v7R1p3yzgnkpsR9KlRL5AwPYsya9t8RrF929jbJkOLYbJuzOq7/U1NxHrzA6zSEKBECS2DBQ2aalj+DHIj4hc8JVVULqkFt4AMibh55bO0FwkX5TLlR99R4qWJ7VsJx9GTT0nfoO2Sv9NXjBaOcYcIZWMqpTMhCU7x8+OQpq6VWHG1IW8TCFnodQA4EFlZEFJY5xzL1uZMKQ91xGd8sAm9RFZkiDaI0g+frNQtusm49MAuzbNVChZvtkmdbHayEfU4Vw2uLU5heSuWhFqHTYu+Wb1xLTmyA6JIlLoBg== 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 CH0PR10MB7440.namprd10.prod.outlook.com (2603:10b6:610:18c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.15; Thu, 25 May 2023 16:15:04 +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 16:15:04 +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, isanbard@gmail.com, Qing Zhao Subject: [V1][PATCH 1/3] Provide element_count attribute to flexible array member field (PR108896) Date: Thu, 25 May 2023 16:14:48 +0000 Message-Id: <20230525161450.3704901-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525161450.3704901-1-qing.zhao@oracle.com> References: <20230525161450.3704901-1-qing.zhao@oracle.com> X-ClientProxiedBy: LO2P123CA0017.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:a6::29) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|CH0PR10MB7440:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b3c5961-688e-48d3-591c-08db5d3b3300 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JUNoQurT43HnKwWyfZqdtxF4VlNXYmmSjq/gwa9EdalAK+Zr4kv1dPeBzI5ZneAVendbQ7X5UvMbp+Ri7ByurXfiW3xjoD2vciz/Uteuv2wua8V/iWSDj/twMSo8qJCkTjzsaGbn/tFWW02jMqVvAXJDZkZFOQFE1oQzceRR2KJaLmSbIM3P1maZdNw1zP7gAvsBmkZYtAL9j8mPQWzdMgoBmCne9hg8alQ3KhZwS2XpywRdiSGy1Jidn9dvSrSGKVoAFUYXpEjluMA9yBhxuWzdtCrKDGqUQZtMDl3zoS3+4Y89/E/kwtW1DL9MTWbGNB0E4+HTdexYBCIn+XY86PRtL8cPKQpE1L2rUz7bImpUvRJ31oN6F+8zoxM7bhaTlRYkfYnUlN1m8UuVgYA7qNUXwJMj2JODvdDA/nJw8Jg2HQ/UeJMojvv0wHW72DKJTGtFw6k1f3hh/wD3JP5eLGXse8gztxrRIzyZSw0TUKshvAooForbqPlPDBEnNyF9XswlOvaqDTO8FqB0tm3gdRyJigfqfFpD8KZHAPvpF9Q= 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)(346002)(376002)(39860400002)(366004)(396003)(136003)(451199021)(6666004)(4326008)(478600001)(107886003)(316002)(41300700001)(66476007)(66556008)(84970400001)(66946007)(6486002)(38100700002)(5660300002)(8936002)(8676002)(6506007)(26005)(44832011)(6512007)(1076003)(2616005)(83380400001)(30864003)(2906002)(186003)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: W3EHgEzL0B8U4E+obHBcMWKm2ZcU5WJv9PQFlFDUEdqFMx2q1/feZSi51m3j3S6a31HjHWfj4Jy7RG27VBZxQV6Gv6gY/ApcvrQvnBxlN4Q5OH0p50JerVvwFvs9aDfOfmuMLnWs+4/KBVbepK7lOPZZrWfkP3W7tg/tQp3E4qcyYTcBth79rgsBHKQHMosPN6kB8MU3Bdx+Hq0F5UkFpZ/NmBMKgBDhxUWOOCsoN8l0moQJGM3gMXmVR7QUAvj21x88O2bP9UovzS4tkbSlmTyRF3rqLHmzsajasfI0qJwAn29Gri/r5WEST1VkNqDxkL6mNJr2MK3BI82Se3m5gd0z0DArJjwbrnE7RWSmf0A+UwDBscKx1fWKF8jampqhXcyAlWzcR/gglBMffvrJkFfyaJzVpBEVPLXYmqfUnixX4RPVm6F+p/pVr4Wh18bN5bRuCcJa2tqjXuiniNvkM5Gx25dIrEfZSsLydLrJACe2WiF6UnZ0edFxO9TmvMs1t6q3IWV86BKgb5/oIaLBKxrw4RlQTVWoGW1HWYjSi0jx4+x9sSB+ZPDtGDoZUjpxfdT0XoXMz5omjyLp0HA2PQ5k887c9ncO8oqbCUDGj+wLUuebhnpAX5WENs54rT28L72xTbJJfwd+oB3aC0rVhjZaznWuKsrXs1+8gQMDwJA54Br4HJME6bEOvWGtzrM+OeyugrpeWkwU5e6Bj2M3d1g6aC4Fo63dXO9S4AQJ3n3ZYAcAUMGI+MbCm47a+y7i8zKQorryv69Dcg0AH4Beb8Zeuve3CNGF1X7TlTflsVovx8vKb9/ekfR3eZUX+lpbH1VXqCQkaMtOgQp/QW/RLuV0H+5D4mkRR0IzSgdoX7lAm3ZU7kCAFEM/2nxKkQKllNQoy/VsSCe88OOG/rDXnlf+Oh50RcjUjC98JYBV14zbpxvHE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Cd1dYs7buKXWPgqx5B4DYkb4/sTnyaBEsiMQPtcm2/TJrrm5IfuNfM+j9UwaZ1sJYH86qAcQ2gaFvlCygyoqvw/JC3dXA6t5EyTPWzmYxsrR11B3pKt3L0XFOYdhgcdeGrOZvRPODEg1W76vamLNmYkXEg6EAS2qUTYHFXddIAaLYak4VW1gV6muZxtOMKOCVLfT5Wd0W5LtcrZtmc9mk9CqOxPhJbKwrHEOfLnsZbhgoIAoTu8262mRnDqGLzF/uKgHUcl08jeOFZRcxqnsiVf1szxR6ZSSxaID9s1MbMDjJ7iuXnXVBcOoj/8Qk8eVO3ky8mv7DBYFpHPVSa2cDrjTiDmpIL+jFAxsWEHQcy4EarsXapI/QQYrextJhoHnJ00bVgkHDhopeZExsPDg4tEw9BikBtrx43OCzjkgMSQ8qN6MWMt7De0LLpHqcrmF6J2AuAM5tfS6f5DdmrL8CsPvu7WsN7ZLeJO8hvkDWCHmdMY8KhhPknHyZq8vUsIO29I38oC3uRTje9xB7JHuhcII/gSA6gcz/SW3LPeyl7d9iWGORpfhZNC5E3L+8hEFenbN3A91ImjdimlmwY21fYG2K4JQTJbhoxqtysC/0x X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b3c5961-688e-48d3-591c-08db5d3b3300 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 16:15:04.5369 (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: lOK6IE+wUTChdDtlmRtdZvT+agg4cCDfh3j2/VFCp019uDBRJ4w6KJ3NYmtZB0Vrch9jPvl6BTAAU16o+cFAIA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7440 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_09,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-2305250134 X-Proofpoint-GUID: FuoNRDHPWfBhuiBzCzft75QGuOGJp7vO X-Proofpoint-ORIG-GUID: FuoNRDHPWfBhuiBzCzft75QGuOGJp7vO 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" 'element_count ("COUNT")' The 'element_count' attribute may be attached to the flexible array member of a structure. It indicates that the number of the elements of the array is given by the field named "COUNT" in the same structure as the flexible array member. GCC uses this information to improve the results of '__builtin_dynamic_object_size' and array bound sanitizer. For instance, the following declaration: struct P { size_t count; int array[] __attribute__ ((element_count ("count"))); }; specify that 'array' is a flexible array member whose number of element is given by the field "'count'" in the same structure. The number of elements information provided by this attribute can be used by __builtin_dynamic_object_size and array bound sanitizer to detect out-of-bound errors for flexible array member references. 2023-05-17 Qing Zhao gcc/c-family/ChangeLog: PR C/108896 * c-attribs.cc (handle_element_count_attribute): New function. * c-common.cc (c_flexible_array_member_type_p): To this. * c-common.h (c_flexible_array_member_type_p): New prototype. gcc/c/ChangeLog: PR C/108896 * c-decl.cc (flexible_array_member_type_p): Renamed and moved to... (add_flexible_array_elts_to_size): Use renamed function. (is_flexible_array_member_p): Use renamed function. (verify_element_count_attribute): New function. (finish_struct): Use renamed function and verify element count attribute. gcc/ChangeLog: PR C/108896 * doc/extend.texi: Document attribute element_count. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-element-count.c: New test. --- gcc/c-family/c-attribs.cc | 51 ++++++++++++++++ gcc/c-family/c-common.cc | 13 ++++ gcc/c-family/c-common.h | 1 + gcc/c/c-decl.cc | 61 ++++++++++++++----- gcc/doc/extend.texi | 21 +++++++ .../gcc.dg/flex-array-element-count.c | 27 ++++++++ 6 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-element-count.c diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 072cfb69147..d45d11077c3 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -103,6 +103,8 @@ static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree, int, bool *); static tree handle_strict_flex_array_attribute (tree *, tree, tree, int, bool *); +static tree handle_element_count_attribute (tree *, tree, tree, + int, bool *); static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ; static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); @@ -373,6 +375,8 @@ const struct attribute_spec c_common_attribute_table[] = handle_warn_if_not_aligned_attribute, NULL }, { "strict_flex_array", 1, 1, true, false, false, false, handle_strict_flex_array_attribute, NULL }, + { "element_count", 1, 1, true, false, false, false, + handle_element_count_attribute, NULL }, { "weak", 0, 0, true, false, false, false, handle_weak_attribute, NULL }, { "noplt", 0, 0, true, false, false, false, @@ -2555,6 +2559,53 @@ handle_strict_flex_array_attribute (tree *node, tree name, return NULL_TREE; } +/* Handle a "element_count" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_element_count_attribute (tree *node, tree name, + tree args, int ARG_UNUSED (flags), + bool *no_add_attrs) +{ + tree decl = *node; + tree argval = TREE_VALUE (args); + + /* This attribute only applies to field decls of a structure. */ + if (TREE_CODE (decl) != FIELD_DECL) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for non-field" + " declaration %q+D", name, decl); + *no_add_attrs = true; + } + /* This attribute only applies to field with array type. */ + else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non-array field", + name); + *no_add_attrs = true; + } + /* This attribute only applies to a C99 flexible array member type. */ + else if (! c_flexible_array_member_type_p (TREE_TYPE (decl))) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non" + " flexible array member field", + name); + *no_add_attrs = true; + } + /* The argument of the attribute should be a string. */ + else if (TREE_CODE (argval) != STRING_CST) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute argument not a string", name); + *no_add_attrs = true; + } + + return NULL_TREE; +} + /* Handle a "weak" attribute; arguments as in struct attribute_spec.handler. */ diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 2b4c82facf7..b2bf7a230e2 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -9516,6 +9516,19 @@ c_common_finalize_early_debug (void) (*debug_hooks->early_global_decl) (cnode->decl); } +/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */ +bool +c_flexible_array_member_type_p (const_tree type) +{ + if (TREE_CODE (type) == ARRAY_TYPE + && TYPE_SIZE (type) == NULL_TREE + && TYPE_DOMAIN (type) != NULL_TREE + && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) + return true; + + return false; +} + /* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the values of attribute strict_flex_array and the flag_strict_flex_arrays. */ unsigned int diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index f96350b64af..77f6633b862 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -907,6 +907,7 @@ extern tree fold_for_warn (tree); extern tree c_common_get_narrower (tree, int *); extern bool get_attribute_operand (tree, unsigned HOST_WIDE_INT *); extern void c_common_finalize_early_debug (void); +extern bool c_flexible_array_member_type_p (const_tree); extern unsigned int c_strict_flex_array_level_of (tree); extern bool c_option_is_from_cpp_diagnostics (int); diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index f8ede362bfd..0f25fe0be0d 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -5187,19 +5187,6 @@ set_array_declarator_inner (struct c_declarator *decl, return decl; } -/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */ -static bool -flexible_array_member_type_p (const_tree type) -{ - if (TREE_CODE (type) == ARRAY_TYPE - && TYPE_SIZE (type) == NULL_TREE - && TYPE_DOMAIN (type) != NULL_TREE - && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) - return true; - - return false; -} - /* Determine whether TYPE is a one-element array type "[1]". */ static bool one_element_array_type_p (const_tree type) @@ -5236,7 +5223,7 @@ add_flexible_array_elts_to_size (tree decl, tree init) elt = CONSTRUCTOR_ELTS (init)->last ().value; type = TREE_TYPE (elt); - if (flexible_array_member_type_p (type)) + if (c_flexible_array_member_type_p (type)) { complete_array_type (&type, elt, false); DECL_SIZE (decl) @@ -9087,7 +9074,7 @@ is_flexible_array_member_p (bool is_last_field, bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x)); bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x)); - bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x)); + bool is_flexible_array = c_flexible_array_member_type_p (TREE_TYPE (x)); unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x); @@ -9117,6 +9104,45 @@ is_flexible_array_member_p (bool is_last_field, return false; } +/* Verify the argument of the element_count attribute of the flexible array + member FIELD_DECL is a valid field of the containing structure's fieldlist, + FIELDLIST, Report error when it's not. */ +static void +verify_element_count_attribute (tree fieldlist, tree field_decl) +{ + tree attr_element_count = lookup_attribute ("element_count", + DECL_ATTRIBUTES (field_decl)); + + if (!attr_element_count) + return; + + /* If there is an element_count attribute attached to the field, + verify it. */ + + const char *fieldname + = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr_element_count))); + + /* Verify the argument of the attrbute is a valid field of the + containing structure. */ + + tree element_count_field = NULL_TREE; + for (tree field = fieldlist; field; field = DECL_CHAIN (field)) + if (TREE_CODE (field) == FIELD_DECL + && DECL_NAME (field) != NULL + && strcmp (IDENTIFIER_POINTER (DECL_NAME (field)), fieldname) == 0) + { + element_count_field = field; + break; + } + + /* Error when the field is not found in the containing structure. */ + if (!element_count_field) + error_at (DECL_SOURCE_LOCATION (field_decl), + "%qE attribute argument not a field declaration" + " in the same structure", + (get_attribute_name (attr_element_count))); + return; +} /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. LOC is the location of the RECORD_TYPE or UNION_TYPE's definition. @@ -9237,7 +9263,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, DECL_PACKED (x) = 1; /* Detect flexible array member in an invalid context. */ - if (flexible_array_member_type_p (TREE_TYPE (x))) + if (c_flexible_array_member_type_p (TREE_TYPE (x))) { if (TREE_CODE (t) == UNION_TYPE) { @@ -9258,6 +9284,9 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, "members"); TREE_TYPE (x) = error_mark_node; } + /* if there is an element_count attribute attached to this field, + verify it. */ + verify_element_count_attribute (fieldlist, x); } if (pedantic && TREE_CODE (t) == RECORD_TYPE diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 69b21a75e62..cdbe3923ef1 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7501,6 +7501,27 @@ When both the attribute and the option present at the same time, the level of the strictness for the specific trailing array field is determined by the attribute. +@cindex @code{element_count} variable attribute +@item element_count ("@var{count}") +The @code{element_count} attribute may be attached to the flexible array +member of a structure. It indicates that the number of the elements of the +array is given by the field named "@var{count}" in the same structure as the +flexible array member. GCC uses this information to improve the results of +@code{__builtin_dynamic_object_size} and array bound sanitizer. + +For instance, the following declaration: + +@smallexample +struct P @{ + size_t count; + int array[] __attribute__ ((element_count ("count"))); +@}; +@end smallexample + +@noindent +specify that @code{array} is a flexible array member whose number of element +is given by the field "@code{count}" in the same structure. + @cindex @code{alloc_size} variable attribute @item alloc_size (@var{position}) @itemx alloc_size (@var{position-1}, @var{position-2}) diff --git a/gcc/testsuite/gcc.dg/flex-array-element-count.c b/gcc/testsuite/gcc.dg/flex-array-element-count.c new file mode 100644 index 00000000000..988f41e9f5e --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-element-count.c @@ -0,0 +1,27 @@ +/* testing the correct usage of attribute element_count. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int size; +int x __attribute ((element_count ("size"))); /* { dg-error "attribute may not be specified for non-field declaration" } */ + +struct trailing { + int count; + int field __attribute ((element_count ("count"))); /* { dg-error "attribute may not be specified for a non-array field" } */ +}; + +struct trailing_1 { + int count; + int array_1[0] __attribute ((element_count ("count"))); /* { dg-error "attribute may not be specified for a non flexible array member field" } */ +}; + +int count; +struct trailing_array_2 { + int count; + int array_2[] __attribute ((element_count (count))); /* { dg-error "attribute argument not a string" } */ +}; + +struct trailing_array_3 { + int other; + int array_3[] __attribute ((element_count ("count"))); /* { dg-error "attribute argument not a field declaration in the same structure" } */ +}; From patchwork Thu May 25 16:14:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 70092 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 174183836E9B for ; Thu, 25 May 2023 16:15:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 174183836E9B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685031350; bh=Lp87e/zggjOskq+IfbmAyPFFSj8n5qTs/FH+rr5l3FY=; 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=XnD+SBaeL/YgijcjJampJAcR8XdgiCtWMAokxUdL8f+QFBP+5+z5fqnpeDCfufrcO FzWtXJuGRRU26ixgSWH1XUCUKqoxg1LgIKBoyc5O4gmOhk+YBiTm08mdV+cA5vWgYK nzjrtsxWNqCpH/xzPYi0WkqaJWRMGrlth/e1abD0= 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 D81803857727 for ; Thu, 25 May 2023 16:15:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D81803857727 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 34PGEmiV014582; Thu, 25 May 2023 16:15:14 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 3qtb0yr02j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:13 +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 34PFPSQw016200; Thu, 25 May 2023 16:15:13 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk6nccsk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=foRRM3uUw5YOBFMbvgIjfQzeMklC9PVLGaFLgcvGxstcN1gH0cyT0hm6tcPnw4Ip4fnBoI4goybiOrCEkwnmjRyJ14n5rRZI34mAPskmH+RBtn3BAphHc9fBxVwjaPYBnsO4H2Ku1t/9O6mjTSkZw700INAc0NEsY+pFg/VD1dMo0a5jg9f0AteLubGaSx78X1rox2TcZAfPoXBjqUvDG5t0jjsgkY4woJmwIR/x/FjtX3OxC2NQetgE0OgceOTmAPbNCl1xYSpWjmSfOPKp3yR5YRwXZYtZv+emXbMBTIBesQ9bBXsL7A+7/E+p1EYeVRvbbP50HHY5wj+LmiL9yQ== 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=Lp87e/zggjOskq+IfbmAyPFFSj8n5qTs/FH+rr5l3FY=; b=WoskqxXxa/CMstNBjdDmqJV8kGRPvcYJFkuSEP2WoX50gwgkZ1HSjTJzuDOx5pRKhWDGt2iv/bHaLc4xn+3iSVZ6e1mLmlsqememVWpsPM6mgx0Dkm/9K1MAe4utAqP63e3QjtZ8KnoZj6FvC/+aebTgg439+SP+zi5fZ+FKoC6sQfqe3k9MAB+RSmfFXl4y+aYLD0paX3gIrsYMN2tTn3i84FoJCR6W4KlqjNEswxoNJBlvCiAubBRLGlhN+8GhFxnYhdpXNJFRTLZEE+APMqZnF/Um2ZmylZOMTNEVaFlOSXB1BEbEW3WnzxO4Ly4qgq79/MlatctVzjYJ75noVA== 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 BLAPR10MB5041.namprd10.prod.outlook.com (2603:10b6:208:30e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.15; Thu, 25 May 2023 16:15:09 +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 16:15:08 +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, isanbard@gmail.com, Qing Zhao Subject: [V1][PATCH 2/3] Use the element_count atribute info in builtin object size [PR108896]. Date: Thu, 25 May 2023 16:14:49 +0000 Message-Id: <20230525161450.3704901-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525161450.3704901-1-qing.zhao@oracle.com> References: <20230525161450.3704901-1-qing.zhao@oracle.com> X-ClientProxiedBy: SA0PR11CA0088.namprd11.prod.outlook.com (2603:10b6:806:d2::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_|BLAPR10MB5041:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b82474e-02d1-42aa-7084-08db5d3b355b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: E64P51ZdmiQrIMf0Mk29XprFtoV4ECHid8CX+TmetoKX20czOG9paL+5JMzLOgsZp2xTwgSiyqSIzhJyPPbSVthhSHUpk9TCv6FGdKLmpmgz+LCHGylATm8lluXwi2H2/Jax7LySVJKScQk1o+KaMLG7560kt5D4rnO8NIn5YQBI722DLI7slxaYv3WoYC7Pecot0zve73nDITIDtf4DMVNt3n5g9h69c0oLUOch4zXEVmcPVb9cMzitg+ECXHiIndl8DGwAuX2RJSSJ3IvXoi4NwVmvoTsxq2j1JUDxAj3Iggx4IwiqkyHmnuh7h4PuOmuBm9edf1yIMb6vipFY5SXGxeRA9/wBm/snzxEboqlHjO6MFkPE8tpjYN5u/Lu1q78B+xjSHf/C0GvDb8NPEerlX5EZcst50/zZUxiPSq87DGkCZHaeVDPUERvONqlxnncm3LqXV7fE5VCb+B3OOJT2Bx+E1dijW0MP6pGg5CH6pFL2qTB2e+uFvfDdilIR2BgXSmj/6ZOm6Ntr1Rpx6J1Wrz/1tfocUYkyICelrMQ= 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)(346002)(39860400002)(366004)(396003)(376002)(136003)(451199021)(2906002)(86362001)(83380400001)(186003)(2616005)(38100700002)(8936002)(36756003)(5660300002)(8676002)(316002)(44832011)(41300700001)(6506007)(107886003)(26005)(6512007)(6666004)(1076003)(4326008)(66476007)(66556008)(66946007)(478600001)(6486002)(84970400001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-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FxftRRjkwqJy4a5QUJ/CiHA5gvrHQF29a1Bfe5a9AKycfaT+ojd2pGrbl0BzKwzOgEwOYbHJQTrxsVTUvlFiAEb2K+4ZPLNdNxl04sqpCbB7jDTtWId30p6dj+dz+5utoEY9jTLWPeb6C4xtgRfxDGiu1YySS5KSa50URM/361+abrz8iis5zSh98wUBuQ9sfghxTCj3Hm3jgCyNWsvyD2ZfAlSi6HXw7MYqw6htMPDn04o0QBJBfhSFuuUYOu5YWZv3YuUJh2vc4aLXeUGvj9BrQGnUDLas3bO3SjybgjOQAn7r2Y23Xl36ELOPuIjJp7fgGnF+UDA3tNix102o1NqM3UDGzzPIOoeg8X6ZCtBGsYKbwAo/hhLAQqT26D0PBZY9YEanmAVYLyVawwgvEwAN3kukLoreCESELsQRwJOVEnayZSCsCWTpEWR9EaS4MBwtlSIbceDpcxQKKfXPTbbLnJodmgSQ/eyVeYSS1KGkss5M5LTV X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b82474e-02d1-42aa-7084-08db5d3b355b X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 16:15:08.8891 (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: 2ZV5iSLXwpWriSrxhcJ+5Zu/I7hbXVu2x2Zn+uMXaPGNbsBKyPT7Vt3dnPQinRy21yeIbYaebYOfqTHc7ZlEvg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5041 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_09,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-2305250134 X-Proofpoint-ORIG-GUID: 2KTPWCr-TWSoyxCbXxkP8c7ShKtx3l03 X-Proofpoint-GUID: 2KTPWCr-TWSoyxCbXxkP8c7ShKtx3l03 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" 2023-05-17 Qing Zhao gcc/ChangeLog: PR C/108896 * tree-object-size.cc (addr_object_size): Use the element_count attribute info. * tree.cc (component_ref_has_element_count_p): New function. (component_ref_get_element_count): New function. * tree.h (component_ref_has_element_count_p): New prototype. (component_ref_get_element_count): New prototype. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-element-count-2.c: New test. --- .../gcc.dg/flex-array-element-count-2.c | 56 +++++++++++ gcc/tree-object-size.cc | 37 ++++++-- gcc/tree.cc | 93 +++++++++++++++++++ gcc/tree.h | 10 ++ 4 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-element-count-2.c diff --git a/gcc/testsuite/gcc.dg/flex-array-element-count-2.c b/gcc/testsuite/gcc.dg/flex-array-element-count-2.c new file mode 100644 index 00000000000..5a280e8c731 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-element-count-2.c @@ -0,0 +1,56 @@ +/* test the attribute element_count and its usage in + * __builtin_dynamic_object_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 flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((element_count ("b"))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (int)); + array_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test () +{ + expect(__builtin_dynamic_object_size(array_flex->c, 1), -1); + expect(__builtin_dynamic_object_size(array_annotated->c, 1), + array_annotated->b * sizeof (int)); +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test (); + DONE (); +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 9a936a91983..f9aadd59054 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -585,6 +585,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, if (pt_var != TREE_OPERAND (ptr, 0)) { tree var; + tree element_count_ref = NULL_TREE; if (object_size_type & OST_SUBOBJECT) { @@ -600,11 +601,12 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, var = TREE_OPERAND (var, 0); if (var != pt_var && TREE_CODE (var) == ARRAY_REF) var = TREE_OPERAND (var, 0); - if (! TYPE_SIZE_UNIT (TREE_TYPE (var)) + if (! component_ref_has_element_count_p (var) + && ((! TYPE_SIZE_UNIT (TREE_TYPE (var)) || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var))) || (pt_var_size && TREE_CODE (pt_var_size) == INTEGER_CST && tree_int_cst_lt (pt_var_size, - TYPE_SIZE_UNIT (TREE_TYPE (var))))) + TYPE_SIZE_UNIT (TREE_TYPE (var))))))) var = pt_var; else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) { @@ -612,6 +614,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, /* For &X->fld, compute object size if fld isn't a flexible array member. */ bool is_flexible_array_mem_ref = false; + while (v && v != pt_var) switch (TREE_CODE (v)) { @@ -639,6 +642,8 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, break; } is_flexible_array_mem_ref = array_ref_flexible_size_p (v); + element_count_ref = component_ref_get_element_count (v); + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) != UNION_TYPE @@ -652,8 +657,11 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, == RECORD_TYPE) { /* compute object size only if v is not a - flexible array member. */ - if (!is_flexible_array_mem_ref) + flexible array member or the flexible array member + has a known element count indicated by the user + through attribute element_count. */ + if (!is_flexible_array_mem_ref + || element_count_ref) { v = NULL_TREE; break; @@ -686,9 +694,24 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, if (var != pt_var) { - var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); - if (!TREE_CONSTANT (var_size)) - var_size = get_or_create_ssa_default_def (cfun, var_size); + if (!element_count_ref) + { + var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); + if (!TREE_CONSTANT (var_size)) + var_size = get_or_create_ssa_default_def (cfun, var_size); + } + else + { + gcc_assert (TREE_CODE (var) == COMPONENT_REF + && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (var))); + var_size + = size_binop (MULT_EXPR, + fold_convert (sizetype, element_count_ref), + fold_convert (sizetype, element_size)); + if (!todo) + todo = TODO_update_ssa_only_virtuals; + } if (!var_size) return false; } diff --git a/gcc/tree.cc b/gcc/tree.cc index 7e6de288886..48753b94f2c 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -12745,6 +12745,99 @@ array_ref_element_size (tree exp) return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp); } +/* For a component_ref that has an array type ARRAY_REF, return TRUE when + an element_count attribute attached to the corresponding FIELD_DECL. + return FALSE otherwise. */ +bool +component_ref_has_element_count_p (tree array_ref) +{ + if (TREE_CODE (array_ref) != COMPONENT_REF) + return false; + + if (TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE) + return false; + + tree struct_object = TREE_OPERAND (array_ref, 0); + tree struct_type = TREE_TYPE (struct_object); + + if (!RECORD_OR_UNION_TYPE_P (struct_type)) + return false; + tree field_decl = TREE_OPERAND (array_ref, 1); + tree attr_element_count = lookup_attribute ("element_count", + DECL_ATTRIBUTES (field_decl)); + + if (!attr_element_count) + return false; + return true; +} + + +/* For a component_ref that has an array type ARRAY_REF, get the object that + represents its element_count per the attribute element_count attached to + the corresponding FIELD_DECL. return NULL_TREE when cannot find such + object. + For example, if: + + struct P { + int k; + int x[] __attribute__ ((element_count ("k"))); + } *p; + + for the following reference: + + p->x[b] + + the object that represents its element count will be: + + p->k + + So, when component_ref_get_element_count (p->x[b]) is called, p->k should be + returned. +*/ + +tree +component_ref_get_element_count (tree array_ref) +{ + if (! component_ref_has_element_count_p (array_ref)) + return NULL_TREE; + + tree struct_object = TREE_OPERAND (array_ref, 0); + tree struct_type = TREE_TYPE (struct_object); + tree field_decl = TREE_OPERAND (array_ref, 1); + tree attr_element_count = lookup_attribute ("element_count", + DECL_ATTRIBUTES (field_decl)); + gcc_assert (attr_element_count); + + /* If there is an element_count attribute attached to the field, + get the field that maps to the element_count. */ + + const char *fieldname + = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr_element_count))); + + tree element_count_field = NULL_TREE; + for (tree field = TYPE_FIELDS (struct_type); field; + field = DECL_CHAIN (field)) + if (TREE_CODE (field) == FIELD_DECL + && DECL_NAME (field) != NULL + && strcmp (IDENTIFIER_POINTER (DECL_NAME (field)), fieldname) == 0) + { + element_count_field = field; + break; + } + + gcc_assert (element_count_field); + + /* generate the tree node that represent the element_count of this array + ref. This is a COMPONENT_REF to the element_count_field of the + containing structure. */ + + tree element_count_ref = build3 (COMPONENT_REF, + TREE_TYPE (element_count_field), + struct_object, element_count_field, + NULL_TREE); + return element_count_ref; +} + /* Return a tree representing the lower bound of the array mentioned in EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ diff --git a/gcc/tree.h b/gcc/tree.h index 0b72663e6a1..8f36370a02d 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5582,6 +5582,16 @@ extern tree get_base_address (tree t); of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ extern tree array_ref_element_size (tree); +/* Give a component_ref that has an array type, return true when an + attribute element_count attached to the corresponding FIELD_DECL. */ +extern bool component_ref_has_element_count_p (tree); + +/* Give a component_ref that has an array type, return the object that + represents its element_count per the attribute element_count attached to + the corresponding FIELD_DECL. return NULL_TREE when cannot find such + object. */ +extern tree component_ref_get_element_count (tree); + /* Return a typenode for the "standard" C type with a given name. */ extern tree get_typenode_from_name (const char *); From patchwork Thu May 25 16:14:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 70093 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 481513856965 for ; Thu, 25 May 2023 16:16:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 481513856965 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685031409; bh=uCnHZfvrvdofJjCFk8fgoSX3Nw+arEg/rNJy0kKZj54=; 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=napXkE9XymxBw2U2oqGiSL3VLd7ohqlFR+m/QCLIhxgPz3rlapkUl9UBuoFUw1lJj iLlRS73kE59pp0OMC6HffJNwRkbE5Y8oyM0N2xa2BMFfgG6k1dyRO3eerVgN/UTTGI /tyXHNNJZbpZGZcaB3fQOcVsVZaGHZRJK1LNr+74= 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 8313A3857437 for ; Thu, 25 May 2023 16:15:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8313A3857437 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 34PGEnD7031888; Thu, 25 May 2023 16:15:15 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 3qtb0y804u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:14 +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 34PFPSQx016200; Thu, 25 May 2023 16:15:14 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3qqk6nccsk-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 May 2023 16:15:14 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lKjONG3D0onxklzte+Clrtac4i1O32ZZlkL0n9EBnSj2OxNRm64+G88reng7Kz65eksecTd4/waDrlk3udObXv8GJMQGYBQPcW2zc4OhM2HW3aTVveX+AAT45bwvI+skpnLyQ/NV8zvNjaeH108xUyqwsoeUvkjVimChc5zPDdQRr2WhH6RqnHT3dyD6jSOZMjXsVwbjEF5RJxF/eZRdsavfH8SxvkKoMUOVGBIuM/oixjgS9Em8359S1bv8b72CVFHzqdd+15DyAZCQXhMQKRSptts4iE1G82wTtkNqRrRrg4mSeD2E8pcoD7/jBn3nNwi6cSee4wgvTCpdY9O3ZQ== 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=uCnHZfvrvdofJjCFk8fgoSX3Nw+arEg/rNJy0kKZj54=; b=VgzgPoMhb4BXPgwncWHUXD4S/zoqw/tE4omdaaupTCKTvq2sscIhF7KsdZTinBVK1LxOrhVsxydkQTs5BVsOcEaExMmkF7e3tkOe6Ny6wagcw/2rClSOOgwegOLJunEFFF1ReMaS88oRJdaOnzc1Wlrpk//E5QYbFioyxB0nKh4wDOKYTABdiAFMmj0YvfOEzmJKLI7KGQYC0+St/GWeWQB8sGmnlW/NWn//bkH1kPK+vpsB9eibnESa2L/f3bmTNlaPSIeyeMXhL0xIzFyjopR9wnu396YKtGSp1XlbLZi4grfBqk1afOLCNo58hofjx9eH6Twme2PzXG0zbGpCWQ== 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 BLAPR10MB5041.namprd10.prod.outlook.com (2603:10b6:208:30e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6433.15; Thu, 25 May 2023 16:15:12 +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 16:15:12 +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, isanbard@gmail.com, Qing Zhao Subject: [V1][PATCH 3/3] Use the element_count attribute information in bound sanitizer[PR108896] Date: Thu, 25 May 2023 16:14:50 +0000 Message-Id: <20230525161450.3704901-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230525161450.3704901-1-qing.zhao@oracle.com> References: <20230525161450.3704901-1-qing.zhao@oracle.com> X-ClientProxiedBy: SA0PR11CA0088.namprd11.prod.outlook.com (2603:10b6:806:d2::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_|BLAPR10MB5041:EE_ X-MS-Office365-Filtering-Correlation-Id: 264aec1f-68bf-4fd8-4f30-08db5d3b379c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iGdSgMAv5H5Pxu2dwzpzrFbmpq+DE+9S5seU64UnQ3jIZ312ervf7jOoiyUz0bk5btlfmPQn/oPltdLtt2Qw3RUT6pL6ndfJ4xIE2hF/D+JWuGMl2m1Hp5CZh6L47yuFnvzyXNtbR5GSUYxelBZKtXp8jsj23p8AdMqVp5+zlxucm1r8MxmoX43sw/ZxnAN+WtYVmeeM7ed3DHxux9qkAP1lvD1nhJx1Vr2Py7OovN5BPjolJbOYC8MHZ866vFWegD4SqhPnSPU9q8DtHftoYUawjWL+KQGx0AQjHAO6dnINwK4hkC1nKQ4HDigBda/+hptHvsh/ItYLxXqGbLRAUc/SxkkEYzBgrfyn6UTmfHH7ABFMvNPPf+vYx0algaNc9htBhttPGUIbyvjUvL09VkAUjXJGjeTjeFfADJ+aY1xi2bivk+hY0QizRR2rTm9YuhoiNjb+52FodggamKkZMMgt9V33M21Sd7kyj+UWpFchmYuurjg9SJ8l16epb5LMjet/e3V1YwETqObBviNaN0gRkbMwivNWXUn9G6ZNMcI= 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)(346002)(39860400002)(366004)(396003)(376002)(136003)(451199021)(2906002)(86362001)(186003)(2616005)(38100700002)(8936002)(36756003)(5660300002)(8676002)(316002)(44832011)(41300700001)(6506007)(107886003)(26005)(6512007)(6666004)(1076003)(4326008)(66476007)(66556008)(66946007)(478600001)(6486002)(84970400001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VzICN2pDu1WOqufDq8d81JS/EO0XDFSu/wnpdt0hbZ7YIZQMCJtkkwRYUikiP+IQUAtXQFj59DsuCrHoMYnrCDFTtS5jwohL7CsJfz9yd06800wjRdzBej8WDCNF5KPgiccQ5gnfNPUtF8Fe55w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ph4AWPsRtdug955oEO0DsNVaBptr2ltS3z8bGTEWcOZkytnZ65PZ0tZ8NilUd7WO7Yd0LN9tyDYEfXuCtxA6iNU7r3yX9lvO4Kuhj0ElkWymB+Yf4aDAPnaboAXyNqEud3xUTpgeQcJVZuFjZWAZk+iGCffNor/CjgphjAyFC23xJ7RrIUmsnxKPOFkoaNIu/dRq5bAYaMyMDHeK3NkKB03yJ1Rj0JCKzKXf3vHfbmN5ERowT54CT/WJVbwFesOlDqNwgCZsV6QtxFPUXbcDj4IDL+YS3/kqdLbLKxTKysgI/epGFVFZ4fdI7mD7PsURmhtVqnaA1hNW X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 264aec1f-68bf-4fd8-4f30-08db5d3b379c X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 May 2023 16:15:12.2342 (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: pJTyjmqfwywiXwNnB43ukTbwCpzNCYJnwt/ZpVt0g91B+Rl5mrzQh6Ml7lesRGtWwlo//OdC6fn7f8puXDAtQQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5041 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_09,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-2305250134 X-Proofpoint-GUID: aP81CjQtuq-y0dpAMvxhU8u7uczT2tUf X-Proofpoint-ORIG-GUID: aP81CjQtuq-y0dpAMvxhU8u7uczT2tUf 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" 2023-05-17 Qing Zhao gcc/c-family/ChangeLog: PR C/108896 * c-ubsan.cc (ubsan_instrument_bounds): Use element_count attribute information. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/ubsan/flex-array-element-count-bounds.c: New test. --- gcc/c-family/c-ubsan.cc | 16 +++++++ .../ubsan/flex-array-element-count-bounds.c | 46 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-element-count-bounds.c diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index cfb7cbf389c..04eb05b2c24 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -362,6 +362,10 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, { tree type = TREE_TYPE (array); tree domain = TYPE_DOMAIN (type); + /* whether the array ref is a flexible array member with valid element_count + attribute. */ + bool fam_has_count_attr = false; + tree element_count = NULL_TREE; if (domain == NULL_TREE) return NULL_TREE; @@ -375,6 +379,17 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, && COMPLETE_TYPE_P (type) && integer_zerop (TYPE_SIZE (type))) bound = build_int_cst (TREE_TYPE (TYPE_MIN_VALUE (domain)), -1); + /* If the array ref is to flexible array member field which has + element_count attribute. We can use the information from the + attribute as the bound to instrument the reference. */ + else if ((element_count = component_ref_get_element_count (array)) + != NULL_TREE) + { + fam_has_count_attr = true; + bound = fold_build2 (MINUS_EXPR, TREE_TYPE (element_count), + element_count, + build_int_cst (TREE_TYPE (element_count), 1)); + } else return NULL_TREE; } @@ -387,6 +402,7 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, -fsanitize=bounds-strict. */ tree base = get_base_address (array); if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT) + && !fam_has_count_attr && TREE_CODE (array) == COMPONENT_REF && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF)) { diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-element-count-bounds.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-element-count-bounds.c new file mode 100644 index 00000000000..be5ee352144 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-element-count-bounds.c @@ -0,0 +1,46 @@ +/* test the attribute element_count and its usage in + bounds sanitizer. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((element_count ("b"))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int annotated_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + annotated_count * sizeof (int)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int normal_index, int annotated_index) +{ + array_flex->c[normal_index] = 1; + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test (10,10); + return 0; +} + +/* { dg-output "36:21: runtime error: index 10 out of bounds for type" } */