Skip to content

Commit 6aac30f

Browse files
authored
Update FEAT_PAuth_LR behaviour for AArch64 (#90614)
Currently, LLVM enables `-mbranch-protection=standard` as `bti+pac-ret`. To align LLVM with the behaviour in GNU, this has been updated to `bti+pac-ret+pc` when FEAT_PAuth_LR is enabled as an optional feature via the `-mcpu=` options. If this is not enabled, then this will revert to the existing behaviour.
1 parent 6ce4c4c commit 6aac30f

File tree

8 files changed

+39
-4
lines changed

8 files changed

+39
-4
lines changed

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ bool AArch64TargetInfo::validateBranchProtection(StringRef Spec, StringRef,
225225
BranchProtectionInfo &BPI,
226226
StringRef &Err) const {
227227
llvm::ARM::ParsedBranchProtection PBP;
228-
if (!llvm::ARM::parseBranchProtection(Spec, PBP, Err))
228+
if (!llvm::ARM::parseBranchProtection(Spec, PBP, Err, HasPAuthLR))
229229
return false;
230230

231231
BPI.SignReturnAddr =

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "llvm/Support/Path.h"
5656
#include "llvm/Support/Process.h"
5757
#include "llvm/Support/YAMLParser.h"
58+
#include "llvm/TargetParser/AArch64TargetParser.h"
5859
#include "llvm/TargetParser/ARMTargetParserCommon.h"
5960
#include "llvm/TargetParser/Host.h"
6061
#include "llvm/TargetParser/LoongArchTargetParser.h"
@@ -1511,7 +1512,24 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
15111512
} else {
15121513
StringRef DiagMsg;
15131514
llvm::ARM::ParsedBranchProtection PBP;
1514-
if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg))
1515+
bool EnablePAuthLR = false;
1516+
1517+
// To know if we need to enable PAuth-LR As part of the standard branch
1518+
// protection option, it needs to be determined if the feature has been
1519+
// activated in the `march` argument. This information is stored within the
1520+
// CmdArgs variable and can be found using a search.
1521+
if (isAArch64) {
1522+
auto isPAuthLR = [](const char *member) {
1523+
llvm::AArch64::ExtensionInfo pauthlr_extension =
1524+
llvm::AArch64::getExtensionByID(llvm::AArch64::AEK_PAUTHLR);
1525+
return (pauthlr_extension.Feature.compare(member) == 0);
1526+
};
1527+
1528+
if (std::any_of(CmdArgs.begin(), CmdArgs.end(), isPAuthLR))
1529+
EnablePAuthLR = true;
1530+
}
1531+
if (!llvm::ARM::parseBranchProtection(A->getValue(), PBP, DiagMsg,
1532+
EnablePAuthLR))
15151533
D.Diag(diag::err_drv_unsupported_option_argument)
15161534
<< A->getSpelling() << DiagMsg;
15171535
if (!isAArch64 && PBP.Key == "b_key")

