From patchwork Wed Mar 20 13:15:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 87409 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BAA4A3858434 for ; Wed, 20 Mar 2024 13:17:16 +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 54CE73858289 for ; Wed, 20 Mar 2024 13:15:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 54CE73858289 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 54CE73858289 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=1710940546; cv=pass; b=uxlOWtJiOe8lgAiwrww0C65xOlEcaV/jhRQmZRuawuoyNdnsGckUVQULhEBqTm81lH4ztgthiF1Elw0XMpw44SMxIe9FPHek0qFb28iwKM5Xk9DupWu3hu49ufu3WAIHaM9eQqCQZAGWcqMYxnXGLKroWfnT/5MEmSvzfTG7xGU= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1710940546; c=relaxed/simple; bh=Oto5t965O2b5BRdlzyUI5NVZm49ypmOOjJ66n5dWNGI=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=TJmgxSIIUJw7b0mpWzk2Rb6tx4jF8d/FlJ/WyBmign9WL2uGvfkHWYCprNVJnK6rVL1aDwen3onaDv0JzT0CUV+Nm+p/GsGprtvbS1AQSpvSuNtVYkNm/IwYBlCX3Iqc9CEUSVimTLqPy8KUMM26gBdEREcORy0wRgVCA6PR6V0= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42KBxE4b004649; Wed, 20 Mar 2024 13:15:40 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=FAG7oZM3DS6R1r1khE5IdasZ5g+2Q8ogq51Isa7ONio=; b=LJ3jibNyl+B4LrFaZwJNNTJPhmodJphX48BUwYTi7kgfXAdtpvMKymIGx7DZkog/swPY 0OatqykwBA9q1RF2cvcVhtJYj2ED9iRrBVKy12VccDvDr/6fYA1GciuImVFSviKADBLg ZZIPoTXdC5GPP0UrwxidSLuiyDBQQ3vX5LJdivs9hBRmOJigd1uEO5Orc8Jukd9UVKOy LDHv6MkFxkamzaO72g/HHhyLez5nVDiAfxZzPRitem0zCbbsvkv5BErJhZK5yBWHPIjI KsqPgu6vyv+/z0eZvpfwL/WxluRSaSHZvaT0yJIF27Moa8Rviq7ipznOtU+GHRpu5kk4 8w== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3ww2bbr348-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:39 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 42KCGH5n007556; Wed, 20 Mar 2024 13:15:38 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2100.outbound.protection.outlook.com [104.47.70.100]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3ww1v8auma-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Mar 2024 13:15:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FCWcdufSqbEdBNUjT70tODDnBN2P45JJhZxQovQ1zWuFiu0s3kvmoUQmmcNaJ47QVdWBS6FFf+/+m0R8YmeM9Od5DBJzlsxtjiZ4VXIT6JPeV3bh18MNK2YgtKw8PeegT5Bc0qAuqCV4Sq+PurxYHb0xKVg8YsdFI6cjNIhIn1NPpzpplaZEyg7EAdf4kxRZrVcm514dOuCUJnpkRJd+mXOEDNACoAZCHP7ZIYmihgge0BscWG5MnVvHyay1/YYQ9+BJ/upCmwe9BlwTKmsqltQ177j1S911ds1OXoZTezZz3Qla4RKR4Lgj1/jPQ82V4euLe1JcLDrOyE0aQ6fGUA== 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=FAG7oZM3DS6R1r1khE5IdasZ5g+2Q8ogq51Isa7ONio=; b=n3lRmJzINcJERLXWa2dRiN+MvOFK09KnGvC3PMkzcWu+6z1G1rXOhIBtpXFPX2+C51DK6KZScLnTxFvC8aZyiOLH+KsYY+f1r60QL/nDXvCIJtPgrCXR8fy8HII6IMHIRXwg/I4cZmCC20aPaNBf2Gf4VhHlCcaPK+oEGIRVYrYlysiUjZ6wJ1l+Fu14BwRdnAXeRFotMmtpzt+hcFSyg6rAR/HsG1CnDucg/NQhy0J5IFI0zhFqfL+1No+qSDoWWi/ECmNyoCq/tzVV5q8WAPituk0omz/FpM9X6+/3U4rdXktM3qNWcXyg8tC0Ivb7Oc21YbDQVnq7xo0AuvnmIg== 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=FAG7oZM3DS6R1r1khE5IdasZ5g+2Q8ogq51Isa7ONio=; b=epi6AmqeSYx2XK5xEwGLe+CKabo/WxuwxqAYl1PujIlJOqRPoRN63pez/CMxv4kV5bACO1BeaQTSgt4hpRCvIjUnp91F0HX5jU7h2gZeLNaDJJZugLWWUzOiedTmTJYP8Zsa0FQNKtilVW17jQ6QauQv8uWBj/ypK3ezySeLo+I= Received: from CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) by PH0PR10MB5562.namprd10.prod.outlook.com (2603:10b6:510:f1::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7386.31; Wed, 20 Mar 2024 13:15:35 +0000 Received: from CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc]) by CY8PR10MB6538.namprd10.prod.outlook.com ([fe80::2dae:7852:9563:b4bc%6]) with mapi id 15.20.7386.031; Wed, 20 Mar 2024 13:15: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 v7 3/5] Use the .ACCESS_WITH_SIZE in builtin object size. Date: Wed, 20 Mar 2024 13:15:16 +0000 Message-Id: <20240320131518.2292317-4-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240320131518.2292317-1-qing.zhao@oracle.com> References: <20240320131518.2292317-1-qing.zhao@oracle.com> X-ClientProxiedBy: PH8PR21CA0012.namprd21.prod.outlook.com (2603:10b6:510:2ce::6) To CY8PR10MB6538.namprd10.prod.outlook.com (2603:10b6:930:5a::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY8PR10MB6538:EE_|PH0PR10MB5562:EE_ X-MS-Office365-Filtering-Correlation-Id: 8bbf0caf-33c4-45a6-433f-08dc48dfd41e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gZj+rbIoWehkJH9FRuNIUTtx/OdiHaSURdn8osoPcbElxUbWeOloIdfPW7AnCg8JH7ovTMY7ndYjIvxw9tBK7jpOdqK1ETyzAux+kyJMlWEJIK/YTVo3bvB9T03C0wonvA0GvxWiEHLuzcTjuyhti8ChLxQKSzYrPfydjs5TMwvZwydxpvtF0CHDOMb5o1EYl72cDflHupIkrp8EzzUcUv6bLxjGl3+qBMUw/69B5jRIZpsXFwDnQYc9sB09Qs1hg/GdNMWX5hZfy61wEFQ7PXUKmRbGvc0+zDq7M4Jbi0bDwBy0cqV8xesZe+SxtbNH7tN2fKkrUvc+6bzGh6s/71bSx2VMdpOL65Qfm/uqJ4LjpqkrkLfyU8H1baLmP8jqTttLPeCDl49pLlnrS/91FiYV1RrEc9YFfQSJqkQV9hfW4e/AOtDCMY9cXOu0WLSat8qlL7E2BYtdyyAe34buBzMkGoLTELhKQNJ933CD/2HrKgD1d+WVtkGgcGL6bDzcK/J5hpTRd+dQPWRKUCbzx+dh5wvfJ40ggZojzTkP2vS1u8te0R/y+6XfdFgjiMh0RzEedzRYdVUxkAdWkVLIfGRWFhFPAbhyKfjaEZiFEJGNMCXKYsq294KCPd8OdA/l X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CY8PR10MB6538.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(1800799015)(376005)(366007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: NuvXVFJsQDg34DkgSEY0UH/Tp3Tw8lVR+n9ykR7ul+yJoWNmv1fForS+qhsTi/UffYQigNTce9Qlg4buEtE3KzoxFZLMqDjfcE0Qrhn0zttPmcgN3uTLT2kCjQ8hkQijIof04WIhSQ+x9L1iFSWy/pszhGgPEP692q4V77Uckqx3/nl5yivc8ns9IoZrZ9dnvsgdTuGk1sujYhK2Vw+u0HhBp/MZwI4tQupWNHeXpAM9HcR4BhfU1j6cokSbTUMYKfAReS5bOECL3ovN/ZOjk7xBQgFuDrVUWy5DKgtYyY5whLybepCmDS4Dt7lP+/GxEd0v7WPFX9OdG10BUVq3Z8ZL7TnTPWa4i55jlkH7QY69VY5XfHXg8ftPXfYwVW85ka1aoh2Ylkkdp0pq/+pMVJ6VnZhhmm6+9EkNYUubELXD5DJPfHGsJ2R+cZr3uqj7WRYY9SXmWv0rPLWNO83u23r4Nll9pxjd7kpJ9T0wVbcCIxNDdNQIPOoBqbTMVNTdE2ItZvMarHI9xbsDhjtxWZubAbSnpQfPyXaAhb/S6CxQd5ChRDFOn80C6yEo0EIVcBwmBepD1To6Fh1Ztm+067Zl7w+IVsmrH+UcGiYLtjRlPBTDF1RIatFvNjP/7BTi9EndkTI5hz4A84+rARxo565vJ96PEK84u7TPOMRwkIFBgMXG3za/9kuaoxk2D0sILLiH92tZtHNRQEaFRZqfFYWPMjqjZ5AmCRFwgj08tZiar846n4dcga4IdfwJINeb5cwJxNCDJgqdaEMJktoUu/NsUXQ5YSqzITHnkgJmdwn2sRWaQ5Yzzoi/D4/N5Owyq70ueNSqBXKx170ZsmcHdKhqVzzCzJ0l28s9b7yqyrf0uc6JcjgOr9jP/UM+8GHko0Snq0XPvkdct04g4Id0xnfYmzfSd2M2zPbhyugsKJtGuddkC/tkDdPy62TykMaQvJfc+aq/Z7wUdNGUnPsB48BL/cmg4Iyfgl+7ImpOQyNj2shqc7+W/y5gpPGcELuwa2SUp7rhO9oMx9E8OF0y3SN1OCLoPzTDPz/pjyYso91PVylIvuoVSDFSM3l9LYs1Q/S948YK+eJbiDwdRYiwyXnIE/giZRSI9eOGttaGtSI0epBo4XNj3igfTX27IJT4EHJ/jNofp0HSfRK4UtSZ1va56n5FnmaGprX4VG8ruRbZ49cc71E51kKtn0aATbr6H+e1Omk1FHlW8W2Fi+/XS6WFOh+cfFjT9pOaiCW2enh0pPUuPoabwLVdBvNxk793CofRFLsscVfjljnZzq3x6u9Jnc89sQW86+d6l063Sc9q/F/suWdz0djQDgvpM9KOul2+YHuZRzsArWd9OUEjf2eX4UiF09gGaeTRYqnP/xb90NXhyg+u/aYfERAOcmWAAoHsabU3/qiZn5MMpYmqTJA5RcNHeo+I6CPeLYUmdtDPFAsNW1VQZDkLk7v+hlT0S3WJAzhy/Y817bDPxmqTBBf8Z/y100iduJypwx8rWjOk0Iw6zGsof4yIohMntyXbek4FP7qUA/L1ALDmNGAN+NU52h7IdUbyW+YOhnPpg8BN0kSibQml+Xv54fPMHGT6 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: fb/Jn7ADszGVy4bp3nnk9MP1RNpaunuI9x9dcl33UtzZmey9KYH5B651HsJu+4hZSjmWsJbkoCD791+xSq7QMWLZFHURN1W/g13JSbi1VinheB0HwDfWNxSe9GaVLa2cMTzbyGZVIV63fKjRsf2An804dA3eCC2Yo0a9LJwrXAxax6bu1QS0BvFAv5GIXFT2G5tRhfqn+RN5jkMqPC72kSekflgER83ThGpWFeuBzlgz6fdwbW3RyFb/8URaMykVc7HDDnaQodE61+VB9xoQjrAd/9hr88BL32UMs6fBYKWqPG1qRu02S6/fJNhDTejLu3v1XTN+EvzBE/SFKgZzCbI7+VrGnRFb2Q3jROXdUvakfaZttudEOka5xlky5hMUU1pLfZb7GedfYmzUzLEEgTjCj+DoKhGN1M1l2pG/JZ9Puackskwaj+4oTDgfk/0XuAld2mrYJZ0eOHJpMLJKvuKqeiaChszsJIOI5SLHP4GVydosZE+2ZRbhKeZHmFs0j0qzuo+xNOSFkumKzfQVUwARUTZSJCbevq6hToD93qc4WM7fe+2Dkjyjd2Tf1ym2/d+iE5l+7pzIJ/rin2AbC3YbYN3dgU7XzeswjuGFs2o= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8bbf0caf-33c4-45a6-433f-08dc48dfd41e X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6538.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Mar 2024 13:15:35.5047 (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: 0CQmfpyFTm6lV7e4+tLJ4AxI3WjP1HpZ88uriXolGX88lv7BmguCaidf8EUTRDODZLm5I/68Y3s3/MnYOnHR0A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB5562 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-20_08,2024-03-18_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2403140000 definitions=main-2403200104 X-Proofpoint-ORIG-GUID: 5kiD-eZnIlxibVFcn2Rln5hOJjsCtTiy X-Proofpoint-GUID: 5kiD-eZnIlxibVFcn2Rln5hOJjsCtTiy X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org gcc/ChangeLog: * tree-object-size.cc (access_with_size_object_size): New function. (call_object_size): Call the new function. gcc/testsuite/ChangeLog: * gcc.dg/builtin-object-size-common.h: Add a new macro EXPECT. * gcc.dg/flex-array-counted-by-3.c: New test. * gcc.dg/flex-array-counted-by-4.c: New test. * gcc.dg/flex-array-counted-by-5.c: New test. --- .../gcc.dg/builtin-object-size-common.h | 11 ++ .../gcc.dg/flex-array-counted-by-3.c | 63 +++++++ .../gcc.dg/flex-array-counted-by-4.c | 178 ++++++++++++++++++ .../gcc.dg/flex-array-counted-by-5.c | 48 +++++ gcc/tree-object-size.cc | 60 ++++++ 5 files changed, 360 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-3.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-4.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-5.c diff --git a/gcc/testsuite/gcc.dg/builtin-object-size-common.h b/gcc/testsuite/gcc.dg/builtin-object-size-common.h index 66ff7cdd953a..b677067c6e6b 100644 --- a/gcc/testsuite/gcc.dg/builtin-object-size-common.h +++ b/gcc/testsuite/gcc.dg/builtin-object-size-common.h @@ -30,3 +30,14 @@ unsigned nfails = 0; __builtin_abort (); \ return 0; \ } while (0) + +#define EXPECT(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + __builtin_printf ("ok: %s == %zd\n", #p, p); \ + else \ + { \ + __builtin_printf ("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + FAIL (); \ + } \ +} while (0); diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c new file mode 100644 index 000000000000..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..d258d0947545 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -37,6 +37,7 @@ along with GCC; see the file COPYING3. If not see #include "attribs.h" #include "builtins.h" #include "gimplify-me.h" +#include "gimplify.h" struct object_size_info { @@ -60,6 +61,7 @@ static tree compute_object_offset (tree, const_tree); static bool addr_object_size (struct object_size_info *, const_tree, int, tree *, tree *t = NULL); static tree alloc_object_size (const gcall *, int); +static tree access_with_size_object_size (const gcall *, int); static tree pass_through_call (const gcall *); static void collect_object_sizes_for (struct object_size_info *, tree); static void expr_object_size (struct object_size_info *, tree, tree); @@ -749,6 +751,60 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, return false; } +/* Compute __builtin_object_size for a CALL to .ACCESS_WITH_SIZE, + OBJECT_SIZE_TYPE is the second argument from __builtin_object_size. + The 2nd, 3rd, and the 4th parameters of the call determine the size of + the CALL: + + 2nd argument REF_TO_SIZE: The reference to the size of the object, + 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents + 0: the number of bytes; + 1: the number of the elements of the object type; + 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE + of the object referenced by REF_TO_SIZE + + the size of the element can be retrived from the result type of the call, + which is the pointer to the array type. */ +static tree +access_with_size_object_size (const gcall *call, int object_size_type) +{ + /* if not for dynamic object size, return. */ + if ((object_size_type & OST_DYNAMIC) == 0) + return size_unknown (object_size_type); + + gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); + /* result type is a pointer type to the original flexible array type. */ + tree result_type = gimple_call_return_type (call); + gcc_assert (POINTER_TYPE_P (result_type)); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); + tree ref_to_size = gimple_call_arg (call, 1); + unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); + tree type = TREE_TYPE (gimple_call_arg (call, 3)); + + tree size = fold_build2 (MEM_REF, type, ref_to_size, + build_int_cst (ptr_type_node, 0)); + + /* If size is negative value, treat it as zero. */ + if (!TYPE_UNSIGNED (type)) + { + tree cond_expr = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, integer_type_node, cond_expr, + build_zero_cst (type), size); + } + + if (class_of_size == 1) + size = size_binop (MULT_EXPR, + fold_convert (sizetype, size), + fold_convert (sizetype, element_size)); + else + size = fold_convert (sizetype, size); + + if (!todo) + todo = TODO_update_ssa_only_virtuals; + + return size; +} /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL. Handles calls to functions declared with attribute alloc_size. @@ -1350,8 +1406,12 @@ call_object_size (struct object_size_info *osi, tree ptr, gcall *call) bool is_strdup = gimple_call_builtin_p (call, BUILT_IN_STRDUP); bool is_strndup = gimple_call_builtin_p (call, BUILT_IN_STRNDUP); + bool is_access_with_size + = gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE); if (is_strdup || is_strndup) bytes = strdup_object_size (call, object_size_type, is_strndup); + else if (is_access_with_size) + bytes = access_with_size_object_size (call, object_size_type); else bytes = alloc_object_size (call, object_size_type);