From patchwork Wed Sep 7 00:28:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 57446 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 1AF613850223 for ; Wed, 7 Sep 2022 00:29:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1AF613850223 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662510575; bh=T5a6I+KyxZ35QBsSB+3XyNmSGjosNjwMk4BiKVDTmb4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=OooUVgdj862jLHIlhe2XMpdP+sWQJI5Fv2d7UYisLRpB45i8jeVftJ3lw/WocDTO+ MDQNrf9i+IqLEBgrnV1H09R/YkCV0V5jSuRvv8NbtG/xFGGgfaqFH9QatZUIYJdZBZ sE1LE1NPJpUnIMwI9ugs+rw2sx+pHM8uWFpZjHD4= 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 78DC0385086A for ; Wed, 7 Sep 2022 00:29:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 78DC0385086A Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 286LdH3C015737; Wed, 7 Sep 2022 00:28:58 GMT Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3jbyftqc62-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 07 Sep 2022 00:28:58 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 286NKuab039726; Wed, 7 Sep 2022 00:28:57 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2109.outbound.protection.outlook.com [104.47.58.109]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3jbwc9vt8m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 07 Sep 2022 00:28:56 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FsZfqv8hcm8jPZ/pWM7zREjr4VyKQOUgvr37b04zuWcIIjFvXWZDy1ppAruHzxaGVQgGYT3o6eIbMZOeUC6s26eXVzKs2K3neMijwavyVwp159gdCrie6uGCPsDrp+j4+1duwjPdIZD8iiD1cDrQRJ5ywNQfIrkaC8YO6ccKL8wfzusJhObpnK58sbgb3+NIZhgxbDoBZSQ1juZrF4/Ip5Fps8xARQqyOPjf9+m8Os3qyQ+j+39sq0649FIXXGMoDA4IWamcHNZeEmxaBgm9rvEfUBP5koVM5Lz8yzkzrnmsrZ5mlf2VLAxjHLAR1bG7UxEd3I78eMycG3NcHHqcIQ== 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=T5a6I+KyxZ35QBsSB+3XyNmSGjosNjwMk4BiKVDTmb4=; b=RmvLkitYsGoMwxuBHrMuKNFVgBNprf0ta/M7gP3uTHF4Lnu3htOYBYSfU9Q1SNBqOKcsPjIdP/NHq6jn+Oskp17xC4JxA0jTXBlRQLEssdbxcaTCZ1s/65WnrYnQ+2B3B/hPql6vEMjDks4rlK9P0tCSpyeJ2DoDFVLUfk6uFltStnAupzUSw4ZqfbcVbrvBOtl/x6JXw3fz19XsTlq0sGEF1GJuoqc4rPpG7BhgamdvaDhn9MHPYGQOzcImpe2L+WSpQYCuPkeLOywA/zxqO3WLcuJRqaCAB+j+R4zSOjhNOp2Q+JyEBlkygA2nhNMDi1bGdrmqPzO7OTUFCYFooA== 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 CY8PR10MB6801.namprd10.prod.outlook.com (2603:10b6:930:98::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5588.10; Wed, 7 Sep 2022 00:28:54 +0000 Received: from CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1]) by CH2PR10MB4344.namprd10.prod.outlook.com ([fe80::84af:9275:f1d7:de1%6]) with mapi id 15.20.5588.018; Wed, 7 Sep 2022 00:28:54 +0000 To: gcc-patches@gcc.gnu.org Subject: [GCC13][Patch][V4][PATCH 2/2] Use array_at_struct_end_p in __builtin_object_size [PR101836] Date: Wed, 7 Sep 2022 00:28:17 +0000 Message-Id: <20220907002817.236178-3-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220907002817.236178-1-qing.zhao@oracle.com> References: <20220907002817.236178-1-qing.zhao@oracle.com> X-ClientProxiedBy: BYAPR21CA0030.namprd21.prod.outlook.com (2603:10b6:a03:114::40) To CH2PR10MB4344.namprd10.prod.outlook.com (2603:10b6:610:af::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f3f0bfc1-9e7d-4d91-a963-08da9067f20a X-MS-TrafficTypeDiagnostic: CY8PR10MB6801:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KtBfZko7Xn4LjHWus0e4nIzZuTEE4cw20C/N4wphL7mvoGLrIkF3WqmDyuLAe5vRhJSJtKlqcz2pkQmHRkRV+9djsb/7UCA9z9O6eWVnnYpsYnW0JhhAwVFsKPxrMxSr9d8OwfRO5vnQn/728l0CLTeWtBEF+b1bh408wTPeSxYHB4qAo69UFfL/zuMXrZRS4hoFAiF78bjXOm/Of2vXyCwkTUSVJl0kP3snRWLZ3CSYSNLQjGEasvnyUQ/WPjnb+KJbobEva/zWuMUA11B0DZucTJOQcUHQF8D1TBQ4zhlxAF6WXC7I59b9HH1Pww7yTswpMQ7itb1u5bSvtKciFv6k3g+kNMnX4bskMYobEH/mqqdbk63Fn7Q0k2fVcquJdcZxiqIV/JV54Qf7UZySCGrztXmHjQ4TcSP9GJ7HaLjRPTTdUaFcqhTPOsXxy9fvfb93nsWia/Rq82oyJJMPuM2v7rrK9KZr157Lsm+BbnOp3Q9QJ0UA/rITepNk15e0p7FnoLG+zv8SPxbl8cQTbt07DOmLAaiBw5yeBcibLTJcBS7dz8rSII/eSqmTcEN1QSAXVEQZBqvJ6WRsJe4AuPhePZvnHMqJz/n/kC/8pB16kuk4EPnZqQ60f1EPYniO0yOd161hWDYYeP62czwko+H82wWb2AKM7oW1lAt67b29dG/fR3Iif12NZRBzxLfCiJH8MyMtAWU1f4fc6pb4DaMj6Lc8Mvk2ZRg3P9SrIcJ9TsvWOsxJgaJC7rHZxNDf/NYyToh+cD4ktxx/pqjCLg== 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:(13230016)(396003)(39860400002)(376002)(366004)(136003)(346002)(6666004)(478600001)(6506007)(26005)(1076003)(6486002)(41300700001)(6512007)(186003)(83380400001)(2616005)(5660300002)(2906002)(30864003)(44832011)(8936002)(6916009)(107886003)(4326008)(66476007)(8676002)(66946007)(66556008)(316002)(86362001)(38100700002)(84970400001)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mvcdanKlf9+IISS65XrT4iTvO2WbKdRehcv8I4mgn6hNnWoX2BXoU+ayrt+jdpV4FEGtxOpeGgvIFPB52+wKcddwH/OG0ICiUNiU/VAS6gfHgY54LVrzY9Yw9quuYzVaqwmvU2f0x6rDc0MOD124EH6PeNNHnd7AQ/YJecotg3EFyc8UjUlw26PkY6mTNkV618eT+svdog65GBRE+BJi6b1Ao9fuhW6EAb0uAQ2TTf1Yya8s4Cynay8B8XJNwiyupqYxVgN8p4Ybq/QMCPm4bn+oSq+dNL1Zx+xa95LxTog6vMxOf02nmO59p7zKOs4Mzvq3iOmXotvduqwjRksOi0PTDgWN9MmVEYE085uNOB3qTkZ8GHyOV5NwY/GOsnWvSYhgCgbn3joYXRQZnuyhS9vYlVaNQExH9t2A0wONvNZ1wQQtvjwZlQ/0+cncnOewL9I2HyrVo1cvjeX+3ve0WvWRVhDyHKKMAnRTLGCoTLiZUJ5n/Rdhl7gKH4zMnyrmS7MgRxs8FOqBhgR3Bq+1cA9YgTpkPniHwDjdnsuFy5ZGvtgzgu6V0SXBZ509zK94fiLvxab8Gmw18qh4/cQXp5YETwgd6U4boZ6ILd+hqw41odI/4rF7KGcekLFbhdaYfcVS/c7dRRqZMoOSCowTViLpc4XD6Qp3jv7NSzpOISFdWTyV2M/kkuwk4cOZCmZQS6OtirBCGy0eW5WrHjY0m0eqhD4KFo8T/CL9rQy0a1Dy0HA22KSMFAPvtQ9HOp7lrQxYFcg3yOul6KPsI2Nh/G2I5YAhSKZzJrBfDqhNikZcw5lFJ0IcmmzVsIwhoe01jAFe/+5cdzRLWbANn/T1f0Dxlg0gH9tlkPQtMQPJIMsy7LbvkMqZWblcuMwx6FjibkwSyDYvvYjWKN5gO7TnMmR28bd9MqyaEUM7J20S6OTmJMt3tKicO2f91qXCMtQ/Z2hVP5HT2uE8M5BZAzm2bbsuTmGO1+Brsat0w6cfER0qMXj+uEW34eAXD4d09CqVeSEHVYG16bIRosBWpNJz/fpu9cw6HBMCKrbqo64F726IhRoUGzUsYi0BxXP4feCuzcVRs78KW+bm5dM274YurilPfc0nH2EZZ6MN3rRBjHArheJxzDlWOrUkNztLXLWPlweCs6Q3d2uk0S/l1Q3R2MP/OYQc5q8djV+MElP1CU9vUSqBg5QlYA82t5FQhmHNVaMZsIaREaDVDjFw+GiQbRFE3iRYH3lT1oec6/No0ZiDSEboF5f9jZFiXxQuETP5LEV+leMWZv4PU61rX89qWsgM5nMvvU3s2FBf5UbaO8oPXIDuJbeeawEWCfO7DR+cNwL24UBJlKgqMOxAx9fcMT7itjPysDBuky/r8FfukP13xcpAgCY5H32RaNn240xToXzG3mPqg+AflrkwXnzEhNHVFHBDKqPt9xjADJWeagg7j57DM18E7ZmiN/6kl/RmEifPcJ/m6aJUHivHCs81Z8fjkvqxh5kS1Q5zVSSJvKDAHH9ksv+cbj6Uo+6iE0LfsFj1sqZs6BUE7LvTSdpFPickQ65EjN8Tf5S4VDnqASF9JE3L9YDc26EF6DlzBbm2 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f3f0bfc1-9e7d-4d91-a963-08da9067f20a X-MS-Exchange-CrossTenant-AuthSource: CH2PR10MB4344.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Sep 2022 00:28:54.5349 (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: 7UqZwMMbE6lHxB5zF+Djr0uKiXB1FNlWQ8Gw26HaDKpC549pnF4O9Up7Ut8ClGL4kRIhqsp7YHCU2molORnvSQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR10MB6801 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-06_11,2022-09-06_02,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=941 phishscore=0 mlxscore=0 spamscore=0 malwarescore=0 suspectscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2209070000 X-Proofpoint-ORIG-GUID: UB_g0mZaHIr5OqBQRl1-LzrSjMFeNU8G X-Proofpoint-GUID: UB_g0mZaHIr5OqBQRl1-LzrSjMFeNU8G 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, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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.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 Cc: jakub@redhat.com, rguenther@suse.de, msebor@gmail.com, joseph@codesourcery.com, keescook@chromium.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Use array_at_struct_end_p to determine whether the trailing array of a structure is flexible array member in __builtin_object_size. gcc/ChangeLog: PR tree-optimization/101836 * tree-object-size.cc (addr_object_size): Use array_at_struct_end_p to determine a flexible array member reference. gcc/testsuite/ChangeLog: PR tree-optimization/101836 * gcc.dg/pr101836.c: New test. * gcc.dg/pr101836_1.c: New test. * gcc.dg/pr101836_2.c: New test. * gcc.dg/pr101836_3.c: New test. * gcc.dg/pr101836_4.c: New test. * gcc.dg/pr101836_5.c: New test. * gcc.dg/strict-flex-array-2.c: New test. * gcc.dg/strict-flex-array-3.c: New test. --- gcc/testsuite/gcc.dg/pr101836.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_1.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_2.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_3.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_4.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr101836_5.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/strict-flex-array-2.c | 60 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/strict-flex-array-3.c | 60 ++++++++++++++++++++++ gcc/tree-object-size.cc | 16 +++--- 9 files changed, 487 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr101836.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_1.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_2.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_3.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_4.c create mode 100644 gcc/testsuite/gcc.dg/pr101836_5.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-2.c create mode 100644 gcc/testsuite/gcc.dg/strict-flex-array-3.c diff --git a/gcc/testsuite/gcc.dg/pr101836.c b/gcc/testsuite/gcc.dg/pr101836.c new file mode 100644 index 000000000000..efad02cfe899 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-arrays is aliased with -ftrict-flex-arrays=3, which is the + strictest, only [] is treated as flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), 0); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_1.c b/gcc/testsuite/gcc.dg/pr101836_1.c new file mode 100644 index 000000000000..e2931ce1012e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_1.c @@ -0,0 +1,60 @@ +/* -fstrict-flex-arrays=3 is the strictest, only [] is treated as + flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=3" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), 0); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_2.c b/gcc/testsuite/gcc.dg/pr101836_2.c new file mode 100644 index 000000000000..78974187721f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_2.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-arrays=2, only [] and [0] are treated as flexiable + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=2" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_3.c b/gcc/testsuite/gcc.dg/pr101836_3.c new file mode 100644 index 000000000000..0e69388e81fb --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_3.c @@ -0,0 +1,60 @@ +/* When -fstrict-flex-arrays=1, [], [0], and [1] are treated as flexible + arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=1" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_4.c b/gcc/testsuite/gcc.dg/pr101836_4.c new file mode 100644 index 000000000000..e0025aa9a7b5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_4.c @@ -0,0 +1,60 @@ +/* when -fstrict-flex-arrays=0, all trailing arrays are treated as + flexible arrays. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=0" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), -1); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr101836_5.c b/gcc/testsuite/gcc.dg/pr101836_5.c new file mode 100644 index 000000000000..0ad8bbf693ce --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101836_5.c @@ -0,0 +1,60 @@ +/* -fno-strict-flex-arrays is aliased to -fstrict-flex-arrays=0, + all trailing arrays are treated as flexible array. */ +/* PR tree-optimization/101836 */ +/* { dg-do run } */ +/* { dg-options "-O2 -fno-strict-flex-arrays" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4]; +}; + +struct trailing_array_2 { + int a; + int b; + int c[1]; +}; + +struct trailing_array_3 { + int a; + int b; + int c[0]; +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), -1); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-2.c b/gcc/testsuite/gcc.dg/strict-flex-array-2.c new file mode 100644 index 000000000000..e474b9ec43fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-2.c @@ -0,0 +1,60 @@ +/* test the combination of attribute strict_flex_arrays and option + -fstrict-flex-arrays: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=3" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4] __attribute__ ((strict_flex_arrays (0))); +}; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute__ ((strict_flex_arrays (1))); +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute__ ((strict_flex_arrays (2))); +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), -1); + expect(__builtin_object_size(trailing_1->c, 1), -1); + expect(__builtin_object_size(trailing_0->c, 1), -1); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/strict-flex-array-3.c b/gcc/testsuite/gcc.dg/strict-flex-array-3.c new file mode 100644 index 000000000000..b45e7b32f6d1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/strict-flex-array-3.c @@ -0,0 +1,60 @@ +/* test the combination of attribute strict_flex_arrays and option + -fstrict-flex-arrays: when both attribute and option specified, + attribute will have higher priority. */ +/* { dg-do run } */ +/* { dg-options "-O2 -fstrict-flex-arrays=0" } */ + +#include + +#define expect(p, _v) do { \ + size_t v = _v; \ + if (p == v) \ + printf("ok: %s == %zd\n", #p, p); \ + else \ + { \ + printf("WAT: %s == %zd (expected %zd)\n", #p, p, v); \ + __builtin_abort (); \ + } \ +} while (0); + +struct trailing_array_1 { + int a; + int b; + int c[4] __attribute__ ((strict_flex_arrays (1))); +}; + +struct trailing_array_2 { + int a; + int b; + int c[1] __attribute__ ((strict_flex_arrays (2))); +}; + +struct trailing_array_3 { + int a; + int b; + int c[0] __attribute__ ((strict_flex_arrays (3))); +}; +struct trailing_array_4 { + int a; + int b; + int c[]; +}; + +void __attribute__((__noinline__)) stuff( + struct trailing_array_1 *normal, + struct trailing_array_2 *trailing_1, + struct trailing_array_3 *trailing_0, + struct trailing_array_4 *trailing_flex) +{ + expect(__builtin_object_size(normal->c, 1), 16); + expect(__builtin_object_size(trailing_1->c, 1), 4); + expect(__builtin_object_size(trailing_0->c, 1), 0); + expect(__builtin_object_size(trailing_flex->c, 1), -1); +} + +int main(int argc, char *argv[]) +{ + stuff((void *)argv[0], (void *)argv[0], (void *)argv[0], (void *)argv[0]); + + return 0; +} diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index 4eb454a4a33b..1f04cb80fd0a 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -604,9 +604,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF) { tree v = var; - /* For &X->fld, compute object size only if fld isn't the last - field, as struct { int i; char c[1]; } is often used instead - of flexible array member. */ + /* 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)) { @@ -633,6 +633,7 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, v = NULL_TREE; break; } + is_flexible_array_mem_ref = array_at_struct_end_p (v); while (v != pt_var && TREE_CODE (v) == COMPONENT_REF) if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) != UNION_TYPE @@ -645,12 +646,9 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) == RECORD_TYPE) { - tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); - for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) - if (TREE_CODE (fld_chain) == FIELD_DECL) - break; - - if (fld_chain) + /* compute object size only if v is not a + flexible array member. */ + if (!is_flexible_array_mem_ref) { v = NULL_TREE; break;