From patchwork Wed Sep 14 14:20:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Corallo X-Patchwork-Id: 57629 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 8E0B73856DC2 for ; Wed, 14 Sep 2022 14:21:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E0B73856DC2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663165278; bh=B4KZADmJwkB6BD3ZnxnCJvHb362RU4hZDTlR+zQ5qpQ=; h=To:Subject:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=vQwh8pVm9DjeNq+sDqanJ8pijl4ZselLoriGRwVdgUvCNyVxkfwP0vOZ/BZyV6wLW /f7VaiwCv3eZTg0s/av+bac9wX7Uf0f+7AsgD2P7RXKfu6+Xu0FkUFPaLwBD8TT7Hq 5Km/srfn9m8GwrVfYZBBBnrus7+2aOUJCV9MCOHk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR02-AM5-obe.outbound.protection.outlook.com (mail-eopbgr00052.outbound.protection.outlook.com [40.107.0.52]) by sourceware.org (Postfix) with ESMTPS id 028AC385841D for ; Wed, 14 Sep 2022 14:20:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 028AC385841D ARC-Seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=AQCKXk+mjmJ8Gjopfgl3nn/x0dEW0UsFrbVP272Zljuze5qsD9XPvV/G7fZlOEL9A8IurVO8Vcukz0WP7pR5Xw3fA+uNGCfG/7hJA/3pkVj+YuAvrTCa7hpmCU9hFJPVU5c9pRdXr19OT+Y1od1wcIg/41zcldKCf/ISwJuX99ryrZOUjzMFOXxwbP8hL7/T+Qmt9PyDTTwvcyU3vVWGoUzLYOseW7OWO8o7jydQ/IZc1aEiSnQCTwXXO+4jaTT7vB/YKNiObmMWRkeFYHZaMM5yKAtdHdUagTWfivQ/9gHgFgxxP6Vr4CjWo8xb37NSbnk491x5cLD9femZsst8pg== 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=B4KZADmJwkB6BD3ZnxnCJvHb362RU4hZDTlR+zQ5qpQ=; b=DyvPsrkfOs3jd5aiG3E4jlu4aMm9Zxv1dG2V7Vj+hyK5q/Xz+rz0hoP+9la7dfFMKqpQADFj5kyK9yT/04yU9dQoxlnM8/ypLBWMp1k0hjoLZt6u4g5Q1IJTHRlA5DoJBnoHS+F0yTFPiEXHMrikfG+vHxwrVJOgrq+U8simjwgwC52YotZ8E57Ma0VDDSWyRhh9m/q60A4dpoaYJuCQpt8dhEaNjUoR37xLldC/tZr8EeLl5aBy4Cx/sa7FNvkKSnsR3Zmh0qOaBo/n5wAEW7j1FWO9fyIN69UualnzoLFDNvpr/y0I5517bfLuX5ksfyXTipmWz0MD7sQ6lTgsEQ== 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 AM5PR0502CA0024.eurprd05.prod.outlook.com (2603:10a6:203:91::34) by AS2PR08MB9475.eurprd08.prod.outlook.com (2603:10a6:20b:5e8::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5612.12; Wed, 14 Sep 2022 14:20:41 +0000 Received: from VE1EUR03FT038.eop-EUR03.prod.protection.outlook.com (2603:10a6:203:91:cafe::5a) by AM5PR0502CA0024.outlook.office365.com (2603:10a6:203:91::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.12 via Frontend Transport; Wed, 14 Sep 2022 14:20:41 +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 VE1EUR03FT038.mail.protection.outlook.com (10.152.19.112) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.12 via Frontend Transport; Wed, 14 Sep 2022 14:20:40 +0000 Received: ("Tessian outbound 9236804a5e9b:v124"); Wed, 14 Sep 2022 14:20:40 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 4261c4040b93e3bd X-CR-MTA-TID: 64aa7808 Received: from 875e19226993.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id D1C3864D-C314-4A59-9451-9C5DC1F18311.1; Wed, 14 Sep 2022 14:20:33 +0000 Received: from EUR04-DB3-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 875e19226993.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 14 Sep 2022 14:20:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I14hjIRLZQZG7TSgeRcsAiNaNUS8lxKk38P2umkhQaXMdNq52Tjvri3JpJstrLiJcG2eYYlowN+q3neIrhpMI7QJoilZ0E4iPEsYcK4V6el24EeTNWk27jiO63xKIhgFiJgA6bIzjNltmACwgm2uQ3bfp7j09lVyGcqzsBibjJN5K3yqPLfKkiBckfGhocTobP1T9YUsAsg0/Nkdyd2iWFkVsSyIdlZPFeX/Imj1nmdPbR6it7CkRYInhqwbwn36S9JHtGe85a9WtJoHTjwc1dpqQQLuMBqdseMca58MJPWW93paWyjtExhcJIMW0XcwDEKv1YcCa3N2xATVX8h53Q== 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=B4KZADmJwkB6BD3ZnxnCJvHb362RU4hZDTlR+zQ5qpQ=; b=OkTHr6SIdoDttXOXik7XA24lWsFMyA3cb6u/Jreb3xXRupF2ae6GegIPQOS4FPZx+T/B4QoaxQaTomz2BOlqf2zelXwY7jfilONXBk01lgHy/6VASuiyn6flboiSwjOITHmE9TIodKUPOxtPs/wcKNTbh8l8ggGDtRMTyCmNl+mDQeU6PXAY6mftwEJ1jZWka81BfeM8aVxiu53dWRGW2e6CjhJnlg1xTfHzWiDenZvZHB2qk/h7wAvjfCyj8BC+ADhED82tVSq1L6zIAbIgZVhOTzXHtw1kzN3bR5Q8ZNM+NS4yDNMnjXBeQr3sDJ3pp7HurXF/sNV6i8R/E5n/BQ== 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 AS9PR07CA0038.eurprd07.prod.outlook.com (2603:10a6:20b:46b::22) by AS2PR08MB9836.eurprd08.prod.outlook.com (2603:10a6:20b:603::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5612.12; Wed, 14 Sep 2022 14:20:28 +0000 Received: from VE1EUR03FT023.eop-EUR03.prod.protection.outlook.com (2603:10a6:20b:46b:cafe::bd) by AS9PR07CA0038.outlook.office365.com (2603:10a6:20b:46b::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.12 via Frontend Transport; Wed, 14 Sep 2022 14:20:28 +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 VE1EUR03FT023.mail.protection.outlook.com (10.152.18.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5632.12 via Frontend Transport; Wed, 14 Sep 2022 14:20:28 +0000 Received: from AZ-NEU-EX04.Arm.com (10.251.24.32) by AZ-NEU-EX04.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.9; Wed, 14 Sep 2022 14:20:27 +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.9 via Frontend Transport; Wed, 14 Sep 2022 14:20:27 +0000 To: Andrea Corallo via Gcc-patches Subject: [PATCH 10/15 V2] arm: Implement cortex-M return signing address codegen References: Date: Wed, 14 Sep 2022 16:20:26 +0200 In-Reply-To: (Andrea Corallo via Gcc-patches's message of "Fri, 12 Aug 2022 17:36:17 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1.50 (gnu/linux) MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: VE1EUR03FT023:EE_|AS2PR08MB9836:EE_|VE1EUR03FT038:EE_|AS2PR08MB9475:EE_ X-MS-Office365-Filtering-Correlation-Id: bbcbd11d-c291-43ce-3923-08da965c4db2 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: 94WZqt3yvddgx8pYRE4f1KaCKh7AUpYHGLgPrG/3JFzxmGaWL+uc0ogAs3i//nbBme5HvrQ4c6YUKU6YjRDmlkICLv6pmbkBsXfF7MvaB/uowein1iOg5rzPF1UdJnI5eQr44ptE6TnKPvSRidMQEjNj29RreMOJmZ+Zz+Tr0P/dx4sUuI6CqQ6wS8SupiB7O3U6x9G75I5L4IfsHofXm/5EbeNBw/NNBb6Uxpj0EMwLRGoOuhqrGKW1nmNk72xp1cEXgKBONwJdcr1K1fbKI54oLH7uvlKkAYP+QG8BiSr9rvATrz2bdiglpRzkiHXsiBE5D5rKfFkabhl03Jn8fezYQPBVLkHNWwC4y682VHC9PrPsstwTvKndasYTatTILN1vT4R5BK0pnL6prNw5IGuuhoWgYaDVMnnfrDGHdA7zyjsp1hiCUOZUp+/qD4Fdyy2bbyCfcPyt8jOYR6n+LbgPiGjVWucbBSLJV6lCJbxaAlHpuYK8B2kpi11CF8J4Alns+XPYX8jizF77gX4xCi9i04olDb95H+EN9FZbTVgViWSGlgcf9d0rFQprdBfMalK0cKG/5fngT5okULj8ExHllEKpR63fZpe5JN+MnWkKMNIPmnT1oSR/QYQ76ToHdSnn0/gGYGhjIs3PfjLYOQIFJBpWamR5QKwM1rWiUCBOjKGsG0J/JdbA7onyZop4Bn0j2/OI9NvACuP2YXnqa8oLPaCK716ICnrf4Z4B/mtxEYDvyKgJ1Nzs/eFGDLWVfPv+7Nc+EkmbzGarvt4hlDhEHp9vSkzSwDDVDASnwleO9pRWLnWffm3f9xJ2k/5+wTnDss40b5gKWubP0ULjosDjn5TQHGZJ5hJ3egHatRh/0HiGLSda7AZGQgu3QHSThBp4lRjGsBi+SowNRhvL3Gy+Zrk1Qz2bo6CS1gfW7dU= 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)(136003)(346002)(376002)(39860400002)(396003)(451199015)(36840700001)(40470700004)(46966006)(84970400001)(40480700001)(54906003)(6916009)(86362001)(4326008)(44832011)(5660300002)(235185007)(336012)(70586007)(426003)(41300700001)(36756003)(2906002)(82740400003)(82310400005)(26005)(70206006)(40460700003)(478600001)(36860700001)(316002)(81166007)(8936002)(47076005)(83380400001)(356005)(33964004)(8676002)(2616005)(186003)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR08MB9836 X-MS-Exchange-Transport-CrossTenantHeadersStripped: VE1EUR03FT038.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 42ab443c-390e-43b0-89da-08da965c464f X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yt8MvXVT4dmIug89ml+oigHIpdK22AamiVaorvdq67s78GiTf0dGszGBu49kA8NGHGIdyhZemBXEkpF1mOmA0c1i6AmdWmyky0XUPu1drWQ/DnP8D1zuITm1WypJ8WUF0EgE3+IDTGG/6pkxBlH7xH/CXsb1IfKmUS1THexFh+8PbIlmV+PE6izvEI907NsYPcU+RHzbqPhlPh++MI3pMYs358jZLnEd9/QH5UbjTbObsB3Ykkh3H3AUbzciYzWqn1bn6295fTfhdEBGbHyx/EobIyvNgAJA20nau4DKr/rRrlHF2bu61snsWNx2NBNMyHoRo1Tkg21ivVOuS11s3KuwU9qA4oP8U2zHtwJ4BzwIc/hv5rqZoHDCsS2BKDQRak2MtVqy+hQBKpM2aL4Crb1PZpNzy8RqkE+aKlVZdJACYJBQGHTjs6mTLwVBDy+4bu/hS7dW1VRon4FDLupiyp42sciNY1RTygMVuF5SYNZFwWi+POvy5VSLFEMmKoBRvEpROQ16XsIePP2/oKZghEwRm3YhoTmVSfwrQ3GGrrbuXu0itunIi098/7aJRDXIDSi0DtoMZdj1QP5bBV1ZkXN43hmMi3zAiIHIirDHDEpfWcwQtVbixZmvFPlkNMw+pLlHZBvETpW+zjqqT5sGPr7YjM9POW00vYa6eM0EHUzs7Rk3AlYP4P7qu6P//r+dOS43gyyyk65CyLmFkJ//IE9cR6+Nemgi5ELtmQmyWa6Bpm0Es91xV53qMt3rxrr07Y2G7yhz+H6qa3f3uk7mcPqHrEc28+kvR6aTLUq67l+Dt2jnx/AU0lw6waff5ghdKY47lvmSTx3TIQS6XsJ4yQ== 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)(39860400002)(376002)(346002)(396003)(136003)(451199015)(46966006)(40470700004)(36840700001)(82740400003)(84970400001)(70586007)(186003)(235185007)(41300700001)(36860700001)(5660300002)(336012)(33964004)(26005)(36756003)(40480700001)(44832011)(47076005)(316002)(81166007)(70206006)(86362001)(82310400005)(478600001)(8676002)(4326008)(426003)(83380400001)(8936002)(2906002)(2616005)(40460700003)(54906003)(6916009); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Sep 2022 14:20:40.9467 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bbcbd11d-c291-43ce-3923-08da965c4db2 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: VE1EUR03FT038.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR08MB9475 X-Spam-Status: No, score=-12.0 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, T_SCC_BODY_TEXT_LINE, 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 Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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.c: (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. 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..0ba9c0b9960 100644 --- a/gcc/config/arm/arm.cc +++ b/gcc/config/arm/arm.cc @@ -3209,6 +3209,9 @@ arm_option_override_internal (struct gcc_options *opts, arm_stack_protector_guard_offset = offs; } + if (arm_current_function_pac_enabled_p () && !(arm_arch7 && arm_arch_cmse)) + error ("This architecture does not support branch protection instructions"); + #ifdef SUBTARGET_OVERRIDE_INTERNAL_OPTIONS SUBTARGET_OVERRIDE_INTERNAL_OPTIONS; #endif @@ -21139,6 +21142,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 +23368,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 +23452,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 +23483,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 +23535,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 +27331,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 +27346,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 +27765,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 +27880,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 +32977,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.md b/gcc/config/arm/arm.md index 60468f6182c..92269a7819a 100644 --- a/gcc/config/arm/arm.md +++ b/gcc/config/arm/arm.md @@ -11518,7 +11518,7 @@ (define_expand "prologue" arm_expand_prologue (); else thumb1_expand_prologue (); - DONE; + DONE; " ) @@ -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_arch7 && arm_arch_cmse" + "pac\t%|ip, %|lr, %|sp" + [(set_attr "length" "4")]) + +(define_insn "pacbti_nop" + [(set (reg:SI IP_REGNUM) + (unspec:SI [(reg:SI SP_REGNUM) (reg:SI LR_REGNUM)] + UNSPEC_PACBTI_NOP))] + "arm_arch7 && arm_arch_cmse" + "pacbti\t%|ip, %|lr, %|sp" + [(set_attr "length" "4")]) + +(define_insn "aut_nop" + [(unspec:SI [(reg:SI IP_REGNUM) (reg:SI SP_REGNUM) (reg:SI LR_REGNUM)] + UNSPEC_AUT_NOP)] + "arm_arch7 && arm_arch_cmse" + "aut\t%|ip, %|lr, %|sp" + [(set_attr "length" "4")]) + ;; 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; +}