From patchwork Wed Mar 20 13:15:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1914105 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=Gzgsm7G4; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=woMIgoEs; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V08HB5blBz1yXD for ; Thu, 21 Mar 2024 00:16:06 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C2BB638582AE for ; Wed, 20 Mar 2024 13:16:04 +0000 (GMT) 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 428DF3858414 for ; Wed, 20 Mar 2024 13:15:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 428DF3858414 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 428DF3858414 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940538; cv=pass; b=bSdfZpN1BooiMbWw3MyPtZSqCNAeO9y5lhXn6OxWJAVnkaw1MpSMwqE1njuZulUAF9UWviVfS07bJlU3FS79I4QSXzxZu1A7IQHObZGMOEp+AT9DLFjc3ne1QjoultjAHT1MJVDQLft6VNNWDiAEVVTMhAZ4cL3K2diqs0CEAB0= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940538; c=relaxed/simple; bh=Ncfkx8saIEEXV37cOPE7jbcbclF/Hzwn4dIT3CVyxS8=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=w7wJHZGLjkTSaQtub1x6RhSA4nhNYXhCUrFpLugoksXURvs9gdJryjZaM5gzU9gmppPKfWZo6afI3gIujdu8tSf6rcRs8MozLE3z/1zv8RB0S78f/slJftsimaBJIr67SL0JZTNJDDFl1MjJqLdk7DBbih9rGg6ShraTQQjP580= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42KBxD0F004591; Wed, 20 Mar 2024 13:15:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=9BKQw9QARHYFLw88iY1uh6TkS3d1RuIHFceN77RqD3I=; b=Gzgsm7G4x7T6MhLdRQuOM0S6ErgCPGo29y7HCB1hyQ9Zl/BD5RwrkosR/eSJz+6f1lkV ROKclJjxNGtViTSZvLupvSIr5tIH1udaD/EKo0r2qJhrSePZZHD3dCZn1hjidniAwH5B rXSI5v341awLHIvXOeEC8RNQUgXIYfX4qaWIRdo4OH/NZudvipRJd8jXjYa0iTgXpICi YJK1kglzKwAaI3QZVY89XcJJ04NnoNgCtAfrYeitRrGnz/YOJ/BLJ9ZhS2AJxGeJoQLL p/X/YCWpaJ6/Y9LJ2dVd69tEvha6FOlwnZSLKOH0q2p55QsDk34WNFpydEUI6aVtsOVj wA== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ww2bbr342-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:31 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 42KCGCeF021858; Wed, 20 Mar 2024 13:15:30 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3ww1v7r8kp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:30 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YBR8YglcYqSh3hpM7kaXDAh4CEwvu7uq7ctH+5RPTHszNfyx5i5ChuFrNvFjRnpQFc3WLJ9+NSBccICjW/8GXKtUL/snGnJ3ZPsy+KcWkWg2vD7T0RX3lDvYpyOzZIekzBc/JaaGKy8cHKlnjqDBDYXQOEPOIoPA3AqKcrgjj0gpPGg0Ietbwcba6yhaxqmvM43R46oFdVwc/TJxUVDgMLXG7tcijcjuae1BxsAiS3QThN0iw+Lid+Fm76ptLMner2O8Ej7uvgDWs3FVM/QS92tkjFDHvte8grCxnR5kzcIPr0jm9Yx/bSmjhCincbGUU4XOtFpB5/s4upgY+95aiw== 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=9BKQw9QARHYFLw88iY1uh6TkS3d1RuIHFceN77RqD3I=; b=B0dNt5baKEc/YjIWFy0vXFlVDJnSAWk0wI2+Uhid8Twnvs4Al2bFBeCZZOnBuQkXf/+cDLWEnzzPgCI7JNjtUPhrRAAozk93pv9YkrJnVcDA57SdeoLacCxCYn+TNJ/GzIHBgOwRUveU+KinLHOQDDt3f9oBPJjiJbBwxwv/ePKduFM9Q//lk5/W+GhKXl1DEpNtLOhuGddmPs5FneUZ9e3odXBGEkNpdHwuAcsukVV+KxsJnrXpkdSwyujlxyJN8rotxKYAFA/njtch0HuBKPGyF4PWse9QR4Gbln0OSRV4J8hdTuJxnNOTRSqKSuAjCF+1bjViAcVu0FmONIPdxQ== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9BKQw9QARHYFLw88iY1uh6TkS3d1RuIHFceN77RqD3I=; b=woMIgoEsXkUVcFtRhfr6F7IxldXIL5UUxDrydFa55LSFPGpng3+2dzdrEPKQVObkjHchFH/hkymQBSJkjlpQ85U3Dy6T01nE6hpBFAkeyruh5Bj1dgyekVyDUorjkQoAXFqglQbHBCydVmTto6xqpsRe+Egmq1YTgHCN0PlcayU= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by PH0PR10MB5562.namprd10.prod.outlook.com (2603:10b6:510:f1::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.31; Wed, 20 Mar 2024 13:15:27 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc%6]) with mapi id 15.20.7386.031; Wed, 20 Mar 2024 13:15:27 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v7 1/5] Provide counted_by attribute to flexible array member field (PR108896) Date: Wed, 20 Mar 2024 13:15:14 +0000 Message-Id: <20240320131518.2292317-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240320131518.2292317-1-qing.zhao@oracle.com> References: <20240320131518.2292317-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH7P221CA0018.NAMP221.PROD.OUTLOOK.COM (2603:10b6:510:32a::17) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|PH0PR10MB5562:EE_ X-MS-Office365-Filtering-Correlation-Id: a3d09f94-9eb2-4344-d006-08dc48dfcf39 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 10BdP5CdshQoyjnMSpqmbSU+tOZC5IgjycohZc7ntwnoeWTwhnoPWuqHZIRCgvG+5oTD8cczkpZgz2RcpkmH1+g3FOM0r8ZvMvS1Rf/arCcI0C0pJvKbnRvBgJM/uYCGskCA3Tlp2vY8Cdn6yvpLzYe061nPmZoeps6++URxVYvDlKJWk25N9JvSLNNkXAyqoCEdFC9TV0go80EXBnSTr66ptE1sTPY3A70ipE95a+kgsmb4D25dBogBp8fyIZTlkpBq8BTT1WTSP4j6h+vEflg2OW+hHrLqWsVZBCew3ZElY7Y7N3et0eLN8BnuZQ19saTWFFkSS8NsuCh4/gdEaLxOm2HY2PxvRoFpX4KQYrvKcuDC7mMtHHVEixPama4lkidOx5Ip0q1h6LLJTgfydsx25WdERXlfp0ZbmaLWcFAOb91/oQwNxERZ5mdaIMozY7XEBzS5MicUEuq6Bo854BGPe/rDYHg2Cseo80KsdOQEHMhsteL6bL/eD64HZ1omTv8rQwBhUuYx+rkIR4hR5imBfX0TkILWgT/wGK1im9tROQBEaGOZwS7Vy50lXITwLy8cjSqITVraxHnLmQ2FtzfJKugs4aiEer2zNggcXtM= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CY8PR10MB6538.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(376005)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: shAHQ+L8bJ+MKcwnOYkNoKFkKCtkLI6AoEjNThvmJHWoqUAK3KbwboU1mpMkJZVOK2a/JAKDccXHwOhF8oGpX5Mun+W4Y/yHCN4wQ5bkw8Z5R67M2odh7awQB3ANiAy6d0pg4hdwokV9SDRsZkNDAeTSWtwI3XaM+5DD/rUsLCz3AqRxOQzH2CeN/8uocEDflJ2H2zyggdwq+CyH7PGvJC0dSRnYN3uGWd3ixl1kgvytodsALhLH5Z54aXEaTcPW1RagGYebf4K5eVur8/3Ft9/Z5Ay0LiljMfbglIMC1Ei7xBqUTZ5tAYZ1bOG4szj36HIiYXTqmOHvfUQyEKUf2tSXAucC0+3n6PyAQj2BRvvm3ZZatKiay7pnT+rsM7GMToug5clWQCIyiJELdvFoZOUnfNL9YmzNQ0UBeaibmCO7UtCgY/R3Vhhwit8imsfvUCCCYthSboZZHHIu0X8ZDzNQdQTflc0TKTTzxQ4UojHmw9kUts2O8qt9Del+a2ZPYy28kIfAIA559vyNno6bBk/8XDAzqYrw/se3qm/H63EqWL0EYwvwkdmf5n/rKQKcQNKVUyqnuYULDZgVWPvKxG0R4U9mjA9bc9GG2tqW9pLbnmQVgWhqW0/Te61M7YPYaNp8txGUYygO3/MHQ0ABJCE95bDnF7380pla8/ilw6WhvK47M85+0GdUAvWdxZklTiP3epRBZSl+hjT7e47vYkWAssH9ZHytcgAf7UHKaPpaIYvHI0QZoVpS9Zwb0aBcTECsOzXU+b2sm/neJA7E7legwLFghOFz7G843v6XENvtXy66iswL8xWZaneWIuyPbOPma2+sgy4emnuGAeZ+6mh0slWHeuWMOlKAmpUBX3zPrWslWuQSklsFIh0k+JmkXziARuGwqjg6m5lYfY0860cKdRNWs38VwtADHU1AMvj2DRKOGGUFOOR+RGMmsZpyYs+kWZXuwBrowcUaLGoVTYHHKiCBImXMUUFc6dKOZzX+gksXJJyZXAdljxLHNS/hQJ9ri2+FyGSScSqKHRQ184qHbHVua5r4ceX7sOelnGslfdLbVMMKyXZzHV8XXm/XjdsXzMfb7n8eEhSFr7qIlBzlA27axAjIvQB+wRBikKaPPNI+nbLhwg5BDoNlW5NmXA5av1+JVUqhS8IPWLR2wpECImICSICjISvWEDI2dA/1Ekscm1EId18iKdIMUX/0LyXcJwbT/R+bEtjAIOAYZ/xR+ELnkLworkQMbexpy40wrDTEVpnKfxAwHDjgvcLugmfzv5javrb3S2JLDoZQrlJbsnrfeWq+oyijYQm8t7+Sl/Vz/JPBRReXY3cmmSN6iYqvSDkBXIPkW7eYnsJ8mY6Y1vFWsCk6iEbQiyxsk6RmBOOObaFbv+w/3IwKil01q1XyBQt07TMKKRW0xOWLRhw0KD5Iiuxby3acaYkQtoA+PlzKyxoM4Ni29KflnuX0co25h7rMkpRQx5d88R3fVgF1lOS99UeU48/hCA9GePc+i2ayLz4Dx19ZBZf2oFq5ecn141qkIbBds2Yu7agBAR7+Avg9unD4+iMfP65Jcf4FMtn3TY3RvC2m0pFkXFBV X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: aVQOlqKGkupKywE/1aNF9BMuOyTNIfvri5o37QOApolwXCZ5Wk92f3u4DkjVh8fC3I6MfawibdnaLDClY3HXix6rZ18zW43Mw8q+v8YJtGc2lFOLiK3kz2c2FPeB/PUL+l+znJkCNRgCwaekuGqqxxiaV7/FZJIL9Vnmk8POnHJVbW0uJgOdudbU5AYZp33RP/XBmeKtTRKvtLMD+9KsGlkQCwZyadMgzjGKNLDXgX16uw3yePC0scnZhBUiYrWFOp8EIGOcR/M65+maWTbE4JY0ofjPrLw2/pJss/WPRd+oWsoC3DB9I7FbeAvx4/3bZWT8Hh4KUFZtBUde6bq74+foi6pn6dlxpxPHgX4Nu82QR09j0bofktBovrp85HR33Lqxiegi72CYrCFHelGeysLtGdEfKC10sE8CKqO+Kqpfhs6EyWTkxZ2Dhj7YP4wUDWIAVr8lqtbSSNxNkpSTI/b2e9J0wulF1bA/O3nHm2oa5B8j/qjwKgePcL8x8BFXx0eWdLZAtLntymTaa9Stn6fOdoVtOxbx0GATflA9DtG5uhqwWtwQbTKZ7TE2SjSfIG+tprfw8jzNEqoTnSBeuxjgJT7aavJvA1uAJ+3Gm6s= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a3d09f94-9eb2-4344-d006-08dc48dfcf39 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2024 13:15:27.2707 (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: Gzj5MyyVuIRVmraE2L4sGtUezck8fzNP3GgjAcGAr2bKNOeStv29OOB+T7W2SsXKxB32fhav5ZsDG97btEx39w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5562 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-20_08,2024-03-18_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 adultscore=0 mlxscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 phishscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403140000 definitions=main-2403200104 X-Proofpoint-ORIG-GUID: nBpP-ZXIIxyDFYottZJY2yBUd9ZoRffq X-Proofpoint-GUID: nBpP-ZXIIxyDFYottZJY2yBUd9ZoRffq 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_MSPIKE_H4, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org 'counted_by (COUNT)' The 'counted_by' attribute may be attached to the C99 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 may use this information to improve detection of object size information for such structures and provide better results in compile-time diagnostics and runtime features like the array bound sanitizer and the '__builtin_dynamic_object_size'. For instance, the following code: struct P { size_t count; char other; char array[] __attribute__ ((counted_by (count))); } *p; specifies that the 'array' is a flexible array member whose number of elements is given by the field 'count' in the same structure. The field that represents the number of the elements should have an integer type. Otherwise, the compiler will report a warning and ignore the attribute. When the field that represents the number of the elements is assigned a negative integer value, the compiler will treat the value as zero. An explicit 'counted_by' annotation defines a relationship between two objects, 'p->array' and 'p->count', and there are the following requirementthat on the relationship between this pair: * 'p->count' must be initialized before the first reference to 'p->array'; * 'p->array' has _at least_ 'p->count' number of elements available all the time. This relationship must hold even after any of these related objects are updated during the program. It's the user's responsibility to make sure the above requirements to be kept all the time. Otherwise the compiler will report warnings, at the same time, the results of the array bound sanitizer and the '__builtin_dynamic_object_size' is undefined. One important feature of the attribute is, a reference to the flexible array member field will use the latest value assigned to the field that represents the number of the elements before that reference. For example, p->count = val1; p->array[20] = 0; // ref1 to p->array p->count = val2; p->array[30] = 0; // ref2 to p->array in the above, 'ref1' will use 'val1' as the number of the elements in 'p->array', and 'ref2' will use 'val2' as the number of elements in 'p->array'. gcc/c-family/ChangeLog: PR C/108896 * c-attribs.cc (handle_counted_by_attribute): New function. (attribute_takes_identifier_p): Add counted_by attribute to the list. * 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_counted_by_attribute): New function. (finish_struct): Use renamed function and verify counted_by attribute. * c-tree.h (lookup_field): New prototype. * c-typeck.cc (lookup_field): Expose as extern function. gcc/ChangeLog: PR C/108896 * doc/extend.texi: Document attribute counted_by. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-counted-by.c: New test. --- gcc/c-family/c-attribs.cc | 54 +++++++++++++- gcc/c-family/c-common.cc | 13 ++++ gcc/c-family/c-common.h | 1 + gcc/c/c-decl.cc | 78 +++++++++++++++----- gcc/c/c-tree.h | 1 + gcc/c/c-typeck.cc | 3 +- gcc/doc/extend.texi | 67 +++++++++++++++++ gcc/testsuite/gcc.dg/flex-array-counted-by.c | 40 ++++++++++ 8 files changed, 237 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by.c diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 40a0cf90295d..51cf91c4fbfd 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -105,6 +105,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_counted_by_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 *); @@ -412,6 +414,8 @@ const struct attribute_spec c_common_gnu_attributes[] = handle_warn_if_not_aligned_attribute, NULL }, { "strict_flex_array", 1, 1, true, false, false, false, handle_strict_flex_array_attribute, NULL }, + { "counted_by", 1, 1, true, false, false, false, + handle_counted_by_attribute, NULL }, { "weak", 0, 0, true, false, false, false, handle_weak_attribute, NULL }, { "noplt", 0, 0, true, false, false, false, @@ -659,7 +663,8 @@ attribute_takes_identifier_p (const_tree attr_id) else if (!strcmp ("mode", spec->name) || !strcmp ("format", spec->name) || !strcmp ("cleanup", spec->name) - || !strcmp ("access", spec->name)) + || !strcmp ("access", spec->name) + || !strcmp ("counted_by", spec->name)) return true; else return targetm.attribute_takes_identifier_p (attr_id); @@ -2806,6 +2811,53 @@ handle_strict_flex_array_attribute (tree *node, tree name, return NULL_TREE; } +/* Handle a "counted_by" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_counted_by_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 is not allowed for a 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 is not allowed 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 is not allowed for a non" + " flexible array member field", + name); + *no_add_attrs = true; + } + /* The argument should be an identifier. */ + else if (TREE_CODE (argval) != IDENTIFIER_NODE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "% argument is not an identifier"); + *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 e15eff698dfd..bc53a5292f37 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -9909,6 +9909,19 @@ c_common_finalize_early_debug (void) (*debug_hooks->early_global_decl) (cnode->decl); } +/* Determine whether TYPE is an ISO C99 flexible array member 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 2d5f53998855..3e0eed0548b0 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -904,6 +904,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); extern tree c_hardbool_type_attr_1 (tree, tree *, tree *); diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index fe20bc21c926..50f66ade66d4 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -5301,19 +5301,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) @@ -5350,7 +5337,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) @@ -9317,7 +9304,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); @@ -9347,6 +9334,53 @@ is_flexible_array_member_p (bool is_last_field, return false; } +/* Verify the argument of the counted_by attribute of the flexible array + member FIELD_DECL is a valid field of the containing structure, + STRUCT_TYPE, Report error and remove this attribute when it's not. */ +static void +verify_counted_by_attribute (tree struct_type, tree field_decl) +{ + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (field_decl)); + + if (!attr_counted_by) + return; + + /* If there is an counted_by attribute attached to the field, + verify it. */ + + tree fieldname = TREE_VALUE (TREE_VALUE (attr_counted_by)); + + /* Verify the argument of the attrbute is a valid field of the + containing structure. */ + + tree counted_by_field = lookup_field (struct_type, fieldname); + + /* Error when the field is not found in the containing structure. */ + if (!counted_by_field) + error_at (DECL_SOURCE_LOCATION (field_decl), + "Argument %qE to the %qE attribute is not a field declaration" + " in the same structure as %qD", fieldname, + (get_attribute_name (attr_counted_by)), + field_decl); + + else + /* Error when the field is not with an integer type. */ + { + while (TREE_CHAIN (counted_by_field)) + counted_by_field = TREE_CHAIN (counted_by_field); + tree real_field = TREE_VALUE (counted_by_field); + + if (TREE_CODE (TREE_TYPE (real_field)) != INTEGER_TYPE) + error_at (DECL_SOURCE_LOCATION (field_decl), + "Argument %qE to the %qE attribute is not a field declaration" + " with an integer type", fieldname, + (get_attribute_name (attr_counted_by))); + + } + + 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. @@ -9408,6 +9442,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, until now.) */ bool saw_named_field = false; + tree counted_by_fam_field = NULL_TREE; for (x = fieldlist; x; x = DECL_CHAIN (x)) { /* Whether this field is the last field of the structure or union. @@ -9468,7 +9503,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) { @@ -9489,6 +9524,12 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, "members"); TREE_TYPE (x) = error_mark_node; } + + /* If there is a counted_by attribute attached to this field, + record it here and do more verification later after the + whole structure is complete. */ + if (lookup_attribute ("counted_by", DECL_ATTRIBUTES (x))) + counted_by_fam_field = x; } if (pedantic && TREE_CODE (t) == RECORD_TYPE @@ -9503,7 +9544,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, 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)); + = is_last_field && c_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))) @@ -9758,6 +9799,9 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, struct_parse_info->struct_types.safe_push (t); } + if (counted_by_fam_field) + verify_counted_by_attribute (t, counted_by_fam_field); + return t; } diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 1fba9c8dae76..c7c23edc4840 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -776,6 +776,7 @@ extern struct c_expr convert_lvalue_to_rvalue (location_t, struct c_expr, extern tree decl_constant_value_1 (tree, bool); extern void mark_exp_read (tree); extern tree composite_type (tree, tree); +extern tree lookup_field (tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t, location_t); extern tree build_array_ref (location_t, tree, tree); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index ddeab1e2a8a1..cead0a055068 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -101,7 +101,6 @@ static bool function_types_compatible_p (const_tree, const_tree, struct comptypes_data *); static bool type_lists_compatible_p (const_tree, const_tree, struct comptypes_data *); -static tree lookup_field (tree, tree); static int convert_arguments (location_t, vec, tree, vec *, vec *, tree, tree); @@ -2375,7 +2374,7 @@ default_conversion (tree exp) the component is embedded within (nested) anonymous structures or unions, the list steps down the chain to the component. */ -static tree +tree lookup_field (tree type, tree component) { tree field; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2b8ba1949bf1..d7e27da24ccc 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7753,6 +7753,73 @@ align them on any target. The @code{aligned} attribute can also be used for functions (@pxref{Common Function Attributes}.) +@cindex @code{counted_by} variable attribute +@item counted_by (@var{count}) +The @code{counted_by} attribute may be attached to the C99 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 may use this information to improve detection of object size information +for such structures and provide better results in compile-time diagnostics +and runtime features like the array bound sanitizer and +the @code{__builtin_dynamic_object_size}. + +For instance, the following code: + +@smallexample +struct P @{ + size_t count; + char other; + char array[] __attribute__ ((counted_by (count))); +@} *p; +@end smallexample + +@noindent +specifies that the @code{array} is a flexible array member whose number of +elements is given by the field @code{count} in the same structure. + +The field that represents the number of the elements should have an +integer type. Otherwise, the compiler will report a warning and ignore +the attribute. +When the field that represents the number of the elements is assigned a +negative integer value, the compiler will treat the value as zero. + +An explicit @code{counted_by} annotation defines a relationship between +two objects, @code{p->array} and @code{p->count}, and there are the +following requirementthat on the relationship between this pair: + +@itemize @bullet +@item +@code{p->count} must be initialized before the first reference to +@code{p->array}; + +@item +@code{p->array} has @emph{at least} @code{p->count} number of elements +available all the time. This relationship must hold even after any of +these related objects are updated during the program. +@end itemize + +It's the user's responsibility to make sure the above requirements to +be kept all the time. Otherwise the compiler will report warnings, +at the same time, the results of the array bound sanitizer and the +@code{__builtin_dynamic_object_size} is undefined. + +One important feature of the attribute is, a reference to the flexible +array member field will use the latest value assigned to the field that +represents the number of the elements before that reference. For example, + +@smallexample + p->count = val1; + p->array[20] = 0; // ref1 to p->array + p->count = val2; + p->array[30] = 0; // ref2 to p->array +@end smallexample + +@noindent +in the above, @code{ref1} will use @code{val1} as the number of the elements in +@code{p->array}, and @code{ref2} will use @code{val2} as the number of elements +in @code{p->array}. + @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-counted-by.c b/gcc/testsuite/gcc.dg/flex-array-counted-by.c new file mode 100644 index 000000000000..e07147b2b8d0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by.c @@ -0,0 +1,40 @@ +/* testing the correct usage of attribute counted_by. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +#include + +int size; +int x __attribute ((counted_by (size))); /* { dg-error "attribute is not allowed for a non-field declaration" } */ + +struct trailing { + int count; + int field __attribute ((counted_by (count))); /* { dg-error "attribute is not allowed for a non-array field" } */ +}; + +struct trailing_1 { + int count; + int array_1[0] __attribute ((counted_by (count))); /* { dg-error "attribute is not allowed for a non flexible array member field" } */ +}; + +int count; +struct trailing_array_2 { + int count; + int array_2[] __attribute ((counted_by ("count"))); /* { dg-error "argument is not an identifier" } */ +}; + +struct trailing_array_3 { + int other; + int array_3[] __attribute ((counted_by (L"count"))); /* { dg-error "argument is not an identifier" } */ +}; + +struct trailing_array_4 { + int other; + int array_4[] __attribute ((counted_by (count))); /* { dg-error "attribute is not a field declaration in the same structure as" } */ +}; + +int count; +struct trailing_array_5 { + float count; + int array_5[] __attribute ((counted_by (count))); /* { dg-error "attribute is not a field declaration with an integer type" } */ +}; From patchwork Wed Mar 20 13:15:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1914106 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=SUfpNHyU; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=JvwgikUE; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V08HF2Ds8z1yXD for ; Thu, 21 Mar 2024 00:16:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 39A7938582A1 for ; Wed, 20 Mar 2024 13:16:07 +0000 (GMT) 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 0F0BC3858C3A for ; Wed, 20 Mar 2024 13:15:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0F0BC3858C3A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0F0BC3858C3A Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940541; cv=pass; b=BOZIDjwG8ym2Eo/4lBK3CLjqUikgSMNqmWYKmOd2TYkGvrbnmJlahxSqLFGEih/6OsCUbEtNV7fiClQG3dLaF6SBAblamPJdNDDqCvYVT375p0Jgo0imYmn2XOIqrTWAJ2iTazQjqG9OUDcge4tMKscq2jqgNAbOTCsnfy40hFw= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940541; c=relaxed/simple; bh=bZVGkGDNahJt7XOlCnscBIeXtjy13WusFFu7kluiPq0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=xucIBP04zv4+Xc37IcF/6kYCjPiEDIgWyiObhNUAgZJfsJwmvGmdVe6fDC6u5Idl94LoT+fYjjaLfRnvYN0DWgJJUeO84CciCN2em2EOrYVUyQx5ds2S6EWmiFbavZfvS1q8Udb7Dl43jDQL/4shCYahVDe0sM1jFeYYCrxASJ0= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42KBx2UK017014; Wed, 20 Mar 2024 13:15:36 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=1AZezWOO5bw9FWhGHXAObbfq/2stTKRrnl+3mQk2J3w=; b=SUfpNHyUsy4b22q+Wa4nVt3XMlWXeKTBCifrjNZmjO2KvHZdCmANAIcLJ2I27iv+MT8a eASX71b0K6fsf8MYMtyLKe3w9WBkjvBawGSNWUTqYlVTK3rXFXO09rqW6tXSJMX2sGPc ucJpPexw/FjmUUQ8k4vH0t5jRaKPP8jBCDFJe8ZuYNv+m8hyBzm7BCOp8Ya42Ao8t26L EAJ2cna7rv4ylGW9/X9UbrQAnzW7+6aACgj8oUaQphMTYsFQJmttnsqFYpaTxTEYarZV HnZL51CMcTjEnWhog1t7u8WeO7EDZEJkJ7mFVPh+3UxI/kTa2hETijI+NSs8+eigVEFC 3g== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ww211826h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:35 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 42KCGHQE007538; Wed, 20 Mar 2024 13:15:34 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3ww1v8augn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:34 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZmbXR6XQgzPutpVhvPrsEA7MlxwDAU4iONfLqovBiY3K7wiAKYYoUgfpod3W0CohyvPUn7bkEj3Do7W+t9FIadnAJlznLY3+kx5e3MZj6PhtbfXWNm/+3AtQZZjCAI+vB3TGhTTss30ZmCsFwr6iRgBQlq6oSpfG/QH5fBPbK3y9xFTcjMzmPgDeOYQgdEu2uqBICaOYm0yHqjhyavhTrUFr13+KG0SrrmkbAhRHyhSdeiBYkFwqjVZT/mkc+fN2vbNTqCXcbWyD9vot8bPRJ7hG9YuwhZtIerZHjresCWWODpe4b7c8XE9rlelBOSuDv17cXuuWNMhiKSd1KA7Htw== 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=1AZezWOO5bw9FWhGHXAObbfq/2stTKRrnl+3mQk2J3w=; b=ZWVtUHQb0VxhpoM+OHiWNDgW0Pw5YDrpa9dAGsD2KBcKWAgJ7CbnIQyzvSUR3raPIPAAevsYcU94eJ3DqTJf+xSVCvtnnWBfelM0LovaCj4t+Q8iZZ5hnTMkReqC3zj+DjasHnFXsIhZR6QVB8sk/sGhNHommsMCMIlHH4YEHOwBwdz7gbz/NSRkGdFlOyg+mmdU4iUUdwV+Qlrkys7pN41kD43aptly+GSCeh4cCE6p5WWf+ebZuIrSApwF8MVCNwTOng8GykgvCCG2mJIUaw3G4Zjkkp2gpu61hH1Jb4qiFba8b74ihYOkS+z5OprUDGBwXMnc93c8j4O0lFzu6w== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1AZezWOO5bw9FWhGHXAObbfq/2stTKRrnl+3mQk2J3w=; b=JvwgikUEQ9oHrcSSI00IhFlacuXRiEiXOTZLtNNGoz4z5X2Wbt5AyYmWvZJDqEYVN36eJJbmes+baIPpVtcJ4Kt+4IN5VHe4GB2SMZEzCYC3IULCmjBQ9ybXGlmJiW0dFOxRHpzXhOphUgCMinuOxB0HbgcfsLblC0sUBjyCdrM= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by PH0PR10MB5562.namprd10.prod.outlook.com (2603:10b6:510:f1::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.31; Wed, 20 Mar 2024 13:15:31 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc%6]) with mapi id 15.20.7386.031; Wed, 20 Mar 2024 13:15:31 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v7 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE. Date: Wed, 20 Mar 2024 13:15:15 +0000 Message-Id: <20240320131518.2292317-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240320131518.2292317-1-qing.zhao@oracle.com> References: <20240320131518.2292317-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH8P222CA0003.NAMP222.PROD.OUTLOOK.COM (2603:10b6:510:2d7::18) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|PH0PR10MB5562:EE_ X-MS-Office365-Filtering-Correlation-Id: b85ffcec-ce8b-478b-e9c2-08dc48dfd18a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cZ7Mb+GIa1bi1iva4hkldBFjOC66rqs4Ce6Vb2O09Rf67x7QuDzpCuZ6OAeDbtv9Y23UYjokGCER4dB/vxzbuUmDFGXl1MJQ/kgLR+ufuHGrqDQS24/7br7Mjkocrzke2zTQOLqPWEhzYh6v09BYxFMk1vvK4PygRcFuuLt5iiG9pTeGsQlbjz3UoqyBNRwib9cE82pDZBzEQWHq3r8clcuFzyWKRFqp4do35BEaTaoTyS8c4ZcFkoXZoB5tSIjvY2/Yyh89Mk0TpE3OQAM+pR0oZQGalAZ6/laZL3iFfuInWJmDwkW50ts4vyE2WRimtgo23Xyo6ZIpqxUnXjY7NWxaGpgciupdo61Tb92L/o+ntteqYOwTFnpi346hMSFjJKWjTJ4KlwuM5ReVYoWmmL4ZpVZKrZBFDaAGYwmEFUUQVlBVxLEpiZAhvvncj3JIKZlvu8lvHhESdCSnxDFodpxT+EyoAM9b+3n3A8f2zHUvTFI7uTactc5X03XOLJ39aI88jbR/C019oYncKUWN4rr6qFC7wzxhkhHh3QNvIxPiOgENZ8fVGQlCbwHvgliLNiDhCy7lb3xtsQ41x04vLbboZQsEtNcETQrNCWcq4fs= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CY8PR10MB6538.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(376005)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Iorsyxv3qJpNYXc04CfD2wVvRXcThf/bAuwoPrHwY61XsAZXOo/tJd1RfPO+7htHGUihEYmyuuQnZs7WnzEZmL6cMsLzydITHrrIZmxi71XhJM6DN6gAHmLm/oqUUEiZrErNNS03PD9MjVwHpprMFFGUJOjeIMRulQ473tBDqtD4pk45bvInqvOa9UN6ckSjx9qSjxWY3ajzZgeSxF015cWYPAf/wW8vq5i0Bw9DTLiIn+Yw+lD/O5DjFO1GsTLMMrX5WFUEzMk3gcn/9OjtwOs++5s+044niGhhzytENOl65++wWUqYSmzWi6BDVQfshPTGV7UvuEIOjDJX1YkzHBztpjFz6Crb3xtbbof03Ukm5MSirOZ2trY4XdjDKNDVK8zmjZNXK9To5xSgxBtTB9uXFBkayOnQ0OfvZdWQQCTovYH2Q/WfJFDKWHh8oYxDyvveBXMXO+06XA3TfxDOp+g/JqmdW44oM7iewtmH/O3OJlN49CNypJx+yJZmGIuOcj0IL0KO9ULSf63XhqtdJexNaE9omKBZ+v/MYyaQvnkjWqAE3wilE8a4hZv0rhjERY/WtdGK+unEf16N5vTr5fpXnNuogugA9dreZ4nefKGVKZubYM65LPm6qj2XxgRsVzOsnAHZvuo7Ql/2aR/sav79WioUK9vsZT50yzCmfpgsKFyOiKnJAYBi0AtMkvwGv2x4lwzwQGv5XmXSDz104RlxNzed7C3fCb4hSCt6OkovteTS7iVbV/BUrn6ovSZLQkfj59F9ui6ig5UDxSgtFccOWd5/5tmVTa2cSGetGT6n+I8gRPOW/IxsnTdpZ2klnp3xUvyq+DtcPEqTZmWnA57wNBKlMR2tUn+GOLzUqvyXLnJ7RoMhdy4Oi4M3GWhO+f5CCVpmwUNBGTToiXBlW3OXw6Ykm1zH3qarrr2qg+ne8ZsA5ERf13VlHvP47K1pjnR5nqslDc/auPP/lqu6kWrfrxrubnS7929mTFqm1uYvtRiW0f+6L4lwTcOS0yQJbQy8hvTa4eI19aH0VdVWQ3p+M+V9OwFJ3BORQdyHDZ6RWlDjfTyqwOxLhAilSVFO8xNc521KBxghOIluhTz43Gi5IVriNQp8bMeUCVZSyBVgFjXn6YBws1XwIkmZ3313EA+M9fh4rvE+wmCBE88dYM0BAdUIOjjkD78T+wJjj4YS3nCzI7QfZugx0g4ofiqKazL9/fiTr76Yh1CwJYcy2r82spfqfdmImdm3aIw5XyThlLhucfw1uufiV3OSVznQewMXV4AYlae5rYd7nwcLej0HdgwIuOhoPFxAUbHptdF9cxg+VoWpaFSQDhrB0rG8ZWWOJE93zLrO+y9Vt8GD1VT+cuZhhmvaTJDffkwd8WPIBXUsuRxb6GC7wF2UrVs9qhJMkKy0GlQU6FrErmoMcfJ00+bIMFn8Z9YR6Q0XSaO1uLlV5ubI/G5FLbUk5QUSEqr++kxWwUEqj5RtX/YUqZNjae0StH6FqPiwzc+rkuF3VMn2fhcjuCIIr/hBJWGqHsz/FRd/ahfKoJ+EL03U5LsOraP497hFp/aOdK0mmczLhoH+YtMwDOJDE0U2m1GS X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: aqscemQdNhMPu8zso0m5S1genUaahisQWssBhht6ztTGzrDMm63ocHTCxcCcOmccpuZxmht7lFHlfrIVWPz7V2wmVTSVkxXqagOUoLrCDLdaIP/qMoVlfKi+ehg6fweNfAE8DtbDLc9zitvVKr4eCXb1BpMHDkZVlaVpk4X+5h6wQs6+8XPEW2+ovG9QdtsSxaiotEoS3kfI8DctirJxKzhEYLyqobKM9kqpmAlLzf2ZArJWt0jbya3YoXO+ma4cWZgDinyGd6VjnWGqfaHQq3St8izdDdOIne6b8atbxwyUdwt7fRvtJM0qLhZfdGoHek0mesVxDKYWEJ0yAorcdyStBR/+Wu0HIHMGiImDX1nZqyfodoPNGlF+vsGa+YDymC8xl/2BuvOjQzOcXgdqd4krKPKqDEiL05X9m40W9+C7lxZvfcH79Mq+58b3ZnGDrHIxUjmH+CU2M+POp/qfrk3/sUzijS8m/jzdouhNartJqRikYHLnnslFPjBnTUU/sJ7+i2j7ATbD7WBeOfs1+x+wXLvTKfhT5te1F+fPa6admyFdm7evx7VCXJ0wCoeyUy62syJ9LhQBFJvHxDNlGFwgsKbHpzgk+B2957UL26Y= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b85ffcec-ce8b-478b-e9c2-08dc48dfd18a X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2024 13:15:31.6539 (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: eiJrHwPHQel5odhv+TMIv3mV/n5pATNhoyYpV9qgU5mifLh417/Doa8fetxOUvc1AnDAWqbpvhRHziHQ7G2JBQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5562 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-20_08,2024-03-18_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403140000 definitions=main-2403200104 X-Proofpoint-GUID: YeljLy4uDYnQYPwLPZXTYWpnFM-3nDut X-Proofpoint-ORIG-GUID: YeljLy4uDYnQYPwLPZXTYWpnFM-3nDut 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_MSPIKE_H4, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Including the following changes: * The definition of the new internal function .ACCESS_WITH_SIZE in internal-fn.def. * C FE converts every reference to a FAM with a "counted_by" attribute to a call to the internal function .ACCESS_WITH_SIZE. (build_component_ref in c_typeck.cc) This includes the case when the object is statically allocated and initialized. In order to make this working, the routines initializer_constant_valid_p_1 and output_constant in varasm.cc are updated to handle calls to .ACCESS_WITH_SIZE. (initializer_constant_valid_p_1 and output_constant in varasm.c) However, for the reference inside "offsetof", the "counted_by" attribute is ignored since it's not useful at all. (c_parser_postfix_expression in c/c-parser.cc) In addtion to "offsetof", for the reference inside operator "typeof" and "alignof", we ignore counted_by attribute too. When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE, replace the call with its first argument. * Convert every call to .ACCESS_WITH_SIZE to its first argument. (expand_ACCESS_WITH_SIZE in internal-fn.cc) * Adjust alias analysis to exclude the new internal from clobbering anything. (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in tree-ssa-alias.cc) * Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE when it's LHS is eliminated as dead code. (eliminate_unnecessary_stmts in tree-ssa-dce.cc) * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and get the reference from the call to .ACCESS_WITH_SIZE. (is_access_with_size_p and get_ref_from_access_with_size in tree.cc) gcc/c/ChangeLog: * c-parser.cc (c_parser_postfix_expression): Ignore the counted-by attribute when build_component_ref inside offsetof operator. * c-tree.h (build_component_ref): Add one more parameter. * c-typeck.cc (build_counted_by_ref): New function. (build_access_with_size_for_counted_by): New function. (build_component_ref): Check the counted-by attribute and build call to .ACCESS_WITH_SIZE. (build_unary_op): When building ADDR_EXPR for .ACCESS_WITH_SIZE, use its first argument. (lvalue_p): Accept call to .ACCESS_WITH_SIZE. gcc/ChangeLog: * internal-fn.cc (expand_ACCESS_WITH_SIZE): New function. * internal-fn.def (ACCESS_WITH_SIZE): New internal function. * tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case IFN_ACCESS_WITH_SIZE. (call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE. * tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call to .ACCESS_WITH_SIZE when its LHS is dead. * tree.cc (process_call_operands): Adjust side effect for function .ACCESS_WITH_SIZE. (is_access_with_size_p): New function. (get_ref_from_access_with_size): New function. * tree.h (is_access_with_size_p): New prototype. (get_ref_from_access_with_size): New prototype. * varasm.cc (initializer_constant_valid_p_1): Handle call to .ACCESS_WITH_SIZE. (output_constant): Handle call to .ACCESS_WITH_SIZE. gcc/testsuite/ChangeLog: * gcc.dg/flex-array-counted-by-2.c: New test. --- gcc/c/c-parser.cc | 10 +- gcc/c/c-tree.h | 2 +- gcc/c/c-typeck.cc | 128 +++++++++++++++++- gcc/internal-fn.cc | 35 +++++ gcc/internal-fn.def | 4 + .../gcc.dg/flex-array-counted-by-2.c | 112 +++++++++++++++ gcc/tree-ssa-alias.cc | 2 + gcc/tree-ssa-dce.cc | 5 +- gcc/tree.cc | 25 +++- gcc/tree.h | 8 ++ gcc/varasm.cc | 10 ++ 11 files changed, 331 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index c31349dae2ff..a6ed5ac43bb1 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -10850,9 +10850,12 @@ c_parser_postfix_expression (c_parser *parser) if (c_parser_next_token_is (parser, CPP_NAME)) { c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful at all. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, - comp_tok->location, UNKNOWN_LOCATION); + comp_tok->location, UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); while (c_parser_next_token_is (parser, CPP_DOT) || c_parser_next_token_is (parser, @@ -10879,11 +10882,14 @@ c_parser_postfix_expression (c_parser *parser) break; } c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, comp_tok->location, - UNKNOWN_LOCATION); + UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); } else diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index c7c23edc4840..59b69ca54f0f 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -778,7 +778,7 @@ extern void mark_exp_read (tree); extern tree composite_type (tree, tree); extern tree lookup_field (tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t, - location_t); + location_t, bool = true); extern tree build_array_ref (location_t, tree, tree); extern tree build_omp_array_section (location_t, tree, tree, tree); extern tree build_external_ref (location_t, tree, bool, tree *); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index cead0a055068..a29a7d7ec029 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2546,15 +2546,116 @@ should_suggest_deref_p (tree datum_type) return false; } +/* For a SUBDATUM field of a structure or union DATUM, generate a REF to + the object that represents its counted_by per the attribute counted_by + attached to this field if it's a flexible array member field, otherwise + return NULL_TREE. + set COUNTED_BY_TYPE to the TYPE of the counted_by field. + For example, if: + + struct P { + int k; + int x[] __attribute__ ((counted_by (k))); + } *p; + + for: + p->x + + the ref to the object that represents its element count will be: + + &(p->k) + +*/ +static tree +build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) +{ + tree type = TREE_TYPE (datum); + if (!(c_flexible_array_member_type_p (TREE_TYPE (subdatum)))) + return NULL_TREE; + + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (subdatum)); + tree counted_by_ref = NULL_TREE; + *counted_by_type = NULL_TREE; + if (attr_counted_by) + { + tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by)); + counted_by_ref + = build_component_ref (UNKNOWN_LOCATION, + datum, field_id, + UNKNOWN_LOCATION, UNKNOWN_LOCATION); + counted_by_ref = build_fold_addr_expr (counted_by_ref); + + /* get the TYPE of the counted_by field. */ + tree counted_by_field = lookup_field (type, field_id); + gcc_assert (counted_by_field); + + do + { + *counted_by_type = TREE_TYPE (TREE_VALUE (counted_by_field)); + counted_by_field = TREE_CHAIN (counted_by_field); + } + while (counted_by_field); + } + return counted_by_ref; +} + +/* Given a COMPONENT_REF REF with the location LOC, the corresponding + COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate an INDIRECT_REF + to a call to the internal function .ACCESS_WITH_SIZE. + + REF + + to: + + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + + NOTE: The return type of this function is the POINTER type pointing + to the original flexible array type. + Then the type of the INDIRECT_REF is the original flexible array type. + + The type of the first argument of this function is a POINTER type + to the orignal flexible array type. + + The 4th argument of the call is a constant 0 with the TYPE of the + object pointed by COUNTED_BY_REF. + + */ +static tree +build_access_with_size_for_counted_by (location_t loc, tree ref, + tree counted_by_ref, + tree counted_by_type) +{ + gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))); + /* The result type of the call is a pointer to the flexible array type. */ + tree result_type = build_pointer_type (TREE_TYPE (ref)); + + tree call + = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, + result_type, 5, + array_to_pointer_conversion (loc, ref), + counted_by_ref, + build_int_cst (integer_type_node, 1), + build_int_cst (counted_by_type, 0), + build_int_cst (integer_type_node, -1)); + /* Wrap the call with an INDIRECT_REF with the flexible array type. */ + call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); + SET_EXPR_LOCATION (call, loc); + return call; +} + /* Make an expression to refer to the COMPONENT field of structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the location of the COMPONENT_REF. COMPONENT_LOC is the location of COMPONENT. ARROW_LOC is the location of the first -> operand if - it is from -> operator. */ + it is from -> operator. + If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate + call to .ACCESS_WITH_SIZE. otherwise, ignore the attribute. */ tree build_component_ref (location_t loc, tree datum, tree component, - location_t component_loc, location_t arrow_loc) + location_t component_loc, location_t arrow_loc, + bool handle_counted_by) { tree type = TREE_TYPE (datum); enum tree_code code = TREE_CODE (type); @@ -2626,7 +2727,13 @@ build_component_ref (location_t loc, tree datum, tree component, int quals; tree subtype; bool use_datum_quals; - + tree counted_by_type = NULL_TREE; + /* Do not handle counted_by when in typeof and alignof operator. */ + handle_counted_by = handle_counted_by && !in_typeof && !in_alignof; + tree counted_by_ref = handle_counted_by + ? build_counted_by_ref (datum, subdatum, + &counted_by_type) + : NULL_TREE; if (TREE_TYPE (subdatum) == error_mark_node) return error_mark_node; @@ -2645,6 +2752,12 @@ build_component_ref (location_t loc, tree datum, tree component, ref = build3 (COMPONENT_REF, subtype, datum, subdatum, NULL_TREE); SET_EXPR_LOCATION (ref, loc); + + if (counted_by_ref) + ref = build_access_with_size_for_counted_by (loc, ref, + counted_by_ref, + counted_by_type); + if (TREE_READONLY (subdatum) || (use_datum_quals && TREE_READONLY (datum))) TREE_READONLY (ref) = 1; @@ -5048,7 +5161,11 @@ build_unary_op (location_t location, enum tree_code code, tree xarg, goto return_build_unary_op; } - /* Ordinary case; arg is a COMPONENT_REF or a decl. */ + /* Ordinary case; arg is a COMPONENT_REF or a decl,or a call to + .ACCESS_WITH_SIZE. */ + if (is_access_with_size_p (arg)) + arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0); + argtype = TREE_TYPE (arg); /* If the lvalue is const or volatile, merge that into the type @@ -5195,6 +5312,9 @@ lvalue_p (const_tree ref) case BIND_EXPR: return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; + case CALL_EXPR: + return is_access_with_size_p (ref); + default: return false; } diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index a07f25f3aee3..e744080ee670 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -3393,6 +3393,41 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) } } +/* Expand the IFN_ACCESS_WITH_SIZE function: + ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE, CLASS_OF_SIZE, + TYPE_OF_SIZE, ACCESS_MODE) + which returns the REF_TO_OBJ same as the 1st argument; + + 1st argument REF_TO_OBJ: The reference to the object; + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: the number of bytes. + 1: the number of the elements of the object type; + 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE + of the object referenced by REF_TO_SIZE + 5th argument ACCESS_MODE: + -1: Unknown access semantics + 0: none + 1: read_only + 2: write_only + 3: read_write + + Both the return type and the type of the first argument of this + function have been converted from the incomplete array type to + the corresponding pointer type. + + For each call to a .ACCESS_WITH_SIZE, replace it with its 1st argument. */ +static void +expand_ACCESS_WITH_SIZE (internal_fn, gcall *stmt) +{ + tree lhs = gimple_call_lhs (stmt); + tree ref_to_obj = gimple_call_arg (stmt, 0); + if (lhs) + expand_assignment (lhs, ref_to_obj, false); + else + emit_insn (expand_normal (ref_to_obj)); +} + /* The size of an OpenACC compute dimension. */ static void diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def index c14d30365c14..0801c8bfe61d 100644 --- a/gcc/internal-fn.def +++ b/gcc/internal-fn.def @@ -510,6 +510,10 @@ DEF_INTERNAL_FN (PHI, 0, NULL) automatic variable. */ DEF_INTERNAL_FN (DEFERRED_INIT, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) +/* A function to associate the access size and access mode information + with the corresponding reference to an object. */ +DEF_INTERNAL_FN (ACCESS_WITH_SIZE, ECF_LEAF | ECF_NOTHROW, NULL) + /* DIM_SIZE and DIM_POS return the size of a particular compute dimension and the executing thread's position within that dimension. DIM_POS is pure (and not const) so that it isn't diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c new file mode 100644 index 000000000000..84899ae620f3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c @@ -0,0 +1,112 @@ +/* test the code generation for the new attribute counted_by. + and also the offsetof operator on such array. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fdump-tree-original" } */ + +#include + +struct annotated { + int b; + char c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +static struct annotated static_annotated = { sizeof "hello", "hello" }; +static char *y = static_annotated.c; + +struct flex { + int b; + char c[]; +}; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +static struct nested_annotated nested_static_annotated + = { sizeof "hello1", 0, "hello1" }; +static char *nested_y = nested_static_annotated.c; + +struct nested_flex { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[]; +}; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (char)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated) + + attr_count * sizeof (char)); + array_nested_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test (char a, char b) +{ + if (__builtin_offsetof (struct annotated, c[0]) + != __builtin_offsetof (struct flex, c[0])) + abort (); + if (__builtin_offsetof (struct annotated, c[1]) + != __builtin_offsetof (struct flex, c[1])) + abort (); + if (__builtin_offsetof (struct nested_annotated, c[0]) + != __builtin_offsetof (struct nested_flex, c[0])) + abort (); + if (__builtin_offsetof (struct nested_annotated, c[1]) + != __builtin_offsetof (struct nested_flex, c[1])) + abort (); + + if (__builtin_types_compatible_p (typeof (array_annotated->c), + typeof (&(array_annotated->c)[0]))) + abort (); + if (__builtin_types_compatible_p (typeof (array_nested_annotated->c), + typeof (&(array_nested_annotated->c)[0]))) + abort (); + + if (__alignof (array_annotated->c) != __alignof (char)) + abort (); + if (__alignof (array_nested_annotated->c) != __alignof (char)) + abort (); + + if ((unsigned long) array_annotated->c != (unsigned long) &array_annotated->c) + abort (); + if ((unsigned long) array_nested_annotated->c + != (unsigned long) &array_nested_annotated->c) + abort (); + + array_annotated->c[2] = a; + array_nested_annotated->c[3] = b; + + if (y[2] != 'l') abort (); + if (nested_y[4] !='o') abort (); + +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test ('A', 'B'); + if (array_annotated->c[2] != 'A') abort (); + if (array_nested_annotated->c[3] != 'B') abort (); + return 0; +} + +/* { dg-final { scan-tree-dump-times "ACCESS_WITH_SIZE" 8 "original" } } */ diff --git a/gcc/tree-ssa-alias.cc b/gcc/tree-ssa-alias.cc index e7c1c1aa6243..8c070e173bdd 100644 --- a/gcc/tree-ssa-alias.cc +++ b/gcc/tree-ssa-alias.cc @@ -2823,6 +2823,7 @@ ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p) return false; case IFN_MASK_STORE_LANES: case IFN_MASK_LEN_STORE_LANES: + case IFN_ACCESS_WITH_SIZE: goto process_args; case IFN_MASK_LOAD: case IFN_LEN_LOAD: @@ -3073,6 +3074,7 @@ call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref, bool tbaa_p) case IFN_UBSAN_OBJECT_SIZE: case IFN_UBSAN_PTR: case IFN_ASAN_CHECK: + case IFN_ACCESS_WITH_SIZE: return false; case IFN_MASK_STORE: case IFN_LEN_STORE: diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc index 636c471d4c89..a54fb1b754dd 100644 --- a/gcc/tree-ssa-dce.cc +++ b/gcc/tree-ssa-dce.cc @@ -1459,8 +1459,8 @@ eliminate_unnecessary_stmts (bool aggressive) update_stmt (stmt); release_ssa_name (name); - /* GOMP_SIMD_LANE (unless three argument) or ASAN_POISON - without lhs is not needed. */ + /* GOMP_SIMD_LANE (unless three argument), ASAN_POISON + or .ACCESS_WITH_SIZE without lhs is not needed. */ if (gimple_call_internal_p (stmt)) switch (gimple_call_internal_fn (stmt)) { @@ -1470,6 +1470,7 @@ eliminate_unnecessary_stmts (bool aggressive) break; /* FALLTHRU */ case IFN_ASAN_POISON: + case IFN_ACCESS_WITH_SIZE: remove_dead_stmt (&gsi, bb, to_remove_edges); break; default: diff --git a/gcc/tree.cc b/gcc/tree.cc index 3dff8c510832..18b4e729a5bc 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -4068,7 +4068,8 @@ process_call_operands (tree t) int i = call_expr_flags (t); /* Calls have side-effects, except those to const or pure functions. */ - if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE))) + if ((i & ECF_LOOPING_CONST_OR_PURE) + || (!(i & (ECF_CONST | ECF_PURE)) && !is_access_with_size_p (t))) side_effects = true; /* Propagate TREE_READONLY of arguments for const functions. */ if (i & ECF_CONST) @@ -13362,6 +13363,28 @@ component_ref_size (tree ref, special_array_member *sam /* = NULL */) ? NULL_TREE : size_zero_node); } +/* Return true if the given node CALL is a call to a .ACCESS_WITH_SIZE + function. */ +bool +is_access_with_size_p (const_tree call) +{ + if (TREE_CODE (call) != CALL_EXPR) + return false; + if (CALL_EXPR_IFN (call) == IFN_ACCESS_WITH_SIZE) + return true; + return false; +} + +/* Get the corresponding reference from the call to a .ACCESS_WITH_SIZE. + * i.e the first argument of this call. return NULL_TREE otherwise. */ +tree +get_ref_from_access_with_size (tree call) +{ + if (is_access_with_size_p (call)) + return CALL_EXPR_ARG (call, 0); + return NULL_TREE; +} + /* Return the machine mode of T. For vectors, returns the mode of the inner type. The main use case is to feed the result to HONOR_NANS, avoiding the BLKmode that a direct TYPE_MODE (T) might return. */ diff --git a/gcc/tree.h b/gcc/tree.h index 972a067a1f7a..73464d3e3ae3 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5760,6 +5760,14 @@ extern special_array_member component_ref_sam_type (tree); cannot be determined. */ extern tree component_ref_size (tree, special_array_member * = NULL); +/* Return true if the given node is a call to a .ACCESS_WITH_SIZE + function. */ +extern bool is_access_with_size_p (const_tree); + +/* Get the corresponding reference from the call to a .ACCESS_WITH_SIZE. + * i.e the first argument of this call. return NULL_TREE otherwise. */ +extern tree get_ref_from_access_with_size (tree); + extern int tree_map_base_eq (const void *, const void *); extern unsigned int tree_map_base_hash (const void *); extern bool tree_map_base_marked_p (const void *); diff --git a/gcc/varasm.cc b/gcc/varasm.cc index fa17eff551e8..0e6e44e0420c 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -5082,6 +5082,11 @@ initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache) } return ret; + case CALL_EXPR: + /* for a call to .ACCESS_WITH_SIZE, check the first argument. */ + if (tree ref = get_ref_from_access_with_size (value)) + return initializer_constant_valid_p_1 (ref, endtype, cache); + /* FALLTHROUGH. */ default: break; } @@ -5276,6 +5281,11 @@ output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align, exp = TREE_OPERAND (exp, 0); } + /* for a call to .ACCESS_WITH_SIZE, check the first argument. */ + if (TREE_CODE (exp) == CALL_EXPR) + if (tree ref = get_ref_from_access_with_size (exp)) + exp = ref; + code = TREE_CODE (TREE_TYPE (exp)); thissize = int_size_in_bytes (TREE_TYPE (exp)); From patchwork Wed Mar 20 13:15:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1914107 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=LJ3jibNy; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=epi6Amqe; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V08JN31NFz1yXD for ; Thu, 21 Mar 2024 00:17:08 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 365FD3858426 for ; Wed, 20 Mar 2024 13:17:06 +0000 (GMT) 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 54CE73858289 for ; Wed, 20 Mar 2024 13:15:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 54CE73858289 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 54CE73858289 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940546; cv=pass; b=uxlOWtJiOe8lgAiwrww0C65xOlEcaV/jhRQmZRuawuoyNdnsGckUVQULhEBqTm81lH4ztgthiF1Elw0XMpw44SMxIe9FPHek0qFb28iwKM5Xk9DupWu3hu49ufu3WAIHaM9eQqCQZAGWcqMYxnXGLKroWfnT/5MEmSvzfTG7xGU= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940546; c=relaxed/simple; bh=Oto5t965O2b5BRdlzyUI5NVZm49ypmOOjJ66n5dWNGI=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=TJmgxSIIUJw7b0mpWzk2Rb6tx4jF8d/FlJ/WyBmign9WL2uGvfkHWYCprNVJnK6rVL1aDwen3onaDv0JzT0CUV+Nm+p/GsGprtvbS1AQSpvSuNtVYkNm/IwYBlCX3Iqc9CEUSVimTLqPy8KUMM26gBdEREcORy0wRgVCA6PR6V0= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42KBxE4b004649; Wed, 20 Mar 2024 13:15:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=FAG7oZM3DS6R1r1khE5IdasZ5g+2Q8ogq51Isa7ONio=; b=LJ3jibNyl+B4LrFaZwJNNTJPhmodJphX48BUwYTi7kgfXAdtpvMKymIGx7DZkog/swPY 0OatqykwBA9q1RF2cvcVhtJYj2ED9iRrBVKy12VccDvDr/6fYA1GciuImVFSviKADBLg ZZIPoTXdC5GPP0UrwxidSLuiyDBQQ3vX5LJdivs9hBRmOJigd1uEO5Orc8Jukd9UVKOy LDHv6MkFxkamzaO72g/HHhyLez5nVDiAfxZzPRitem0zCbbsvkv5BErJhZK5yBWHPIjI KsqPgu6vyv+/z0eZvpfwL/WxluRSaSHZvaT0yJIF27Moa8Rviq7ipznOtU+GHRpu5kk4 8w== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ww2bbr348-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:39 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 42KCGH5n007556; Wed, 20 Mar 2024 13:15:38 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3ww1v8auma-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FCWcdufSqbEdBNUjT70tODDnBN2P45JJhZxQovQ1zWuFiu0s3kvmoUQmmcNaJ47QVdWBS6FFf+/+m0R8YmeM9Od5DBJzlsxtjiZ4VXIT6JPeV3bh18MNK2YgtKw8PeegT5Bc0qAuqCV4Sq+PurxYHb0xKVg8YsdFI6cjNIhIn1NPpzpplaZEyg7EAdf4kxRZrVcm514dOuCUJnpkRJd+mXOEDNACoAZCHP7ZIYmihgge0BscWG5MnVvHyay1/YYQ9+BJ/upCmwe9BlwTKmsqltQ177j1S911ds1OXoZTezZz3Qla4RKR4Lgj1/jPQ82V4euLe1JcLDrOyE0aQ6fGUA== 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=FAG7oZM3DS6R1r1khE5IdasZ5g+2Q8ogq51Isa7ONio=; b=n3lRmJzINcJERLXWa2dRiN+MvOFK09KnGvC3PMkzcWu+6z1G1rXOhIBtpXFPX2+C51DK6KZScLnTxFvC8aZyiOLH+KsYY+f1r60QL/nDXvCIJtPgrCXR8fy8HII6IMHIRXwg/I4cZmCC20aPaNBf2Gf4VhHlCcaPK+oEGIRVYrYlysiUjZ6wJ1l+Fu14BwRdnAXeRFotMmtpzt+hcFSyg6rAR/HsG1CnDucg/NQhy0J5IFI0zhFqfL+1No+qSDoWWi/ECmNyoCq/tzVV5q8WAPituk0omz/FpM9X6+/3U4rdXktM3qNWcXyg8tC0Ivb7Oc21YbDQVnq7xo0AuvnmIg== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FAG7oZM3DS6R1r1khE5IdasZ5g+2Q8ogq51Isa7ONio=; b=epi6AmqeSYx2XK5xEwGLe+CKabo/WxuwxqAYl1PujIlJOqRPoRN63pez/CMxv4kV5bACO1BeaQTSgt4hpRCvIjUnp91F0HX5jU7h2gZeLNaDJJZugLWWUzOiedTmTJYP8Zsa0FQNKtilVW17jQ6QauQv8uWBj/ypK3ezySeLo+I= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by PH0PR10MB5562.namprd10.prod.outlook.com (2603:10b6:510:f1::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.31; Wed, 20 Mar 2024 13:15:35 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc%6]) with mapi id 15.20.7386.031; Wed, 20 Mar 2024 13:15:35 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v7 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Wed, 20 Mar 2024 13:15:16 +0000 Message-Id: <20240320131518.2292317-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240320131518.2292317-1-qing.zhao@oracle.com> References: <20240320131518.2292317-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH8PR21CA0012.namprd21.prod.outlook.com (2603:10b6:510:2ce::6) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|PH0PR10MB5562:EE_ X-MS-Office365-Filtering-Correlation-Id: 8bbf0caf-33c4-45a6-433f-08dc48dfd41e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gZj+rbIoWehkJH9FRuNIUTtx/OdiHaSURdn8osoPcbElxUbWeOloIdfPW7AnCg8JH7ovTMY7ndYjIvxw9tBK7jpOdqK1ETyzAux+kyJMlWEJIK/YTVo3bvB9T03C0wonvA0GvxWiEHLuzcTjuyhti8ChLxQKSzYrPfydjs5TMwvZwydxpvtF0CHDOMb5o1EYl72cDflHupIkrp8EzzUcUv6bLxjGl3+qBMUw/69B5jRIZpsXFwDnQYc9sB09Qs1hg/GdNMWX5hZfy61wEFQ7PXUKmRbGvc0+zDq7M4Jbi0bDwBy0cqV8xesZe+SxtbNH7tN2fKkrUvc+6bzGh6s/71bSx2VMdpOL65Qfm/uqJ4LjpqkrkLfyU8H1baLmP8jqTttLPeCDl49pLlnrS/91FiYV1RrEc9YFfQSJqkQV9hfW4e/AOtDCMY9cXOu0WLSat8qlL7E2BYtdyyAe34buBzMkGoLTELhKQNJ933CD/2HrKgD1d+WVtkGgcGL6bDzcK/J5hpTRd+dQPWRKUCbzx+dh5wvfJ40ggZojzTkP2vS1u8te0R/y+6XfdFgjiMh0RzEedzRYdVUxkAdWkVLIfGRWFhFPAbhyKfjaEZiFEJGNMCXKYsq294KCPd8OdA/l X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CY8PR10MB6538.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(376005)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: NuvXVFJsQDg34DkgSEY0UH/Tp3Tw8lVR+n9ykR7ul+yJoWNmv1fForS+qhsTi/UffYQigNTce9Qlg4buEtE3KzoxFZLMqDjfcE0Qrhn0zttPmcgN3uTLT2kCjQ8hkQijIof04WIhSQ+x9L1iFSWy/pszhGgPEP692q4V77Uckqx3/nl5yivc8ns9IoZrZ9dnvsgdTuGk1sujYhK2Vw+u0HhBp/MZwI4tQupWNHeXpAM9HcR4BhfU1j6cokSbTUMYKfAReS5bOECL3ovN/ZOjk7xBQgFuDrVUWy5DKgtYyY5whLybepCmDS4Dt7lP+/GxEd0v7WPFX9OdG10BUVq3Z8ZL7TnTPWa4i55jlkH7QY69VY5XfHXg8ftPXfYwVW85ka1aoh2Ylkkdp0pq/+pMVJ6VnZhhmm6+9EkNYUubELXD5DJPfHGsJ2R+cZr3uqj7WRYY9SXmWv0rPLWNO83u23r4Nll9pxjd7kpJ9T0wVbcCIxNDdNQIPOoBqbTMVNTdE2ItZvMarHI9xbsDhjtxWZubAbSnpQfPyXaAhb/S6CxQd5ChRDFOn80C6yEo0EIVcBwmBepD1To6Fh1Ztm+067Zl7w+IVsmrH+UcGiYLtjRlPBTDF1RIatFvNjP/7BTi9EndkTI5hz4A84+rARxo565vJ96PEK84u7TPOMRwkIFBgMXG3za/9kuaoxk2D0sILLiH92tZtHNRQEaFRZqfFYWPMjqjZ5AmCRFwgj08tZiar846n4dcga4IdfwJINeb5cwJxNCDJgqdaEMJktoUu/NsUXQ5YSqzITHnkgJmdwn2sRWaQ5Yzzoi/D4/N5Owyq70ueNSqBXKx170ZsmcHdKhqVzzCzJ0l28s9b7yqyrf0uc6JcjgOr9jP/UM+8GHko0Snq0XPvkdct04g4Id0xnfYmzfSd2M2zPbhyugsKJtGuddkC/tkDdPy62TykMaQvJfc+aq/Z7wUdNGUnPsB48BL/cmg4Iyfgl+7ImpOQyNj2shqc7+W/y5gpPGcELuwa2SUp7rhO9oMx9E8OF0y3SN1OCLoPzTDPz/pjyYso91PVylIvuoVSDFSM3l9LYs1Q/S948YK+eJbiDwdRYiwyXnIE/giZRSI9eOGttaGtSI0epBo4XNj3igfTX27IJT4EHJ/jNofp0HSfRK4UtSZ1va56n5FnmaGprX4VG8ruRbZ49cc71E51kKtn0aATbr6H+e1Omk1FHlW8W2Fi+/XS6WFOh+cfFjT9pOaiCW2enh0pPUuPoabwLVdBvNxk793CofRFLsscVfjljnZzq3x6u9Jnc89sQW86+d6l063Sc9q/F/suWdz0djQDgvpM9KOul2+YHuZRzsArWd9OUEjf2eX4UiF09gGaeTRYqnP/xb90NXhyg+u/aYfERAOcmWAAoHsabU3/qiZn5MMpYmqTJA5RcNHeo+I6CPeLYUmdtDPFAsNW1VQZDkLk7v+hlT0S3WJAzhy/Y817bDPxmqTBBf8Z/y100iduJypwx8rWjOk0Iw6zGsof4yIohMntyXbek4FP7qUA/L1ALDmNGAN+NU52h7IdUbyW+YOhnPpg8BN0kSibQml+Xv54fPMHGT6 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: fb/Jn7ADszGVy4bp3nnk9MP1RNpaunuI9x9dcl33UtzZmey9KYH5B651HsJu+4hZSjmWsJbkoCD791+xSq7QMWLZFHURN1W/g13JSbi1VinheB0HwDfWNxSe9GaVLa2cMTzbyGZVIV63fKjRsf2An804dA3eCC2Yo0a9LJwrXAxax6bu1QS0BvFAv5GIXFT2G5tRhfqn+RN5jkMqPC72kSekflgER83ThGpWFeuBzlgz6fdwbW3RyFb/8URaMykVc7HDDnaQodE61+VB9xoQjrAd/9hr88BL32UMs6fBYKWqPG1qRu02S6/fJNhDTejLu3v1XTN+EvzBE/SFKgZzCbI7+VrGnRFb2Q3jROXdUvakfaZttudEOka5xlky5hMUU1pLfZb7GedfYmzUzLEEgTjCj+DoKhGN1M1l2pG/JZ9Puackskwaj+4oTDgfk/0XuAld2mrYJZ0eOHJpMLJKvuKqeiaChszsJIOI5SLHP4GVydosZE+2ZRbhKeZHmFs0j0qzuo+xNOSFkumKzfQVUwARUTZSJCbevq6hToD93qc4WM7fe+2Dkjyjd2Tf1ym2/d+iE5l+7pzIJ/rin2AbC3YbYN3dgU7XzeswjuGFs2o= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8bbf0caf-33c4-45a6-433f-08dc48dfd41e X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2024 13:15:35.5047 (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: 0CQmfpyFTm6lV7e4+tLJ4AxI3WjP1HpZ88uriXolGX88lv7BmguCaidf8EUTRDODZLm5I/68Y3s3/MnYOnHR0A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5562 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-20_08,2024-03-18_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403140000 definitions=main-2403200104 X-Proofpoint-ORIG-GUID: 5kiD-eZnIlxibVFcn2Rln5hOJjsCtTiy X-Proofpoint-GUID: 5kiD-eZnIlxibVFcn2Rln5hOJjsCtTiy 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_MSPIKE_H4, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): New function. (call_object_size): Call the new function. gcc/testsuite/ChangeLog: * gcc.dg/builtin-object-size-common.h: Add a new macro EXPECT. * gcc.dg/flex-array-counted-by-3.c: New test. * gcc.dg/flex-array-counted-by-4.c: New test. * gcc.dg/flex-array-counted-by-5.c: New test. --- .../gcc.dg/builtin-object-size-common.h | 11 ++ .../gcc.dg/flex-array-counted-by-3.c | 63 +++++++ .../gcc.dg/flex-array-counted-by-4.c | 178 ++++++++++++++++++ .../gcc.dg/flex-array-counted-by-5.c | 48 +++++ gcc/tree-object-size.cc | 60 ++++++ 5 files changed, 360 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-3.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-4.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-5.c diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-common.h b/gcc/testsuite/gcc.dg/builtin-object-size-common.h index 66ff7cdd953a..b677067c6e6b 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-common.h +++ b/gcc/testsuite/gcc.dg/builtin-object-size-common.h @@ -30,3 +30,14 @@ unsigned nfails = 0; __builtin_abort (); \ return 0; \ } while (0) + +#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); diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c new file mode 100644 index 000000000000..0066c32ca808 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c @@ -0,0 +1,63 @@ +/* test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + int c[] __attribute__ ((counted_by (b))); +} *array_nested_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; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated) + + attr_count * sizeof (int)); + array_nested_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)); + EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1), + array_nested_annotated->b * sizeof (int)); +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test (); + DONE (); +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c new file mode 100644 index 000000000000..3ce7f3545549 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c @@ -0,0 +1,178 @@ +/* test the attribute counted_by and its usage in +__builtin_dynamic_object_size: what's the correct behavior when the +allocation size mismatched with the value of counted_by attribute? +we should always use the latest value that is hold by the counted_by +field. */ +/* { dg-do run } */ +/* { dg-options "-O -fstrict-flex-arrays=3" } */ + +#include "builtin-object-size-common.h" + +struct annotated { + size_t foo; + char others; + char array[] __attribute__((counted_by (foo))); +}; + +#define noinline __attribute__((__noinline__)) +#define SIZE_BUMP 10 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +/* In general, Due to type casting, the type for the pointee of a pointer + does not say anything about the object it points to, + So, __builtin_object_size can not directly use the type of the pointee + to decide the size of the object the pointer points to. + + there are only two reliable ways: + A. observed allocations (call to the allocation functions in the routine) + B. observed accesses (read or write access to the location of the + pointer points to) + + that provide information about the type/existence of an object at + the corresponding address. + + for A, we use the "alloc_size" attribute for the corresponding allocation + functions to determine the object size; + (We treat counted_by attribute the same as the "alloc_size" attribute) + + For B, we use the SIZE info of the TYPE attached to the corresponding access. + + The only other way in C which ensures that a pointer actually points + to an object of the correct type is 'static': + + void foo(struct P *p[static 1]); + + See https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624814.html + for more details. */ + +/* in the following function, malloc allocated more space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? */ + +static struct annotated * noinline alloc_buf_more (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index + SIZE_BUMP) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index; + + /*when checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + we always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /*when checking the pointer p, we only have info on the observed allocation. + So, the object size info can only been obtained from the call to malloc. + for both MAXIMUM and MINIMUM: A = (index + SIZE_BUMP) * sizeof (char) */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +/* in the following function, malloc allocated less space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? + NOTE: this is an user error, GCC should issue warnings for such case. + this is a seperate issue we should address later. */ + +static struct annotated * noinline alloc_buf_less (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index + SIZE_BUMP; + + /*when checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + we always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /*when checking the pointer p, we only have info on the observed + allocation. So, the object size info can only been obtained from + the call to malloc. */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +int main () +{ + struct annotated *p, *q; + p = alloc_buf_more (10); + q = alloc_buf_less (10); + + /*when checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(p->array, 0), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 1), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 2), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 3), p->foo * sizeof(char)); + /*when checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(p, 0), -1); + EXPECT(__builtin_dynamic_object_size(p, 1), -1); + EXPECT(__builtin_dynamic_object_size(p, 2), 0); + EXPECT(__builtin_dynamic_object_size(p, 3), 0); + + /*when checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(q->array, 0), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 1), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 2), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 3), q->foo * sizeof(char)); + /*when checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(q, 0), -1); + EXPECT(__builtin_dynamic_object_size(q, 1), -1); + EXPECT(__builtin_dynamic_object_size(q, 2), 0); + EXPECT(__builtin_dynamic_object_size(q, 3), 0); + + DONE (); +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c new file mode 100644 index 000000000000..37fc215e5e5c --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c @@ -0,0 +1,48 @@ +/* test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. when the counted_by field is negative. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + int c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +void __attribute__((__noinline__)) setup (int attr_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated)); + array_nested_annotated->b = attr_count -1; + + return; +} + +void __attribute__((__noinline__)) test () +{ + EXPECT(__builtin_dynamic_object_size(array_annotated->c, 1), 0); + EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1), 0); +} + +int main(int argc, char *argv[]) +{ + setup (-10); + test (); + DONE (); +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 018fbc30cbb6..d258d0947545 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see #include "attribs.h" #include "builtins.h" #include "gimplify-me.h" +#include "gimplify.h" struct object_size_info { @@ -60,6 +61,7 @@ static tree compute_object_offset (tree, const_tree); static bool addr_object_size (struct object_size_info *, const_tree, int, tree *, tree *t = NULL); static tree alloc_object_size (const gcall *, int); +static tree access_with_size_object_size (const gcall *, int); static tree pass_through_call (const gcall *); static void collect_object_sizes_for (struct object_size_info *, tree); static void expr_object_size (struct object_size_info *, tree, tree); @@ -749,6 +751,60 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, return false; } +/* Compute __builtin_object_size for a CALL to .ACCESS_WITH_SIZE, + OBJECT_SIZE_TYPE is the second argument from __builtin_object_size. + The 2nd, 3rd, and the 4th parameters of the call determine the size of + the CALL: + + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: the number of bytes; + 1: the number of the elements of the object type; + 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE + of the object referenced by REF_TO_SIZE + + the size of the element can be retrived from the result type of the call, + which is the pointer to the array type. */ +static tree +access_with_size_object_size (const gcall *call, int object_size_type) +{ + /* if not for dynamic object size, return. */ + if ((object_size_type & OST_DYNAMIC) == 0) + return size_unknown (object_size_type); + + gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); + /* result type is a pointer type to the original flexible array type. */ + tree result_type = gimple_call_return_type (call); + gcc_assert (POINTER_TYPE_P (result_type)); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); + tree ref_to_size = gimple_call_arg (call, 1); + unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); + tree type = TREE_TYPE (gimple_call_arg (call, 3)); + + tree size = fold_build2 (MEM_REF, type, ref_to_size, + build_int_cst (ptr_type_node, 0)); + + /* If size is negative value, treat it as zero. */ + if (!TYPE_UNSIGNED (type)) + { + tree cond_expr = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, integer_type_node, cond_expr, + build_zero_cst (type), size); + } + + if (class_of_size == 1) + size = size_binop (MULT_EXPR, + fold_convert (sizetype, size), + fold_convert (sizetype, element_size)); + else + size = fold_convert (sizetype, size); + + if (!todo) + todo = TODO_update_ssa_only_virtuals; + + return size; +} /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL. Handles calls to functions declared with attribute alloc_size. @@ -1350,8 +1406,12 @@ call_object_size (struct object_size_info *osi, tree ptr, gcall *call) bool is_strdup = gimple_call_builtin_p (call, BUILT_IN_STRDUP); bool is_strndup = gimple_call_builtin_p (call, BUILT_IN_STRNDUP); + bool is_access_with_size + = gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE); if (is_strdup || is_strndup) bytes = strdup_object_size (call, object_size_type, is_strndup); + else if (is_access_with_size) + bytes = access_with_size_object_size (call, object_size_type); else bytes = alloc_object_size (call, object_size_type); From patchwork Wed Mar 20 13:15:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1914109 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=JwjM7pYU; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=fpLmHGR9; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V08KW5xSGz1yXD for ; Thu, 21 Mar 2024 00:18:06 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DC6353858C98 for ; Wed, 20 Mar 2024 13:18:04 +0000 (GMT) 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 D6A803858282 for ; Wed, 20 Mar 2024 13:15:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6A803858282 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D6A803858282 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940548; cv=pass; b=YJbo6lmUKg161zazmB3qi+Yo0z7Rvmgq1N3MvyAQlmqNV9BLz3VRSAiaQ9o58dHvtMCnqHcZiWzMIpoH0AoY6myI6Z5E8qzED+1jN2dX/7faSweQfkjYe/JDyIQgGOoh3lRx1TABFW8LNeBd488yUDnqSgncWM7Jmin3o0KYQaE= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940548; c=relaxed/simple; bh=0YRWIQX9ls3JhXwyeR0otx4g4eCi0zJEtHjlY4lQVsA=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=kASg/Kacc4m1GNuUd4KAHHr72xm2Zf/DU7Yl0aV6DJN1nw0qX1f3IsnZzke5GTzszE9piNP3skB2oo95tTfS+m/ImKuVoZPUe4EynBi3wVmuANdMYdzbeYGYbB+AaX7mlxeBpvvDu7H2M4OJAgCBhRdgEGOBU2SlBPMIMvNZbNA= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42KBxRYn028175; Wed, 20 Mar 2024 13:15:43 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=bziWo9A3n6MHzlSmXm2qiRcKVF9Nw3ikvHsWgJlTapM=; b=JwjM7pYUHc4uRGGworH9tfWnkkUqDEMAWCa70GhDFjTdbI7pfn4oanxuQrLzQc6boyTR 9Y4DMMPHc79TGw57Q8mZeBO2/MIobYv32FKeGPVKQBIOInLkwg/nsh2gb32eHIDRZAW4 17J0oZVmDDNwJkc5Xq2UBtJsxSMsJEM4E7Wz/Z1kHXxJkATwa5hSFty5g/il7naOfdr5 kWw1MXmdozq3UAh7QPFl9QLo+bQrAFsMFGE7MreZ4UodFnYojBVK4pjGNElCwyDU8CpS TtXf/tf8ykRW3rj4xftFJg2eAexPKbIsyvhj7YHoEx/cOQqMDjQWuBlnIfmQLg477YKi Pg== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ww3aafxkh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:42 +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 42KCGA9b006165; Wed, 20 Mar 2024 13:15:41 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3ww1v81b44-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DSukCRYHh4E9JuuzRYAMeEyXcUjCJnIufGDk2Sw+NG3NU1DVXhhRQAADxG3V/02UmiBTg0AUn+6LVY0OXfNNSEVlxL0aenXbt9RdW7kbw1g9MBy0kj+YmpJU3UD0lAIyMf3OlyloXSRtAmHXZeprmsdw5CNIsRR/h3sBq18J6Ynm0xQNuMB66bpF7wbWSHqTS9FHeKNDUrhTtfnDe2lc6haypuxyR/PyHgr7ux90VPeZg5PT/Md8qlAKd63e8/59c/rPAzQcGJAayCgSOMvoovcAroHb3CL4Q/PnLmp9b5NuP2PLMhmXmrq9zUD1QK/Eu30/Z677BBwm/1AGWQzsow== 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=bziWo9A3n6MHzlSmXm2qiRcKVF9Nw3ikvHsWgJlTapM=; b=An5g4oALP/juL0k2AT8Hl0/zHrmPNAKaCue33IKE+HEP/tilfMIpwxqw7Q9LMvHy+iCg4Q3oQ8AUnYfKD3Tcpd/VUZKj2fqTwYgU1u94Yn+ijBq+3KVVI5MGvXjykwryJLXvHMFZ5SlGKfGl/QdwVXQOhATGm3iq+ak9hp0CFlSaIMAQdfPaocKTyVFx/UqP5V5Sf85wt4DVmL0VTpesVGNiJ7Ps4d73J2o/eeS+ibAnkFJqHYusJB5/T8ZWbeXHlsVg3kWUnYz4MgllHg6Z4BIFSaQtS+964pN/H71Y8dRqDBPw60LUCVDZPySuPgCS2I9ZhRFlN5NNXF44Zu/jQA== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bziWo9A3n6MHzlSmXm2qiRcKVF9Nw3ikvHsWgJlTapM=; b=fpLmHGR9YKdDYhQkmIPctyW2b0NottEbHOaV49VVFfDwzjQ2gvrqGkIaCzvp50N0DOX7V9V6bxyLkeFc+1cCovPXzweu03vJGoL70fvyLsKwi6pOae/rUuNJGNsmjRKYe5z6GTCqZuzHpPBs3CnKFF1MOT/vH/lW4mRBQPZMgag= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by PH0PR10MB5562.namprd10.prod.outlook.com (2603:10b6:510:f1::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.31; Wed, 20 Mar 2024 13:15:39 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc%6]) with mapi id 15.20.7386.031; Wed, 20 Mar 2024 13:15:39 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v7 4/5] Use the .ACCESS_WITH_SIZE in bound sanitizer. Date: Wed, 20 Mar 2024 13:15:17 +0000 Message-Id: <20240320131518.2292317-5-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240320131518.2292317-1-qing.zhao@oracle.com> References: <20240320131518.2292317-1-qing.zhao@oracle.com> X-ClientProxiedBy: BYAPR07CA0052.namprd07.prod.outlook.com (2603:10b6:a03:60::29) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|PH0PR10MB5562:EE_ X-MS-Office365-Filtering-Correlation-Id: acc10a73-a639-4b40-9e09-08dc48dfd67b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: llrJFgDkfPYTPRzaJU9pXWfg7M/DNJM5LUJ/UBtPpDj44EVZyx836o6U6JU/Nuv0TSbi9atXwwQIcTqjxgN+haXYFv6df/Ybc/EKlBhXHiZ095i/ruXt2N7SfVZkVEIWTjPgKR+R8fW1DBPV4/m8JaHhw9rjQe0PIRfphARRQ1h5VVZ/46ZCXcUn0FVKYM4c7aSdDL+pk1mztLFWBZ2unneLXQFB9+xbGl4xyTciLMgvQqXq7mBjqvj5zXU8HC0oHUj9tFgvPxj7ynnprEY9uj+DVqcXp7NCyo3Bad8VB9XiONehrj5/kYWA6LFJs01ua2NiRq5cSiACVcaJTqYOzzQs1fOdVMANuKn6RbeKkkBKFp+/5StMXAI0x53hilQH22AxHBtuQKkuBM00onAntc4jJdsEl3tTVe9NrwFzjsXyjrKG6MAN/ExCDV6f44gmsMNL1psz/l90QCOixhVQjvVe4gAXkW7pq4Wb3qBH31v56ZeKTWqH8KT5HWWAPkZ1M0KdViSCgsDpDaR8PCU2YFWVFy2DgjVrQah7hH3R6YSSC20pPyixJFSG4duXa9cQsPTGzZRQGIfMcm3nnWfoBOU/lKMiFIhfPurgP+TJoBrRA1pzSe7jaSp6nwqAdDqx X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CY8PR10MB6538.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(376005)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZGMTVnCZwmLR8rdWUwnfern2xlel3pd6DDEZ4z0h6h57lQ33QC6OqfZ+sMrApiXBZV4P0llnyLDrVjMyx1txu1V+1b92wnCxGaNAFLTt6VelZR2vpMs1dQZLCy2TkWcD+m4IAUZ6Lv1c8LBwPpjXDeGgqxm77KciAYHZaBHsK7D2ZPK+pwXqxbg/W9al2gDvsR5j6+p3ZxoXFAJHFIHLyuMY9EfkXhfPrR3vAzDede0a5UduQVmZWqBnH5ZE+DXugIJiiNabSO2ysoQ1Adei6R5T9UnE2zcWcw+SQAYXNr1J7knHg5rZW9j9ZjS0J6KA4r8ftH26ASWzxNYDLh9uI7d7P4Glhnz7t6fZpaKBQiMm2Ty8lIJ2MAVx7evh7xcIRxvRG93Fl22nAH9uDCnuqC+YM/Iu8hMK6r/7q4OgyejyDUS+fq7BGKuegkA3b8UuCg9LKR03v0VYGVK84ScEMO1ygkUM/zg1+LMptk6P40FzcEFMXuoWK98kv89fcbe5hHjP5U+DXUXbARZ/Zi5k3a4sLQdgDpZHu82tm0BR82N35W9rolURTekLiP8PfZ0PmX8MIaBx4a1Onb/CNWhaEhYIGJimbFxpLww0HJyzVS363Cx4oxcWKJLVtI8vhtVjcc/uiheZd7hDGcipsrqmNhD0fehgWd8lTZhxA7b8YCOjHAT8UuSQQh9jFjqaTtUFKL2qJJhgwdNFgdvxFcsId4evIQh688HIemhpRG7j2u2JbB5NECz7PhjlwA5eXJXxy3j5xvDPaYerdmIPCpa59aT/VjVM9qO8mjAvvijymLmuYh/7me1NyyZpIeVu6uBB0sXnj4jZuaEW+uUQyopqfUwTMedP0t0REFKEEeN6emEJo4ZkP3XYHO2UzXsiKy3qptOcvxCFM6WY6Us/5C1qx1RNS+07+7vWimPXlFbJl+6dKuAlo9/D7q/KAerLc8mDZwaHPbu05dE7gJpde8GY1N1N2FZPo3u10hPfgbUr0l8bXcLjN5LfE0e1LArjp2MCBxYaxVTF7nuJNQ/3kQXQbhXB+aveciz2PICPKX5TehC/ytUqwxpS+rGkAyp+I+9iYUGE0jMJy3vUYi9tHj9ISOxYzznwZlZ2XOsk4dESvLep1ET+sfRoCd4oTykdo9qlhCsPtm2RMKVKM5CPYe97hY/J/AU0Cnid+EM2BqPGAKkm5UqJJxw3bTcRx/vmWWtei0T3qi8iHV2vpFa9PkL5OnNOQY/yUHOrdnuXJ/pQC7Ea9wMB2YLR6zr8Ri07U+kDyUqaWQl8fBMeHXRRWXiGkjnaEriWv2giUjXUgAmESXetloMq4e+bhtW51xh7N2ff70vgFqDtVgH36CMXO4evh0yE/DHT9SUWNR1eecGlTBr2MdLQKsKRJu9J/F00n8UJOhkxAzqaCZ3kfOsOyimzOOWJ7aOMgMN5j0lKlqVLPEzM0KSEfPVr5vERqeiqgZfPbr1p4ECX3Z8eMufKOqcx0gMuiIjfqihAeSNvM5VqngSYustMJQoL2n+5sw5FVqoJCiecaMUZ7rXt4cO8dNmBlb2HG7B0pD6SEF8p8Kozh0ZLNbEfPd5E2UBMGE3CMM95 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 6lLSo7GYu7mutjKMcGIIjnic+76JNckoUpRvuFvDd7KXK3y6KTQDMeqSpiQvJ/vWKKbD8g5NnpRlquvnsEpymYuFhlxN6eUBEI5rzw1wIk4wlM/tTaW2SB9uHU9SStE4v1dSAqCHi9tMcHGIWgmjQONgBTO6C8RA/DiS+EHo9WyMA1fkbc8knKoRLkHs82HmXgArOhY5HjFKuhw+qFxSFRcVFm3eCtIe40AyZue7095zjtgh/VO6ZoRBNyKSX0pyA8zAFaPxAVQIcQxkHlxHJ70GPY9xyMwI/w63zXrDORSX6/PGRJ2kz/XlBoYK07tmMe+E42XfkwuNFUrPy/WTI6UzDaw2z0m+YG/ODCmLb0cnXbU5xzHrapmI9N2+0IZ3vygo3Egapi69T64kechug3BFSMbk1FSKavV4T3Gn9eMb5nIJuKEKbBIIp8CooUj45ntLFoEIcQqj5nT/4th60jQyHsPm3koqPj69DNRVf8KLJFKGt1BfG7YAqnRpddmgrMkrinBhezeyT71vAw9XnEJA61fuLzr9iZbo9n4H8WK94lvTr5ZUMzkE6rMAO00/D1lIb3iD6gWkIl/TgkF6hXL/27rjWnId/uCfC7tiqu4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: acc10a73-a639-4b40-9e09-08dc48dfd67b X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2024 13:15:39.4408 (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: ZcdPH07ZaT7DEuKHDyAQ4JrssPAKRi7dwXjFs1P7Rv0W0f018a6rl5mEVQDQ7NFVYRuu0/LIdfUi0uEsZ/Sw+A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5562 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-20_08,2024-03-18_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 phishscore=0 malwarescore=0 mlxscore=0 spamscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403140000 definitions=main-2403200104 X-Proofpoint-ORIG-GUID: rrekCdy8J3MNALOlbiI6Oq1dJbg5OGBy X-Proofpoint-GUID: rrekCdy8J3MNALOlbiI6Oq1dJbg5OGBy X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/c-family/ChangeLog: * c-ubsan.cc (get_bound_from_access_with_size): New function. (ubsan_instrument_bounds): Handle call to .ACCESS_WITH_SIZE. gcc/testsuite/ChangeLog: * gcc.dg/ubsan/flex-array-counted-by-bounds-2.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds-3.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds-4.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds.c: New test. --- gcc/c-family/c-ubsan.cc | 42 +++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-2.c | 45 ++++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-3.c | 34 ++++++++++++++ .../ubsan/flex-array-counted-by-bounds-4.c | 34 ++++++++++++++ .../ubsan/flex-array-counted-by-bounds.c | 46 +++++++++++++++++++ 5 files changed, 201 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index 940982819ddf..7cd3c6aa5b88 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -376,6 +376,40 @@ ubsan_instrument_return (location_t loc) return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data)); } +/* Get the tree that represented the number of counted_by, i.e, the maximum + number of the elements of the object that the call to .ACCESS_WITH_SIZE + points to, this number will be the bound of the corresponding array. */ +static tree +get_bound_from_access_with_size (tree call) +{ + if (!is_access_with_size_p (call)) + return NULL_TREE; + + tree ref_to_size = CALL_EXPR_ARG (call, 1); + unsigned int class_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2)); + tree type = TREE_TYPE (CALL_EXPR_ARG (call, 3)); + tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size), + build_int_cst (ptr_type_node, 0)); + /* If size is negative value, treat it as zero. */ + if (!TYPE_UNSIGNED (type)) + { + tree cond = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, type, cond, + build_zero_cst (type), size); + } + + /* Only when class_of_size is 1, i.e, the number of the elements of + the object type, return the size. */ + if (class_of_size != 1) + return NULL_TREE; + else + size = fold_convert (sizetype, size); + + return size; +} + + /* Instrument array bounds for ARRAY_REFs. We create special builtin, that gets expanded in the sanopt pass, and make an array dimension of it. ARRAY is the array, *INDEX is an index to the array. @@ -401,6 +435,14 @@ 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); + else if (INDIRECT_REF_P (array) + && is_access_with_size_p ((TREE_OPERAND (array, 0)))) + { + bound = get_bound_from_access_with_size ((TREE_OPERAND (array, 0))); + bound = fold_build2 (MINUS_EXPR, TREE_TYPE (bound), + bound, + build_int_cst (TREE_TYPE (bound), 1)); + } else return NULL_TREE; } diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c new file mode 100644 index 000000000000..148934975ee5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c @@ -0,0 +1,45 @@ +/* test the attribute counted_by and its usage in + bounds sanitizer combined with VLA. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ +/* { dg-output "index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 20 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 10 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ + + +#include + +void __attribute__((__noinline__)) setup_and_test_vla (int n, int m) +{ + struct foo { + int n; + int p[][n] __attribute__((counted_by(n))); + } *f; + + f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n])); + f->n = m; + f->p[m][n-1]=1; + return; +} + +void __attribute__((__noinline__)) setup_and_test_vla_1 (int n1, int n2, int m) +{ + struct foo { + int n; + int p[][n2][n1] __attribute__((counted_by(n))); + } *f; + + f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n2][n1])); + f->n = m; + f->p[m][n2][n1]=1; + return; +} + +int main(int argc, char *argv[]) +{ + setup_and_test_vla (10, 11); + setup_and_test_vla_1 (10, 11, 20); + return 0; +} + diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c new file mode 100644 index 000000000000..33bdea1c430b --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c @@ -0,0 +1,34 @@ +/* test the attribute counted_by and its usage in bounds + sanitizer. when counted_by field is negative value. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int annotated_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int annotated_index) +{ + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (-3); + test (2); + return 0; +} + +/* { dg-output "24:21: runtime error: index 2 out of bounds for type" } */ diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c new file mode 100644 index 000000000000..c6b55defeae4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-4.c @@ -0,0 +1,34 @@ +/* test the attribute counted_by and its usage in bounds + sanitizer. when counted_by field is zero value. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int annotated_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int annotated_index) +{ + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (0); + test (1); + return 0; +} + +/* { dg-output "24:21: runtime error: index 1 out of bounds for type" } */ diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c new file mode 100644 index 000000000000..81eaeb3f2681 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c @@ -0,0 +1,46 @@ +/* test the attribute counted_by 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__ ((counted_by (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" } */ From patchwork Wed Mar 20 13:15:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1914108 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=ZQQ4JZCX; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=nfc7Nv1T; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V08Jt1rbCz1yXD for ; Thu, 21 Mar 2024 00:17:34 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 36C293858C98 for ; Wed, 20 Mar 2024 13:17:32 +0000 (GMT) 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 6942A3858427 for ; Wed, 20 Mar 2024 13:15:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6942A3858427 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6942A3858427 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940551; cv=pass; b=APrsOCShviLwdGsbptdk+HqCF3Bud7d3TjHShP9R1JMmigdyhf+N6eybGRUBqT85bONzu3fb8eYiZnX4wpFML8KIHPrpLgVq+l3U8wUNFMVPxjQQmftO/fLZFfu3MQGhbh9xjYlHUNdI4bLzpLwzo5bFbzC+LGO0GcrdHkV5TzE= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940551; c=relaxed/simple; bh=MShth2ijcX1WA+8MtDWd/FamxaD8D4LzLw94wYTrjEU=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=AAbJsg6Xjdgs8Op7BlgbWB2gCDOik/n3BQ9tJN+Hurp6KR46wiLObIDkJXzNxBQCPF/QfNY8J1ZxxGbjTUETQYcYR/TQ68EZECE1BsVLuhs96d6wj/iJjBg0WiSI3csV6o/XcZNgrhYCzLxtiew8nwUnAl0z+vjH0Snco8j3Szw= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42KBxEUI004663; Wed, 20 Mar 2024 13:15:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=QWU6Sy0kANHR2I5Q4mXWznv4/9oVJ1xQB6f74jnUB3o=; b=ZQQ4JZCXHk1OBWkZLlF+/wrOFNmAIArU6iFY6mftVX+2LQrqI/aCEFTgwHm2iuHlyfBJ JOpN9HYlBVZkSGm8K6vLa1HFCuIp+V5o4lMZBrUtQWsjK94tMcCd6+EhcQa3/xudLInN rrms1D0YF7hxTC1P6R4ODGAOR7erPNECOHgIX5owXc8SWstcWJPrFWM7MfKG46gxVAgy tsf1zrncS4uyIBqxKsh0vrAWia/dCPM524DlzJ+buA/vDmSQHWScrwtAFj/ZcXmfQFdt tt8JV2S2XskMJsDZpyp7AMLIoHXpYg3lJ259BzL5MI3or/G7Lg9nCUcTOIgHxEjNvwGC ZQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ww2bbr34g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:47 +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 42KCGAcE006139; Wed, 20 Mar 2024 13:15:46 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2101.outbound.protection.outlook.com [104.47.70.101]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3ww1v81b9e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GPMWLNlztRzYp88lwSpZnUOPYtcBXqrpWk0uu4oD2ftYIvq/62zEEuXxwETqo5eac0ieg1kniSbS9jKyBlL7oiEfkgS8qqNLKiwMC+LBjrCk1a3D2G8g7whusB9TOpxLhGT2siWSBCf9Q0f1y31xzYEMLgfVyFxusvH0yhufbkhJpSw22BJewMTQxKyvpKdj9waJ8ghUjFyJyBTOsNHMFUWeiR2JG6i4zqSx9s5ZdfGMEMay1lT2kah5ioeblPAUyqyLznTxhK324Jkq9Bm1IqKIz4kwwowAYFMpiWZQ9vmulY/FxkcGe12i9+dcg39CcGE4HK4g5CkrlRQV7F0lzw== 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=QWU6Sy0kANHR2I5Q4mXWznv4/9oVJ1xQB6f74jnUB3o=; b=nQJ3f/i2g5i0GwDIufQcngdEoO72Gla+xsUJmqQrvYxJKF1yPa/1vAlaXWrxPr5BHLvjVaEAtjlQJi6GLqiVbp+V5mXtnH3uUCRQ/m7D8osaYkkc9xnlFjuulnet5C8boYledBem3cJPYq8RQWOaGD5I4AtorGLKWwGLhpvtJc4yTXW5L3nsvJnc+EQt1vcj+pxYtQO+v/omhsLrnzBgKo/aTsI8SWvoGEQBAfh6feb7fmhQRZOjAKxX2n4OUOfqU6+d9U78n/XvddXfVIqPLWx6vtaU1OhNQqIN9sQB1dXJ9T0vzrG0NLeOOAqbh3FIplA7zO+oxdJU0dhWq9Z+eQ== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QWU6Sy0kANHR2I5Q4mXWznv4/9oVJ1xQB6f74jnUB3o=; b=nfc7Nv1Tf2yCbKR+JEfdPEoDtucsxcmOhCsIO6p0eO+uspu1pMuVC4W9dJGg1v7BUhUbaZwJk4j8FV4t5QaMe95CYQxLCNwg4armeMkWgdEMVTAe2OuFC6kdFGPsasQxA+N7ROxAMiQJFiWi11BGQlyKKYctT604be2FEGCzeOE= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by PH0PR10MB5562.namprd10.prod.outlook.com (2603:10b6:510:f1::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.31; Wed, 20 Mar 2024 13:15:43 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc%6]) with mapi id 15.20.7386.031; Wed, 20 Mar 2024 13:15:43 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v7 5/5] Add the 6th argument to .ACCESS_WITH_SIZE Date: Wed, 20 Mar 2024 13:15:18 +0000 Message-Id: <20240320131518.2292317-6-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240320131518.2292317-1-qing.zhao@oracle.com> References: <20240320131518.2292317-1-qing.zhao@oracle.com> X-ClientProxiedBy: BYAPR08CA0041.namprd08.prod.outlook.com (2603:10b6:a03:117::18) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|PH0PR10MB5562:EE_ X-MS-Office365-Filtering-Correlation-Id: 51122036-4d8a-4c9e-5f41-08dc48dfd92e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fVtRRQRf4sXeasyRna7uqAJhFlsu7kL2fTMHKwu4Bcx0piXS6/qvjNfsVMKvWBZooVXDstEVnicIK7LWk0PNPRdQOonVO+vtjt+V+raY/zC2XwDP7M/2FwsC0n0UHo2FkpmtOcGnRpDaF1Kiz5GTsMmmyl+yIPAu4XqUDBhYYwEe9g96/3HlRyO9gVHwaZxN38LnlJKtN4myw795lXXhNwUimp6Ma/bjsjqhq96Mr/OHwXRA41uJ2lx69f7nKHIKkgmR1NsO7ow/2MXSaie7eE8DaICpWgts3eo8w3FCWQdWzHQh03vu+V3zxxm+PbQo+JjYr0FMlT0z4AQMc33Wv8Of8EBdDlbHku+u/FlOkCCrPoj66xLHdjeg+LESM8a0YzV6SdVHdxOgH3SnN/K9e4494G3aEyiCGha0BCslxo0fuEalFi7HI0Fa6hJXWICwL5jPY3C9Qxj1ElFS+1LhH9/kkMu9x7VFUrHWXq1M33j6dp7IZg0CFYfzywfbLa6vXW9qAeEx2BtOYR11XJNjH8BDAMESHS4BiBuKnFt0fqGEUA/HQeOUd2s81l984APw1LnvcySa8Yyo9SUQ9cJtOaOEmw55HBREODmt7IDhW6Q= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CY8PR10MB6538.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(376005)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OHtLodp4KQ5/wqnxhet06JrO1NasrELyXkIXxT3+1E3javuFhpoUB26ooLBxHTnOjIQOE8NA+ZESgpccJ7jUmEsoTU5fcMgRDuQVkjKMzQUImaW6uNbesKqq0I9bBNSRm2lvY6O0zoVLrmNEuZGs+t+2bgIt7eGWExrwg6KXtUI5X8x5QzbrnSI2XSvO3yyewERepaAjGsTSlN5fmSs6L5f7LR2jh69cOsdlNc6OCrk+/697VMMU/by/q2VVMz+8WirMaTvmanYwNlhURX/coZlmD5kSYD1ReNQ/+hI8FFCWub+0VJLokb9Q0Xc7dXbegOZUPokHg8BC66burQ6jB0NMwhe77nZIr+d2yeWCA4TIRXweDhxBZSjlx9CPy1T3OohRt4nfiQDstDg7ws07YXZkmOq08iebF8Y8Y9vWuajzOTRrVEzAE3ridi+DqTYHBhemzzKdqWWiol9x5h0v/rvCghzFw7W14CjndgLxOEintiOt5LDK5yyxHQMRSCfr4XV20ug5Uo67OJLQ2bmR6IVlfNV6AeB8rE8yVzbuw4MJH3JEr1ern7tpBfIr0AtuOKrOwLrRYVf9vGbN1WXZdZ0O4nX/XfLtgh4MKQsr9Fp+6dquQP5G++vVn2eOwD3ynA5Cz2kCptULDHxJUjLHMsMMrOW1e1alb3ioMUZGFfK9t6VYbpcnSpF08mV03kzMLlTsEmMp+5AbTr0za78QNzzso747XkkQlT9viehYdKJXe6xy7FV7T7vODNvd7XGfiryYXO8oBkxNvrxuKsCqSncNtXSDFYCVuBn/wzFk5iuWV63TSXHrDHMOsmkAn9Ay4guTPGVMhdwRUzjE/mZ0HAhDtDWtfJwjtQR3hGUSomH/b0lxXxgP4K7YEOooIRE+aHw+8X23yGUpWCMZtFnIjhps3tWFrPC+Snmh5Q6gdqAz6t+4sZexF9lScM7q3VtN1e6G/LA0YGyONSnvkz/KZ0x2WyFOFcpnINoin8v8GZQ5XfskublPpQsk+zvHa8cxpGiXdXw21qDuT1ulLx1aQWlJ+ywgjzCamPorOfImvJe/YRdeEN0kuw9ugLkYmyKnQ/hgtDd+TuvZZlYXT8POtNPrdJYCCD5E3ZdJn6/3tHwCIjwE3Uc2OEfT76cwywuiqHTGSJ+bFr2TP5+J9cqYGmfQr5tQ0bbxUweGZLIyHAfUEF2APcyYBSF34NARG7kIeoOlSIjz5dC9beFEzErnQNXWm+UeaNRu4FLGBsEJyo2o/jwPQ2UAQFexezo9C6D9T6cU5zG1zKNZ/EdzE6gb0l5TD65a1ThEd5fFOv3f6qfydIt6GDyy2OG9lZXyrIMcdmshEt+kJbDBw8QEQWbxcJ2tHQLz8VfBWhMsAjmmEBo9p9Q+yW8PgnGfto+f3uLlyq5fTsA2QH2HTojxQEJOsLKazk1RjO7e5MYgCwLoPm+PNAINUnIDGP55ktI12Sb9/Nik4qAUTe2ZSzYpiXjNwbi7Hwg+ePPU1apelQG2r6KlDpj9wChgg27tfnigzt2yd+1332ZTZoCjdQQZI9J4f4Wo0poO7ng+52FbDlVCZElRPw8niT8j+JfOKnSkhItW X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 5bJZh15ujOGYVCTH2rhQIsm7AvulKElcvAIWHNStyDqP8W68uOweuImv0+LHLpCzC4hJeB6B9ZcSXiQskOISYqGGwlFHWylYiD4lUTO5jfE4kGAHYvmznATRcnEbG1QXOrpcn9pujcGlAFldBJhXtoP1CMONNsZPyQSdBqqHk+Atg7LdxVzcn/0SsLwFEQb0bgz6g+s9qEP+iBp4hdQ9Gf5dk/SBIGgUypiL9ZYHxuxjknACMIXOjNkGF37PRh7DeU79ButAMJpe3gSUm5XCMEMwSLkrqeaDPxmTS6c7X8JVaQjolpumZ1ocTWJko+xL8io8+Px08T7SWF/S0XS60tHBNE3X1ne6vaGdmg0YeNHFz/jt5jYpjxhA/74C+V0wRXWjYfjEDIJKbs+Lb7MMsi5ojYGmNnwT1c1IYjWwyrh1jJz9u94dlm0n+62YB56eF05z8i7gHIbBqaFtPWunBYaqDsbHn83Mah4rvsz5sWKP6/6kVUaLRFoVdLjv64GSD3N5jcUr0T+A0Q9g+gKHir1YhM3fzsE6jUUsLDfnOPDT7pDIFW3k8fkb8YctqWuzA8x9xrtXFtNNaH7wIGSlaN3x0+qPDBnxSzlFURE15Hc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 51122036-4d8a-4c9e-5f41-08dc48dfd92e X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2024 13:15:43.9094 (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: HGb/JeiUbdj8DAwGBED3kX7euA1Wa2LMLbv4qr5UfpxkVjQuhXUQkk2m7O1bo7KUQanj20MJa6h5qYEnAdpKkg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5562 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-20_08,2024-03-18_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 phishscore=0 malwarescore=0 mlxscore=0 spamscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403140000 definitions=main-2403200104 X-Proofpoint-ORIG-GUID: YbFVx5MFJztQVNLiemxp1kaqPFKr2p15 X-Proofpoint-GUID: YbFVx5MFJztQVNLiemxp1kaqPFKr2p15 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org to carry the TYPE of the flexible array. Such information is needed during tree-object-size.cc. We cannot use the result type or the type of the 1st argument of the routine .ACCESS_WITH_SIZE to decide the element type of the original array due to possible type casting in the source code. gcc/c/ChangeLog: * c-typeck.cc (build_access_with_size_for_counted_by): Add the 6th argument to .ACCESS_WITH_SIZE. gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): Use the type of the 6th argument for the type of the element. gcc/testsuite/ChangeLog: * gcc.dg/flex-array-counted-by-6.c: New test. --- gcc/c/c-typeck.cc | 11 +++-- gcc/internal-fn.cc | 2 + .../gcc.dg/flex-array-counted-by-6.c | 46 +++++++++++++++++++ gcc/tree-object-size.cc | 16 ++++--- 4 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-6.c diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index a29a7d7ec029..c17ac6862546 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2608,7 +2608,8 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) to: - (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1, + (TYPE_OF_ARRAY *)0)) NOTE: The return type of this function is the POINTER type pointing to the original flexible array type. @@ -2620,6 +2621,9 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) The 4th argument of the call is a constant 0 with the TYPE of the object pointed by COUNTED_BY_REF. + The 6th argument of the call is a constant 0 with the pointer TYPE + to the original flexible array type. + */ static tree build_access_with_size_for_counted_by (location_t loc, tree ref, @@ -2632,12 +2636,13 @@ build_access_with_size_for_counted_by (location_t loc, tree ref, tree call = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, - result_type, 5, + result_type, 6, array_to_pointer_conversion (loc, ref), counted_by_ref, build_int_cst (integer_type_node, 1), build_int_cst (counted_by_type, 0), - build_int_cst (integer_type_node, -1)); + build_int_cst (integer_type_node, -1), + build_int_cst (result_type, 0)); /* Wrap the call with an INDIRECT_REF with the flexible array type. */ call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); SET_EXPR_LOCATION (call, loc); diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index e744080ee670..34e4a4aea534 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -3411,6 +3411,8 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) 1: read_only 2: write_only 3: read_write + 6th argument: A constant 0 with the pointer TYPE to the original flexible + array type. Both the return type and the type of the first argument of this function have been converted from the incomplete array type to diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c new file mode 100644 index 000000000000..65a401796479 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c @@ -0,0 +1,46 @@ +/* test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. when the type of the flexible array member + * is casting to another type. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +typedef unsigned short u16; + +struct info { + u16 data_len; + char data[] __attribute__((counted_by(data_len))); +}; + +struct foo { + int a; + int b; +}; + +static __attribute__((__noinline__)) +struct info *setup () +{ + struct info *p; + size_t bytes = 3 * sizeof(struct foo); + + p = (struct info *)malloc (sizeof (struct info) + bytes); + p->data_len = bytes; + + return p; +} + +static void +__attribute__((__noinline__)) report (struct info *p) +{ + struct foo *bar = (struct foo *)p->data; + EXPECT(__builtin_dynamic_object_size((char *)(bar + 1), 1), 16); + EXPECT(__builtin_dynamic_object_size((char *)(bar + 2), 1), 8); +} + +int main(int argc, char *argv[]) +{ + struct info *p = setup(); + report(p); + return 0; +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index d258d0947545..ee9a0415c21c 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -762,9 +762,11 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, 1: the number of the elements of the object type; 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE of the object referenced by REF_TO_SIZE + 6th argument: A constant 0 with the pointer TYPE to the original flexible + array type. - the size of the element can be retrived from the result type of the call, - which is the pointer to the array type. */ + the size of the element can be retrived from the TYPE of the 6th argument + of the call, which is the pointer to the array type. */ static tree access_with_size_object_size (const gcall *call, int object_size_type) { @@ -773,10 +775,12 @@ access_with_size_object_size (const gcall *call, int object_size_type) return size_unknown (object_size_type); gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); - /* result type is a pointer type to the original flexible array type. */ - tree result_type = gimple_call_return_type (call); - gcc_assert (POINTER_TYPE_P (result_type)); - tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); + /* the type of the 6th argument type is the pointer TYPE to the original + flexible array type. */ + tree pointer_to_array_type = TREE_TYPE (gimple_call_arg (call, 5)); + gcc_assert (POINTER_TYPE_P (pointer_to_array_type)); + tree element_type = TREE_TYPE (TREE_TYPE (pointer_to_array_type)); + tree element_size = TYPE_SIZE_UNIT (element_type); tree ref_to_size = gimple_call_arg (call, 1); unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); tree type = TREE_TYPE (gimple_call_arg (call, 3));