From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR04-DB3-obe.outbound.protection.outlook.com (mail-db3eur04on2072.outbound.protection.outlook.com [40.107.6.72]) by sourceware.org (Postfix) with ESMTPS id 5369C3858D1E for ; Thu, 22 Dec 2022 17:14:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5369C3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QXfTrNb6S0n+Vj1PA5ewrs1zdeLlmpZsbVJeJUQ53oc=; b=KNSdonEiSwvOub19OJOGGnwklNEI1m5gl4dNbQ3FZiGRZU3z1g98O0MfYBZMEJd05+8K+pp/Wlweiw+MVRFF53qgPdChbRPS0gDGQvo1aTXQBbZWKHHPQOO+XlN7cm8TRGZqzqzG6MisxU28QFAS9PLxFb61Q9r9JvBYT+NrREw= Received: from DB8PR09CA0025.eurprd09.prod.outlook.com (2603:10a6:10:a0::38) by AS8PR08MB6742.eurprd08.prod.outlook.com (2603:10a6:20b:351::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.6; Thu, 22 Dec 2022 17:14:09 +0000 Received: from DBAEUR03FT013.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:a0:cafe::df) by DB8PR09CA0025.outlook.office365.com (2603:10a6:10:a0::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.11 via Frontend Transport; Thu, 22 Dec 2022 17:14:08 +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 DBAEUR03FT013.mail.protection.outlook.com (100.127.142.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.13 via Frontend Transport; Thu, 22 Dec 2022 17:14:08 +0000 Received: ("Tessian outbound baf1b7a96f25:v132"); Thu, 22 Dec 2022 17:14:08 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 139f63c568ced29f X-CR-MTA-TID: 64aa7808 Received: from 13ef9384a800.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id D58D1678-5CE6-4C03-935C-82A82C252679.1; Thu, 22 Dec 2022 17:14:02 +0000 Received: from EUR04-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 13ef9384a800.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 22 Dec 2022 17:14:02 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VTq8/aDgnSbFQyJ5esQ0V7w25dNGbrVkp17ox4RYYWRcOiydJKsxMVRPFXV3ymUq/KbRByOfwsSAh32DtU2Bfa54+ByU5SOwlhJUPDzNwfSoN0G05JfGZDZnyLywzRRhnx6WzdDil9jXFd0rJLGWWoBGmdEIlxZ842ZsrP8Uqa8zldl70izWBVzTbmzCwvzk/Wqq3elWqhbJFoP70iyn+N8NuOr8HQ1GnK3FTRtsLbc/wA5EEXnjP3zRoJduD46r4uWVU6x8QhyS7nffWJIjj9npwn8pYBeVV/pWAhaMn2+oPh2Ahoo8MTKmZvTB5ovZGSDLdBRb8jn22hv1z30/6A== 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=QXfTrNb6S0n+Vj1PA5ewrs1zdeLlmpZsbVJeJUQ53oc=; b=kQGWReztQjVkgJX6OUBhkeHRozVXpQr9tajmmunIcWD0NhpQ8nUjXTUcVjsVXHMrgizXK/JjBdG7tv3Tx9hHhwJYPBKawHq3VmbUU48aO3hNkcisrWPzXZU64dkaaVSVJUhG5VFzULyeenz43SlOjeV8h5io/iweCtjXZGHuG1Gi7R3O+iNb02aiQykqGzhQwBDIK8R9+shqNeshgMlWYDoSXId95Rg7RCuen9to7D6dZ6vETmqHaIrDrU1FeBMQPyM6C51oGYB4PtG0e6K8l/iv8KLaEi5pfTmzu/jeTD8rm9gRH8Hin8thROhcAIP0OtEUElqUOeH3wlgsq0/0Xw== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QXfTrNb6S0n+Vj1PA5ewrs1zdeLlmpZsbVJeJUQ53oc=; b=KNSdonEiSwvOub19OJOGGnwklNEI1m5gl4dNbQ3FZiGRZU3z1g98O0MfYBZMEJd05+8K+pp/Wlweiw+MVRFF53qgPdChbRPS0gDGQvo1aTXQBbZWKHHPQOO+XlN7cm8TRGZqzqzG6MisxU28QFAS9PLxFb61Q9r9JvBYT+NrREw= Received: from DU2PR04CA0299.eurprd04.prod.outlook.com (2603:10a6:10:28c::34) by PA4PR08MB7618.eurprd08.prod.outlook.com (2603:10a6:102:273::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.12; Thu, 22 Dec 2022 17:14:00 +0000 Received: from DBAEUR03FT026.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:28c:cafe::60) by DU2PR04CA0299.outlook.office365.com (2603:10a6:10:28c::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.21 via Frontend Transport; Thu, 22 Dec 2022 17:14:00 +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 DBAEUR03FT026.mail.protection.outlook.com (100.127.142.242) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5944.13 via Frontend Transport; Thu, 22 Dec 2022 17:14:00 +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.16; Thu, 22 Dec 2022 17:13:59 +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.16 via Frontend Transport; Thu, 22 Dec 2022 17:13:59 +0000 From: Andrea Corallo To: Richard Earnshaw CC: Kyrylo Tkachov , Richard Earnshaw , nd , Andrea Corallo via Gcc-patches Subject: [PATCH 12/15 V5] arm: implement bti injection References: <30990d91-0c32-a3b0-7954-846bf1eddc8d@foss.arm.com> <7db3982c-cd42-023f-0dd1-0eb7b7dbfb20@foss.arm.com> Date: Thu, 22 Dec 2022 18:13:58 +0100 In-Reply-To: (Richard Earnshaw's message of "Wed, 14 Dec 2022 17:03:19 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2.50 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: DBAEUR03FT026:EE_|PA4PR08MB7618:EE_|DBAEUR03FT013:EE_|AS8PR08MB6742:EE_ X-MS-Office365-Filtering-Correlation-Id: 3466a6b5-6135-4e2a-7971-08dae43ff020 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: GLtafjyu5ddsR0tCu9+oplRB5w4gTuSrbfDBA8bdR0GQItDWQjIhBH4saYD/1ztczNSZBmLVkQBWn9AdagLnRbg/QWTzYKJfiMJHJGxKC43AYeX2qCX/Pem2X5EojMQe8oMNyf4LhX/5l2emfKTUo1GebUNXomcpYdEihjyeTlXbbkA5b4UKBuctCWUU2ZNxg3vk4u1oiYkIoKksBhtvHr0k/3mZiqsDPopCpdkPAlGVjPitQ+fVr3eZDHGvtgmsuyD40mrv3OSSMrTk+EzEEibkQ4RTbw8T7Gi/j/LAQ1o3OzJ2MZFj2rWutrhGs87dpuNzYPHOkNH7UPhqKHyqV1HATxwtN8nyaG4aREcE48/PnqkDk3tpbEIygpx5KDbV3yEnwF38zTf8ePZFdPNB5E9lpbblJZttOrxy08Nk3t5kTmYDzQetIeDV46Wv1jyUuv4aQv3mQNiFRmDalFDPoJtrKtp3c11hM47qJiCF2NyFcpJiO92CEWFesnRLNHntImiF/tGYn8y37LlAxwNV2nr3qG7+8Gb28nENYcgRm714niS4SgpGMmM9FYYk5ApbJuJvWtxPAPqOhAdpuOTYiWGVg29ctS1XFrit7TKn7ziL4EE2P1s6ZlMuRiCyrZQUkYBdiGO5CsYBEr0+mQuZk/qrlUiSDuq26qm+LRHOFY/QIbizJI77ge/0mg0ttJBd9xK3cAjTqD1UFPaan7FZdg== 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)(376002)(346002)(396003)(39860400002)(136003)(451199015)(46966006)(36840700001)(40480700001)(41300700001)(82740400003)(81166007)(356005)(186003)(53546011)(26005)(70206006)(33964004)(70586007)(54906003)(316002)(478600001)(2906002)(86362001)(36860700001)(4326008)(426003)(336012)(36756003)(2616005)(8676002)(82310400005)(47076005)(83380400001)(235185007)(5660300002)(44832011)(8936002)(6862004)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR08MB7618 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DBAEUR03FT013.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 85ca50e0-1c36-41c0-d237-08dae43feb31 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0KTX++CcJMpLBuaKqAk5Kd8sWGNKxlJ+q0J+upo2xIPQIW/U4F8++XwZ0Pcg1QEFDvNYKVOH+1hUHuY0bVkXoZ3Qgw9WkyZyMkSbbEGe+rKI17ELdjHGsCRWueO1efnezl5Cxik/i7phh9ucgH4oASj8uCOFo5tuMpIm09j4Wz9CGRnEWcZUrxY0rhGwm7WIwswbEkJO3m6mXKEdSptWyZ/0zNZ4icazd+v6LbtnaV7oAqeCUBuqqZuaAbajEX/pZ1wKCu7Pr9+fsAHeCwqQnI3+lD07T9vBaPei8ce/ohuAxTWw4AkJX0bTOjxmfVFzCEtvsVu5bnnEIK9LIVGJf1TrblAM5ZoaybBJpmPBIRQOaWOEdJXt9AdktUSr5pqHFfRB/gw5x8jLSJ5NKbc9dIWNTmgyf2A6yLC2MrpIRr66e2kBJEeBBgiusMMmVvcdQSjHW/gtsnpHc3KcB4PoDYTdPVe8eYJ0SSvg0R1C9wu/ZosC3LvLyjIYeAYAtboRmNGZRuBw4+DO8h/oPfAB+HtZA8DyVlfqGmMMhoeZOJyjy61Jtw7N97RDtayVberBwGEHXc1Re0WeNrW0xAGpGah+8+5uqppcm1P+SGR8q4tqX3CX6Xm7NCZtmD+XRYO5qK/foK6Om9pZQuODH1qjjfCa6yQgWTvvsoDPMl/ggP5CtvxRj2UO6hFZmY5aFE9Ro6lSbFSUyvHa8XWN2ncCnw== 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)(376002)(396003)(346002)(136003)(39860400002)(451199015)(36840700001)(46966006)(40470700004)(4326008)(8676002)(53546011)(6862004)(8936002)(70206006)(70586007)(5660300002)(235185007)(41300700001)(33964004)(54906003)(82310400005)(316002)(2906002)(40460700003)(336012)(44832011)(36756003)(86362001)(478600001)(186003)(82740400003)(81166007)(26005)(40480700001)(36860700001)(2616005)(83380400001)(426003)(47076005);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Dec 2022 17:14:08.8922 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3466a6b5-6135-4e2a-7971-08dae43ff020 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: DBAEUR03FT013.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB6742 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00,BODY_8BITS,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 List-Id: --=-=-= Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Richard Earnshaw writes: > On 14/12/2022 17:00, Richard Earnshaw via Gcc-patches wrote: >> On 14/12/2022 16:40, Andrea Corallo via Gcc-patches wrote: >>> Hi Richard, >>> >>> thanks for reviewing. >>> >>> Richard Earnshaw writes: >>> >>>> On 28/10/2022 17:40, Andrea Corallo via Gcc-patches wrote: >>>>> Hi all, >>>>> please find attached the third iteration of this patch addresing >>>>> review >>>>> comments. >>>>> Thanks >>>>> =C2=A0=C2=A0=C2=A0 Andrea >>>>> >>>> >>>> @@ -23374,12 +23374,6 @@ output_probe_stack_range (rtx reg1, rtx reg2) >>>> =C2=A0=C2=A0=C2=A0 return ""; >>>> =C2=A0 } >>>> >>>> -static bool >>>> -aarch_bti_enabled () >>>> -{ >>>> -=C2=A0 return false; >>>> -} >>>> - >>>> =C2=A0 /* Generate the prologue instructions for entry into an ARM or = Thumb-2 >>>> =C2=A0=C2=A0=C2=A0=C2=A0 function.=C2=A0 */ >>>> =C2=A0 void >>>> @@ -32992,6 +32986,61 @@ arm_current_function_pac_enabled_p (void) >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 && !crtl->is_leaf)); >>>> =C2=A0 } >>>> >>>> +/* Return TRUE if Branch Target Identification Mechanism is >>>> enabled.=C2=A0 */ >>>> +bool >>>> +aarch_bti_enabled (void) >>>> +{ >>>> +=C2=A0 return aarch_enable_bti =3D=3D 1; >>>> +} >>>> >>>> See comment in earlier patch about the location of this function >>>> moving.=C2=A0=C2=A0 Can aarch_enable_bti take values other than 0 and = 1? >>> >>> Yes default is 2. >> It shouldn't be by this point, because, hopefully you've gone >> through the equivalent of this hunk (from aarch64) somewhere in >> arm_override_options: >> =C2=A0=C2=A0 if (aarch_enable_bti =3D=3D 2) >> =C2=A0=C2=A0=C2=A0=C2=A0 { >> =C2=A0#ifdef TARGET_ENABLE_BTI >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 aarch_enable_bti =3D 1; >> =C2=A0#else >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 aarch_enable_bti =3D 0; >> =C2=A0#endif >> =C2=A0=C2=A0=C2=A0=C2=A0 } >> And after this point the '2' should never be seen again.=C2=A0 We use >> this trick to permit the user to force a default that differs from >> the configuration. >> However, I don't see a hunk to do this in patch 3, so perhaps that >> needs updating to fix this. > > I've just remembered that the above is to support a configure-time > option of the compiler to enable branch protection. But perhaps we > don't want to have that in AArch32, in which case it would be better > not to have the default be 2 anyway, just default to off (0). > > R. Done in 1/15 (needs approval again now). >> >>> [...] >>> >>>> +=C2=A0 return GET_CODE (pat) =3D=3D UNSPEC_VOLATILE && XINT (pat, 1) = =3D=3D >>>> UNSPEC_BTI_NOP; >>>> >>>> I'm not sure where this crept in, but UNSPEC and UNSPEC_VOLATILE have >>>> separate enums in the backend, so UNSPEC_BIT_NOP should really be >>>> VUNSPEC_BTI_NOP and defined in the enum "unspecv". >>> >>> Done >>> >>>> +aarch_pac_insn_p (rtx x) >>>> +{ >>>> +=C2=A0 if (!x || !INSN_P (x)) >>>> +=C2=A0=C2=A0=C2=A0 return false; >>>> + >>>> +=C2=A0 rtx pat =3D PATTERN (x); >>>> + >>>> +=C2=A0 if (GET_CODE (pat) =3D=3D SET) >>>> +=C2=A0=C2=A0=C2=A0 { >>>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 rtx tmp =3D XEXP (pat, 1); >>>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (tmp >>>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 && GET_CODE (tmp) =3D=3D UNSPEC >>>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 && (XINT (tmp, 1) =3D=3D UNSPEC_PAC_NOP >>>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 || XINT (tmp, = 1) =3D=3D UNSPEC_PACBTI_NOP)) >>>> +=C2=A0=C2=A0=C2=A0 return true; >>>> +=C2=A0=C2=A0=C2=A0 } >>>> + >>>> >>>> This will also need updating (see review on earlier patch) because >>>> PACBTI needs to be unspec_volatile, while PAC doesn't. >>> >>> Done >>> >>>> +/* The following two functions are for code compatibility with aarch64 >>>> +=C2=A0=C2=A0 code, this even if in arm we have only one bti instructi= on.=C2=A0 */ >>>> + >>>> >>>> I'd just write >>>> =C2=A0 /* Target specific mapping for aarch_gen_bti_c and >>>> =C2=A0 aarch_gen_bti_j. For Arm, both of these map to a simple BTI >>>> instruction.=C2=A0 */ >>> >>> Done >>> >>>> >>>> @@ -162,6 +162,7 @@ (define_c_enum "unspec" [ >>>> =C2=A0=C2=A0=C2=A0 UNSPEC_PAC_NOP=C2=A0=C2=A0=C2=A0 ; Represents PAC s= igning LR >>>> =C2=A0=C2=A0=C2=A0 UNSPEC_PACBTI_NOP=C2=A0=C2=A0=C2=A0 ; Represents PA= C signing LR + valid landing pad >>>> =C2=A0=C2=A0=C2=A0 UNSPEC_AUT_NOP=C2=A0=C2=A0=C2=A0 ; Represents PAC v= erifying LR >>>> +=C2=A0 UNSPEC_BTI_NOP=C2=A0=C2=A0=C2=A0 ; Represent BTI >>>> =C2=A0 ]) >>>> >>>> BTI is an unspec volatile, so this should be in the "vunspec" enum and >>>> renamed accordingly (see above). >>> >>> Done. >>> >>> Please find attached the updated version of this patch. >>> >>> BR >>> >>> =C2=A0=C2=A0 Andrea >>> >> Apart from that, this is OK. >> R. Cool, attached the updated patch. Also I added some error handling not to run the bti pass if the march selected does not support bti. BR Andrea --=-=-= Content-Type: text/plain; charset="utf-8" Content-Disposition: attachment; filename="0001-PATCH-12-15-arm-implement-bti-injection.patch" >From afd54e771268733b7f1f4945c9b2cdabe1d6a6e5 Mon Sep 17 00:00:00 2001 From: Andrea Corallo Date: Thu, 7 Apr 2022 11:51:56 +0200 Subject: [PATCH] [PATCH 12/15] arm: implement bti injection Hi all, this patch enables Branch Target Identification Armv8.1-M Mechanism [1]. This is achieved by using the bti pass made common with Aarch64. The pass iterates through the instructions and adds the necessary BTI instructions at the beginning of every function and at every landing pads targeted by indirect jumps. Best Regards Andrea [1] gcc/ChangeLog 2022-04-07 Andrea Corallo * config.gcc (arm*-*-*): Add 'aarch-bti-insert.o' object. * config/arm/arm-protos.h: Update. * config/arm/aarch-common-protos.h: Declare 'aarch_bti_arch_check'. * config/arm/arm.cc (aarch_bti_enabled) Update. (aarch_bti_j_insn_p, aarch_pac_insn_p, aarch_gen_bti_c) (aarch_gen_bti_j, aarch_bti_arch_check): New functions. * config/arm/arm.md (bti_nop): New insn. * config/arm/t-arm (PASSES_EXTRA): Add 'arm-passes.def'. (aarch-bti-insert.o): New target. * config/arm/unspecs.md (VUNSPEC_BTI_NOP): New unspec. * config/arm/aarch-bti-insert.cc (rest_of_insert_bti): Verify arch compatibility. * config/arm/arm-passes.def: New file. * config/aarch64/aarch64.cc (aarch_bti_arch_check): New function. * config/arm/aarch-bti-insert.cc (gate): Make use of 'aarch_bti_arch_check'. gcc/testsuite/ChangeLog 2022-04-07 Andrea Corallo * gcc.target/arm/bti-1.c: New testcase. * gcc.target/arm/bti-2.c: Likewise. --- gcc/config.gcc | 2 +- gcc/config/aarch64/aarch64.cc | 4 ++ gcc/config/arm/aarch-bti-insert.cc | 7 +++- gcc/config/arm/aarch-common-protos.h | 1 + gcc/config/arm/arm-passes.def | 21 ++++++++++ gcc/config/arm/arm-protos.h | 2 + gcc/config/arm/arm.cc | 60 +++++++++++++++++++++++++++- gcc/config/arm/arm.md | 7 ++++ gcc/config/arm/t-arm | 10 +++++ gcc/config/arm/unspecs.md | 1 + gcc/testsuite/gcc.target/arm/bti-1.c | 12 ++++++ gcc/testsuite/gcc.target/arm/bti-2.c | 58 +++++++++++++++++++++++++++ 12 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 gcc/config/arm/arm-passes.def create mode 100644 gcc/testsuite/gcc.target/arm/bti-1.c create mode 100644 gcc/testsuite/gcc.target/arm/bti-2.c diff --git a/gcc/config.gcc b/gcc/config.gcc index 86abcd26185..f578b88dd49 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -351,7 +351,7 @@ arc*-*-*) ;; arm*-*-*) cpu_type=arm - extra_objs="arm-builtins.o arm-mve-builtins.o aarch-common.o" + extra_objs="arm-builtins.o arm-mve-builtins.o aarch-common.o aarch-bti-insert.o" extra_headers="mmintrin.h arm_neon.h arm_acle.h arm_fp16.h arm_cmse.h arm_bf16.h arm_mve_types.h arm_mve.h arm_cde.h" target_type_format_char='%' c_target_objs="arm-c.o" diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index 37c4f798abc..737bb4f0532 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -8806,6 +8806,10 @@ aarch64_return_address_signing_enabled (void) && known_ge (cfun->machine->frame.reg_offset[LR_REGNUM], 0))); } +/* Only used by the arm backend. */ +void aarch_bti_arch_check (void) +{} + /* Return TRUE if Branch Target Identification Mechanism is enabled. */ bool aarch_bti_enabled (void) diff --git a/gcc/config/arm/aarch-bti-insert.cc b/gcc/config/arm/aarch-bti-insert.cc index 2d1d2e334a9..30d5bfcb77b 100644 --- a/gcc/config/arm/aarch-bti-insert.cc +++ b/gcc/config/arm/aarch-bti-insert.cc @@ -190,7 +190,12 @@ public: /* opt_pass methods: */ virtual bool gate (function *) { - return aarch_bti_enabled (); + if (aarch_bti_enabled ()) + { + aarch_bti_arch_check (); + return true; + } + return false; } virtual unsigned int execute (function *) diff --git a/gcc/config/arm/aarch-common-protos.h b/gcc/config/arm/aarch-common-protos.h index 374982752ad..f795730a59c 100644 --- a/gcc/config/arm/aarch-common-protos.h +++ b/gcc/config/arm/aarch-common-protos.h @@ -42,6 +42,7 @@ extern int arm_no_early_alu_shift_value_dep (rtx, rtx); extern int arm_no_early_mul_dep (rtx, rtx); extern int arm_no_early_store_addr_dep (rtx, rtx); extern bool arm_rtx_shift_left_p (rtx); +extern void aarch_bti_arch_check (void); extern bool aarch_bti_enabled (void); extern bool aarch_bti_j_insn_p (rtx_insn *); extern bool aarch_pac_insn_p (rtx); diff --git a/gcc/config/arm/arm-passes.def b/gcc/config/arm/arm-passes.def new file mode 100644 index 00000000000..71d6b563640 --- /dev/null +++ b/gcc/config/arm/arm-passes.def @@ -0,0 +1,21 @@ +/* Arm-specific passes declarations. + Copyright (C) 2022 Free Software Foundation, Inc. + Contributed by Arm Ltd. + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GCC is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GCC; see the file COPYING3. If not see + . */ + +INSERT_PASS_BEFORE (pass_shorten_branches, 1, pass_insert_bti); diff --git a/gcc/config/arm/arm-protos.h b/gcc/config/arm/arm-protos.h index d97a1c3bf56..61c2bb7b526 100644 --- a/gcc/config/arm/arm-protos.h +++ b/gcc/config/arm/arm-protos.h @@ -24,6 +24,8 @@ #include "sbitmap.h" +rtl_opt_pass *make_pass_insert_bti (gcc::context *ctxt); + extern enum unwind_info_type arm_except_unwind_info (struct gcc_options *); extern int use_return_insn (int, rtx); extern bool use_simple_return_p (void); diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc index 4af809373e4..0f105f60c08 100644 --- a/gcc/config/arm/arm.cc +++ b/gcc/config/arm/arm.cc @@ -33108,13 +33108,69 @@ arm_current_function_pac_enabled_p (void) && !crtl->is_leaf)); } +/* Raise an error if the current target arch is not bti compatible. */ +void aarch_bti_arch_check (void) +{ + if (!arm_arch8m_main) + error ("This architecture does not support branch protection instructions"); +} + /* Return TRUE if Branch Target Identification Mechanism is enabled. */ -static bool -aarch_bti_enabled () +bool +aarch_bti_enabled (void) +{ + return aarch_enable_bti != 0; +} + +/* Check if INSN is a BTI J insn. */ +bool +aarch_bti_j_insn_p (rtx_insn *insn) +{ + if (!insn || !INSN_P (insn)) + return false; + + rtx pat = PATTERN (insn); + return GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == VUNSPEC_BTI_NOP; +} + +/* Check if X (or any sub-rtx of X) is a PACIASP/PACIBSP instruction. */ +bool +aarch_pac_insn_p (rtx x) { + if (!x || !INSN_P (x)) + return false; + + rtx pat = PATTERN (x); + + if (GET_CODE (pat) == SET) + { + rtx tmp = XEXP (pat, 1); + if (tmp + && ((GET_CODE (tmp) == UNSPEC + && XINT (tmp, 1) == UNSPEC_PAC_NOP) + || (GET_CODE (tmp) == UNSPEC_VOLATILE + && XINT (tmp, 1) == VUNSPEC_PACBTI_NOP))) + return true; + } + return false; } + /* Target specific mapping for aarch_gen_bti_c and aarch_gen_bti_j. + For Arm, both of these map to a simple BTI instruction. */ + +rtx +aarch_gen_bti_c (void) +{ + return gen_bti_nop (); +} + +rtx +aarch_gen_bti_j (void) +{ + return gen_bti_nop (); +} + /* 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 d624cfae5b8..36062292b90 100644 --- a/gcc/config/arm/arm.md +++ b/gcc/config/arm/arm.md @@ -13001,6 +13001,13 @@ (define_insn "aut_nop" "aut\t%|ip, %|lr, %|sp" [(set_attr "conds" "unconditional")]) +(define_insn "bti_nop" + [(unspec_volatile [(const_int 0)] VUNSPEC_BTI_NOP)] + "arm_arch8m_main" + "bti" + [(set_attr "conds" "unconditional") + (set_attr "type" "nop")]) + ;; 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/t-arm b/gcc/config/arm/t-arm index 041cc6ec045..683342cb528 100644 --- a/gcc/config/arm/t-arm +++ b/gcc/config/arm/t-arm @@ -175,3 +175,13 @@ arm-d.o: $(srcdir)/config/arm/arm-d.cc arm-common.o: arm-cpu-cdata.h driver-arm.o: arm-native.h + +PASSES_EXTRA += $(srcdir)/config/arm/arm-passes.def + +aarch-bti-insert.o: $(srcdir)/config/arm/aarch-bti-insert.cc \ + $(CONFIG_H) $(SYSTEM_H) $(TM_H) $(REGS_H) insn-config.h $(RTL_BASE_H) \ + dominance.h cfg.h cfganal.h $(BASIC_BLOCK_H) $(INSN_ATTR_H) $(RECOG_H) \ + output.h hash-map.h $(DF_H) $(OBSTACK_H) $(TARGET_H) $(RTL_H) \ + $(CONTEXT_H) $(TREE_PASS_H) regrename.h + $(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \ + $(srcdir)/config/arm/aarch-bti-insert.cc diff --git a/gcc/config/arm/unspecs.md b/gcc/config/arm/unspecs.md index 370f0b12da1..93447a8ce9d 100644 --- a/gcc/config/arm/unspecs.md +++ b/gcc/config/arm/unspecs.md @@ -257,6 +257,7 @@ (define_c_enum "unspecv" [ ; instruction. VUNSPEC_PACBTI_NOP ; Represents PAC signing LR + valid landing pad VUNSPEC_AUT_NOP ; Represents PAC verifying LR + VUNSPEC_BTI_NOP ; Represent BTI ]) ;; Enumerators for NEON unspecs. diff --git a/gcc/testsuite/gcc.target/arm/bti-1.c b/gcc/testsuite/gcc.target/arm/bti-1.c new file mode 100644 index 00000000000..79dd8010d2d --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/bti-1.c @@ -0,0 +1,12 @@ +/* Check that GCC does bti instruction. */ +/* { dg-do compile } */ +/* { dg-skip-if "avoid conflicting multilib options" { *-*-* } { "-marm" "-mcpu=*" } } */ +/* { dg-options "-march=armv8.1-m.main -mthumb -mfloat-abi=softfp -mbranch-protection=bti --save-temps" } */ + +int +main (void) +{ + return 0; +} + +/* { dg-final { scan-assembler "bti" } } */ diff --git a/gcc/testsuite/gcc.target/arm/bti-2.c b/gcc/testsuite/gcc.target/arm/bti-2.c new file mode 100644 index 00000000000..33910563849 --- /dev/null +++ b/gcc/testsuite/gcc.target/arm/bti-2.c @@ -0,0 +1,58 @@ +/* { dg-do compile } */ +/* -Os to create jump table. */ +/* { dg-options "-Os" } */ +/* { dg-skip-if "avoid conflicting multilib options" { *-*-* } { "-marm" "-mcpu=*" } } */ +/* { dg-options "-march=armv8.1-m.main -mthumb -mfloat-abi=softfp -mbranch-protection=bti --save-temps" } */ + +extern int f1 (void); +extern int f2 (void); +extern int f3 (void); +extern int f4 (void); +extern int f5 (void); +extern int f6 (void); +extern int f7 (void); +extern int f8 (void); +extern int f9 (void); +extern int f10 (void); + +int (*ptr) (void); + +int +f_jump_table (int y, int n) +{ + int i; + for (i = 0; i < n ;i ++) + { + switch (y) + { + case 0 : ptr = f1; break; + case 1 : ptr = f2; break; + case 2 : ptr = f3; break; + case 3 : ptr = f4; break; + case 4 : ptr = f5; break; + case 5 : ptr = f6; break; + case 6 : ptr = f7; break; + case 7 : ptr = f8; break; + case 8 : ptr = f9; break; + case 9 : ptr = f10; break; + default: break; + } + y += ptr (); + } + return (y == 0)? y+1:4; +} + +int +f_label_address () +{ + static void * addr = &&lab1; + goto *addr; +lab1: + addr = &&lab2; + return 1; +lab2: + addr = &&lab1; + return 2; +} + +/* { dg-final { scan-assembler-times "bti" 15 } } */ -- 2.25.1 --=-=-=--