Skip to content

Commit 73c9f16

Browse files
committed
[LowerTypeTests] Add ENDBR to .cfi.jumptable for x86 Indirect Branch Tracking
Similar to D81251 for AArch64 BTI. This fixes `./a.out test` for ``` void foo(void) {} void bar(void) {} static void (*fptr)(void); int main(int argc, char **argv) { if (argv[1]) fptr = foo; else fptr = bar; fptr(); } ``` `clang -flto=thin -fvisibility=hidden -fsanitize=cfi-icall -fcf-protection=branch -fuse-ld=lld a.cc` Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D140655
1 parent 9768a71 commit 73c9f16

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
11791179
}
11801180

11811181
static const unsigned kX86JumpTableEntrySize = 8;
1182+
static const unsigned kX86IBTJumpTableEntrySize = 16;
11821183
static const unsigned kARMJumpTableEntrySize = 4;
11831184
static const unsigned kARMBTIJumpTableEntrySize = 8;
11841185
static const unsigned kRISCVJumpTableEntrySize = 8;
@@ -1187,6 +1188,10 @@ unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
11871188
switch (Arch) {
11881189
case Triple::x86:
11891190
case Triple::x86_64:
1191+
if (const auto *MD = mdconst::extract_or_null<ConstantInt>(
1192+
M.getModuleFlag("cf-protection-branch")))
1193+
if (MD->getZExtValue())
1194+
return kX86IBTJumpTableEntrySize;
11901195
return kX86JumpTableEntrySize;
11911196
case Triple::arm:
11921197
case Triple::thumb:
@@ -1215,8 +1220,17 @@ void LowerTypeTestsModule::createJumpTableEntry(
12151220
unsigned ArgIndex = AsmArgs.size();
12161221

12171222
if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
1223+
bool Endbr = false;
1224+
if (const auto *MD = mdconst::extract_or_null<ConstantInt>(
1225+
Dest->getParent()->getModuleFlag("cf-protection-branch")))
1226+
Endbr = MD->getZExtValue() != 0;
1227+
if (Endbr)
1228+
AsmOS << (JumpTableArch == Triple::x86 ? "endbr32\n" : "endbr64\n");
12181229
AsmOS << "jmp ${" << ArgIndex << ":c}@plt\n";
1219-
AsmOS << "int3\nint3\nint3\n";
1230+
if (Endbr)
1231+
AsmOS << ".balign 16, 0xcc\n";
1232+
else
1233+
AsmOS << "int3\nint3\nint3\n";
12201234
} else if (JumpTableArch == Triple::arm) {
12211235
AsmOS << "b $" << ArgIndex << "\n";
12221236
} else if (JumpTableArch == Triple::aarch64) {
@@ -1389,6 +1403,9 @@ void LowerTypeTestsModule::createJumpTable(
13891403
// by Clang for -march=armv7.
13901404
F->addFnAttr("target-cpu", "cortex-a8");
13911405
}
1406+
// When -mbranch-protection= is used, the inline asm adds a BTI. Suppress BTI
1407+
// for the function to avoid double BTI. This is a no-op without
1408+
// -mbranch-protection=.
13921409
if (JumpTableArch == Triple::aarch64) {
13931410
F->addFnAttr("branch-target-enforcement", "false");
13941411
F->addFnAttr("sign-return-address", "none");
@@ -1398,6 +1415,11 @@ void LowerTypeTestsModule::createJumpTable(
13981415
// the linker.
13991416
F->addFnAttr("target-features", "-c,-relax");
14001417
}
1418+
// When -fcf-protection= is used, the inline asm adds an ENDBR. Suppress ENDBR
1419+
// for the function to avoid double ENDBR. This is a no-op without
1420+
// -fcf-protection=.
1421+
if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64)
1422+
F->addFnAttr(Attribute::NoCfCheck);
14011423
// Make sure we don't emit .eh_frame for this function.
14021424
F->addFnAttr(Attribute::NoUnwind);
14031425

llvm/test/Transforms/LowerTypeTests/function.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ define i1 @foo(ptr %p) {
7777

7878
; NATIVE-SAME: "s,s"(ptr @f.cfi, ptr @g.cfi)
7979

80-
; X86-LINUX: attributes #[[ATTR]] = { naked nounwind }
81-
; X86-WIN32: attributes #[[ATTR]] = { nounwind }
80+
; X86-LINUX: attributes #[[ATTR]] = { naked nocf_check nounwind }
81+
; X86-WIN32: attributes #[[ATTR]] = { nocf_check nounwind }
8282
; ARM: attributes #[[ATTR]] = { naked nounwind
8383
; THUMB: attributes #[[ATTR]] = { naked nounwind "target-cpu"="cortex-a8" "target-features"="+thumb-mode" }
8484
; RISCV: attributes #[[ATTR]] = { naked nounwind "target-features"="-c,-relax" }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
;; Test jump table generation with Indirect Branch Tracking on x86.
2+
; RUN: opt -S -passes=lowertypetests -mtriple=i686 %s | FileCheck --check-prefixes=X86,X86_32 %s
3+
; RUN: opt -S -passes=lowertypetests -mtriple=x86_64 %s | FileCheck --check-prefixes=X86,X86_64 %s
4+
5+
@0 = private unnamed_addr constant [2 x ptr] [ptr @f, ptr @g], align 16
6+
7+
define void @f() !type !0 {
8+
ret void
9+
}
10+
11+
define internal void @g() !type !0 {
12+
ret void
13+
}
14+
15+
declare i1 @llvm.type.test(ptr %ptr, metadata %bitset) nounwind readnone
16+
17+
define i1 @foo(ptr %p) {
18+
%x = call i1 @llvm.type.test(ptr %p, metadata !"typeid1")
19+
ret i1 %x
20+
}
21+
22+
!llvm.module.flags = !{!1}
23+
!0 = !{i32 0, !"typeid1"}
24+
!1 = !{i32 8, !"cf-protection-branch", i32 1}
25+
26+
; X86: define private void @.cfi.jumptable() #[[#ATTR:]] align 16 {
27+
; X86-NEXT: entry:
28+
; X86_32-NEXT: call void asm sideeffect "endbr32\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0Aendbr32\0Ajmp ${1:c}@plt\0A.balign 16, 0xcc\0A", "s,s"(ptr @f.cfi, ptr @g.cfi)
29+
; X86_64-NEXT: call void asm sideeffect "endbr64\0Ajmp ${0:c}@plt\0A.balign 16, 0xcc\0Aendbr64\0Ajmp ${1:c}@plt\0A.balign 16, 0xcc\0A", "s,s"(ptr @f.cfi, ptr @g.cfi)
30+
31+
; X86_64: attributes #[[#ATTR]] = { naked nocf_check nounwind }

0 commit comments

Comments
 (0)