clang/test/Preprocessor/aarch64-target-features.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@
616616
// ================== Check Armv9.5-A Pointer Authentication Enhancements(PAuth_LR).
617617
// RUN: %clang -target arm64-none-linux-gnu -march=armv8-a -x c -E -dM %s -o - | FileCheck -check-prefix=CHECK-PAUTH-LR-OFF %s
618618
// RUN: %clang -target arm64-none-linux-gnu -march=armv9.5-a -x c -E -dM %s -o - | FileCheck -check-prefix=CHECK-PAUTH-LR-OFF %s
619+
// RUN: %clang -target arm64-none-linux-gnu -march=armv9.5-a -mbranch-protection=standard -x c -E -dM %s -o - | FileCheck -check-prefixes=CHECK-PAUTH-LR-OFF,CHECK-BRANCH-PROTECTION-NO-PC %s
620+
// RUN: %clang -target arm64-none-linux-gnu -march=armv9.5-a+pauth-lr -mbranch-protection=standard -x c -E -dM %s -o - | FileCheck -check-prefixes=CHECK-PAUTH-LR,CHECK-BRANCH-PROTECTION-PC %s
621+
// RUN: %clang -target arm64-none-linux-gnu -march=armv9.5-a+nopauth-lr -mbranch-protection=standard -x c -E -dM %s -o - | FileCheck -check-prefixes=CHECK-PAUTH-LR-OFF,CHECK-BRANCH-PROTECTION-NO-PC %s
619622
// RUN: %clang -target arm64-none-linux-gnu -march=armv8-a+pauth -mbranch-protection=none -x c -E -dM %s -o - | FileCheck -check-prefix=CHECK-PAUTH-LR-OFF %s
620623
// RUN: %clang -target arm64-none-linux-gnu -march=armv8-a+pauth-lr -mbranch-protection=none -x c -E -dM %s -o - | FileCheck -check-prefix=CHECK-PAUTH-LR %s
621624
// RUN: %clang -target arm64-none-linux-gnu -march=armv8-a+pauth-lr -mbranch-protection=bti -x c -E -dM %s -o - | FileCheck -check-prefix=CHECK-PAUTH-LR %s
@@ -636,6 +639,7 @@
636639
// RUN: %clang -target arm64-none-linux-gnu -march=armv8-a+pauth-lr -mbranch-protection=pac-ret+pc+b-key -x c -E -dM %s -o - | FileCheck -check-prefixes=CHECK-PAUTH-LR,CHECK-BRANCH-PROTECTION-PC-BKEY %s
637640
// RUN: %clang -target arm64-none-linux-gnu -march=armv8-a+pauth-lr -mbranch-protection=pac-ret+pc+leaf -x c -E -dM %s -o - | FileCheck -check-prefixes=CHECK-PAUTH-LR,CHECK-BRANCH-PROTECTION-PC-LEAF %s
638641
// RUN: %clang -target arm64-none-linux-gnu -march=armv8-a+pauth-lr -mbranch-protection=pac-ret+pc+leaf+b-key -x c -E -dM %s -o - | FileCheck -check-prefixes=CHECK-PAUTH-LR,CHECK-BRANCH-PROTECTION-PC-LEAF-BKEY %s
642+
// CHECK-BRANCH-PROTECTION-NO-PC: #define __ARM_FEATURE_PAC_DEFAULT 1
639643
// CHECK-BRANCH-PROTECTION-PC: #define __ARM_FEATURE_PAC_DEFAULT 9
640644
// CHECK-BRANCH-PROTECTION-PC-BKEY: #define __ARM_FEATURE_PAC_DEFAULT 10
641645
// CHECK-BRANCH-PROTECTION-PC-LEAF: #define __ARM_FEATURE_PAC_DEFAULT 13

llvm/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ Changes to the AArch64 Backend
7676
* Added support for Cortex-A78AE, Cortex-A520AE, Cortex-A720AE,
7777
Cortex-R82AE, Neoverse-N3, Neoverse-V3 and Neoverse-V3AE CPUs.
7878

79+
* ``-mbranch-protection=standard`` now enables FEAT_PAuth_LR by
80+
default when the feature is enabled. The new behaviour results
81+
in ``standard`` being equal to ``bti+pac-ret+pc`` when ``+pauth-lr``
82+
is passed as part of ``-mcpu=`` options.
83+
7984
Changes to the AMDGPU Backend
8085
-----------------------------
8186

llvm/include/llvm/TargetParser/AArch64TargetParser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ inline constexpr Alias CpuAliases[] = {{"cobalt-100", "neoverse-n2"},
676676

677677
inline constexpr Alias ExtAliases[] = {{"rdma", "rdm"}};
678678

679+
const ExtensionInfo &getExtensionByID(ArchExtKind(ExtID));
680+
679681
bool getExtensionFeatures(
680682
const AArch64::ExtensionBitset &Extensions,
681683
std::vector<StringRef> &Features);

llvm/include/llvm/TargetParser/ARMTargetParserCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct ParsedBranchProtection {
4646
};
4747

4848
bool parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,
49-
StringRef &Err);
49+
StringRef &Err, bool EnablePAuthLR = false);
5050

5151
} // namespace ARM
5252
} // namespace llvm

llvm/lib/TargetParser/AArch64TargetParser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,8 @@ bool AArch64::ExtensionSet::parseModifier(StringRef Modifier) {
280280
}
281281
return false;
282282
}
283+
284+
const AArch64::ExtensionInfo &
285+
AArch64::getExtensionByID(AArch64::ArchExtKind ExtID) {
286+
return lookupExtensionByID(ExtID);
287+
}

llvm/lib/TargetParser/ARMTargetParserCommon.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ ARM::EndianKind ARM::parseArchEndian(StringRef Arch) {
139139
// returned in `PBP`. Returns false in error, with `Err` containing
140140
// an erroneous part of the spec.
141141
bool ARM::parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,
142-
StringRef &Err) {
142+
StringRef &Err, bool EnablePAuthLR) {
143143
PBP = {"none", "a_key", false, false, false};
144144
if (Spec == "none")
145145
return true; // defaults are ok
@@ -148,6 +148,7 @@ bool ARM::parseBranchProtection(StringRef Spec, ParsedBranchProtection &PBP,
148148
PBP.Scope = "non-leaf";
149149
PBP.BranchTargetEnforcement = true;
150150
PBP.GuardedControlStack = true;
151+
PBP.BranchProtectionPAuthLR = EnablePAuthLR;
151152
return true;
152153
}
153154

0 commit comments

Comments
 (0)