From patchwork Fri Apr 12 13:54:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1923153 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=bdT+LJew; 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=GLZmX7SB; 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 4VGJ3j6jpbz1yYB for ; Fri, 12 Apr 2024 23:55:13 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1F0BD384AB5A for ; Fri, 12 Apr 2024 13:55:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id E35473858CDB for ; Fri, 12 Apr 2024 13:54:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E35473858CDB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 E35473858CDB Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930086; cv=pass; b=aFjG9Ilj0uxgseCoG+3TpeMakfqsWh75q7D5rQcJgjEjSwjUw4o3stgfJli08YnTZxNr7frWUhXz2OE2EVE3f/XBGl2M8ClQs0IXdOPADxCub+pYkaf5U01mkXFxv4Isjj8oeR2OJX0G6b8MQiyOOPJqnxB4RdTrvU3Dd4kUSqw= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930086; c=relaxed/simple; bh=1iSBEWYy2mUDQy8pMKvpCMu6mxLJDWOOtQADe4QY4BY=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=hRgIuSq2nvA5/9Y/0ZjtD+oe2C9Ykv0SELTUytYTWnFAqvkrnS02tekoLx43nsbq4Oc+NWgnDsFRtWTrRxgO2DZ5mQBWG9/W0CG1qA3mAqDZIoFpDXIz21SMEaANom4GB1hM+2PHrMQKOhL5aLEPZN/dMxzz0qp83gW0U4V6tgE= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43CCKoai032551; Fri, 12 Apr 2024 13:54:42 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=U56iM1O0sur09sLoQUMTjmP9V0/9FusmE5iOoUK9q5M=; b=bdT+LJewqUQ8qAcp542biRIDHLKH2Cw09fgWMDwL2T46vlc2xvBaB/hFurjRXw1DOrT7 8gIGGArO1B+CStsGkTlbBI/AONr44eKrYiBWnX7n3onMnqVeqxrrnTmsuJhfgJIdEUYA 2jHN0NVShyYgZjuMSvcIG97TmyJaPra+WQ140OPg8u7tywdTJf5FNQPkTUGOM2RvMZ8e 7c+rzOL6bsIiC/dvZJ/9alFIUb/owPH2V6JUyW5VjdoToaLjMJH2XcgA/RRSyvT3oSeE eirikYd3LavqRegla/y+lKDn36tacZdEJfbuMIBO+cdQjIbY9xZvcYQ+T5HORav/YIDZ 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 3xaxedv0n8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:41 +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 43CDolIp010526; Fri, 12 Apr 2024 13:54:40 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2169.outbound.protection.outlook.com [104.47.59.169]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3xavub12gm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YHR6IalEeLDSXEtVqDkwJHhca8RC6by3q10nQFOBJ6jSGW94D/ycrgtCCh2M1P2hKP7+S9D+qjd5WxS6SFV6xSXl5sYC26D2xa0rEeKWrSyns9zEA3CcaiKdsXWabUln8qSc08tPuaT4I8816lOilNn+25XGiNW2UwWAL6TzcJtOaz2tp42EH6qsg/N+qfNWniJRqONFGvFqFdUM/AxLl55oSagwuQ/k0uI91hE3jOYjS+AfhWN7O0Refi6QX7HJGcuePPMQI5qEeFJ5L24BZyhd92/ZaVXgH06lvr/mPaQs6ZN0U7GaQnga1qyoqIS0Zsm/09iG7Et0yg8U3ozsCA== 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=U56iM1O0sur09sLoQUMTjmP9V0/9FusmE5iOoUK9q5M=; b=Bpnk00Yu61DEY1jpmPxw4UM9cHBdtNH+68KygssdsKTTJpQaEPLGXcmiqxRfwpzFMoCOLGRSqi7wJQNzkxDIIIu1YbNOG0aGQnPYkPojFfxRRybAexrbnIrULFJ8xaIrLJEMLxjcEFXK9M3/GYXuoPJpUB4l7p/NYVwh30acp3OBhNsG58fiOZZ4e7P73tC0q83QafPcaMVGkLOjlzgsdLMhVOqKKUpGPxl5LjocTV3WqieOuNQrnbMxs/9PFlTP1Hjpamr2NJckqsiTC4nej0MBTPkLpQfNMKiqvJilKm5m4KblVOaFbNa0xaqQP/gzMQ+JBBaT+R9I3+aJlHNuLg== 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=U56iM1O0sur09sLoQUMTjmP9V0/9FusmE5iOoUK9q5M=; b=GLZmX7SBoXKuJBlt2s2YdyFO5/uG4SKFesDLy0oogvMWOtyvVtyqlhixxCzPYXDEsP2tqaY4NkjaDs1Dtu9nOUy0CsPeKd2kw2IhEZOuBSGgh0CW8JAyNhXxCW9p67v+wqtWedTu9QSTkfH87xaSjEZMOz7Sznt5KRL/HxEZ5iA= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by CYYPR10MB7572.namprd10.prod.outlook.com (2603:10b6:930:b8::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Fri, 12 Apr 2024 13:54:38 +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.7409.042; Fri, 12 Apr 2024 13:54:38 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at, keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org Cc: Qing Zhao Subject: [PATCH v9 1/5] Provide counted_by attribute to flexible array member field (PR108896) Date: Fri, 12 Apr 2024 13:54:26 +0000 Message-Id: <20240412135430.4122328-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240412135430.4122328-1-qing.zhao@oracle.com> References: <20240412135430.4122328-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH2PEPF00003848.namprd17.prod.outlook.com (2603:10b6:518:1::65) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|CYYPR10MB7572:EE_ X-MS-Office365-Filtering-Correlation-Id: c5f6e1c2-9d69-47e3-4f32-08dc5af817f1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zgbUqRhAPEjFyUBqmcTmFDSVQqOXxUZ6GTWr4HmmF3yUndoPIeCGX54+DZ4cA3d+a1aLskKKek5KhAbEYcl6wnJFtvvJW6Q1vBzAst2nTPs07dtpuofCZ7YWyeE3/6APWv7IckaKhF0QkLcLaFv3D9LtmN2Lo32qkALGiEhhyCrDHXXHsqLf7SYf91wbhIcR1o+lE6RIU54r67Ds8TKKhezhoEEDcgVMYt95g/Df39ARMNkZgXtJHo17F4hODipGZ/XOJLhM8tnIvcrksoMd9uZKs6tUvINqNh84VaDaTXqWlQpnsOK42Yo6T/tYwfOE9csjnbaz6ciCh7oFlV5JdstVxbw/TKc9FxpPBZw8C8z3T1xx8+f8Ex+R4BBz4Zaz2IzUpzP7P3uDi1YWLwrGfOFgTkVjZWhWAc4BnDx3Rqet3d2hSVgTVenLFav7KSGQB4NN79FRo83dUmBLd4Kg7uwJB+1ubICHDqRc5c0qWz28GTESzagQDg1gOvZhWXupdeo3IR+mlfny/PeCn407I9RyZX9Jr7lUU6icIo5mBBcKvpXCHRB5aoLr52FwBrRHX4XrJm/VYO9wXCESoapRgL/wzY40Vi7mWy6cEF4gaENbwD5n5K9r3t3Qea9AkITZ 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)(376005)(1800799015)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pA3G1G9MVhCjPR4DpCzV9nwd47oNpDCGhSwb9OhENIAETh5kuA5zxX3Dka3VSh75MzFvFipOHKeWljJ4DppDGctNKsngYcaEomQQwHVLo8uw83bZNdSQtFN9cA90vqNxOFb9uItMbI7xdHP8ZOGbCjKz02IVkGYb/0r518IPCkkImjXguwNamHZH9BCC3mIxFxAlksUcmxfSOD3n7h8XKOF9EelnBZLwhQxQCH5iigWo32UV0xXG6WgZYnZyjbB4mjxU9uZT3nh5u3fY1VScmuKib3MR73GicrwY7E/YJmkUCTB3JOxHVbRKs/xZwMjonaSr2rSP+wGp7V15senOFbqsadQpSsIcLXOm3AWWynBhk0YpcOnl2fdSxbWsXV/UEIswjrdWF5rIC8fish7pancm02734AUGHtFKTAS5rlNpGveihRSE7SmBLVQ1ndTO4CM6XnMKzqoU95deZ8TnxzrQHMWCxM759T19/KD+xCQFy/72Y1hxeX1+RNhYvv9MCRleDTZhw9YhjKzUTyVppMVgQJzTOgcNZwiqb+/L570RW98jDY0AkRfwyEoh1Twd28y31UWg98uss8/2boNELH8hzq00p+95/Upt+1A0AdYQ9GojyXUphcjlO7zC8tJsXTinIO3uhnmHqN7he4SzhUp8NkTSOz8Ouxmu9CN3wzpdxt7o7btMwMpOaTa1HQu1s8uNhHr0zTS1o8qf6qOfW4IuZWaZcHXN9X+ySPN0fky7tFl/v7uYtguE4iTE7HG6RQMuDLMMOmWUYWKzyhqH1Cd5R9tthldMFzxBlZGNWFuYDA4cybAPIpELFzAWRGDAoxPqjZwYZmUu6SBIU3LjKqAz4Z/oOmPtKiPtvrYKFQjOLxdRiY1rne2wtDvk4dVq0/jKVwNU43iAtqZ0ZQ7VNAiv1PLKaJxq4gDbW7fVZydeq1Mm/7KorieP9GnJF3Yoi09BtcGV90Y17foWhIynLqHJ+bmV4vCIWdq2HjCij1XUBYIna2/WBiMhG/bRW2YETy8Xq3nm2O6x4iF6Z/Znv+OYa6VArRmrg3epLVrteORe6b4OVY09GDlZ6HG/121CH/+Jfi1kxrwH8JS2bXySDMq31gUGdGGTi/caS5VOCZb/KB2CuSJcapbu9ffaVFwBzUQGdrQmU7m+OxvE4Mh9Nhhj79o8Htr0UozcbvWjEFVRLVhs9nN6x3Lbv2QKUChJdrxLFcvDoMMXcav2d0V9tG3vO9XylTk2m3PZMsFR6hQDgXOi7GIb0xvoJ3faJBYUAvHonrmdaF9ywSxW6NaSzUvC0JoVBtRC+CjyrUGdBpyXUW+5EV8yHSptyGbXO12L7QymHac2LMwQ3v4Bt5xgOrM7jc/1Dt5wM/374L+W6f1ImarPT0DuoaDM4/l1SPITChPDx96ZdN6AVCIOAqd9UltcFsYroGDyZzZQeE/C6nO2Ik46QJZ0GPHouLLjarnIGDGJBIN0xgMWmQmGh3O5jHbB7f3m8RCqgnD+JbgAaler6VH1Ix5BXsNK5EDeBFKLkNYRd/PjI0ITi9sQP/qMdwX+OFaWRhjYe6NOkrNZmIYKdLuIQYuosMTfOHn8Yg4M X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ZTuvZ9AkRM6j8KaMMfBkUAsl8c8U50Jk8Qh92C1vDP7odwIVVHvvRqeW4kI137MWD4HAAjo4Ab4Os9lteacu30JEbf92pU3AwR05sZCkvAJdK9DpTuJKmOs52c0OURaN5xxT7tSMCmFSUVnZKGscyao6SGhhCXtUPU4wpFItugb8/N86hdlKDs1I699kbyfUUO+G3VTiRc5yiOWvP69PCBUsoZWyY3dvdcSaaL/9MHa1dAKp4zwJuNcg15Gosi8zhno4/wdGpwsWDsKI3KgiDsX+muIl73lrOdFPBJ1zD7XlHJtnx9nYJWQqGjZfmIlFjEY3xx6IDolr9zjN4zOZyaWmU+JL5UCIcksGsXNcSWxhtP+gZxaY371bJkphjCFZicC6eck2xLg4cbLG4141WXQ5qJapgsagnqVwCXkK6VwYmJJrkiZADGJ+oCnvUgL1eDn4hl0RTFmo7dKoVhlAu1xjZJ0EdYUqlzJHtabLL98h9VUiYAmxq7La19NywEsjvyNTAY+JVAlXZLpxeBFgjjmhgjzSMxZXYvMgloyUVzQsQnERE8Mw3UyaI9u2LqIxS9TS4qP0r5XkGFdkeD5RmGrSAHtHmwF/ioaT7ubsN4M= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c5f6e1c2-9d69-47e3-4f32-08dc5af817f1 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2024 13:54:38.2041 (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: RTq98mYlgbqTCodui9Uz8b/WwlmtTwXslLHnx3+OPqvHpQalCmBtq2XHGJR9ABB0tocsdJDTMXpX+VGGMXxbFw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR10MB7572 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-04-12_10,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 malwarescore=0 suspectscore=0 spamscore=0 mlxscore=0 bulkscore=0 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404120100 X-Proofpoint-GUID: lIycPnx1Zi1SFyAGFp5Dvq9BYAHqQID9 X-Proofpoint-ORIG-GUID: lIycPnx1Zi1SFyAGFp5Dvq9BYAHqQID9 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_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.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 "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 reports an error and ignores the attribute. When the field that represents the number of the elements is assigned a negative integer value, the compiler treats 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 reports 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 uses 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' uses 'val1' as the number of the elements in 'p->array', and 'ref2' uses '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. (tagged_types_tu_compatible_p): Check counted_by attribute for structure type. 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.dg/flex-array-counted-by-7.c: New test. * gcc.dg/flex-array-counted-by-8.c: New test. --- gcc/c-family/c-attribs.cc | 68 +++++++++- 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 | 37 ++++- gcc/doc/extend.texi | 68 ++++++++++ .../gcc.dg/flex-array-counted-by-7.c | 8 ++ .../gcc.dg/flex-array-counted-by-8.c | 127 ++++++++++++++++++ gcc/testsuite/gcc.dg/flex-array-counted-by.c | 62 +++++++++ 10 files changed, 442 insertions(+), 21 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-7.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-8.c 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..39e5824ee7a5 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,67 @@ 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); + tree old_counted_by = lookup_attribute ("counted_by", DECL_ATTRIBUTES (decl)); + + /* 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; + } + /* Issue error when there is a counted_by attribute with a different + field as the argument for the same flexible array member field. */ + else if (old_counted_by != NULL_TREE) + { + tree old_fieldname = TREE_VALUE (TREE_VALUE (old_counted_by)); + if (strcmp (IDENTIFIER_POINTER (old_fieldname), + IDENTIFIER_POINTER (argval)) != 0) + { + error_at (DECL_SOURCE_LOCATION (decl), + "% argument %qE conflicts with" + " previous declaration %qE", argval, old_fieldname); + *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..3dc21e5ee9ce 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 (!INTEGRAL_TYPE_P (TREE_TYPE (real_field))) + 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..12fae8591462 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 (const_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..fb7587f05f1f 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); @@ -1621,6 +1620,38 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree t2, && st2 && TREE_CODE (st2) == INTEGER_CST && !tree_int_cst_equal (st1, st2)) return false; + + tree counted_by1 = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (s1)); + tree counted_by2 = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (s2)); + /* If there is no counted_by attribute for both fields. */ + if (!counted_by1 && !counted_by2) + continue; + + /* If only one field has counted_by attribute. */ + if ((counted_by1 && !counted_by2) + || (!counted_by1 && counted_by2)) + return false; + + /* Now both s1 and s2 have counted_by attributes, check + whether they are the same. */ + + tree counted_by_field1 + = lookup_field (t1, TREE_VALUE (TREE_VALUE (counted_by1))); + tree counted_by_field2 + = lookup_field (t2, TREE_VALUE (TREE_VALUE (counted_by2))); + + gcc_assert (counted_by_field1 && counted_by_field2); + + while (TREE_CHAIN (counted_by_field1)) + counted_by_field1 = TREE_CHAIN (counted_by_field1); + while (TREE_CHAIN (counted_by_field2)) + counted_by_field2 = TREE_CHAIN (counted_by_field2); + + if (DECL_NAME (TREE_VALUE (counted_by_field1)) + != DECL_NAME (TREE_VALUE (counted_by_field2))) + return false; } return true; @@ -2375,8 +2406,8 @@ 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 -lookup_field (tree type, tree component) +tree +lookup_field (const_tree type, tree component) { tree field; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2b8ba1949bf1..2def553961ce 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7753,6 +7753,74 @@ 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 "@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 reports an error and ignores +the attribute. + +When the field that represents the number of the elements is assigned a +negative integer value, the compiler treats 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 reports 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 uses 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} uses @code{val1} as the number of the elements in +@code{p->array}, and @code{ref2} uses @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-7.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-7.c new file mode 100644 index 000000000000..fcb6f1b79690 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-7.c @@ -0,0 +1,8 @@ +/* Testing the correct usage of attribute counted_by: _BitInt */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-O2 -std=c23" } */ + +struct trailing_array { + _BitInt(24) count; + int array[] __attribute ((counted_by (count))); +}; diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-8.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-8.c new file mode 100644 index 000000000000..058d58fb2931 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-8.c @@ -0,0 +1,127 @@ + /* Testing the correct usage of attribute counted_by in c23, multiple + * definitions of the same tag in same or different scopes. + * { dg-do compile } + * { dg-options "-std=c23" } + */ + +/* Allowed redefinitions of the same struct in the same scope, with the + same counted_by attribute. */ +struct f { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); }; +struct f { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); }; +struct f { + int b; + int c; + int a[]; }; /* { dg-error "redefinition of struct or union" } */ + +/* Error when the counted_by attribute is defined differently. */ +struct f { + int b; + int c; + int a[] __attribute__ ((counted_by (c))); }; /* { dg-error "redefinition of struct or union" } */ + +struct h { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); } p; + +void test (void) +{ + struct h { + int b; + int c; + int a[] __attribute__ ((counted_by (b))); } x; + + p = x; +} + +void test1 (void) +{ + struct h { + int b; + int c; + int a[] __attribute__ ((counted_by (c))); } y; + + p = y; /* { dg-error "incompatible types when assigning to type" } */ +} + +struct nested_f { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +}; + +struct nested_f { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +}; + +struct nested_f { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (n))); +}; /* { dg-error "redefinition of struct or union" } */ + +struct nested_h { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} nested_p; + +void test_2 (void) +{ +struct nested_h { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} nested_x; + + nested_p = nested_x; +} + +void test_3 (void) +{ +struct nested_h { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (n))); +} nested_y; + + nested_p = nested_y; /* { dg-error "incompatible types when assigning to type" } */ +} 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..e8b54c2de1c0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by.c @@ -0,0 +1,62 @@ +/* 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" } */ +}; + +struct trailing_array_6 { + int count; + int array_6[] __attribute ((counted_by (count))) __attribute ((counted_by (count))); +}; + +struct trailing_array_7 { + int count1; + int count2; + int array_7[] __attribute ((counted_by (count1))) __attribute ((counted_by (count2))); /* { dg-error "conflicts with previous declaration" } */ +}; + +struct trailing_array_8 { + _Bool count; + int array_8[] __attribute ((counted_by (count))); +}; + +enum week {Mon, Tue, Wed}; +struct trailing_array_9 { + enum week days; + int array_9[] __attribute ((counted_by (days))); +}; From patchwork Fri Apr 12 13:54:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1923154 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=DxBDxB5c; 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=loZbfYRJ; 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 4VGJ3m4m1Qz1yYB for ; Fri, 12 Apr 2024 23:55:16 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E71FB3858427 for ; Fri, 12 Apr 2024 13:55:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id D31A43858C31 for ; Fri, 12 Apr 2024 13:54:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D31A43858C31 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 D31A43858C31 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930091; cv=pass; b=LT4u0PATzEEUJbVoXGVRmraHt23wJtgDyiAXyXr9wGs18BTswpigEH0rGMt7BjsE6QwdeeNYcawZunFpufLLOFW1aaJdwFlMp5kDzKRxb5e/zhJWM/6A4hH9M1hwLwte8ypZHGWeBhdMc3LaSSejQAbI2vY58+ZqMK4OgdZpSgQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930091; c=relaxed/simple; bh=PZue2mvsN9N6+6S53kV5qVC0Yu/NnYdjQ9N7sWHXLs8=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=IDTfL0rmdRI6LIYWP+lwC5XtTlqs9NLqizND722ykZ/oRqqcO2BwKENGD8MZmBcPfyxMnH8F+qRLtRxKzwnCP6t4GpDVHWH3+bTVfUOmBp6g0pHD9fWWAz9EpdQHjIu6Uyq5PKJmr+dyIwcGWzc8enPPq3w+m3RxCEdux6jWD88= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43CDSIqp009588; Fri, 12 Apr 2024 13:54: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=BlM8Rxjz85BWuGsv63OD28xLvDeGRBiBNiD8CUQGnys=; b=DxBDxB5cYvqa1q2FIsCDP1uLrLhYr+OFyFIink3haO+fczpOyC53xnmDakhRLb28LS10 xqExLtf7jiTAl3hOqqaklNPO6JJDBCOTB2JM6CK4FdrOQMMLpwnAKvKjtigIVwZYNzvE UTz1BcCwL0ORmBj+aaDSHJKMe4QdVRYW5A0WzCNT5TbVRBwVRWfZXsCaBqsY99yU6np8 a5g7oo3O+4zws0zwrp/2sn14e6N/OOrOMGjv/HK60Ging3l/h7ZT62MJiUfVPXNtXkQb AbFrRie5p7bMcV/9hcZz8vjO+lMARA9y9LFtm9q70+IqGVdnvEQqicGP2IwYSKu++bZi IQ== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xed4jtq8r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:46 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 43CDXefO002873; Fri, 12 Apr 2024 13:54:45 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2168.outbound.protection.outlook.com [104.47.59.168]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3xavuhabwx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mkA3JWDDaPXHIc/q7IBA0qFhpLBS/jmY8hFYkQ8q3oCPOgfL1qpk+1K7/2udJyA09EhWxj1ZkQkvl1mQ7Pt945DbkY07c1R80kxhTSYZJIekCtv5kB1ivSQE0Qw/2UOmKHp8WkA7PYzGpZbXFtJnoqKwsFqLlSnd96lTRuqH3ypSfeZg6c3Ysp0pG2CWMMoKz632zPPGKyRorahmSu3FIp+uvbi/LnNQmqTNMtAz01QgGCxo4PqyS8Qz6xACyDokGunpG3oCJFxZT8tUv+E2bcy3y8CItx6VuW3t23qP9elsamazvIqrbGGvZc5fdZCmhPIXE8P/gPxm4JYu66D6pg== 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=BlM8Rxjz85BWuGsv63OD28xLvDeGRBiBNiD8CUQGnys=; b=OhCuHReFxvZT24WTP2VjLwaC9Idw4N4Sq/0vGA2+2Tn7HwZk1KdBsCaKtJ/UnFUunPw1dfcypvgpJqLnZe2jBkxhkuK6Nsgaa0CexXz6IvhVYqTQjsUUZipMkkmrGFx0ZpdtKaVIE+2EGvNIN67dCgbB9h3uluywKyolojtxqhwLVHtm/jGDNtzFM3M3cMdNleJfWhF45/nSJDe14KIwIJgFBTpa4cxelegXHgPxcCudz3WDgxk6z4lSjGEJT5k/TTImceXK2zUdv7BKY7GIiNmOgl/g1h/L3oJX42N9Wl8iwzHQsddKG6eP9aJ57WUWh27L0OyMCOdcmb46cPJNAA== 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=BlM8Rxjz85BWuGsv63OD28xLvDeGRBiBNiD8CUQGnys=; b=loZbfYRJ9McB2cqPUKpleB13co2fbc+wXAc1j87fmLne6lWPVIt4tQGdqv39QzNrxxVOzJNzFglztAkFIIV6pow8RIt3+3sxSahQ9t/wozsyUfjTKRf9pSmaSYf79YPSZl2TVDn6YAX7tqzggpq50pr9RKJzdvN//YSwygIwYTM= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by CYYPR10MB7572.namprd10.prod.outlook.com (2603:10b6:930:b8::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Fri, 12 Apr 2024 13:54:41 +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.7409.042; Fri, 12 Apr 2024 13:54:41 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at, keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org Cc: Qing Zhao Subject: [PATCH v9 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE. Date: Fri, 12 Apr 2024 13:54:27 +0000 Message-Id: <20240412135430.4122328-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240412135430.4122328-1-qing.zhao@oracle.com> References: <20240412135430.4122328-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH8PR05CA0009.namprd05.prod.outlook.com (2603:10b6:510:2cc::26) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|CYYPR10MB7572:EE_ X-MS-Office365-Filtering-Correlation-Id: 78a65dac-7f47-44af-f060-08dc5af81a2c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SAPhCNCrg7JyS1FMCIl4GkLCHoVqQrmJrzOip7Z1BJCyoO/oJe5X/W7vq1duWDqef87GuiN5rsAbb1mZLCKm9bTFteAfjRIQGLksn2GyHeGOdALWQTphmX7JTBpJmdguKaz0T2KqJvV8L1iDxNuOppc4rpuUpk0ob/u35poYLXt9pH3RWyVpMDog40XPzoacNJXW9+XHNtvnq2HPC8lMu1CdKoMMtNfZkBTdQHFNrEeQkdatWvmJgBNSlLdQpw6EGwkxwsnyz3tyASa6quWFq54fFmWqaCJBlc5IXU8nAxCJZffgmcUUSkTrFdIf5OwnMImv+zO/3nSpTAWuK1biId0Xibyu+feOW6V1kYE+BxNOS9fqS45ZdgtDcKtWGFZd3A4HH1H80wDqAWYFJyO7c62og1Qs8s+rgfo3mckZOngcqISgH6OJBVatQ/Z20HbgmezABTa3OiNm0IUdyrHXNJdtvhwNQ8mnG1AWFYLYCfE6ceyshZ95H47juTGwvCsAZeWX/2jBGzIP+Mdva5FAnxXF5XBv+5Y0ZMhlVI4KXxd08eebpbPoA+zeUAi7GqylTu0vFY5ysfGZ0P7uMX5HgPTFlfdr/xw5XYe3P3XzZOc= 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)(376005)(1800799015)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KPFy+JO6BCLZ/9YhFBM2Uew2WDRO9b/TkkAFFyr1Lv0KbmLLF8a32Sel5sl5yKSb7RpgOHWpkILgHEVpcmIGwCB5YiJvnJWpzjQrlPutvi3acKnBUBvuP2OtVAgii3siH7BNn4r/8BMj/7dgUk5cXN8zldz05Qm9OpCojC6zjjCluuEQ9mI7lsk4kp4pobvcyTulmpkNiVuaOMZOfBUhTyRW5j96GQEpoFk3gU0DEYyQbey/BRQrYQqEZ8asorLwU0ebEqFG6uxXFvkMu8/xBek+rXFJ2xoAX00xIgJtLRd65Nc9MU1FFOlpAUjrAGSxsx+lGWOCk8QHn+CKyNl3m2xRVvb6/gMqd9LSZnMB46bN6UEO/CklWV4RwdiFE/EDEiSJg34Ey9t6rlcBIFQYjm/bnnO6QGJjOtFR0ty6sb2LLQoIj9v05ypHGHoR54buy76BszRNgtvcPWlXDtHWKDNqnBb4tXoGuhvbYq9G0JWY4ddFWib1y0sasCG0aNHmnh2/BJObb3qrWzyUCXNxQYHe/OZjc3hqNYXTDqTXoJICgw4wIOfsr9e8uACZ3HLvKBKcx+WELFi8HOOH/mw0UiE2cm6GDtrUDEB9jIBOq1CdOvR8/w0eJg65KD7YDI12ZICc+rhvYvD5fKchBH4enf6p288UwvA1D4uMSOdmF4hgyARC3nJ1JggSGXBJ2WhoIROU07dgK+AvrcWot7RbYRBQgTkosrO/j3OVVc2xFsjSEo4GcRsHhS8tugOFq2B0+qAdkiK3uY57G2NY7Oc2pqtWlB5HlaeD4bRj19vJd755rcSrkn5Ya1IU3I4tvJ/N1sEhadnhivkAHTdRAiCetC0OWOIgb96IRngGkbytbTlPqRjXY/L/0d/Ml3taCyyNB9hlaDFl6mTt7abK9Z2yCkO/oE0jEcX0BvoJPI1kv/x2jQ6RiHleV9H2Rk5w9g4ObSLZkz9Wbgu0d1Y0wXzTfymYrNYKIogRMy6qU9GNLCpfsyoJWnQdNaVk244blVlFNjOPOC6v/gtQR8qMdOtov0JqqRzvH8StTfKflzEuCBNsHjE4IywAHh/iGpsRS2fHEZfkuYrANOgQMNc9sXOVs7FPWGOC6GAbcnkbOu33pkRKxJEAqaW25mnYkqsyDjlZuEPAcNpoxZfzT9vmZaDAMuH0WhYI5t0tNFIv+LrnFOdrvzgsvMHfT+U+mTVZxnTbKw9AR5n4aiN2D7hMCFNKcGUWVS08zSvMf6uOlYuBUFjbCvhGHAdFxuz9njyvMGEzYBnruzeswUkyqwdgKm+BRBEXG6eiKLhgVWaGCqBux3VTUh2FOMAY472R/LDnyQvSqDhHNbBPTBr1rzVafdnmVvVElbk8HCo9m1hGvHinl+iCcHOqlgPVgoudouCctsl1yiqVKdh0w8xfDJMQ9O3cR76aaed+kGs+TvjjY4H9YoeloaWJgeslOMkeNDvI/vcwiZIZfbR+XOb0r2QpLV4s1lbCPHOcQt+tQFS6qUxltYvzCfL3HjhC/6FSVEU9te73wzCf2jlub5NFFeegOOZ71qJOQ8aIN4UxYffm713vaqE60ObTH6NBgpo8hUK7KhQ/ X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: MKHQAeerHmZWYja3EV2j4yhcXBzcImlg1i4rXN3R5GyzjOlCLahiFivIK0KsZABVs7HBACQB+F4PJJDLqj8PU+F2tQqwrNEnlPzsFZXygU7tJFxd9+MhZzglP5DSJTsKQtgU/RUm9aBuOrwHUG9KLF8y/9uX4RgQWyKt1rBMRxjcBP4QM8HrudpQMEerwgQcEqX/r6Bs8vCyChqe0Ia7H/hz2CYZRb5mNdJuzb22QmlPoUFZXW71LKJ3+WQ4nrBAEzK/ll6hMCnPEgw8krCH/2d0kh1SVBW7bDt6zMJ5FIJk13m1P6gUyD3wyaHWEm68wURwA4ZW5zqq37TLfjVFiq9WNMQhWCj+tE+3AgYYW3YzaaY0CEr1Bd53YylHeybDzSDGNrpz1cyB+g6p8en8RB336iaqSUVwUxI1c6btbbLwOszb6JoudkMTect8D6mLjNBJVuwfkmsu5tM5gLyQZquX2zCLWx8D681w75tvRkQ54RJYy6HtIKfLthtuNrF1gELSNpBjQh/Or14vYcmxHaAD1v3hn6XuuyyNoMMzsQAOb9qLkv+bDjEOUCvnREeXTT8KY+gl8rZFGL7CXNAMPCXyFJh1kU+czIudT532zMk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 78a65dac-7f47-44af-f060-08dc5af81a2c X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2024 13:54:41.9093 (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: nWsU6YZ6lEbEE4K0sVsMnzpoS/5ptrN413fVzpeXKI/jPf+KNdCYRM46ecjnk8G6ACykvf1NxuC0QyHFs7cefw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR10MB7572 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-04-12_10,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 adultscore=0 malwarescore=0 bulkscore=0 suspectscore=0 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404120100 X-Proofpoint-GUID: B3WrVbe2nGwr-qtEys6lSKpLyTiG9Z8c X-Proofpoint-ORIG-GUID: B3WrVbe2nGwr-qtEys6lSKpLyTiG9Z8c 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_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.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 12fae8591462..402e8f78db2a 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 (const_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 fb7587f05f1f..ff6685c6c4ba 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2578,15 +2578,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 original 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 + a 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); @@ -2658,7 +2759,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; @@ -2677,6 +2784,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; @@ -5080,7 +5193,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 @@ -5227,6 +5344,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..d4899a63af3c --- /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..5fdb425f612a 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..fbaef3e5fb5c 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..d75b23668925 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 Fri Apr 12 13:54:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1923156 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=lHAOdr/b; 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=AkbG+N36; 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 4VGJ54030Vz1yYB for ; Fri, 12 Apr 2024 23:56:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3A3543858417 for ; Fri, 12 Apr 2024 13:56:22 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id F2700385B532 for ; Fri, 12 Apr 2024 13:54:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F2700385B532 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 F2700385B532 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930093; cv=pass; b=VKnL3TL//ad/1S+y73Q88SLn2GNAhe3j+1Pamd0vInuSW/msjeau5NCcJOiIhhSKkl61TERe6dziczi31M7VFUrMV/L6cdqX5g9Dr8gIuC9F5hyPYyM2fcYoMDtdyszM8nt48wY0gLUSzYATuQPEdT08+vKurYejyeGJmwt2GmY= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930093; c=relaxed/simple; bh=k6laV2/S+lpsg60nksLhNFZR3HMSDSZace6OpIsLnys=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=Le1GYylqLJBD3fU2A0VzOx1G0Es04Gh2miYerkHAAjJV1epQdz96mw6QSiEE+aXnZyBc5XISZZU8ERdZaa46TolB2r79MXiHQrnuUCaBEkS0re8ilK+4ob4YdOux7aQvcoLDXrDbvqhySTzAhN+LutwEme1REnq47WCaP0hqQN8= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43CCKoal032551; Fri, 12 Apr 2024 13:54:50 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=G8sePRIFL0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=; b=lHAOdr/b2IyMBKWK+pjLT7eBx1QI0y61VPmHv8c4ey87dx/25N2ZWLQaOb2SD1i0RLUi R714g2Wq2wZbCeTm8i9q9ILL5KoM1DP+EURDtkHfxLdS7hALJmiavIsEY1n0izi88tNf uJgjwkVYzSuiisosajsF2LQ+NGsBC3M7oQ1SRp/28W+S73tPvmgTX6WWXNd2lwR39dCU 0thgTGgMpnB/o7Ed6B6AH/fJkb/9EjiK52UZF+r9S1oY9bjtd4RpmLk6x5Jxm1MJy0Zs YNcMW0qqReJIESyWO+qB6zn6YakFjTisN9JroOy0CE8PXI/E83ezDQPHWjOIQXlyf5dt /Q== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xaxedv0ns-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:49 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 43CDlSUn007848; Fri, 12 Apr 2024 13:54:48 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2168.outbound.protection.outlook.com [104.47.57.168]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3xavub17s5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:48 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ivx1dh+sIMmpjBmCn7Y5vcZ8xD+R6iovE9EiG1QQH4Wi52W/SOBGRRJODzYXJVVvOHVcUu4O2FlAUmaQragelWDuXnFqOAG6Ul+9EBeQnhIvC/FDLu/DkZh5vor23Sp3imkBR6pAowW5OwUiTUC4l8PYiSNdtDFazPCVA/z3s58asHLm1EeYLCjvTBWNy+3cVhfhfq+/g7jJL0rBD9NJI669NZKbu8xISNt4nZ29MHhG7HvKU6aVVDl3W1YD8AH05vvVPyggj46iHSa35QhIBZEpIQ273sJshYvamnrjZe3EaQJN8AGNz51SZ3wEOQ9U1QqyVdr9raogs+iK+gUogA== 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=G8sePRIFL0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=; b=U2vuTYPgKOI4zJ4+5o1WeqNPw3L8UMYm1ynbIZu2SyUfOpiqT+2E8js5JIYwRqa7Q7pmSPT1AZ06aWNFdPRwm+IITKJXdbWPpOBn/nWr7YfW1ZhTMTkPxKCAAHvkqQAYLvprKwER3FkTybmj9I0r/lI5QVIzZ3mdUI61m88sw7efG1N5sEA0a/eLUlgDPo2r25srFjJhvq3kSnvN/V0ZOSfIIj0jngFiFcqHOyzld/CoKIlIfGBjbaA+X1EPwj0nZ3jcm4amdGkY/5KpGZNtutB4gxg2dpBwhVaBQx/zuLmvuyTb2DuGevB1k0oKDmdC8ZF0zSverM/Ncg5SSQPZQg== 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=G8sePRIFL0O9qIP7D5ALR/Ap+YtPr+HvCczMpVsAtug=; b=AkbG+N36T5xH1mO7eNjmzfko/dZcrBKUrHpSc/26dbfXq6qHDt4rGQR1lKr4G6eIXfW8+IGUD0DN4cHJCDD4BsaCGHbjUBaj2zjtttk8uEUl5RTtUj4jt+WrNaIxi9obWg7ammVDg8U2Tjf8vfszd4/S/KykbuNRSxDDIwAWxv0= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SA2PR10MB4796.namprd10.prod.outlook.com (2603:10b6:806:115::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Fri, 12 Apr 2024 13:54:46 +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.7409.042; Fri, 12 Apr 2024 13:54:46 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at, keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org Cc: Qing Zhao Subject: [PATCH v9 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Fri, 12 Apr 2024 13:54:28 +0000 Message-Id: <20240412135430.4122328-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240412135430.4122328-1-qing.zhao@oracle.com> References: <20240412135430.4122328-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH7PR13CA0013.namprd13.prod.outlook.com (2603:10b6:510:174::11) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|SA2PR10MB4796:EE_ X-MS-Office365-Filtering-Correlation-Id: 36b344c8-80c9-4112-84e4-08dc5af81caf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EmCtfs5iUEqBdM5c86sjiSf4qPsJJvA0ZiYvbbwJhH/rPX1nt227NUszg9pJvDeaynkW5oR9SlES5vYEAR2DG8v9a4XbNXJesVRWTPP1WY6qK+6vJEfYLkkI/2vrxFWAKApwu6u0k6iL7KxKlvipX6W2CHU4xTWP8o/mILs+divySgzGQXjTUnpY1OtO6ykxmI1Zet3B4Od0/LTALjhbeMNGzpRWGpiYQB1TKoHBj9s5gfALhG71/clS24gwf4cnP/f9w4YHunHk7D1zbA+BuxAG1F4Ph8l08gVyCn2T6tsNZWOBNTpVwnzqEBZNCXmltATNbLc/htIGIqGjfnDJjKK4yinGV53GUD8J5ZYGjR/fxUDOTZKI646mzcl3o9xGCh6Xh4aRPr1PMiEJaDmI6viMOOaoU49XoVWjEvOouUDesNX/9CSQdX+QegHRUIvxsS4NEuurDRq0cQQVnLx9diYZGeBFNERqfPRHB5Wou1XHNto3UZGJ1jvfk5y549yHuCj+2zPAYey1db/Tk8mQ0HSYtNJx8e7lkjcgkmcwGzuHMM5BwD4aFmh/+2E35oRaihA29Or20KCwASjG5T0HFV6B9EW1ACRmOMF7aeB0EMq0SMWJwTDpUupOwpx+H5mg 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)(366007)(1800799015)(376005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TW5mNqm370oDYv7yXhw4mztIj7YxCi8iMjYiYw6ZDHscsqot8h+zCPUMiG5kn4APsRsqMCzbarSyle7oBzFRSCoLm/DHuXWCy54hEpHTydyasmhhPQv+R69ps6plzzeo4gw3q1gbrseEreht6wDU9th5gEwhCN+6WLk/Icl8xezHWMwvdRPaYumF7yzDoHjl6Jplb9DvCSsVdjiuY2K9ZTT23Igdj8lAQqkHwvBQNNjNdGfu6bvkK+uxNSfHOSdfHwgMMLjre/GvVBSitiweq/hP9MTsOAprEmXUnTlku/T7NOI7jC60M3grWTohmXQiyaXUFtqzkqi8qYVHs+1l7K6AEFB9+oE0Xox1C/PGEZ21+5C2C9el3nvch10lj0l4s0XK/hsbXI7nUsFo6QaQJriok/OPz7YPR7OFmlIm2e4Df81WKdHz8hnUQEfG1x4oRW+wlpzr1M+ROFuMotK0msGR/wE8AJ/NxXR7nWsKjx7pAkHx/ITIxgADes+wnE+M2FsA0hGml65c3pkJflfP0VzFZXRgjZweIjHIWQ0m4iDYBKzGkrL2oPtPat6fYe0PwGq7GQM94TZ3idEX8G37QQP/73q2lGH8UAO4Ewx0gkOPWRJq984d+7+HSgnj5907fd40j4FlxnD3nqW1aI0ipVgGWT2DCspRv+n5SFn4cjFMsd4gcIxueJyiAxMSlIniV32+xSANY7uq2w33jLEPmMjQOh1PQ9Zzd9chhp/dJ5ngJfmv7qAacBLdaZnRADeDiHI5xFKccGsDMtnviSaI1+Isu8dskx65nBmb3bwXVTu2W3Klk/Q5+TkJaEL198CXbyfHmUt9G2dc2A8eZLbWWn5bH5wwZfroa8FUV4LCTad1pFcd2iudih1kaQXXpte5I9jYpFl6sBlMOUvnXXrDKciQG4QBjuahgJv3oKqqYVfXe0DNKD6NkPOmzhSTiU8pJK1Vb1V4VT04WjoyJ+GJF8KM5irSxkaRVOhDapjYyVOQvogS8iDE4EAzk+FHak75Z1dHUnfdez82YCkbNRZyYBtOmRQversCRH8GhoLA+bHJo9/3EP56M/ksXxWOI0czoVEr8kFy3DO1HR7xykadYmC5V+eUUwLCddm9vt8MShLnze1e2X9raPR1PCUdiv0n12x1Cr5Bw99mYhuX1fzHSrHz33xqS2wXzcy5KQK+AxKKmbnd9+GygG+APiVIytbfHacN746pOaM8CeGqV34HxFvIcNhxKUgtMGjB2XZpvgO7cekcWrsM/3Oa71a0Y3rAM6hcej2cNAf4//4URYqSbX60MpYmgGu8OqMHa+TgDLaudNneDbGv3ToTV+K2DU40JJEiUzsW5jLk8ss/FbVcq0aJOCdw2B8H0VV/V8glkQrvUdymzIVvEhchwP+PY9UHcp+vphLetguguochSUd2EW/SI1Egt92xC67TEqh6RwUarQKES8rpRwjqsa0NzJVfGZo0ON7SlgqHAUwZuKiSETKyiAbUpt4tUH1xHEdsK2GHJXhL7ZrFbxoZ2mYC4NrlrxRZIak3/W+j9LeIaaLRN5wsl38HyHVZolSOmkTANL5joHkKkvPJQlAfh/X4re8T X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: CS8mcuHCctChPPesgpuuuy03KW+33nFQH+0bXsN7AEfi5vQ9k/Zwpvg5tj3LvWK1CGGGwBOS0zdKM5KjsOJLed1M799CreMo3UylvQWiLi9l6g053dCkgSTOnJMjj4J+F4kgpnoeNSDbfJLUExPYIgN4+mhnZ7yEY9qTN5hMzLZ+c78G+IA8VUDh85JZer0PfNpm6elfYkZeRQvTBAGgkWpamkQG1lwhwbfL8ppBuCvXDVIIG7TF0EWCuaeieFNNiP9JFCX5cZQubz1TM1nAVwGlg4wOz1p4od2WGZ8GxvkIZMRHwLG7x3OyzBFmt4ePUDhr4gu5fTCexViL3IVgZcx3k8vRvL9bO9YLf3OtfAXbJR4E+trABLgCAXt74NITscqk4xUC3I7krKhr0j2ctQ0oc0ZGv4Ih1PJ/dosQc0ezUHAJiDdjjFYo3uCqaBsYdw/W94wWLCeynEIfiQOfSB1TU/UQUJLyC1gvWKS7DYTj5KLQEL1d+a59N+aGzRwnQos3GU+078XtmJFzr0UatZNYNQwqPrPHhoEPkEni/eo0Sk72YTSfOgV8DO5R0V+ZSxOLz51bZWeYQoXYvC+cEwlpRus/ajnM1CFdlseJJIA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 36b344c8-80c9-4112-84e4-08dc5af81caf X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2024 13:54:46.1017 (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: HawarJcgbbCNk9T1osFw1BTO1fGDM410LIGt5GPrPxj7kzCcP3gp64ayuIAd2hlEPRTWA2aVLCstm8ghuPDxXg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4796 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-04-12_10,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 suspectscore=0 mlxscore=0 adultscore=0 phishscore=0 bulkscore=0 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404120100 X-Proofpoint-GUID: 2wu5nS3lSNDS-gMzq4XyrlTmw6jn8vg9 X-Proofpoint-ORIG-GUID: 2wu5nS3lSNDS-gMzq4XyrlTmw6jn8vg9 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, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.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..78f50230e891 --- /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..20103d58ef51 --- /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..68f9b0f7c8d2 --- /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..8de264d1dee2 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 Fri Apr 12 13:54:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1923158 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=dGob0mLz; 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=foObH2C3; 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 4VGJL03hdlz1yYB for ; Sat, 13 Apr 2024 00:07:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 917F93858417 for ; Fri, 12 Apr 2024 14:07:34 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id CF25F3858CDB for ; Fri, 12 Apr 2024 14:07:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CF25F3858CDB Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 CF25F3858CDB Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930837; cv=pass; b=WeUFcN6Vnf3eS7an88T+xSQEm4IVv/8l+JsIj5Jb0k2gZSHupADck75BN8FCjnhJ456grRIrtDzJ0ATS2NKG4NRZWJlEv6bvZiTaUBFWGgHo4ZIh6Foe00YWBgRAbe1NNLhBBzpcE4LE+sDuUy2HgN22CpozT4a/YYbKlKdrGQ8= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930837; c=relaxed/simple; bh=VYUdRmfEB4cFFMIRemDI8zFmG/JE6raxBSU4mCyIkg0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=Zry5V8CxfFmg0J54Ysl/6i6glgR+p4+vlHe6l15mWPVC9dak5nLLWFM/de7TOnbjMyNQw/cFw0pGvTEfWVM+m7aDOAoIQz7uiVxmlpQL7HsmIoBopkKirb5YUdqPi2gUfXKoXMq6xxoKWZV2uLzBzuRYOKn+DQOLaVaXZYmaNbs= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43CCKoHP032579; Fri, 12 Apr 2024 14:07:14 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=oKYSrntSY5aRPtPOaaLCJLyKDz2RVKzXgLdz/F6HqBM=; b=dGob0mLzVpC7XV33Il+F/f0DFdmqx91bsNo39ykf4YUYXgBMk2bd32XkfqJ9tyJA1S0v mrFmKBUNlVJxTkKZHwyMwS4vOjKZFfvoF+V7m/GFK2Km/VM3bQ9UqNd/bHrZG/UUdvFq k5EUz+UU6iQYEwEfDY5Vy5gt9KP+3zI7ZoO5/UxETBJSUMYs9X/Y7j3usVYhMb6TRXnm b7IuxWchsW6rAqtSEMTHNI1fcDwKoH3jrGNjfXks5EEATs7Sgjg/wGO1oDXS2+5MyKo7 GQCh41NffpLw1z1LNrewctvs7xHfCo/iIjLNqYbUFpmz4vqjJV4Qga+T0eObGqpZcYlk hQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xaxedv1r2-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 14:07:13 +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 43CDrEUL040021; Fri, 12 Apr 2024 13:54:52 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2169.outbound.protection.outlook.com [104.47.57.169]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3xavuhaggb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HEOtVubKcAzBjg2HqFGUFnL3bbaAGMd9dKpFyOMNRAdFm3o4ki37fmZNGNZTNLF+3PLfAGDK/IxtAncoUGVF90obldgUlGySywvUnW56kBh4205ED3joku2KyOAVKavKvllfsaLOHKf+Pjpn1lSuGBTc1kPvTWyfNbUGS0LK19u8L1/QzX8ZetnDrKd9ByHPr1G3/Acee0ievtFoOR+aaOA57iJmcCysNd9MqaznlaphcEDt9AXTKOY/Q5m2RoVvtXRDVBJWiUFHFDmr2xZ8ZjT5O8AAGVfXT+gCTYAT2iREJtvgh/N2G/9BqfVS6G4sYiufGm5H/wNE0hp8qfAU4g== 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=oKYSrntSY5aRPtPOaaLCJLyKDz2RVKzXgLdz/F6HqBM=; b=i2R6cdNSH435jlbOweOPR9+Yid49CeMHhT/RitSCLgEYX2DJZX3XO7Kgu1Tp43zOwhfVM4/+FK6t+hd+b6D+KmcplcVCiwvPwIY+cSN+0+CNy254dwP4P1qMXFe0f/GtKBzgtg0PyLC5WWf8YrS7dDbjHbOE1VTjwCULRUKISZUgtU4hDSUB/p5+Y6DPQ/1mPXMgpT6C+M85/A6412qcZ/kQCp6O8TPYfGZGnByYJQd6PWF/4R2fiOeQ4VUK4Rgk6UvafIky4qRIYkcL2uQmAK0g37L4nwqOGBFBxRstxWLlq67qCk1HevEZa2/Ctuz3YtxueQRSCSPst5r2vNm8sg== 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=oKYSrntSY5aRPtPOaaLCJLyKDz2RVKzXgLdz/F6HqBM=; b=foObH2C3Jc6rzc0YegaTGNfU8qowKnZqsLb2YcgcwY1yFW5cBJz/4ng4wExcs6Rblu1xd19LyG9LBvNJI2W0yu14BiQEvuK3FLoidc1i6gIeaQPwog5eG4CIpn5vsPsc9rNUnV2wg26O5ZGo8tAsr1lUS6MFR6MhmBjWOPKc9bE= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SA2PR10MB4796.namprd10.prod.outlook.com (2603:10b6:806:115::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.46; Fri, 12 Apr 2024 13:54:49 +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.7409.042; Fri, 12 Apr 2024 13:54:49 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at, keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org Cc: Qing Zhao Subject: [PATCH v9 4/5] Use the .ACCESS_WITH_SIZE in bound sanitizer. Date: Fri, 12 Apr 2024 13:54:29 +0000 Message-Id: <20240412135430.4122328-5-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240412135430.4122328-1-qing.zhao@oracle.com> References: <20240412135430.4122328-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH0PR07CA0054.namprd07.prod.outlook.com (2603:10b6:510:e::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_|SA2PR10MB4796:EE_ X-MS-Office365-Filtering-Correlation-Id: df724748-d447-48d5-d12d-08dc5af81ee1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6YQPrDSc8tFOO44Ub4DR1GqnqzntFELNma9bcvh1IDEQ+T+7uUTVHAAsJdBOobMXN4uvbPKBSim/7njUaCCtI/d2q/0bWQCPgy5iEoqUmTGcEzNN5ufFxDLL1x1Sa/Q9LVGD0dW+++a/hsA1U1oHD5WcM/2Ui1KIa7C6pPLyCyCoanx/+JjMHNNAtVdKhZjjYiQJPnJblv4g0QRzxCfzoAkKvtAfyyaXeE/CK5AMauCm8ASILpxM4xT9zs3iGGc4CFhccp10FXwLS8JjjY516kbZxf0Xj1WgWT11Hzx0tqhGKqD3XBYtc8Qhnsto3UIobjvQu+FasuDlde3lQXcbwh+Reu9fopcXy2ocuqO4tvDPQjEOLzkKk3KXXHU6iJwSRx3/zpnfcyYzpE/h5hfidhO0RMTooABQOj8t78YSQfdD34J2vFSbAVnjzWhHGPJ5NnOLmLVW4I55nxdNF5v58kkUX7VnPoyV9NEkek6sXSDT+sHYbjAJJA07IpupAma1KhSEmeyIhY+kcMo8lyNg3NVzMof4kvPI+bi0fsJFUuqQGDgpQXOM8N97ZBPNNUS0Et8IWzjpwsn6Th0YKbpd+xt8AAwGY1sj0D2aiKyErg+X8ImpAmQdpE29vtJii7F4 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)(366007)(1800799015)(376005); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kltcImQ5SDivjL1FOWyN31HLDjR66yv+ARqRhNvYRxzxCiD9E6ltmmetPim/iNWf+s628E5KonY6Jkgr8UXTJejbaVWF/n00aVENqWI+0m5/1i2iqoZTJE33mSwKWAtxrvxLIy3Vt2Rw56EaLWjsp44F/6Hw9oaoOHIFqKEN6jmJRQyWZYRhJkv9myihUgAQs+0fF7oFRlNN6rKG3VcK7y4aSm/sWOQhBTvM53Nfr+MaGYCc4IhxBD/UMHdkz2Sdm1CdbETej9S5LNa6fwumPwQQFFb8utSxQ2ikJT/ezNAzrsFlNBpnETWHDMC4pTnK75YMCjsCgL3hoSLbtQi6ALvjUp5ilC/Ua9z3H3sQGujITLGx7do5GsXKB+vUHOnNDX+LeKd+7sXZVurNf9bwWpUW2rr/mPVbXBOQrqxRdS5hfPCbNbwMOAsew3RTKrbTDunFrSHwz2LBVTCjWLCYW4h9gxUJL/FGndWJ35xfqv6/LhWG9jYvODnSv7vH4yZy/dqQX1P6L+4Gq867CWuc4mzGPqKGdSL30f4uun6sSIJJRtLVnpMgygUQb4kIpKjDpcA2peSYHtWZOsN+aCo6lKlRyveVpS0x7B/GzRTXv2egy14IQMWXZLUYn9xh7T7opqxG/EiUiKghX6f/GUYF3UZrVhvjzP/uRNSAH9Nd1P1qOxGlPO5QR+zd7waKr6XsAi4s2dKnsTlTSSnR3gyTgNkKpW9cJ4Q3505SVlfwEAQbPNb98W5gRT96JPenH5LkyyUJbbhP9UlqWFKVKJ57lnRXsv1+XR3VF6enN+eRXIqT5G5Apu5tNKWgCeHDA8CAChwBypFqq4ct43ogI3DDLfes9dCufUVcK9UgaKBnFZrxcxh5uiljdLVOcZaaVZwJEqZU9JA9FRZAbJqP0g4g1sYQMqKuwHnXhD2WXyRSBwhSBNop+FqRlJCstgVPJa1kjeTUip5Xl8aJmL3remo4gHiwRoY5Z75P/3qlfWzAgauUEw+/8p+lXz718mlqq7bAiiEBAFMvlBbFcZVbemahihQ2J6w1zSLThf78rhwF1IfuKr0lSsNctogX3q8bzzPdswWVJk3KwkIfXXiQKYvGWz/6vPxdmwVx2Y1tNRASWcwfK0r0a2cLGA91M+2e+aGxDkfJ9qhfelhysL3HTmj7Mkq8O+hzjzqn0K7RxlvH2plxaHwyTAhBQh/PNQW/B/ih4tcxt7L6hI/gwjxksdJ3gFCp+S3iPJs97oYjZ2B89eZQLc7f5engkT+03jE6Zv9Tcy04oVDizZSSBQeBVj0CBMGpt0n843KrCFYdm3izx2RSP83rOVHaE6oRMZJ6A+u+0OOPz0PtmgulEJ1SQIIQPKe9reoxGrPgRztu+YD/iB0zVnTaCukHdSzQ1AieeDMwI9T2ZgzF5xdYozTuE9ws6v3ZwSMKFPrFMV8Igt1/Bj+nwbd5Wz18ur/N/INg3TP3K4lCV+NdP7uWrmgp2wbZt8E80vWQL3RSaWGPqUcQ65Bsup8TYrIifAFLKcJv+nko+fxWF2KFkPKVdNo5DxiXOoBxQX7NG9rg6AGMCIRcP1/mKkLJzM/fLSkig/4IXajS X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: GGI/TH9/MNcuU3WUtIW73xD/Dw3vviHjnuXRPUCp2Fj0H4LUHHTa/6XFWPfooiq+p9dq2IbNihCpmpZMNzSr+4Hu65omOI5L23uGTTqM+HXKFr03jq+e4imOqsyYohPytfAVsOAJpi7vloaV8H8N1WOz095D/Yu7NaN3Un2dzNKAsBs8y/IzZTLwPkkVyZVRpbdxZw1HrD7q+2TaA/fp4fNp1DNkZyJeVyexnGyfAQlt5sfWXHeX+yy+VFSG4G2hgqjaK/vJqpHjgJQO0IszFKlAFOTterjxut29f2FKG1DLf7zYLBsTjX8qzubTQmOViW8os52UnDwz8JcTNgfUh5Ii1KNgbPUupZTHUML2Sj00+RbxUiQRTgqqzqvhZ6B4cBweigmKTWSY4LbHs14YiIfDsXGOuQdrJ2h9oPUaDIrgSG4vm+uVvgQbjNzj87EOltAqcZt0VIRbUz95On7qAwBOqRwJl8HmS7L38MWaT2vO9hPP2nAWdw/YpNKZZhGpCrtlAXId1BxbzbL6xDUtMmRcRWqHiyRWCyZqu4sl9G1F5MnYINodWAoMpA6KW9U/yqtSqlMsm0X8WTB+js8yq7tiNq0k20dftOrY3rywwrI= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: df724748-d447-48d5-d12d-08dc5af81ee1 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2024 13:54:49.7853 (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: z7MnfFUXHmim4RAEHp8Ykynru0sZvxSS9MBYK1XL51t8DchxbpZTbQSUhewxsxjMNgXCdwsgpMmzF6vBu0cmzA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA2PR10MB4796 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-04-12_10,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 suspectscore=0 adultscore=0 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404120100 X-Proofpoint-GUID: 4Lc9ijGnfYpveoI4IrsdE5pQNBG_UEUK X-Proofpoint-ORIG-GUID: 4Lc9ijGnfYpveoI4IrsdE5pQNBG_UEUK X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.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..b503320628d2 --- /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..9da25644af3e --- /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..bd7e144274fc --- /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..e2b911dde626 --- /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 Fri Apr 12 13:54:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1923155 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=SBeYOTUC; 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=uzU9f2d/; 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 4VGJ4T4lNGz1yYB for ; Fri, 12 Apr 2024 23:55:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D6580384AB6C for ; Fri, 12 Apr 2024 13:55:51 +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 DF6473858403 for ; Fri, 12 Apr 2024 13:54:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DF6473858403 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 DF6473858403 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=1712930104; cv=pass; b=CiWXbhhVg1vnzeSP64/A2EnvYnREIIOgI0AOvHS+AyfMF/YFs2RMPtxNtC4E1Uw1ccub2cZDIKZU7B8y5kJXHE9SjOLOKjVKYIZ/UKKlOdobBOA8xXjq9YEji7jn3sQNG7O97LynCSNCskEi+47yJ7qRN0yioXfLpsFC2HTOOzU= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712930104; c=relaxed/simple; bh=Tp7fVz/qePzkjUGJIjpyguWwtk53aaYSD99UTxH5kLA=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=r5Agi3dJIQNiwBQDfw135b9LW0jYL9ZKSL/j2TIRZQRQvjhOJYjcFYjJdoosvlyEzTeNKtl9KLTpmWwSlbobzhHmDWANMRu6wvxRpiKzr9vLirbTxZH4sS9UkqYaASc4z0196jLUu5tmEwi9sN8Rn6nnVA+zPyrrUT5Bgrb7wBk= 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 43CDj3Md023471; Fri, 12 Apr 2024 13:54:58 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=F7BUBOFE9RqyYj0U8aQVUhUhr1s0aa/jcoPQeW5qKuo=; b=SBeYOTUCSm2K240Rnz1V8sChRSzVTd5Ga8cRNulLUxdeaTTUk8+bImuyZSYF6RkC6hFr ikAXuHBZIT6psRO5clmWtMu6BAO+zFEHGGHRud5b1vogosP0SaN7hup2opxMyiBoByho 7zlQ62qY69LaXOvWjl/wj1p30zx8EPv/PJTPmely4b117a804TE8N5CU/64azLiA5yjX kajXqoXGR8vg/l7b0kvnrVrKkS54lqZzNcGl92Rv3rIVXfgGb6FzEWWw0tvVZASrYO+O iiKs2NkYFwZJ9RSHM4HUWoj4oWnUYMIH3+/FtODtiwZQ1SSwqxqrl7wivv5M72dJ2hBh 2g== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3xax9bbpfh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:57 +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 43CDCHcq032527; Fri, 12 Apr 2024 13:54:57 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2168.outbound.protection.outlook.com [104.47.73.168]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3xavubqfg3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 12 Apr 2024 13:54:57 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ETg5+zAa1iCcFbHNmLBBe0j//KhXgbGT3vbpYvoxJBeiUPnL5eW99rXw3Rnk9IUyRz40h9HacrKvt+ib4c9uCcgMGJyCaok295OBIRaZX73suq6mrQIQs9JTDRJTZqf30RtGTFitljD/MYbHvQgkVPk89SMTfMtCrUXyMv1kULMO+x0YPrtYPdb03bg+m1Q54yWPbe/23nAKz/+h1IW3O3AxLmACqRO/kxsaukic3mMvQMmB08+LpimWGwXC4m49QgaeDUPTH2oNXP7OZ+BDNDJRDMNMNoS9PrEAYLIbTEo7mEtMfhWLjl61gJnORjk26fztZmszU1s7D6nTQwED5A== 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=F7BUBOFE9RqyYj0U8aQVUhUhr1s0aa/jcoPQeW5qKuo=; b=K+EgY1ay0+POYoj+SaRnjDbOxTNBdMUDQrbLpcVMidaGB/TJ5pa1leuMz1b0jJ7Z9Sh6r0JxxAoo9QBBHmBA6JLqRy2DTxdyPBP/dHdfY6k2dUM07EJYpWIN6FyVENowKefLle03e/Fe1A3LL5nUfL1evKXYRrVVz5116tvjlhDCfZHh0RBYZQBkP+RjT+QkhQIIlxgKTd2CO2NkJlZMeOUjz4zH3NBcFz/HRlvfpC/e1XAJAY92myxnnEe/xDYObHTqebLP6IykrGN8UhdNOYhIUhiftX69pU5xXp+uHn+aYyF/1TEtUqtkCaEHw+Dm0BbuW0pB9eBZrv7tlNeRbw== 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=F7BUBOFE9RqyYj0U8aQVUhUhr1s0aa/jcoPQeW5qKuo=; b=uzU9f2d/osoraDxN3hbg0qHNI8FWWzjr4sIQqJkyTeHqY7SIORerimhKjNyNhjD/M05dmCh2QQCNNc1g69b3bWTTM86J6XNLCOETPXPVevhwazQvG5a51AbwRdxJEr4/tFXr3gbOv6q/LZ2beKHEsMwL2I3P0JH5ZJTtW9zX5WQ= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by BL3PR10MB6114.namprd10.prod.outlook.com (2603:10b6:208:3b9::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.55; Fri, 12 Apr 2024 13:54:53 +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.7409.042; Fri, 12 Apr 2024 13:54:53 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at, keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org Cc: Qing Zhao Subject: [PATCH v9 5/5] Add the 6th argument to .ACCESS_WITH_SIZE Date: Fri, 12 Apr 2024 13:54:30 +0000 Message-Id: <20240412135430.4122328-6-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240412135430.4122328-1-qing.zhao@oracle.com> References: <20240412135430.4122328-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH3PEPF0000409A.namprd05.prod.outlook.com (2603:10b6:518:1::46) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|BL3PR10MB6114:EE_ X-MS-Office365-Filtering-Correlation-Id: c25f13e2-1301-4ebe-2beb-08dc5af820d4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: drngj9nHPWM1sHb5/2wz3/B3NrbZPBRnUw8k07cergD9I6kAvCMI5I5ampJSy2I139liSBWeYmnw0syyjDnI3RAR7M1NUQiXMscwAEfpNxtxH4/36aXYK/A0z0Az8b+BdFw6l7j2GEaCkmz6OSbP+pkB7AvBCy08e+JqosvbPx94/amWPZQi4uU0Kxu4bWJvZ+Tfd9TPtJE3nYGXVrfs14owyOLvmLehI0qbc+ZxYOxILZjyRbNWKKl5jtBdciP5rZl8/F4qroaQOQvM4tO5dJQoCf613DIfxhZl/FUz6k85HwOXYF+9X5rzBAXNg36/5bfI+a3zRn2U0FmwJDxxTG0icJ4w2wu/oEwDV9Y6rjHvK4AJsMugx7HDRfBESJ+6X4XnEYlvHm6IxwL1z5UwgqGgSNhS0mPlFQVDb/4yoykug4D+F48tRF/jxTTObE+z8KnehpY3goEws6cK7GY8b8Nof1JGmybaBHzoQVPNwX9t/OIVRQ1jLepJtL6qiunTeNczi/rXgtFIWiTjtcYmklWLdNmfxbfJN1o+j+2g1+r6cj8o8b2vtqP8LuuTihHBBDtssu/GksDvefYREfLvnQWa56rwJYa+U3qiz/z9OCM= 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)(376005)(1800799015)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: E2POAHY+OxZCGyedi9DD/A2EX/8uUp8rtSZbu39Rjv8n0D+auASPuuyaxsSXFZzXG0PJoR1zZvxINoelXTnmbQDhFoJXmsBseTdqBC+cIcFSH4A2RMGZRExBZCxlZvA3nKAo6j7NERtyov7aBE7dAinDvndBVnhCOjadGyzmYHrgk+EtC71yJWIn0XjtQXE+i7KHEk1Ord8ShlXT2xhElhgUduBWB5AGbgHXUN+bJfdHBnVILjqSvOIZfVFCGX5JGT+ySpJKo3HPuJaLnfXfceDXKd9lb3o+7v/d6XknW4DJMnlupiLAb/hv0jxbixGwXc+ryCfZ6J08AVnNdnwIz2Kpzv5J9s1hVZJ3swAvIQCS7a5VY+i2/U3Eu3pavXA5T6rmXrutjjxW/7uBJAy5faDgvy6BS/FaYO78/oQSaw1w/RLls0uaq2asWLvQ2jMmWGkJwMJjP5V6sUolbcDG72kOZAGqsKRMu+8jiIVj1+E2UnW4rpnVIVHjZTfTyTYb6N7fPYFQTnkV8k9PsiC9AWcLtO6XUHSTHvTgfy2KBMiKgCDovLnpYpvKHDMQ9SiiTOk071EbEOPIeUnJIakmI3beEUUIeloGOXU1Fc9mhcBeH5Lhfl+hUawut3Yb8lsiUMiKoDoHPj21xYeTYNQ2Gtiym7qO8LplPFws5vuTPPAePDkgE74XnHXXu+LDeca8S+jo3uaxMDf64NIa+2oqzgwuaRWd2OqC3A+xqQQhc3v+n3Af3HVL/hD+n6tolz7SGpdDpdiLQt3f/Mi/h8skT0/DF3UVeVbcXopMmmfIWofGguyhm/5aa1iWAKAwvWeRJmU5olklS0Ssc58TMCeKRFJ0SzWR245Tbn1ZARjYrKOFCu2Pj6YNyul/721/TLZ9u2TJDzw744pVimSmvELLF+xcq7nPuM5XkkAH4bSuDE0V1t3au2dfk44VukCRDf6FVABT07jI39ar7Tfc4fyFG3nVGGFI3pRtTUohMGahp8O2h8svMo0Y+JQSRarwMNXTiB88cQPDX0Pxo4ghGrfw9hRmUvPvIUgYzWghqIvAVk7LAkkKEjUVaISJMXRfPiXfTu8EVybwtZi4LcAeohX4Mmt9oTEQH+uCOeLx3O2h/egLKIpYIotsWzZ4pMYJFMKvo3wyVeZ8FFpjVeNpL9hZugl11XSM9iQkyZJGDqRBv8uLKqt6rA/IgqmxuQO2SLAOCKWW6uwLuTp3E0HwE/Y0pxJjG8s988GT26Pb9smN3bTfvp6RL+RqGMKCdRccEiCxQbMerRcjMGK+2XypWqCtNBcxmJqI+hVDGroaYpPj5IbKYLgdCuCm9opruXfcT8sQbK3S2uoVfoztHew+NO8jfoeu81e9IN9dHtRKJDqeX87AhKHMSpRVEemI8bl1IC4YZENMqoGR0CJ3zQal5T+cqY8nOMWIDWnucqDptoXphulqvxfkpmdKQlanEEPMJUt9rxufpcXmLKhXhlDEKng/OxIyXx3kkWw6WtCPFhFkZkdzpXJvXN9BZF1uFXG5gr7iXK/gDUCXGxkdi7oTBbG7WQvzfPv5vLzkcWjMuFfJ8rEC0yYX4GZlMj927iza/ic7 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: phExjfuuLY2eq5MMnh6sJMaztkACsRBFrgYrWy9bcuRGGJlqth2cLEyGDhdELqfXhRchDaY4sskxSLZvU9/2QDJzJciIEEYGNA5uHBPaGwcmBUFwKFdusHA5m8vuTBj91PAOFGd5rel0+7AA+661PJm4dThq+Y6yJtMxvwDw8E8NQSx32bTc99+8C1W9V/r47yAuxYjoukTtj9oeOmf5A0G8LTMSjfnDfDq/XoLNdiXBPZaTbXCf/aPHBK6EZqJSXSZfqL6o3B4rXSs0hkZ+fVpN1IsFjoZ3vFnzdE4xrEpxKNUqpbV8y8PlnOUGNE+h5asG1g/sOwGfXnWEtu0Gp7JdE73y1BpkNYudfP+gZHS+/V2rwO3ogJIKTMPA9vEfWi/ycmr20xFOMemLTuuN1NeF36npGNIX6vCxn7UNhJuzspoCOh5iBCd+9W8TQdFGyzDNC33+SDCRs5acKRIXc/rVmhTXCz1mOSEq1jhS1IVd8IPSBwCjMLQUEqnspAwyhbdGkDZ581/piJ8YDSsmN9Uh9hyy0tZmzta5kYR2RhsFydWjDlfxEtmacz5HO7whOgBPr51O5Cs0sNrDpHOQIp/kvUgQiYSUIFdbrq62PVk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c25f13e2-1301-4ebe-2beb-08dc5af820d4 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Apr 2024 13:54:53.0328 (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: 7D1JQjPvdyCCHHdqI7+9LW8zAlT4/GbFQHnCZbsZBEqRAfrwPq0fT9QM34p8V4x15q7kl5MBrlVPzxBemafuLQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR10MB6114 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-04-12_10,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 suspectscore=0 bulkscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 phishscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404120100 X-Proofpoint-GUID: sfqrdf6v-ElOfkzvkuXP7n5OHd7KIW7o X-Proofpoint-ORIG-GUID: sfqrdf6v-ElOfkzvkuXP7n5OHd7KIW7o X-Spam-Status: No, score=-11.2 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 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 ff6685c6c4ba..0ea3b75355a4 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2640,7 +2640,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. @@ -2652,6 +2653,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, @@ -2664,12 +2668,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..65fa01443d95 --- /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 8de264d1dee2..4c1fa9b555fa 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));