Skip to content

Commit c5a4900

Browse files
[AArch64] Add BTI to CFI jumptables.
With branch protection the jump to the jump table entries requires a landing pad. Reviewed By: eugenis, tamas.petz Differential Revision: https://reviews.llvm.org/D81251
1 parent e6f332e commit c5a4900

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
12051205

12061206
static const unsigned kX86JumpTableEntrySize = 8;
12071207
static const unsigned kARMJumpTableEntrySize = 4;
1208+
static const unsigned kARMBTIJumpTableEntrySize = 8;
12081209

12091210
unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
12101211
switch (Arch) {
@@ -1213,7 +1214,12 @@ unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
12131214
return kX86JumpTableEntrySize;
12141215
case Triple::arm:
12151216
case Triple::thumb:
1217+
return kARMJumpTableEntrySize;
12161218
case Triple::aarch64:
1219+
if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
1220+
M.getModuleFlag("branch-target-enforcement")))
1221+
if (BTE->getZExtValue())
1222+
return kARMBTIJumpTableEntrySize;
12171223
return kARMJumpTableEntrySize;
12181224
default:
12191225
report_fatal_error("Unsupported architecture for jump tables");
@@ -1232,7 +1238,13 @@ void LowerTypeTestsModule::createJumpTableEntry(
12321238
if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
12331239
AsmOS << "jmp ${" << ArgIndex << ":c}@plt\n";
12341240
AsmOS << "int3\nint3\nint3\n";
1235-
} else if (JumpTableArch == Triple::arm || JumpTableArch == Triple::aarch64) {
1241+
} else if (JumpTableArch == Triple::arm) {
1242+
AsmOS << "b $" << ArgIndex << "\n";
1243+
} else if (JumpTableArch == Triple::aarch64) {
1244+
if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
1245+
Dest->getParent()->getModuleFlag("branch-target-enforcement")))
1246+
if (BTE->getZExtValue())
1247+
AsmOS << "bti c\n";
12361248
AsmOS << "b $" << ArgIndex << "\n";
12371249
} else if (JumpTableArch == Triple::thumb) {
12381250
AsmOS << "b.w $" << ArgIndex << "\n";
@@ -1394,6 +1406,10 @@ void LowerTypeTestsModule::createJumpTable(
13941406
// by Clang for -march=armv7.
13951407
F->addFnAttr("target-cpu", "cortex-a8");
13961408
}
1409+
if (JumpTableArch == Triple::aarch64) {
1410+
F->addFnAttr("branch-target-enforcement", "false");
1411+
F->addFnAttr("sign-return-address", "none");
1412+
}
13971413
// Make sure we don't emit .eh_frame for this function.
13981414
F->addFnAttr(Attribute::NoUnwind);
13991415

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: opt -S -lowertypetests -mtriple=aarch64-unknown-linux-gnu < %s | FileCheck --check-prefixes=AARCH64 %s
2+
3+
; Test for the jump table generation with branch protection on AArch64
4+
5+
target datalayout = "e-p:64:64"
6+
7+
@0 = private unnamed_addr constant [2 x void (...)*] [void (...)* bitcast (void ()* @f to void (...)*), void (...)* bitcast (void ()* @g to void (...)*)], align 16
8+
9+
; AARCH64: @f = alias void (), void ()* @[[JT:.*]]
10+
11+
define void @f() !type !0 {
12+
ret void
13+
}
14+
15+
define internal void @g() !type !0 {
16+
ret void
17+
}
18+
19+
!0 = !{i32 0, !"typeid1"}
20+
21+
declare i1 @llvm.type.test(i8* %ptr, metadata %bitset) nounwind readnone
22+
23+
define i1 @foo(i8* %p) {
24+
%x = call i1 @llvm.type.test(i8* %p, metadata !"typeid1")
25+
ret i1 %x
26+
}
27+
28+
!llvm.module.flags = !{!1}
29+
30+
!1 = !{i32 4, !"branch-target-enforcement", i32 1}
31+
32+
; AARCH64: define private void @[[JT]]() #[[ATTR:.*]] align 8 {
33+
34+
; AARCH64: bti c
35+
; AARCH64-SAME: b $0
36+
; AARCH64-SAME: bti c
37+
; AARCH64-SAME: b $1
38+
39+
; AARCH64: attributes #[[ATTR]] = { naked nounwind "branch-target-enforcement"="false" "sign-return-address"="none"

0 commit comments

Comments
 (0)