Skip to content

Commit bb64966

Browse files
committed
Mark CXX module initializer with PACBTI attributes
The CXX module initializer function, which is called at program startup, needs to be tagged with Pointer Authentication and Branch Target Identification marks whenever relevant. Before this patch, in CPUs set up for PACBTI execution, the function wasn't protected with return address signing and no BTI instruction was inserted at the start of it, thus leading to an execution fault. This patch fixes the issue by marking the function with the function attributes related to PAC and BTI if relevant.
1 parent 2218587 commit bb64966

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,12 @@ void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) {
818818
Fn->addFnAttr("device-init");
819819
}
820820

821+
if (getTarget().isBranchProtectionSupportedArch(
822+
getTarget().getTargetOpts().CPU)) {
823+
TargetInfo::BranchProtectionInfo BPI(getLangOpts());
824+
getTargetCodeGenInfo().setBranchProtectionFnAttributes(BPI, (*Fn));
825+
}
826+
821827
// We are done with the inits.
822828
AllImports.clear();
823829
PrioritizedCXXGlobalInits.clear();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -emit-module-interface -target-feature +pacbti -mbranch-target-enforce -std=c++20 %s -o %t.pcm
2+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -std=c++20 %t.pcm -emit-llvm -o - | \
3+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-BTI %s
4+
5+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -emit-module-interface -target-feature +pacbti -msign-return-address=non-leaf -std=c++20 %s -o %t.pcm
6+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -std=c++20 %t.pcm -emit-llvm -o - | \
7+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-PAC %s
8+
9+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -emit-module-interface -target-feature +pacbti -msign-return-address=all -std=c++20 %s -o %t.pcm
10+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -std=c++20 %t.pcm -emit-llvm -o - | \
11+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-PAC-ALL %s
12+
13+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -emit-module-interface -target-feature +pacbti -msign-return-address=non-leaf -mbranch-target-enforce -std=c++20 %s -o %t.pcm
14+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -std=c++20 %t.pcm -emit-llvm -o - | \
15+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-PAC-BTI %s
16+
17+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -emit-module-interface -target-feature +pacbti -msign-return-address=all -mbranch-target-enforce -std=c++20 %s -o %t.pcm
18+
// RUN: %clang_cc1 -triple thumbv8.1m.main-unknown-none-eabi -std=c++20 %t.pcm -emit-llvm -o - | \
19+
// RUN: FileCheck --check-prefixes=CHECK,CHECK-PAC-BTI-ALL %s
20+
21+
// CHECK: define void @_ZGIW3foo() #0
22+
// CHECK-BTI: attributes #0 = { nounwind "branch-target-enforcement" }
23+
// CHECK-PAC: attributes #0 = { nounwind "sign-return-address"="non-leaf" "sign-return-address-key"="a_key" }
24+
// CHECK-PAC-ALL: attributes #0 = { nounwind "sign-return-address"="all" "sign-return-address-key"="a_key" }
25+
// CHECK-PAC-BTI: attributes #0 = { nounwind "branch-target-enforcement" "sign-return-address"="non-leaf" "sign-return-address-key"="a_key" }
26+
// CHECK-PAC-BTI-ALL: attributes #0 = { nounwind "branch-target-enforcement" "sign-return-address"="all" "sign-return-address-key"="a_key" }
27+
28+
module;
29+
30+
export module foo;
31+
32+
export void func();

0 commit comments

Comments
 (0)