From patchwork Fri Aug 25 15:24:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 1826204 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=VVud1acF; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RXP1S6ffLz1yfK for ; Sat, 26 Aug 2023 01:26:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 42ABB385842A for ; Fri, 25 Aug 2023 15:26:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42ABB385842A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692977178; bh=J2ZZj7DJBAapJ3W2Hsq+zazNq3PQ0nuKgjYcmTQezkw=; 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=VVud1acF28Lu0gd1CyVv/LlLss54JwvGOmtXqZfIxtYDQKFRheANCe/oC3jX4/qeP MZ7+hrCexC5RIj6nyEGQYwhJzUVAEWgS0eu1KVgEdJ/vjn9PZhSzu8g83TIQgYRVqA O21w18HnT2Gg4pfLbJxOpLgXxJPM0EFugxnU7+BY= 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 4DBB538582B7 for ; Fri, 25 Aug 2023 15:24:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DBB538582B7 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 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 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 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> 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: V5QZ3mFwNuSmVIecqnU+SnPokFLFB6HhhAIZPrFU0oZmY3alI+nJdHl10vcXb1PDpp72li3qpEV5Zld6iaZgox973LmqEDN5ZqaNs2PCC3zGc3MVbJCKPkHXnTbw+S/OAL+gbMDpuM9ssaR/Wep7/85+cCMK1d7QbgynTUMDldjaNn13sAS26rIri4YTaFUGv4szogAY7VTkxyJ8kcTKcdojONUBpNW3HhKuXKLqwaNSKOdCWd8c0fmsSZrhs07dcqI5tz08sPkgxHcN/ktQls6v6tzAxNqiQrLwShDFq4RhTtejEnpxHIxSzElWTvyqCPIubQjoFCajUCrTQHZMcKJUIjU+rdPwG0R1F9h5PQwFwa63gbRnmI5vN9/js2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D0fqiyPqUofsF5a4lA/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 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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" } */