From patchwork Wed Jan 24 00:29:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 191257 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp688936dyi; Tue, 23 Jan 2024 16:31:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IHKKoC2Ac4vHCE6ovTVvlu4qP5rIz9uhxIHf/qhN9ZA19YfhCYvTLi4y6IKq8KbdMyJSn5e X-Received: by 2002:a05:620a:10ad:b0:783:33a1:f4ec with SMTP id h13-20020a05620a10ad00b0078333a1f4ecmr6260213qkk.76.1706056308621; Tue, 23 Jan 2024 16:31:48 -0800 (PST) ARC-Seal: i=3; a=rsa-sha256; t=1706056308; cv=pass; d=google.com; s=arc-20160816; b=QkTylFrwpMsUrHh/XxCtxLXl4Iuyy0XZ7Myj2NyKKAAoLVzKp0b9wlKDHMi/1hKnb/ YnFentA7zWhiuPS8Sl3FV7+Ca8avJtBtuqASktmLHCxYQ4YtOvar5Zll/Dg0ZzKIXb5x f+k/+BjU/CLrby28wyFn6zT1WxJ5vE4goNMmROJL2M5HBQ3EK1PEuFAXVMtN+E0Qqcsn axbCWHf0W+U56452k+HZ8Kzz+l3YP+whsng47Kms1BoYZxeHLd+7l+/Ba0jTzY/0LfV8 3FIUBcBqT0OC5PQ/S5RJP5t48c0pamEAhXORvuPBfRZjTAtXVWT1/IM5YD3WLizWWqDS q5WQ== ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version :content-transfer-encoding:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=hVBJU+fkwYSwGtDgXcGaAkkrnY2YEIQQPo6DB7xHpq8=; fh=CJ4gvMxvfPB25iGbJgp7lolaZhkZrM7MYpkDmawi2Is=; b=yBpkQgocQ0R+SMfSQ5nK2HD98c3aj0gZ6PUJGyYytv4jFNe3idmM2tNlwNIwH7x1ZP najXddDWaQ48RVLPsaW8ipJUkvUbtlBjVsT7lxtD0KE1XBRK9Noi047BnQkakPD0IM8v yqhzpQ1WQbxtYsD7NIPpCsLA2LWlXZz/hX2g1N6sSHHuY5L7GCT+YWFyDhRxckY/T5gp LWybyulg+SyCLIZCLJZKdGxl++i1lNGOr2Hqf6l816D0Z10xPjFp2T+6QACfM9SZcSZc ExdHl0K5HBrD03cgLccVRhExQlIFABaQ+cMhwFtPL7bMggcOD6dkiV2ecYGexmS4+DlX WPXg== ARC-Authentication-Results: i=3; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=Bxo07Gqx; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=g2XzqT+m; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b14-20020a05620a088e00b007839aeed6d3si6733414qka.35.2024.01.23.16.31.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 16:31:48 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=Bxo07Gqx; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=g2XzqT+m; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1D82C3861846 for ; Wed, 24 Jan 2024 00:31:48 +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 87387385E012 for ; Wed, 24 Jan 2024 00:30:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 87387385E012 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 87387385E012 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=1706056226; cv=pass; b=Xha9AF98agFUmGxba4VuOxHhOivm1y2kNiRpb4I5ZIvbCo+pfUOzT44p3iKjxvxF5eijWszHMqUIvKiriRaS0OuHXUimljyEMmQnMCgGVdEYvfunMzZSAcWrwQRK1viuH3ITrAycsCv3a/5cqzwDIPmpH6Z8kKPP0mz2fV+0KPU= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1706056226; c=relaxed/simple; bh=FQyZjQv0+GPQumv59uazMKkqFflp/43Id5/ttY8DuiA=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=Ith9RteA49ojY2SNiD16ioZZBbt/bLnugCvnS72z7CV/hwZDMTqwp7hsFKX/fHvJ2sPQu9Zjy85QQzynOZjaqha9chKjmdt575uSBphIVKK392CfedEJwykpsqOUeoQu9d7/qW0iIUehrkC5wwDddoUyK4QiLtUvFnSZYfsajus= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40NMpl77014411; Wed, 24 Jan 2024 00:30:20 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=hVBJU+fkwYSwGtDgXcGaAkkrnY2YEIQQPo6DB7xHpq8=; b=Bxo07GqxsVEz/GvyObQX3Qkh8GK22oWNOv7iKKf5BBL7P0bd0mihIuKiWIcijGo9Zg59 Da6qaOj8ylO/1y6yctQS7H/amOwo/I1ZXha5dMMZ9eH40qQIWCRe1QTIzaDzWaQ1no8h jiYoNL7w1mVg4UwI+W7ZnpQDj16/IO4aH3ySRDOE6x0IBi4BbGANPZ0POoVVi1EOC795 nCTn798trZXjpaXSxWPXKMoVNkFl+jmARdxJv15rr5T3yLRZoqCVRaClJnKwQC2Zq4P9 pcbuqxiLZXXFZfJkQLO17EQ23w49cknBoTObiyo7kOZ6zCQWPLFq1ln9/jLlzuI6Y3ii pQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3vr79nfuwf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:19 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 40NMkq1s005575; Wed, 24 Jan 2024 00:30:18 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3vs371n69c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SmaoevYa+S/J026R32zu0+G+rRSEXTzk/hCa+UQU9cGjiNvHTe6yofcW0kM2CKvqzsDmxU2sVjpKBhtt1zeCIVKvX+ORJtcW0HeVqQAvFGxpnFsApQMRCkZXTrIYBCXhtax8dLmb3oj4KU/TDKOPN+taSViydWJ89+V2F6XC0Alg0HYJpaZvQ/kAh0hDYfii4VnZkpq5Qw7fSvHhmGz4Lm3NGF10Kd9aAVQ7kjnury+7ghqi2yWZUB8ea5NyVWyFgTO6tMRRgJuP5NKWDN6K7vvtH8bwyNIbUyYrSMZ/mxnZyij0XA8y89EAsTg/PxR4dif8f0F47qYdimVFnOw1cg== 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=hVBJU+fkwYSwGtDgXcGaAkkrnY2YEIQQPo6DB7xHpq8=; b=BcLzcw9cyPS/+VvZ1Dp92tKjevcWHfuRGqXlmF7Zl9YKXTOHEulXgrxUX0gcIxf+eRXkn8nyf1ZAwBtTngWps/NQPVGL91Qpo513py2NVttU2Wl6T1lTiuwdu+i6HvGQNj6v+XCnEMUD94BbSNGtr9A0dR3WGm+G/7DySGNztf8H16DvN/v9cWDqjySVBJq3gtAP9a9r00VQq7DZoRO7Pc7dqm2aURWHCdVjbNsdiBARL7fy61ruVqllQ+ylyDVgTCVSu2TS563W475ynaMVK2mRw4J64ACryMKhXGkBdNthuAw8qGP73KgI6gCvyy7yHxSgYm5BUBDEQ4T30YvjPw== 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=hVBJU+fkwYSwGtDgXcGaAkkrnY2YEIQQPo6DB7xHpq8=; b=g2XzqT+mloa0dkSwGll273fuk8TljJjJZse0uLLK4EEVg7nluYIbgPKAtc/R9QYLau0HuSyrzEcV4J8bkmci+uFk4roTq+OKD3JAi4I7bCtZpQAuZ5QnPbPNlAgGzcPgbUOzUIERrut1HCAYnL0vpKpevsPv9BU1R5umkn6q1Hw= Received: from CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) by IA1PR10MB5994.namprd10.prod.outlook.com (2603:10b6:208:3ee::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Wed, 24 Jan 2024 00:30:15 +0000 Received: from CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44]) by CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44%6]) with mapi id 15.20.7202.035; Wed, 24 Jan 2024 00:30:14 +0000 From: Qing Zhao To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v4 1/4] Provide counted_by attribute to flexible array member field (PR108896) Date: Wed, 24 Jan 2024 00:29:52 +0000 Message-Id: <20240124002955.3387096-2-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240124002955.3387096-1-qing.zhao@oracle.com> References: <20240124002955.3387096-1-qing.zhao@oracle.com> X-ClientProxiedBy: BL1P223CA0014.NAMP223.PROD.OUTLOOK.COM (2603:10b6:208:2c4::19) To CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7957:EE_|IA1PR10MB5994:EE_ X-MS-Office365-Filtering-Correlation-Id: 52bfcb6b-ed2a-44a5-5107-08dc1c73a237 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0qLxcJ6V68m/MGRv4NjJ7MiCcdAVsdeM2YgyJgeVvbyU2Bash03uXyNd92h6JafqSrOUNTxVP05xBd6wcXVsItSKEqmSR6hr1mD90f1K/085JkKZaBYLEArl55xYqnf5KKwoA1u5R+4zNzPLJXZyQLN2HUwn2reOoglZZz8SdszRRrDpzqaSVC0P4VUyHAuYGV5XzMeoMZC9l2v0ETJhaBIdigoysHuDd0dnmik6UedagCh3VSjZAPhqpci1PV+aKBVokH0NZ7RT27TPV+kRUCCwFh+Hecp06xTqkGmZI1uw0nNbJ/XQh6OYY6vTTMKuQu+VaPFbwRB9941iH9YXjRN21H3HUqtt5P2kDRdQVBt9Ke2lUa3CQcfO55AD12e9n/FC5fpV1fqckYNuRW7xkIneY0X0wlF6DMmgBpd62FkaccpwOrMmLmIwqN/GfRzJRPaxX4hr8xysvFljVNr3mj7PYEYbVgs8UkKVGyIKiwuI85cUHCzaavWdfx7YDPgA5RqlEr2is8+AQzD5QEAE0tINQ7fH6E7m7gZfF40jMZKROJZXO22y2EH9cluK5AIUkmAIMi68hYHvA7vuGrRYdZd2ImzB32PhDtAQrvb7ESdHFTtLDBF70EKXwHlYA0jC X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR10MB7957.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(376002)(136003)(346002)(39860400002)(366004)(396003)(230922051799003)(230173577357003)(230273577357003)(451199024)(186009)(64100799003)(1800799012)(84970400001)(6666004)(6486002)(478600001)(38100700002)(26005)(2616005)(83380400001)(6512007)(6506007)(316002)(44832011)(5660300002)(30864003)(2906002)(8936002)(8676002)(4326008)(36756003)(86362001)(66556008)(66476007)(66946007)(1076003)(107886003)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hTAo04lRtDiZtffDXx6SwMfKykXuxifbt3PM9/cKcdx+1w82vdVhL/y1+s1Leox7rKDmbUh5TBvMprl/K9nGGUuyt68OjZsZdO0UKAwP4u9ZKA6eks5YjgANO6cKcBq3C+uPCpPAFmg5Hct5azDkPfw08wTYolUDW5Am8nBKPBSpZKSnAj7rU+HEHr5TLuFb8fl8qxXTOhPivMo9wfNaqsVrAVO6tpH1DlJNm+4RPdTHJAnBmVF1ICbYwiJKtf4GpezTQK7vAO1odnPhQKt3Y5FCgJaOA6sVjk46+yl05v7zRsl6MK9QwV7IHqSg0vgaJLuSANVYoiX86AJiTzjyHM3hDV5sZiow6NH1vcQYtpXSQrgD1TgSsOmmmF2cQTd3JtHsya6AQWJpoiQT8sFooyKCcN1zS5shqiwk34cABonjPnsd9fq4GH3V9/nsl5hZB4wOS8W5BTT/IKpPV6bSiphl4A8AJ00YG8e6Y0cxvDcx3O73jDDNgnTLDhaHXqBu3+C95Boz75+6124i9p5fHSnu0ZdETRPM4/gaJ5Pn57UyOiMQ8r6TwyG01u3xMSNBfa3L/z5juOdCxR/c/1LkIVzrFDNUx8SC3IzMp+u/T2DGct8XAhCmMPrLRTEsT7Vh62luxoRuOmkZELtIEykIm/F1JaPDPc7QoWj54N4ULtTFQHPH0C4+TI60w/ZMAhxgQBfEOl0CgONI9Gt80eacjDARbyzoXJvxldduuZy4UWq8azZNhPNXdlrDhXOIAiCAXeDkzs8FFlPQAprKNCWbRX4CIoRJL9EAukF5sjHNcpQGjpXm1gbJm8Fq5Dqi/lYzhWNSxQieICQCr5QiB40Iva7BQ0dgb3BynWaxg5uaU2a9IrsFbsXOGmeGS+9dqDJlgbGRDULu8E8/aotuSkogjNEoNbrJ1DIwRWDJ/ekoJIYKNpAwpT/CWRPUgLEco/6ZZ3X1Is7z9MzQXHQdVX2O14xCSH6hodHZ7EGNMRmSq4bffDS8FhdOpCGXd/M1YsIiHJU8wisRlQkf4cMb/KczOREfdhIjb4DYAASNfigShnteoXJE+iBu1mAE8LpjNTSAccatkyjyrRMnpwVg4WmMI3i9xn7aSHHf2wbaaL23p5OLcVWR/dgCugbhMYCDlOy9EAZuhxhxiejAX4W5YWOEYTVxm/7toGAbN7zZwFloaS0OWoCg7XOOdf9xJTw7JIZua+zim8coLpOeeoz9JHCn7OiAc5c6BvVB47W5rTSw614TPuvdzf+RwD37SvLjRSUCMl6wVmhr25JYhjST0j7BRx6vcyeaHM4ARa21+6j5zmpPJncmJBtL8N89UVGXa6dHKOOHz7HFKyrGLqS7OH0s2KL1AylbvqZ55kNYLfOwml+4O/5YuLr+ktBcxONJ+StetLtUdvUexQX3ryYigE3x6Iwcn7IdGa8rwQmBZxTutPDTWsixxxnHq/fgjpK8HCDdNK7iM5CibJ4ZMrgYAWrlJdHI+fMdqy8qSs2xmnPq0Jv02YhW+CJMGcWVMv4TAKaC0TzSG0LWCsTuV73jB+0BWoSUrgZUHKZknS3xPE+HfIpCjRSc4774TwV/OGSKQtSJ X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: BVXw/m1PUQD5pPBI1FUKb4PuH/dSGVLthvzhIya2rpRwsLVW8VmPxzuvU9aTjdNXs+myaZHmQG65KfhytwZAKUagPlBuveXwwLUPTtM6z0erOkp74SaYSqf2c19sj3BONiXBsReRBmxnIu3B2h4Ud+Iit/CHV1cwhHNpgK8toYKBBqf5RNt/t1lo6JjtRv5xme5MXV6lqj27NrxLmtdreaOF8TyjJm7ZO1dwVXarlDCOx+Sh6ymIGhsYeEDaB//yjek6mrY5NPbGXLApfluniMEZ7uqh499cRNblaEJaAAYokl+bfSxRbSdMIAdH/G2UVN+uknTt5qdilthVBQ637mWFHgPt0l4wlMVwYT4NgWg66kobSdNixfkCnH/1hDP+RMwtFSzqCmR5oPTijPfhl9r5JlF1w9yPMrzmuXfuYOyfVAHNGaciU/sU5d2+BnFd8ythHpnXTpSteWmQsSvEHO5yPJMhQFUuA/jOOhywkEEgTqTDs438yNQtef/FEvOuCo7EIw2Dk9x1sO79Mxl4zOJt851sM2XRURy0S4O1rBH+Vk1VloP1H2hZTfNkQH0mtKfF6xJUSn8swVewXLYW3NbKsEJM2jgTd4RDXFuhBtg= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 52bfcb6b-ed2a-44a5-5107-08dc1c73a237 X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7957.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2024 00:30:14.9236 (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: YKNT10wXNVfDE1aPP8oc+9j0FpTwOqaL51MPr+IG/7+0VJH9ZOF292Q8yJDcO6g4Jz/ZNfwJZ9zjh/0bgGkjLw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB5994 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-01-23_14,2024-01-23_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 bulkscore=0 mlxlogscore=999 mlxscore=0 phishscore=0 adultscore=0 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401240001 X-Proofpoint-GUID: 99LLaB3vLn6rqfwPMssOIyjwe9OfQVv7 X-Proofpoint-ORIG-GUID: 99LLaB3vLn6rqfwPMssOIyjwe9OfQVv7 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788929699644979276 X-GMAIL-MSGID: 1788929699644979276 'counted_by (COUNT)' The 'counted_by' attribute may be attached to the C99 flexible array member of a structure. It indicates that the number of the elements of the array is given by the field named "COUNT" in the same structure as the flexible array member. GCC uses this information to improve the results of the array bound sanitizer and the '__builtin_dynamic_object_size'. For instance, the following code: struct P { size_t count; char other; char array[] __attribute__ ((counted_by (count))); } *p; specifies that the 'array' is a flexible array member whose number of elements is given by the field 'count' in the same structure. The field that represents the number of the elements should have an integer type. Otherwise, the compiler will report a warning and ignore the attribute. 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' should be initialized before the first reference to 'p->array'; * 'p->array' has _at least_ 'p->count' number of elements available all the time. This relationship must hold even after any of these related objects are updated during the program. It's the user's responsibility to make sure the above requirements to be kept all the time. Otherwise the compiler will report warnings, at the same time, the results of the array bound sanitizer and the '__builtin_dynamic_object_size' is undefined. One important feature of the attribute is, a reference to the flexible array member field will use the latest value assigned to the field that represents the number of the elements before that reference. For example, p->count = val1; p->array[20] = 0; // ref1 to p->array p->count = val2; p->array[30] = 0; // ref2 to p->array in the above, 'ref1' will use 'val1' as the number of the elements in 'p->array', and 'ref2' will use 'val2' as the number of elements in 'p->array'. gcc/c-family/ChangeLog: PR C/108896 * c-attribs.cc (handle_counted_by_attribute): New function. (attribute_takes_identifier_p): Add counted_by attribute to the list. * c-common.cc (c_flexible_array_member_type_p): ...To this. * c-common.h (c_flexible_array_member_type_p): New prototype. gcc/c/ChangeLog: PR C/108896 * c-decl.cc (flexible_array_member_type_p): Renamed and moved to... (add_flexible_array_elts_to_size): Use renamed function. (is_flexible_array_member_p): Use renamed function. (verify_counted_by_attribute): New function. (finish_struct): Use renamed function and verify counted_by attribute. * c-tree.h (lookup_field): New prototype. * c-typeck.cc (lookup_field): Expose as extern function. gcc/ChangeLog: PR C/108896 * doc/extend.texi: Document attribute counted_by. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-counted-by.c: New test. --- gcc/c-family/c-attribs.cc | 54 ++++++++++++- gcc/c-family/c-common.cc | 13 +++ gcc/c-family/c-common.h | 1 + gcc/c/c-decl.cc | 85 ++++++++++++++++---- gcc/c/c-tree.h | 1 + gcc/c/c-typeck.cc | 3 +- gcc/doc/extend.texi | 62 ++++++++++++++ gcc/testsuite/gcc.dg/flex-array-counted-by.c | 40 +++++++++ 8 files changed, 239 insertions(+), 20 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by.c diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 40a0cf90295d..4395c0656b14 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -105,6 +105,8 @@ static tree handle_warn_if_not_aligned_attribute (tree *, tree, tree, int, bool *); static tree handle_strict_flex_array_attribute (tree *, tree, tree, int, bool *); +static tree handle_counted_by_attribute (tree *, tree, tree, + int, bool *); static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ; static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); @@ -412,6 +414,8 @@ const struct attribute_spec c_common_gnu_attributes[] = handle_warn_if_not_aligned_attribute, NULL }, { "strict_flex_array", 1, 1, true, false, false, false, handle_strict_flex_array_attribute, NULL }, + { "counted_by", 1, 1, true, false, false, false, + handle_counted_by_attribute, NULL }, { "weak", 0, 0, true, false, false, false, handle_weak_attribute, NULL }, { "noplt", 0, 0, true, false, false, false, @@ -659,7 +663,8 @@ attribute_takes_identifier_p (const_tree attr_id) else if (!strcmp ("mode", spec->name) || !strcmp ("format", spec->name) || !strcmp ("cleanup", spec->name) - || !strcmp ("access", spec->name)) + || !strcmp ("access", spec->name) + || !strcmp ("counted_by", spec->name)) return true; else return targetm.attribute_takes_identifier_p (attr_id); @@ -2806,6 +2811,53 @@ handle_strict_flex_array_attribute (tree *node, tree name, return NULL_TREE; } +/* Handle a "counted_by" attribute; arguments as in + struct attribute_spec.handler. */ + +static tree +handle_counted_by_attribute (tree *node, tree name, + tree args, int ARG_UNUSED (flags), + bool *no_add_attrs) +{ + tree decl = *node; + tree argval = TREE_VALUE (args); + + /* This attribute only applies to field decls of a structure. */ + if (TREE_CODE (decl) != FIELD_DECL) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for non-field" + " declaration %q+D", name, decl); + *no_add_attrs = true; + } + /* This attribute only applies to field with array type. */ + else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non-array field", + name); + *no_add_attrs = true; + } + /* This attribute only applies to a C99 flexible array member type. */ + else if (! c_flexible_array_member_type_p (TREE_TYPE (decl))) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute may not be specified for a non" + " flexible array member field", + name); + *no_add_attrs = true; + } + /* The argument should be an identifier. */ + else if (TREE_CODE (argval) != IDENTIFIER_NODE) + { + error_at (DECL_SOURCE_LOCATION (decl), + "% argument not an identifier"); + *no_add_attrs = true; + } + + return NULL_TREE; +} + /* Handle a "weak" attribute; arguments as in struct attribute_spec.handler. */ diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index dc67562ce923..8e9f7370abfe 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -9903,6 +9903,19 @@ c_common_finalize_early_debug (void) (*debug_hooks->early_global_decl) (cnode->decl); } +/* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */ +bool +c_flexible_array_member_type_p (const_tree type) +{ + if (TREE_CODE (type) == ARRAY_TYPE + && TYPE_SIZE (type) == NULL_TREE + && TYPE_DOMAIN (type) != NULL_TREE + && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE) + return true; + + return false; +} + /* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the values of attribute strict_flex_array and the flag_strict_flex_arrays. */ unsigned int diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 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 4029bbc59fe2..77130e839a27 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,60 @@ 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), + "%qE attribute argument not a field declaration" + " in the same structure, ignore it", + (get_attribute_name (attr_counted_by))); + + DECL_ATTRIBUTES (field_decl) + = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl)); + } + else + /* Error when the field is not with an integer type. */ + { + while (TREE_CHAIN (counted_by_field)) + counted_by_field = TREE_CHAIN (counted_by_field); + tree real_field = TREE_VALUE (counted_by_field); + + if (TREE_CODE (TREE_TYPE (real_field)) != INTEGER_TYPE) + { + error_at (DECL_SOURCE_LOCATION (field_decl), + "%qE attribute argument not a field declaration" + " with integer type, ignore it", + (get_attribute_name (attr_counted_by))); + + DECL_ATTRIBUTES (field_decl) + = remove_attribute ("counted_by", DECL_ATTRIBUTES (field_decl)); + } + } + + 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 +9449,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 +9510,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 +9531,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 +9551,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 +9806,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 cf29534c0915..27a5c857dfbd 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -775,6 +775,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 66c6abc9f076..d0895d5d9f28 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); @@ -2370,7 +2369,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 d1893ad860c9..64d3dfcefe65 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7743,6 +7743,68 @@ align them on any target. The @code{aligned} attribute can also be used for functions (@pxref{Common Function Attributes}.) +@cindex @code{counted_by} variable attribute +@item counted_by (@var{count}) +The @code{counted_by} attribute may be attached to the C99 flexible array +member of a structure. It indicates that the number of the elements of the +array is given by the field named "@var{count}" in the same structure as the +flexible array member. GCC uses this information to improve the results of +the array bound sanitizer and the @code{__builtin_dynamic_object_size}. + +For instance, the following code: + +@smallexample +struct P @{ + size_t count; + char other; + char array[] __attribute__ ((counted_by (count))); +@} *p; +@end smallexample + +@noindent +specifies that the @code{array} is a flexible array member whose number of +elements is given by the field @code{count} in the same structure. + +The field that represents the number of the elements should have an +integer type. Otherwise, the compiler will report a warning and ignore +the attribute. + +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} should be initialized before the first reference to +@code{p->array}; + +@item +@code{p->array} has @emph{at least} @code{p->count} number of elements +available all the time. This relationship must hold even after any of +these related objects are updated during the program. +@end itemize + +It's the user's responsibility to make sure the above requirements to +be kept all the time. Otherwise the compiler will report warnings, +at the same time, the results of the array bound sanitizer and the +@code{__builtin_dynamic_object_size} is undefined. + +One important feature of the attribute is, a reference to the flexible +array member field will use the latest value assigned to the field that +represents the number of the elements before that reference. For example, + +@smallexample + p->count = val1; + p->array[20] = 0; // ref1 to p->array + p->count = val2; + p->array[30] = 0; // ref2 to p->array +@end smallexample + +@noindent +in the above, @code{ref1} will use @code{val1} as the number of the elements in +@code{p->array}, and @code{ref2} will use @code{val2} as the number of elements +in @code{p->array}. + @cindex @code{alloc_size} variable attribute @item alloc_size (@var{position}) @itemx alloc_size (@var{position-1}, @var{position-2}) diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by.c b/gcc/testsuite/gcc.dg/flex-array-counted-by.c new file mode 100644 index 000000000000..f8ce9776bf86 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by.c @@ -0,0 +1,40 @@ +/* testing the correct usage of attribute counted_by. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +#include + +int size; +int x __attribute ((counted_by (size))); /* { dg-error "attribute may not be specified for non-field declaration" } */ + +struct trailing { + int count; + int field __attribute ((counted_by (count))); /* { dg-error "attribute may not be specified for a non-array field" } */ +}; + +struct trailing_1 { + int count; + int array_1[0] __attribute ((counted_by (count))); /* { dg-error "attribute may not be specified for a non flexible array member field" } */ +}; + +int count; +struct trailing_array_2 { + int count; + int array_2[] __attribute ((counted_by ("count"))); /* { dg-error "argument not an identifier" } */ +}; + +struct trailing_array_3 { + int other; + int array_3[] __attribute ((counted_by (L"count"))); /* { dg-error "argument not an identifier" } */ +}; + +struct trailing_array_4 { + int other; + int array_4[] __attribute ((counted_by (count))); /* { dg-error "attribute argument not a field declaration in the same structure, ignore it" } */ +}; + +int count; +struct trailing_array_5 { + float count; + int array_5[] __attribute ((counted_by (count))); /* { dg-error "attribute argument not a field declaration with integer type, ignore it" } */ +}; From patchwork Wed Jan 24 00:29:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 191256 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp688931dyi; Tue, 23 Jan 2024 16:31:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IENRiW20p/2B83x/2h8LeatBTqZNTm0qhLyXKNCCsYenBNpGhOMWSg2i7Y5z4FG99MPZNMv X-Received: by 2002:a9d:6ad5:0:b0:6dd:ef0b:4f62 with SMTP id m21-20020a9d6ad5000000b006ddef0b4f62mr714501otq.77.1706056307874; Tue, 23 Jan 2024 16:31:47 -0800 (PST) ARC-Seal: i=3; a=rsa-sha256; t=1706056307; cv=pass; d=google.com; s=arc-20160816; b=vKNXFFzQzkvba9dkT4LdGciEPFoMXNRF4CUEc6Q3rEcTfWsdnoSaj3U2I32w9GeBRp v0OMYuFXNS12GmdW/jikxuIMrrAOsAm7ONueZQTIwUIpale8W7xri4sWKHcfKjacBuCT fMkgnNqYe3dUq9BeqFJ+KfrYyKWvKx7dJTRne0LMgIND32F8rEG1TQJYGWTz9POvvyg3 5/lwJMkKDEh5tnYHCENOaPZ1X/NGw7evURbkvTCjrEX6hYx9d+9/glN3edXBX+tTV6bJ IyteyWupNgR5UcC0blaw+MkYya0oNSfCguPygpOU7wZ4x7zbwuXxysBEn39nc7PUDxnS 7dHg== ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version :content-transfer-encoding:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=xr5usrw/6zfdj6GD5IC/kZP74iYVuS2FAziP3AE6XF8=; fh=CJ4gvMxvfPB25iGbJgp7lolaZhkZrM7MYpkDmawi2Is=; b=gAc96+dPOIWrR4edBVxiFFNYf06k80+dNFnbkVJPwsgr6yjpTJX9Rlv7w1d3wlrBWM CncAmH+7Qf/iRoD9EGmvB9GW4bVJPBK/5nntW3yy2Wg9Dggd44fq8Yp3aYj8y3nCUnQR bNJPfPI4DEvy9L577AMn+fQUsTvvmiJo5ZINgF/0OFSJR/g7S7Y6LEFunj0yujjNQjJs bKzLxWsWAT6ZceTXhVax2LIsqepOpDRXNwu5CwUeSx7d8ouQtL0eyBI5FHKeJmtlQTaA Yh2lzsQAFui9/BngBai1AcPVE5D8htfnLR18MUKrwQKMCBQZTj2l4n7yYb6Y+Uzcpk4e Fz5Q== ARC-Authentication-Results: i=3; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=FJm96uJb; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=ihkvkA0d; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id x15-20020ac85f0f000000b0042a2a01bd35si9129598qta.600.2024.01.23.16.31.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 16:31:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=FJm96uJb; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=ihkvkA0d; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C37323861846 for ; Wed, 24 Jan 2024 00:31:46 +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 7A3A4385E02D for ; Wed, 24 Jan 2024 00:30:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7A3A4385E02D 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 7A3A4385E02D 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=1706056230; cv=pass; b=Ra/gjQwKhuwCeCxDZANSXbzWjkLDrDjpq+y4XROVT2mPGXMdZTcdo1QjOFwTD5C+SVNVhoLmkqKoQgUGN194XdJ/L0XxdgDgqgTNPJvkU97h7a9xbSbXZz/wL7IozZNnsikVKPB+Kxu1SxjdrvXxp50YiGi1ws+onUN2lKn6Ljc= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1706056230; c=relaxed/simple; bh=MFatVRJyZVmZnueyOMhFIow3ECE/Rqvtp2TPr8oPJwA=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=Z/6ox0PnftwsQvX83GfdOii0biaQFbD4weueBnTDO6B4UM1x8zuVEicKh1OIVBZC3cxvNKgULdVjNRxc2Kd5V6q+pFX6E3ph5cgQjBFcoi1vUrfKZz6PzGb1fcqcXpgZ8AVZksFjczZyHQWOHruc02IW/o9Tk2MWd9+FcWuQxgM= 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 40NMqojZ011685; Wed, 24 Jan 2024 00:30:22 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=xr5usrw/6zfdj6GD5IC/kZP74iYVuS2FAziP3AE6XF8=; b=FJm96uJbE2a11W/bm82CYypyfdaW0pWL4ogAxX/wdatKwNQMjrhfetlsk1TTbOVKzQ0y TJyejTu2WKIN5ghwpOjHnlE106aC2rxhdrvro3Xm4f0JuAEjYssiYpT5c3kK70mty7Dy IRWG46Y6yYo9HMqPfw7kpqguqJctC+ypO1Jm7mgsAq2w+Ekb4LtxqlFGG2Uj/FBRcPHD KY5Z0VYMTytbEfgkbNk8wZjmABmlF0+Qe+MObDbhVhsy2r+Qwg7G+ssTa97rU+UDH76U ycC5HTNTb+uxSz1ABMV9Ks7tZXyTiN8s3jOvaYJ/ftjTRdw3irwbJ6ltcfJzi0of9eF2 Fg== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3vr7cuqua1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:22 +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 40NMZjhB014539; Wed, 24 Jan 2024 00:30:21 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3vs3724hmd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=P3M4dF4NBO90cLz9BeovNHl5j2STEaNYfKmFBrWt8NwPPNfeKiEdJnpc3NlXR1eFtpt2v7vqZ28lj3vXxmwL6xf82N8dYC9eqj2o6/iB1FqlVluIZPlnNtwcRbmFsvdDKDSFGSG754CacXDgDoQBYezD6Ey/Bink5hmWac7V6JwJ+awjrj3CbkBhko54FrgXBzP2uYudc7ADN5Ch60CnUF3/XKcbKWYXnPmaDrnkli+4XdGHi0ia7vdF6D5KztR6ddo/P5naxo3dEKErwRsas9EmuTjGRcqPyWpqbsbafmj/YoZPDKnnLQtx6Lgt2FCoEJ5F8jtXowRrzcdIecVj+g== 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=xr5usrw/6zfdj6GD5IC/kZP74iYVuS2FAziP3AE6XF8=; b=NqmE397s2Jw5zhpQh5Pf1e1VQwJNmefTrecnRPWadFp7Mb+JUGSEfQONMh7GF2VrzaviAtRbVN7ffDcznva1symbDduoZPl7Aeb899j+XhMivFgAhLbihuT74XRLz9OYzyQlZvHO7WC1WrTAFe7ppeqrfjXIaXYwkWv1F4+rFhKoHdrWjk0ya+kawhpwWJCtCElWA1u1ObuQQV3tzOi7z9gPyGT09Icklv1ByxI/bsvyC7wrOEyXgMqSZVr4Rf2jaZ/WhDZwPPVrfIuKYzLUL8gEqDhdRnn2xPp5CO7nBZGt9CgDORWQp0n/VfLZC4IFmnDhk+Is0WUy7p2dTr9tEQ== 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=xr5usrw/6zfdj6GD5IC/kZP74iYVuS2FAziP3AE6XF8=; b=ihkvkA0dy0Sm0S8FWS3UBc49SRw6A1OazxlDjxyJn0yM2WE98ulEdf1ys7C0fIn+HFxjJwc3GjzGDOhoNOafQVE1rD+StQawz7vlmT1235p0Sxdwnt7ew+FWRqhIwId+MpNcXu2ztU7lhWaFVk0X+I9tA0ocAmzB/DH6MqBCKMw= Received: from CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) by IA1PR10MB5994.namprd10.prod.outlook.com (2603:10b6:208:3ee::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Wed, 24 Jan 2024 00:30:18 +0000 Received: from CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44]) by CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44%6]) with mapi id 15.20.7202.035; Wed, 24 Jan 2024 00:30:18 +0000 From: Qing Zhao To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v4 2/4] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE. Date: Wed, 24 Jan 2024 00:29:53 +0000 Message-Id: <20240124002955.3387096-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240124002955.3387096-1-qing.zhao@oracle.com> References: <20240124002955.3387096-1-qing.zhao@oracle.com> X-ClientProxiedBy: BL0PR0102CA0016.prod.exchangelabs.com (2603:10b6:207:18::29) To CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7957:EE_|IA1PR10MB5994:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f1c561b-ac96-4a02-6468-08dc1c73a47a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2/+WW9ClTDJCkG0jAZ8aovh7RL8rjsfNDqdDll4RKrLyHgJeSCrCcKnavu4FXQZQjb7KNPJga1CT5EGNTPkQxAh5a45CQoAWvTg2dkmtckonlUnvhZPYg9RfdrA7U16jF8jtkM0vx0qzu1kDgSpEUXdHtOiHJoIkKCaJq/ZwOWKFS5Fbi1FgWYkhziOW6a+vRIfVpE9dPspbMU99LyFXFwN/OQgjhkQESdqMYhaNqx9vntGi94Xdyf/stvRZLCKDdmzoyQge0DU7tpaMMX+WSvmNDLYu6/DswqtCfVj8eQzQ+z+h4rRkD05i/G0Q/tHYWrZBGg8TXA9gUsn2ysSSjh+ymS6I/x6qthWXGS9FdXK7CkkjZosjX5VQDdGeDqij3Q19Xv1Pr6XytoyIYdnoyy8W7WzFjXiwxb43IqLTRPElBlblIHrXJtkJ/4YHNSLmAt66uztYVERpQ4miWE6VRVir6cDsgm436/cLB3OhNwoTogbTMJMnzyhoBA2U/iUQ186prwRVsKQZguwJ2mVgZlt7tZPErIEyBXUorMrETBI0VrIroNX6wdxCeyrWQloy8F04EdKucLPsGv45jhbNzzSP5KrCi+w02suPWJKublvHNIOBcHHHC3OhBLEbLVAmfmqqvfpR+I8yAJIBvYOkQQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR10MB7957.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(376002)(136003)(346002)(39860400002)(366004)(396003)(230922051799003)(230173577357003)(230273577357003)(451199024)(186009)(64100799003)(1800799012)(84970400001)(6666004)(6486002)(478600001)(38100700002)(26005)(2616005)(83380400001)(6512007)(6506007)(316002)(44832011)(5660300002)(30864003)(2906002)(8936002)(8676002)(4326008)(36756003)(86362001)(66556008)(66476007)(66946007)(1076003)(107886003)(41300700001)(142923001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pcqDfzVDHZWhiOTOPUpdVcb8MIBgKXM5AafmiTOEU3y5IsKQPomw8i+BaAAxkMe+3Ebk8U4OV3l5UMhhQv+7EboOepO09vS5ShR0R6LEoDgwWpshr88JdYshJlzCJnQ1Xrdu/3XdkD2zSVSAF65t0Yzpb3kAtX6rLvdLM2YYTCabc1L9o0/NJgK3qT80WcE4MZjRx97Z5R8ScyfOonf9qwUcFXs1tyaRP+rt3mWn4KcJtkxjTqTu6Ok8syX9Jez9SmFyp6EI4Ickz9wGSnQXsa0HQ1bNWEu5yAFxWy1wDy+bYoAIvR5ZfMRL9EgoYL7r+x9J5IU4c8kwRGKI1FFxTrLTYaaNMVRCgQe1q5GoibuI61nFb40hzSrsFoU97wGoE2INbUQmz/P2TOTmjGiPyz9pxmDzo+vBiJ11Vy+wRwXDl7WQp5MtE1u5GPiH8vq9eYLEwJ7i0a6ooLpeYV/s+/Y0Q3iyliq+Ol5p7IHLfXN9r6XcK7PHvBFY3QlRK5wt6r0Jdab3R3P3fNnhHogEUCUbjBz3/gnNd9iYwldtgtRof5WZnPzLIJu38/qyl1gN3jicnbRLtJJEbwUP/CU8drC2AzEQl4gbhCz3X1BQj93pA70oB3gjhFesNNUJf0llMgVOa2W0s1JS09rQHVsmNwR/ylF6/yMMj6Lmszv/wTD5wuv+ugSw6NCVmjm7+Qip5bLT0mtL+YojiP3Z6z9Sa5F+C6IFbb8wJ7ftaSUmKY12hkqNtQMh/piv68VtyYopOc10UY7oo9AS6WW9580qKfu7tr6qzEi1RhLPcSxF0D3Zrx2HBjTD8K/IOpBb9hZygpIlR9j6stTib0vOkBgmzYmqD6XrAmvHZ559lZCnsOePX4xHES66I57VdoLjt1SxCuI874BS8qAN4ct1YEmv9i8o40uPbvtHFfo2Ka4KBm9YiaShmGfT1gZhV1KdUkWZZQxfnE8VamDPStQhUhYV80GJon8kDlZzXwbMv0a9D+32IKv0E7zV1EaKbFjKpZVcS5OQYh/g/rFMHpqDmNIqGnCvr68iXIa/Lfw0b2Ss5wBhXUKEa9UhlHWZPNf0KD/y3hdymIHhvUOkaj29pEozvyfre+C/5hBFAiqxyX89OgJdjp1e5GjMxEMTc5QlQBtOqWEjnn0aqT2DnDXh6UMlQ+HD/GfNFPSkMEDeMaAHOuekcegXLVzyiy4jYQL37PVnrMR0z0FWY6v3FlUQ9E3S6xvVi/1apCxUU7gkKo5szgAKqvkeJDGTRIebjRWYa5yTNRJyQK6s+d5aQ+wl1ljlep2Du+BiU7AXazH7dVfGOJRRshyi4yz/CGHc5ebGGs2vlTdAml/Nb7A52p/Rou2d0K6lKSf+NWCOr80NG+J/pcgtRz0jN25fqA5oQ6usliFmHKiFcmcV2m9WMGZ7ccplgZYcisXW5LrYXxIhX8bEltwFZU1PTArL9kaYPNuK1IQfM4VlX1ELCzwABe380MdStDd4z2gFgy7zJhzukfRbe/QvNGXnl+rWhklBdt5oGE8vp1KFqEw2x/Sop1VMRj1EqIAyBai8xk6C1nVtbwAPEKtr3l5lDYUG4UWrJAKJ9jgt X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: foze75C/7ZpvK1AwVoXFXXelyVcQQShaZFhKKZkXqMbnqpM2w2cq5qqq50W71Sg84FJ92DQ+uEStO0EM9I06SgftCiGjFkmku0d2KO6poRGdy+VfzR/zQhSAL5btFOLqXRMfMRgGHra4Sg0mrZBXNqtvvo6x1Xyl4Of+rITe+8H6Br7Agn90ztKA4TonDZdwclaI6Mj9HLrjchF2rfO9vvvcyyniDWhTFu/+mCDBdvXeYK2Z8mMwn3Ep5vY/YX5v6Za47eGIT4r7fnYMoW+iWP8oZOuUyf5YgGx59Ipa9OTUFaMVp5APGIq+Rnr+r+8m8xczD4pOsLdkLRcH7OBYgJWsu7RmlnxPyNNGbkN2z3MwaWvH1I6FNosWuFkUfBAIa/WUeYK2e7mgzEsYrYiYrZOZqCd8Rj+r5jcaqfSUnWP0H0qnBiV5e89Zw02k0Qu1BPt/qhbN0+di0bUO+Tif/58U91wO1Y8pAh4HO4KhnbvVwZv+EDRUVnFzDqOLPqVpKsAFcF0v0biWV2PXC4XmSafczvRD9l5O2p8LdGusZEdlHzoq0ndcENgXHdbVEhd8DRBVr+X5q2ncNOpgC3xNstLTshzRu1WnzAF4qn+oDzc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3f1c561b-ac96-4a02-6468-08dc1c73a47a X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7957.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2024 00:30:18.7283 (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: sOtaf97u52TDmLPSzf6ARw9cRyIalbN5ekpj2QIGrui077w484Yj1ubzEjCVwaqlLk04T+GpM/2AF8vo/xepkw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB5994 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-01-23_14,2024-01-23_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxlogscore=999 malwarescore=0 mlxscore=0 phishscore=0 bulkscore=0 suspectscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401240001 X-Proofpoint-ORIG-GUID: E7rEGwu3xY5luIKy1OQJCnPJhfi-8gpJ X-Proofpoint-GUID: E7rEGwu3xY5luIKy1OQJCnPJhfi-8gpJ X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788929698980063736 X-GMAIL-MSGID: 1788929698980063736 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) * 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. 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 | 108 +++++++++++++++++- gcc/internal-fn.cc | 35 ++++++ gcc/internal-fn.def | 4 + .../gcc.dg/flex-array-counted-by-2.c | 94 +++++++++++++++ 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, 294 insertions(+), 9 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 27a5c857dfbd..fdda413da498 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -777,7 +777,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 d0895d5d9f28..4020bafc8e36 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2541,15 +2541,107 @@ 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 location LOC, the corresponding + COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate a call to the + internal function .ACCESS_WITH_SIZE. + + REF + + to: + + .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, num_bytes, -1) + + NOTE: 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. + */ +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))); + tree result_type = build_pointer_type (TREE_TYPE (TREE_TYPE (ref))); + unsigned int counted_by_precision = TYPE_PRECISION (counted_by_type); + + 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 (integer_type_node, + counted_by_precision), + build_int_cst (integer_type_node, -1)); + SET_EXPR_LOCATION (call, loc); + return call; +} + /* Make an expression to refer to the COMPONENT field of structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the location of the COMPONENT_REF. COMPONENT_LOC is the location of COMPONENT. ARROW_LOC is the location of the first -> operand if - it is from -> operator. */ + it is from -> operator. + If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate + call to .ACCESS_WITH_SIZE. otherwise, ignore the attribute. */ tree build_component_ref (location_t loc, tree datum, tree component, - location_t component_loc, location_t arrow_loc) + location_t component_loc, location_t arrow_loc, + bool handle_counted_by) { tree type = TREE_TYPE (datum); enum tree_code code = TREE_CODE (type); @@ -2621,7 +2713,11 @@ build_component_ref (location_t loc, tree datum, tree component, int quals; tree subtype; bool use_datum_quals; - + tree counted_by_type = NULL_TREE; + 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; @@ -2640,6 +2736,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; diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index a07f25f3aee3..e132117307e6 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, TYPE_OF_SIZE, + PREC_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 TYPE_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: unknown; + 1: the number of the elements of the object type; + 2: the number of bytes; + 4th argument PREC_OF_SIZE: The precision of the object refed 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..d96d025b63d6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c @@ -0,0 +1,94 @@ +/* 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 (); + + 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" 4 "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 8aee3ef18d82..022010ae78a7 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) @@ -13357,6 +13358,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 (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..75c97145cb3c 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 (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 d2c879b7da44..72fec9b38c73 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -5077,6 +5077,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; } @@ -5271,6 +5276,11 @@ output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align, exp = TREE_OPERAND (exp, 0); } + /* for a call to .ACCESS_WITH_SIZE, check the first argument. */ + if (TREE_CODE (exp) == CALL_EXPR) + if (tree ref = get_ref_from_access_with_size (exp)) + exp = ref; + code = TREE_CODE (TREE_TYPE (exp)); thissize = int_size_in_bytes (TREE_TYPE (exp)); From patchwork Wed Jan 24 00:29:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 191259 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp689743dyi; Tue, 23 Jan 2024 16:33:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IEpjYG4vPVQWwpiHV2u1Fz6HSVBgGiWJBcqcd8j27236nhrYHxbHBaShxblPQPosYhQR0bw X-Received: by 2002:a05:6214:e48:b0:686:aadd:9c88 with SMTP id o8-20020a0562140e4800b00686aadd9c88mr1051869qvc.46.1706056409528; Tue, 23 Jan 2024 16:33:29 -0800 (PST) ARC-Seal: i=3; a=rsa-sha256; t=1706056409; cv=pass; d=google.com; s=arc-20160816; b=CB1x6fyVbjoNZ+VW2OYrqhvWQkoV7av/KsT5XFy/JEVwmW4QcOPenLSqlzU/UGk8bE uI1CMFDyCEIMO1wDdDCwG2UntnngN18if+3GBn0tlMltf6YW12yWiJ2kKTy5AHzPLq8T l4G1bWuimV+x8ihk2unHvOw5//2+/LicbpMXVSOS2n3Oj7qgdSKL3hl6ZZqKaW+IXxWA 03vyww643KwLuNDkoYBc9f5VuV0LPoEZj46+CEw2JGtadE9t05MH2kg+2D/8vMAh6jv7 iXkTYgUY9akJNfqpAMZN+IkQ3uU4lfZMAAuRLtBv3TR9Cw8PlY2cG5xg1zn6w5O4hg1+ +CmA== ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version :content-transfer-encoding:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=kxReP94QyGknp+VMmJbR7KCPZyXGLc8N2AkInosVMFw=; fh=CJ4gvMxvfPB25iGbJgp7lolaZhkZrM7MYpkDmawi2Is=; b=u3tiqM7i18dR+H1aNCy3JXmNqp5UV8wU+2lT2/CKwbGYw/WiMplv/uIWB9t1ZPXoOM HWZUpmCYXqQ5aB6L//85YC22qaQuc7deAV63jFHpfBVvcBKdmwAPRIlRZswfHxNjDc7W sMdFGU4LssbgnWC5nxYrf/PFlId4D+U9u095ES7CGKrZHihDKCa+vo5k9HBZy6j5k+uo tjotymXheozNzX7m5J1ccoiRTM3AeuQ4tO0RlpmdPvAdbQPiN0DCPPPH2Bsgxv+aypjc Px++aoP51G/tW0jsadK2EsyeH6ER5KlhPatNl4JPfl5ggEzqCuh1SywPIErmNz6rn9D1 iikQ== ARC-Authentication-Results: i=3; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=bu2Zxm3X; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=xCgWlGJG; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z3-20020a0cf243000000b0067ef8e49371si9088032qvl.297.2024.01.23.16.33.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 16:33:29 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=bu2Zxm3X; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=xCgWlGJG; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3DC9F385E446 for ; Wed, 24 Jan 2024 00:33:29 +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 2DFCA386182C for ; Wed, 24 Jan 2024 00:30:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DFCA386182C 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 2DFCA386182C 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=1706056231; cv=pass; b=nxqv8uJSzeE3JC0qCcAqdtcJGFCLdhkEmkxOX0q2co0rtzJeoFLT3FWlS3d19FTMvcbQO8QqyT2i+b5Vi6bDEHSkQMmLni8V2vFCmtx1YF0Nfix27vh1NavZ/u4woqx/d1x0Z5SAF/K7WocC4uXpecxx7UZVyahqXA0yGG2m+jQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1706056231; c=relaxed/simple; bh=/6xKl1KBwKY6WYrnPV3i5SlauTJcG9gzS8hQtW/kPok=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=I+B9/CNt07e4PFk+ud6RbbAHyrldvMNXQIR8s9FrW5+qNvlQkGtqAf5j7JK69KTLMeTo4rfBzUc7QRvP3Xofxhb1FlE8b3HZFxsrCrqSf0oxGNfskF97W8GlLzbNNFoM8mcKEgrkhICcUeeUsm9eVSH0eg4Q+wovJCoqOK19e+8= 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 40NMcgvf025439; Wed, 24 Jan 2024 00:30:25 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=kxReP94QyGknp+VMmJbR7KCPZyXGLc8N2AkInosVMFw=; b=bu2Zxm3XN+tVTB8AoDqlCjJoVmcXkDIEBtxAYozu50eAQkMqKQwpS5Vz4bhXpSVk+Nxv QXy6nQ6/UoAPSG8AJC5+KRr4msPvm+PMpBLF3rhzng/hr81asJrsxIxCM3EwCwcbRiNT uMQzqyFEGw9HFveTAUM2cJ14MIBfMFVgrJKIgWysrori2Po4h8/QrxiMF9tzOJDh+K/G B47Q4K6ADCCqrgxJpzYESlaLOJUTmhh8maLIp1/EkcUxyobUvWSSolC1qotY41gtR55h gPye17MrjzAGvlCobfh7DD9lqw/eVx2y14SaysSRgvHmMZ71pEu7A6+hr2fL2v7WhxHC 4A== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3vr7abyu2w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:25 +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 40O0Aqwt014895; Wed, 24 Jan 2024 00:30:24 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3vs33ttkfg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=naBJrjKU1kvFI7B49Nh/Oo4lOtM5WzaXj8kGeNlB0jKcoMjrpFF+2cPJvcNyxm56NajC3DksNKVR1YaEBSnCbVcNHlDqdFRBFoy00+ujFRedawi/fKO3xo1653Y0TxgamkDX2ZYjrY9UVXRjaP0tJZbE7xvdOacs1gkrgx/miXJIGpxX/zR8e72F9hMFEISRFLKC8Uzhrc0kGa9NXaMAY7zaTvizQOJ9eQh60/MK+1a/Xq+3n69yD0xqtAbUtsgPyyoysyu+L+zuQA7SVqb0OysoYBzs4QEzldPWkjGcqvbjaQ6ZAzgzxsvoerY/f6kZb46UMbR+dW9KBk0sz/socA== 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=kxReP94QyGknp+VMmJbR7KCPZyXGLc8N2AkInosVMFw=; b=RNR6ZenJH7/2PGDd3Ov0UpoGz0jA9eqZJVq4TXUsij0BpcjLAXLYwOmZtMU/i7aNCCRRLhJv9duKrwfzsGLQvI6XpU2YPbHzS3fk8sP4pUlkNUM3yw+5IyBtKQzBrAQKMpAuMJxyaXDsVnb98HzSW65El2GpVfxFzm7D9x8AkGs41gNWFTp33g23DdzvwmN+Fomy8MnIotyzozAx+3yHRHEOpQLQqdiRQMEHoqnlag2MPYGG3kT0XIho47nUsQbqlumCV35ELT0ay5RQX+ZqaA3+sLnaxfcvdc+IPlNkN0+vrM/x2deIFwGXg6HaMj7zkKMEMw0A3XagOyFftEG14Q== 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=kxReP94QyGknp+VMmJbR7KCPZyXGLc8N2AkInosVMFw=; b=xCgWlGJGUiVRVXbATi7Jykp9cSEY9Qpjdo5kBhXoyt/nVZogTGR0kuVI/rcda+c/x1xs3FzzYbzyfG3pwX0YIvVP4Jh5vfUrZWV9lJU0ReZfhcDjoDuog6X+pOgOfzukcf2phy411ZAsxRZMotBNfN4bztko3S3x6q4FZhaJeKw= Received: from CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) by IA1PR10MB5994.namprd10.prod.outlook.com (2603:10b6:208:3ee::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Wed, 24 Jan 2024 00:30:21 +0000 Received: from CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44]) by CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44%6]) with mapi id 15.20.7202.035; Wed, 24 Jan 2024 00:30:21 +0000 From: Qing Zhao To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v4 3/4] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Wed, 24 Jan 2024 00:29:54 +0000 Message-Id: <20240124002955.3387096-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240124002955.3387096-1-qing.zhao@oracle.com> References: <20240124002955.3387096-1-qing.zhao@oracle.com> X-ClientProxiedBy: BL1PR13CA0205.namprd13.prod.outlook.com (2603:10b6:208:2be::30) To CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7957:EE_|IA1PR10MB5994:EE_ X-MS-Office365-Filtering-Correlation-Id: 3ad00228-cae8-4462-2699-08dc1c73a64f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wFrAeRlVJXM2RuBRBDmOB+rXZJoU7DLwuoRbO+Za25bcP1Qw7te66j7qP4RdoN3+Bf8EzUESsYjlgtUT7BDDcufI4TFubFy0xvFn/FyWJSl1O5+0YFW6dt4K84hxd8DANbzWTeSNs4oGU2x+QH64O+eE5pagASigsXVclVgEVVTaGN8540yLWbbLk98TukXwZdjAIvYhjUA3niZ2Dl6/El2pn/qrQKAJ/E7YaFNbBiwqNbr3MXQ/79IXDdAWia0UlAlGTksjxp41hRZZJXbFplnoPuOZx76AMIM2OFIc10/8tLVDDuUVMWR2g8RbbLKdj9PTdcFgEYlf5ZYg1ShE6IaZqp621voG68U+J7xfm4h49Oe58uQylLt7tCbg1LaQmPzqQlP/9vMvJemyYMKzEPFqVD+4Pq0gbb8wrWtbG24VhVuThyupVsLLNMnJ6hSoyBhWmhlzLrVYBj1otdYnfxxAg6EPo51K3C0Qmn4Z4X4gvdc+ZAqd++XNHVb8I9Yk21H7BnQcWaWOTQjek+ujnx+5bIO4lPFzbICEDbe2cKIBQ6/FvDBoNSJGw5KkYyjgho/G8bDtJJqLDasH3VC8GPdnwurxMVoD54hmafjdg101e6pWXkjug8/2vm4bjwCCjdwQBa9dWZWDpyenN9Ubvg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR10MB7957.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(376002)(136003)(346002)(39860400002)(366004)(396003)(230922051799003)(230173577357003)(230273577357003)(451199024)(186009)(64100799003)(1800799012)(84970400001)(6666004)(6486002)(966005)(478600001)(38100700002)(26005)(2616005)(83380400001)(6512007)(6506007)(316002)(44832011)(5660300002)(30864003)(2906002)(8936002)(8676002)(4326008)(36756003)(86362001)(66556008)(66476007)(66946007)(1076003)(107886003)(41300700001)(142923001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 87NgcMISz7t6324+3nLxIhDYuxx3CWLe7hhtpLsQndi9dE5GKISymXZfzdx8NgYhZT43JegOZ91Mn5QUMYYJE9q2y2q5AkOx5reX/tLsQmfMANSRrOBcmHoouyjbyw2Ds1df+ZpBmEzzHNleLVpuQs+3UmDumBnL6N8Hf5NbvJuKZIG7Be+/cewnm0w0TCk815CqhHTDmevaeP4hqf8SDIW9TeJFOOojzb8dm15xGMHoNNzf11dTcx6b5Q+unKqsbGBZ+u1Y2EyvIsgfgNiDmWyY28BgjZ4B5Pj/cUOk+2MwVa1W3dGjplYBsvqdXWDo3SKfPABWbmk4FuNzM7aN7+4KArfoItJY4ntjiLDCaltKeNJA4DEpI+u5VqFXOrKERj6tzi+DZuArB0sZaddNB0wvpdMcK8WJGkpjBTN6fQrZDMho3qp/K06nV52V0qBaSv0nwFeKHHJUHyA4sGkBZLvSSVLh0r1KJzy+pOFu1IxOuD+eo4ogAOE1OSIEtr18mlY8620TErMwbA7Xg1j+JaCPA7blvtMVRSYHP8fPI9NZtYNPBgi5haKdYgbn5jbq2mTJgxzvjjBf34VSLUE/uMnbsZgv8UE2hkSg2RlyNAGdv5g61F9wAXLz6rWFrHfRYgI8B3Z7gmRQjMDcNs5TdLhsDoGx5VEq2mm/msMH/Z3vo1BHD030RjbodLtS9/bQWao0ojuTTjCWofZFL+2qz2MXgoHPX6blGhWv45TX/+sOpcyVWZrO0fBu/4xjJyrjnh7AaCAw2QEv1RX14q/UktdExa4/PfWjtGB4h6KKIz5mVF8ENg5ba9Ca2xn4avvsFmDoXw35RncaSMulKybpuzMkzAaQWJsDGx6fCoRZhuFgTKian8S3bp9p4xqqv8Mb+4wds+h2L9Shg646tHOx1LsnoGrKpjdO88aCSzr9hmz9QjiLfyk1q71anU4IkxMJJXhSCn9u1Uqr6TKWuW1eigbtXpnVHM+TNQA6hX2wSFZ1YhUHOkjRwokvHUTgRiiyLHB3g3fgQAGA9YgDKfOKe1SxAChKtSLB12DwNYJXxobDjAWlAmoov9I6ZOxnE2ihPcufzMSitG5PBzoGdb1gKF3q0tjQXVfEBUse6DuGkuoUWBRvHceS5qljdxeK/MPUenCi2LEu6Czq35OSNT1VYee5BcCIVjQueTctoNhGyegrJkf2ehwtqSed2yYrikk6ifLpK8fNM/T9p4t3AAoBzrCOA3ZxlOBXxW0DD3vrn+IZKwo0vIyETBrMxqA8pUeZTvpIJQbSGBd9Nf1Az1kBAiun5zDYHQ9OMdvrcMJzIH7fxvN5wdJVvP5dyaTFX4FXqANf8WTHcQOXl0w2MyCZKf4YkIALejdw33QreFaCPHZfWq97xLqTkPUjf6gN7MC7lyplfdLwnexFAZF4Y8XjNvKxjiZ+e74016RvOSx0Qf+UuaAbHQqkFTpO5PhdtpWPOykZ1fNIkMcMc3pGakpwtM6SXMt2I+L7EiBf/TtPCA9O2rbjYDL7geEzE1VtlVEa9oWbCZhQEmp409UMmA0XzNLO9AZ1oBmPdEZMwgdGYR0pB7Y/sCyRTPQBDtZzorxi X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: SOzghq4q7P9hsoaJ83K2Xr0NxWXDpCH+1X+qZq8majo5lGOSwZBVkyy5ifFb5z7ctfQaR1KWSJGCsSBd6/uOZzzYDllJy+PPSSjaH+oeX+vnY2uMl/KVnBi2FIDlUH7yYNXBwRcJfIwQm8eb2yGA1RmXlOwYNsXKxQlbLBBHcdMWWQmmxHC7Pp8F3wqt2ZojNBf1swCPHPRQAfBsbDXwFDjWQU/yECuVOyZRmY3qqx0/aHdHC6SFn+4znO0VRMpQq16CGCuxueA+7lOAr7v5V1njnXUgmsxW9PWqT5Ee3W093qzR37MaEGF4t0j/9CYLNj/1emeIBsjIyRCFO9V5yWxkDNprMrG4mzOSn/4o2wI48ReU2h+MkBExM/y6tVItDNzdTvWHPq1gzB1AsYAChKSDYwV9wQz1rqtE2s74ULZL5qsr4Zx2wUTRGwr0mhCnTVOd0E+fGrokxc/eYrSjI5JHDOYGGBJmKo4YZqeOSMjGwUv+TzY91yJ+zI8C/F0xFhnsiS3pIhLecuq5exWHaBr5AS6eE6lURGPfiReTTTbT86J1g1ea/xf+E6+DE6YjFhRSIaBl8eF4F4ykQ1bKEllWR4Nsd05RyA0nmFzgRxw= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3ad00228-cae8-4462-2699-08dc1c73a64f X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7957.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2024 00:30:21.7784 (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: +/gYGQmUEHkVBnBaypVzbp/p6xQ/YPsS7ilQusHQOG2GxjsCuC/YgKxUH5DXX3utAH9CZ7OCvlo/0B9ixR7g/g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB5994 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-01-23_14,2024-01-23_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 spamscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 bulkscore=0 adultscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401240001 X-Proofpoint-GUID: ifeYumqyJ0myrOFB09gwG51MzbFzUTkk X-Proofpoint-ORIG-GUID: ifeYumqyJ0myrOFB09gwG51MzbFzUTkk X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788929805329507702 X-GMAIL-MSGID: 1788929805329507702 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/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/tree-object-size.cc | 47 +++++ 4 files changed, 299 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 diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-common.h b/gcc/testsuite/gcc.dg/builtin-object-size-common.h index 66ff7cdd953a..b677067c6e6b 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-common.h +++ b/gcc/testsuite/gcc.dg/builtin-object-size-common.h @@ -30,3 +30,14 @@ unsigned nfails = 0; __builtin_abort (); \ return 0; \ } while (0) + +#define EXPECT(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + __builtin_printf ("ok: %s == %zd\n", #p, p); \ + else \ + { \ + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + FAIL (); \ + } \ +} while (0); diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c new file mode 100644 index 000000000000..0066c32ca808 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c @@ -0,0 +1,63 @@ +/* test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + int c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (int)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated) + + attr_count * sizeof (int)); + array_nested_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test () +{ + EXPECT(__builtin_dynamic_object_size(array_flex->c, 1), -1); + EXPECT(__builtin_dynamic_object_size(array_annotated->c, 1), + array_annotated->b * sizeof (int)); + EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1), + array_nested_annotated->b * sizeof (int)); +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test (); + DONE (); +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c new file mode 100644 index 000000000000..3ce7f3545549 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c @@ -0,0 +1,178 @@ +/* test the attribute counted_by and its usage in +__builtin_dynamic_object_size: what's the correct behavior when the +allocation size mismatched with the value of counted_by attribute? +we should always use the latest value that is hold by the counted_by +field. */ +/* { dg-do run } */ +/* { dg-options "-O -fstrict-flex-arrays=3" } */ + +#include "builtin-object-size-common.h" + +struct annotated { + size_t foo; + char others; + char array[] __attribute__((counted_by (foo))); +}; + +#define noinline __attribute__((__noinline__)) +#define SIZE_BUMP 10 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +/* In general, Due to type casting, the type for the pointee of a pointer + does not say anything about the object it points to, + So, __builtin_object_size can not directly use the type of the pointee + to decide the size of the object the pointer points to. + + there are only two reliable ways: + A. observed allocations (call to the allocation functions in the routine) + B. observed accesses (read or write access to the location of the + pointer points to) + + that provide information about the type/existence of an object at + the corresponding address. + + for A, we use the "alloc_size" attribute for the corresponding allocation + functions to determine the object size; + (We treat counted_by attribute the same as the "alloc_size" attribute) + + For B, we use the SIZE info of the TYPE attached to the corresponding access. + + The only other way in C which ensures that a pointer actually points + to an object of the correct type is 'static': + + void foo(struct P *p[static 1]); + + See https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624814.html + for more details. */ + +/* in the following function, malloc allocated more space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? */ + +static struct annotated * noinline alloc_buf_more (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index + SIZE_BUMP) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index; + + /*when checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + we always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /*when checking the pointer p, we only have info on the observed allocation. + So, the object size info can only been obtained from the call to malloc. + for both MAXIMUM and MINIMUM: A = (index + SIZE_BUMP) * sizeof (char) */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +/* in the following function, malloc allocated less space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? + NOTE: this is an user error, GCC should issue warnings for such case. + this is a seperate issue we should address later. */ + +static struct annotated * noinline alloc_buf_less (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index + SIZE_BUMP; + + /*when checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + we always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /*when checking the pointer p, we only have info on the observed + allocation. So, the object size info can only been obtained from + the call to malloc. */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +int main () +{ + struct annotated *p, *q; + p = alloc_buf_more (10); + q = alloc_buf_less (10); + + /*when checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(p->array, 0), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 1), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 2), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 3), p->foo * sizeof(char)); + /*when checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(p, 0), -1); + EXPECT(__builtin_dynamic_object_size(p, 1), -1); + EXPECT(__builtin_dynamic_object_size(p, 2), 0); + EXPECT(__builtin_dynamic_object_size(p, 3), 0); + + /*when checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(q->array, 0), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 1), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 2), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 3), q->foo * sizeof(char)); + /*when checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(q, 0), -1); + EXPECT(__builtin_dynamic_object_size(q, 1), -1); + EXPECT(__builtin_dynamic_object_size(q, 2), 0); + EXPECT(__builtin_dynamic_object_size(q, 3), 0); + + DONE (); +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 018fbc30cbb6..74d778f71eed 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -60,6 +60,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 +750,48 @@ 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 TYPE_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: unknown; + 1: the number of the elements of the object type; + 2: the number of bytes; + 4th argument PREC_OF_SIZE: The precision of the object refed 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 type of element. */ +static tree +access_with_size_object_size (const gcall *call, int object_size_type) +{ + gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); + tree result_type = gimple_call_return_type (call); + gcc_assert (POINTER_TYPE_P (result_type)); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (result_type)); + tree ref_to_size = gimple_call_arg (call, 1); + unsigned int type_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); + unsigned int prec_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 3)); + tree type = build_nonstandard_integer_type (prec_of_size, 1); + tree size = fold_build2 (MEM_REF, type, ref_to_size, + build_int_cst (ptr_type_node, 0)); + + if (type_of_size == 0) + return size_unknown (object_size_type); + else if (type_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 +1393,12 @@ call_object_size (struct object_size_info *osi, tree ptr, gcall *call) bool is_strdup = gimple_call_builtin_p (call, BUILT_IN_STRDUP); bool is_strndup = gimple_call_builtin_p (call, BUILT_IN_STRNDUP); + bool is_access_with_size + = gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE); if (is_strdup || is_strndup) bytes = strdup_object_size (call, object_size_type, is_strndup); + else if (is_access_with_size) + bytes = access_with_size_object_size (call, object_size_type); else bytes = alloc_object_size (call, object_size_type); From patchwork Wed Jan 24 00:29:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 191258 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp689488dyi; Tue, 23 Jan 2024 16:32:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IEc7+qfg34VitqgOJvyLWCxd8g6T31IOqd962YZkPtxScSSraJbVCxebHiTDClNfuLYAmoR X-Received: by 2002:a0c:f054:0:b0:680:c1e5:e488 with SMTP id b20-20020a0cf054000000b00680c1e5e488mr1603456qvl.36.1706056376350; Tue, 23 Jan 2024 16:32:56 -0800 (PST) ARC-Seal: i=3; a=rsa-sha256; t=1706056376; cv=pass; d=google.com; s=arc-20160816; b=Ji330AvO+mYq6XO/u6L6Pt4cAxh7TZnAA6kIUbvzsm1LaXD8vvz7dCnAxnx7WKYD/2 LLbPMCi2cfJt78g9u8LilcvTOUEr3khYtfpF2A0655vI2guC+SQWT5Po4nbOkF25K2ZL nDgpk6fyQoNC1oM3nE0BVPVGszmRdDy7YxA4Lunf2s/R+dsON9RMjLVQ16OBEQs8nXtA 1slodnX/dgU4ANjkfrdXq+NM/r4y7BLnbWpEohtVMLwB9bq0+UftHI/0yNmLejDQPD3H F0ZHarNfZXNwF9aM5mpn4Hm6G7LNup4I5yzh8TbbJJCAZXIsbKBOKgtYQ6HNHijmsIc5 qyaQ== ARC-Message-Signature: i=3; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version :content-transfer-encoding:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=K88RStHMpvPAztc3mwpnGPHPq7BSeiP+vi3+4FdpGoY=; fh=CJ4gvMxvfPB25iGbJgp7lolaZhkZrM7MYpkDmawi2Is=; b=Qs4AMK0FT/u5rno2fts57fw3SMkYfw2EImWmSFwMd47/42QYUNw0PfNzdX+H8VQfx4 WeeONJiwI0Bmqzz68wriarWTskhLsYPK8rOke73KdyU2I8OJxwYLNaQbgUY2Z56FEfVu PBBmFgqz+Z0fVXwR+sE7s9/ABKRFPVIf2kBb4sELZY7GoLzLx8r5m1jSoa+wYYONTNWD Ds8prh0s3UY5Fnc/KnDbFVGqlaTS8mijtjwKAIWEdz52cUClL7XYUQqnAvIureVaBtu8 ztWJvFqg5i3V9rVhO9YCB9tAZZiuH5hNCh/LzO2MbkjQ8IMfwohlL0ahm8IEH/qlkOH/ dXow== ARC-Authentication-Results: i=3; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=fO5z3VBz; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=ahg5VA6Y; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id o14-20020a0c8c4e000000b00686a0c905e7si2354311qvb.181.2024.01.23.16.32.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 16:32:56 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b=fO5z3VBz; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=ahg5VA6Y; arc=pass (i=2); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 09F92386185B for ; Wed, 24 Jan 2024 00:32:56 +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 B8503385E02A for ; Wed, 24 Jan 2024 00:30:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B8503385E02A 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 B8503385E02A 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=1706056237; cv=pass; b=HrrabnyE8vbkIvAQUJlggLvONPMrxik8uUb2RuOJCAoiyXwC8wlKN47eu13YXfPB0oUp/lEcdwfO+7yhN2ScssM86h32n4CPRupUC2rk7Da+PCmCl1oMWGM7gm+y3ycufLCoZ29LNLZef90kDz8SSuuNhmLd/Iy8JYlXQAcl/cU= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1706056237; c=relaxed/simple; bh=+BFs7d9iztmRV+L0vetnfVUop4c7Y4pWqalgpa67NrE=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=GKcBW+bRYgdR222bot3qmkkaaYZ17JanmZQgMBhBlsWM2cWJrrU9ueaKfcWHo3HqocAH5yz/LEoEPAkhOBBnIYkgSnOKtbZEADxZQF3/6REFSUWl4zlbZPwYq15S41i4rCaj9t0GbuJW1hrS/UNnq5aQUY2Le6qvVLnBH2T0zow= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40NMv0st014414; Wed, 24 Jan 2024 00:30:29 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=K88RStHMpvPAztc3mwpnGPHPq7BSeiP+vi3+4FdpGoY=; b=fO5z3VBz4g/dZGRNgYBoyjyNh3Mv5Un/aBRofTDrlYRidoeFUTpot1+VbIribrUsdp7t n+3VZS399xCCKwJd8g/q2PDqnj/oO018TIQU07fWCZj+jxcvCdcexHmnURmcpyYQNdOf E2nb4V8VJlDgHPJEWEDM4CqbjLv9N7Pd9LAwUShpz2nE7gMng9KRpqzBxxZ9gSs/f3fu GG6AbBhVLH7ueBfoQoDKXsVKBJJnBA4jyZqVJhC5sYoLR9sqzjCBozL9mKjwYGbLnx1J U25TVTdoGungiu68Dtk/mxQx0NkDDeTYm7nTS65ierH/UZY4M5M88hZ+ya6MRODkTqxC zQ== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3vr79nfux2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:29 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 40NMkOOq001150; Wed, 24 Jan 2024 00:30:28 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2101.outbound.protection.outlook.com [104.47.70.101]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3vs3235bdg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 24 Jan 2024 00:30:28 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TljV4iL0IUsGCl0wSDIKkdd7ukUPEZe1PO5+g+XFHZtdnCi3xa0EAI/39TxzMoaz0rRZBWRlrC/W1dfGGsJuUd/Say6CbVWnoE3GNJkkJ134MJMzPGjM/Ui1nPypIITH15pyzhxqLbyywKcHa7TNz+XESY9J55uC289yXCsvUGsv388j2JLisoESU9bNNHHD4autkIPfzjuQVJSw2PyWiN8gnV1axm6618CHZkZPqPPBbDlZqdsuaZ9iTO3SuAzpmg71TPj4AATuJUUfQW0xZIyCtFm1hJeMaLVfMPIO1kx/m2dhqIq8FlKlz4lpp38E9QbOJr3VLwASOu9ljwwXsQ== 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=K88RStHMpvPAztc3mwpnGPHPq7BSeiP+vi3+4FdpGoY=; b=EOlpBaLEIiAEcgzoejHFtMb2WUf0im0qzPIFPET0cFU4eWlsadA5tmNuLFB6LhYYJ0nsbYs80BRBvSGxKcVK16kkyDAnVjETsa6rIoyqNL3tfrCB9LGABBHtczbZMw0hcJPNVN24+eYk/ycTwEffFPuaHNGpXj4TgvbNN7ex4NBNDeAtVDVdAHgmH7XEuEggcIPVW8YrKUtzHTAeQhM0GMjuq8eOfJL5hMFivTvpPGC8+owGkbVwsZF4v+Rz7AKv9lQXwuPbeG24PmVISwfsFCaDRD7QM356oDjaacj8CkInnBT1q3lbEzqzr4Mmw/DxrjY2yPdTY6gAdLcnDkS9Xg== 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=K88RStHMpvPAztc3mwpnGPHPq7BSeiP+vi3+4FdpGoY=; b=ahg5VA6YvMS7aO6xFaX7SYJ2a5legyKuksWu9wCusUrQGbgzafrn6j8aiautlN+tIt30LiJzvmqDitx/WqvdaK8RWexyfOmuBAWKaSRZRPn/xi+d8RgdyIHIlIY4p0kCIpYsMSpkISQSUUw/t/tzBP1U7IhtoFT9hVU8jZJMqZU= Received: from CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) by IA1PR10MB5994.namprd10.prod.outlook.com (2603:10b6:208:3ee::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Wed, 24 Jan 2024 00:30:26 +0000 Received: from CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44]) by CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::6234:e7a1:b517:c44%6]) with mapi id 15.20.7202.035; Wed, 24 Jan 2024 00:30:26 +0000 From: Qing Zhao To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v4 4/4] Use the .ACCESS_WITH_SIZE in bound sanitizer. Date: Wed, 24 Jan 2024 00:29:55 +0000 Message-Id: <20240124002955.3387096-5-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240124002955.3387096-1-qing.zhao@oracle.com> References: <20240124002955.3387096-1-qing.zhao@oracle.com> X-ClientProxiedBy: BL0PR0102CA0005.prod.exchangelabs.com (2603:10b6:207:18::18) To CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7957:EE_|IA1PR10MB5994:EE_ X-MS-Office365-Filtering-Correlation-Id: b9e5ceb0-aef8-4914-c790-08dc1c73a8e6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7jXYlq7GJ58F8Ee5pQNj4/WuzOtqUpRAk031VkdX0PhyORSwVGg3beVTC3rn2ft0SlZ3rTyeIZ+wskkqsaOucnl95CzroXE2tocYSVUGVClHc248xxYVRxAaETldKRVJqjGLfMVaVyX2SWscG2G0JecpBtmIWg1o3ObN4lv5ZDxlGBEl9KJeHQKV7Yg94X8pDYjRUVRTd2Uz794qMdhbaQf/ulJ0K+fGgLxMm9K4FmRiNyubYG6KmuPJQxrReOHdYPdjjkhWqlgT+SrplUW6+YXuOvVagctKLFJRiIbQTimOmb0/0dWBG2VcFu3xrIfDHD3s12yA2gXW9iZcL2Ad+T2SAsF5hEseBT2FWA/aJeIkVLMe0n/See8sXnxpU5E28lb3rS4oZFNt3/UrR5PtUmFWCu0V9YZxOZ0RFzVIeIJwDSBw86dNbYOehMDKdi0cef/ynBAOLV1U2tyMrWKifz3sKVPwo6OFkJbpR7iEn3CRdBkJM7bKl193Qw8texF4oiRVsXcG2nROPwUQrgaOAmX84KY43uoeau8K9t5Blo6g8rEh1laZPpCC75/FbfTq+wTdlGfDfWsUYLwazBUNJdaV4o5qPHatBH4+QQgpV44= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR10MB7957.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(376002)(136003)(346002)(39860400002)(366004)(396003)(230922051799003)(451199024)(186009)(64100799003)(1800799012)(84970400001)(6666004)(6486002)(478600001)(38100700002)(26005)(2616005)(83380400001)(6512007)(6506007)(316002)(44832011)(5660300002)(30864003)(2906002)(8936002)(8676002)(4326008)(36756003)(86362001)(66556008)(66476007)(66946007)(1076003)(107886003)(41300700001)(142923001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qhQPMG3aKfjzlq5eoLhzSFufzDJq9uAKzkVPhiHVmFqZR4d921wicqpEAjpJfvfDsrxn6NhzcpdnlPRxggFbzQQ8CwItRrZXrU+Sw0AiaQF8QYxdMJK/3GXMAXc3hc/om0pAd3ekyND65svySpQpAw6ZR9qfyB5tTEXILoFDUmgIRb1aAPkUHr01Ahr8Kz+exMDqalJsuTq3GoVd0YPNL6ts6Dg6Anm7ixSH3K32MKEMcCxYpRNI9tuhe1Qucxuw6JARkC0L0yqAbxHhYvQwUxuSIZE4KIoi/0d3kPQ5HEiYnC1XtgGHv+Z1s0hd6GXADaExcDZzhZHNF2wxUaWbqb+2JngAJWZFlRY7BPkRSph4llVNrc0kl0T3xRUTKh7PjFLBqSGJDzXgAcTqE89AlNHe/xrypJKQjS7T1MnNa+WvVx6emyw8ou+EpTV9Lsj3i9VFt0AYvE+DAEoc5ikZ3i4nIh5od9Z2GO4sn5LzXQy54s9BNtoCu8Jo3Ljims0DhVXujbRDE17kUwDHVDF9FYBgcVUuC2ubU/EDDOfxlNGTyPHM4Tu+xzEcKobpkw/wTdqKvyZBMjNKr2gs0r7zf/Kp4jUfknYRrwOWScoIKpcuPiT+rMe2/L/PYIG4no5Qp0MjjzWCARu9umDseTLJItD1GPH0ovXhyofWrinnmSlrLvkTd1YYKXTnd3guo1j6VxyA9P2UToplKyhmPNpYiPtzakKoHptocfGMgXaPl4AZemqDI9i4+l07wLhB+qXMYSvUTX8+NjRuo7h9PZ9Lb3oCq5oycZHoNhtVl9T4m1oSbhYlBG0m01RNu5Zd9k3eUAlQg546oUgMREZ00TMdkFQ37KKe6uzPzPW6riRuEluOyNu3Rb77g0SFqZSNBQZp59DwmVEYkps4MGcqV1D+jhZdnnXZKphzZc72S4ChK2jTSDREy1WtniSjjzupEpJPDpjCaZ6NN94GZ46QdjJnrtb1YVOzryWejQqnG5uNVFdz7P1b5xMmO/hjGgoGIBoocDx1kM0E9Q6GYzSB5uamepkcnPEoW4kVfsQTuXRoTtRx5e8frMs7F+jz0HGl601jPZGW/YpDsLwE2L/bwG/7yfskAvyA7pR/IuoR3Sk76Gj3g2Zhl3jJmAOznVMb7IUaANf1C/T/edt8TGPlTrcvwMBQf+/oMOMZvTLIDhMVJRTQAvGK4efdh7TpvG9Q8fwKxDMZ4NbHkTNyGID5lgT7WtS4/0lOnTljZ/+QWcX5SzDL6xZX9wGJ5CsB7BDYqR85zEF7f1TreRCoBDouDKPnSahwglFfkJ7JWk8//jmguXiF/rF1uMMFybxHBW4QHGxyHJqRHlst6qXeRdYcDUR/Bo1uvTrRyrYFI6KuTrEKTUJpmpixEa1OSQfhCrSvcn8DGWiR5IOwEifRzcZT7iuV12HPIDmV5w8bq4Ptc0vB+ibYcibB1An5FdhMm8VxixaAOetKJcBKKlXQHF1ZbLNjWWUoMxoftwXs0DO2yGYwh/5ia94TX29EdmnHdGzEfe7G0Kmx2oN8F0WsJLxZqQyJt6lULMkwuOvXHjGZ4eEdQeymsUdDBgwDypTdLMXPkeeL X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: I3mroFNvaes48Vawk/J3gg1t9QMYq6KHZnyUFMUxxbpF4RhMRJ5ELgZWccvt4mqhEhQJhjpUzGCzyGvkJ4u+qeyyv7kxVhw4DdbQvQ4IWArYNyJ1zOgWqogBlHsGgXr4Z+9L01v0j5ThPpHl85uqNawlxkxAdaP8Tiw/RsQx2o1uQ5EPLQ83vPRj619gA2Bt3m7qu9XhDjE8tMdtd9kot1lfK3WjWLruSiYbdMqCnoOX7Lt39T5rgoacZWTrwssXx5jvEDjRzK4/lXcHSTHLU+YaXcRr5czLfxxggdp1iHki+dat2BF8htoIOYIcmoEsBpsxxxlwlpR1jwmsgRL8MW1PNA8hfQ7q7bLE1jMnCiwj1P7JBRw7hCEjkHrfIgmBxvpwAuHkHtBgQ/S9rlhasaE4Er+p/V9o7uRTHLwC8XbsH/oESbhLDNkYlv1VGAzoOerjjJ0GxDT0AOdUIvIJqHWWQZBaWR1V/G2Pmua96InIHytAnbn6OpUiM7LGm6E23FliO5fda5ZcFRWbKe62bAWarZXrYGfN22w9EYcgFlmP2V485wglUumbLCPpaCousHxchzUwsPWqHnWD7ykN/AcKJlvQtxNkctUyLzt/HtY= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b9e5ceb0-aef8-4914-c790-08dc1c73a8e6 X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7957.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2024 00:30:26.1257 (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: qsoz8nh7fgn0j/hAmYl6sxZ2CfRVUnOdnaKIPtMGYVO6thdN3qg7WOKSmItWrqKxVzDSF8YeBb8UEmFsh9Eo3Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB5994 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-01-23_14,2024-01-23_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 suspectscore=0 phishscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401240001 X-Proofpoint-GUID: 9UWOkYGWNFC9ib2b64P50QUAm7-AkT9c X-Proofpoint-ORIG-GUID: 9UWOkYGWNFC9ib2b64P50QUAm7-AkT9c X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788929771148399800 X-GMAIL-MSGID: 1788929771148399800 Since the result type of the call to .ACCESS_WITH_SIZE is a pointer to the element type. The original array_ref is converted to an indirect_ref, which introduced two issues for the instrumenation of bound sanitizer: A. The index for the original array_ref was mixed into the offset expression for the new indirect_ref. In order to make the instrumentation for the bound sanitizer easier, one more argument for the function .ACCESS_WITH_SIZE is added to record this original index for the array_ref. then later during bound instrumentation, get the index from the additional argument from the call to the function .ACCESS_WITH_SIZE. B. In the current bound sanitizer, no instrumentation will be inserted for an indirect_ref. In order to add instrumentation for an indirect_ref with a call to .ACCESS_WITH_SIZE, we should specially handle the indirect_ref with a call to .ACCESS_WITH_SIZE, and get the index and bound info from the arguments of the call. gcc/c-family/ChangeLog: * c-gimplify.cc (ubsan_walk_array_refs_r): Instrument indirect_ref. * c-ubsan.cc (get_bound_from_access_with_size): New function. (ubsan_instrument_bounds_indirect_ref): New function. (ubsan_indirect_ref_instrumented_p): New function. (ubsan_maybe_instrument_indirect_ref): New function. * c-ubsan.h (ubsan_maybe_instrument_indirect_ref): New prototype. gcc/c/ChangeLog: * c-typeck.cc (build_counted_by_ref): Minor style fix. (build_access_with_size_for_counted_by): Add one more argument. (build_array_ref): Set the 5th argument of a call to .ACCESS_WITH_SIZE to the index. gcc/testsuite/ChangeLog: * gcc.dg/ubsan/flex-array-counted-by-bounds-2.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds.c: New test. --- gcc/c-family/c-gimplify.cc | 2 + gcc/c-family/c-ubsan.cc | 130 ++++++++++++++++++ gcc/c-family/c-ubsan.h | 1 + gcc/c/c-typeck.cc | 14 +- .../ubsan/flex-array-counted-by-bounds-2.c | 45 ++++++ .../ubsan/flex-array-counted-by-bounds.c | 46 +++++++ 6 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc index 494da49791d5..25a3ca1a9a99 100644 --- a/gcc/c-family/c-gimplify.cc +++ b/gcc/c-family/c-gimplify.cc @@ -108,6 +108,8 @@ ubsan_walk_array_refs_r (tree *tp, int *walk_subtrees, void *data) } else if (TREE_CODE (*tp) == ARRAY_REF) ubsan_maybe_instrument_array_ref (tp, false); + else if (TREE_CODE (*tp) == INDIRECT_REF) + ubsan_maybe_instrument_indirect_ref (tp); else if (TREE_CODE (*tp) == MODIFY_EXPR) { /* Since r7-1900, we gimplify RHS before LHS. Consider diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index 940982819ddf..7bb6464eb5b5 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -376,6 +376,7 @@ ubsan_instrument_return (location_t loc) return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data)); } + /* 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. @@ -501,6 +502,68 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, *index, bound); } +/* 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 type_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2)); + unsigned int prec_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 3)); + tree type = build_nonstandard_integer_type (prec_of_size, 1); + tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size), + build_int_cst (ptr_type_node, 0)); + /* Only when type_of_size is 1,i.e, the number of the elements of + the object type, return the size. */ + if (type_of_size != 1) + return NULL_TREE; + else + size = fold_convert (sizetype, size); + + return size; +} + +/* Instrument array bounds for INDIRECT_REFs whose pointers are + POINTER_PLUS_EXPRs of calls to .ACCESS_WITH_SIZE. We create special + builtins that gets expanded in the sanopt pass, and make an array + dimension of it. ARRAY is the pointer to the base of the array, + which is a call to .ACCESS_WITH_SIZE. + We get the INDEX from the 6th argument of the call to .ACCESS_WITH_SIZE + Return NULL_TREE if no instrumentation is emitted. */ + +tree +ubsan_instrument_bounds_indirect_ref (location_t loc, tree array) +{ + if (!is_access_with_size_p (array)) + return NULL_TREE; + tree bound = get_bound_from_access_with_size (array); + tree index = CALL_EXPR_ARG (array, 5); + /* When the index is a constant -1, it's an invalid index. */ + if ((TREE_CODE (index) == INTEGER_CST) + && TREE_INT_CST_LOW (index) == (long unsigned int) -1) + return NULL_TREE; + gcc_assert (bound); + + /* Create a "(T *) 0" tree node to describe the original array type. + We get the original array type from the first argument of the call to + .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, num_bytes, -1). + + Originally, REF is a COMPONENT_REF with the original array type, + it was converted to a pointer to an ADDR_EXPR, and the ADDR_EXPR's + first operand is the original COMPONENT_REF. */ + tree ref = CALL_EXPR_ARG (array, 0); + tree array_type + = unshare_expr (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (ref, 0), 0))); + tree zero_with_type = build_int_cst (build_pointer_type (array_type), 0); + return build_call_expr_internal_loc (loc, IFN_UBSAN_BOUNDS, + void_type_node, 3, zero_with_type, + unshare_expr (index), bound); +} + /* Return true iff T is an array that was instrumented by SANITIZE_BOUNDS. */ bool @@ -536,6 +599,73 @@ ubsan_maybe_instrument_array_ref (tree *expr_p, bool ignore_off_by_one) } } +/* Return true iff T is an INDIRECT_REF that was instrumented + by SANITIZE_BOUNDS. */ + +bool +ubsan_indirect_ref_instrumented_p (const_tree t) +{ + if (TREE_CODE (t) != INDIRECT_REF) + return false; + + tree pointer = TREE_OPERAND (t, 0); + if (TREE_CODE (pointer) != POINTER_PLUS_EXPR) + return false; + tree offset = NULL_TREE; + if (is_access_with_size_p (TREE_OPERAND (pointer, 0))) + offset = TREE_OPERAND (pointer, 1); + else if (is_access_with_size_p (TREE_OPERAND (pointer, 1))) + offset = TREE_OPERAND (pointer, 0); + else + return false; + return TREE_CODE (offset) == COMPOUND_EXPR + && TREE_CODE (TREE_OPERAND (offset, 0)) == CALL_EXPR + && CALL_EXPR_FN (TREE_OPERAND (offset, 0)) == NULL_TREE + && CALL_EXPR_IFN (TREE_OPERAND (offset, 0)) == IFN_UBSAN_BOUNDS; +} + +/* Instrument an INDIRECT_REF, if it hasn't already been instrumented. + Right now, we only instrument an INDIRECT_REF when its pointer is a + POINTER_PLUS_EXPR, with one operand is a call to .ACCESS_WITH_SIZE, + and the other operand is an offset to the array. We will compute the + array index based on the offset and the size of each element, and use + the computed index for the instrumentation. */ + +void +ubsan_maybe_instrument_indirect_ref (tree *expr_p) +{ + if (!ubsan_indirect_ref_instrumented_p (*expr_p) + && sanitize_flags_p (SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT) + && current_function_decl != NULL_TREE) + { + tree pointer = TREE_OPERAND (*expr_p, 0); + if (TREE_CODE (pointer) != POINTER_PLUS_EXPR) + return; + tree array = NULL_TREE; + tree offset = NULL_TREE; + int nth_op = 0; + if (is_access_with_size_p (TREE_OPERAND (pointer, 0))) + { + array = TREE_OPERAND (pointer, 0); + offset = TREE_OPERAND (pointer, 1); + nth_op = 1; + } + else if (is_access_with_size_p (TREE_OPERAND (pointer, 1))) + { + array = TREE_OPERAND (pointer, 1); + offset = TREE_OPERAND (pointer, 0); + } + else + return; + + tree e = ubsan_instrument_bounds_indirect_ref (EXPR_LOCATION (*expr_p), + array); + if (e != NULL_TREE) + TREE_OPERAND (pointer, nth_op) + = build2 (COMPOUND_EXPR, TREE_TYPE (offset), e, offset); + } +} + static tree ubsan_maybe_instrument_reference_or_call (location_t loc, tree op, tree ptype, enum ubsan_null_ckind ckind) diff --git a/gcc/c-family/c-ubsan.h b/gcc/c-family/c-ubsan.h index 9df03445a2ba..ed495266e82d 100644 --- a/gcc/c-family/c-ubsan.h +++ b/gcc/c-family/c-ubsan.h @@ -28,6 +28,7 @@ extern tree ubsan_instrument_return (location_t); extern tree ubsan_instrument_bounds (location_t, tree, tree *, bool); extern bool ubsan_array_ref_instrumented_p (const_tree); extern void ubsan_maybe_instrument_array_ref (tree *, bool); +extern void ubsan_maybe_instrument_indirect_ref (tree *); extern void ubsan_maybe_instrument_reference (tree *); extern void ubsan_maybe_instrument_member_call (tree, bool); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 4020bafc8e36..4fcb99fa0a5d 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2576,7 +2576,8 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) { tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by)); counted_by_ref - = build_component_ref (UNKNOWN_LOCATION, datum, field_id, + = build_component_ref (UNKNOWN_LOCATION, + datum, field_id, UNKNOWN_LOCATION, UNKNOWN_LOCATION); counted_by_ref = build_fold_addr_expr (counted_by_ref); @@ -2602,11 +2603,15 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) to: - .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, num_bytes, -1) + .ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, num_bytes, -1, INDEX) NOTE: 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. + + INDEX is -1 when we build the call to .ACCESS_WITH_SIZE. and + will be set to the corresponding tree node when we parse the + index at build_array_ref. */ static tree build_access_with_size_for_counted_by (location_t loc, tree ref, @@ -2619,12 +2624,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 (integer_type_node, counted_by_precision), + build_int_cst (integer_type_node, -1), build_int_cst (integer_type_node, -1)); SET_EXPR_LOCATION (call, loc); return call; @@ -3006,6 +3012,8 @@ build_array_ref (location_t loc, tree array, tree index) gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE); gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE); + if (is_access_with_size_p (ar)) + CALL_EXPR_ARG (ar, 5) = index; ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar, index, false), RO_ARRAY_INDEXING); diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c new file mode 100644 index 000000000000..148934975ee5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c @@ -0,0 +1,45 @@ +/* test the attribute counted_by and its usage in + bounds sanitizer combined with VLA. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ +/* { dg-output "index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 20 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 10 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ + + +#include + +void __attribute__((__noinline__)) setup_and_test_vla (int n, int m) +{ + struct foo { + int n; + int p[][n] __attribute__((counted_by(n))); + } *f; + + f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n])); + f->n = m; + f->p[m][n-1]=1; + return; +} + +void __attribute__((__noinline__)) setup_and_test_vla_1 (int n1, int n2, int m) +{ + struct foo { + int n; + int p[][n2][n1] __attribute__((counted_by(n))); + } *f; + + f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n2][n1])); + f->n = m; + f->p[m][n2][n1]=1; + return; +} + +int main(int argc, char *argv[]) +{ + setup_and_test_vla (10, 11); + setup_and_test_vla_1 (10, 11, 20); + return 0; +} + diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c new file mode 100644 index 000000000000..81eaeb3f2681 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c @@ -0,0 +1,46 @@ +/* test the attribute counted_by and its usage in + bounds sanitizer. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int annotated_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + annotated_count * sizeof (int)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int normal_index, int annotated_index) +{ + array_flex->c[normal_index] = 1; + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (10, 10); + test (10, 10); + return 0; +} + +/* { dg-output "36:21: runtime error: index 10 out of bounds for type" } */