From patchwork Fri Oct 28 16:34:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Corallo X-Patchwork-Id: 12448 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp928890wru; Fri, 28 Oct 2022 09:36:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM51ReY2RaZF+hi45ICBSfTAksSkqc9DaEgWRh+jKOmj8MB9Vip9p98sHGjeT69gof16CpD6 X-Received: by 2002:a17:907:2c68:b0:7ad:8ba7:664a with SMTP id ib8-20020a1709072c6800b007ad8ba7664amr193259ejc.488.1666974962240; Fri, 28 Oct 2022 09:36:02 -0700 (PDT) Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o20-20020a170906975400b00782161b3422si5534635ejy.914.2022.10.28.09.36.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Oct 2022 09:36:02 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ISbgHBOg; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 2659F385D0E0 for ; Fri, 28 Oct 2022 16:36:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2659F385D0E0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666974961; bh=VzFR/0Dk2tRIOuIs5xc8/6KwlNvwDxX38N2EX+LL1eI=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ISbgHBOg2o+Hw9ezcOixwTO30XPHXIMlIyqWijPNPYE8VtWXjfVZ5L3krzdDRHs2A tSNARylftf0LAreWafahB57ZLG6Z92BsaCODo8msMWI0ymlT3VBgci4sNf/j3QdKp+ UEiP9kcTm3yqukBUPBLhIAtz7pBR6aQfvSJ+TxIM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03on2059.outbound.protection.outlook.com [40.107.105.59]) by sourceware.org (Postfix) with ESMTPS id 4CBC53858D20 for ; Fri, 28 Oct 2022 16:35:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4CBC53858D20 ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=d/EDbwol08CHpQ3Avjt0WYUSSkJ/m8p96wMWp82CncdlMWfXYZE5DWOWgOm4Erce6o3mQUsXbwdiJamvYc/QPFq9v/91WJqnpGu3RjZ+K8lKjwNWK9mk16c9qGUW8ukBESns7O4I/DgbCWtaNM50IphTrFH/O3/LyPHo+n4B2knrpTz5uNxTRBXrye2IaOW56gMJVFLph80VhrKv18W249/8+k5OX/q28A3Ahw9j5Wm++JMGp8oBKkMh332JNrsF2GLE6xSO3pbhPSV1cW97ZNVHzm19ewXDKpop2qimbBwYlw4EG/0XJegSq83vXB9lVAHf08Z7vc4cxWCKZe8MLg== ARC-Message-Signature: i=2; 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=VzFR/0Dk2tRIOuIs5xc8/6KwlNvwDxX38N2EX+LL1eI=; b=E2uQxy79TR7nC5mJBPs/2uM7IwMRIJElkXMsGmi1u7X2DIinwjq/jwdAIF83lhGWXY3D4S0tuTf71q6w3Eh5lXY06iELMq0pnqHXfjdMFv9uRKSWE5qs+24izCwSzsMm27rni7ruyptGbIr0q8ZvrNCtVioZ/Hl1pz65j+ONs+4BDjONS+mG3vVMNFdh1U+cWUd1CvufsdOi7NxL5eRJqlB38yVf/NywYovzTMCMSxH33Bp3Uohg/L9j8YhHULhIut79axa/F1VoJCppdxvcPl5I7+rtfLEIeWiHZEeMxswXMggCG+/yOOu+hD+r1+/CnVUEkadj3q8ojl6QCYhqzA== ARC-Authentication-Results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dmarc=[1,1,header.from=arm.com]) Received: from AS9PR06CA0619.eurprd06.prod.outlook.com (2603:10a6:20b:46e::33) by AM9PR08MB5892.eurprd08.prod.outlook.com (2603:10a6:20b:2dd::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.14; Fri, 28 Oct 2022 16:35:00 +0000 Received: from AM7EUR03FT053.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:46e:cafe::61) by AS9PR06CA0619.outlook.office365.com (2603:10a6:20b:46e::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.15 via Frontend Transport; Fri, 28 Oct 2022 16:35:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM7EUR03FT053.mail.protection.outlook.com (100.127.140.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.16 via Frontend Transport; Fri, 28 Oct 2022 16:34:59 +0000 Received: ("Tessian outbound b4aebcc5bc64:v130"); Fri, 28 Oct 2022 16:34:58 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 873eb35a4f6db29b X-CR-MTA-TID: 64aa7808 Received: from 5a6490c63ff3.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 63254FE6-199B-437B-B2C3-7B82C9E0B3A5.1; Fri, 28 Oct 2022 16:34:50 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 5a6490c63ff3.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 28 Oct 2022 16:34:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g5U1b6ALw+SkL9SrIfBcE8cNODSMtrSJDaQUHpShfuCp93mZIy/nfPO+4e+OUFuaJI0v4EGJrJgQK6sSjEFrXLekwJu6/UuNUEIgyR1pJ8O+avYfkAz84fBIh5iK+/LkEeKJqmuUzFpRtVejWMwsD2+k34rvPcybURqjOOQSlc1OARhgQy3p58hny51Gs3YmiriI5f/e9ctNGVKAUDwFboMJL7/MZyjpCjlL2fGjrYQh4i3qWkgstmaTb4Zlo53vdA/US2Rn1lQu0h6KdnnXIhc1HNwfDulnDT3TQsPqA6eppegJHyqZUbUvHaCpP4uBt5Wg6OiGfpZiXpEoop/hhg== 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=VzFR/0Dk2tRIOuIs5xc8/6KwlNvwDxX38N2EX+LL1eI=; b=BPRG6oIKfEL7zk3K+ign68QZ15xlGh8wO7yYoutuUvHDaggadGzBbVf3aX4PQtMpid4cU6+xeu7Z+8GmrTs2hbYi7O0Gjgz6KHfPPLOf7AdwcFpgu+HAl0Ri9VE1Mtxkvy1pCX+WAsLJEQMKtmie3fTuDyNOGmWvknd5z6ugE4BeeE/Tyz0ISqe6jTeU+YiT8h8gaqOWu64CeKx8ZQ1cP2QG967ane1f0RpncrUAwyhLtFhBllVnMGJLkAvu0hox+MtpMYOrY3pJ8FyK8ea0lvbX/07Z9T4g8oQtQHo+xePOh/nF9zYBUQWSl2ynPF7i7TLX//MlslEY4ijKFcgXgw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=gcc.gnu.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none Received: from AM6PR04CA0056.eurprd04.prod.outlook.com (2603:10a6:20b:f0::33) by DU2PR08MB7373.eurprd08.prod.outlook.com (2603:10a6:10:2f1::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.15; Fri, 28 Oct 2022 16:34:44 +0000 Received: from AM7EUR03FT060.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:f0:cafe::3b) by AM6PR04CA0056.outlook.office365.com (2603:10a6:20b:f0::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.15 via Frontend Transport; Fri, 28 Oct 2022 16:34:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by AM7EUR03FT060.mail.protection.outlook.com (100.127.140.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5723.32 via Frontend Transport; Fri, 28 Oct 2022 16:34:44 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX03.Arm.com (10.251.24.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.12; Fri, 28 Oct 2022 16:34:43 +0000 Received: from e124257 (10.34.105.24) by mail.arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.12 via Frontend Transport; Fri, 28 Oct 2022 16:34:43 +0000 To: Andrea Corallo via Gcc-patches Subject: [PATCH 10/15 V3] arm: Implement cortex-M return signing address codegen In-Reply-To: (Andrea Corallo via Gcc-patches's message of "Wed, 26 Oct 2022 17:48:00 +0200") References: Date: Fri, 28 Oct 2022 18:34:42 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: AM7EUR03FT060:EE_|DU2PR08MB7373:EE_|AM7EUR03FT053:EE_|AM9PR08MB5892:EE_ X-MS-Office365-Filtering-Correlation-Id: dd9e2877-40a3-4f99-e949-08dab9025acc x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: T/Hknl9RMKsi45evVuOCsTTzRslce2bS8S/J3PQHU/c/lhwtgGYE9glAYvtjfLnuFRRqrYb+zkHip1mdly6Pt/b0tZXn5n1qHjeXk50dPMxyPscID/VAsp+LiaMogc3v8CJbw6+7WrVdnQ4A17LgGaQFrnNiUKvYLKGvdSdLN/PjCXFTzx/Dr03hLhvKf6SaJmx7WlyJdFuGmjip+hAsWlvM6cvydVukR5zjo6avcF5D0DGEKR08WbQQoPbLA0pvarDJGLS/0EM9qkpR/fmYDJmlOif5POG8bgvsrZln8tyQoLyG5XrobK5E684EdwAq5AQHGA8zQDCOehO4fwtc7JABzUBNkRBTjuKtj+bJar7XCUASW/GcwifZFKc/g25loQhCBjYKGVHiF2Lpi2Vg/SnYRWPhprFbBqzyV1EMBQqRsCRcIChJ7SWJG8yGbYoN5GXHfAQYa2glLIp2bK3zBn2rqY5GOzjZ0KrD/jRLYzOwREmmI9dK/uk8u2+FoqtV8EuQBX3VCkvEzj1dBssyCxJm5N4oHIrfvMbqZLIJbUVrkJLokOuj1Nw+VZt2TC2oixciWYXcXQkiGuU9nhxDvHfe8Ddo7F0KwkCYEHYpEmb2QxgK1vDJVI//bbgaJUQPWsuGNJaDCBBiEKGtnjPPGqsL4wyEQrp8hwe+TvfqAPVvnyk8r5SnwOgOn2wwbSkeVtachNaTyfphROtafDiUVESDYr9/z4RReMZhp/8cHNbYuIYr8sU07SrNyHbOItRKpeg5zVQrv2qTS53wv6VcAx263/0XzUYBzCtjwU3MeJIvWj4mCLXvST5uzd0VQfEE X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:nebula.arm.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230022)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199015)(36840700001)(46966006)(40470700004)(86362001)(564344004)(36756003)(82740400003)(81166007)(356005)(2906002)(47076005)(186003)(336012)(426003)(40480700001)(40460700003)(44832011)(33964004)(26005)(36860700001)(478600001)(2616005)(83380400001)(82310400005)(54906003)(6916009)(316002)(41300700001)(8676002)(4326008)(70586007)(5660300002)(235185007)(8936002)(70206006)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU2PR08MB7373 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM7EUR03FT053.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 6ef3f214-4ead-4a7e-623d-08dab902521f X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QeG9K6lXw8hl8e8+cPipj0wyjBLIK3O+IwmoAv9CcoI//rdW+jczMKd8uAktsh9L7voxlIR3a6hWoFq+D4tx94fesYQsRsnPkFuh8UV5gGZ6cLbDRPzHU3gPPC+/H2pRQOuWfjeU1T+i9FHx+ZLzjXEqCflK10QbUWQuty/voaA2oFJETcccdUDqTY+hJswTW5NjoUHaCjjHbp9m87nAz7ePDgEB+k1IfmmE0l0YhaOaY3lpGVz/gr1yIERVrqN5qai++As7cd+SCF9QC5xby1vvIUawBz9gYw7fb1pO8crV/+/j+RYi0ZhYfeKrNffU5iCdToylyyO2omqd2u7XQG0TbiUx+NdZGD1dpcR3f7vwlptHcrzGnQQFjJlB6HUdss8ILQcF2Dr0TaYmawRHQP49CZz2nnle4ozTas3dKH8jYzrwzrdixz7oThfE9g8GzjFI6NuHgoVpxXdbgzj2gC71nHbyU1NaCmzHMirYzUzgc4S5Xi/22W5Ai4QlPwCZYb90QeotJMAYOmmGHn353Z3T7QAlLbHIN/2apjwgpmxcdVKNRyIJcKvPo72kF23g/GlX3xt6fDyzfsAurzn+a+7fVKWHPR4ydnY6M7OEHsQBWqGnzJfFvXOZHFcL3ve5KkPUiX9o9KpvCoeo5Fo4ELIlGpMoPDu+4DxTqrnHCsKxKJRytSX33t5OFzp/MEikQxFIszwJlENmQYFVglEGbQjgPg4DViAMkG2xVvmzXTlygHu1icoxwfiws+f1lemCDQtwr3iMNQ7/0El+8RFygg== X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(13230022)(4636009)(346002)(376002)(396003)(39860400002)(136003)(451199015)(46966006)(40470700004)(36840700001)(2906002)(186003)(82310400005)(47076005)(426003)(336012)(2616005)(83380400001)(26005)(33964004)(36860700001)(44832011)(40460700003)(70586007)(235185007)(41300700001)(70206006)(478600001)(564344004)(54906003)(5660300002)(40480700001)(6916009)(316002)(86362001)(8936002)(81166007)(8676002)(82740400003)(4326008)(36756003); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Oct 2022 16:34:59.0016 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dd9e2877-40a3-4f99-e949-08dab9025acc X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM7EUR03FT053.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR08MB5892 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, UNPARSEABLE_RELAY 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: Andrea Corallo via Gcc-patches From: Andrea Corallo Reply-To: Andrea Corallo Cc: Richard Earnshaw , nd , Richard Earnshaw Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1740970280852940504?= X-GMAIL-MSGID: =?utf-8?q?1747949938315009162?= Hi all, the third iteration of this patch is attached addresing review comments. Thanks Andrea From b42e28be75f374a4e1a5943c8c9002e07dbcc567 Mon Sep 17 00:00:00 2001 From: Andrea Corallo Date: Thu, 20 Jan 2022 15:36:23 +0100 Subject: [PATCH] [PATCH 10/15] arm: Implement cortex-M return signing address codegen Hi all, this patch enables address return signature and verification based on Armv8.1-M Pointer Authentication [1]. To sign the return address, we use the PAC R12, LR, SP instruction upon function entry. This is signing LR using SP and storing the result in R12. R12 will be pushed into the stack. During function epilogue R12 will be popped and AUT R12, LR, SP will be used to verify that the content of LR is still valid before return. Here an example of PAC instrumented function prologue and epilogue: void foo (void); int main() { foo (); return 0; } Compiled with '-march=armv8.1-m.main -mbranch-protection=pac-ret -mthumb' translates into: main: pac ip, lr, sp push {r3, r7, ip, lr} add r7, sp, #0 bl foo movs r3, #0 mov r0, r3 pop {r3, r7, ip, lr} aut ip, lr, sp bx lr The patch also takes care of generating a PACBTI instruction in place of the sequence BTI+PAC when Branch Target Identification is enabled contextually. Ex. the previous example compiled with '-march=armv8.1-m.main -mbranch-protection=pac-ret+bti -mthumb' translates into: main: pacbti ip, lr, sp push {r3, r7, ip, lr} add r7, sp, #0 bl foo movs r3, #0 mov r0, r3 pop {r3, r7, ip, lr} aut ip, lr, sp bx lr As part of previous upstream suggestions a test for varargs has been added and '-mtpcs-frame' is deemed being incompatible with this return signing address feature being introduced. [1] gcc/Changelog 2021-11-03 Andrea Corallo * config/arm/arm.h (arm_arch8m_main): Declare it. * config/arm/arm.cc (arm_arch8m_main): Define it. (arm_option_reconfigure_globals): Set arm_arch8m_main. (arm_compute_frame_layout, arm_expand_prologue) (thumb2_expand_return, arm_expand_epilogue) (arm_conditional_register_usage): Update for pac codegen. (arm_current_function_pac_enabled_p): New function. * config/arm/arm.md (pac_ip_lr_sp, pacbti_ip_lr_sp, aut_ip_lr_sp): Add new patterns. * config/arm/unspecs.md (UNSPEC_PAC_IP_LR_SP) (UNSPEC_PACBTI_IP_LR_SP, UNSPEC_AUT_IP_LR_SP): Add unspecs. gcc/testsuite/Changelog 2021-11-03 Andrea Corallo * gcc.target/arm/pac.h : New file. * gcc.target/arm/pac-1.c : New test case. * gcc.target/arm/pac-2.c : Likewise. * gcc.target/arm/pac-3.c : Likewise. * gcc.target/arm/pac-4.c : Likewise. * gcc.target/arm/pac-5.c : Likewise. * gcc.target/arm/pac-6.c : Likewise. * gcc.target/arm/pac-7.c : Likewise. * gcc.target/arm/pac-8.c : Likewise. --- gcc/config/arm/arm-protos.h | 1 + gcc/config/arm/arm.cc | 77 +++++++++++++++++++++++----- gcc/config/arm/arm.h | 4 ++ gcc/config/arm/arm.md | 23 +++++++++ gcc/config/arm/unspecs.md | 3 ++ gcc/testsuite/gcc.target/arm/pac-1.c | 12 +++++ gcc/testsuite/gcc.target/arm/pac-2.c | 11 ++++ gcc/testsuite/gcc.target/arm/pac-3.c | 11 ++++ gcc/testsuite/gcc.target/arm/pac-4.c | 10 ++++ gcc/testsuite/gcc.target/arm/pac-5.c | 28 ++++++++++ gcc/testsuite/gcc.target/arm/pac-6.c | 18 +++++++ gcc/testsuite/gcc.target/arm/pac-7.c | 32 ++++++++++++ gcc/testsuite/gcc.target/arm/pac-8.c | 34 ++++++++++++ gcc/testsuite/gcc.target/arm/pac.h | 17 ++++++ 14 files changed, 268 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/gcc.target/arm/pac-1.c create mode 100644 gcc/testsuite/gcc.target/arm/pac-2.c create mode 100644 gcc/testsuite/gcc.target/arm/pac-3.c create mode 100644 gcc/testsuite/gcc.target/arm/pac-4.c create mode 100644 gcc/testsuite/gcc.target/arm/pac-5.c create mode 100644 gcc/testsuite/gcc.target/arm/pac-6.c create mode 100644 gcc/testsuite/gcc.target/arm/pac-7.c create mode 100644 gcc/testsuite/gcc.target/arm/pac-8.c create mode 100644 gcc/testsuite/gcc.target/arm/pac.h diff --git a/gcc/config/arm/arm-protos.h b/gcc/config/arm/arm-protos.h index cff7ff1da2a..84764bf27ce 100644 --- a/gcc/config/arm/arm-protos.h +++ b/gcc/config/arm/arm-protos.h @@ -379,6 +379,7 @@ extern int vfp3_const_double_for_bits (rtx); extern void arm_emit_coreregs_64bit_shift (enum rtx_code, rtx, rtx, rtx, rtx, rtx); extern bool arm_fusion_enabled_p (tune_params::fuse_ops); +extern bool arm_current_function_pac_enabled_p (void); extern bool arm_valid_symbolic_address_p (rtx); extern bool arm_validize_comparison (rtx *, rtx *, rtx *); extern bool arm_expand_vector_compare (rtx, rtx_code, rtx, rtx, bool); diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc index a5cf4225aa2..fa0f9a61498 100644 --- a/gcc/config/arm/arm.cc +++ b/gcc/config/arm/arm.cc @@ -927,6 +927,11 @@ int arm_arch8_3 = 0; /* Nonzero if this chip supports the ARM Architecture 8.4 extensions. */ int arm_arch8_4 = 0; + +/* Nonzero if this chip supports the ARM Architecture 8-M Mainline + extensions. */ +int arm_arch8m_main = 0; + /* Nonzero if this chip supports the ARM Architecture 8.1-M Mainline extensions. */ int arm_arch8_1m_main = 0; @@ -3209,6 +3214,9 @@ arm_option_override_internal (struct gcc_options *opts, arm_stack_protector_guard_offset = offs; } + if (arm_current_function_pac_enabled_p () && !arm_arch8m_main) + error ("This architecture does not support branch protection instructions"); + #ifdef SUBTARGET_OVERRIDE_INTERNAL_OPTIONS SUBTARGET_OVERRIDE_INTERNAL_OPTIONS; #endif @@ -3855,6 +3863,7 @@ arm_option_reconfigure_globals (void) arm_arch_arm_hwdiv = bitmap_bit_p (arm_active_target.isa, isa_bit_adiv); arm_arch_crc = bitmap_bit_p (arm_active_target.isa, isa_bit_crc32); arm_arch_cmse = bitmap_bit_p (arm_active_target.isa, isa_bit_cmse); + arm_arch8m_main = arm_arch7 && arm_arch_cmse; arm_arch_lpae = bitmap_bit_p (arm_active_target.isa, isa_bit_lpae); arm_arch_i8mm = bitmap_bit_p (arm_active_target.isa, isa_bit_i8mm); arm_arch_bf16 = bitmap_bit_p (arm_active_target.isa, isa_bit_bf16); @@ -21139,6 +21148,9 @@ arm_compute_save_core_reg_mask (void) save_reg_mask |= arm_compute_save_reg0_reg12_mask (); + if (arm_current_function_pac_enabled_p ()) + save_reg_mask |= 1 << IP_REGNUM; + /* Decide if we need to save the link register. Interrupt routines have their own banked link register, so they never need to save it. @@ -23362,6 +23374,12 @@ output_probe_stack_range (rtx reg1, rtx reg2) return ""; } +static bool +aarch_bti_enabled () +{ + return false; +} + /* Generate the prologue instructions for entry into an ARM or Thumb-2 function. */ void @@ -23440,12 +23458,13 @@ arm_expand_prologue (void) /* The static chain register is the same as the IP register. If it is clobbered when creating the frame, we need to save and restore it. */ - clobber_ip = IS_NESTED (func_type) - && ((TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM) - || ((flag_stack_check == STATIC_BUILTIN_STACK_CHECK - || flag_stack_clash_protection) - && !df_regs_ever_live_p (LR_REGNUM) - && arm_r3_live_at_start_p ())); + clobber_ip = (IS_NESTED (func_type) + && (((TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM) + || ((flag_stack_check == STATIC_BUILTIN_STACK_CHECK + || flag_stack_clash_protection) + && !df_regs_ever_live_p (LR_REGNUM) + && arm_r3_live_at_start_p ())) + || (arm_current_function_pac_enabled_p ()))); /* Find somewhere to store IP whilst the frame is being created. We try the following places in order: @@ -23470,7 +23489,8 @@ arm_expand_prologue (void) { rtx addr, dwarf; - gcc_assert(arm_compute_static_chain_stack_bytes() == 4); + gcc_assert(arm_compute_static_chain_stack_bytes() == 4 + || arm_current_function_pac_enabled_p ()); saved_regs += 4; addr = gen_rtx_PRE_DEC (Pmode, stack_pointer_rtx); @@ -23521,6 +23541,14 @@ arm_expand_prologue (void) } } + if (arm_current_function_pac_enabled_p ()) + { + if (aarch_bti_enabled ()) + emit_insn (gen_pacbti_nop ()); + else + emit_insn (gen_pac_nop ()); + } + if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM) { if (IS_INTERRUPT (func_type)) @@ -27309,7 +27337,7 @@ thumb2_expand_return (bool simple_return) to assert it for now to ensure that future code changes do not silently change this behavior. */ gcc_assert (!IS_CMSE_ENTRY (arm_current_func_type ())); - if (num_regs == 1) + if (num_regs == 1 && !arm_current_function_pac_enabled_p ()) { rtx par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2)); rtx reg = gen_rtx_REG (SImode, PC_REGNUM); @@ -27324,10 +27352,20 @@ thumb2_expand_return (bool simple_return) } else { - saved_regs_mask &= ~ (1 << LR_REGNUM); - saved_regs_mask |= (1 << PC_REGNUM); - arm_emit_multi_reg_pop (saved_regs_mask); - } + if (arm_current_function_pac_enabled_p ()) + { + gcc_assert (!(saved_regs_mask & (1 << PC_REGNUM))); + arm_emit_multi_reg_pop (saved_regs_mask); + emit_insn (gen_aut_nop ()); + emit_jump_insn (simple_return_rtx); + } + else + { + saved_regs_mask &= ~ (1 << LR_REGNUM); + saved_regs_mask |= (1 << PC_REGNUM); + arm_emit_multi_reg_pop (saved_regs_mask); + } + } } else { @@ -27733,7 +27771,8 @@ arm_expand_epilogue (bool really_return) && really_return && crtl->args.pretend_args_size == 0 && saved_regs_mask & (1 << LR_REGNUM) - && !crtl->calls_eh_return) + && !crtl->calls_eh_return + && !arm_current_function_pac_enabled_p ()) { saved_regs_mask &= ~(1 << LR_REGNUM); saved_regs_mask |= (1 << PC_REGNUM); @@ -27847,6 +27886,9 @@ arm_expand_epilogue (bool really_return) } } + if (arm_current_function_pac_enabled_p ()) + emit_insn (gen_aut_nop ()); + if (!really_return) return; @@ -32941,6 +32983,15 @@ arm_fusion_enabled_p (tune_params::fuse_ops op) return current_tune->fusible_ops & op; } +/* Return TRUE if return address signing mechanism is enabled. */ +bool +arm_current_function_pac_enabled_p (void) +{ + return (aarch_ra_sign_scope == AARCH_FUNCTION_ALL + || (aarch_ra_sign_scope == AARCH_FUNCTION_NON_LEAF + && !crtl->is_leaf)); +} + /* Implement TARGET_SCHED_CAN_SPECULATE_INSN. Return true if INSN can be scheduled for speculative execution. Reject the long-running division and square-root instructions. */ diff --git a/gcc/config/arm/arm.h b/gcc/config/arm/arm.h index 3495ab857ea..e33425bbf42 100644 --- a/gcc/config/arm/arm.h +++ b/gcc/config/arm/arm.h @@ -510,6 +510,10 @@ extern int arm_arch8_3; /* Nonzero if this chip supports the ARM Architecture 8.4 extensions. */ extern int arm_arch8_4; +/* Nonzero if this chip supports the ARM Architecture 8-M Mainline + extensions. */ +extern int arm_arch8m_main; + /* Nonzero if this chip supports the ARM Architecture 8.1-M Mainline extensions. */ extern int arm_arch8_1m_main; diff --git a/gcc/config/arm/arm.md b/gcc/config/arm/arm.md index 60468f6182c..7255fa98f5d 100644 --- a/gcc/config/arm/arm.md +++ b/gcc/config/arm/arm.md @@ -12890,6 +12890,29 @@ (define_insn "*speculation_barrier_insn" (set_attr "length" "8")] ) +(define_insn "pac_nop" + [(set (reg:SI IP_REGNUM) + (unspec:SI [(reg:SI SP_REGNUM) (reg:SI LR_REGNUM)] + UNSPEC_PAC_NOP))] + "arm_arch8m_main" + "pac\t%|ip, %|lr, %|sp" + [(set_attr "conds" "unconditional")]) + +(define_insn "pacbti_nop" + [(set (reg:SI IP_REGNUM) + (unspec:SI [(reg:SI SP_REGNUM) (reg:SI LR_REGNUM)] + UNSPEC_PACBTI_NOP))] + "arm_arch8m_main" + "pacbti\t%|ip, %|lr, %|sp" + [(set_attr "conds" "unconditional")]) + +(define_insn "aut_nop" + [(unspec:SI [(reg:SI IP_REGNUM) (reg:SI SP_REGNUM) (reg:SI LR_REGNUM)] + UNSPEC_AUT_NOP)] + "arm_arch8m_main" + "aut\t%|ip, %|lr, %|sp" + [(set_attr "conds" "unconditional")]) + ;; Vector bits common to IWMMXT, Neon and MVE (include "vec-common.md") ;; Load the Intel Wireless Multimedia Extension patterns diff --git a/gcc/config/arm/unspecs.md b/gcc/config/arm/unspecs.md index 7748e784379..dbe243a03f6 100644 --- a/gcc/config/arm/unspecs.md +++ b/gcc/config/arm/unspecs.md @@ -159,6 +159,9 @@ (define_c_enum "unspec" [ UNSPEC_VCDE ; Custom Datapath Extension instruction. UNSPEC_VCDEA ; Custom Datapath Extension instruction. UNSPEC_DLS ; Used for DLS (Do Loop Start), Armv8.1-M Mainline instruction + UNSPEC_PAC_NOP ; Represents PAC signing LR + UNSPEC_PACBTI_NOP ; Represents PAC signing LR + valid landing pad + UNSPEC_AUT_NOP ; Represents PAC verifying LR ]) diff --git a/gcc/testsuite/gcc.target/arm/pac-1.c b/gcc/testsuite/gcc.target/arm/pac-1.c new file mode 100644 index 00000000000..6cd64dbd014 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-1.c @@ -0,0 +1,12 @@ +/* Testing return address signing. */ +/* { dg-do run } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-require-effective-target arm_pacbti_hw } */ +/* { dg-options "-march=armv8.1-m.main+pacbti+fp -mbranch-protection=pac-ret+leaf -mthumb -mfloat-abi=hard --save-temps -O0" } */ + +#include "pac.h" + +/* { dg-final { scan-assembler-times "pac\tip, lr, sp" 2 } } */ +/* { dg-final { scan-assembler-times "aut\tip, lr, sp" 2 } } */ +/* { dg-final { scan-assembler-not "\tbti" } } */ + diff --git a/gcc/testsuite/gcc.target/arm/pac-2.c b/gcc/testsuite/gcc.target/arm/pac-2.c new file mode 100644 index 00000000000..945ce938592 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-2.c @@ -0,0 +1,11 @@ +/* Testing return address signing. */ +/* { dg-do run } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-require-effective-target arm_pacbti_hw } */ +/* { dg-options "-march=armv8.1-m.main+pacbti+fp -mbranch-protection=pac-ret -mthumb -mfloat-abi=hard --save-temps -O0" } */ + +#include "pac.h" + +/* { dg-final { scan-assembler "pac\tip, lr, sp" } } */ +/* { dg-final { scan-assembler "aut\tip, lr, sp" } } */ +/* { dg-final { scan-assembler-not "\tbti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/pac-3.c b/gcc/testsuite/gcc.target/arm/pac-3.c new file mode 100644 index 00000000000..47e290a5840 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-3.c @@ -0,0 +1,11 @@ +/* Testing return address signing. */ +/* { dg-do run } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-require-effective-target arm_pacbti_hw } */ +/* { dg-options "-march=armv8.1-m.main+pacbti+fp -mbranch-protection=bti+pac-ret+leaf -mthumb -mfloat-abi=hard --save-temps -O2" } */ + +#include "pac.h" + +/* { dg-final { scan-assembler-times "pacbti\tip, lr, sp" 2 } } */ +/* { dg-final { scan-assembler-times "aut\tip, lr, sp" 2 } } */ +/* { dg-final { scan-assembler-not "\tbti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/pac-4.c b/gcc/testsuite/gcc.target/arm/pac-4.c new file mode 100644 index 00000000000..cf915cdba50 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-4.c @@ -0,0 +1,10 @@ +/* Testing return address signing. */ +/* { dg-do compile } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-options "-march=armv8.1-m.main+pacbti+fp -mthumb -mfloat-abi=hard --save-temps -O2" } */ + +#include "pac.h" + +/* { dg-final { scan-assembler-not "\tbti\t" } } */ +/* { dg-final { scan-assembler-not "\tpac\t" } } */ +/* { dg-final { scan-assembler-not "\tpacbti\t" } } */ diff --git a/gcc/testsuite/gcc.target/arm/pac-5.c b/gcc/testsuite/gcc.target/arm/pac-5.c new file mode 100644 index 00000000000..c70087eb6b6 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-5.c @@ -0,0 +1,28 @@ +/* Testing return address signing. */ +/* { dg-do run } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-require-effective-target arm_pacbti_hw } */ +/* { dg-options "-march=armv8.1-m.main+pacbti+fp -mbranch-protection=pac-ret+leaf -mthumb -mfloat-abi=hard --save-temps -O0" } */ + +#include + +int +__attribute__((noinline)) +foo1 (int a, int b) +{ + int square (int z) { return z * z; } + return square (a) + square (b); +} + +int +main (void) +{ + if (foo1 (1, 2) != 5) + abort (); + + return 0; +} + +/* { dg-final { scan-assembler-times "pac\tip, lr, sp" 3 } } */ +/* { dg-final { scan-assembler-times "aut\tip, lr, sp" 3 } } */ +/* { dg-final { scan-assembler-not "\tbti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/pac-6.c b/gcc/testsuite/gcc.target/arm/pac-6.c new file mode 100644 index 00000000000..c5329f0ef48 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-6.c @@ -0,0 +1,18 @@ +/* Check that GCC does .save and .cfi_offset directives with RA_AUTH_CODE pseudo hard-register. */ +/* { dg-do compile } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-options "-march=armv8.1-m.main+fp -mbranch-protection=pac-ret+leaf -mthumb --save-temps -O0 -g" } */ + +int i; + +void foo (int); + +int bar() +{ + foo (i); + return 0; +} + +/* { dg-final { scan-assembler "pac\tip, lr, sp" } } */ +/* { dg-final { scan-assembler "aut\tip, lr, sp" } } */ +/* { dg-final { scan-assembler-not "bti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/pac-7.c b/gcc/testsuite/gcc.target/arm/pac-7.c new file mode 100644 index 00000000000..cdaebca5cfa --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-7.c @@ -0,0 +1,32 @@ +/* Testing return address signing. */ +/* { dg-do run } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-require-effective-target arm_pacbti_hw } */ +/* { dg-options "-march=armv8.1-m.main+pacbti+fp -mbranch-protection=pac-ret+leaf -mthumb -mfloat-abi=hard --save-temps -O0" } */ + +#include + +int +__attribute__((noinline)) +foo1 (int a, int b) +{ + int x = 4; + int foo2 (int a, int b) + { + return a + b + x; + } + return foo2 (a, b); +} + +int +main (void) +{ + if (foo1 (1, 2) != 7) + abort (); + + return 0; +} + +/* { dg-final { scan-assembler-times "pac\tip, lr, sp" 3 } } */ +/* { dg-final { scan-assembler-times "aut\tip, lr, sp" 3 } } */ +/* { dg-final { scan-assembler-not "\tbti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/pac-8.c b/gcc/testsuite/gcc.target/arm/pac-8.c new file mode 100644 index 00000000000..3f37dcfa5c8 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac-8.c @@ -0,0 +1,34 @@ +/* Testing return address signing. */ +/* { dg-do run } */ +/* { dg-require-effective-target mbranch_protection_ok } */ +/* { dg-require-effective-target arm_pacbti_hw } */ +/* { dg-options "-march=armv8.1-m.main+pacbti+fp -mbranch-protection=pac-ret+leaf -mthumb -mfloat-abi=hard --save-temps -O0" } */ + +#include +#include + +int acc (int n, ...) +{ + int sum = 0; + va_list ptr; + + va_start (ptr, n); + + for (int i = 0; i < n; i++) + sum += va_arg (ptr, int); + va_end (ptr); + + return sum; +} + +int main() +{ + if (acc (3, 1, 2, 3) != 6) + abort (); + + return 0; +} + +/* { dg-final { scan-assembler-times "pac\tip, lr, sp" 2 } } */ +/* { dg-final { scan-assembler-times "aut\tip, lr, sp" 2 } } */ +/* { dg-final { scan-assembler-not "\tbti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/pac.h b/gcc/testsuite/gcc.target/arm/pac.h new file mode 100644 index 00000000000..7355e6b2954 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/pac.h @@ -0,0 +1,17 @@ +#include + +int +__attribute__((noinline)) +foo1 (int a, int b) +{ + return a + b; +} + +int +main (void) +{ + if (foo1 (1, 2) != 3) + abort (); + + return 0; +}