From patchwork Fri Aug 4 19:44:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1817146 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=FJ5bJ5H6; dkim-atps=neutral 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RHblp0w45z1yYl for ; Sat, 5 Aug 2023 05:45:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F31723854806 for ; Fri, 4 Aug 2023 19:45:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F31723854806 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691178308; bh=dF5n7UEiHUNs5Ksy5gCs5t8W8UKIPToZQoUIJK4DxCE=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=FJ5bJ5H60iStUEaq2JOsm5PlofUp4rPwGXqOOgEozTklMzIZqL96mZ3CYnRi4KIS2 aepPVF6Ho+td+556hbAMPCqRpswQVHvN5KVD7i99RYZA2+56DzQXGV0tIVEs9XV0JZ PIcCzA3dwvPe907P3IwrAa9m1XyheX3PgIX9BbWQ= 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 B553C3858C5F for ; Fri, 4 Aug 2023 19:44:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B553C3858C5F Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 374JCm4v025354; Fri, 4 Aug 2023 19:44:42 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3s4tcu4fj6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Aug 2023 19:44:41 +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 374Ius0X034866; Fri, 4 Aug 2023 19:44:41 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2104.outbound.protection.outlook.com [104.47.70.104]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3s8m09u9ej-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Aug 2023 19:44:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PS5x2RzD897VERfKXC9Jiv4ChMnvTW2NQzX30iKTqIdzyMRJE4d/cFQUDP2EAKV4NXrTHYYdh0ZJiq4xEcZloZxrAFcIiT4AeL1oUlTKIqnssuny07qq2F4rHrgndPBrryp/yTXwDazZc7uO8vpx/2KAFi/pgDzTUud8EHHciaiIqBLV3cwv0WD0XWUyV9DoQgEjp6tptRKItPmz8sDYZnNIYbHXeGey6NFn6Xm26kvwxKQXnk0KFAq002ztH868QdY0SFXx1naQJRGp7xpBzPcgLSy+jcdCKQ0IEvpX/8UlQcCet3TSf2US29d8c0hP4Rm6er5Um34JCU7ltc/HHA== 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=dF5n7UEiHUNs5Ksy5gCs5t8W8UKIPToZQoUIJK4DxCE=; b=amar8lflet/tZoBQDu9ElN57jsQhvnkKGf+t/3jtZGssaaLakg/JEKEeW6mwxatJZML52hccZFFBCI0BepEObBTqM4m619JgWCTuI5uZRZjg6lb0CiAgGHTpmYiKIqDBfDuFdEK3TqSsami6F/nhHfB6EZJrGIplTAcwMDB18oon/UO57sN8+0c9Frrkx+CJE430AzsYnoTmB6folEVd77ehMd4xN1/2liYJqsmKuss59NI4QysTe/GZLRRzJsUW3Ov64Ez5HGv7/Lhn/lkaHyVyMIaEcIOzKfcDFPxM045w7Zn6iPwWVWH0VSDlyilLvOkJWdgdAefEoG2YkLzZCw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by MW6PR10MB7590.namprd10.prod.outlook.com (2603:10b6:303:24c::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.47; Fri, 4 Aug 2023 19:44:38 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::5afd:d02a:38ef:94ba]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::5afd:d02a:38ef:94ba%7]) with mapi id 15.20.6652.020; Fri, 4 Aug 2023 19:44:38 +0000 To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, siddhesh@gotplt.org, uecker@tugraz.at, isanbard@gmail.com, Qing Zhao Subject: [V2][PATCH 0/3] New attribute "counted_by" to annotate bounds for C99 FAM(PR108896) Date: Fri, 4 Aug 2023 19:44:28 +0000 Message-Id: <20230804194431.993958-1-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 X-ClientProxiedBy: SJ0PR03CA0213.namprd03.prod.outlook.com (2603:10b6:a03:39f::8) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|MW6PR10MB7590:EE_ X-MS-Office365-Filtering-Correlation-Id: d5537dc1-c95c-44af-4dc0-08db95233d2f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HjGkPbz7dC5L0likSI5fxMRmYgmd1X83i0OkaqjRfU1WwTR0R2ZHldTs0VNmWbPzFYKntXODNazsOi5Y9GDwkYvy5JAo1ssqdaIdo8swQ93PbMU1fQKVfsI8DUDb9cMi70tcQLITa3HdnYAf1JkzqPHxQSNgav5BeThSBaiC5xmL1N/udzaop7rf/qh3W9wWaKQyiPER8XOdbydV+AwdzlFMGbBE66IJVxvtcGtGJq0inlDlN6fFtRHCjwZQPpHEGMZxSRmNZzPut9XKMc6Bky0HzGChRnPq67pT5pXiu+LDCA2AwSsp/UgOb7EBj7WEbRcKdXfiH/oswI5VbmdysgvfZqDlV8viU9BWGeMM9tEmbad4StM2f4dRmENGFz4k8brR+gQOiV3LaddKVO0UbCp5GCg6NGHfWNPmOHWEHxtfRPNILnIxmQzU7bjGdOCGj+EayRlrlE9XFmDtoY7VdGwaTztM+5v2sZbW/ZtunjGudcvYgXoW6q6PwWUm4Ppqy4Cyrp6MovyGlWI6y5WmTbcBXgkbgcNpjPWPg744zeo= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR10MB4344.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(346002)(39860400002)(136003)(376002)(396003)(366004)(451199021)(1800799003)(186006)(83380400001)(1076003)(2616005)(107886003)(6506007)(26005)(8676002)(66556008)(2906002)(316002)(66946007)(5660300002)(66476007)(4326008)(41300700001)(8936002)(44832011)(6486002)(6512007)(6666004)(966005)(478600001)(38100700002)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?fRnu0NiBlv+B7uy8uZ/3Y5kqUYlM?= =?utf-8?q?OsLJ8CZpfxQRwQazmx2pWJR8UOSIFwhQd17FboVRpBjbCjOYx0uqZb3HxSqQO2Qkw?= =?utf-8?q?7TNq7sGNdPMHP64tdUyvfYs3TdDMp/u84s3iG+ni2rNBefwY8oZlvFfn8U2mL8VSo?= =?utf-8?q?r2LjvvWRBufZcKaEne7E8HBukgIJqABXRi9R8goyu7Wz6QRejfRMVVzdcWRo/WTLS?= =?utf-8?q?Q15mHLXrW8S2L453IjuyN5uzxrwDOWvAJ0xiZeRmOT+Iy6BtebtIv+WfxSwc64vgc?= =?utf-8?q?rOOaws6gsy5XBgUVHk5aeRmA8sG4AgLHKR//cucN4obRF0xZc/0AHL85jRmME7G0O?= =?utf-8?q?y6uWz0PnUvUaUmSM/PNYMMzP5rTRC5yqVt+PPcjZWYzP9tlLgul4842sOQpefFzM+?= =?utf-8?q?lce0dm+TREdv5t5kaRaTREkK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fL5UQ9MNOh58aiYSicFsPcMX0XDsa/6kQ45a5x1QaFKZVf47Rg3?= =?utf-8?q?lTlItLoiHO6zH4sSNFUUl0IGt97+fucw95cJPsF1p4HAdKTZUHaGB7DAg8ZWOvt8p?= =?utf-8?q?4WVbdUFKkb5aiaqNd/+lUElLsZ5xBLIui/1IvQ3whp39odz3L05syhQRjZ8GFJr6Q?= =?utf-8?q?p0hJcR527JncWtMMA+JUdRcCpPgNRWEoooSCTBLtjQrLy+RzZ2PpH/Ox2uzjsS28B?= =?utf-8?q?rkK9AvVR68O6?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: =?utf-8?q?PzUc2B8/wRD7vpNd?= =?utf-8?q?8OVoKgbt/603JeeCABDo0KItTQsYRQcb2tFUM9F4qhVq+RaV48GfiruZ+mi7Yh339?= =?utf-8?q?eNXB3pvRVrysU+sznYDsQr0F3Xbwsp5nEJkb8sK9kH1nn1maBx3FfJ2DbC8DhUf+a?= =?utf-8?q?0yVEX76NGFrvSv2Tx4QFr20jT17Y57uYT07XfcTVQpfhly73hIGQjPVMTrGCZ23VD?= =?utf-8?q?sqMf3p2yRX9pAXO4M3Pq/rmDC3NRAbDBFaRgQoYB4U6uDsXH/sbMrNWFtBjIgkNts?= =?utf-8?q?1mmyjBGr1/1rcbqwN1a8vRjjJu1VEuOwBo64mUUp9FtulX9RxoOLDBxyj0/qROhYN?= =?utf-8?q?id1t37+rQlscrkJ2jDSJ0PFZH2HmtHZKhev/On2PnQXtIoi8s/fBsMHRLW7v5JJS1?= =?utf-8?q?YzDkca/6pKFg8ChbsDGn0yG1yQtaQWBaiU+OlE/lU0EgMKwK8OfpYJ6ti40oKKwtu?= =?utf-8?q?bBfxsmF2c21n5da7mWtnn6Cy5qUx1i7/iizAuCSHhkkwarGkXiOdZgZgLCme8TtbC?= =?utf-8?q?UsQ9Nv4AYUlHqlXtLyho5Gtk794HSpXGmam3n4j7MY6p4xWjYHVdjQd9FcRhuhP3y?= =?utf-8?q?n8HkouDS+i6c/HCceuhggOIREW/OfrDXrWgXXu//dRHE0zxw0wiEFPtbJf4rimWKe?= =?utf-8?q?ETIkpzAoOQDe/RC7JjGfQJSpVM222TJJ3Qi0963mc83OZ+eLqIXP+2D7QwgOeMi2R?= =?utf-8?q?oaw2s1UmW1bZglzCYg7IZVYDSkRCj2KlkiYpYpRFi40LvKLRQL4YVnoqw0hRsLZjS?= =?utf-8?q?fE3qGlI4XavOwQZSA2YHtF7opsNptk9MmgUAePi+OF/7VKcBjLStrgUiYWcmNyfL2?= =?utf-8?q?3ibrFHJNsxGOJmpw58NfRcC0agTw2C+KUPMLPHKe4RHe5MfyZ5ArRqxFvh85fkd1Y?= =?utf-8?q?dbH8iYbkzUH4nhqujR1SSqAac/kEDeY1cx?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d5537dc1-c95c-44af-4dc0-08db95233d2f X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Aug 2023 19:44:38.7722 (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: ftgVgpsiguDa6eekltkp/wnXKBJJ7JclZ/m/aDxw7z+TE0d5uwLV2fynICehrU2TGZBvoSlWa4L2A0x9/0NJZQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR10MB7590 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-04_20,2023-08-03_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxscore=0 phishscore=0 bulkscore=0 suspectscore=0 mlxlogscore=999 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2308040177 X-Proofpoint-GUID: CaGMmfrKO0791vgOGa7uNzD4J4GhlBTV X-Proofpoint-ORIG-GUID: CaGMmfrKO0791vgOGa7uNzD4J4GhlBTV X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Qing Zhao via Gcc-patches From: Qing Zhao Reply-To: Qing Zhao Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, This is the 2nd version of the patch, per our discussion based on the review comments for the 1st version, the major changes in this version are: 1. change the name "element_count" to "counted_by"; 2. change the parameter for the attribute from a STRING to an Identifier; 3. Add logic and testing cases to handle anonymous structure/unions; 4. Clarify documentation to permit the situation when the allocation size is larger than what's specified by "counted_by", at the same time, it's user's error if allocation size is smaller than what's specified by "counted_by"; 5. Add a complete testing case for using counted_by attribute in __builtin_dynamic_object_size when there is mismatch between the allocation size and the value of "counted_by", the expecting behavior for each case and the explanation on why in the comments. As discussed, I plan to add two more separate patch sets after this initial patch set is approved and committed. set 1. A new warning option and a new sanitizer option for the user error when the allocation size is smaller than the value of "counted_by". set 2. An improvement to __builtin_dynamic_object_size for the following case: struct A { size_t foo; int array[] __attribute__((counted_by (foo))); }; extern struct fix * alloc_buf (); int main () { struct fix *p = alloc_buf (); __builtin_object_size(p->array, 0) == sizeof(struct A) + p->foo * sizeof(int); /* with the current algorithm, it’s UNKNOWN */ __builtin_object_size(p->array, 2) == sizeof(struct A) + p->foo * sizeof(int); /* with the current algorithm, it’s UNKNOWN */ } Bootstrapped and regression tested on both aarch64 and X86, no issue. Please see more details on the description of this work on: https://gcc.gnu.org/pipermail/gcc-patches/2023-May/619708.html Okay for committing? thanks. Qing Qing Zhao (3): Provide counted_by attribute to flexible array member field (PR108896) Use the counted_by atribute info in builtin object size [PR108896] Use the counted_by attribute information in bound sanitizer[PR108896] gcc/c-family/c-attribs.cc | 54 ++++- gcc/c-family/c-common.cc | 13 ++ gcc/c-family/c-common.h | 1 + gcc/c-family/c-ubsan.cc | 16 ++ gcc/c/c-decl.cc | 79 +++++-- gcc/doc/extend.texi | 73 +++++++ .../gcc.dg/flex-array-counted-by-2.c | 74 +++++++ .../gcc.dg/flex-array-counted-by-3.c | 197 ++++++++++++++++++ gcc/testsuite/gcc.dg/flex-array-counted-by.c | 40 ++++ .../ubsan/flex-array-counted-by-bounds-2.c | 27 +++ .../ubsan/flex-array-counted-by-bounds.c | 46 ++++ gcc/tree-object-size.cc | 37 +++- gcc/tree.cc | 133 ++++++++++++ gcc/tree.h | 15 ++ 14 files changed, 780 insertions(+), 25 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-3.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by.c 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.c