From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 4DBB538582B7 for ; Fri, 25 Aug 2023 15:24:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DBB538582B7 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37PDOChY018192; Fri, 25 Aug 2023 15:24:47 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-03-30; bh=J2ZZj7DJBAapJ3W2Hsq+zazNq3PQ0nuKgjYcmTQezkw=; b=dRGToUypS20Z0O0Xez65z78I54Mxn8alZriI5cWK/WfflpzgdcMtJMfhArN9LCiMCbbX FLTXugOLpeceWyGR0+QpGwJP1TCCWY8W8d1CA/Yn9mAkKGZ+53EnewVUJW0qPVMcZMIS cFnFAWTZ1PN/3JhR3XbwtFuX6jWnC6Jdu3jLvZdEp0gtMj3mOt9HV3+NQDi2cAfPOOTJ akcqT5wVqxG9pCXSPXjvYHV7nqy3MMfLKKUWe04ey4tA+t/6UfmnZJw5L4IrDsZkyvQu lLaNJT/dmntUePWkC4pdGCPbH4K8A5u3/k99tM5+HN3iAdLfit/JhrdZ7wA5ZdIfbOVP KQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3sn1yxphha-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 25 Aug 2023 15:24:47 +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 37PFGirl001127; Fri, 25 Aug 2023 15:24:46 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1nam02lp2043.outbound.protection.outlook.com [104.47.57.43]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3sn1yuydrg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 25 Aug 2023 15:24:46 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Meqi/YPKglTOgzGC8D0Kvbc8RRYwvZRq+hhR+uEGZG+4ISV60QaU/bdFhh1DlPjeiq0SD3/2I9Ngf+RZEzBydcnS6DZBcolp+zfMnlanwc+U7o0mjk/qWFS0YXHD7jSCjrIcPWktN7rut8gGw5caP0AyQszSRxvUlWUC0BTKtBLkHA8wG+7Z+neyHZG2RPm6p8OhrxECvY7hxhcdyM6l7H1NFyZjGaoH+kMh9v3sHHzS4+2smmAAe4ViGcs3BLdEAiD56xXCOiM2Y4yayBt7X2xrJ5f3Qr7bVDbxRAIzz4mSmXYhTVtwejf2ZKs9XyQ6goKdxD0XpNEsyOIjvTe1uA== 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=J2ZZj7DJBAapJ3W2Hsq+zazNq3PQ0nuKgjYcmTQezkw=; b=jgzf13keOtpGUCDr3aepL/9zpioQHhSF3HR6RkCDANrI+0kERobrnWt97Ob0P4GYFz0TONBJ23Ops2/1Eir78IS5amOhldWX/DF2E4TAJzSLvXRCJKaYpbtGCJADGQJu3csWSCxPKeEXAse9BuuqjGdnw7ndiEQL07RexmB4eidfz+bRuWIHSSF3XWXOmsRw0lhgNXAV8z8x8W0D2riGqgTBCTEk7nAflgRTEEJ/sGRT5WMzUKf/sFgQjyYKqLuOL8q7TmbZYwnv6gxLecnb8Kr/waxPZQXL+ZjHyXX+iBC204rJuPmS7H34zUaHr7qnXFVVKnVTSJF8+cRCPXix1Q== 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=J2ZZj7DJBAapJ3W2Hsq+zazNq3PQ0nuKgjYcmTQezkw=; b=F52gTzn52rhUjauvVz955Ft7OVpv+6B3feldDVWqI86pTya62T9VH2/LM9Jy0xdtA47bRjKDLW5oX3dzMXpU4jwPeQdJHJI6ZxTK76EZxCUn8WjojYHV81uJZB8ohkIiLBSDCqc+bubU6RCUeeTrH18DZp98cLSllDqBpQUDadc= 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:44 +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:44 +0000 From: Qing Zhao 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 3/3] Use the counted_by attribute information in bound sanitizer[PR108896] Date: Fri, 25 Aug 2023 15:24:25 +0000 Message-Id: <20230825152425.2417656-4-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> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SJ0PR03CA0068.namprd03.prod.outlook.com (2603:10b6:a03:331::13) 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: e1c7ac23-2be3-477b-4774-08dba57f68e3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gawLQsUCoZymUDU/qB9Cgd6wNuK/JfNtMIJbrHK7sUEkaqCGVEqQ1JJ5kW/3QktLufeopbEngkIS1ItNco0aVAlY8OBJ/mlhh7ASDeJJvlfcan9KVRRhdYFx/lgIu4PLLfxun1wzwVSlfdahrVQCMQ7WZP52axgJ8CkWNSLg36oQ9nOiNovTqbJSKZduEyeZvTkAYqjdE2KMWKcvdqQ8fxDti4QJDAJZESmwYRk99HL6gXdeB/7mwgV4GyvYe2dHBkBRfysI/MU8nNexYcWyghTrei3/G4sNv9v5PlPfQIZzhchc6eblpVe9HH3YbDg7HPo5wdrmEGexDeW9mwteaqK0K8A7rxYdFDKLBHpu0hon5YLAcvq9s6ykW412IAB45tjGL62nseY6PvgxnoQqDHfAG5q0Md5d0ComrfntCYMGFKX7HS1STfbZhaIJZeFgSCvL+gVQwFaieLuvSi2qdNK0ukdjzdK9SBRGwIMSKrc7x7IThbrDNElVo7PhreOrSif2MCONWdSvhHFKXaFzFjoavQlhtqPgxj7OGLp7J3XCl1EKOIIUuLlCYHZrhgj2QYLjj+AISiZT90ZFuD9yYA== 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)(8676002)(4326008)(8936002)(1076003)(2616005)(5660300002)(107886003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?V5QZ3mFwNuSmVIecqnU+SnPokFLFB6HhhAIZPrFU0oZmY3alI+nJdHl10vcX?= =?us-ascii?Q?b1PDpp72li3qpEV5Zld6iaZgox973LmqEDN5ZqaNs2PCC3zGc3MVbJCKPkHX?= =?us-ascii?Q?nTbw+S/OAL+gbMDpuM9ssaR/Wep7/85+cCMK1d7QbgynTUMDldjaNn13sAS2?= =?us-ascii?Q?6rIri4YTaFUGv4szogAY7VTkxyJ8kcTKcdojONUBpNW3HhKuXKLqwaNSKOdC?= =?us-ascii?Q?Wd8c0fmsSZrhs07dcqI5tz08sPkgxHcN/ktQls6v6tzAxNqiQrLwShDFq4Rh?= =?us-ascii?Q?TtejEnpxHIxSzElWTvyqCPIubQjoFCajUCrTQHZMcKJUIjU+rdPwG0R1F9h5?= =?us-ascii?Q?PQwFwa63gbRnmI5vN9/js2ZBmFW3CQr+OCthPoHZtWIRFvhkwFC/KfafRH97?= =?us-ascii?Q?ZtbUxcOljU/7IWDBGq1WlQTlzmmJaBLFVRPeOOgGYio6eoGID622Zsf58lQu?= =?us-ascii?Q?yWD134N4Om6yewpMV7FSWJgmm0qOntLXLG7ZLOkrhbdQlUyQfdVcKMXTT7IM?= =?us-ascii?Q?gdG35VGpmEmaIkzTJNW6+G7dhzL9ZsaNUWKcdu09XLirxnDn2V47yGMvWYC9?= =?us-ascii?Q?2DDd9H3fOL5j8ZuITadeDn61j//nTcVJNZoqLbDDjOnciSpL7q6SZeYb3f/E?= =?us-ascii?Q?Ym7ydzE86Dj9sT64Kn+CHOJoP/YpEIsMFDt7gg+b0zm4hSi5KZN9a3wFtZkG?= =?us-ascii?Q?zYQ2NyeiX7etZh4YkYwaYBlGn+eqBlEKfaWLZ0vo2uoF6ybzajRljn81GLBG?= =?us-ascii?Q?29H31LtK+MlNnoI4Q6iKFxjnFV5aU5hqsTWS5HE279J7UZ30nn0r+cX92YXL?= =?us-ascii?Q?+bOz0U7nGYLpzXLlYhVeUkCzHBx/8kNMEZp/UUky3JkNPYxkP2jVGatBRj8e?= =?us-ascii?Q?ACSaBTR4RKfGT4jPh9HGByNCYR8tz73QrwaiNtSXbrRVs4dlRpTg9a1TIBkh?= =?us-ascii?Q?N022visOlxyUNGYoWJx5lnL/kIRl/ccdaKi66JXVOX2dTfuDbKZolTdTZcIx?= =?us-ascii?Q?YEo8iE6v36pya+kwYcoPQWHusx2X7GI/1cvdIedxV84BJUYQfs1zwYOo55+E?= =?us-ascii?Q?9yGfKDHRXP061G4P1BlVzfRfVfMX9xzRRFhnMugsu+yyh5+qQbM7+0ml3mo0?= =?us-ascii?Q?eoOiGP3jr0XmWkwkUr6lIdSLlJoXIB705h+En/+1F5R9hDXOQwGe2L49dciO?= =?us-ascii?Q?NlnBwYJWm0HZSPSOI4F+MQtPk3FHpnG8ZfAsesMlU9nIO+/PwvizJkXyzj5o?= =?us-ascii?Q?wXCtx9ejXGe7bxaIUpa8rSBK5/RTyMbuGx86eMXHbob3U5OEeYChdyoIID+J?= =?us-ascii?Q?btQVE27flJBFcBUomkcYBOmtOLhtCkbEIlL1EPWj7Q+W4XHG18zZlTFnm8we?= =?us-ascii?Q?0aL43eHEDiyx6v0r4LtJisbbglwOwtyl8IqMgU1C7TyjlRxEgRwyMUtabxAN?= =?us-ascii?Q?gIIlK9mKIeY2JSoeC4RXDka2HiMKJfH8vWqP3j/wYhUGX4Ayguiqtd2F/Q5T?= =?us-ascii?Q?Eu21iAB+78py/ieR3ptWiq1atOJqpGh4vXOz2DTExbg5/RsqXtJDOFUu4i83?= =?us-ascii?Q?tdpe8qUAaaxtpadBqECQXHqRBu8U9kjOQxxI2T8C?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: =?us-ascii?Q?vE1b9dSNwcX1EcMlKUB5cI5uVNfPnaz59o2iw79BOybUf5PtPvGtiDhxVb5c?= =?us-ascii?Q?bqjr8547Rsh04mMexr/1cx1rqr+pr1DI9jCUV/6fEPMI3caE/22wSC1ofzUL?= =?us-ascii?Q?i0LYY8zj1fA6odMbRAJOQ7Y0+uCC/sdzn/QxtRrVlbdP1sBGGkLOSHDVZn94?= =?us-ascii?Q?ZDKqRGCbhGOaLFRFQ2gTRR2TSJeA/OFk0L4IZq8xvU8cyuMkXCweW4RC0ymK?= =?us-ascii?Q?W716xQn6Os6xKqq83slWwTU1QPvuqm2D32PPmZaa4ceRMAs6GxIB7iX8+TgP?= =?us-ascii?Q?cQEpJKJuaWjLE0FG/g1uV+uNBpf7isFHGRbNr1o288AHnkH4mFkKtylIFIXc?= =?us-ascii?Q?QzfeEkt37LKQ3EIZkZ+1XH4ySZ4Vyu9oAcX/G9HDQSPiQfRRgBPhtwChBs/i?= =?us-ascii?Q?8nddVp948TmndBF0hytERm+gphekWvqiWItiGHR7LURfaC5ClW204uZvaKER?= =?us-ascii?Q?I8as7A5axGzF4dVbUr4g7c0TE0tTl4f51wGy2WbqVcc+pLjQsdYBCw0i2yZj?= =?us-ascii?Q?CaKDava5HEJi+pLQqMBhwHfppsstDPbMYcheCnDU7gK55ZnIE85SamG0TT7r?= =?us-ascii?Q?WTXzZC5H/xq6qvIEqU9BCqDcIn8xE/Jdk26wgTbKS9aTkfyBAK1w2JbGWaw/?= =?us-ascii?Q?HKHg8R7ZB7aOQrgFsv1W/EHgcb3lizVdj9gyHT7rgXFOSfUrxDoQoc58tT2Z?= =?us-ascii?Q?jHG1H5+pkcfmuGmbP+h0uXdxNh1KT17h6NNy/nye3ftDI5DEMWHVnFwyQr31?= =?us-ascii?Q?DIivyZxZ/B+zm8anvolWrt4BCPSpYQceT29LJSPgPR05wO7EZLJmjItJOPkT?= =?us-ascii?Q?UJA39mhO3J67PhHzghRh+lVrlsWMDE49Ol1e8x456tME7FD0fqiyPqUofsF5?= =?us-ascii?Q?a4lA/1+6GC1a6LjnmRnWkGaYC+XB2Bq3dCKcHrffDeEsewktm6NqD7Vykydp?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e1c7ac23-2be3-477b-4774-08dba57f68e3 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:44.4518 (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: y50KNEhi9DZZzNGKp+n+qdqd90vnM6wKS0uTKQFjARo1sdR/4epq+O3NpyItzKQn0ZgmK0j/XNJg+d7YZlIoGw== 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 mlxscore=0 spamscore=0 bulkscore=0 malwarescore=0 phishscore=0 suspectscore=0 adultscore=0 mlxlogscore=810 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2308100000 definitions=main-2308250137 X-Proofpoint-GUID: VKkodp4UVymexTdzLqOOhb6qQsCVmAR_ X-Proofpoint-ORIG-GUID: VKkodp4UVymexTdzLqOOhb6qQsCVmAR_ 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_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 List-Id: Use the counted_by attribute information in bound sanitizer. gcc/c-family/ChangeLog: PR C/108896 * c-ubsan.cc (ubsan_instrument_bounds): Use counted_by attribute information. gcc/testsuite/ChangeLog: PR C/108896 * gcc.dg/ubsan/flex-array-counted-by-bounds.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds-2.c: New test. --- gcc/c-family/c-ubsan.cc | 16 +++++++ .../ubsan/flex-array-counted-by-bounds-2.c | 27 +++++++++++ .../ubsan/flex-array-counted-by-bounds.c | 46 +++++++++++++++++++ 3 files changed, 89 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.c diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index 51aa83a378d2..a99e8433069f 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -362,6 +362,10 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, { tree type = TREE_TYPE (array); tree domain = TYPE_DOMAIN (type); + /* whether the array ref is a flexible array member with valid counted_by + attribute. */ + bool fam_has_count_attr = false; + tree counted_by = NULL_TREE; if (domain == NULL_TREE) return NULL_TREE; @@ -375,6 +379,17 @@ 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); + /* If the array ref is to flexible array member field which has + counted_by attribute. We can use the information from the + attribute as the bound to instrument the reference. */ + else if ((counted_by = component_ref_get_counted_by (array)) + != NULL_TREE) + { + fam_has_count_attr = true; + bound = fold_build2 (MINUS_EXPR, TREE_TYPE (counted_by), + counted_by, + build_int_cst (TREE_TYPE (counted_by), 1)); + } else return NULL_TREE; } @@ -387,6 +402,7 @@ ubsan_instrument_bounds (location_t loc, tree array, tree *index, -fsanitize=bounds-strict. */ tree base = get_base_address (array); if (!sanitize_flags_p (SANITIZE_BOUNDS_STRICT) + && !fam_has_count_attr && TREE_CODE (array) == COMPONENT_REF && base && (INDIRECT_REF_P (base) || TREE_CODE (base) == MEM_REF)) { 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..77ec333509d0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c @@ -0,0 +1,27 @@ +/* test the attribute counted_by and its usage in + bounds sanitizer combined with VLA. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#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; +} + +int main(int argc, char *argv[]) +{ + setup_and_test_vla (10, 11); + return 0; +} + +/* { dg-output "17:8: runtime error: index 11 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" } */ -- 2.31.1