Skip to content

Commit 3ccd041

Browse files
[LowerTypeTests] Emit cfi_jt aliases regardless of function export
A constant complaint we get is that the __typeid__ symbols in the CFI jump tables causes confusing stack traces in applications. Emit the more readable cfi_jt aliases regardless of function export (LTO vs Thin LTO). Reviewed By: pcc, tejohnson Differential Revision: https://reviews.llvm.org/D107934
1 parent cb90e53 commit 3ccd041

File tree

2 files changed

+131
-8
lines changed

2 files changed

+131
-8
lines changed

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,17 +1561,28 @@ void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
15611561
ArrayRef<Constant *>{ConstantInt::get(IntPtrTy, 0),
15621562
ConstantInt::get(IntPtrTy, I)}),
15631563
F->getType());
1564-
if (Functions[I]->isExported()) {
1565-
if (IsJumpTableCanonical) {
1566-
ExportSummary->cfiFunctionDefs().insert(std::string(F->getName()));
1567-
} else {
1568-
GlobalAlias *JtAlias = GlobalAlias::create(
1569-
F->getValueType(), 0, GlobalValue::ExternalLinkage,
1570-
F->getName() + ".cfi_jt", CombinedGlobalElemPtr, &M);
1564+
1565+
const bool IsExported = Functions[I]->isExported();
1566+
if (!IsJumpTableCanonical) {
1567+
GlobalValue::LinkageTypes LT = IsExported
1568+
? GlobalValue::ExternalLinkage
1569+
: GlobalValue::InternalLinkage;
1570+
GlobalAlias *JtAlias = GlobalAlias::create(F->getValueType(), 0, LT,
1571+
F->getName() + ".cfi_jt",
1572+
CombinedGlobalElemPtr, &M);
1573+
if (IsExported)
15711574
JtAlias->setVisibility(GlobalValue::HiddenVisibility);
1575+
else
1576+
appendToUsed(M, {JtAlias});
1577+
}
1578+
1579+
if (IsExported) {
1580+
if (IsJumpTableCanonical)
1581+
ExportSummary->cfiFunctionDefs().insert(std::string(F->getName()));
1582+
else
15721583
ExportSummary->cfiFunctionDecls().insert(std::string(F->getName()));
1573-
}
15741584
}
1585+
15751586
if (!IsJumpTableCanonical) {
15761587
if (F->hasExternalWeakLinkage())
15771588
replaceWeakDeclarationWithJumpTablePtr(F, CombinedGlobalElemPtr,

llvm/test/LTO/X86/cfi_jt_aliases.ll

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
; Full LTO test
2+
; RUN: opt %s -o %t.bc
3+
; RUN: llvm-lto2 run -o %t.o %t.bc -save-temps \
4+
; RUN: -r=%t.bc,a,px \
5+
; RUN: -r=%t.bc,b,px \
6+
; RUN: -r=%t.bc,c,px \
7+
; RUN: -r=%t.bc,d,px
8+
; RUN: llvm-readelf --symbols %t.o.0 | grep \.cfi_jt | FileCheck --check-prefix=CHECK-FULL-RE %s
9+
; RUN: llvm-objdump -dr %t.o.0 | FileCheck --check-prefix=CHECK-FULL-OD %s
10+
; RUN: llvm-dis %t.o.0.4.opt.bc -o - | FileCheck --check-prefix=CHECK-USED %s
11+
; Thin LTO test
12+
; RUN: opt -thinlto-bc -thinlto-split-lto-unit %s -o %t.bc
13+
; RUN: llvm-lto2 run -o %t.o %t.bc \
14+
; RUN: -r=%t.bc,a,px \
15+
; RUN: -r=%t.bc,b,px \
16+
; RUN: -r=%t.bc,c,px \
17+
; RUN: -r=%t.bc,d,px
18+
; RUN: llvm-readelf --symbols %t.o.0 | grep \.cfi_jt | FileCheck --check-prefix=CHECK-THIN-RE %s
19+
; RUN: llvm-objdump -dr %t.o.0 | FileCheck --check-prefix=CHECK-THIN-OD %s
20+
21+
; CHECK-FULL-RE: FUNC LOCAL DEFAULT {{[0-9]+}} a.cfi_jt
22+
; CHECK-FULL-RE-NEXT: FUNC LOCAL DEFAULT {{[0-9]+}} b.cfi_jt
23+
; CHECK-FULL-RE-NEXT: FUNC LOCAL DEFAULT {{[0-9]+}} c.cfi_jt
24+
25+
; CHECK-THIN-RE: FUNC GLOBAL HIDDEN {{[0-9]+}} b.cfi_jt
26+
; CHECK-THIN-RE-NEXT: FUNC GLOBAL HIDDEN {{[0-9]+}} c.cfi_jt
27+
; CHECK-THIN-RE-NEXT: FUNC GLOBAL HIDDEN {{[0-9]+}} a.cfi_jt
28+
29+
; CHECK-FULL-OD: a.cfi_jt>:
30+
; CHECK-FULL-OD: jmp {{.*}} <a.cfi_jt
31+
; CHECK-FULL-OD-NEXT: R_X86_64_PLT32 .La$local
32+
; CHECK-FULL-OD: b.cfi_jt>:
33+
; CHECK-FULL-OD: jmp {{.*}} <b.cfi_jt
34+
; CHECK-FULL-OD-NEXT: R_X86_64_PLT32 .Lb$local
35+
; CHECK-FULL-OD: c.cfi_jt>:
36+
; CHECK-FULL-OD: jmp {{.*}} <c.cfi_jt
37+
; CHECK-FULL-OD-NEXT: R_X86_64_PLT32 .Lc$local
38+
39+
; CHECK-THIN-OD: b.cfi_jt>:
40+
; CHECK-THIN-OD: jmp {{.*}} <b.cfi_jt
41+
; CHECK-THIN-OD-NEXT: R_X86_64_PLT32 b
42+
; CHECK-THIN-OD: c.cfi_jt>:
43+
; CHECK-THIN-OD: jmp {{.*}} <c.cfi_jt
44+
; CHECK-THIN-OD-NEXT: R_X86_64_PLT32 c
45+
; CHECK-THIN-OD: a.cfi_jt>:
46+
; CHECK-THIN-OD: jmp {{.*}} <a.cfi_jt
47+
; CHECK-THIN-OD-NEXT: R_X86_64_PLT32 a
48+
49+
; CHECK-USED: @llvm.used = appending global [3 x i8*] [i8* bitcast (void ()* @a.cfi_jt to i8*), i8* bitcast (void ()* @b.cfi_jt to i8*), i8* bitcast (void ()* @c.cfi_jt to i8*)], section "llvm.metadata"
50+
51+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
52+
target triple = "x86_64-unknown-linux-gnu"
53+
54+
@f = internal global [4 x void ()*] [void ()* @a, void ()* @b, void ()* @c, void ()* null], align 16
55+
56+
define dso_local void @a() !type !5 !type !6 { ret void }
57+
define dso_local void @b() !type !5 !type !6 { ret void }
58+
define dso_local void @c() !type !5 !type !6 { ret void }
59+
60+
define dso_local void @d() !type !5 !type !6 {
61+
entry:
62+
%i = alloca i32, align 4
63+
store i32 0, i32* %i, align 4
64+
br label %for.cond
65+
66+
for.cond: ; preds = %for.inc, %entry
67+
%0 = load i32, i32* %i, align 4
68+
%idxprom = sext i32 %0 to i64
69+
%arrayidx = getelementptr inbounds [4 x void ()*], [4 x void ()*]* @f, i64 0, i64 %idxprom
70+
%1 = load void ()*, void ()** %arrayidx, align 8
71+
%tobool = icmp ne void ()* %1, null
72+
br i1 %tobool, label %for.body, label %for.end
73+
74+
for.body: ; preds = %for.cond
75+
%2 = load i32, i32* %i, align 4
76+
%idxprom1 = sext i32 %2 to i64
77+
%arrayidx2 = getelementptr inbounds [4 x void ()*], [4 x void ()*]* @f, i64 0, i64 %idxprom1
78+
%3 = load void ()*, void ()** %arrayidx2, align 8
79+
%4 = bitcast void ()* %3 to i8*, !nosanitize !7
80+
%5 = call i1 @llvm.type.test(i8* %4, metadata !"_ZTSFvvE"), !nosanitize !7
81+
br i1 %5, label %cont, label %trap, !nosanitize !7
82+
83+
trap: ; preds = %for.body
84+
call void @llvm.ubsantrap(i8 2), !nosanitize !7
85+
unreachable, !nosanitize !7
86+
87+
cont: ; preds = %for.body
88+
call void %3()
89+
br label %for.inc
90+
91+
for.inc: ; preds = %cont
92+
%6 = load i32, i32* %i, align 4
93+
%inc = add nsw i32 %6, 1
94+
store i32 %inc, i32* %i, align 4
95+
br label %for.cond
96+
97+
for.end: ; preds = %for.cond
98+
ret void
99+
}
100+
101+
declare i1 @llvm.type.test(i8*, metadata)
102+
declare void @llvm.ubsantrap(i8 immarg)
103+
104+
!llvm.module.flags = !{!0, !1, !2, !3}
105+
106+
!0 = !{i32 1, !"wchar_size", i32 4}
107+
!1 = !{i32 4, !"CFI Canonical Jump Tables", i32 0}
108+
!2 = !{i32 7, !"uwtable", i32 1}
109+
!3 = !{i32 7, !"frame-pointer", i32 2}
110+
!5 = !{i64 0, !"_ZTSFvvE"}
111+
!6 = !{i64 0, !"_ZTSFvvE.generalized"}
112+
!7 = !{}

0 commit comments

Comments
 (0)