From patchwork Fri Feb 16 19:47:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1900234 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=R2CIJOTi; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=0U+EQnjJ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tc2Zf4m2hz23hy for ; Sat, 17 Feb 2024 06:49:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17DBD385770D for ; Fri, 16 Feb 2024 19:49:44 +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 9A1603857702 for ; Fri, 16 Feb 2024 19:47:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A1603857702 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 9A1603857702 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=1708112867; cv=pass; b=ndrMlzaUzsJ0TAOL7UYg7XB7nixotq/T1JhO5WvO6aLeYfwWOXd0SPWrwIsPbyfuRgARnBJ5X4mL85o4Sf/Wr6oamPSi0IKvi6H05PsRmFl7puF7JXZEsWSnMSAiIW9XRyu3C4zQIL1lfcbNGiGwn5NIUwdxqplgl8QoU/zAkmE= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708112867; c=relaxed/simple; bh=j2aWEyclDfZ2jJSmwl6JyM2M64cqX5RVGMQORSUT0cs=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=EOEmI5cJGFA8hLLJybDppxtVapxnLoO775CCxKh1S/HQf9IIsTgYxPQOSsdsObON++rFQyKglDAv/FfmhRAkYVYNnTuvtMYOWrQo3hfMz2E+FrriyrmDbVUlR3OCEEVL5e17TW9xtriNapqMXe/OzkX4sJEIlG893z5XZtCWkKw= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41GJ6a9E018079; Fri, 16 Feb 2024 19:47:42 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-11-20; bh=rNUn0GGPln4fo7Wq9qOITi6B5Gkf9BHJwmt+YxmnZfw=; b=R2CIJOTi40SPQm/O/tB1PXLC7k/tQrYDmCU2fLZOo+SKec0A9R27+AuI5gDrvDOup5aT XwRD3HG+o0WDxWsOd6L+emqy4nsUrU8i8P9ycyuVkzs/C3BQ2G6KLrPaxk/LMpruWREB Xu2fmspb6HdLw0u3Si4WO2Upux9JURDA7xp0WnjfS96w8hS+Ix8fLVw5ev9aBn5/D5kn kH2/QycoaJICPHiR8X+Ysm388xESgHEVOxeZKyWnP/N2/xKlsbxS9M+/9utRTF8EYoKy zL/RHcAI9PcmyKZ+aVu+l7JZQk8FJhYkJ66gcFJOlZd13XAbBHgGZh0I0Y9+Rm3/P/M7 Dg== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3w9301p0jv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:42 +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 41GJSqsZ014968; Fri, 16 Feb 2024 19:47:41 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 3w5ykcaxdc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:41 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mCFoKIMfYKKHqWVQul8f7rbcKPJAoWxOXihYjtOsJICgq78mU8IC97R4T15lPFtt2wawZBVws7UFJ2JmfYKXjDsvMaCQfJacmrz8gzrrO6Hyk8CfSDgtbFUKDa7C+lhPazUXhPU0sDB/LFzdgmIUypqiwkAwP5E1XCofEObw4QRD7TrEHRTIZXwM/kNGTgQwJjyGQpXWq5NLkR7rAmxgpnfvOGnQoaBeoVzrtCRlhpFyUMq+wBtWkbkhAYQ+szJJIRARr4tLT4lv7fudUe7POAMlo9MGQQ8NN477HM1a747X/QzP22Gy4j4P1ox8jB5PpJmkBOAgN8j93X8xTMOoGQ== 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=rNUn0GGPln4fo7Wq9qOITi6B5Gkf9BHJwmt+YxmnZfw=; b=gqbARW55TBohwKLfdl20nTHrKItw2PgCfqXMMdaSj9u3Z7VSBKQ5geaKOiJFNohCm32FGdlKKX5Ay71Lt/h7V4K9yrtSWTHaV/uA2sbD+X+Z++bTlhlVsyjIbO3gewY09YFY3Lap7ozHMLd1x43AB/PLFcEsjWYBoNfV5/OII+W9OQqK+zaNTGSvvVWA/SIUsJWj+hpKKpQWaUH6mICawutFyV6rnRSRUizGFSnsoiKnxNDv7KjV2/vr4h1PnWCX4enCmTyjHVGWJgN+xs+64V9P6VoLSna5czjOmGj7mDUb4bzzpkEaXVuixYeDKKARHGcOIG3M13WPpE6+v6RAXA== 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=rNUn0GGPln4fo7Wq9qOITi6B5Gkf9BHJwmt+YxmnZfw=; b=0U+EQnjJN0j287Mn+GHAeky2OGzexxe6OQiLNPyNRg92xtGpqJCHWoQiAXljol65TQ5LXUp4+oD8uXiEhIshbqaX24/6rCiCDK4mbOpm7FHgd/awxKApwYGU9UXSDMga41lRLHTImIdjskO8P7EcXKKtEw8mr/IMCeD2VYdQ6vA= 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:35 +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:35 +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 1/5] Provide counted_by attribute to flexible array member field (PR108896) Date: Fri, 16 Feb 2024 19:47:19 +0000 Message-Id: <20240216194723.391359-2-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: SJ0PR03CA0385.namprd03.prod.outlook.com (2603:10b6:a03:3a1::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_|IA1PR10MB7238:EE_ X-MS-Office365-Filtering-Correlation-Id: 41fa732b-7d1e-4639-1999-08dc2f281f4f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bQDov0VR66qRjbyNaBxT0tNo0tKNHw0P6ydgR/qrtB24Y9WoAetq69LtpwkS0dTcoXUfgz5mRj0rJT0uqwU2T1pLYEfpL9Pmd/gxSbyHvMIcNWvXptf5Alhk5Iuf7j4JAvncTW6PLv2qkj5wRdUdn9l+3ys34G0YK3z0RY70IdSY//O4O7FAWGyz7b/DUhvKFcryFRzYUCsrzQcLRm4Ucaw9B//ZtwSAC83Rs4QLJ3Iy0b8b2d/S1WW7wkVuoe7IHE3tR5WCxWRQZcgDXcP0DGCNEkiaeDGGmJYqmPk82SelFYRLDrudfhJftYrVJq2+1Svcu94HsvK9PEm/Nn0zeN/bTFGsAV7iioB7OAplHF0TvfjZqLRYowL/fu/vOfoDQOeiReOi5BqDCjP1FLCeGxUKdTPp5V8eVLhYfxlgZwj8fROPgO820FLtpTsSInLg2gduJEK9FKRyT4p4dTptsgmJZdv8thwrY8BFuO+HbAqdJ+Cm7rwjC/6fLLGNEYltJRsUD9D03N5Zc95f3WhwIWGH6/sczueNGonATBjk/W8= 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)(6512007)(6486002)(84970400001)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hdYari60xobuW2ERkWliMQKuhRMRxZ8FM3XTjZdLBqrxUtaStDec1qQelD54DcyT5f65wtdQdMDkq8q13wqJeUWO1yySzx8x8w/B6QTkrD+xE3kf6FD0JzKYyvthLgbbaRreTsSiJ4HM0ZdkJNf+gz32dSO0kkANUVHFi6DXfB6ltLx6UM4uTcBGiZ8RpBbFNEEyrMz9lWlY4u0JalR0wQ/m6YfUkYfoHWEsduQhqelFDz3YYdlQ9EWbjAYizAosA9xgJ7z+ed3vJtqUiZjPL3kUcZU1munQgOSZOXMqeMnXxMuGWHe6srod/RevnzEmoEKUfqa0A5dgRDubEC/wOeNu8nf1G2s7HpKmGhV5J1pmnU96x/pxxGN4lLcVUvF7CYTTdATM0imuff9QNyXlCm+xFLxEvLDxYQ0eD89KyCq896/RvKuW4+AMpmzW27zcomYoRk+kxxp4JsJvrizYAA+xduPFsbfpB5U3v+DzVRbUZMjGvETwNgqJZw906bh7OJcXXs/cAna/egfwvXD59KJ2RplU4uGZOk5jg5xNpx7d2SzDSoScfUzxyOKGBzKjdjsRagSqRi7QV6snoBq5a8sgqlvKrEP2AI/xcl7aIfzKmDqg0w8BB2qj9Fzcm2xdMSOt4yN7sFlDCYKqJ1suLdRUZu8wzJk4T4owwO69uN+2T9wYsbcAm9DIWQrxNUa6ylbkMLpqWnRxn8QwUURcbZncnEX5CwVd7hvMsUE/TcK9Dl6F+175doy8a0faJ/4oX3dssBmno5J0AkjdFP/koFLQZDAtY29GL8YRDZeCpdP8KQybIRvZwJKxXqwstBaSEhtgrEd1y4MEvdR11vlQoU9v4kkHIfdeppnPEU3MS2ojYWVTgfE0S+59dzWuC0KfEzVfMyQsxyKK6YQzno20BUKJF/DIP8r3re+m/w0gIPb0pft8P5LSDIgs9hNlTau7Bn8vyXdMvdzXRKLizWtNO+Fg0v9alaVvVVwqMsF31OOMm2iSOTeF9F8SF+1paHf31aIg0FCtfc2HYMlFuMRFeFFR/n86uShCPVx27fTgZQFPh2ybGWjA9n1fHzqzQfsXFb0olED69GdAHdMAmSUUruV9ctrmqi3zNhr9lde0MRQUlQlNlXckRoVBxjBEq+du+8uP4pk+WAzxDNg19ah2zdhMp7cb88q8XrE6/tBVTmAyAhFHOOyMGzyml3SbVU6IC94yxdpCoCWsgKZhKqqSzXqXjfhDVXiHrbvlXi0eyJ0GmPQb3xYzwRA8n4Ta9pihH6SNG5/nvzCXjlOFWC0PSjiJeL6YkCboeKGlbEkNEhOR4Y22feX3I2tc693ToKJI3olt1AsB62zRt9mwcmNA2Wyc1j4uJoQdUrtQacXB2XvSAAbxufX0J3mcPs1pUt3lmiqs+1XqyDQ5WGfPLdER5wDcW5c/uQF+856Lla0I61k84E8EBCO/a4aKwlqKoHj81+8fQQtXiwW+C752o3GgQFEFf5J5v2uExGZl59NSlMi02W6Dptf++0lXwZrprcWKuMHsqs4KcHY8a8w7N8VDc9vd0wKOvIMu84/I2PKj5S/DQH6q09pR9Uw29ydY8+QF X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: walfwo4YzkmWBHCpks8G90TrkzHgNxTv4YUekLNMzITDwWWxkYT22q+9P1c40dgLD7ldp7TKqyaSBDiqdq3t95Uq8RxaUxt2qz4QlWCW5naRym5cdj2g4TCQz/J0IFl5nUoR8obPw6tuM3IW1K+B2ckvWYuSEXF4vhDo/83W8c1VAZ2L6LamUyfEaa87+xLBPa2rNXp70kualeM3Nf6U7rLGVWZ7vwyKWPkubT4mjTULVo+OpSmN7nYy0DsgQ6OGznFoSpcJqeOZWNEQfXSW4+GRgz4I5nSFk7tVdpDISi/GzQnXIICvdgTSyGoP62Xu2ib7FV+sGPa0fzhYEYgoBQQjmIhllBhxlD03kF+HT4d9B6oSNT52QZhcQGJXetswBAs7pbUzQ10UseZWbvahGqYG9f69P0kF7K3FuUjS7eWXMyiP+/KC/H/d5hXTVbwE3eZzBH2YkGJl6C0d4wcLf8wb80YZLTucZRfWeT5fUX5a1fT9D5EjB0dPLIb8045yXGsMbuKFncL6wpFNYdMR9CLpGWLcEzPoN+bVzLnWO7e+f4bhRgFL8Tt+FYVhDFBCcdpIVJ0KiWdhoEWIbXrVpEI134HD0532AgMU8oopBH4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 41fa732b-7d1e-4639-1999-08dc2f281f4f 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:35.3100 (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: a2zx9Thft220XSej+5TUkMAVIDGEBhQjZMcWoOfvbBNsneTc2vGA8rf4pVKm1efmxc3qcbTnWu+uiPIMGpY7Cg== 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 adultscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 mlxscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402160154 X-Proofpoint-GUID: hWRL1R-sQlKv272TXAjsOYd4fjN3bTvk X-Proofpoint-ORIG-GUID: hWRL1R-sQlKv272TXAjsOYd4fjN3bTvk 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, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 'counted_by (COUNT)' The 'counted_by' attribute may be attached to the C99 flexible array member of a structure. It indicates that the number of the elements of the array is given by the field 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. When the field that represents the number of the elements is assigned a negative integer value, the compiler will treat the value as zero. An explicit 'counted_by' annotation defines a relationship between two objects, 'p->array' and 'p->count', and there are the following requirementthat on the relationship between this pair: * 'p->count' 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 | 64 +++++++++++++++ gcc/testsuite/gcc.dg/flex-array-counted-by.c | 40 +++++++++ 8 files changed, 241 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 e15eff698dfd..56d828e3dfaf 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -9909,6 +9909,19 @@ c_common_finalize_early_debug (void) (*debug_hooks->early_global_decl) (cnode->decl); } +/* Determine whether TYPE is 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 fe20bc21c926..4348123502e4 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 1fba9c8dae76..c7c23edc4840 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -776,6 +776,7 @@ extern struct c_expr convert_lvalue_to_rvalue (location_t, struct c_expr, extern tree decl_constant_value_1 (tree, bool); extern void mark_exp_read (tree); extern tree composite_type (tree, tree); +extern tree lookup_field (tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t, location_t); extern tree build_array_ref (location_t, tree, tree); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index ddeab1e2a8a1..cead0a055068 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -101,7 +101,6 @@ static bool function_types_compatible_p (const_tree, const_tree, struct comptypes_data *); static bool type_lists_compatible_p (const_tree, const_tree, struct comptypes_data *); -static tree lookup_field (tree, tree); static int convert_arguments (location_t, vec, tree, vec *, vec *, tree, tree); @@ -2375,7 +2374,7 @@ default_conversion (tree exp) the component is embedded within (nested) anonymous structures or unions, the list steps down the chain to the component. */ -static tree +tree lookup_field (tree type, tree component) { tree field; diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 2b8ba1949bf1..fc2fbc702b44 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7753,6 +7753,70 @@ 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. +When the field that represents the number of the elements is assigned a +negative integer value, the compiler will treat the value as zero. + +An explicit @code{counted_by} annotation defines a relationship between +two objects, @code{p->array} and @code{p->count}, and there are the +following requirementthat on the relationship between this pair: + +@itemize @bullet +@item +@code{p->count} 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 Fri Feb 16 19:47:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1900230 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=kkGAVNB/; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=COCyj9zc; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tc2Xy0LFFz23hy for ; Sat, 17 Feb 2024 06:48:18 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C8EB43857712 for ; Fri, 16 Feb 2024 19:48:15 +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 40EA03857709 for ; Fri, 16 Feb 2024 19:47:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40EA03857709 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 40EA03857709 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=1708112867; cv=pass; b=JwZs3siZWuSCzjtm4COH4mDehVEIxfdvfM+oEj4ZCjxkgz7/RH1smyidHEhSR6dDj6zAfj6QQqZyY6IDz5Vqyzdo87O4N5UAx5PLpi6/gU3r0fgLmwl7Djjd8+s5mGw/7dmTgnzlBf01BaRm9MCNeG4wYETfqmXwBw8h+tztono= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708112867; c=relaxed/simple; bh=v96YDIPRMLeWzCtpQxZzXU/W6oaTmJtofHEpeJeg2OE=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=uGmc9Xazt8+rYPxqpMUJuVs5XoFHrs19uWJVhOaQeIvdvUaJ25Hcl47Mb62GKGD754OU5VnlhIjYQEkG928FeSx5aD2r7WbKg7tAJO1zSDKRcoHAG25q7P7PeO2ev3S5rUivm/k4bUBLjhvB1lKoaYKAluq3gaZohw3PLyH53rk= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41GJVF9N003294; Fri, 16 Feb 2024 19:47:43 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=5M2qcrGjKVuogOU4hXr9b12mNHRSVzQRqsWxpD+d7RI=; b=kkGAVNB/3uBeXHMY8Yiv4Rftov/5YykC7rR44xqCJQrSNq0bB5L7jftb0c3+hCdu19hg mvMWQZ0npCrhKxtmRaZ+tUW183Jx+YYn5u40OPH0DJsxNYUB+7BrJMZ74dAhJF6QEGs5 nPCRl1S2FCPQhFYbqKe9lFC1H8V3Dk4rTMJ38GciBZh2ckN7SwDT/aHnbVnO/xj5ffLf DFhwM/FauUlExmktUKhEqHaURvoZ0csRJDQHKKSLMoKUttU/7hzIUgkX8hnddw5tK62C aXSjYU2mGfyEHoXm09tQ0pRNTmWf2TL0EQ3FyHPQflTqMAPn0bbTPnL2ITzDCgSlGvN8 6g== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3w92j0p0ya-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:43 +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 41GJSqsa014968; Fri, 16 Feb 2024 19:47:42 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 3w5ykcaxdc-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Wl4gUUFYcPDANBSisWqK8JuLcr+HSA4LsnYqmf4tf2IOAvvWdnbcvRlb3o58epqpQLHNA9okRknHgab9Fq1Z0Ger0IBpFHtmVTrRTzpGLvS1kFblUerMsSe0xSvbUPfEGyzuvCjSS187QKdW4CIXgG3Wi95aNK7cvDDrGNCtkU56VhsGWgTYx7vQbwowZLRHT8RzjrPtJmhP7iCtLM43A4/9Z8rzyh4jSCoh9P40Kfx1FA1ZCxMg1R469iY8AkyRMEW8DkDuX9t9OabvgeMAckSjhWBwy3In3GjiRFspv1swU1Zx9FzAdeyyuXZTExlBBLBS5SnijTTlGK+6yt4XTA== 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=5M2qcrGjKVuogOU4hXr9b12mNHRSVzQRqsWxpD+d7RI=; b=MxsCFx+KNjUYcrX2RKGnCPZ3MzsgoCkUpC5k2XK802JqyMMhUM462SI1Q6tv+MvtXN/mySfWbHL6OriAy7hwOfSHM6axRXaHiKlgtbafSTAt+vg4otNpT8YacuJ4wTeDI2L2xGzc5Jka2zry39AIUgkx5icrFUvsWZd5U0HxATxGdnOy0PdSkH9FzmXpLp2t9GEbY5p/ogXCE6zqb12xfXq12xsTG0BcXs3yMX66+baw3sNdGUJ9D+GsGxhKcOAx2eer9+ownvKqYEwy2moBX4fnunQgC67MexyeCER9WSz1MbOa2VBJjJcNvRtJo5E7n1aQOE4/qmJfElB3cSQ6Vg== 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=5M2qcrGjKVuogOU4hXr9b12mNHRSVzQRqsWxpD+d7RI=; b=COCyj9zcTXijS4PTH9cszKZtLbLuiUTVrSVMWFoWHfE39YseqiwxFBdeOuEYtfEyduYqBOCNAynfA2aJn7dpNcQVhHZmoscNaA1q3pK4MGiElGkmfUNaZ5X18CE1BZ0+AtD+DHUbKIZCL3Rg4UBoMS0vE3ZRqGQAaU6Xnp6FeXw= 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:39 +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:39 +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 2/5] Convert references with "counted_by" attributes to/from .ACCESS_WITH_SIZE. Date: Fri, 16 Feb 2024 19:47:20 +0000 Message-Id: <20240216194723.391359-3-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: SJ0P220CA0026.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:41b::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_|IA1PR10MB7238:EE_ X-MS-Office365-Filtering-Correlation-Id: 0b616fd8-9911-4a6c-0861-08dc2f2821d5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eqzIWIPXJr1F/KNg6tmRr6yZAck3hIwEUGQE82eKIvrWSNAKtEAFQE2aSTOyuUb9/fMpYDM6J0IhHYzMiWn6KclIh6W2OyTqFbnb0u/VecXixhGhQ4GZgdtQgoKUD6VFpBr2s+umGj68boMZzzyMN0fzugr3+2zE29HabxSKm7V3FlUZOOY4ks3ONQ1ax11oIZTKl9Z/qVICD8AaauOG2iqdEq9TOihg4heGvhwSoSGgWsy1CNYZjy2taETIp/X5q/ix1xHXYaI+J9Lm9VIV5ITdnwb0Lwv3jlWRRjaDmnvaiiks5ZIU72iZCHCjqO5BRQnxtyso4J/QmQv9/YVNGc6Dpk6pLmYPGvhiRNTAJCgq8kh0LUSHdi0dhMtIrc1HbPF26e/itT2sp1eVAXiWQWfMy17wC+xdG3PUV1G9F0YTPJhpyihFSHoSYt2yfpQwB9135HzwXBDd+N/lDkkvOAZr7CXhn6UQQyxRYQyvJQBSd/Wwn1j6cIkTZ1zf+no3++XwAcikKIO5P00JrVHbb4BFAeyLb5DvXSCK9fXI0Xs= 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)(6512007)(6486002)(84970400001)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Er2f5jcy6PHllBg7/fMLjrWoKYaoJU5t9JxQ0rGROWrc05/pqQ40WeaoP7Cl51nFmC5p4vMJk0kwJWajO92cbOHejLCbmuXqtbf2ww+INVSAoZU+IlNrUek+dy4I12d1/r+pCCHi7KVaEFBBfbq28ANXjoIR81j24CwGyqnVeIjHBxEYx/2vTzVn1mQ8mgWQfYo2/vSAOL7sE2Dfo+EUhVN3J/av397YFiVyMdbx3oM0sz4mirbc+SBEhZeLHK+GSjz0ihWFai6Rml8mHEBEAy2PMjmqVa9rUR1htPv3eZlseklyitdRYZHEjATjN68utz8JzOGV7no6agU3pRioRgcDmk6F5XKHUfgl0SUbOVsr+g8N1SbrkxeHRzwRKS1cZuwRTZf1m1hl9GgZDBVrB/lNKCyNJrgMekxJyvoRYBxjX9qCbiq0OVTXDMmHztQTe4U3ZdNHH4zCq2TEYrhbIx78DtHHjbyDHBa5sgo4j7HoPs+8yRpcibOPdOVpSdXVa5HPf8OT/ilgYePvq/7g0gfcYQEAGDWOSOT34m23y4f34EFCAIrTWBWQjLtL4gsOT1zYVOJhCadhl+VnbiwQ1QgRh/zU03PWtsKC2OpNNWglNx45DmpzGdN6hWDBhF7c7mPIiq7VDJkKlbNC0yLTokpKGh7fGFwV0ySBQ3putM+9FaANef5j/jUSvofyxldngXG7Z6dIh8/8GyRiwT31ys56+mAWPBhHSJN/+homXFkBPbmeQoAvdJJzNlFmZlG8fNIh7c+NcNCwhaUaZNjH+dLeCZju5naMjbrhWmY2S9e+wXjfvQxAP6v8NZ1ben5lOxrlFKg+HYRiAjMqLd5vGzbR5KW4tmY/vSKwwsaa5ftqdZfqSY1eEe/E5ZF0D8DnRxsCcxvubfeHWmimVP8oa4LAn4bPM13zsuwHqr/r4jMaxPE9n35ggDCkdjLEELUhYrY1C+9m+Pm7x6ZiV2DR1h+ZRdEKvdXSaL8fbUHAsnNhciKWJq+3wXmE5+SLECRKrWnz4SbMemgcnDhnBNEZ4ecJQuphYN4hEp3Xx9L/CJT6NTcJl8yTllyNjN2vDxgx3oxznHAbEY2vxYGarH49pD/YXPykuK90edyCE7LOPpxENnUfnRxCX0uMdF0eANl4CXfH/CqIl/2/uaKQgxcMJn+jC4OJtcKJ3BvUtIJuQBKrbnHulqfF02ZYiJutVNL4O/mnMDpz+aOYA7HlszqBdxDL5d6SSSFb1ksl9CdzFfAu3TqgPKEUqIluvysyN8Omsd6+S0QDepUZEoz0VtqnDE001DaZTbL4XCUQEj/Y3gC6tj+djeu6EHU7L01heMlpARxdzxfaorX7q24oYzMOM18PJTmeX+q7M3xWCBpO1kXjMXMjtCpeJ2kAcz2MvUExgCq/b7XIdDocNi8Cfrn2RC8dRjCJohYlERiBS+CH5G0jFS/8x/QnwB9bDVuXqx0UTixdODlT/hZm5LNnQVbrEZhFclJlsVJfvjcsOWLG8HDnSiPB3cwOazxNXwLAGxfW20pCVpZlqBO3qXadcIc9flwNtirNUsMpxidA+dR05fotyPUVmW9tZkVDklDnS8lH X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: QF45CgJ+aoIy4h/1pJZq3zn7Vs5ndXai/l5wRZ1iVnlUtyA7B94F1pwnPiefFkWz63WMwf2MAP0EFCQMeNO2c50TDMaAf1R4+OfGIDPM5DqYb07NPD6uEHYzl2PXN2g2zEKQwVrLeSCqzpdsLQfQiVPGh07KpeJLR5NANOVrR9SckKPUV8NXJFbuRyAgSbY0TIUUYrnABlhYwUDUxRayIizmyl8+k+UsYF+FX/M9Z/rYgqUhuAMIIq7SX82VyXg+q2q8vo5x644bJQBhOx4Zt6hs+PJJR5RlCs7Xs7h9v0zc2p1cnek2CoJWByR/9Day+HGN+bZbb2eu+UDZ6Wo7e9cFOzLFJqACG0glsZwafP5MUut6tG326utdq+7yPYEY4KSq3Nu0qdJz7UWd9SgfJ//z2qAiKz8Cs+pnYZcELj+gmpPKyzYSTHeVii6ICxcuYEMm2kO9ZFxEWzDIgW4AIDtDTarF0lXiJCPlKEN3ZyYOuj8JPvuSdDQfm5DuXmpTOGGT2yqqEVJcmQuc52vxmFdpzX5g64wp3fnuaZBfOQev43ehKhb9GbVIa9YeDGZVtWWMnYRA1Z0eeVcaPuEmwKuoqdZZy9A5OTPS7ENybOM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0b616fd8-9911-4a6c-0861-08dc2f2821d5 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:39.4820 (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: R9uZekQT1fg2WfFLlLZ0l1Wdy29fEo4KMH7MUUvowHUmeHByBJg5Dsy6COmZkjDL2lA7mUWmSF4sWbhYzGxIYQ== 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 adultscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 phishscore=0 mlxscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402160154 X-Proofpoint-GUID: -GWR_s8o2-v5SiOF99i02BAEeCSfCX7w X-Proofpoint-ORIG-GUID: -GWR_s8o2-v5SiOF99i02BAEeCSfCX7w 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, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Including the following changes: * The definition of the new internal function .ACCESS_WITH_SIZE in internal-fn.def. * C FE converts every reference to a FAM with a "counted_by" attribute to a call to the internal function .ACCESS_WITH_SIZE. (build_component_ref in c_typeck.cc) This includes the case when the object is statically allocated and initialized. In order to make this working, the routines initializer_constant_valid_p_1 and output_constant in varasm.cc are updated to handle calls to .ACCESS_WITH_SIZE. (initializer_constant_valid_p_1 and output_constant in varasm.c) However, for the reference inside "offsetof", the "counted_by" attribute is ignored since it's not useful at all. (c_parser_postfix_expression in c/c-parser.cc) In addtion to "offsetof", for the reference inside operator "typeof" and "alignof", we ignore counted_by attribute too. When building ADDR_EXPR for the .ACCESS_WITH_SIZE in C FE, replace the call with its first argument. * Convert every call to .ACCESS_WITH_SIZE to its first argument. (expand_ACCESS_WITH_SIZE in internal-fn.cc) * Adjust alias analysis to exclude the new internal from clobbering anything. (ref_maybe_used_by_call_p_1 and call_may_clobber_ref_p_1 in tree-ssa-alias.cc) * Adjust dead code elimination to eliminate the call to .ACCESS_WITH_SIZE when it's LHS is eliminated as dead code. (eliminate_unnecessary_stmts in tree-ssa-dce.cc) * Provide the utility routines to check the call is .ACCESS_WITH_SIZE and get the reference from the call to .ACCESS_WITH_SIZE. (is_access_with_size_p and get_ref_from_access_with_size in tree.cc) gcc/c/ChangeLog: * c-parser.cc (c_parser_postfix_expression): Ignore the counted-by attribute when build_component_ref inside offsetof operator. * c-tree.h (build_component_ref): Add one more parameter. * c-typeck.cc (build_counted_by_ref): New function. (build_access_with_size_for_counted_by): New function. (build_component_ref): Check the counted-by attribute and build call to .ACCESS_WITH_SIZE. (build_unary_op): When building ADDR_EXPR for .ACCESS_WITH_SIZE, use its first argument. (lvalue_p): Accept call to .ACCESS_WITH_SIZE. gcc/ChangeLog: * internal-fn.cc (expand_ACCESS_WITH_SIZE): New function. * internal-fn.def (ACCESS_WITH_SIZE): New internal function. * tree-ssa-alias.cc (ref_maybe_used_by_call_p_1): Special case IFN_ACCESS_WITH_SIZE. (call_may_clobber_ref_p_1): Special case IFN_ACCESS_WITH_SIZE. * tree-ssa-dce.cc (eliminate_unnecessary_stmts): Eliminate the call to .ACCESS_WITH_SIZE when its LHS is dead. * tree.cc (process_call_operands): Adjust side effect for function .ACCESS_WITH_SIZE. (is_access_with_size_p): New function. (get_ref_from_access_with_size): New function. * tree.h (is_access_with_size_p): New prototype. (get_ref_from_access_with_size): New prototype. * varasm.cc (initializer_constant_valid_p_1): Handle call to .ACCESS_WITH_SIZE. (output_constant): Handle call to .ACCESS_WITH_SIZE. gcc/testsuite/ChangeLog: * gcc.dg/flex-array-counted-by-2.c: New test. --- gcc/c/c-parser.cc | 10 +- gcc/c/c-tree.h | 2 +- gcc/c/c-typeck.cc | 128 +++++++++++++++++- gcc/internal-fn.cc | 36 +++++ gcc/internal-fn.def | 4 + .../gcc.dg/flex-array-counted-by-2.c | 112 +++++++++++++++ gcc/tree-ssa-alias.cc | 2 + gcc/tree-ssa-dce.cc | 5 +- gcc/tree.cc | 25 +++- gcc/tree.h | 8 ++ gcc/varasm.cc | 10 ++ 11 files changed, 332 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index c31349dae2ff..a6ed5ac43bb1 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -10850,9 +10850,12 @@ c_parser_postfix_expression (c_parser *parser) if (c_parser_next_token_is (parser, CPP_NAME)) { c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful at all. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, - comp_tok->location, UNKNOWN_LOCATION); + comp_tok->location, UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); while (c_parser_next_token_is (parser, CPP_DOT) || c_parser_next_token_is (parser, @@ -10879,11 +10882,14 @@ c_parser_postfix_expression (c_parser *parser) break; } c_token *comp_tok = c_parser_peek_token (parser); + /* Ignore the counted_by attribute for reference inside + offsetof since the information is not useful. */ offsetof_ref = build_component_ref (loc, offsetof_ref, comp_tok->value, comp_tok->location, - UNKNOWN_LOCATION); + UNKNOWN_LOCATION, + false); c_parser_consume_token (parser); } else diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index c7c23edc4840..59b69ca54f0f 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -778,7 +778,7 @@ extern void mark_exp_read (tree); extern tree composite_type (tree, tree); extern tree lookup_field (tree, tree); extern tree build_component_ref (location_t, tree, tree, location_t, - location_t); + location_t, bool = true); extern tree build_array_ref (location_t, tree, tree); extern tree build_omp_array_section (location_t, tree, tree, tree); extern tree build_external_ref (location_t, tree, bool, tree *); diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index cead0a055068..a29a7d7ec029 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2546,15 +2546,116 @@ should_suggest_deref_p (tree datum_type) return false; } +/* For a SUBDATUM field of a structure or union DATUM, generate a REF to + the object that represents its counted_by per the attribute counted_by + attached to this field if it's a flexible array member field, otherwise + return NULL_TREE. + set COUNTED_BY_TYPE to the TYPE of the counted_by field. + For example, if: + + struct P { + int k; + int x[] __attribute__ ((counted_by (k))); + } *p; + + for: + p->x + + the ref to the object that represents its element count will be: + + &(p->k) + +*/ +static tree +build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) +{ + tree type = TREE_TYPE (datum); + if (!(c_flexible_array_member_type_p (TREE_TYPE (subdatum)))) + return NULL_TREE; + + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (subdatum)); + tree counted_by_ref = NULL_TREE; + *counted_by_type = NULL_TREE; + if (attr_counted_by) + { + tree field_id = TREE_VALUE (TREE_VALUE (attr_counted_by)); + counted_by_ref + = build_component_ref (UNKNOWN_LOCATION, + datum, field_id, + UNKNOWN_LOCATION, UNKNOWN_LOCATION); + counted_by_ref = build_fold_addr_expr (counted_by_ref); + + /* get the TYPE of the counted_by field. */ + tree counted_by_field = lookup_field (type, field_id); + gcc_assert (counted_by_field); + + do + { + *counted_by_type = TREE_TYPE (TREE_VALUE (counted_by_field)); + counted_by_field = TREE_CHAIN (counted_by_field); + } + while (counted_by_field); + } + return counted_by_ref; +} + +/* Given a COMPONENT_REF REF with the location LOC, the corresponding + COUNTED_BY_REF, and the COUNTED_BY_TYPE, generate an INDIRECT_REF + to a call to the internal function .ACCESS_WITH_SIZE. + + REF + + to: + + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + + NOTE: The return type of this function is the POINTER type pointing + to the original flexible array type. + Then the type of the INDIRECT_REF is the original flexible array type. + + The type of the first argument of this function is a POINTER type + to the orignal flexible array type. + + The 4th argument of the call is a constant 0 with the TYPE of the + object pointed by COUNTED_BY_REF. + + */ +static tree +build_access_with_size_for_counted_by (location_t loc, tree ref, + tree counted_by_ref, + tree counted_by_type) +{ + gcc_assert (c_flexible_array_member_type_p (TREE_TYPE (ref))); + /* The result type of the call is a pointer to the flexible array type. */ + tree result_type = build_pointer_type (TREE_TYPE (ref)); + + tree call + = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, + result_type, 5, + array_to_pointer_conversion (loc, ref), + counted_by_ref, + build_int_cst (integer_type_node, 1), + build_int_cst (counted_by_type, 0), + build_int_cst (integer_type_node, -1)); + /* Wrap the call with an INDIRECT_REF with the flexible array type. */ + call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); + SET_EXPR_LOCATION (call, loc); + return call; +} + /* Make an expression to refer to the COMPONENT field of structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the location of the COMPONENT_REF. COMPONENT_LOC is the location of COMPONENT. ARROW_LOC is the location of the first -> operand if - it is from -> operator. */ + it is from -> operator. + If HANDLE_COUNTED_BY is true, check the counted_by attribute and generate + call to .ACCESS_WITH_SIZE. otherwise, ignore the attribute. */ tree build_component_ref (location_t loc, tree datum, tree component, - location_t component_loc, location_t arrow_loc) + location_t component_loc, location_t arrow_loc, + bool handle_counted_by) { tree type = TREE_TYPE (datum); enum tree_code code = TREE_CODE (type); @@ -2626,7 +2727,13 @@ build_component_ref (location_t loc, tree datum, tree component, int quals; tree subtype; bool use_datum_quals; - + tree counted_by_type = NULL_TREE; + /* Do not handle counted_by when in typeof and alignof operator. */ + handle_counted_by = handle_counted_by && !in_typeof && !in_alignof; + tree counted_by_ref = handle_counted_by + ? build_counted_by_ref (datum, subdatum, + &counted_by_type) + : NULL_TREE; if (TREE_TYPE (subdatum) == error_mark_node) return error_mark_node; @@ -2645,6 +2752,12 @@ build_component_ref (location_t loc, tree datum, tree component, ref = build3 (COMPONENT_REF, subtype, datum, subdatum, NULL_TREE); SET_EXPR_LOCATION (ref, loc); + + if (counted_by_ref) + ref = build_access_with_size_for_counted_by (loc, ref, + counted_by_ref, + counted_by_type); + if (TREE_READONLY (subdatum) || (use_datum_quals && TREE_READONLY (datum))) TREE_READONLY (ref) = 1; @@ -5048,7 +5161,11 @@ build_unary_op (location_t location, enum tree_code code, tree xarg, goto return_build_unary_op; } - /* Ordinary case; arg is a COMPONENT_REF or a decl. */ + /* Ordinary case; arg is a COMPONENT_REF or a decl,or a call to + .ACCESS_WITH_SIZE. */ + if (is_access_with_size_p (arg)) + arg = TREE_OPERAND (TREE_OPERAND (CALL_EXPR_ARG (arg, 0), 0), 0); + argtype = TREE_TYPE (arg); /* If the lvalue is const or volatile, merge that into the type @@ -5195,6 +5312,9 @@ lvalue_p (const_tree ref) case BIND_EXPR: return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE; + case CALL_EXPR: + return is_access_with_size_p (ref); + default: return false; } diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index a07f25f3aee3..331dfe57879b 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -3393,6 +3393,42 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) } } +/* Expand the IFN_ACCESS_WITH_SIZE function: + ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE, CLASS_OF_SIZE, + TYPE_OF_SIZE, ACCESS_MODE) + which returns the REF_TO_OBJ same as the 1st argument; + + 1st argument REF_TO_OBJ: The reference to the object; + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: 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 + 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..84899ae620f3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c @@ -0,0 +1,112 @@ +/* test the code generation for the new attribute counted_by. + and also the offsetof operator on such array. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fdump-tree-original" } */ + +#include + +struct annotated { + int b; + char c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +static struct annotated static_annotated = { sizeof "hello", "hello" }; +static char *y = static_annotated.c; + +struct flex { + int b; + char c[]; +}; + +struct nested_annotated { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[] __attribute__ ((counted_by (b))); +} *array_nested_annotated; + +static struct nested_annotated nested_static_annotated + = { sizeof "hello1", 0, "hello1" }; +static char *nested_y = nested_static_annotated.c; + +struct nested_flex { + struct { + union { + int b; + float f; + }; + int n; + }; + char c[]; +}; + +void __attribute__((__noinline__)) setup (int normal_count, int attr_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated) + + attr_count * sizeof (char)); + array_annotated->b = attr_count; + + array_nested_annotated + = (struct nested_annotated *)malloc (sizeof (struct nested_annotated) + + attr_count * sizeof (char)); + array_nested_annotated->b = attr_count; + + return; +} + +void __attribute__((__noinline__)) test (char a, char b) +{ + if (__builtin_offsetof (struct annotated, c[0]) + != __builtin_offsetof (struct flex, c[0])) + abort (); + if (__builtin_offsetof (struct annotated, c[1]) + != __builtin_offsetof (struct flex, c[1])) + abort (); + if (__builtin_offsetof (struct nested_annotated, c[0]) + != __builtin_offsetof (struct nested_flex, c[0])) + abort (); + if (__builtin_offsetof (struct nested_annotated, c[1]) + != __builtin_offsetof (struct nested_flex, c[1])) + abort (); + + if (__builtin_types_compatible_p (typeof (array_annotated->c), + typeof (&(array_annotated->c)[0]))) + abort (); + if (__builtin_types_compatible_p (typeof (array_nested_annotated->c), + typeof (&(array_nested_annotated->c)[0]))) + abort (); + + if (__alignof (array_annotated->c) != __alignof (char)) + abort (); + if (__alignof (array_nested_annotated->c) != __alignof (char)) + abort (); + + if ((unsigned long) array_annotated->c != (unsigned long) &array_annotated->c) + abort (); + if ((unsigned long) array_nested_annotated->c + != (unsigned long) &array_nested_annotated->c) + abort (); + + array_annotated->c[2] = a; + array_nested_annotated->c[3] = b; + + if (y[2] != 'l') abort (); + if (nested_y[4] !='o') abort (); + +} + +int main(int argc, char *argv[]) +{ + setup (10,10); + test ('A', 'B'); + if (array_annotated->c[2] != 'A') abort (); + if (array_nested_annotated->c[3] != 'B') abort (); + return 0; +} + +/* { dg-final { scan-tree-dump-times "ACCESS_WITH_SIZE" 8 "original" } } */ diff --git a/gcc/tree-ssa-alias.cc b/gcc/tree-ssa-alias.cc index e7c1c1aa6243..8c070e173bdd 100644 --- a/gcc/tree-ssa-alias.cc +++ b/gcc/tree-ssa-alias.cc @@ -2823,6 +2823,7 @@ ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p) return false; case IFN_MASK_STORE_LANES: case IFN_MASK_LEN_STORE_LANES: + case IFN_ACCESS_WITH_SIZE: goto process_args; case IFN_MASK_LOAD: case IFN_LEN_LOAD: @@ -3073,6 +3074,7 @@ call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref, bool tbaa_p) case IFN_UBSAN_OBJECT_SIZE: case IFN_UBSAN_PTR: case IFN_ASAN_CHECK: + case IFN_ACCESS_WITH_SIZE: return false; case IFN_MASK_STORE: case IFN_LEN_STORE: diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc index 636c471d4c89..a54fb1b754dd 100644 --- a/gcc/tree-ssa-dce.cc +++ b/gcc/tree-ssa-dce.cc @@ -1459,8 +1459,8 @@ eliminate_unnecessary_stmts (bool aggressive) update_stmt (stmt); release_ssa_name (name); - /* GOMP_SIMD_LANE (unless three argument) or ASAN_POISON - without lhs is not needed. */ + /* GOMP_SIMD_LANE (unless three argument), ASAN_POISON + or .ACCESS_WITH_SIZE without lhs is not needed. */ if (gimple_call_internal_p (stmt)) switch (gimple_call_internal_fn (stmt)) { @@ -1470,6 +1470,7 @@ eliminate_unnecessary_stmts (bool aggressive) break; /* FALLTHRU */ case IFN_ASAN_POISON: + case IFN_ACCESS_WITH_SIZE: remove_dead_stmt (&gsi, bb, to_remove_edges); break; default: diff --git a/gcc/tree.cc b/gcc/tree.cc index 3dff8c510832..18b4e729a5bc 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -4068,7 +4068,8 @@ process_call_operands (tree t) int i = call_expr_flags (t); /* Calls have side-effects, except those to const or pure functions. */ - if ((i & ECF_LOOPING_CONST_OR_PURE) || !(i & (ECF_CONST | ECF_PURE))) + if ((i & ECF_LOOPING_CONST_OR_PURE) + || (!(i & (ECF_CONST | ECF_PURE)) && !is_access_with_size_p (t))) side_effects = true; /* Propagate TREE_READONLY of arguments for const functions. */ if (i & ECF_CONST) @@ -13362,6 +13363,28 @@ component_ref_size (tree ref, special_array_member *sam /* = NULL */) ? NULL_TREE : size_zero_node); } +/* Return true if the given node CALL is a call to a .ACCESS_WITH_SIZE + function. */ +bool +is_access_with_size_p (const_tree call) +{ + if (TREE_CODE (call) != CALL_EXPR) + return false; + if (CALL_EXPR_IFN (call) == IFN_ACCESS_WITH_SIZE) + return true; + return false; +} + +/* Get the corresponding reference from the call to a .ACCESS_WITH_SIZE. + * i.e the first argument of this call. return NULL_TREE otherwise. */ +tree +get_ref_from_access_with_size (tree call) +{ + if (is_access_with_size_p (call)) + return CALL_EXPR_ARG (call, 0); + return NULL_TREE; +} + /* Return the machine mode of T. For vectors, returns the mode of the inner type. The main use case is to feed the result to HONOR_NANS, avoiding the BLKmode that a direct TYPE_MODE (T) might return. */ diff --git a/gcc/tree.h b/gcc/tree.h index 972a067a1f7a..73464d3e3ae3 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5760,6 +5760,14 @@ extern special_array_member component_ref_sam_type (tree); cannot be determined. */ extern tree component_ref_size (tree, special_array_member * = NULL); +/* Return true if the given node is a call to a .ACCESS_WITH_SIZE + function. */ +extern bool is_access_with_size_p (const_tree); + +/* Get the corresponding reference from the call to a .ACCESS_WITH_SIZE. + * i.e the first argument of this call. return NULL_TREE otherwise. */ +extern tree get_ref_from_access_with_size (tree); + extern int tree_map_base_eq (const void *, const void *); extern unsigned int tree_map_base_hash (const void *); extern bool tree_map_base_marked_p (const void *); diff --git a/gcc/varasm.cc b/gcc/varasm.cc index fa17eff551e8..0e6e44e0420c 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -5082,6 +5082,11 @@ initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache) } return ret; + case CALL_EXPR: + /* for a call to .ACCESS_WITH_SIZE, check the first argument. */ + if (tree ref = get_ref_from_access_with_size (value)) + return initializer_constant_valid_p_1 (ref, endtype, cache); + /* FALLTHROUGH. */ default: break; } @@ -5276,6 +5281,11 @@ output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align, exp = TREE_OPERAND (exp, 0); } + /* for a call to .ACCESS_WITH_SIZE, check the first argument. */ + if (TREE_CODE (exp) == CALL_EXPR) + if (tree ref = get_ref_from_access_with_size (exp)) + exp = ref; + code = TREE_CODE (TREE_TYPE (exp)); thissize = int_size_in_bytes (TREE_TYPE (exp)); From patchwork Fri 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: 1900231 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=N1mOtA9D; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=tQkCHfTH; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tc2YQ5RQjz23hy for ; Sat, 17 Feb 2024 06:48:42 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7CDB3857706 for ; Fri, 16 Feb 2024 19:48:40 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): New function. (call_object_size): Call the new function. gcc/testsuite/ChangeLog: * gcc.dg/builtin-object-size-common.h: Add a new macro EXPECT. * gcc.dg/flex-array-counted-by-3.c: New test. * gcc.dg/flex-array-counted-by-4.c: New test. * gcc.dg/flex-array-counted-by-5.c: New test. --- .../gcc.dg/builtin-object-size-common.h | 11 ++ .../gcc.dg/flex-array-counted-by-3.c | 63 +++++++ .../gcc.dg/flex-array-counted-by-4.c | 178 ++++++++++++++++++ .../gcc.dg/flex-array-counted-by-5.c | 48 +++++ gcc/tree-object-size.cc | 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); From patchwork Fri Feb 16 19:47:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1900232 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=oavEtdfr; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=CzCRj9U9; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tc2YR6kS1z23hy for ; Sat, 17 Feb 2024 06:48:43 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B59C63857709 for ; Fri, 16 Feb 2024 19:48:41 +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 2F58A3857708 for ; Fri, 16 Feb 2024 19:47:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F58A3857708 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 2F58A3857708 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=1708112872; cv=pass; b=Ozkpu4jKPZkHfEiAe680Y+Cnc8GsHWEwQ6YqXEFY0UtZnLPBmMLMzLhWVg3MQbS7BRzgYtFI35Cd69tFT+mEYE+iJdp1TRoFuz8JjHMDu7SgqQLSss/5zomGEt/9MKboGqLLXr8l6o1AB43yKe9jhmzeIJ1ID/fl8icFOH74ygY= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708112872; c=relaxed/simple; bh=QWNdJyUSeW99rAMWgPFWQIwvp6mmUrXdvKZ7IXeHThw=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=ZG1fP8SNLq6Ifi07czVY8IUJkyOUW32uMNIPMwTdBiwuWLzd00ytjNGpSda3I+9m+FyVaizb3rS86Eqeawny03mWRez/nmoqTZ4FI/fFdZ9/2u1l4NmodZf5qgxniSgmn153K7lm/53mt62TiSe/NS8Sdq4IsYtS2H9CKhGBvHE= 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 41GJGPNA006584; Fri, 16 Feb 2024 19:47:48 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=NVs5N3JU6rpeTiI4CFfGlJke/DLoz1UnDQ+whSU3fEg=; b=oavEtdfrCQwaDCOo0Q7o9M8dohFWc3gGZjAuBErOM85inWWh42rSPK8SYbBj4tePJ29t CzPfaRgeY3y8bDpXehBvMGX7IRuAaZKey2HIvKU9fTbIMrGK3iGeEu6OfnH+PXs2A9R5 29iIglp7XBy0aH0ZJ7RLHMgxRmh3ywX4TbaJf0RpF7IgwQjXUzvBovX+0CmEbpQ6T5dP GPSPzMRdOXjxWOLLUoNl+tt21LSQ6x+kb1vPsdyCBmLbz8ltgvcYsWLT32w/oUkZ0B2j yAlzaQI2LT2ULJKL6l+/hn+zg7FmaQP9pPXnHhrRU/mjcW9/fHqYEzNhhdSNgwDtbWF2 Cw== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3w91f068dc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:48 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 41GJG8lp015103; Fri, 16 Feb 2024 19:47:47 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3w5ykcm1a5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nckFxuhsP2LJ9nAMy/WNzhUhj8q/oP5FMpBXAcniLUHOxfrRWoM+zIAotPHkqLc7Ovawrqvu2xPn0KhqW/4gCvIEvEkTBK49yLw9zzRKpii1+lYaqNSPiIbMdQDByo7bMib/KX01QW8hd/PCR/bMJaJfViWjakvR1aEkw3tIqvkNkHRTYxppQN/b0XboK3YAgjDKUuFLxcEXTQLDGa914EsxNYSdsmcfilx1VgytTJ1NI6eX7MwP/VA3WRva25sz6cNWk2b709lEzHABONJz6+zcVqU5qRtmDM3lGZz1bS3+mSSY6wWvAVympURWWb+2FZl346Zc0CnY7lrf+tMk9Q== 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=NVs5N3JU6rpeTiI4CFfGlJke/DLoz1UnDQ+whSU3fEg=; b=iuCmInfVBLvg9uAjfD23n5jMYv5wihOTVKwxAVeru6mcii/fuqsl2WzIPGTYKUYCMZLeTwrhKbhdCZbI2mKXTpILfg17Lf9pDeqnU7yMEDZg5Mzj91cb/QtCbZHp4C2BP7vGUyg1b+0xS2TWoXxE3deR5C1cir7H1aopvd4gJ5xnovWiLK9EamKerFmWQDoM0woF2+Ko3/aGJEEqf2fjCh0sOb6fzXwO/lN2JCDqjDsYF1oq4WqhaPC19iE++5Nuu9eTx/VlAdB+nt5j6ybQLIzOB1rnM4q9Gx568RJwNpqNTY9KcHuFnx8mtjQ87wkrjtW7gBqKokoNE50UuEJDyw== 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=NVs5N3JU6rpeTiI4CFfGlJke/DLoz1UnDQ+whSU3fEg=; b=CzCRj9U9duw7zFn/25hDQ0/OYTXrAbaZeaaQLCs1HKTTajEqaRTVX04Ko0JCWltmaRuxIzzUSTlpqrDGKbtiDdVegDg8fUDOgfBt+odo/ssLkALzBaxBF0Od7LD4VfA0euDIH0IzspaGUnqKPUnP/fcQZ94d+NEPZ1SfkvLYLPE= 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:45 +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:44 +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 4/5] Use the .ACCESS_WITH_SIZE in bound sanitizer. Date: Fri, 16 Feb 2024 19:47:22 +0000 Message-Id: <20240216194723.391359-5-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: MN2PR06CA0022.namprd06.prod.outlook.com (2603:10b6:208:23d::27) 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: 24bc95f7-e4b4-4ed2-181c-08dc2f28251e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Wu0vvvWpDWGvdiiBanEEk6Rr+0pNKTYt4uBGterwn0ejnJa6ZrFrPe+rqOWTmhYQZEgHwko0WObSSLsLv3XckTsZ+0tF+aj7iKc5fqu1X4FfpUhX8WlVObvWNB7rwvAk+g+ggojWChjq0/yQ+d8njem9UTvWaAAINIo+qk2LmnJJq8f/IR1Im1o7l2JxoOzjeI0G/u2DcZJoSy+g64Wrl00SMGZeZ6095mS+i5O2oUpwcJBsfKutPYOB4jFQmynb0p9Z44LoQ2TJpBikBfPQz80KoC3tvKlh4eChWRvJ4NxeKv2aBODgU6wASLKl0BEkpVIDRvdhf7Ab+S0nlkKorgI1smcPWFhIMvfi0Tujs3z4kBY0iWV4LxWSFad1iRwBm59lElNWZyh6PtEHBAuSEWEd7MPS5fDW5VVykRi3OyBwZV/dAjnZ3683v1KgbiFJvGHGRNuKhXL8GfZKs3Eo3JYzENaZ9OtVVLFRjv4TbDfGkhgiaANWGC0b4HX5lsv3FG3T1y42lRgjC3Bj2plLV+1FHJYql5l3THQHWtF4QHA= 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)(230922051799003)(451199024)(1800799012)(186009)(64100799003)(4326008)(8936002)(8676002)(5660300002)(6666004)(2906002)(83380400001)(86362001)(107886003)(1076003)(36756003)(38100700002)(26005)(66556008)(2616005)(66946007)(316002)(66476007)(6506007)(44832011)(478600001)(6512007)(6486002)(84970400001)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Itsp/ZfTBP2i0xE2grqIPaVQdD+tE9ImroNvr2zxuhtCBFMIGYLk22xyAeiXCADlE9RMMYDMzGSMQhCs1qWNLp0jM9IFHve6V25Se8txxkEP56dgPERvwZ4TOqEz/vzixek1M64CNXygrluFMnOpL091M0YVApGaY6e1AJYXsvvJYqn/ypj8U6zpJRHVFLVVa5qtaXL16MKc+lnXdhHatsJAJC7wChhf5dSM23rACItZKVzB4e+hgf/D/zyOcCEK6is6d7BH9TlD/1k30K7fAt9BAFO2ugcdELEPxyU2btRcaBixrX6a84mjl/5gLHrzM77Sck88sDlxdO9cE2m7l+f7i33yzFI8CldnSJtkGqMQJdxb0N2ofQeoxuijqSknHlGHIdbjT4kr3yqhf2oLSMZL77mN2917UoKBHZRF55Aeqf71NBv7oVMXBPRMloZ1/lufOvGrWF6gAQRbhqsvWw+c6yJ+2vtKmI8yLza9T1IZYRjL+yF/YEZJmQd3RdHl2YA3aNp3/45TR7TSSwO5V1nONHh9rIH3inaj6MARwY8qHifAs7fzW6pn7t7AmKLlW17ME4MKa4UkFOaZS9WpP9tp0qpTgMh82iciX3U4Xih8txjsYmLJICzL6A7X/s3v3yXQjXOOoxA7CqhIF77rFNDO3XWBVjaboTMSW8/wWAyAsJch5ggytqSYffRLG6wFfTda0VnIbxIvQL4ulI5NmYDV0zTHfecY6LdXpXtC+R1x+VRBgKxAEgb+apKukhxQOR0hkkkxXnSqf3mafSi1lZw25tu2Jo/uFZPnaD0hXV+h4lZp6NZOZgyxaINsIQz28Z9RPH7snMc0Q/ZkfbT84PmhyZJ46x0Gyl6/WoQlVaS14cSYSc7SkghyUFE9d84/gurZi8ex1QGaq6pm9Uv1ieNqmoeeIV5MubreEDBv6AT4+mh6gBRkhCxSGf1DQVTjbH/mlQSGwGLGU+WmEHWXq6TK81FMCfbq8mFQrVV+WxrNn42kJalH6osoT2HV4lifAWKkj89QI51JuOdUE9khl4Se2Hhmno13np/pgYWbffl87Cpk3fXbO3Q5azlbKI8qGGZc/tZrD3JRBWunWltmZ05EZxhqh09z1vnsbDkXQJQgnkFJe/BY3YBiGsuap6iCuNPQorecp7L2qEfYLm+yrHaImfUZSMImbQs0FUMHOgxBSl5/SkuOi156MroKWE+S6ZpDO7dM/PuLqCIAje0kdg9CsE3mQJVypsPbOp+t+2TLTaxcGSYxvlIW29Vh+Sqz4dD+5LmTubuM7D//BocYqVr0lG1Ec93Qek6cRavulq21PZmBaKgP7Jkeb6JayXJfWe48DdUvkCkqbT8iJDyzPow1C09uQB/YnuXauftQCL9uHywpSv6hMZkwZpbZAM7qtNLzkTfyQPLiDeMac+FDB1mG6iziq2OfsSW+xTKKrSgbR47knHjx70MVZT2aEDw462ck6dZxiGKV3frF0pPiOFKzdBpNfxgOL6s2n7bhtunGeOWVHoeYK/rBCt3vX3PoyEp9OTUCJG9m11SEAr7Jq7Ul2opo0wsyXS8hPOs6QSEKDw6op4d8QMhgoCe8IjJz X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: Gzq48VKX9IwvKfxLlvmJnCMfDTptPnJY69iTjHLCKEz+IQkLgEQuERR2+ZuBwJt9+GL7eXW1U4ZINRQN9yvaAcD7EI7z0EltoAkYdCCsoD5fh90wil/bdh8GzYp1HCoJZDGpgM9KcfAHJTm5FxvFb25zSwPWRFJ9Y5nLH41iTBaJi3TEHqY2Qdh6DqRgOv2Rl0heSn8mWazEya73UT+Wu9c5IMJeBkxnx7QJldQ7mUIj6sMs3j7k2fEQho05M+1pj+RL0lH7RzQO9Yi9glIu8/UzZZbnp049LcEaMoFMDZB0WuzL55LoDjrylleRwsPkv1R6cwgcspjKykI62w3bHjTzUnhJ44AXvkbvPhfZu9SR5CWJprHkcjhSp2Vd9h+n5uc6wNZiKmPjWpx1uCGe8a2kp/xSdjqy0m50n+4pOzUl+eqMdI1C8O4Ri9EDPe7M+wPHe5hnZQ9USG0V/08y7u7oRBQJVsaES7d5zQbJfulRLbb90CF0NNWozQywgoJ9KtOhNXErgtacTCn6vYleGQi8O0LfXrYhSA8pbcy2LABLuxnS5XBFs9Q4HSC2seQIiAOMPo3yriGpJX53JFNTgZtH82zB6qI/F4zac+iAOQc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 24bc95f7-e4b4-4ed2-181c-08dc2f28251e 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:44.9311 (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: +0klReQnrw1M+Sf3K3U9S4rUshnhdzipCSif/EVDwJ1tPZ42PdN0ibIsP0mMznXaSktMxJqekOA4104H2Dz7JQ== 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 mlxscore=0 bulkscore=0 suspectscore=0 phishscore=0 adultscore=0 spamscore=0 mlxlogscore=999 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402160154 X-Proofpoint-ORIG-GUID: lFs5WjF_k1PKx7QV5sfE7m-mI05EnShn X-Proofpoint-GUID: lFs5WjF_k1PKx7QV5sfE7m-mI05EnShn X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/c-family/ChangeLog: * c-ubsan.cc (get_bound_from_access_with_size): New function. (ubsan_instrument_bounds): Handle call to .ACCESS_WITH_SIZE. gcc/testsuite/ChangeLog: * gcc.dg/ubsan/flex-array-counted-by-bounds-2.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds-3.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds.c: New test. --- gcc/c-family/c-ubsan.cc | 42 +++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-2.c | 45 ++++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-3.c | 34 ++++++++++++++ .../ubsan/flex-array-counted-by-bounds.c | 46 +++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c create mode 100644 gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.c diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index 940982819ddf..164b29845b3a 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -376,6 +376,40 @@ ubsan_instrument_return (location_t loc) return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data)); } +/* Get the tree that represented the number of counted_by, i.e, the maximum + number of the elements of the object that the call to .ACCESS_WITH_SIZE + points to, this number will be the bound of the corresponding array. */ +static tree +get_bound_from_access_with_size (tree call) +{ + if (!is_access_with_size_p (call)) + return NULL_TREE; + + tree ref_to_size = CALL_EXPR_ARG (call, 1); + unsigned int type_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2)); + tree type = TREE_TYPE (CALL_EXPR_ARG (call, 3)); + tree size = fold_build2 (MEM_REF, type, unshare_expr (ref_to_size), + build_int_cst (ptr_type_node, 0)); + /* If size is negative value, treat it as zero. */ + if (!TYPE_UNSIGNED (type)) + { + tree cond = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, type, cond, + build_zero_cst (type), size); + } + + /* Only when 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 ARRAY_REFs. We create special builtin, that gets expanded in the sanopt pass, and make an array dimension of it. ARRAY is the array, *INDEX is an index to the array. @@ -401,6 +435,14 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, && COMPLETE_TYPE_P (type) && integer_zerop (TYPE_SIZE (type))) bound = build_int_cst (TREE_TYPE (TYPE_MIN_VALUE (domain)), -1); + else if (INDIRECT_REF_P (array) + && is_access_with_size_p ((TREE_OPERAND (array, 0)))) + { + bound = get_bound_from_access_with_size ((TREE_OPERAND (array, 0))); + bound = fold_build2 (MINUS_EXPR, TREE_TYPE (bound), + bound, + build_int_cst (TREE_TYPE (bound), 1)); + } else return NULL_TREE; } diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c new file mode 100644 index 000000000000..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-3.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c new file mode 100644 index 000000000000..33bdea1c430b --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c @@ -0,0 +1,34 @@ +/* test the attribute counted_by and its usage in bounds + sanitizer. when counted_by field is negative value. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int annotated_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int annotated_index) +{ + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (-3); + test (2); + return 0; +} + +/* { dg-output "24:21: runtime error: index 2 out of bounds for type" } */ diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds.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" } */ From patchwork Fri Feb 16 19:47:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1900233 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.a=rsa-sha256 header.s=corp-2023-11-20 header.b=eiVCCCGI; dkim=pass (1024-bit key; unprotected) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-oracle-onmicrosoft-com header.b=WfjFJre+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tc2YV27gqz23ms for ; Sat, 17 Feb 2024 06:48:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3150E3857716 for ; Fri, 16 Feb 2024 19:48:44 +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 6C6B63857705 for ; Fri, 16 Feb 2024 19:47:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C6B63857705 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 6C6B63857705 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=1708112874; cv=pass; b=TYP4s5HrbfdjQXYXXEEX85oe6ENUjL4E3rqT2thGUCVrlVT3A4/38t57NWczIith4dDC3D1LR/AEFwMUOKmqLkT9my1gZgzKAqc6oAz2sKpKL3s1DMYwkq2yLDPA0VPUvzqiXtR2rfXG1FeJDOe036+J5i4R0BjU851+/GWRpvY= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708112874; c=relaxed/simple; bh=VSBb2xJ72uoKEOycV54e3OQhCgpje6Cin0GiRWPuOsM=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=uUByYnW2BqHZY4+T+vTeibQ5ecpyvlB6tL1bQN6tnTppSyFAOsjLKi0ksYu4eyZGS7JgBb3Kn5g5EtfrqoEbuj0uYc9FRnxi3XyAo/kWBfFZs9EHr358lCGaUclht503bTO/klN5VQ1VMkhu7vONwSHNvsZHP9D43Bc9YgKHgZw= 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 41GJVgG1015639; Fri, 16 Feb 2024 19:47:51 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=bdUoxJy8S/jcGDM7ZZCyg7gimp/kv+oR3hrfmBNmtik=; b=eiVCCCGIAH7Ux4R/7Vib2LKuQzJtf/4c9QW8DtoHnNve3d+KgHRVP+3ZoMAj+SEEjwF2 2QCZIZ6+bi5iW9WGv59V4Bt13rSZCVYBjVkFKZNYVNCpfgSSW0Y0wTRbLw77vHmvj6+K AnY6sDGMizKAIhAzhdPytxS7Iqf0BKvY3lNtU2xi6P8SPrezANZ9locP8a7c3g3KEY+s aqwWCUwRE/pQ5XRMTqDIU+2M6bhWhysEFxFQk/Kz/51MleiF2XbOCtsy23YVizyhLxD7 I0KL7wyVgXQ0pzCdlnBfGraVzf3/d4XEe7sm3MxiZbXQvFJgnMZ6ObDn9lJVYTYv1ihg 1w== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3w92db5y4s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:51 +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 41GIw7nh031672; Fri, 16 Feb 2024 19:47:50 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3w5ykcky68-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=M4LOHgbFoOQ2NUcjMGXIguGm1x+5qNM8bEo0eZt3jVaYpc0BFmNNct5aHV55JTN9h4FDh6gtVdzSo/rXuiW6MYJ4GK3wMccgI/8Fp6zPoFspeKF+u2FuYZk3dqsUqPbPPf2/Q9+KOVWyvoBJXXr/dmcJw3DkEnerejGJS97cAUJPAn+LgLaT7uEIuMMbmQKJWlEsk1p22r7/mBUefz+7mFcaJm/9qPe9JZOgbWxnI6w1rYKDPHahDDx2U4598UO5lcU75YALXNAXLfYFTyi5qWUxfQDCRX4SS/cCn9CF7czVzGFhYBOanhg9uLkk9SVETDHfVBG7uKKBbaSVaaaD5w== 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=bdUoxJy8S/jcGDM7ZZCyg7gimp/kv+oR3hrfmBNmtik=; b=VCP503459qXrSIETaUuPCE2HKWYLc/7mp/r/ethrXa7AxUG5Rn3ncKb6MtdbTkdGiOlHlBHBv5diFnWLqZ+2jfoqO9y7yuRhURKL6Ef3VI10rEV5YIvP+QOQfKDJSfejZgLJ8FZmKiuQjPUn+QoJx0OM3GbC3Onj1DIhErHrs8S3Kh6HG3hBBRmZ3qwryw33uBzd9Aroa5KgwPXyaz14NdDr6VaohFwnfO730i1NL1AmKxmj0Ti2+DUQaNk0IZNSNbfQk5ng4t2D/tmSBrv7ZtXBFP7ovE0nZQ03Zofrv30la/DKcGTZRW+yaOy6hWBHDTErhLi0xArSgUOGwD7w5A== 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=bdUoxJy8S/jcGDM7ZZCyg7gimp/kv+oR3hrfmBNmtik=; b=WfjFJre+iA3MlSLGNKvwQzypQ8su1d7NRx/wJhSE2eaaZPO+M7v/OnnOLgPV5cSGndViFxHdj/kORCfgKtr4EVc1bNM0gAscDJMrHIL3aSNIR6OHe1XRzHIROP3sjEYf0IUeDBoBUPw6JlF7hjY1j4X+h2nfACvRcyTkhcBZtkE= 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:48 +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:48 +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 5/5] Add the 6th argument to .ACCESS_WITH_SIZE Date: Fri, 16 Feb 2024 19:47:23 +0000 Message-Id: <20240216194723.391359-6-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: SN7PR04CA0113.namprd04.prod.outlook.com (2603:10b6:806:122::28) 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: 7a0c5ec4-05c3-4461-68f4-08dc2f2826ee X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HfwMxUUky3rSsPHm9HFRqC3lDkw9APDwSMkgq70yzZXkclniuFRX4e78QdOtaceVT881o4Pqhk7Wca/6chp2TdYEoftljdJybZLhMYLg3rq55flAKkwsmNCvXg63D9N0T1+kpEQGWwIheICewEUu47VwQ8fcj9yzmEO/1NZG0ACw2zmzKyuHspURomdm6O1cWAnJyztaL61hGWBfg4bz9AmmTlWoMb2gXzN78xAVDLW6lp0eRFcvkRhR6ChXnSRY9Cg9WS8d2aKuK1CcJib1EUQGRlwhXso0R7xzUwYSdP5TA3TNm/smTRZTYe/0Z23mqCxG83qEq7n0pY9ueb1I+o/TVWKwRIJLzVn/czI551h2/D9tjYByqFoyW01izJUKvV8SW+XyHm/00YwxPmA2Ew4jlTQmV3abgnxgXxEX/xpt2iGM2BQ4w9ov5ZmkbBdFLw1d494DrbXJ0NeH8QgMmpPPniS2HbiausUsG+vAb8gvtcj/iJUnAJqGSk34JAaVKw2unIYLDQvKZUldxAxuPjPaRZzpfAGksNrSY+yFkJ4= 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)(230922051799003)(451199024)(1800799012)(186009)(64100799003)(4326008)(8936002)(8676002)(5660300002)(6666004)(2906002)(83380400001)(86362001)(107886003)(1076003)(36756003)(38100700002)(26005)(66556008)(2616005)(66946007)(316002)(66476007)(6506007)(44832011)(478600001)(6512007)(6486002)(84970400001)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oyH0JVA6qHSZBS7VWxubJHVMmzst1is6pqG2vu5GhzrC0ULsMaO5L4WVtb6FoUpH+6Wokq43ogrSCrEtOAvTGX27O/niQ3n9G8909KDCFAH/ZpulN3Xg+7UwjAdfzH+DzIQq9hNItLzHNGYNbHtEzM7bYveFpaIvucQ54oA9CP2QkDOc7QBHyZ2Iahf9GOUMir9dQpMh6VqgDlF3v119QMX5m5W1dCI15Nd8Mj2enKaMuX5YU8cXf1v0qnYDjdV1mTN9H8l6scSS5HBre5A7eYSM5YuU58Zx35D/icVXi1J5qfCEvwpTlREsNjs/CqgEGqzpDnjTABG50zSvEu7ZrGqcwz1y6FZOLL7slvW+OjqN7oOVmwstySNhTypPjaPsYUL8hqcdGKjDahR2IXqvPS7+0oTfOqqo+AgnH3Vd0BoeHe7HLoN6QFT1Ol1H9Zyn208OA1c4TU18rl4brr1ccoK/w1w9MLARmEMVDpNzVCjgg0/g31Dii8kISJ6RmPliIk+VOz2tpK3IuL4pP4xPqddl6UpOZGcfs6flip6z1NQAq0wWEZVmwqAKiNMm+MqR/nsXU+9h96h6Urfg2ScSNCWd/R9EwJE02/JRGCTQMfb46dNDZ4HDIjjGNSDMp9F3OYsrnCIxLevcLcm3fm3ClN1TMZXrfEDWKYv+tMakT/nh9d87mMsjfQAlKHD6G91acu0048aLPG3mLFDCmDkaP6TlNDCg2l5ILQFXx0haBGJ7E4l46wEHDHMd5+feetbkRUxfb4IsnyAhsWYZsp05copheKLPah2sMI0bwD3QLyJ6zzPhf2BLuCr0ufvDQ5VDYxV+l+dINEQ8OsDCW/eMoTs52utqamokGJAhVUEyEnCT3uO9ME6jU9c05P4uN/HX200jwfP9amDP753usbYwvGLzXJbPYu8BzJBXKtrU9rGBBLUmUxvoNi8ibJcLWCFt4iaIi9KKgWSwf7388I2GWK9VTo8eABJYKB6Z1nTTpwUgcHY3110fHzwrlO5hB+mIokSAVenfJwHr7OwiIKhRdW+iXwC1eAIB/t+SIqTWguBNF6s/qCfwcBHYFmPjrbwFsBjtbTM1ukBT7rudAg0i80M86+VpHG2VckWNodGvzlu6JODDOMSGGCTEjx2CmfZzl2G/eQM7BKoKt5rHudTf7qdGGtnR1V8U8o2M8i3aai01KPYbVjZ9wuQl9lUM23EzPWCt3lUl5WDFYVwS6LEYGnrury0tvFfXhUb2a9TtRUqhcp95WjyRKEjlu2eK8rfWZuf6e4JYgsC59gtgP5ZYWJHvupovkVU2j/rgtAZ8pEH5QwjqK2cYuFUoRn/Nlfx+C52W3+2pV2VLJzbBSpdsPAXrFUftHzJTE2UlFM6s/IOAEED5L6VUDe97hRxRpWpTPT7fKdK1UW7z7Xgnq1x2WaKx9AaFR8p+HAMCcUzk/fAa+ulRWHKwyDCwo2Zn9mpMFenqGzyi5hcj35RU7CKwZaOvbb04qvmKo6ql/Dyii2mb/LHuaqlEIkATNfjK8347YkCp7iUX98sjdbmSPOmYIaV0E4RsHEoJ/Zvjyyi6bkzDaoz7GVrFJ3IzxzS0SBUn X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 8nKSRnunB5jo+6s9AOmUB6r+QgWRVnXjbBnZFU7FoGXcWXqv1PNv156hjq9iGip2F6FQvXeGN1APJD+t+eZKOgalyPsxjCk5w5r0sKHTBl+TePWP6elO25ZM3EIjA/3yjjcQjf9FWgc+mnfth/6grX1+b69pAlkfO4sNdBovi7WKvTENWAikZi+LbgTlro/24m0TNtJxsKbEptcscaXtoRQToYCL+HUryTgc5ngR+RzzCOxOtaqPMoxrdInLn6A54Yt3MdKM1wu2vufzgPzl3PXUaRn4GvFGEe5iWi3Wqvbw22V89pbWhkgdMDQMfhkEju9iIWrU2MhaU6X9N1uKXXtgr1SwvKHQq9cATewd1oxctxBMLGMrsAXgMOeaCfPd8NqVSW0luoWngdSqExmOCjL5KKvKxnnz2qeb4wFZN9piEyMgmrJgi61/sSmjb/0vijKefeDUx5R6NIDccf2All86kQAPR4ft72u1X4mUbLr9eB7q9vkD4K8lUQtbSft+zynVDGUvXNWSLyrnljEGcNwtJfjUoUu8X75E2tCctDt5Lajtjb3xdFFedjzQVtwen8uhsn4wsN0xuEZ36LzopZRUJqmoqpirBQBxfMoR/uk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a0c5ec4-05c3-4461-68f4-08dc2f2826ee 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:47.9591 (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: j/M9qlqOtcTlozXChfojrRCt8bRKyBmUZ7GMG4HFbgTAs9OkVpquOdWmL/T2zb0raIdqdSvWhmFbTmY4EF/gQA== 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: cWI9V_HdKtsvNT2ntohwON7e8vtRS_xG X-Proofpoint-ORIG-GUID: cWI9V_HdKtsvNT2ntohwON7e8vtRS_xG X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_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+incoming=patchwork.ozlabs.org@gcc.gnu.org to carry the TYPE of the flexible array. Such information is needed during tree-object-size.cc. We cannot use the result type or the type of the 1st argument of the routine .ACCESS_WITH_SIZE to decide the element type of the original array due to possible type casting in the source code. gcc/c/ChangeLog: * c-typeck.cc (build_access_with_size_for_counted_by): Add the 6th argument to .ACCESS_WITH_SIZE. gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): Use the type of the 6th argument for the type of the element. gcc/testsuite/ChangeLog: * gcc.dg/flex-array-counted-by-6.c: New test. --- gcc/c/c-typeck.cc | 11 +++-- .../gcc.dg/flex-array-counted-by-6.c | 46 +++++++++++++++++++ gcc/tree-object-size.cc | 16 ++++--- 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-6.c diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index a29a7d7ec029..c17ac6862546 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -2608,7 +2608,8 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) to: - (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1, + (TYPE_OF_ARRAY *)0)) NOTE: The return type of this function is the POINTER type pointing to the original flexible array type. @@ -2620,6 +2621,9 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) The 4th argument of the call is a constant 0 with the TYPE of the object pointed by COUNTED_BY_REF. + The 6th argument of the call is a constant 0 with the pointer TYPE + to the original flexible array type. + */ static tree build_access_with_size_for_counted_by (location_t loc, tree ref, @@ -2632,12 +2636,13 @@ build_access_with_size_for_counted_by (location_t loc, tree ref, tree call = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, - result_type, 5, + result_type, 6, array_to_pointer_conversion (loc, ref), counted_by_ref, build_int_cst (integer_type_node, 1), build_int_cst (counted_by_type, 0), - build_int_cst (integer_type_node, -1)); + build_int_cst (integer_type_node, -1), + build_int_cst (result_type, 0)); /* Wrap the call with an INDIRECT_REF with the flexible array type. */ call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); SET_EXPR_LOCATION (call, loc); diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c new file mode 100644 index 000000000000..65a401796479 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c @@ -0,0 +1,46 @@ +/* test the attribute counted_by and its usage in + * __builtin_dynamic_object_size. when the type of the flexible array member + * is casting to another type. */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "builtin-object-size-common.h" + +typedef unsigned short u16; + +struct info { + u16 data_len; + char data[] __attribute__((counted_by(data_len))); +}; + +struct foo { + int a; + int b; +}; + +static __attribute__((__noinline__)) +struct info *setup () +{ + struct info *p; + size_t bytes = 3 * sizeof(struct foo); + + p = (struct info *)malloc (sizeof (struct info) + bytes); + p->data_len = bytes; + + return p; +} + +static void +__attribute__((__noinline__)) report (struct info *p) +{ + struct foo *bar = (struct foo *)p->data; + EXPECT(__builtin_dynamic_object_size((char *)(bar + 1), 1), 16); + EXPECT(__builtin_dynamic_object_size((char *)(bar + 2), 1), 8); +} + +int main(int argc, char *argv[]) +{ + struct info *p = setup(); + report(p); + return 0; +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 630b0a7aaa4b..c3098c521a43 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -763,17 +763,21 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, 2: the number of bytes; 4th argument TYPE_OF_SIZE: A constant 0 with the TYPE of the object refed by REF_TO_SIZE + 6th argument: A constant 0 with the pointer TYPE to the original flexible + array type. - the size of the element can be retrived from the result type of the call, - which is the pointer to the array type. */ + the size of the element can be retrived from the TYPE of the 6th argument + of the call, which is the pointer to the array type. */ static tree access_with_size_object_size (const gcall *call, int object_size_type) { gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); - /* result type is a pointer type to the original flexible array type. */ - tree result_type = gimple_call_return_type (call); - gcc_assert (POINTER_TYPE_P (result_type)); - tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); + /* the type of the 6th argument type is the pointer TYPE to the original + flexible array type. */ + tree pointer_to_array_type = TREE_TYPE (gimple_call_arg (call, 5)); + gcc_assert (POINTER_TYPE_P (pointer_to_array_type)); + tree element_type = TREE_TYPE (TREE_TYPE (pointer_to_array_type)); + tree element_size = TYPE_SIZE_UNIT (element_type); tree ref_to_size = gimple_call_arg (call, 1); unsigned int type_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); tree type = TREE_TYPE (gimple_call_arg (call, 3));