Skip to content

[ARM][AArch64] BTI,GCS,PAC Module flag update. #86212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,22 +1182,29 @@ void CodeGenModule::Release() {
"tag-stack-memory-buildattr", 1);

if (T.isARM() || T.isThumb() || T.isAArch64()) {
// Previously 1 is used and meant for the backed to derive the function
// attribute form it. 2 now means function attributes already set for all
// functions in this module, so no need to propagate those from the module
// flag. Value is only used in case of LTO module merge because the backend
// will see all required function attribute set already. Value is used
// before modules got merged. Any posive value means the feature is active
// and required binary markings need to be emit accordingly.
if (LangOpts.BranchTargetEnforcement)
getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
1);
2);
if (LangOpts.BranchProtectionPAuthLR)
getModule().addModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr",
1);
2);
if (LangOpts.GuardedControlStack)
getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 1);
getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
if (LangOpts.hasSignReturnAddress())
getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 2);
if (LangOpts.isSignReturnAddressScopeAll())
getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
1);
2);
if (!LangOpts.isSignReturnAddressWithAKey())
getModule().addModuleFlag(llvm::Module::Min,
"sign-return-address-with-bkey", 1);
"sign-return-address-with-bkey", 2);

if (getTriple().isOSLinux()) {
assert(getTriple().isOSBinFormatELF());
Expand Down
12 changes: 6 additions & 6 deletions clang/test/CodeGen/aarch64-sign-return-address.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
// NONE-NOT: !"branch-target-enforcement"
// ALL-NOT: !"branch-target-enforcement"
// PART-NOT: !"branch-target-enforcement"
// BTE: !{i32 8, !"branch-target-enforcement", i32 1}
// BTE: !{i32 8, !"branch-target-enforcement", i32 2}
// B-KEY-NOT: !"branch-target-enforcement"

// NONE-NOT: !"sign-return-address"
// ALL: !{i32 8, !"sign-return-address", i32 1}
// PART: !{i32 8, !"sign-return-address", i32 1}
// ALL: !{i32 8, !"sign-return-address", i32 2}
// PART: !{i32 8, !"sign-return-address", i32 2}
// BTE-NOT: !"sign-return-address"
// B-KEY: !{i32 8, !"sign-return-address", i32 1}
// B-KEY: !{i32 8, !"sign-return-address", i32 2}

// NONE-NOT: !"sign-return-address-all"
// ALL: !{i32 8, !"sign-return-address-all", i32 1}
// ALL: !{i32 8, !"sign-return-address-all", i32 2}
// PART-NOT: !"sign-return-address-all"
// BTE-NOT: !"sign-return-address-all"
// B-KEY-NOT: !"sign-return-address-all"
Expand All @@ -47,6 +47,6 @@
// ALL-NOT: !"sign-return-address-with-bkey"
// PART-NOT: !"sign-return-address-with-bkey"
// BTE-NOT: !"sign-return-address-with-bkey"
// B-KEY: !{i32 8, !"sign-return-address-with-bkey", i32 1}
// B-KEY: !{i32 8, !"sign-return-address-with-bkey", i32 2}

void foo() {}
8 changes: 4 additions & 4 deletions clang/test/CodeGen/arm-branch-protection-attr-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
// NONE-NOT: !"branch-target-enforcement"
// PART-NOT: !"branch-target-enforcement"
// ALL-NOT: !"branch-target-enforcement"
// BTE: !{i32 8, !"branch-target-enforcement", i32 1}
// BTE: !{i32 8, !"branch-target-enforcement", i32 2}

// NONE-NOT: !"sign-return-address"
// PART: !{i32 8, !"sign-return-address", i32 1}
// ALL: !{i32 8, !"sign-return-address", i32 1}
// PART: !{i32 8, !"sign-return-address", i32 2}
// ALL: !{i32 8, !"sign-return-address", i32 2}
// BTE-NOT: !"sign-return-address"

// NONE-NOT: !"sign-return-address-all", i32 0}
// PART-NOT: !"sign-return-address-all", i32 0}
// ALL: !{i32 8, !"sign-return-address-all", i32 1}
// ALL: !{i32 8, !"sign-return-address-all", i32 2}
// BTE-NOT: !"sign-return-address-all", i32 0}

void foo() {}
2 changes: 1 addition & 1 deletion clang/test/Frontend/arm-ignore-branch-protection-option.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ __attribute__((target("arch=cortex-m0"))) void f() {}
// CHECK-NOT: attributes { {{.*}} "branch-target-enforcement"

/// Check that there are branch protection module attributes despite the warning.
// CHECK: !{i32 8, !"branch-target-enforcement", i32 1}
// CHECK: !{i32 8, !"branch-target-enforcement", i32 2}
10 changes: 10 additions & 0 deletions llvm/include/llvm/IR/AutoUpgrade.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ namespace llvm {
/// info. Return true if module is modified.
bool UpgradeDebugInfo(Module &M);

/// Copies module attributes to the functions in the module.
/// Currently only effects ARM, Thumb and AArch64 targets.
/// Supported attributes:
/// - branch-target-enforcement
/// - branch-protection-pauth-lr
/// - guarded-control-stack
/// - sign-return-address
/// - sign-return-address-with-bkey
void CopyModuleAttrToFunctions(Module &M);

/// Check whether a string looks like an old loop attachment tag.
inline bool mayBeOldLoopAttachmentTag(StringRef Name) {
return Name.starts_with("llvm.vectorizer.");
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {

UpgradeModuleFlags(*M);
UpgradeSectionAttributes(*M);
CopyModuleAttrToFunctions(*M);

if (PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE)
M->setIsNewDbgInfoFormat(UseNewDbgInfoFormat);
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7070,6 +7070,8 @@ Error BitcodeReader::materializeModule() {

UpgradeARCRuntime(*TheModule);

CopyModuleAttrToFunctions(*TheModule);

return Error::success();
}

Expand Down
100 changes: 100 additions & 0 deletions llvm/lib/IR/AutoUpgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5296,6 +5296,106 @@ void llvm::UpgradeFunctionAttributes(Function &F) {
}
}

// Check if the module attribute is present and set to one.
static bool isModuleAttributeOne(Module &M, const StringRef &ModAttr) {
const auto *Attr =
mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(ModAttr));
return Attr && Attr->isOne();
}

// Check if the module attribute is present and set to two.
static bool isModuleAttributeTwo(Module &M, const StringRef &ModAttr) {
const auto *Attr =
mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(ModAttr));
return Attr && Attr->getZExtValue() == 2;
}

// Check if the function attribute is not present and set it.
static void SetFunctionAttrIfNotSet(Function &F, StringRef FnAttrName,
StringRef Value) {
if (!F.hasFnAttribute(FnAttrName))
F.addFnAttr(FnAttrName, Value);
}

// Check if the function attribute is not present and set it if needed.
// If the attribute is "false" then removes it.
// If the attribute is "true" resets it to a valueless attribute.
static void ConvertFunctionAttr(Function &F, bool Set, StringRef FnAttrName) {
if (!F.hasFnAttribute(FnAttrName)) {
if (Set)
F.addFnAttr(FnAttrName);
} else {
auto A = F.getFnAttribute(FnAttrName);
if ("false" == A.getValueAsString())
F.removeFnAttr(FnAttrName);
else if ("true" == A.getValueAsString()) {
F.removeFnAttr(FnAttrName);
F.addFnAttr(FnAttrName);
}
}
}

void llvm::CopyModuleAttrToFunctions(Module &M) {
Triple T(M.getTargetTriple());
if (!T.isThumb() && !T.isARM() && !T.isAArch64())
return;

if (isModuleAttributeTwo(M, "branch-target-enforcement"))
return;
if (isModuleAttributeTwo(M, "branch-protection-pauth-lr"))
return;
if (isModuleAttributeTwo(M, "guarded-control-stack"))
return;
if (isModuleAttributeTwo(M, "sign-return-address"))
return;

bool BTE = isModuleAttributeOne(M, "branch-target-enforcement");
bool BPPLR = isModuleAttributeOne(M, "branch-protection-pauth-lr");
bool GCS = isModuleAttributeOne(M, "guarded-control-stack");
bool SRA = isModuleAttributeOne(M, "sign-return-address");

StringRef SignTypeValue = "non-leaf";
if (SRA && isModuleAttributeOne(M, "sign-return-address-all"))
SignTypeValue = "all";

StringRef SignKeyValue = "a_key";
if (SRA && isModuleAttributeOne(M, "sign-return-address-with-bkey"))
SignKeyValue = "b_key";

for (Function &F : M.getFunctionList()) {
if (F.isDeclaration())
continue;

if (SRA) {
SetFunctionAttrIfNotSet(F, "sign-return-address", SignTypeValue);
SetFunctionAttrIfNotSet(F, "sign-return-address-key", SignKeyValue);
} else {
if (auto A = F.getFnAttribute("sign-return-address");
A.isValid() && "none" == A.getValueAsString()) {
F.removeFnAttr("sign-return-address");
F.removeFnAttr("sign-return-address-key");
}
}
ConvertFunctionAttr(F, BTE, "branch-target-enforcement");
ConvertFunctionAttr(F, BPPLR, "branch-protection-pauth-lr");
ConvertFunctionAttr(F, GCS, "guarded-control-stack");
}

if (BTE)
M.setModuleFlag(llvm::Module::Min, "branch-target-enforcement", 2);
if (BPPLR)
M.setModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr", 2);
if (GCS)
M.setModuleFlag(llvm::Module::Min, "guarded-control-stack", 2);
if (SRA) {
M.setModuleFlag(llvm::Module::Min, "sign-return-address", 2);
if (isModuleAttributeOne(M, "sign-return-address-all"))
M.setModuleFlag(llvm::Module::Min, "sign-return-address-all", 2);
if (isModuleAttributeOne(M, "sign-return-address-with-bkey"))
M.setModuleFlag(llvm::Module::Min, "sign-return-address-with-bkey", 2);
}
}

static bool isOldLoopArgument(Metadata *MD) {
auto *T = dyn_cast_or_null<MDTuple>(MD);
if (!T)
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Linker/IRMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,11 @@ Error IRLinker::run() {
// Loop over all of the linked values to compute type mappings.
computeTypeMapping();

// Convert module level attributes to function level attributes because
// after merging modules the attributes might change and would have different
// effect on the functions as the original module would have.
CopyModuleAttrToFunctions(*SrcM);

std::reverse(Worklist.begin(), Worklist.end());
while (!Worklist.empty()) {
GlobalValue *GV = Worklist.back();
Expand Down Expand Up @@ -1770,6 +1775,11 @@ IRMover::IRMover(Module &M) : Composite(M) {
for (const auto *MD : StructTypes.getVisitedMetadata()) {
SharedMDs[MD].reset(const_cast<MDNode *>(MD));
}

// Convert module level attributes to function level attributes because
// after merging modules the attributes might change and would have different
// effect on the functions as the original module would have.
CopyModuleAttrToFunctions(M);
}

Error IRMover::move(std::unique_ptr<Module> Src,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/ARM/ARMAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ void ARMAsmPrinter::emitAttributes() {

auto *BTIValue = mdconst::extract_or_null<ConstantInt>(
SourceModule->getModuleFlag("branch-target-enforcement"));
if (BTIValue && BTIValue->isOne()) {
if (BTIValue && !BTIValue->isZero()) {
// If "+pacbti" is used as an architecture extension,
// Tag_BTI_extension is emitted in
// ARMTargetStreamer::emitTargetAttributes().
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/LowerTypeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ bool LowerTypeTestsModule::hasBranchTargetEnforcement() {
// the module flags.
if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("branch-target-enforcement")))
HasBranchTargetEnforcement = (BTE->getZExtValue() != 0);
HasBranchTargetEnforcement = !BTE->isZero();
else
HasBranchTargetEnforcement = 0;
}
Expand Down
15 changes: 9 additions & 6 deletions llvm/test/Bitcode/upgrade-branch-protection.ll
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
;; Test that module flags "branch-target-enforcement" and "sign-return-address" can be upgraded to
;; are upgraded from Error to Min.
;; Test that module flags "branch-target-enforcement" and "sign-return-address"
;; can be upgraded to are upgraded from Error to Min and the value is changed 2
;; as the module is converted to the semantic.

; RUN: llvm-as %s -o - | llvm-dis - | FileCheck %s

target triple = "aarch64-unknown-linux-gnu"

!llvm.module.flags = !{!0, !1, !2, !3}

!0 = !{i32 1, !"branch-target-enforcement", i32 1}
!1 = !{i32 1, !"sign-return-address", i32 1}
!2 = !{i32 1, !"sign-return-address-all", i32 1}
!3 = !{i32 1, !"sign-return-address-with-bkey", i32 1}

;CHECK: !0 = !{i32 8, !"branch-target-enforcement", i32 1}
;CHECK: !1 = !{i32 8, !"sign-return-address", i32 1}
;CHECK: !2 = !{i32 8, !"sign-return-address-all", i32 1}
;CHECK: !3 = !{i32 8, !"sign-return-address-with-bkey", i32 1}
;CHECK: !0 = !{i32 8, !"branch-target-enforcement", i32 2}
;CHECK: !1 = !{i32 8, !"sign-return-address", i32 2}
;CHECK: !2 = !{i32 8, !"sign-return-address-all", i32 2}
;CHECK: !3 = !{i32 8, !"sign-return-address-with-bkey", i32 2}
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/Thumb2/pacbti-m-outliner-5.ll
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ attributes #1 = { minsize nofree norecurse nounwind optsize }
!llvm.module.flags = !{!0, !1, !2}

!0 = !{i32 8, !"branch-target-enforcement", i32 0}
!1 = !{i32 8, !"sign-return-address", i32 1}
!1 = !{i32 8, !"sign-return-address", i32 2}
!2 = !{i32 8, !"sign-return-address-all", i32 0}
16 changes: 0 additions & 16 deletions llvm/test/LTO/AArch64/Inputs/foo.ll

This file was deleted.

35 changes: 35 additions & 0 deletions llvm/test/LTO/AArch64/TestInputs/bar.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
;; This file contains the new semantic of the branch-target-enforcement, sign-return-address.
;; Used for test mixing a mixed link case and also verify the import too in llc.

; RUN: llc -mattr=+pauth -mattr=+bti %s -o - | FileCheck %s

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"

define dso_local void @bar() #0 {
entry:
ret void
}
; CHECK-LABEL: bar:
; CHECK-NOT: hint
; CHECK-NOT: bti
; CHECK: ret

define dso_local void @baz() #1 {
entry:
ret void
}

; CHECK-LABEL: baz:
; CHECK: bti c
; CHECK: ret

attributes #0 = { noinline nounwind optnone uwtable }
attributes #1 = { noinline nounwind optnone uwtable "branch-target-enforcement" }

!llvm.module.flags = !{!0, !1, !2, !3}

!0 = !{i32 8, !"branch-target-enforcement", i32 2}
!1 = !{i32 8, !"sign-return-address", i32 2}
!2 = !{i32 8, !"sign-return-address-all", i32 2}
!3 = !{i32 8, !"sign-return-address-with-bkey", i32 2}
Loading
Loading