From patchwork Fri Feb 16 19:47:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qing Zhao X-Patchwork-Id: 85905 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 D2D23385C6E5 for ; Fri, 16 Feb 2024 19:48:50 +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 2F58A3857708 for ; Fri, 16 Feb 2024 19:47:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F58A3857708 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 2F58A3857708 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=1708112872; cv=pass; b=Ozkpu4jKPZkHfEiAe680Y+Cnc8GsHWEwQ6YqXEFY0UtZnLPBmMLMzLhWVg3MQbS7BRzgYtFI35Cd69tFT+mEYE+iJdp1TRoFuz8JjHMDu7SgqQLSss/5zomGEt/9MKboGqLLXr8l6o1AB43yKe9jhmzeIJ1ID/fl8icFOH74ygY= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1708112872; c=relaxed/simple; bh=QWNdJyUSeW99rAMWgPFWQIwvp6mmUrXdvKZ7IXeHThw=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=ZG1fP8SNLq6Ifi07czVY8IUJkyOUW32uMNIPMwTdBiwuWLzd00ytjNGpSda3I+9m+FyVaizb3rS86Eqeawny03mWRez/nmoqTZ4FI/fFdZ9/2u1l4NmodZf5qgxniSgmn153K7lm/53mt62TiSe/NS8Sdq4IsYtS2H9CKhGBvHE= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41GJGPNA006584; Fri, 16 Feb 2024 19:47:48 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=NVs5N3JU6rpeTiI4CFfGlJke/DLoz1UnDQ+whSU3fEg=; b=oavEtdfrCQwaDCOo0Q7o9M8dohFWc3gGZjAuBErOM85inWWh42rSPK8SYbBj4tePJ29t CzPfaRgeY3y8bDpXehBvMGX7IRuAaZKey2HIvKU9fTbIMrGK3iGeEu6OfnH+PXs2A9R5 29iIglp7XBy0aH0ZJ7RLHMgxRmh3ywX4TbaJf0RpF7IgwQjXUzvBovX+0CmEbpQ6T5dP GPSPzMRdOXjxWOLLUoNl+tt21LSQ6x+kb1vPsdyCBmLbz8ltgvcYsWLT32w/oUkZ0B2j yAlzaQI2LT2ULJKL6l+/hn+zg7FmaQP9pPXnHhrRU/mjcW9/fHqYEzNhhdSNgwDtbWF2 Cw== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3w91f068dc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:48 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 41GJG8lp015103; Fri, 16 Feb 2024 19:47:47 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3w5ykcm1a5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Feb 2024 19:47:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nckFxuhsP2LJ9nAMy/WNzhUhj8q/oP5FMpBXAcniLUHOxfrRWoM+zIAotPHkqLc7Ovawrqvu2xPn0KhqW/4gCvIEvEkTBK49yLw9zzRKpii1+lYaqNSPiIbMdQDByo7bMib/KX01QW8hd/PCR/bMJaJfViWjakvR1aEkw3tIqvkNkHRTYxppQN/b0XboK3YAgjDKUuFLxcEXTQLDGa914EsxNYSdsmcfilx1VgytTJ1NI6eX7MwP/VA3WRva25sz6cNWk2b709lEzHABONJz6+zcVqU5qRtmDM3lGZz1bS3+mSSY6wWvAVympURWWb+2FZl346Zc0CnY7lrf+tMk9Q== 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=NVs5N3JU6rpeTiI4CFfGlJke/DLoz1UnDQ+whSU3fEg=; b=iuCmInfVBLvg9uAjfD23n5jMYv5wihOTVKwxAVeru6mcii/fuqsl2WzIPGTYKUYCMZLeTwrhKbhdCZbI2mKXTpILfg17Lf9pDeqnU7yMEDZg5Mzj91cb/QtCbZHp4C2BP7vGUyg1b+0xS2TWoXxE3deR5C1cir7H1aopvd4gJ5xnovWiLK9EamKerFmWQDoM0woF2+Ko3/aGJEEqf2fjCh0sOb6fzXwO/lN2JCDqjDsYF1oq4WqhaPC19iE++5Nuu9eTx/VlAdB+nt5j6ybQLIzOB1rnM4q9Gx568RJwNpqNTY9KcHuFnx8mtjQ87wkrjtW7gBqKokoNE50UuEJDyw== 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=NVs5N3JU6rpeTiI4CFfGlJke/DLoz1UnDQ+whSU3fEg=; b=CzCRj9U9duw7zFn/25hDQ0/OYTXrAbaZeaaQLCs1HKTTajEqaRTVX04Ko0JCWltmaRuxIzzUSTlpqrDGKbtiDdVegDg8fUDOgfBt+odo/ssLkALzBaxBF0Od7LD4VfA0euDIH0IzspaGUnqKPUnP/fcQZ94d+NEPZ1SfkvLYLPE= Received: from CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) by IA1PR10MB7238.namprd10.prod.outlook.com (2603:10b6:208:3f6::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.28; Fri, 16 Feb 2024 19:47:45 +0000 Received: from CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::1e58:cfe8:d9f5:6363]) by CH3PR10MB7957.namprd10.prod.outlook.com ([fe80::1e58:cfe8:d9f5:6363%6]) with mapi id 15.20.7292.029; Fri, 16 Feb 2024 19:47:44 +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 v6 4/5] Use the .ACCESS_WITH_SIZE in bound sanitizer. Date: Fri, 16 Feb 2024 19:47:22 +0000 Message-Id: <20240216194723.391359-5-qing.zhao@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20240216194723.391359-1-qing.zhao@oracle.com> References: <20240216194723.391359-1-qing.zhao@oracle.com> X-ClientProxiedBy: MN2PR06CA0022.namprd06.prod.outlook.com (2603:10b6:208:23d::27) To CH3PR10MB7957.namprd10.prod.outlook.com (2603:10b6:610:1bf::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR10MB7957:EE_|IA1PR10MB7238:EE_ X-MS-Office365-Filtering-Correlation-Id: 24bc95f7-e4b4-4ed2-181c-08dc2f28251e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Wu0vvvWpDWGvdiiBanEEk6Rr+0pNKTYt4uBGterwn0ejnJa6ZrFrPe+rqOWTmhYQZEgHwko0WObSSLsLv3XckTsZ+0tF+aj7iKc5fqu1X4FfpUhX8WlVObvWNB7rwvAk+g+ggojWChjq0/yQ+d8njem9UTvWaAAINIo+qk2LmnJJq8f/IR1Im1o7l2JxoOzjeI0G/u2DcZJoSy+g64Wrl00SMGZeZ6095mS+i5O2oUpwcJBsfKutPYOB4jFQmynb0p9Z44LoQ2TJpBikBfPQz80KoC3tvKlh4eChWRvJ4NxeKv2aBODgU6wASLKl0BEkpVIDRvdhf7Ab+S0nlkKorgI1smcPWFhIMvfi0Tujs3z4kBY0iWV4LxWSFad1iRwBm59lElNWZyh6PtEHBAuSEWEd7MPS5fDW5VVykRi3OyBwZV/dAjnZ3683v1KgbiFJvGHGRNuKhXL8GfZKs3Eo3JYzENaZ9OtVVLFRjv4TbDfGkhgiaANWGC0b4HX5lsv3FG3T1y42lRgjC3Bj2plLV+1FHJYql5l3THQHWtF4QHA= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:CH3PR10MB7957.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230031)(136003)(376002)(39860400002)(346002)(396003)(366004)(230922051799003)(451199024)(1800799012)(186009)(64100799003)(4326008)(8936002)(8676002)(5660300002)(6666004)(2906002)(83380400001)(86362001)(107886003)(1076003)(36756003)(38100700002)(26005)(66556008)(2616005)(66946007)(316002)(66476007)(6506007)(44832011)(478600001)(6512007)(6486002)(84970400001)(41300700001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Itsp/ZfTBP2i0xE2grqIPaVQdD+tE9ImroNvr2zxuhtCBFMIGYLk22xyAeiXCADlE9RMMYDMzGSMQhCs1qWNLp0jM9IFHve6V25Se8txxkEP56dgPERvwZ4TOqEz/vzixek1M64CNXygrluFMnOpL091M0YVApGaY6e1AJYXsvvJYqn/ypj8U6zpJRHVFLVVa5qtaXL16MKc+lnXdhHatsJAJC7wChhf5dSM23rACItZKVzB4e+hgf/D/zyOcCEK6is6d7BH9TlD/1k30K7fAt9BAFO2ugcdELEPxyU2btRcaBixrX6a84mjl/5gLHrzM77Sck88sDlxdO9cE2m7l+f7i33yzFI8CldnSJtkGqMQJdxb0N2ofQeoxuijqSknHlGHIdbjT4kr3yqhf2oLSMZL77mN2917UoKBHZRF55Aeqf71NBv7oVMXBPRMloZ1/lufOvGrWF6gAQRbhqsvWw+c6yJ+2vtKmI8yLza9T1IZYRjL+yF/YEZJmQd3RdHl2YA3aNp3/45TR7TSSwO5V1nONHh9rIH3inaj6MARwY8qHifAs7fzW6pn7t7AmKLlW17ME4MKa4UkFOaZS9WpP9tp0qpTgMh82iciX3U4Xih8txjsYmLJICzL6A7X/s3v3yXQjXOOoxA7CqhIF77rFNDO3XWBVjaboTMSW8/wWAyAsJch5ggytqSYffRLG6wFfTda0VnIbxIvQL4ulI5NmYDV0zTHfecY6LdXpXtC+R1x+VRBgKxAEgb+apKukhxQOR0hkkkxXnSqf3mafSi1lZw25tu2Jo/uFZPnaD0hXV+h4lZp6NZOZgyxaINsIQz28Z9RPH7snMc0Q/ZkfbT84PmhyZJ46x0Gyl6/WoQlVaS14cSYSc7SkghyUFE9d84/gurZi8ex1QGaq6pm9Uv1ieNqmoeeIV5MubreEDBv6AT4+mh6gBRkhCxSGf1DQVTjbH/mlQSGwGLGU+WmEHWXq6TK81FMCfbq8mFQrVV+WxrNn42kJalH6osoT2HV4lifAWKkj89QI51JuOdUE9khl4Se2Hhmno13np/pgYWbffl87Cpk3fXbO3Q5azlbKI8qGGZc/tZrD3JRBWunWltmZ05EZxhqh09z1vnsbDkXQJQgnkFJe/BY3YBiGsuap6iCuNPQorecp7L2qEfYLm+yrHaImfUZSMImbQs0FUMHOgxBSl5/SkuOi156MroKWE+S6ZpDO7dM/PuLqCIAje0kdg9CsE3mQJVypsPbOp+t+2TLTaxcGSYxvlIW29Vh+Sqz4dD+5LmTubuM7D//BocYqVr0lG1Ec93Qek6cRavulq21PZmBaKgP7Jkeb6JayXJfWe48DdUvkCkqbT8iJDyzPow1C09uQB/YnuXauftQCL9uHywpSv6hMZkwZpbZAM7qtNLzkTfyQPLiDeMac+FDB1mG6iziq2OfsSW+xTKKrSgbR47knHjx70MVZT2aEDw462ck6dZxiGKV3frF0pPiOFKzdBpNfxgOL6s2n7bhtunGeOWVHoeYK/rBCt3vX3PoyEp9OTUCJG9m11SEAr7Jq7Ul2opo0wsyXS8hPOs6QSEKDw6op4d8QMhgoCe8IjJz X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: Gzq48VKX9IwvKfxLlvmJnCMfDTptPnJY69iTjHLCKEz+IQkLgEQuERR2+ZuBwJt9+GL7eXW1U4ZINRQN9yvaAcD7EI7z0EltoAkYdCCsoD5fh90wil/bdh8GzYp1HCoJZDGpgM9KcfAHJTm5FxvFb25zSwPWRFJ9Y5nLH41iTBaJi3TEHqY2Qdh6DqRgOv2Rl0heSn8mWazEya73UT+Wu9c5IMJeBkxnx7QJldQ7mUIj6sMs3j7k2fEQho05M+1pj+RL0lH7RzQO9Yi9glIu8/UzZZbnp049LcEaMoFMDZB0WuzL55LoDjrylleRwsPkv1R6cwgcspjKykI62w3bHjTzUnhJ44AXvkbvPhfZu9SR5CWJprHkcjhSp2Vd9h+n5uc6wNZiKmPjWpx1uCGe8a2kp/xSdjqy0m50n+4pOzUl+eqMdI1C8O4Ri9EDPe7M+wPHe5hnZQ9USG0V/08y7u7oRBQJVsaES7d5zQbJfulRLbb90CF0NNWozQywgoJ9KtOhNXErgtacTCn6vYleGQi8O0LfXrYhSA8pbcy2LABLuxnS5XBFs9Q4HSC2seQIiAOMPo3yriGpJX53JFNTgZtH82zB6qI/F4zac+iAOQc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 24bc95f7-e4b4-4ed2-181c-08dc2f28251e X-MS-Exchange-CrossTenant-AuthSource: CH3PR10MB7957.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Feb 2024 19:47:44.9311 (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: +0klReQnrw1M+Sf3K3U9S4rUshnhdzipCSif/EVDwJ1tPZ42PdN0ibIsP0mMznXaSktMxJqekOA4104H2Dz7JQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB7238 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-02-16_19,2024-02-16_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 bulkscore=0 suspectscore=0 phishscore=0 adultscore=0 spamscore=0 mlxlogscore=999 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402160154 X-Proofpoint-ORIG-GUID: lFs5WjF_k1PKx7QV5sfE7m-mI05EnShn X-Proofpoint-GUID: lFs5WjF_k1PKx7QV5sfE7m-mI05EnShn X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, 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/c-family/ChangeLog: * c-ubsan.cc (get_bound_from_access_with_size): New function. (ubsan_instrument_bounds): Handle call to .ACCESS_WITH_SIZE. gcc/testsuite/ChangeLog: * gcc.dg/ubsan/flex-array-counted-by-bounds-2.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds-3.c: New test. * gcc.dg/ubsan/flex-array-counted-by-bounds.c: New test. --- gcc/c-family/c-ubsan.cc | 42 +++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-2.c | 45 ++++++++++++++++++ .../ubsan/flex-array-counted-by-bounds-3.c | 34 ++++++++++++++ .../ubsan/flex-array-counted-by-bounds.c | 46 +++++++++++++++++++ 4 files changed, 167 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-3.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 940982819ddf..164b29845b3a 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -376,6 +376,40 @@ ubsan_instrument_return (location_t loc) return build_call_expr_loc (loc, t, 1, build_fold_addr_expr_loc (loc, data)); } +/* Get the tree that represented the number of counted_by, i.e, the maximum + number of the elements of the object that the call to .ACCESS_WITH_SIZE + points to, this number will be the bound of the corresponding array. */ +static tree +get_bound_from_access_with_size (tree call) +{ + if (!is_access_with_size_p (call)) + return NULL_TREE; + + tree ref_to_size = CALL_EXPR_ARG (call, 1); + unsigned int type_of_size = TREE_INT_CST_LOW (CALL_EXPR_ARG (call, 2)); + tree type = TREE_TYPE (CALL_EXPR_ARG (call, 3)); + tree size = fold_build2 (MEM_REF, type, unshare_expr (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 = fold_build2 (LT_EXPR, boolean_type_node, + unshare_expr (size), build_zero_cst (type)); + size = fold_build3 (COND_EXPR, type, cond, + build_zero_cst (type), size); + } + + /* Only when type_of_size is 1,i.e, the number of the elements of + the object type, return the size. */ + if (type_of_size != 1) + return NULL_TREE; + else + size = fold_convert (sizetype, size); + + return size; +} + + /* Instrument array bounds for ARRAY_REFs. We create special builtin, that gets expanded in the sanopt pass, and make an array dimension of it. ARRAY is the array, *INDEX is an index to the array. @@ -401,6 +435,14 @@ 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); + else if (INDIRECT_REF_P (array) + && is_access_with_size_p ((TREE_OPERAND (array, 0)))) + { + bound = get_bound_from_access_with_size ((TREE_OPERAND (array, 0))); + bound = fold_build2 (MINUS_EXPR, TREE_TYPE (bound), + bound, + build_int_cst (TREE_TYPE (bound), 1)); + } else return NULL_TREE; } 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..148934975ee5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-2.c @@ -0,0 +1,45 @@ +/* test the attribute counted_by and its usage in + bounds sanitizer combined with VLA. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ +/* { dg-output "index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 20 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 11 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*index 10 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ + + +#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; +} + +void __attribute__((__noinline__)) setup_and_test_vla_1 (int n1, int n2, int m) +{ + struct foo { + int n; + int p[][n2][n1] __attribute__((counted_by(n))); + } *f; + + f = (struct foo *) malloc (sizeof(struct foo) + m*sizeof(int[n2][n1])); + f->n = m; + f->p[m][n2][n1]=1; + return; +} + +int main(int argc, char *argv[]) +{ + setup_and_test_vla (10, 11); + setup_and_test_vla_1 (10, 11, 20); + return 0; +} + diff --git a/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c new file mode 100644 index 000000000000..33bdea1c430b --- /dev/null +++ b/gcc/testsuite/gcc.dg/ubsan/flex-array-counted-by-bounds-3.c @@ -0,0 +1,34 @@ +/* test the attribute counted_by and its usage in bounds + sanitizer. when counted_by field is negative value. */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ + +#include + +struct annotated { + int b; + int c[] __attribute__ ((counted_by (b))); +} *array_annotated; + +void __attribute__((__noinline__)) setup (int annotated_count) +{ + array_annotated + = (struct annotated *)malloc (sizeof (struct annotated)); + array_annotated->b = annotated_count; + + return; +} + +void __attribute__((__noinline__)) test (int annotated_index) +{ + array_annotated->c[annotated_index] = 2; +} + +int main(int argc, char *argv[]) +{ + setup (-3); + test (2); + return 0; +} + +/* { dg-output "24:21: runtime error: index 2 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" } */