From patchwork Fri Feb 16 19:47:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 85904 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 061CE3857357 for ; Fri, 16 Feb 2024 19:48:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by sourceware.org (Postfix) with ESMTPS id 0AC46385782F for ; Fri, 16 Feb 2024 19:47:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0AC46385782F 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 0AC46385782F Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708112869; cv=pass; b=YMDX8BImsigvEAyua4AzSj/lsA9c0kWg++yD8w6yGMdzljaNjnm8AlTTGuVkGnH0VVK5GlqcrQr3QMaIPtC0jSYBzE4OGZ/IYSaJMpXI6AESHKpQHWUkdlSD7ojfbKijLjGsH1VIKSkzY4G0D51vZDfx5gOR1opSE+sH9kJckOQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708112869; c=relaxed/simple; bh=VzpExqZHbepKVW1bxQtA10MU0l2s1C4upTfCdeclWsk=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=enHsNAoJM/wBl357fK/fSSQSpI7hWWn6VSoisTd8iad8Py+Xadie3k6DUsC9kOwSr9RVfDs2g4nI9LthkwOE3MyDAUfdRKtFV2nwmm0h4b5AILeuNrKTSGSAvdBoQbSY1E8FF0F8C47KcsdD1inakBYmAqGtWEFIQ2OgCppDX5o= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41GJlecd015652; Fri, 16 Feb 2024 19:47:45 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=MGC6C0sZKYOPwQAo3h6Nf4l/lFhhzCERxTC45epo1iA=; b=N1mOtA9Dj+bt8IMptnaMpPS81PdTFF4sZIoz0xSW7kIn74guVQi7XdAJ+6wtBBJFMd5j B3TvmJUum9aW3douyupC/QhC0LyH35oZEU+APvwBnVqnu83MIncX4FySvUz2AU1WYV/A rr6xW8+WleQEwhbk2cxoI1QN1pTE3GWkiI/ZcEasK49/9q1eptBFbUSotOYbhIPEDyHK T+l/E/bUxyIF0OI6bKVy5hjp42pNDw9t6EIl5UxnTagitKlnAJNtq59JHija2gaWo5Ds HMmpiT3ELja3wE3O6C0bIGX31P3c6qRgMGrdx0mgd21GIkaJ31PZ2CtYoL9bz9bw2WBH uw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3w92db5y4h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:45 +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 41GIsBfm031331; Fri, 16 Feb 2024 19:47:44 GMT Received: from nam02-bn1-obe.outbound.protection.outlook.com (mail-bn1nam02lp2040.outbound.protection.outlook.com [104.47.51.40]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3w5ykcky44-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BTpCKb6ptSbtdcgs5Bgg4jgMghEV4Mj3KFslLQkDmzKVYx7uDSj7Vuu2IGbgtyAPm5JsMGL8Pv3pBZ/d6jQPJr5yLEyiieP7ANNeKPqwT3ORsmZe8WumIJnTTfTlf1BdEqstrEeO/DejPSMCjhf2PO1gfWVtSw+52UxuOlHqmBS8pEI07u8pJeNkm6UqphJ0p24b1RSmLNpSR0Yj/UW+e/1k6zryzhIy/NoJCSH8qYPVq7YOHemH/V9ZeqLjmLDvy6jAYMFoBNluE9vZLvy4CGDJLJjFjP467xi9YxmHXN/tjX3ciNXxtquW3u9DDqGJwUs+O7+lKZrCqqq5av5ROQ== 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=MGC6C0sZKYOPwQAo3h6Nf4l/lFhhzCERxTC45epo1iA=; b=RPTY3ZPHNbRAhXOpzpXteToxs/fPXW99+6pg6FNuNiQMRHjDxfq5QEInwW+QEkFa6+PYcw2xNqZL9TZjQkpdvOdD7rXcvy574lahwDrhgQJgTkLzD+R2UEzjVN5iLHo3SGoaXeWEdIZmDaUdcYc0RIKx7l5CgpU82j65j2C22J2l0Q1xPQDYcSpP0JM/y0KOAkiZPWle0gZBtNw7oys/z2kKw3lJdmSWt8wpAixCSm8KBTSxaWEW2JKLm7048K62omw2vvoHlRhw3k9JYTXN8Qq0YxS4dDjOS0CAERTqxKpcnRHv0knuG2gjlxpiJln3ux7nDfRjbd2lU5k/vyoerQ== 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=MGC6C0sZKYOPwQAo3h6Nf4l/lFhhzCERxTC45epo1iA=; b=tQkCHfTHpPK92I2iQZIzvUI9X10RGyy9BK+kuxA+G0TC+1oKfw0RlILbTd34Yyfy8CV2V8xCc87VbSNZqogUmtie8elbV94OzcVas4xzN61H8jzssoEK4TUYaZo1oU31QxJcRsZDf4AlWHJgWi8oSCLWQ4/OjFbHmUr+WD3au78= Received: from CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) by IA1PR10MB7238.namprd10.prod.outlook.com (2603:10b6:208:3f6::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.28; Fri, 16 Feb 2024 19:47:42 +0000 Received: from CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::1e58:cfe8:d9f5:6363]) by CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::1e58:cfe8:d9f5:6363%6]) with mapi id 15.20.7292.029; Fri, 16 Feb 2024 19:47:42 +0000 From: Qing Zhao To: josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org, Qing Zhao Subject: [PATCH v6 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Fri, 16 Feb 2024 19:47:21 +0000 Message-Id: <20240216194723.391359-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240216194723.391359-1-qing.zhao@oracle.com> References: <20240216194723.391359-1-qing.zhao@oracle.com> X-ClientProxiedBy: MN2PR06CA0017.namprd06.prod.outlook.com (2603:10b6:208:23d::22) To CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7957:EE_|IA1PR10MB7238:EE_ X-MS-Office365-Filtering-Correlation-Id: c3fc58d8-c90b-4c32-4ccd-08dc2f282381 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dESxd8YqEhnjajf8hHgmvqf9+xm1BlK9gAOQHW5Y3CgFbHYGiw8ezkmtgci3UYPVj5yugxZSFqQsGQnSyyeH5e7WYfd/3KmIwCJ/1+G+byL0BeG4m/uAYvaa65sETvnBhs+v5WiX+YVkQ8DcAcOKpP56TUTBl3tlA7CPSf2zAa5iXD8+7rU+73mI+85k5Gw8HFq0U77ZwGtejJrIE1D64Cavd4OdCiTg+MY8kuRmEk2TTh+XuHXH+QLS8OaXeIloKJrQbjCg2bnmbBbtrC8R7KoP8ecv8HgDPHvxZ2d2Q90cKrQDmRoma+SIXFyHwxAFB4fjxO3sbtOFXJONPJTbzjKSCzjvrMv29ruRTY7AY4nkVSKbma81oBglfMqlJWVl2f3EI4yjh7IvXFYVZ1GzQoDrSMzHlJYCQGF8fHCbiMt97I9PKx2FPxlbXBOybEGUBpBFUPAXASZYdROnuabgIxMuRvUjPsXznsvOZGSC/Jvkk2rJM71bCFTSu9sXGWMl97Sgu67SlEfcEbBI7LPwRcybjBt6l61res2IGmE6Jqs= 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)(136003)(376002)(39860400002)(346002)(396003)(366004)(230273577357003)(230922051799003)(451199024)(1800799012)(186009)(64100799003)(4326008)(8936002)(8676002)(5660300002)(6666004)(2906002)(30864003)(83380400001)(86362001)(107886003)(1076003)(36756003)(38100700002)(26005)(66556008)(2616005)(66946007)(316002)(66476007)(6506007)(44832011)(478600001)(966005)(6512007)(6486002)(84970400001)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2r4fAqU3OLE9zhBwwxPReGSo/yozCTDHTuudV8yMoVr2evDcXnRG4HjPJyKtP9DBnjO447VMHDq9IbAaiO68pHqluOEwWWhAWMUeJpq6a/L1XXm3NxsMiFyXHulxBgRyUDtklC/GZBh7N4rONa8CZgcvkmOKSUAXFVfH3WltfedNM2IdZRb3FPObigBNo4e96tWYcbcvRldhwsRlcNvz/wWi2f85esNohLy0tsuv1ibCy08uK4RjA89HB9BxUVKd61UtyBGlBPEVChEgHdqYnIG7TIH1yrqvM8GStx3OLBTNSE+mew1FbZ331IJIco6o8zJgQQqon16Y8o90RsBs0wD5A8NprE8BAqR/HHkgB5rXNooOPHptiYL8uFpD5LQS9VBnubljB9c0bmEBASAb5hOJhmKvMREmqEEaD0lwPsvDXp9KxdcRuVlbArYPc2hjDkXqtcU/IJrpDoIYMDjKPpsckMAnRB506hJt+JIxJyrV5Wqn7nH4RFdnzX8zhEILbUIm2PZA4M022f++Z6l8sSxdTtDtmYJ5GgCZYO2amCh0wPp2Mwo4kSOKDnA3wNzxJAuSDLNWOvy+RCfjnxV/NtquK3PKubWZsb0xxJhM93bHOy3eW7/88SjzvzsCi2kiiuciFGtQh5Ir45VcQD6GBTwfjF+ilq6Oq0f6F2JnxVT5XRu4CzjTjnJgT2NtunKg7XLCMqYjXPt6GEIjzq47qxdth9+kgNy9dR/untSYyp0as4VUuZyGb4S2tGoPhJvFcnM0woG0qZ30tFUpdUyjIH1tD1+EmEPUD9ZdagbirpsVDaMORREx5Y5QtEfqSN2EhwlAA7/gBOkd4vc/zI9L0pBwUpCfU4bLcVW9X2iY4UuwD8ZGffT5cIrd8HGuFPJ9ikjSYjjCEMk+6NmDkBqA7A9FTKnz/KFtWUHAj8MsMK1ggV4bY/Euqrp7se7Di8O0isDbJ+kq53QDqXrpFoTrO9vW1MAUN0FvRtjhnHdII6v3iK8vI+bYJjGSTRuMYEgs3ExxqyE9ljAdf60NlTBx7PQNWUO/QL5SkoQm8/m/9lUQ7kKmBPl87mQphBmZW8wN2199hTblDkKax40f9N0HPYtklWGv2ztz4Pz4EahwXvJyB1rHB6B8dOInqkKp/ifCIdqilvl6fI6cxYGnPkJrRZuqThbZE/6/6Z6AmxsAY72twzCNq49H7n4yP5k5G4nPM+qE3Zpof7ro1mX93la/T6DMCyrGpuXfLIEux65cm+ItTiKK5Un+VVbWaB29afPd09BMXZhQWkO8LnhygS6mSo9014I8wwJ2+QfQ8/pUvSMmZWu6DLOwU2EM4ESXHAslHxITJep9pe7oPP8H1k6TyyDZH6uONKqQq9hi2no/pruVw1DsnqD2VenhCnKKk0xvLo8VrzA4zpl3c6k9SQvjZcrkkTOZ4vwvEKPuHvHC7YYSdR9xF61diaal6tAnoEPClBaGYiBxNul41DRDzdVAtVZWg9La+59QSc7maywYdgP55fI31QyZ0K4q9wC1/1Bj/5X/LnENJOJojNhUZ+8dXvBe3gZ42P3SYNYzwcbtxAGHgS1q5JDOQomMj+SuGJDn X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: UqSJvhoshpUheDX33JyJgfZdCL3Go0U/NPYHpb+/bKZgj7p5TE976ZGyTc2uY5RgrBydmsYxNwZBX/2OAIuVfTCFexf/tGoJvCjo3FJUJ8KaHn3ao3+CWkb7V2Givs4BQSQ97qpA5Zejt4KaKhVgqsur7U9CcYK1Itd1QS7ueU7yRPJmyj6rjuvtPsLQcLh12Uw13+AS6iF6+I1TQj5D4fWUHGsWL2+nklqak5k6fyApbXLirYtXrOc14w4Yi/fsF/CigyO1mhwbYk8ss324RUrcCbKNy70vEdnPU7cPIhfFAXRharMzBYygT6Pm6nvwPe47SfA3rmEEQaBPkv0t//gyw0asXubMz+eVrHGAI68Vyeekve2gJnxeMzzxVXsN+POyiYId50zxZUeI4UkED70OMJ7UfxQFcEJyGVQP4+rmoipuGF3TAHtX6n4oRBc5aEFa31QvegsGNBc9gyVYVKCE85LsEu96tUE4WnUzXpiQJHHP4Ek0mc8zAq9qu5hYwK63+T1WOUrWZWH8gNa1Xu3hH8TFJ/O60f7LrLEoR2j0Gm1yvW9/0Hntl7JP6JdiSp0ZffdarRkT1y5UEa9/3CiHp0BAdH2y63cJlcjsDho= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c3fc58d8-c90b-4c32-4ccd-08dc2f282381 X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7957.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Feb 2024 19:47:42.1683 (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: QEvHIfYFoHzr5XLT2jH2JfdJa8b/iV+8SSY23Hc93CFtyrKq6B0xzqBBJQVYbfwxti0n5G2xLE1y0fqw1HJ/WQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB7238 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-02-16_19,2024-02-16_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402160154 X-Proofpoint-GUID: RoOeqxCwGhxDBG0ROjU4Np3MRErMBJvS X-Proofpoint-ORIG-GUID: RoOeqxCwGhxDBG0ROjU4Np3MRErMBJvS X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, 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+patchwork=sourceware.org@gcc.gnu.org gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): New function. (call_object_size): Call the new function. gcc/testsuite/ChangeLog: * gcc.dg/builtin-object-size-common.h: Add a new macro EXPECT. * gcc.dg/flex-array-counted-by-3.c: New test. * gcc.dg/flex-array-counted-by-4.c: New test. * gcc.dg/flex-array-counted-by-5.c: New test. --- .../gcc.dg/builtin-object-size-common.h | 11 ++ .../gcc.dg/flex-array-counted-by-3.c | 63 +++++++ .../gcc.dg/flex-array-counted-by-4.c | 178 ++++++++++++++++++ .../gcc.dg/flex-array-counted-by-5.c | 48 +++++ gcc/tree-object-size.cc | 59 ++++++ 5 files changed, 359 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-3.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-4.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-5.c diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-common.h b/gcc/testsuite/gcc.dg/builtin-object-size-common.h index 66ff7cdd953a..b677067c6e6b 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-common.h +++ b/gcc/testsuite/gcc.dg/builtin-object-size-common.h @@ -30,3 +30,14 @@ unsigned nfails = 0; __builtin_abort (); \ return 0; \ } while (0) + +#define EXPECT(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + __builtin_printf ("ok: %s == %zd\n", #p, p); \ + else \ + { \ + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + FAIL (); \ + } \ +} while (0); diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c new file mode 100644 index 000000000000..0066c32ca808 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c @@ -0,0 +1,63 @@ +/* test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +struct flex { + int b; + int c[]; +} *array_flex; + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + int c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_flex + = (struct flex *)malloc (sizeof (struct flex) + + normal_count * sizeof (int)); + array_flex->b = normal_count; + + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (int)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated) + + attr_count * sizeof (int)); + array_nested_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test () +{ + EXPECT(__builtin_dynamic_object_size(array_flex->c, 1), -1); + EXPECT(__builtin_dynamic_object_size(array_annotated->c, 1), + array_annotated->b * sizeof (int)); + EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1), + array_nested_annotated->b * sizeof (int)); +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test (); + DONE (); +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c new file mode 100644 index 000000000000..3ce7f3545549 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-4.c @@ -0,0 +1,178 @@ +/* test the attribute counted_by and its usage in +__builtin_dynamic_object_size: what's the correct behavior when the +allocation size mismatched with the value of counted_by attribute? +we should always use the latest value that is hold by the counted_by +field. */ +/* { dg-do run } */ +/* { dg-options "-O -fstrict-flex-arrays=3" } */ + +#include "builtin-object-size-common.h" + +struct annotated { + size_t foo; + char others; + char array[] __attribute__((counted_by (foo))); +}; + +#define noinline __attribute__((__noinline__)) +#define SIZE_BUMP 10 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +/* In general, Due to type casting, the type for the pointee of a pointer + does not say anything about the object it points to, + So, __builtin_object_size can not directly use the type of the pointee + to decide the size of the object the pointer points to. + + there are only two reliable ways: + A. observed allocations (call to the allocation functions in the routine) + B. observed accesses (read or write access to the location of the + pointer points to) + + that provide information about the type/existence of an object at + the corresponding address. + + for A, we use the "alloc_size" attribute for the corresponding allocation + functions to determine the object size; + (We treat counted_by attribute the same as the "alloc_size" attribute) + + For B, we use the SIZE info of the TYPE attached to the corresponding access. + + The only other way in C which ensures that a pointer actually points + to an object of the correct type is 'static': + + void foo(struct P *p[static 1]); + + See https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624814.html + for more details. */ + +/* in the following function, malloc allocated more space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? */ + +static struct annotated * noinline alloc_buf_more (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index + SIZE_BUMP) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index; + + /*when checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + we always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /*when checking the pointer p, we only have info on the observed allocation. + So, the object size info can only been obtained from the call to malloc. + for both MAXIMUM and MINIMUM: A = (index + SIZE_BUMP) * sizeof (char) */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +/* in the following function, malloc allocated less space than the value + of counted_by attribute. Then what's the correct behavior we expect + the __builtin_dynamic_object_size should have for each of the cases? + NOTE: this is an user error, GCC should issue warnings for such case. + this is a seperate issue we should address later. */ + +static struct annotated * noinline alloc_buf_less (size_t index) +{ + struct annotated *p; + size_t allocated_size + = MAX (sizeof (struct annotated), + (__builtin_offsetof (struct annotated, array[0]) + + (index) * sizeof (char))); + p = (struct annotated *) malloc (allocated_size); + + p->foo = index + SIZE_BUMP; + + /*when checking the observed access p->array, we have info on both + observered allocation and observed access, + A.1 from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + A.2 from the counted-by attribute: + p->foo * sizeof (char) + + we always use the latest value that is hold by the counted-by field. + */ + + EXPECT(__builtin_dynamic_object_size(p->array, 0), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 1), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 2), + (p->foo) * sizeof(char)); + + EXPECT(__builtin_dynamic_object_size(p->array, 3), + (p->foo) * sizeof(char)); + + /*when checking the pointer p, we only have info on the observed + allocation. So, the object size info can only been obtained from + the call to malloc. */ + EXPECT(__builtin_dynamic_object_size(p, 0), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 1), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 2), allocated_size); + EXPECT(__builtin_dynamic_object_size(p, 3), allocated_size); + return p; +} + +int main () +{ + struct annotated *p, *q; + p = alloc_buf_more (10); + q = alloc_buf_less (10); + + /*when checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(p->array, 0), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 1), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 2), p->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(p->array, 3), p->foo * sizeof(char)); + /*when checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(p, 0), -1); + EXPECT(__builtin_dynamic_object_size(p, 1), -1); + EXPECT(__builtin_dynamic_object_size(p, 2), 0); + EXPECT(__builtin_dynamic_object_size(p, 3), 0); + + /*when checking the access p->array, we only have info on the counted-by + value. */ + EXPECT(__builtin_dynamic_object_size(q->array, 0), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 1), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 2), q->foo * sizeof(char)); + EXPECT(__builtin_dynamic_object_size(q->array, 3), q->foo * sizeof(char)); + /*when checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + EXPECT(__builtin_dynamic_object_size(q, 0), -1); + EXPECT(__builtin_dynamic_object_size(q, 1), -1); + EXPECT(__builtin_dynamic_object_size(q, 2), 0); + EXPECT(__builtin_dynamic_object_size(q, 3), 0); + + DONE (); +} diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c new file mode 100644 index 000000000000..37fc215e5e5c --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-5.c @@ -0,0 +1,48 @@ +/* test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. when the counted_by field is negative. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + int c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +void __attribute__((__noinline__)) setup (int attr_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated)); + array_nested_annotated->b = attr_count -1; + + return; +} + +void __attribute__((__noinline__)) test () +{ + EXPECT(__builtin_dynamic_object_size(array_annotated->c, 1), 0); + EXPECT(__builtin_dynamic_object_size(array_nested_annotated->c, 1), 0); +} + +int main(int argc, char *argv[]) +{ + setup (-10); + test (); + DONE (); +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 018fbc30cbb6..630b0a7aaa4b 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see #include "attribs.h" #include "builtins.h" #include "gimplify-me.h" +#include "gimplify.h" struct object_size_info { @@ -60,6 +61,7 @@ static tree compute_object_offset (tree, const_tree); static bool addr_object_size (struct object_size_info *, const_tree, int, tree *, tree *t = NULL); static tree alloc_object_size (const gcall *, int); +static tree access_with_size_object_size (const gcall *, int); static tree pass_through_call (const gcall *); static void collect_object_sizes_for (struct object_size_info *, tree); static void expr_object_size (struct object_size_info *, tree, tree); @@ -749,6 +751,59 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, return false; } +/* Compute __builtin_object_size for a CALL to .ACCESS_WITH_SIZE, + OBJECT_SIZE_TYPE is the second argument from __builtin_object_size. + the 2nd, 3rd, and the 4th parameters of the call determine the size of + the CALL: + + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: unknown; + 1: the number of the elements of the object type; + 2: the number of bytes; + 4th argument TYPE_OF_SIZE: A constant 0 with the TYPE 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 array type. */ +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)); + /* result type is a pointer type to the original flexible array type. */ + tree result_type = gimple_call_return_type (call); + gcc_assert (POINTER_TYPE_P (result_type)); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); + tree ref_to_size = gimple_call_arg (call, 1); + unsigned int type_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); + tree type = TREE_TYPE (gimple_call_arg (call, 3)); + + tree size = fold_build2 (MEM_REF, type, ref_to_size, + build_int_cst (ptr_type_node, 0)); + + /* If size is negative value, treat it as zero. */ + if (!TYPE_UNSIGNED (type)) + { + tree cond_expr = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, integer_type_node, cond_expr, + build_zero_cst (type), size); + } + + if (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 +1405,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);