From patchwork Fri Aug 25 15:24:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 136924 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp1874620vqm; Fri, 25 Aug 2023 08:26:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGzVLcZPSfDJrU05kna67Y/Z6K+328Z+H27geGcMsDev9V3VyXwILLJu4iwARhtl30hZ5Ms X-Received: by 2002:a2e:9e17:0:b0:2b5:9778:7ce2 with SMTP id e23-20020a2e9e17000000b002b597787ce2mr13327941ljk.15.1692977211890; Fri, 25 Aug 2023 08:26:51 -0700 (PDT) Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m6-20020a17090679c600b009930c925bd9si962163ejo.945.2023.08.25.08.26.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 08:26:51 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=hiKh1I9q; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 450AA3851C00 for ; Fri, 25 Aug 2023 15:25:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 450AA3851C00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692977143; bh=POcN9KzdUPo9n2oyKAfS9kEFITrqjYKcSrpS3rWpc2o=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=hiKh1I9qT/ci7qYebe0bu4hQ6zzwVKSarl510LPuIq5ECJNDhhKlGuvmCbkPJQz9w i+5fhEWERKWZnesRRJ7yPIuuQ6YgTVYJ9q89zasNZZfb4DS5g/uL+BFKoZHrKGyNnp jMMxaF+JNVDcbXqRyyKDn0nn/sbBCloPl6SCfYiE= 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 600B03858C31 for ; Fri, 25 Aug 2023 15:24:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 600B03858C31 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 37PDOC6L021100; Fri, 25 Aug 2023 15:24:44 GMT Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3sn1yvxng8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 25 Aug 2023 15:24:43 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 37PDmvH4036051; Fri, 25 Aug 2023 15:24:43 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1nam02lp2041.outbound.protection.outlook.com [104.47.57.41]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3sn1yxfvcp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 25 Aug 2023 15:24:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GBc49Kq4/p/rlnBMfCmAMxetbV+P81cTEWlzCFsoiKEY/cTsmDvA0csyzSN5a4zYsHBNK/H8eVuTuMYcRukiJyXgIr51Qb4ALeaWeMOyjl9yeA9exM6P0RgG0bbAyRbLIWYfjLMwcInVCQwBbPvUiidr3+aKwzgB6YAwK1ZE/gKVhmVRIuPCerOdT9QXbFgjbOw9UfF1sZOUfvWG6XoM/SF4auLT887Va1Nx8Nwr6/cIQS5GfM4gFoMe4mJwgJtO6TyjI4NIZZEM0zKaVMFvm+X9LfVKtmKc5RdNeL4fvZQuYVNRz97n75+pWzZmGtl6Nv9R66LkAi7umE8a4gOBWA== 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=POcN9KzdUPo9n2oyKAfS9kEFITrqjYKcSrpS3rWpc2o=; b=ixVDKdA0uDOGLSmZsByvfep02GTicKO3TO8DgtX9mtjWn/GCfexKNOl6F7Da5iL94Aq/+BKT55bX3UwEF3UrU5asjOTXDhHmy6aYVI7iGO13M0BDcW5NkFM3HmKQW2Wg7yvt8pVq53kd2PwlWjL0XeQnVPBHKOFgGVX23zrPq3p4Lz/SqDgUupI022GDTsxIoqb9Aq8QgXjwIV7Y563d/iOZrwROPEteOaCEZYVlKm/rPwyQGzqClg1DazDy1LMozxJL63X1yYOMWIlpY9sc7A/2babfXm4liiar2twP89EVDicHj121/ZhUV2mfsnLpO8g68dKboibSfoROL0AoBw== 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 Received: from CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) by BY5PR10MB4241.namprd10.prod.outlook.com (2603:10b6:a03:208::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6699.27; Fri, 25 Aug 2023 15:24:40 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::5afd:d02a:38ef:94ba]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::5afd:d02a:38ef:94ba%7]) with mapi id 15.20.6699.028; Fri, 25 Aug 2023 15:24:40 +0000 To: joseph@codesourcery.com, richard.guenther@gmail.com, jakub@redhat.com, gcc-patches@gcc.gnu.org Cc: keescook@chromium.org, siddhesh@gotplt.org, uecker@tugraz.at, isanbard@gmail.com, Qing Zhao Subject: [V3][PATCH 2/3] Use the counted_by atribute info in builtin object size [PR108896] Date: Fri, 25 Aug 2023 15:24:24 +0000 Message-Id: <20230825152425.2417656-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230825152425.2417656-1-qing.zhao@oracle.com> References: <20230825152425.2417656-1-qing.zhao@oracle.com> X-ClientProxiedBy: BYAPR05CA0075.namprd05.prod.outlook.com (2603:10b6:a03:e0::16) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR10MB4344:EE_|BY5PR10MB4241:EE_ X-MS-Office365-Filtering-Correlation-Id: e5c5efca-c000-41cf-5c5e-08dba57f6651 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mVpEVDTs89Ddly/kkkhhGAjKK6kSk7eV+E4o7NB9HQrggug9Cwpi+92LiwNlRUYDsO8GarOVzpPdMr3E6DXgmFGtXOiIExCBBxi7Y+Bh8V62KPxUe3ZSh2PJ+88tC6sBKVbTF/cEm2Ba4DIZYgVUFPQyiI5z7Bh17rmiUVGJtrsuNUOWWNdVMUJozEf1O9pVvehb8N/NKf3gY2fWEo2C22Y7weDO1XTliZeDjiFMGRVSI4lHWsOiRwMz3t+MXyK8e+2RC0APLl00u5gn8vAESoRDjP6vV7kFFk/mvrxMLXAVyoeGO1+C6AHLu9BJmFYZRUJtKpmHGycq/EaxYs6pkBkLnn7+gANVAA/HHBHrHrf+f+J3TQWKyw6xs11MAhb3qzgxStVQtjt9wioTPwT+ceLULEVZ/yG+GX1U5WJ6nhnomAaI9Vz+DL840NHv6gKWlm9SJUf2L6M8Shf7hCwqdbEO/RsbhDT2qEH5c61eDu4Wg9etux+4rXTPqOvPFmnImbyReCRBrl7H2ZajU6BkhSCK5jx+npuIw6+HNSt9a4DKbGv/9zcDs5DJ/s0FQTnVvKf+R4AeozL+9gv5GCttRQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH2PR10MB4344.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(346002)(136003)(396003)(39860400002)(366004)(376002)(451199024)(186009)(1800799009)(66946007)(66476007)(66556008)(316002)(478600001)(26005)(44832011)(6666004)(38100700002)(84970400001)(41300700001)(86362001)(6486002)(6512007)(6506007)(2906002)(966005)(8676002)(4326008)(8936002)(1076003)(2616005)(5660300002)(107886003)(83380400001)(36756003)(30864003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: a3PKKD+9TXX3W32T/bi47vQKMSR52tYmaAxZJYf6fRQ2Sc7JYrPF4TCo6Xne473SPQ0QhwBdNJR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vKZg0pe6pNMUMx0w3jF4ssUahoLfKrXbvuEGgPEmA5xQQPGP+HWDK9MRcsuJfYoF0ZxczH2Mb7lao2YNrAQG1pecLnDS2wNcvWn53lkNmVf5f9HMAJfNKxuKe/1Fh+d0j+ZAn8Ofix4MESOwJKpVS4zNJF9HakgpLRQK+CE+dXbOavtyz2cbRkxpBC/qlwVRPIhcgyab7tX+mmBqvUr4xmeCo1Kd4kGs6ZhR30v+qnPZzOIpVN0e5GZbAjb16FXfY0F0GBFQZdg2PiKSIJp/8ygmyKMhciWZvq1pxU+JSu2PTRkh38E/aS/W6EaLDwwDfAVhGqPH2zFVAp/Q8jHPMEvaWejDfes9V8mCyf4oiIGLLDUt6xAzljSRw9vzOuEW+7azfpBF0 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e5c5efca-c000-41cf-5c5e-08dba57f6651 X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Aug 2023 15:24:40.1201 (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: sK6B8Fn99E4VHzoSFFJ+KFAE0AGPLS1BtvG+exrkB+YCJSLGLHjHFglrw02lO93JDqdiGvILSjHB9HsaA0NseQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB4241 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-08-25_13,2023-08-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 malwarescore=0 spamscore=0 phishscore=0 mlxlogscore=999 adultscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2308100000 definitions=main-2308250137 X-Proofpoint-GUID: Q9TFI-A0Fmu-pju_1M_VCKByWIgJ93R6 X-Proofpoint-ORIG-GUID: Q9TFI-A0Fmu-pju_1M_VCKByWIgJ93R6 X-Spam-Status: No, score=-11.3 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Qing Zhao via Gcc-patches From: Qing Zhao Reply-To: Qing Zhao Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775215272454401287 X-GMAIL-MSGID: 1775215272454401287 Use the counted_by atribute info in builtin object size to compute the subobject size for flexible array members. gcc/ChangeLog: PR C/108896 * tree-object-size.cc (addr_object_size): Use the counted_by attribute info. * tree.cc (component_ref_has_counted_by_p): New function. (component_ref_get_counted_by): New function. * tree.h (component_ref_has_counted_by_p): New prototype. (component_ref_get_counted_by): New prototype. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/flex-array-counted-by-2.c: New test. * gcc.dg/flex-array-counted-by-3.c: New test. --- .../gcc.dg/flex-array-counted-by-2.c | 74 ++++++ .../gcc.dg/flex-array-counted-by-3.c | 210 ++++++++++++++++++ gcc/tree-object-size.cc | 37 ++- gcc/tree.cc | 95 +++++++- gcc/tree.h | 10 + 5 files changed, 418 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-2.c create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-3.c 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..ec580c1f1f01 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-2.c @@ -0,0 +1,74 @@ +/* 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" + +#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); + +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-3.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c new file mode 100644 index 000000000000..a0c3cb88ec71 --- /dev/null +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-3.c @@ -0,0 +1,210 @@ +/* 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? */ +/* { 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 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); + +#define noinline __attribute__((__noinline__)) +#define SIZE_BUMP 10 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(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; + + For B, we use the SIZE info of the TYPE attached to the corresponding access. + (We treat counted_by attribute as a complement to the SIZE info of the TYPE + for FMA) + + 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. from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + B. from observed access: p->foo * sizeof (char) + */ + + /* for size in the whole object: always uses A. */ + /* for size in the sub-object: chose the smaller of A and B. + * Please see https://gcc.gnu.org/pipermail/gcc-patches/2023-July/625891.html + * for details on why. */ + + /* for MAXIMUM size in the whole object: use the allocation size + for the whole object. */ + expect(__builtin_dynamic_object_size(p->array, 0), + allocated_size - __builtin_offsetof (struct annotated, array[0])); + + /* for MAXIMUM size in the sub-object. use the smaller of A and B. */ + expect(__builtin_dynamic_object_size(p->array, 1), + MIN (allocated_size - __builtin_offsetof (struct annotated, array[0]), + (p->foo) * sizeof(char))); + + /* for MINIMUM size in the whole object: use the allocation size + for the whole object. */ + expect(__builtin_dynamic_object_size(p->array, 2), + allocated_size - __builtin_offsetof (struct annotated, array[0])); + + /* for MINIMUM size in the sub-object: use the smaller of A and B. */ + expect(__builtin_dynamic_object_size(p->array, 3), + MIN (allocated_size - __builtin_offsetof (struct annotated, array[0]), + (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. from observed allocation: + allocated_size - offsetof (struct annotated, array[0]) + B. from observed access: p->foo * sizeof (char) + */ + + /* for size in the whole object: always uses A. */ + /* for size in the sub-object: chose the smaller of A and B. + * Please see https://gcc.gnu.org/pipermail/gcc-patches/2023-July/625891.html + * for details on why. */ + + /* for MAXIMUM size in the whole object: use the allocation size + for the whole object. */ + expect(__builtin_dynamic_object_size(p->array, 0), + allocated_size - __builtin_offsetof (struct annotated, array[0])); + + /* for MAXIMUM size in the sub-object. use the smaller of A and B. */ + expect(__builtin_dynamic_object_size(p->array, 1), + MIN (allocated_size - __builtin_offsetof (struct annotated, array[0]), + (p->foo) * sizeof(char))); + + /* for MINIMUM size in the whole object: use the allocation size + for the whole object. */ + expect(__builtin_dynamic_object_size(p->array, 2), + allocated_size - __builtin_offsetof (struct annotated, array[0])); + + /* for MINIMUM size in the sub-object: use the smaller of A and B. */ + expect(__builtin_dynamic_object_size(p->array, 3), + MIN (allocated_size - __builtin_offsetof (struct annotated, array[0]), + (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 observed access p->array, we only have info on the + observed access, i.e, the TYPE_SIZE info from the access. We don't have + info on the whole object. */ + expect(__builtin_dynamic_object_size(p->array, 0), -1); + expect(__builtin_dynamic_object_size(p->array, 1), p->foo * sizeof(char)); + expect(__builtin_dynamic_object_size(p->array, 2), 0); + 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 observed access p->array, we only have info on the + observed access, i.e, the TYPE_SIZE info from the access. We don't have + info on the whole object. */ + expect(__builtin_dynamic_object_size(q->array, 0), -1); + expect(__builtin_dynamic_object_size(q->array, 1), q->foo * sizeof(char)); + expect(__builtin_dynamic_object_size(q->array, 2), 0); + expect(__builtin_dynamic_object_size(q->array, 3), q->foo * sizeof(char)); + /*when checking the pointer p, we have no observed allocation nor observed + access, therefore, we cannot determine the size info here. */ + expect(__builtin_dynamic_object_size(q, 0), -1); + expect(__builtin_dynamic_object_size(q, 1), -1); + expect(__builtin_dynamic_object_size(q, 2), 0); + expect(__builtin_dynamic_object_size(q, 3), 0); + + DONE (); +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index a62af0500563..cf7843c5684b 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -585,6 +585,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, if (pt_var != TREE_OPERAND (ptr, 0)) { tree var; + tree counted_by_ref = NULL_TREE; if (object_size_type & OST_SUBOBJECT) { @@ -600,11 +601,12 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, var = TREE_OPERAND (var, 0); if (var != pt_var && TREE_CODE (var) == ARRAY_REF) var = TREE_OPERAND (var, 0); - if (! TYPE_SIZE_UNIT (TREE_TYPE (var)) + if (! component_ref_has_counted_by_p (var) + && ((! TYPE_SIZE_UNIT (TREE_TYPE (var)) || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var))) || (pt_var_size && TREE_CODE (pt_var_size) == INTEGER_CST && tree_int_cst_lt (pt_var_size, - TYPE_SIZE_UNIT (TREE_TYPE (var))))) + TYPE_SIZE_UNIT (TREE_TYPE (var))))))) var = pt_var; else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) { @@ -612,6 +614,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, /* For &X->fld, compute object size if fld isn't a flexible array member. */ bool is_flexible_array_mem_ref = false; + while (v && v != pt_var) switch (TREE_CODE (v)) { @@ -660,6 +663,8 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, /* Now the ref is to an array type. */ gcc_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE); is_flexible_array_mem_ref = array_ref_flexible_size_p (v); + counted_by_ref = component_ref_get_counted_by (v); + while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) != UNION_TYPE @@ -673,8 +678,11 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, == RECORD_TYPE) { /* compute object size only if v is not a - flexible array member. */ - if (!is_flexible_array_mem_ref) + flexible array member or the flexible array member + has a known element count indicated by the user + through attribute counted_by. */ + if (!is_flexible_array_mem_ref + || counted_by_ref) { v = NULL_TREE; break; @@ -707,9 +715,24 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, if (var != pt_var) { - var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); - if (!TREE_CONSTANT (var_size)) - var_size = get_or_create_ssa_default_def (cfun, var_size); + if (!counted_by_ref) + { + var_size = TYPE_SIZE_UNIT (TREE_TYPE (var)); + if (!TREE_CONSTANT (var_size)) + var_size = get_or_create_ssa_default_def (cfun, var_size); + } + else + { + gcc_assert (TREE_CODE (var) == COMPONENT_REF + && TREE_CODE (TREE_TYPE (var)) == ARRAY_TYPE); + tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (var))); + var_size + = size_binop (MULT_EXPR, + fold_convert (sizetype, counted_by_ref), + fold_convert (sizetype, element_size)); + if (!todo) + todo = TODO_update_ssa_only_virtuals; + } if (!var_size) return false; } diff --git a/gcc/tree.cc b/gcc/tree.cc index fcd36ae0cd74..3b6ddcbdcbf8 100644 --- a/gcc/tree.cc +++ b/gcc/tree.cc @@ -12745,6 +12745,32 @@ array_ref_element_size (tree exp) return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp); } +/* For a component_ref that has an array type ARRAY_REF, return TRUE when + an counted_by attribute attached to the corresponding FIELD_DECL. + return FALSE otherwise. */ +bool +component_ref_has_counted_by_p (tree array_ref) +{ + if (TREE_CODE (array_ref) != COMPONENT_REF) + return false; + + if (TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE) + return false; + + tree struct_object = TREE_OPERAND (array_ref, 0); + tree struct_type = TREE_TYPE (struct_object); + + if (!RECORD_OR_UNION_TYPE_P (struct_type)) + return false; + tree field_decl = TREE_OPERAND (array_ref, 1); + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (field_decl)); + + if (!attr_counted_by) + return false; + return true; +} + /* Given a field list, FIELDLIST, of a structure/union, return a TREE_LIST, with each TREE_VALUE a FIELD_DECL stepping down the chain to the FIELD whose name is FIELDNAME, which is the last TREE_VALUE of the list. @@ -12771,7 +12797,7 @@ get_named_field (tree fieldlist, const char *fieldname) fields inside it recursively. */ else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))) if ((named_field = get_named_field (TYPE_FIELDS (TREE_TYPE (field)), - fieldname)) != NULL_TREE) + fieldname)) != NULL_TREE) { named_field = tree_cons (NULL_TREE, field, named_field); break; @@ -12784,6 +12810,73 @@ get_named_field (tree fieldlist, const char *fieldname) return named_field; } +/* For a component_ref that has an array type ARRAY_REF, get the object that + represents its counted_by per the attribute counted_by attached to + the corresponding FIELD_DECL. return NULL_TREE when cannot find such + object. + For example, if: + + struct P { + int k; + int x[] __attribute__ ((counted_by (k))); + } *p; + + for the following reference: + + p->x[b] + + the object that represents its element count will be: + + p->k + + So, when component_ref_get_counted_by (p->x[b]) is called, p->k should be + returned. +*/ + +tree +component_ref_get_counted_by (tree array_ref) +{ + if (! component_ref_has_counted_by_p (array_ref)) + return NULL_TREE; + + tree struct_object = TREE_OPERAND (array_ref, 0); + tree struct_type = TREE_TYPE (struct_object); + tree field_decl = TREE_OPERAND (array_ref, 1); + tree attr_counted_by = lookup_attribute ("counted_by", + DECL_ATTRIBUTES (field_decl)); + gcc_assert (attr_counted_by); + + /* If there is an counted_by attribute attached to the field, + get the field that maps to the counted_by. */ + + const char *fieldname + = IDENTIFIER_POINTER (TREE_VALUE (TREE_VALUE (attr_counted_by))); + + tree counted_by_field = get_named_field (TYPE_FIELDS (struct_type), + fieldname); + + gcc_assert (counted_by_field); + + /* generate the tree node that represent the counted_by of this array + ref. This is a (possible nested) COMPONENT_REF to the counted_by_field + of the containing structure. */ + + tree counted_by_ref = NULL_TREE; + tree object = struct_object; + do + { + tree field = TREE_VALUE (counted_by_field); + + counted_by_ref = build3 (COMPONENT_REF, + TREE_TYPE (field), + unshare_expr (object), field, + NULL_TREE); + object = counted_by_ref; + counted_by_field = TREE_CHAIN (counted_by_field); + } + while (counted_by_field); + return counted_by_ref; +} /* Return a tree representing the lower bound of the array mentioned in EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ diff --git a/gcc/tree.h b/gcc/tree.h index 4859becaa1e7..07eed7219835 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -5619,11 +5619,21 @@ extern tree get_base_address (tree t); of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ extern tree array_ref_element_size (tree); +/* Give a component_ref that has an array type, return true when an + attribute counted_by attached to the corresponding FIELD_DECL. */ +extern bool component_ref_has_counted_by_p (tree); + /* Given a field list, FIELDLIST, of a structure/union, return the FIELD whose name is FIELDNAME, return NULL_TREE if such field is not found. searching nested anonymous structure/union recursively. */ extern tree get_named_field (tree, const char *); +/* Give a component_ref that has an array type, return the object that + represents its counted_by per the attribute counted_by attached to + the corresponding FIELD_DECL. return NULL_TREE when cannot find such + object. */ +extern tree component_ref_get_counted_by (tree); + /* Return a typenode for the "standard" C type with a given name. */ extern tree get_typenode_from_name (const char *);