From patchwork Fri Mar 29 16:06:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 87825 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A0DFC3858C5E for ; Fri, 29 Mar 2024 16:07:55 +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 BB8983858D33 for ; Fri, 29 Mar 2024 16:07:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB8983858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BB8983858D33 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=1711728442; cv=pass; b=VymevCnsc1+S6mbK9XJ+69KCJf1IJomWifeum0WOWy/fcvkNBeCN5ItBkIsAQWrLy/LtMPUR4YTZFaCZ14juJj0HpO8B/KB9f26Uk7yk/CZhtuAJC7ej3dMVNMWiUZfvdtkYgmVxn0ZPP0ztOEP+5CqKnAISpF9qGdwCLMRdDac= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1711728442; c=relaxed/simple; bh=lrQevKfhRHV9LT737YVBPfXCt53N8vOPHWCkR4pftZQ=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=tBkfw4WGZE6VFQMLRmPotzcNGgOhStterFRHKpQS4AmuoyEFMi3UsoXnNtT7kyW/4p9d3jXz0nmicw8JYJLJhkwFW1BKRLRmLKv6q6jQ+WD9rUqV/uOGs7goTcuPDoRJFRWnFTyGX2qjlVH3jTXkXWXn0nz4NgL3gbgJWbSuQUg= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42TFOCYu029976; Fri, 29 Mar 2024 16:07:16 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=xXL0aERSQxMmUk4GwDTeR9QBripPAyFOkiAQ+ZnYLIQ=; b=PBQ1oo8DZ/Hg+hfsYFmDRhoFkwly4A2SSxLawkomsZb43xYadTiOeP7cVhXU2wJ3N0l3 7xvd5wKQp2RYVzPwRaGJB63Fgo5BULAiJD8Jkki08PEvVC6RiHsNDVRfWWtOs3BO7mx0 8mfhe3vzEst63Rnwxw0J5lCdvbVVlKmuPtpf98+WXLnkBL/L1dVAi32uAazWYcK6nNIl FUu2gNhaTIQMIIoxNELnU26XB/iOIcj0X+bvNnqLs2T80ujA0UMyHW7PMg3hfZ+YDQTN fIxafLAffyhauWSuzN+I6F8lxloA3aGWwoXFfpHPfHFkJ7iCbbUblczl0dKpzQMluF52 iQ== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3x2s9h2e54-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:15 +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 42TEbjHR012800; Fri, 29 Mar 2024 16:07:14 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2041.outbound.protection.outlook.com [104.47.74.41]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3x1nhbd67k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:14 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G9m52xJFGCslfDWiKqiYQGUvWORceMnbTPoC4CNdyyHa97DzHXCcs9BIwAvjc5SZJlf7dU5fYCoDyU4JW8pz3qPcSuF1MlWBGZaDcfcgvaLsQAWmmstFrtX8U8z3avWishrM4KXaP8UhKnRKZtvQp74uopTKbhVpf6apv5NA01+bOoqM16UfyMxHhT/swGm4SW+W7Tw+jTuII/TV/GTXqpbNV4VbYQOXy70uJTzvrOmZcFQzESPLQPIGytj68KhMB5AzM2+cxH9PEtQB+kWuC+ofPwj0FhjgZGgwUcM7LTNIOkZqgVCSBhwr/Pf8lRAFTTnWQLwFL5U3PmsiA8pJ1Q== 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=xXL0aERSQxMmUk4GwDTeR9QBripPAyFOkiAQ+ZnYLIQ=; b=odEVpY7wU46BuQUEzu2YaAJqInuy0YkMa6E+4ZrOdwLZvafUlQ/xPC3iI8PbziV0DG3b8LCF2Ofb/E2B37DTPr76JFXd7ZravUIudqTNcp5alEoAmXkEMsFfANR1fqSukDJ1/ZKad/fE1+g2M+ngwqe7k7vo0m48gujhYMq/6owYWyQCVfaaRTmLK55I5cO2oV0nE8JLSUhP6voxAovqcfuxaL1UCSzQqaWVhDjmShsoUUetBC08hduuM+AC/ijuxMtLVTIxsOIYDvhyRCxvI2pUjyvmeUsiH0qf1ABsOBCE5QXCMLIl4mTa/W2qX6Dv3LPad+Ot2b6dIdx77pp8vw== 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=xXL0aERSQxMmUk4GwDTeR9QBripPAyFOkiAQ+ZnYLIQ=; b=aL/dV67gDt6b8nWN6BDb660Q2bTG/eruX6cErfPpQ+VMN5m6Q1jQkRiYBOI3EiOMnW8I8EH1cNGpQtl9DgSgHBwhz+yvXfeFyhbq9OtGXxyfOylQucAzy7zF9ZV4zCgei77w6hzRmn15Rl+8p9gzYtwsUwCNvWfwOrGkpFAbFnw= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SJ0PR10MB4638.namprd10.prod.outlook.com (2603:10b6:a03:2d8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.31; Fri, 29 Mar 2024 16:07:11 +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.039; Fri, 29 Mar 2024 16:07:11 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v8 1/5] Provide counted_by attribute to flexible array member field (PR108896) Date: Fri, 29 Mar 2024 16:06:59 +0000 Message-Id: <20240329160703.4012941-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240329160703.4012941-1-qing.zhao@oracle.com> References: <20240329160703.4012941-1-qing.zhao@oracle.com> X-ClientProxiedBy: SJ0PR03CA0192.namprd03.prod.outlook.com (2603:10b6:a03:2ef::17) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|SJ0PR10MB4638:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pzPdZpIjOoX/rXpqHXZ5z0m5B94PSMUYcptF7EY1YOENs37gM/CLKyLsw0FZfT7g6Y1/Ulnm28yS0kdI0vFxffh9HZGXgjSxs00nYQj/N9CaPkL+cmCyhhgtrWsSoYCj483op9Hg6NMwSCb51E6FqeBaZLFa+04fJv8ez5/3GbKM/zKp2YNOERj/GdIj8ubYZv/lgRkYWGuLDpjNjP5b2A1fto+oigHzBko3UY9gEd6ieLO1p5ngum9FHH2JUEUoQTdG5Cp2ORCkOSVOwEAluHy5G3BfnQVc/PnjihuGpG9EoQ9HP3CLDn28gO55PKxkqqMj9t0QX6LQpn3s86Bp7cEnMaDlxRmjARgHEJJnTFmn0km6gtdmMAZeXKajKzWn1NVBeBbqW2DL/gBECVsmHiHyrQWHItt9jnUFn/ymIG084yATJAPlrknth7amWBVSp788jU2Ak6c0fVfcHGcCjxm06UbPIaZGLvKD/J04nRHdEnOoPzzkS04Sswi4G5tdNNMYco955aex3zE8Thc+EoisxkqC0aKwMRn7L959ENpI2abBn0DWiLhMEc3pPVnQVIak3q7zBgaMbglZYeK4EIPQeZWds/hIcrCW5HxWEvfOfGDOGY8974O1SJRMh9fq 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:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DWmBgMHNdDXpN7lBPsAAM5W3FMkBEfnkJWtlTXCBJZyhhvqXfYRkPYJnu85fGB0Px6AF2RkB/JhQTT4l6pzMSBMCXcQSElHKck0u9nMFegPLZn6GSz2FaTaw/ZYS4QdllpCZ7TThyeTsEyHkJqhRwElO1U6mQEC0NEryEWqnsRy0Rt/SwUQqz1pqPCI39JMN33wxByQVOYxx8HAuYgrexvcfCAxSdqgaOgxlslbs6sQZysmWBssFAawr2Ui8gkzxvXWThYyNqO4LRPsAwCPGc9WvOK1pnywaSjRrdrh3CiJm2IE/e0vQ6A5yS/prR+ETnsMHbeqGg4JC9S60FqlxltQ7rGufpOpEFe3ZxR3y5WHkPnW9lTrDITMx5MkX79HE4rwofF5XRHXNQun5FnB/nPVv2AFmBHMFXb+3XD7QUmlRSS5FArlJkAdIDOvFhT4FiL0P6bACvOqW8vIZcs1QsK2pbG9b23BxEzpG2FRQgA4/p63uq4Dr+oCkKjB71vwImM9YWG3KbABOmHGidYRzCg3ZHnSdWXb/+o4sqrt7FsuQMK+HIUOgDWrRT21ofFkzJchrX7obV58gQBIfFZt/k8sJegAUd4xXdRIRJHJKVw24jeY8evPqyOKwZIuq/xizQ2PK7gx4rI+LQuGvNe5fsDFYtom+S/tsivhRBfE6W+v8l4NYgY507xMJ8aVFb40Qq1QVYnPnAEdNwTuZUbvUH1RCY4/pojJ4KxM9+5JmK3Jvv3cFkEJjdrSMycSpGjlk3+nSxZ/syuFA2Sw4UfK/aMtH2cs1tZorHHTwCnnxJeodnlccOHHw+djyco7APiPMPGsfalqRjg5Ci4tZiUwx93JN5wnufrvTPTQkJdOAg8viNZgfZWHYNPBiN88g0eSKYjbODYnfTRkuE3WZMSfSOD5W7WL2zLGLC9W1QIrF1icJpt9aSrmOsB/zililAfpqXFj6tvicD/2DvwqsmhZKY9+sDa6GlnkW4DlrMc+3GGVMuRq5bjQ7p7Bqq4hXs2hz6z5yU3p/W+CI8BKUhUb37xSseHXIXnfNeHNgNloJ1G+XwaQGdp8iQkYw9W/hhDDjI25pxVcwllqwfhKJeb66l7LHJ8ISP0n3En9fa43DmNwrSh5UDhCrafLkdbDDPJFQTuijXZb3MeW1ISthi8i+R0YoHWU3qx74Jg/6VByDwuH1+Qq3V29tha0qnyzAgBUc07V0RALRnNgtAl2zw/lmfrWqcW2i02c0I7qcIDByUONV6gnznh9ONkMpZuvoZtJYLlZGFfJZDP73YMx46c2LP51m3QG23zyKIYGmFyK6I/eapFKHeM6qgx80I+Bbkohqn+avgdEcTRe4t7tsEvFiFVlXFuI4d4KOuT0l2/3Y9hBQazlGODFORUkF8iIMvP+wCZ0sBjWimfeZ9rYGOvTvMcPzk4EHdzDbZKclXHrDC1GKyJYc5IYVCj88MEm/sqM50ckQIUen7/w/V5MrLgMdaDVV7ZrHz2EhFe7kJdCcnDpHmTR1khr+uD2tHXtziqDc1TqcwsBvLl8A/KAO+w2Fwu7T0mDfH4b9+16JKz7GO8QjjDEDEBw3Q3JHj806Hi3b X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: UtqtyiIT599xUT4RDXy1dsN4rROYA7NTXMIPeufoU/XDeulYiU72WQDuOsegHhqlU2rHHCZFc3TzzNUO+WX6dR8pyrtWH45a77zdG8gtsXIUMYQ3WNuCtDnUiQ5CRtVJh/cJlC5gGF2wdmWF4lGXijSNq7gequTCX+qRJljncz09AhTQTy9WL1fB9XhtUkgECq4jwlZCK6kaeEJmPFEkRZEd+oqq5RRXk1hobX/8muPa0+0qno1p/eYKdJo9TF7IKkATjp07bxAeqpiWpoSDZDDEgXRNH1P42FlTyYxOCUgMu1xt3nS3sS9iIn6lPxp0UxASXqsPaaviiCMzOxcTWfB1YDT6NXGGn+q8TH5D+o0HuWmARWJeLHyilH+EG+Ts9nDPM8n4KthUg00ZjWJXYYm6D9HUy1JG3MgahxS8R1BO0Vjq0Tct7b2ckfsUu6UD8qnHib9qxQjCL8hoiA2A346os15SUY1dn0Ov2kYitJpzYCo/13JKduUf/KFtFR0fUnmnnwLXSk6zaiXoDesAhYA2iahuZlq70p5eNTB/T/uvkN3Bf4pQmDhSg9Gkc6SlW0ldIixeQ0/4WUMW4D/6ucz1SE6jiXP6CrBUbNleKGI= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 53e54581-dc00-4142-4fab-08dc500a4aa3 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Mar 2024 16:07:11.3524 (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: CLKOV15mjJACVsNbINbbxMI3W4S+4biyWLR2ey+qpf+/fztzFrMalyo2ggjTWUHmDCxZj9X6UGfQdQx3NIwYuA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4638 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-29_13,2024-03-28_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 suspectscore=0 adultscore=0 mlxscore=0 spamscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403290142 X-Proofpoint-GUID: WUdOtpl0FW4jnOSR8zrA2QShKnlihi8q X-Proofpoint-ORIG-GUID: WUdOtpl0FW4jnOSR8zrA2QShKnlihi8q 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+patchwork=sourceware.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. 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/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 | 3 +- gcc/doc/extend.texi | 68 ++++++++++++++++ .../gcc.dg/flex-array-counted-by-7.c | 8 ++ gcc/testsuite/gcc.dg/flex-array-counted-by.c | 62 +++++++++++++++ 9 files changed, 282 insertions(+), 20 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.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..c7c23edc4840 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -776,6 +776,7 @@ extern struct c_expr convert_lvalue_to_rvalue (location_t, struct c_expr, extern tree decl_constant_value_1 (tree, bool); extern void mark_exp_read (tree); extern tree composite_type (tree, tree); +extern tree lookup_field (tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t, location_t); extern tree build_array_ref (location_t, tree, tree); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index ddeab1e2a8a1..cead0a055068 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -101,7 +101,6 @@ static bool function_types_compatible_p (const_tree, const_tree, struct comptypes_data *); static bool type_lists_compatible_p (const_tree, const_tree, struct comptypes_data *); -static tree lookup_field (tree, tree); static int convert_arguments (location_t, vec, tree, vec *, vec *, tree, tree); @@ -2375,7 +2374,7 @@ default_conversion (tree exp) the component is embedded within (nested) anonymous structures or unions, the list steps down the chain to the component. */ -static tree +tree lookup_field (tree type, tree component) { tree field; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2b8ba1949bf1..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.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 Mar 29 16:07:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 87827 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 91D523857C4F for ; Fri, 29 Mar 2024 16:09:02 +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 53D0C3858D32 for ; Fri, 29 Mar 2024 16:07:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53D0C3858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 53D0C3858D32 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=1711728444; cv=pass; b=gBpRxV+qGIlRopnbtbWMj4eIyAKsKUSyWLSYVti3k9A9q/oRwHvj/dSmCgUTnG1xZk4RoNNWld8gIEfx5imh16xoYQcEqefujuChwtMv6nqUYtOwXPHzHbbaFiuD8Ej8g1gMt8f5IZN5CymrNHyjf9VVb35DKLXM2BK5a+WnHi8= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1711728444; c=relaxed/simple; bh=zpIEo5UyVhm2VdnVflkY0u9jwsY/b6zApHStne2OPMM=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=DWiWH1Q4kxU5GTW/WaiymijimT1MDve9BNOCFRYviNjecKAWKD+PB2BkKx48MUSrGrCho8Onv4VH6mWuIJtos6mWOyiGB44P0ZUHmzbmfGnz6qMHj1S0Cj4z8L2TKuoO3M74/TUC0NW6fJ2zPLJRkSM1lYdFaBouvfTeWxIQSAc= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42TFOL7J018649; Fri, 29 Mar 2024 16:07:19 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=nYSX+NXEA1AM0aJqwyfMhf/uW6kqwdBJc02Dg+omPTo=; b=igMbg3fer5T4iJF/qB6zovyVq57edRpOIqvSjYZH3bWRGYmub06CzWkNaaye7muY4VCx lnCuPGtpc9q9WoLEiRRN9qomcnzmh4ZrmZaQhTbKVQUzNTaQtZe3zdf9rxz9jZyu6yAu yC8QQk+2XibZtyzpdZEzon+vs/fZT3EYBwfZkyQxmUjI4Vy+caV7AmfE5pvDcsxS71XY ez/tDCntSEZMDwqPa5LJNX7PykZKt21HCZZpPqqts+Qj590Xjqa3VaTULcleCplHve78 QLtygYQ3D5/wsRcGhZg6LKx7lQkOj0LHWBT3+vp6OkDGM64jRuwl5qj8iFjQcg9Bclay Fw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3x28ctb8uw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:19 +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 42TEkBLQ008903; Fri, 29 Mar 2024 16:07:18 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2041.outbound.protection.outlook.com [104.47.74.41]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3x1nhbw9e4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fEA1vDNeWfDbwVaUmZKe4aNZ7Z0tPurr89/mBFkAK8VJB5vA4N+HzayjG6v4OrSkkqkaTK1XPoJaA8j/5Zs5KeavppS17kSxRZnaD2L53vsFdwTF1eMAePXyCRs5xO3MVEZOaMaNhQSQAB0hJc/2HUh1dQk6NrIOujL2MpNTL8sThWO/SXwJaZmqQoG3TjEeLmEg6NYWV7wBUdSDEs7omBwGBO0f0SNDrr1lV1Qa7BPW/LD2Kfr9tppoB3j/LEsZXtzlnEkmMEkkrXSQswY3/5HL8nh+wrbpK14Vv2Lc+dZbEr42DpvZI7XQevmUFl1gUj1TLrX3OU5AEWU1R8N6PA== 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=nYSX+NXEA1AM0aJqwyfMhf/uW6kqwdBJc02Dg+omPTo=; b=SHrfgoCYjgBRgJOBEP+fK3b9NwjVrd+RGPcjzBeKvPlEA4tzCsBbSLSsdLQLhbcDVfUHEjTPBBbkGVWFz/zHcs12urR2KI8qN1XU3BK8gE6uFjyvyCAKO6TZA+25H8tvAtA2umBuaf7g6R56MSyQ9eod/ZU9+BGZNkdKROl3FSg/6yPUlZldvVhvGeBYJ1onTtJMRInUOaShsPHHQ+37Pq3tAl/w19a2l6EWi9tst8NmC/rGAc9p09CIHXfBmBP0egzG1FxQiu8wk5PprEHGxscWvAtjZRG6FVxawoG3DkCMvdZAMO63YdO53ZieNoLQUAuXGKXV7wjM0hQahMXodg== 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=nYSX+NXEA1AM0aJqwyfMhf/uW6kqwdBJc02Dg+omPTo=; b=sNHakM4Ukvq3gKo3gqDMy3WT2X8lpXvFA6+ormzDevl0l5qwW1QooqYmHh9yeopoaA9DXvyQ7xUi7xB6pTFgBaxI2yHDgaByqudHji3y2Ool9zEFlDdgiG0f9LtIhR6sSHV3iTv++2emaCGeyRgarjZrrgjl9S/VS7L0p0uJSkQ= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SJ0PR10MB4638.namprd10.prod.outlook.com (2603:10b6:a03:2d8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.31; Fri, 29 Mar 2024 16:07:15 +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.039; Fri, 29 Mar 2024 16:07:15 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v8 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE. Date: Fri, 29 Mar 2024 16:07:00 +0000 Message-Id: <20240329160703.4012941-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240329160703.4012941-1-qing.zhao@oracle.com> References: <20240329160703.4012941-1-qing.zhao@oracle.com> X-ClientProxiedBy: BY5PR04CA0019.namprd04.prod.outlook.com (2603:10b6:a03:1d0::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_|SJ0PR10MB4638:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cWrYaS/pQVaU6MPYDE+aWzdhlq8ci/U+QrfCSfcScjQ66VzgGpkQxtwIhLTwGoVBZT5bebSgcgHL/sFbdUGdqrswY4BcdOJSaygXfU3z4U9va/SZdOLPi63OBNtcbPahA8hwZSF8q41o9Ne3qpySbmLr+kWcI9LuDRi0bjGYV1GZwS/IbFakBiUW3tJTGRez0AoyDu9Uk6CMzjQtQwvkiT+XAK3ye/h2a9SrFcay0yEKOLHJY2DIiAseorQfSu2Q5tK2K0Hg8N1k1tgQC65Shw5KezBrJpJhg2pHPGSA9F51M8r7gmBAl3YOL9iEjLTKNrVI+T/qBJV3pNCsPSoBTlvOhtAjsT0u+hy9JsbDR0ow/suiJ/W48Ot142DhhxV1rLOvSbA5xxVfaxXFGG3wsKZ/G6XE4gAfvLPTmLGWMO2yd7qgAhSbvPVy8Uhr+y8KUWcQ1Jc75RtHQ9CoEV+PlY4y6mqPIZvNp4WC0eQoKG/qcFg/IPDI8BNS8yJi35aI7CD9cNO1ZOkcycUlbo7tdvWzlOtch16E/hANpvTevfcF+iC+7wuXaXp2skVdbCFNUNVT/APSowpRJxvlSolTVb5qiHpHh+VAzdtfC3uBceU= 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:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: N0FVsV+qqPI4Qf50DQ50HSUVy4SR5ws47X1HzQhefT9MSiCTOXDVUFy02fu50w0tA49j+bRKIMLmGWe6JAiiGhsd6P++NrJAGb2qIMglpu4Y+M1iskIg2BXvucC0qsZEVc7W62axr1X0bw0d7c/rPPoCHzi9tZbMwc2DLA5MsNRTofuW0gJJyRAXkN5LYTKok0/yX71KPemHwfdALYjCg12eB8rXvzxAlt8+uLJ4iJaqYh08fB/JHMga9rfwX3ovBOAgqA1o3cYOtaf/uWia/wXs9L+FB7crR6Zb8lvpltvz1KR84yn1EUpPRm5qRvff6D4Zy1nT5XbOINDCZGAmZk/xgCjAKyqcu+2P5oPYMFFAgrP+zoykkqJSuztEUWErKzs5JqiKwSZiFvF03M26Nx/OkwRf7T8UoMLVLKv5/0YrBehqNsF1ZbzQk6t0xHRGNW5m9TTxpmt6q40jl1rCKMr1KivGIyhQA0kSz3SYWaUXkTViUZvi4kizhgI+uNAHHoWikh4reQ7QQRJhSyYpBRA8zofl/wrdzVnV616semG8ghHdv+P/RJ0lv7J1QZJE2fWhBdswImTZWsqhzN+XgQlJmGAUxxMBJKuarp+f0LmE5Og0zt4h8LjlebpBJ8c2YHhW3TptlfJZdzZUEp1ZljhAECLXRPiQ3X0QxOA7XPGbyPbr0ySNrxj74oHaynoadSIc1b7F/Y0SV9TcMHOm6/L1EfK0uXZ2qOWBTNH4YtDvm8Hvdfe4XNO0DHIV8XFlkaCHKW8e+pB6qPJgZfmwd4c2LHL7qfRl/tRvVW9ms7LZJWSyOTccUGMgvqHSUOzSIwAwm3+8teKQ+bsHLgwxrG2nf3SjVmuSeccCAPRQeAXHapNSe6EtAX6ieHthaIGDQ1lPrIrT7/76MO5RbeOz98bSeu6ygHwGlK53wMej1lfvf1VYh4rBSy/v0kmyBL+ySA8Lxfs2CfwSXIYnM8Y1/rjdn3s1io4tNXMntZ6ZmLwkgepfc/v3z0lrPQtvo8dE/O4ITZReLBhb6EEbXkLSod6Q7u9o7Jwbbcww1PBQzQXxGiKW2RLeok4Z8OLBpywXPhPCc/+Rwp3XOxtxasjk5D+/WqxzUWm3JXFGHzzB7nXm1Lk3ELIAahhJ/WwxfrLAMGvT2RiXnlFprYuCYBZetv7lZx0bh2DeYf8kYfPkLZnxwhYZhCybx+iiW0GGE3DgzGX+F/6S0bKlqDAu3njptHBXLNF+lsgw9yZqXUPHTRMOwoLR7I7jhtgcoNcsjuEBrm4iJy1AXygNBLbcSjTbb8ze5+RcpYn7iFnpEnTB9hpa98TwLvIk+G5f1//xFTcSnXC9rufwtnYPEE4XsomJJ4sap0i7mHNwcstJmeapHIXJ7r1rzTtR/PRd1xKNjQREdvLJdxcADy2y6fW3/UOCXdBM6DXzcpylXoUYUAmZJ0Yad9aeA4s5JTZLr/aMlzpGdcb8GK/q4YahX2rglbnlVlKMoqOF3F2f5tqwi5yEG3N93cwWpgrUlJPiMnjZWpILUhtJWaWvwDKdNhTzLKFyFrp8Vebgju2gdcVtASeML1HTtpmn74GV4JDMEdsA+10/ X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: zKVsqv7PDc8CwXkFCO1xTCuldFywGbS9ZzKbcLpkoXppS4SPiQMPZiwey68KSZjkEieKKegATe5dlyMkL2xp+UQXplZnuq7ShzbBxCRpol/Df2SmTVZW99NZGNJsHz04+NOufKZ1xzMmvl73yh3ptpVmTIaXhJGJo+waiT3GOCTzBLzuskQT126TL1BI4PZkuO8mXTNnYiJHkr5WEudFimlfQQmzMIoKBRMgvugcWKzBFJxyRXKVnU/FcTF0QhkaiwhgCazSnXdYonXPdzYAH9aFOzz4+hBMpDVtHKsHiEAU3LFtmnDVU2sC+AnoWRxMWyzuuWpD3N8L/x2YNCKGO3P9mngWLrPBIhbzEA6eqMTG2lhYFi1PNvTEwxn6ETqYsA8kZE31CSg7wKtXweXlG8jSzDTeZLZodYi3OsPwzlYLQnltMAZS37AxTMJOdwau/+/6QW5nwiZNIg6pgJ+dCaNdVMnpoZb0HTiPtUN/OD3DudWAAOAbR0QSNVGrnJZOXHCE1CYyT7aeBjtXUIWovS4M+HdqlZO19rFKaQ6nOjZJ8dQjdyr1cef55mGzwBgtSiZ+oUU68I0kiRXNRvaPHZxLtQYi6qqvZWeOlmaxrAQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e7e4c308-f41f-4e60-a516-08dc500a4d1d X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Mar 2024 16:07:15.5107 (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: czitwIkFWXbRtyiBmFy1Y6TvaXiil4/HwW1Coe61VTxCJYhsR4ZoY79kxHEMbqrJjcahXuLgzZapRwmM6SlFKA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4638 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-29_13,2024-03-28_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 bulkscore=0 spamscore=0 adultscore=0 mlxscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403290142 X-Proofpoint-GUID: QGos2LajYZo0Nwktg8IF9uEeWAgi1S3s X-Proofpoint-ORIG-GUID: QGos2LajYZo0Nwktg8IF9uEeWAgi1S3s 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+patchwork=sourceware.org@gcc.gnu.org Including the following changes: * The definition of the new internal function .ACCESS_WITH_SIZE in internal-fn.def. * C FE converts every reference to a FAM with a "counted_by" attribute to a call to the internal function .ACCESS_WITH_SIZE. (build_component_ref in c_typeck.cc) This includes the case when the object is statically allocated and initialized. In order to make this working, the routines initializer_constant_valid_p_1 and output_constant in varasm.cc are updated to handle calls to .ACCESS_WITH_SIZE. (initializer_constant_valid_p_1 and output_constant in varasm.c) However, for the reference inside "offsetof", the "counted_by" attribute is ignored since it's not useful at all. (c_parser_postfix_expression in c/c-parser.cc) In addtion to "offsetof", for the reference inside operator "typeof" and "alignof", we ignore counted_by attribute too. When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE, replace the call with its first argument. * Convert every call to .ACCESS_WITH_SIZE to its first argument. (expand_ACCESS_WITH_SIZE in internal-fn.cc) * Adjust alias analysis to exclude the new internal from clobbering anything. (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in tree-ssa-alias.cc) * Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE when it's LHS is eliminated as dead code. (eliminate_unnecessary_stmts in tree-ssa-dce.cc) * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and get the reference from the call to .ACCESS_WITH_SIZE. (is_access_with_size_p and get_ref_from_access_with_size in tree.cc) gcc/c/ChangeLog: * c-parser.cc (c_parser_postfix_expression): Ignore the counted-by attribute when build_component_ref inside offsetof operator. * c-tree.h (build_component_ref): Add one more parameter. * c-typeck.cc (build_counted_by_ref): New function. (build_access_with_size_for_counted_by): New function. (build_component_ref): Check the counted-by attribute and build call to .ACCESS_WITH_SIZE. (build_unary_op): When building ADDR_EXPR for .ACCESS_WITH_SIZE, use its first argument. (lvalue_p): Accept call to .ACCESS_WITH_SIZE. gcc/ChangeLog: * internal-fn.cc (expand_ACCESS_WITH_SIZE): New function. * internal-fn.def (ACCESS_WITH_SIZE): New internal function. * tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case IFN_ACCESS_WITH_SIZE. (call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE. * tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call to .ACCESS_WITH_SIZE when its LHS is dead. * tree.cc (process_call_operands): Adjust side effect for function .ACCESS_WITH_SIZE. (is_access_with_size_p): New function. (get_ref_from_access_with_size): New function. * tree.h (is_access_with_size_p): New prototype. (get_ref_from_access_with_size): New prototype. * varasm.cc (initializer_constant_valid_p_1): Handle call to .ACCESS_WITH_SIZE. (output_constant): Handle call to .ACCESS_WITH_SIZE. gcc/testsuite/ChangeLog: * gcc.dg/flex-array-counted-by-2.c: New test. --- gcc/c/c-parser.cc | 10 +- gcc/c/c-tree.h | 2 +- gcc/c/c-typeck.cc | 128 +++++++++++++++++- gcc/internal-fn.cc | 35 +++++ gcc/internal-fn.def | 4 + .../gcc.dg/flex-array-counted-by-2.c | 112 +++++++++++++++ gcc/tree-ssa-alias.cc | 2 + gcc/tree-ssa-dce.cc | 5 +- gcc/tree.cc | 25 +++- gcc/tree.h | 8 ++ gcc/varasm.cc | 10 ++ 11 files changed, 331 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index c31349dae2ff..a6ed5ac43bb1 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -10850,9 +10850,12 @@ c_parser_postfix_expression (c_parser *parser) if (c_parser_next_token_is (parser, CPP_NAME)) { c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful at all. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, - comp_tok->location, UNKNOWN_LOCATION); + comp_tok->location, UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); while (c_parser_next_token_is (parser, CPP_DOT) || c_parser_next_token_is (parser, @@ -10879,11 +10882,14 @@ c_parser_postfix_expression (c_parser *parser) break; } c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, comp_tok->location, - UNKNOWN_LOCATION); + UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); } else diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index c7c23edc4840..59b69ca54f0f 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -778,7 +778,7 @@ extern void mark_exp_read (tree); extern tree composite_type (tree, tree); extern tree lookup_field (tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t, - location_t); + location_t, bool = true); extern tree build_array_ref (location_t, tree, tree); extern tree build_omp_array_section (location_t, tree, tree, tree); extern tree build_external_ref (location_t, tree, bool, tree *); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index cead0a055068..f7b0e08459b0 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2546,15 +2546,116 @@ should_suggest_deref_p (tree datum_type) return false; } +/* For a SUBDATUM field of a structure or union DATUM, generate a REF to + the object that represents its counted_by per the attribute counted_by + attached to this field if it's a flexible array member field, otherwise + return NULL_TREE. + set COUNTED_BY_TYPE to the TYPE of the counted_by field. + For example, if: + + struct P { + int k; + int x[] __attribute__ ((counted_by (k))); + } *p; + + for: + p->x + + the ref to the object that represents its element count will be: + + &(p->k) + +*/ +static tree +build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) +{ + tree type = TREE_TYPE (datum); + if (!(c_flexible_array_member_type_p (TREE_TYPE (subdatum)))) + return NULL_TREE; + + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (subdatum)); + tree counted_by_ref = NULL_TREE; + *counted_by_type = NULL_TREE; + if (attr_counted_by) + { + tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by)); + counted_by_ref + = build_component_ref (UNKNOWN_LOCATION, + datum, field_id, + UNKNOWN_LOCATION, UNKNOWN_LOCATION); + counted_by_ref = build_fold_addr_expr (counted_by_ref); + + /* Get the TYPE of the counted_by field. */ + tree counted_by_field = lookup_field (type, field_id); + gcc_assert (counted_by_field); + + do + { + *counted_by_type = TREE_TYPE (TREE_VALUE (counted_by_field)); + counted_by_field = TREE_CHAIN (counted_by_field); + } + while (counted_by_field); + } + return counted_by_ref; +} + +/* Given a COMPONENT_REF REF with the location LOC, the corresponding + COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate an INDIRECT_REF + to a call to the internal function .ACCESS_WITH_SIZE. + + REF + + to: + + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + + NOTE: The return type of this function is the POINTER type pointing + to the original flexible array type. + Then the type of the INDIRECT_REF is the original flexible array type. + + The type of the first argument of this function is a POINTER type + to the 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); @@ -2626,7 +2727,13 @@ build_component_ref (location_t loc, tree datum, tree component, int quals; tree subtype; bool use_datum_quals; - + tree counted_by_type = NULL_TREE; + /* Do not handle counted_by when in typeof and alignof operator. */ + handle_counted_by = handle_counted_by && !in_typeof && !in_alignof; + tree counted_by_ref = handle_counted_by + ? build_counted_by_ref (datum, subdatum, + &counted_by_type) + : NULL_TREE; if (TREE_TYPE (subdatum) == error_mark_node) return error_mark_node; @@ -2645,6 +2752,12 @@ build_component_ref (location_t loc, tree datum, tree component, ref = build3 (COMPONENT_REF, subtype, datum, subdatum, NULL_TREE); SET_EXPR_LOCATION (ref, loc); + + if (counted_by_ref) + ref = build_access_with_size_for_counted_by (loc, ref, + counted_by_ref, + counted_by_type); + if (TREE_READONLY (subdatum) || (use_datum_quals && TREE_READONLY (datum))) TREE_READONLY (ref) = 1; @@ -5048,7 +5161,11 @@ build_unary_op (location_t location, enum tree_code code, tree xarg, goto return_build_unary_op; } - /* Ordinary case; arg is a COMPONENT_REF or a decl. */ + /* Ordinary case; arg is a COMPONENT_REF or a decl, or a call to + .ACCESS_WITH_SIZE. */ + if (is_access_with_size_p (arg)) + arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0); + argtype = TREE_TYPE (arg); /* If the lvalue is const or volatile, merge that into the type @@ -5195,6 +5312,9 @@ lvalue_p (const_tree ref) case BIND_EXPR: return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; + case CALL_EXPR: + return is_access_with_size_p (ref); + default: return false; } diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index a07f25f3aee3..e744080ee670 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -3393,6 +3393,41 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) } } +/* Expand the IFN_ACCESS_WITH_SIZE function: + ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE, CLASS_OF_SIZE, + TYPE_OF_SIZE, ACCESS_MODE) + which returns the REF_TO_OBJ same as the 1st argument; + + 1st argument REF_TO_OBJ: The reference to the object; + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: the number of bytes. + 1: the number of the elements of the object type; + 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE + of the object referenced by REF_TO_SIZE + 5th argument ACCESS_MODE: + -1: Unknown access semantics + 0: none + 1: read_only + 2: write_only + 3: read_write + + Both the return type and the type of the first argument of this + function have been converted from the incomplete array type to + the corresponding pointer type. + + For each call to a .ACCESS_WITH_SIZE, replace it with its 1st argument. */ +static void +expand_ACCESS_WITH_SIZE (internal_fn, gcall *stmt) +{ + tree lhs = gimple_call_lhs (stmt); + tree ref_to_obj = gimple_call_arg (stmt, 0); + if (lhs) + expand_assignment (lhs, ref_to_obj, false); + else + emit_insn (expand_normal (ref_to_obj)); +} + /* The size of an OpenACC compute dimension. */ static void diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def index c14d30365c14..0801c8bfe61d 100644 --- a/gcc/internal-fn.def +++ b/gcc/internal-fn.def @@ -510,6 +510,10 @@ DEF_INTERNAL_FN (PHI, 0, NULL) automatic variable. */ DEF_INTERNAL_FN (DEFERRED_INIT, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL) +/* A function to associate the access size and access mode information + with the corresponding reference to an object. */ +DEF_INTERNAL_FN (ACCESS_WITH_SIZE, ECF_LEAF | ECF_NOTHROW, NULL) + /* DIM_SIZE and DIM_POS return the size of a particular compute dimension and the executing thread's position within that dimension. DIM_POS is pure (and not const) so that it isn't diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c new file mode 100644 index 000000000000..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 Mar 29 16:07:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 87828 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E79333858CDB for ; Fri, 29 Mar 2024 16:09:59 +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 D6C67385840D for ; Fri, 29 Mar 2024 16:07:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6C67385840D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D6C67385840D 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=1711728448; cv=pass; b=Hkw/OU4w9f23LPLWB7guPItpGX5WDfOUAiyc9z3/ovzoEZni19cbtYwOx0PL87dCQfhR1EnjseN8i8Ud2w1lP4HHny6DTKNELOQbgTv1ZzYeoQErUpK7Vvl0S55bU+W5cxTC4xgIRrXjv0NijmCS2/4hrGPrWU/BoDE8MZzpbJo= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1711728448; c=relaxed/simple; bh=k6laV2/S+lpsg60nksLhNFZR3HMSDSZace6OpIsLnys=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=SL9TfX2jgb+zFcuJMMwx5RA0Qa9/JYSK412z0no6jcqIMDVHPSxd6mejkXy0mOumfPVzfDR58vr8oucKnnt6eyVWxaJsw6C6yax2tZ1xGYbkv7Mmc6MNH0B15cKc6mYDQL466gIwnH9mzBSy58Chs+blV4eFPK/fxNYTN4hxvLY= 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 42TFNrwd016578; Fri, 29 Mar 2024 16:07:23 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=KlXtvfZR/XSIOrEk09MF06YQTyj+V2KsRfEmQL6/lo4QWWRm5bnZ5P45qMLRO6LVwRu9 6Mh2tzcInyAJ6qe9mt7tq13Vt0SZokGDOJuSeeEOtYtGX9yhWBM4j9tKDg+8snFM3gnh DCAiPCCGz/8Reuy11y3vEDeuLyIjHa/3DRB8GCpYKbYcF5kwR6yqBJj3qaqmm66QYwVv pdyMuK3NY6nRoYTlkz+xwkbJz/luk8zv7CfIQZSJrJFYHBnwC+JOU9K3dgXSBx4n2Vfl HZT499qF3YFzwdCswvAH+U2Ylni5BiCoXl3S91pqxabgN/P1vCdrMPk/DSbo+n+cw7Gs aw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3x1pybuu1j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:23 +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 42TFelmE008879; Fri, 29 Mar 2024 16:07:22 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2040.outbound.protection.outlook.com [104.47.74.40]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3x1nhbw9g2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NAiobqwhWOR6b1Wbyi7fqg0svH59tgNir16o/Hlhm7q5I9mY717KY7CTHs7KVHhuf9jQt1qH7+RGWY/yAxZFw7kNSJ54cu0MTkBWKl9V8/FLqw5ZagZ5pve7m+3NLGl0yJ13PBWnSjYXbdv5ZrO9pTAgVbDF8Ymbx2nA/3s4rpE8VEt3Hx+6JqG8ZKMRHde2ptA7y2rXcFKvr/WaaljDThclcNFIxTLT5qGqRRzfEi7p3Z1sdMBUxxkook25P9QE6xwTMOnLE9rJueclnLI73UrCBQh6g2c63GwfprDGn10GVWVkcnG1i7iub/Y/iqH1obnlCeVChoG7w//PkUOzhw== 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=DK1h6SzptlgvdQ8Y7Ew2Ml3PXi2OkeCh60qv97Sey1DpMjTpvNTWNwP9jCcQ54tgOnlSNy2cXW0NY8I62m4NGfnG0+K5AQ0Ix91AX9PlhsJa1pUrS9V6CBnqVdv2Qf9Q3jw6fScm/wbv9AZSvjZ68CvtBJwoCKRCrPK+9BSYmI6A2B/VW5HLylIt4knoqySGh+LC2ZTCw59sijwwx3y42FBjWWRIyLjU1Flsp02PR1+cih8Fr3ap4fozupUVm6hwn+aEbS/Fw78aSE1MsVYswLDxgYqtwuHoTOGLAPk0WhuhNKuUGKqwMAz+tJ4dQc+pWvs9StnXcpbuiojOhvwVmw== 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=hIOg/4FHnvWB46RMfzJV85cjZZiycEw7LcbkZjNk+4L4Xzlt7QnkU0v1fUD5iiAyJ1tgoMiN4oTcRXZRWljVxU6bi4n33yOjlcyDmDPazzZZJRPIzN3F1xhi9tRpmqE+MHkIrgK7+M006+teEgoPvqmiWljFxspodRo8LXI4Vwk= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SJ0PR10MB4638.namprd10.prod.outlook.com (2603:10b6:a03:2d8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.31; Fri, 29 Mar 2024 16:07:19 +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.039; Fri, 29 Mar 2024 16:07:19 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v8 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Fri, 29 Mar 2024 16:07:01 +0000 Message-Id: <20240329160703.4012941-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240329160703.4012941-1-qing.zhao@oracle.com> References: <20240329160703.4012941-1-qing.zhao@oracle.com> X-ClientProxiedBy: SJ0PR05CA0126.namprd05.prod.outlook.com (2603:10b6:a03:33d::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_|SJ0PR10MB4638:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: f46HeRYCW0sBHcTadIRKdEf2GSAiDB2JhuQfMDyGcPG1iBbgWoc/in8+PyOKqsmE9OgrrpX03E1hFDVj1kyNCXnJ9Jf5hN/ljfn2WnfgTZOVdouwiq3T0UxeV9m2T5QuDDrh1juk1ufSWuLSbmYig0oXgl3kUF9/rfJxNBsdnQRKBFvf27pSErFG4DpZSxdZf8N0sEbIPUSPwTCNBeerBeJSc4LP0PxPH4Ui4N4TjW7/k30TgeeMFoCawskW2g2+An+0COGA3iH3g0lwXoslNbwzh1pthd1WnaOmsFKIsPRTgE+CQX4YK7sXcl/lS9hjZS1D7MTq3EQbFgseo9A8LOey45gNtuIlDY6ihl9Ao6OYpuY7sCrykkQnexyOG+w0R+jcGDr/ccGfXHFTHrIj2zofPoN0xceDv0NpapphZDeX35dtnjB5ii2D5twJ+bGUvbcKvBInO7cZW4vNt8fpvNukUMxRR+JSLUHX8DbsX10lu/GNvvU/KAA9THKnZW2hbN+5RD8siH/YsoC27mxCorMiEVU2aWWQuV/C2Zw1wtdBOHlfQoFhKiEmkT5WoUYCT99HT5UgFzWlW3lMiPyLiUPsCZJ3hvs8nliaN9Js3C+a3TCVak6xk3pT1udeJMHT 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:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KxoLGIrQOrxcHMaQmgB7F2jGiw25SH9FyUSR6xq+1RCzubWcGeElM+wVzD+5w4vu4tU5VsHfSsGRK2Q09A3ESKhe8aNVtRnI8e3n/iFdTlp6mVWl8BRrAmnz33P5zOlnAcdeYDLVgCZCH7LqMt6BEw1Y27c21x0rEB5YYp+29ajGEO2pIoAxP7Hdz3o0qZ7+TNxb6syBk0e7k7UEM0FxiSZf4UnxVOMEQ4BydNCfc8g7RC4fkU0Lf7Uqn+kA19a+8Kr1w2QzHD6sbv/Xamq/osYzyOSrK9RlKsb95lSttdH/oPeCV9BVCV2qukF2TVFWMWb/X4cLeRcMSInefa67xd3gBZHjL+Js3woYEWm/IJWVUgsBOa45u9jHmZ7qyTd/jbSu0wHZnCv6mGOqGZn0JLtafDh54qu09+pwXCqvmrE4ZBPWQXAhXModrsR/HJ9WdalQJtM60oPGKUERJ47kEj532HPGQJcQ6jLGPkbGITNoybmSdZGTYj3/Rf2sIv4hve9ZNSMQjMZiULcj/AnvVxyA+wxWbxyrD5CD0vnlJoTNVBOCnFvgvbxVPaHe0ggRpYvy0huvAK1qQAJzTKpWATGv/GGB7nng1mo8mAdQh1C4ZZQGFyVbCU4kiOzs0j3rYPXo7EG7sP3mdFM9P3BaAmAIkzJ8k/2pW398RtKaImPdFvaj7uQhsyVwgENIta4QMAbt4i3NsMsAd96LvU8mO0U8ECtKAtpkjFELUBmEx7WN3gZArX5Jo4CuwKwAo3SpFB0C8JJZvX9pWe0Bl+PSQ8riIrXluAeTQQdLVzZTbZm81/ZI7Q/IlOdWuRw/ufU0S5q8U8LX7q9h6wke/A6RXlml9AekdWAZ63H6SIal0uUjvUYVKUxrfnt9s5vGauBeRuqkgoi+0BGRtuNB+qk+LeLSqTfbuhr8N5BHSNXIa5xjbgXxMxhRg83GkNyuch4BGakKx3l0licrRubBc9vxtRBN2se/7NDG1UWhlALc5YSCrcwq6W1NSwtKNMdNpgqSd5At+aGC499TlvCN6ygr/gLhGj8TQdEVfgMG6R1kPAUdhoVDgpuiHfbV1MpGMGrwyCylRN7EHQt8xk3IozMsuXq0jYph1DSeJ1HGZOkK8BeWI7SQXd7UX70Xb6T9kh1Kq0YF9blxO2lyOYoDCPz5BKU3kDmrwv734cQc8Ork1+un80F3I9mcUh+35azJVraxzRdwToR72eYDT8BOoRg3lgB59lcGIC8DTCTJJNbezujKmoxeNePXbYFusSL+HwRDYYyuDmPfTgRuvn3qmRn60Y3K/GVFq+/pT1wow4hQcVZPUAabBRGjXVcLvEuGpANhFBiFBv6MPkSnazUDPbhB6YF4cVkuvXHM46folT2zzdvknZGWGnVcojzbV7j353QFZkzVcZXwhwUdao4QbqMCltSWA+oI/sK7b/hljXL/1EkCOh5Y5BkS4cgq6/iNelFrQf9krrI7+IenX84uNogFn9C9JO5TIZoRw1zSl+Fj/7PlR5Ru1R4CWweKi4FNbVJA/kmPC6HVo5kTjuqqkeLaSIBYHsKhqLH+z4NpDWe75cOoNuZ/dyavVrpG7S+QoTgS X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: eohbdFo/wZy9rFI67h8LWSR3TEW4gA3OmiUTZx1jaUftp6fGDMxa+sRjJSrUXuo28o2De8R9gguUueXG4fIkd80hT6JL0CG8ImnvAfJwTeMm9qqHHaKuKEbSsJBNRU25+wXl4XKks+dsYqSiIKG/aZsO/Vnq2DDyO52OIqmz9uKpxtAJpwdkbuNfNOIEry1z/8mfeDxAVKkFcWA4FNueFuHV6hprs0wKvOpYLiUs/LQpHAO2v7c7w4uQyAxwTi586NuyuahkUBiS65TD3b4gGO51N+9H8qgn5lk8xmiHUztf1F7z/yvftt3zenzvs/WAAI4jDDMYSghX6eHSIGhBrjYxp2gUyOMP4lf0rk4XUus1HupP3+8HbO8KI0bFjKJ6ZFVs1rBVcgaGRJHIUVZL6vnRNpgqehAG0Gy052DMkzfLDFF+O96J2uxlXbGXo0kOzxItrnaS5N6Ht1wqefZUk9OZXPUpa9OCwllLCZydk6EulUuWxUZkDK3AAVUHiY8DA7R4Cv4AtEfw2g88ZlUTk8Igxr+ryQAdvv3QhQQxvfZw+tTEGgof5GSkRdI7pTTXbbdMBv3SoL5jdR6HZukx82AuSadWNtG/uqHe2Dl2IMY= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b43e0c25-a3f3-4d5a-43ec-08dc500a4fa8 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Mar 2024 16:07:19.7745 (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: fH8grsr5Qpfe3xR8AUJa/OMFFWFffrUP6tdz+KL3gSv5iW9XY+mTqlz7511UNPw/iwa8sEvJkKEO0CD6KVL92Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4638 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-29_13,2024-03-28_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 bulkscore=0 spamscore=0 adultscore=0 mlxscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403290142 X-Proofpoint-GUID: oHIN6RSuy8Ctf8ROIkWLVE1Y-GxqHKJ4 X-Proofpoint-ORIG-GUID: oHIN6RSuy8Ctf8ROIkWLVE1Y-GxqHKJ4 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, KAM_SHORT, 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+patchwork=sourceware.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 Mar 29 16:07:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 87829 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 80EF53858410 for ; Fri, 29 Mar 2024 16:10:56 +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 1788C385842D for ; Fri, 29 Mar 2024 16:07:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1788C385842D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1788C385842D 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=1711728452; cv=pass; b=K5566DZi0CHGwzS7B3lp7+KJOH+UmAJgvUt3J7Ft0RJ6pn1m0v8RzyS3gc8IryZJExFm0m6er2669hpQ0RXpxRJvBzh5fX0jStF+tBmCCJC4/J5Y4RML5WX3JJMcokn5AkVPKmaLJRRuIyyyiJPCQoFVjs0xppesP7WMERkZoT0= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1711728452; c=relaxed/simple; bh=VYUdRmfEB4cFFMIRemDI8zFmG/JE6raxBSU4mCyIkg0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=rOrpkmTk6nSrPviFpBDAWFeBnUkUpgZk+P5u3bmY0JtyZRKPJHpqlKDSnqvAie5rfyf5fyx9dvY+3NpFoRhnDT+2P+wPeDssVZjCnzaAmgDWeMbRkLa9fawEDJlTfPUocKL/yiMsqLtApyN3Fy0N+FIKJMj422pWSPwEhZ70jIc= 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 42TFNnWZ030325; Fri, 29 Mar 2024 16:07:26 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=TZKrsn7FfTg0n0hoIlHEIT6gJayVnYiXh93rj1aD06sxcijnFpL7QlqHZ5qAGfxvyWtH /8T1D706ST8MlqJDnp0VeEjIX72okSanTX7XHx8/LdMTyQ77At8C3c5oyqt/fRFbFLI9 wBrObUxSDWaoIlfsNQ158R0dORbS/4wTLGkPutWRpkLx42br4K71x+1esni2QuE6Jt38 //w9NPdfeOBDe4myS4mn8rnN2bZQmgydv9uJqdcl8weOUp97y6dpqRmlfi5Xws8CfDEu bGHqs8z1djD2LUOKT3biQmvZF8LrGlgHDJOiGMLhGd8O7S6KL1kiJVBEa/Q4/IYsHHtf oA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3x1nv4bwy8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:25 +0000 Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 42TEg9gK019129; Fri, 29 Mar 2024 16:07:25 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2040.outbound.protection.outlook.com [104.47.74.40]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3x1nhhqv7a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MQYn0IB7ZvEtYioK7QdzCp7bC07jECLy3pCC4c9JkWcDX+MrLaZQBlk0JVPoZkWIRfd++lTiuz1z9y8i/tfmzX27ifCWuvTUKvhJkQ7zvbL2qcgFoeiejuXJKDa6pQfHQ5SKFjnwCRQYjDCZVvQZv0R5+DExI5bJMgupmQxhww61tWpj2UqjRH0GntTVZeEPBp+K6m0ihkLT0TLa+LsgcSKaitXFHopZ3zx0+HcKA4hQubONEzOd83530NLIWmjxgcFPOq55lt06w0TGJBsMuoYDyYfs7hshWVI+mHU1QypvtmCiyoJNXJTQv3/+oVF7OGUPnlVvzQ/9rKlGOyOeQw== 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=YgELT5+XVvirTWH3bdtnFXXYHhHTYdoIMGqnVTHCHa/NBZSjffDIcQQ97wvJK1pnPjXCQfszUvZJzwUhDWR0Xdk6b9b0T4GUOaRaHkG1oyASjdAHt1VzoPjjfSkGGx58isq8AyJJfz8cbe42nOP3EVyu7ZbeRzd9AEqZWkE6Bc6+dcUa7VcXyGYkrnfWQaJ1Qu92Yez1Y4K36rTWDsI4usaKRE7R4ur7Cx9HewsH7eCTT1dV0XD0xPEEtvU4omq/LXkt61aEFZK/N2K2hhwcIWIFheKEyY3goVlYLou6tDWZFU4N6MUbyiorGNVxDQAPVbKrd7OwVJRL2BBok5LktQ== 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=ZuYxww1UPUKRRiE+xEK43MSPGiSXpQ4HXPbtgKX22GL9iIMkc+ZvM8UBLv1Y30nAdVQPtveK2X7vbY1SD5HBQhXhgk957ztaRXWqRHpU6QnFLNzg/hWjp28q/dZAohWeb/PubdCBbEUNJt98mAi0k4GUPXbSSVZ2AOFtjQsJ4BY= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SJ0PR10MB4638.namprd10.prod.outlook.com (2603:10b6:a03:2d8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.31; Fri, 29 Mar 2024 16:07:23 +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.039; Fri, 29 Mar 2024 16:07:23 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v8 4/5] Use the .ACCESS_WITH_SIZE in bound sanitizer. Date: Fri, 29 Mar 2024 16:07:02 +0000 Message-Id: <20240329160703.4012941-5-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240329160703.4012941-1-qing.zhao@oracle.com> References: <20240329160703.4012941-1-qing.zhao@oracle.com> X-ClientProxiedBy: SJ0PR05CA0122.namprd05.prod.outlook.com (2603:10b6:a03:33d::7) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|SJ0PR10MB4638:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iHKratZ+ll6DAZAlKPQs1ds45ejfiHU3OCUU1vn/4NTzKpjNYFh/DJytNIiUYCVWIoSZuThriK8yooMqEhzsxg2X8ZUB7uVqKTLpCtDQhfe5WNhKv2XG2cePh2DA4AGICnXvwQGrDO3s0Kddl1TmKOGPSutJwE/TaOk6cFPOttJd+48WFASFmebCK27prHNJoElb1j/iBjdEd6r0U8PtTAs1nk5ArcvOKXcDTJGhAA7UwatNnU032+fqGJO3s53W7GPPO+zpHV/+iYNL3g1KM8K7wiG8RK4lTvZBK/nnzAn7yivDMI/TflX5gmW98Ifch4zYQSA8nMBmnO09re7WxgIr0TE1rODwpdqWSteqNSQSPydbSJ1aF3UmzQLOk34etx56VA3AGM7ot44jCuC14D3YbKzMsS6zPtTY4A7Kl6/KeZRX1/h2y5ippoNJY03Uz0ndZEHrnwXlej+RjVTaqYqmwwJrbwGGVp67mzuZImfLPRpjl50UZ0HufoN7/fWjYjGRznHusgrAunO/gtmngUvaUJBhjkLXrskJx1QkGvI7NAO8cGZ7GcEtc9LqMQKOfQbG7hECutL88Wn8QhJrzEYQAVN1kJQ5vI/0wLNpLhWnWPUvvJ+y9DkNN/KJMzbH 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:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: V82pc1aTEsquD+Lx+sQGBo1egr64iQdmPAdHva4V+oXbiRnG5iYenujMRXt0jfUIjhJ++yeT8KgLUdekEusOKu5EbyWAiVlJs9jXFmAgKcNKi+QuN1VcJNuZ6q2YXM9BlblLPWuFmy3kZwQ05UNuQzYwfQ6QyZisSZsUowBtQ1ANy0ecDLpU2zlVSDJ4TlydyeF4xK00CsPitSBgofVtBlPl2YG27DMXkTaDMe3wnw3Ewzes5vWD0RU5Dk+EfD8x6dYNmfqSM6opNHrR1q54AB/Sh/ZfDz15hlLfGdKzFKr4TcoQ0HfwGUGKgKGlC+D3H9NMopOPvbvOmwy+XIVzMFrF17IKqQEzM9kuO6tpgZfNIE7cKf7+9FbH+O9KJJY8LCpxzkZiBKN0tWskxNGhy+KWQFW/R9LKQk5y3ir8IUnRGq8iI3ewEIOHQcKmDUnA2BujXCUUlORs+XXRWesvfLAwJA1wvRoPj5pWnlqhNZ1gHSK+XzqfyVv+Ggz7KQ25Ysr9hIpXDdRoJeU/R3xq6MQA5Lew6Qdu/KwjRX9tTCnROh5rOSYA1MTLo0OdX8SHDnnFi4JJ1r/kxvLLIgww4OuPIvoTDNHHGl9n9wr69vR5+hDm7nq65aOfsOHzVgYzDceOuKx1vrshN+a0kXr/1EIVJgT4cLs14Y26pEcGHlzeGKkwOX+t4NKfBFj7kD0unGbAhS/smyqslFQa5KhPwKVi8d5OPh5/6UDZF35E8XxIP5iX+5tPMWk00BnzGoiqOQ0pQIokEuKZ5ruMz8eK6F8+6lJN78vQB28xiaS095XCpIW6LGZe68HNjP/VbRmtG5txPVWrWidREKI3rRjzconoob4LYsda/9UCXAaw5cUtFfuADkLHidplbD/nhnRt7lezwJBBo5ZOVBUhG9Asyu2g3PYrZB0jSkCCfFxG3GAtOlnkG5/Gmej7z+9V8TiC7uK9lvOuO7DrILg3qpGcBLy4gkGq/okkrXFLmAWOA9FabxT9/rlq8qEELH204/o6vr6QPAOWvVVIBsUAp/X+L5eh0R1BW4I+jdjDtHsYmE1nqNDg8oV7Kup02t86EBUFcNccpAdjCa1dap6Y1rIlBcudgpRwsuEH3Lj6CWg6A0FIteV2Bhat+ICoFsWeILcA09MCrINSMA5e3Rv1D/RIG9Si8gYU2UcuTvpitmZXj6jNK6D/HhsoJK2rIJHQrbNKyrPiX9DJeYSUJo4JU7rFjfvis9UqNY4Fl6/6jDA25mjuu4jBkzIzLAEBPu/hoECO7p2QFtalssC1nZru452Os8k0cgBbIMhoz+rrlIXMRhM017KCvPuml74XHfuOXdm2CsY+Clbwp35NT5OyB0HqX547S9zObbR6QRIzodIfU1ut7MoWJFr6cFq+AAtiBSo8gX7ZzCHzwWWKVlI4N2Zge5ERj/VsDCt3r8sJupqbS6YqKiiFOHwb4D4icsEIyAB/54/HZbX0dFJDdbvygwc5pE7k5tsE8QgB8OL9XvkJzTTsOaDDVqxtV0J16cEg6dRF8Yivq5fZqW5pM3RUgnd+rUZmN8n26QlDVG0hRIzzK/+RpqL+TqPW/jl7vKHlEXvl X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: hPbdY+nU2PIZ4ThTCsi6STQUmuz3x/rkV6Lv7D2ocCjBWzuaKto6updJN054EqGubBfH2TH7ToppnMjCnz3w1vXqan8fpuwGXhBuzPjkO4/eTRB0Zdo2SzMUYr/MDUoi74UmnA/d/GOWf5ygFxjmQm66z8MwV23eJmIxVWz06NV+gtHu7QPW8FWfXlAIFQpBXHYNXZqlUn3hvwyZ4f4Sg/M66HfFCq1f6vWXyDotYOzW7CdONoaj9hYOJLKRakDLeaBvphROS/oADSpCSXoN/w5pJp5vyhowKA4+ad8pob04rWZpOw64l3+2+yh9BQE8e4SO4K/iHRiBbHy0zjUJqNebHxTC70atZ3lSSvKu0GfagXdfJAAuvqO4nRnicOtEO2QzJdxHR17/TmSIs6Lro4NShi7/xbJayRuGEvBm7byA+JWz3XXhGLRFaip9XhAOzdAdgQA7ZkaWRBAzZxsc4ZSkWDN3AfdmTs0UbQsv0mARUsDn+bbmp/+VlMtpYbxCJnGFdX4FH0V6CuhCCRn1FfJmS8FocZ0+etAhz0GWomzmkEDxK890ayAR9QLjmbymNnuYieQFNG85SsB47VWkIKcNe8zf7pZ2eihhKVrwjow= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 55f192e9-99d2-483f-9008-08dc500a51c4 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Mar 2024 16:07:23.3052 (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: pZk0tclcNJiP7hA2iZxR+0IGUG69MiT0x7yGDY026/fZJoaXwYY/GfMA4jOcsdOGDSDZkdm2ftm5xnWBTIEHGA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4638 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-29_13,2024-03-28_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 bulkscore=0 adultscore=0 mlxscore=0 phishscore=0 malwarescore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403290142 X-Proofpoint-ORIG-GUID: kiLDz98DmdW7Xqj9ZIUARREjA9OVtIN9 X-Proofpoint-GUID: kiLDz98DmdW7Xqj9ZIUARREjA9OVtIN9 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+patchwork=sourceware.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 Mar 29 16:07:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 87826 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 622FE385840D for ; Fri, 29 Mar 2024 16:09:00 +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 F06943858403 for ; Fri, 29 Mar 2024 16:07:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F06943858403 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F06943858403 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=1711728454; cv=pass; b=Wx9s+3cPleftsqUUf6XcSHy/Ny93oZCAnafoRqmXV8Uz2/wKpjOtXCMtj+RVXTnyAnzkcqfmD+d0FPnYhmJneE7I56s+ttV8iXcbEMrc7HOZX0QZdEFweOsWk1tnxTNOY4J2ECqgur6L/K/hvGMkLjO99TEoiyTtt0dSCw53h6E= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1711728454; c=relaxed/simple; bh=7bjX6iVvqWpVSBih/7auveEI4o0ttRTupMLEATcbYd0=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=ZK4ShkHirL1f+c5GTpD+iJa8rUf+/YTp6W4iCgN1zC2t30tjXvPSP6wXWp2tYnf1ITRGYwqa5fWkTHGK1bQgXFOvlV0g4D6mDLY4drWvOFE21OGyZd+mVKvkRQRDZxnk2mbAkrUg3p7rBLQFtIPNcIgwZoG0jofN7zQg2CpeIBs= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42TFO7vZ029883; Fri, 29 Mar 2024 16:07:30 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=AkqzJzCoZvTkFH1G/lDg7xg8Zn5aeK7WBqKf4Gj/PRs=; b=Yacy8o4rV/cgDDgDC23pW3edU0ICM6v1qdQozJTQ4hi0jhEc/x88p8eppeMTzq0zuod1 S+5LgcnhhHCmRquUAvU2NHh91n72oDmb0EjNFe3C5V4rm84GzK/6vguaEhoZguYU7U/+ wPeoOPYLM4yKwmT2LRsJYuKpy2TIZOMKgeEy1IT8UOrW6JrAiAbZdAOcheJuEMGsNx3j qjqSJ2zjx7xUDII6p5qQv7V+ajDRw9rPzl9M+ns49Ytl7/218FhoZuKOqZqwb9ef2n10 xGR9DzPAiXuoncXGKcUyb//qCOqBEho4v3Ty5fndkptI7bmZhvcnk/yWQgA4rpsHl+HN hg== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3x2s9h2e5u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:29 +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 42TG3AcR018111; Fri, 29 Mar 2024 16:07:29 GMT Received: from nam04-bn8-obe.outbound.protection.outlook.com (mail-bn8nam04lp2041.outbound.protection.outlook.com [104.47.74.41]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3x1nhbn26f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 29 Mar 2024 16:07:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N21U3I3SQv3pKI/K3MG0Jbio3UYPOymK9PgjEUZMjW4nHoOPOmpq+wDzCRjrCYvyUqJkqEyqoufvb6OPf5BkjWyyTVQbMPONIPDYgg4Ii/XZCWFC4j+KKGvNEdetT4nqs6dtxnaWONGAzLhU8CATKhskGRx49TbFbn+ryVChDfrHCheZFcVfbrbHJpuOQBFfGJc7zIkQYRnBzTKTcX3Af3eUCSUUbm/l0clZYK1cgxr7i1cthVgzV2wBgpQqxVpAaFPAIiZoXQVUXCXcP/XAOvFnoPwBJUGNyDENoKJVmtR/4H0MCZZZFnmUYszooYyhnXgIhp0c2PRwfGXOE8gXLg== 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=AkqzJzCoZvTkFH1G/lDg7xg8Zn5aeK7WBqKf4Gj/PRs=; b=kMe94bSg8VySOnsW1IF0pMZfT+dDxyPxdLGfH4YqyEIeu+SbLdd1VxbHKXztp1wLPEtT4xCaK5ZhFuoKjMk6TeNdfKCkOPrepHKMoW0r4ZWW6qhHOoaLQXmG4Ep3cXwWtIas3Rs56jMwevVeLq4r3dx1KsUlKBCpPJStSw9OmDXBxJhJU6uUcfXDS8By21Auq5xBhym33pCdBdo+eGx4N3W07CKK/XZk+xYicS4/yqdhNuJ101xDug2gYsiQcR6btA6xG7D33L93HrPEg1fBN+QyXzLvXIsCLR91/cajMPa3AkUfcbzTL8jzvfGi6muUEPS/Wm4UB7Pb/ZXatbYF6g== 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=AkqzJzCoZvTkFH1G/lDg7xg8Zn5aeK7WBqKf4Gj/PRs=; b=fOuIk+UmZS7m+4crRiIUgrnu7WXf/xvLODR/mm8Xk7uTwEVvpBr/DdCxTe1NhzfVp2Nbdhzy8iLASHQFu+2oO6iblp/hZqu9PHPHusCFHWP/r5WUVx7b3DSk+3cvXc/pnp1TofcKtoM8S58ITS6aybrV3/3Y/J6x5L4Os/7dWy0= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by SJ0PR10MB4638.namprd10.prod.outlook.com (2603:10b6:a03:2d8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.31; Fri, 29 Mar 2024 16:07:26 +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.039; Fri, 29 Mar 2024 16:07:26 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v8 5/5] Add the 6th argument to .ACCESS_WITH_SIZE Date: Fri, 29 Mar 2024 16:07:03 +0000 Message-Id: <20240329160703.4012941-6-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240329160703.4012941-1-qing.zhao@oracle.com> References: <20240329160703.4012941-1-qing.zhao@oracle.com> X-ClientProxiedBy: BY5PR17CA0041.namprd17.prod.outlook.com (2603:10b6:a03:167::18) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|SJ0PR10MB4638:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4Ih2MA2eEtAjRl62M2WEVNqfweTldMH/KLouH7OEXpzqYEZ2AJn2aw0UqbMlFAII/wFngb1qsUjC7M9kQHUMCnYYwRyx6p4z9qGkeAIORD70sFjudMI23ub8Nqg5b2QstQEInfIKNKZKbQImKg9u7+DsAWGvNTQfT3z0Y3yAl36rJCLCIVko71LFI/nueq4g6urtp1y6BP0LzPDBTMhtyEFdjoO/DcgF196/unKQvbj71j9/P//fVnI4a82dGwFnbvyREjXEI07+VWAj7/4P4Y0PQoHWvTZVrw8+YkLJlm+lioMg2vQJDWjumR2s5PUks3XQNcOCBlPfI9QmkRNvNjWnKpOYTqGanH5+3u3gOV8TCO3ZlDyGPVxo8cSe0XPeBQVkv7WTkupyJpAZA5rqg3/Exg9oA3b8jzaT5EuW5WqZXE/vIq0SB67KXGHsYRQBwwCvgQVgkSsAjqTcUTqMLeLhx/4oBJfJhHuET2hRuzJTiavgyXiTxnr7NrvMvX/yF0Td1YjEgB2US82hxWokNDEaFBE87FyyFHk1+SZfAE7IsUI6cpEhDMAZYcobsMCbHCTSXTK79IqMlEEGWSyQgyaiOeud5/CtgDITTb8XntM= 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:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AASwSFFql4FxZFWfE9BAjkxiyY25fyWbuAesd5VWvWg7CgRMeGqvSSt7/nHVrMeT3XexdVG1BW55nc3RAlEE8aaS0KsQuGRVQC9uEo17zIEKpwWR4EHOiIMZLHKD3N9p07Xf2CWbTbHk8NTXAv1lqQ5jiBW3JDw+N9QSjwAHa3MaYNDFDDCJuC+8Bl4pOwToWI9/U4i/j5DDP7j2nxi7hR/YbokTyucg/v98IKSiwD6i4Q9c+64WdfCsTIwGkpDCC24+TlL6BICYvey6/ogSOLEaEGlnzYRmu2eoMZ4Oe/hTBf+eDmq2FxUHPftjl+2537qZwh5wYftGxaAyX3hEw/e3KUJftG5xRMo42dfKhMlVlaRxqfJ4FEUfbqNNvfMXygQaxVxuYGH8xsbbd1rBMrn/7lQJ6wWyaWC1OQix6tWvXixBasmLT/+XZPv/ESAkgKOYsl4JWIkc57Bcj/Q/vvG8zAkoeOirsIa1QSR1mXMgat9rWE+Q3lh2O+JAbaAegbJ3uGbFOnlq/m2bPZgJIeUz2hJmUPVYwIslpWGKMW41NdiyNZGePQTHiTH4L8UbFRqKbxF8jglIusUaH5f0Xw+D4cvXvXwEm313vws4Ur9UEP4CUtM3PDQ1R1afrL2TYl4piCf4ZwEK2pJ5AL+AQPYqeMn00s32NpQk9EluLptzw6O4rdu7uI3RLaUKV6SEFJYLo07GwxF/VE1T7+ZaLeJbf4u8NYU7Fyy9YfCcONBkohzGHTzVDuS3x04bGal6rjAlj7+o4ipUPH5Snoqg2MJqgxs4wK5PePI1w+NSS5SqTxnQoXBo1aktBEMItwCbVuYJXUS9GJ25DDX7K8WBO7YS03ASD1Q+6bUkZzbWGWfMKz3AAUUPMySW5LuDCY1A24/8obAHDHl2fSdRTIvJCAiRmphLcBY5un2/JqAR/FTPzK/Edk+0b3icgTp9rdtCOk6Y6pR2lpy+RmkUCzw/InjtEK92Jy5bUscy9hOuK8pouiHFT69FLQu3lVC9CPzMyu0u+N0d42Kp2PKgLG6gWqW4Qe4+xW7ImmPyRoMzT2bzVwtm+TqdcPAS1/8EXuGoz7Nt3NScvN9h4l/byROFmPKc2Qw5vKFj06mWlbKCFa/uPjUf2f0WLfaZS7XSAYjup+02TJq5M+ujJe7ks1t1GpHlYsvKOMIhMiOCHbVdkTdqEr+m+gZ2koWmyWTeqjI9RmK8rutUhaW3MF62HPXkqB9avQ6OM7vsxtDGO38qeN6zOYzVSx0zplXB+7mm7jM7DF5wfifxuEQrW/dgbO6zH57eFZSLobPdVR+DpQuBr+WnIj4jJJX/XMaz//oWHWndX2fsBFe3kGqBMZxPLfrulZHh9bEamf22ZcwpqUwdqtgsxXNtb+Miq4nlXAeTemhbu8SYbvvqd9z96fm1UnK78VScKpl9IupHorkPPIM9i7gqIv1mWuu/4TDwS2WRRCrLq0bh5NaOT3P554CAvlz1Wly0zKhrw5n7QQ5aHgvK5Fnwet9ZeocOhHH0gw1KeCZ1CvejPLtalg9RVJyLAT7K+oNShg5hMRa/llRch44Kcnp5v3FgKpoUC19kjLiIqDFH X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: Mgzs7zzaSPnsI0V9VCZvVs9MW9NSboLyQ0ZSeOXSkF8CGPF05WEr8XizjBP4kx+FfTfvfmVqzQflaiE3k88jApTpu6GkZ5eDxydHZpA5zSdp0N3HrG+wp3c+8ZTzQK720Pj4LuAOJwwe8t0DwoDeM/h9EhDqjVzmAlUSsPCAqntXzzjUAjzstrASNu8R48by6i4Bsq/IvhAoPrfS1AQed0ZQnlcSwYrunfjCZd1NBfqGUENRVl6Akeocr13yeR6ENhQaT9gJyrKe1bqN1Za4qgDvbNQGPdevQjA2LPhBAhRHFYgVPZBg37hjSHuXz0gw8VqVbtp+Q6TII5MgSzI+IlQkk0A/lbIl1EaePfOoTRp2/NQvK0lWMAyGIsNssAHfXk5JWYrTaOX0tvRqpIOyUk+xFAAwTvHrTYW9Ks42laQe4GOiIZZYOucAA7z/IuPbabw1yY0nx99GurD173bOMeSSPkWIPgeDH0+XHtceX2IkAkUwm99JpeX85fWMRJjaaxh0dUlztIkZTThOAbNgDK4SXFlJL8wIgpK/mSU1HvZmigY8svtQdM0zQaVqcrnTIVn+SNaM9+Nlkxa5SJZiDqkWdxXWltclGmVL31Nb2YQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1213848f-b0f5-4e12-ad9d-08dc500a53c8 X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Mar 2024 16:07:26.6842 (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: 1oM1aGyu+x8ojryL7wBQCp9KmMzfbK7Ppcim3eLxT9fV2ZwBxECye1H4nE/7dPEfQ7WjiueiNVDHHXLrauGKcg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB4638 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-29_13,2024-03-28_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 malwarescore=0 suspectscore=0 mlxlogscore=999 mlxscore=0 adultscore=0 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403210000 definitions=main-2403290142 X-Proofpoint-GUID: chXn81Fvt1p3-flK2rpjvcG7m_NpmoUg X-Proofpoint-ORIG-GUID: chXn81Fvt1p3-flK2rpjvcG7m_NpmoUg 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+patchwork=sourceware.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 f7b0e08459b0..05948f76039e 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2608,7 +2608,8 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) to: - (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1, + (TYPE_OF_ARRAY *)0)) NOTE: The return type of this function is the POINTER type pointing to the original flexible array type. @@ -2620,6 +2621,9 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) The 4th argument of the call is a constant 0 with the TYPE of the object pointed by COUNTED_BY_REF. + The 6th argument of the call is a constant 0 with the pointer TYPE + to the original flexible array type. + */ static tree build_access_with_size_for_counted_by (location_t loc, tree ref, @@ -2632,12 +2636,13 @@ build_access_with_size_for_counted_by (location_t loc, tree ref, tree call = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, - result_type, 5, + result_type, 6, array_to_pointer_conversion (loc, ref), counted_by_ref, build_int_cst (integer_type_node, 1), build_int_cst (counted_by_type, 0), - build_int_cst (integer_type_node, -1)); + build_int_cst (integer_type_node, -1), + build_int_cst (result_type, 0)); /* Wrap the call with an INDIRECT_REF with the flexible array type. */ call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); SET_EXPR_LOCATION (call, loc); diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index e744080ee670..34e4a4aea534 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -3411,6 +3411,8 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) 1: read_only 2: write_only 3: read_write + 6th argument: A constant 0 with the pointer TYPE to the original flexible + array type. Both the return type and the type of the first argument of this function have been converted from the incomplete array type to diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c new file mode 100644 index 000000000000..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));