Skip to content

Commit 0603664

Browse files
committed
[clang][PAC] Enable the PAC dynamic_cast to final class optimization
Update the codegen for the the dynamic_cast to a final class optimization when pointer authentication is enabled.
1 parent 82046c7 commit 0603664

File tree

5 files changed

+170
-3
lines changed

5 files changed

+170
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ Bug Fixes to C++ Support
184184
(``[[assume(expr)]]``) creates temporary objects.
185185
- Fix the dynamic_cast to final class optimization to correctly handle
186186
casts that are guaranteed to fail (#GH137518).
187+
- Support the dynamic_cast to final class optimization with pointer
188+
authentication enabled.
187189

188190
Bug Fixes to AST Handling
189191
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,8 +2292,7 @@ llvm::Value *CodeGenFunction::EmitDynamicCast(Address ThisAddr,
22922292
bool IsExact = !IsDynamicCastToVoid &&
22932293
CGM.getCodeGenOpts().OptimizationLevel > 0 &&
22942294
DestRecordTy->getAsCXXRecordDecl()->isEffectivelyFinal() &&
2295-
CGM.getCXXABI().shouldEmitExactDynamicCast(DestRecordTy) &&
2296-
!getLangOpts().PointerAuthCalls;
2295+
CGM.getCXXABI().shouldEmitExactDynamicCast(DestRecordTy);
22972296

22982297
std::optional<CGCXXABI::ExactDynamicCastInfo> ExactCastInfo;
22992298
if (IsExact) {

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,14 @@ llvm::Value *ItaniumCXXABI::emitExactDynamicCast(
17451745
llvm::BasicBlock *CastFail) {
17461746
const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl();
17471747
const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl();
1748+
auto AuthenticateVTable = [&](Address ThisAddr, const CXXRecordDecl *Decl) {
1749+
if (!CGF.getLangOpts().PointerAuthCalls)
1750+
return;
1751+
(void)CGF.GetVTablePtr(ThisAddr, CGF.UnqualPtrTy, Decl,
1752+
CodeGenFunction::VTableAuthMode::MustTrap);
1753+
};
17481754

1755+
bool PerformPostCastAuthentication = false;
17491756
llvm::Value *VTable = nullptr;
17501757
if (ExactCastInfo.RequiresCastToPrimaryBase) {
17511758
// Base appears in at least two different places. Find the most-derived
@@ -1756,8 +1763,16 @@ llvm::Value *ItaniumCXXABI::emitExactDynamicCast(
17561763
emitDynamicCastToVoid(CGF, ThisAddr, SrcRecordTy);
17571764
ThisAddr = Address(PrimaryBase, CGF.VoidPtrTy, ThisAddr.getAlignment());
17581765
SrcDecl = DestDecl;
1766+
// This unauthenticated load is unavoidable, so we're relying on the
1767+
// authenticated load in the dynamic cast to void, and we'll manually
1768+
// authenticate the resulting v-table at the end of the cast check.
1769+
PerformPostCastAuthentication = CGF.getLangOpts().PointerAuthCalls;
1770+
CGPointerAuthInfo StrippingAuthInfo(0, PointerAuthenticationMode::Strip,
1771+
false, false, nullptr);
17591772
Address VTablePtrPtr = ThisAddr.withElementType(CGF.VoidPtrPtrTy);
17601773
VTable = CGF.Builder.CreateLoad(VTablePtrPtr, "vtable");
1774+
if (PerformPostCastAuthentication)
1775+
VTable = CGF.EmitPointerAuthAuth(StrippingAuthInfo, VTable);
17611776
} else
17621777
VTable = CGF.GetVTablePtr(ThisAddr, CGF.UnqualPtrTy, SrcDecl);
17631778

@@ -1774,8 +1789,32 @@ llvm::Value *ItaniumCXXABI::emitExactDynamicCast(
17741789
llvm::ConstantInt::get(CGF.PtrDiffTy, -Offset);
17751790
AdjustedThisPtr = CGF.Builder.CreateInBoundsGEP(CGF.CharTy, AdjustedThisPtr,
17761791
OffsetConstant);
1792+
PerformPostCastAuthentication = CGF.getLangOpts().PointerAuthCalls;
17771793
}
17781794

1795+
if (PerformPostCastAuthentication) {
1796+
// If we've changed the object pointer we authenticate the vtable pointer
1797+
// of the resulting object.
1798+
llvm::BasicBlock *NonNullBlock = CGF.Builder.GetInsertBlock();
1799+
llvm::BasicBlock *PostCastAuthSuccess =
1800+
CGF.createBasicBlock("dynamic_cast.postauth.success");
1801+
llvm::BasicBlock *PostCastAuthComplete =
1802+
CGF.createBasicBlock("dynamic_cast.postauth.complete");
1803+
CGF.Builder.CreateCondBr(Success, PostCastAuthSuccess,
1804+
PostCastAuthComplete);
1805+
CGF.EmitBlock(PostCastAuthSuccess);
1806+
Address AdjustedThisAddr =
1807+
Address(AdjustedThisPtr, CGF.IntPtrTy, CGF.getPointerAlign());
1808+
AuthenticateVTable(AdjustedThisAddr, DestDecl);
1809+
CGF.EmitBranch(PostCastAuthComplete);
1810+
CGF.EmitBlock(PostCastAuthComplete);
1811+
llvm::PHINode *PHI = CGF.Builder.CreatePHI(AdjustedThisPtr->getType(), 2);
1812+
PHI->addIncoming(AdjustedThisPtr, PostCastAuthSuccess);
1813+
llvm::Value *NullValue =
1814+
llvm::Constant::getNullValue(AdjustedThisPtr->getType());
1815+
PHI->addIncoming(NullValue, NonNullBlock);
1816+
AdjustedThisPtr = PHI;
1817+
}
17791818
CGF.Builder.CreateCondBr(Success, CastSuccess, CastFail);
17801819
return AdjustedThisPtr;
17811820
}

clang/test/CodeGenCXX/dynamic-cast-exact-disabled.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// RUN: %clang_cc1 -I%S %s -triple x86_64-apple-darwin10 -O1 -fvisibility=hidden -emit-llvm -std=c++11 -o - | FileCheck %s --check-prefixes=CHECK,INEXACT
44
// RUN: %clang_cc1 -I%S %s -triple x86_64-apple-darwin10 -O1 -fapple-kext -emit-llvm -std=c++11 -o - | FileCheck %s --check-prefixes=CHECK,INEXACT
55
// RUN: %clang_cc1 -I%S %s -triple x86_64-apple-darwin10 -O1 -fno-assume-unique-vtables -emit-llvm -std=c++11 -o - | FileCheck %s --check-prefixes=CHECK,INEXACT
6-
// RUN: %clang_cc1 -I%S %s -triple arm64e-apple-darwin10 -O1 -fptrauth-calls -emit-llvm -std=c++11 -o - | FileCheck %s --check-prefixes=CHECK,INEXACT
76

87
struct A { virtual ~A(); };
98
struct B final : A { };
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// RUN: %clang_cc1 -I%S %s -triple arm64e-apple-darwin10 -O1 -fptrauth-calls -fptrauth-vtable-pointer-address-discrimination -fptrauth-vtable-pointer-type-discrimination -emit-llvm -std=c++11 -o - | FileCheck %s --check-prefixes=CHECK
2+
3+
struct A {
4+
virtual ~A();
5+
};
6+
struct B {
7+
int foo;
8+
virtual ~B();
9+
};
10+
struct C final : A, B {
11+
virtual void f(){};
12+
};
13+
struct D final : B, A {
14+
virtual void f(){};
15+
};
16+
17+
struct Offset {
18+
virtual ~Offset();
19+
};
20+
struct E {
21+
virtual ~E();
22+
};
23+
struct F final : Offset, E {
24+
};
25+
struct G {
26+
virtual ~G();
27+
int g;
28+
};
29+
struct H : E {
30+
int h;
31+
};
32+
struct I : E {
33+
int i;
34+
};
35+
struct J : virtual E {
36+
int j;
37+
};
38+
struct K : virtual E {
39+
int k;
40+
};
41+
struct L final : G, H, I, J, K {
42+
int l;
43+
};
44+
struct M final: G, private H { int m; };
45+
46+
// CHECK-LABEL: @_Z10exact_to_CP1A
47+
C *exact_to_C(A *a) {
48+
// CHECK: [[UNAUTHED_VPTR:%.*]] = load ptr, ptr %a, align 8
49+
// CHECK: [[VPTR_ADDRI:%.*]] = ptrtoint ptr %a to i64
50+
// CHECK: [[VPTR_ADDR_DISC:%.*]] = tail call i64 @llvm.ptrauth.blend(i64 [[VPTR_ADDRI]], i64 62866)
51+
// CHECK: [[UNAUTHED_VPTRI:%.*]] = ptrtoint ptr [[UNAUTHED_VPTR]] to i64
52+
// CHECK: [[AUTHED_VPTRI:%.*]] = tail call i64 @llvm.ptrauth.auth(i64 [[UNAUTHED_VPTRI]], i32 2, i64 [[VPTR_ADDR_DISC]])
53+
// CHECK: [[IS_EXPECTED:%.*]] = icmp eq i64 [[AUTHED_VPTRI]], ptrtoint (ptr getelementptr inbounds nuw inrange(-16, 24) (i8, ptr @_ZTV1C, i64 16) to i64)
54+
// CHECK: br i1 [[IS_EXPECTED]], label %dynamic_cast.end, label %dynamic_cast.null
55+
// CHECK: [[NULL_CHECKED_RESULT:%.*]] = phi ptr [ %a, %dynamic_cast.notnull ], [ null, %dynamic_cast.null ]
56+
// CHECK: ret ptr [[NULL_CHECKED_RESULT]]
57+
return dynamic_cast<C*>(a);
58+
}
59+
60+
// CHECK-LABEL: @_Z9exact_t_DP1A
61+
D *exact_t_D(A *a) {
62+
// CHECK: dynamic_cast.notnull:
63+
// CHECK: [[SRC_UNAUTHED_VPTR:%.*]] = load ptr, ptr %a
64+
// CHECK: [[SRC_VPTR_ADDRI:%.*]] = ptrtoint ptr %a to i64
65+
// CHECK: [[SRC_VPTR_DISC:%.*]] = tail call i64 @llvm.ptrauth.blend(i64 [[SRC_VPTR_ADDRI]], i64 62866)
66+
// CHECK: [[SRC_UNAUTHED_VPTRI:%.*]] = ptrtoint ptr [[SRC_UNAUTHED_VPTR]] to i64
67+
// CHECK: [[SRC_AUTHED_VPTRI:%.*]] = tail call i64 @llvm.ptrauth.auth(i64 [[SRC_UNAUTHED_VPTRI]], i32 2, i64 [[SRC_VPTR_DISC]])
68+
// CHECK: [[SUCCESS:%.*]] = icmp eq i64 [[SRC_AUTHED_VPTRI]], ptrtoint (ptr getelementptr inbounds nuw inrange(-16, 16) (i8, ptr @_ZTV1D, i64 56) to i64)
69+
// CHECK: br i1 [[SUCCESS]], label %dynamic_cast.postauth.success, label %dynamic_cast.postauth.complete
70+
// CHECK: dynamic_cast.postauth.success:
71+
// CHECK: [[ADJUSTED_THIS:%.*]] = getelementptr inbounds i8, ptr %a, i64 -16
72+
// CHECK: [[ADJUSTED_UNAUTHED_VPTR:%.*]] = load ptr, ptr [[ADJUSTED_THIS]]
73+
// CHECK: [[ADJUSTED_VPTR_ADDRI:%.*]] = ptrtoint ptr [[ADJUSTED_THIS]] to i64
74+
// CHECK: [[ADJUSTED_VPTR_DISC:%.*]] = tail call i64 @llvm.ptrauth.blend(i64 [[ADJUSTED_VPTR_ADDRI]], i64 28965)
75+
// CHECK: [[ADJUSTED_UNAUTHED_VPTRI:%.*]] = ptrtoint ptr [[ADJUSTED_UNAUTHED_VPTR]] to i64
76+
// CHECK: [[ADJUSTED_AUTHED_VPTRI:%.*]] = tail call i64 @llvm.ptrauth.auth(i64 [[ADJUSTED_UNAUTHED_VPTRI]], i32 2, i64 [[ADJUSTED_VPTR_DISC]])
77+
// CHECK: [[ADJUSTED_AUTHED_VPTR:%.*]] = inttoptr i64 [[ADJUSTED_AUTHED_VPTRI]] to ptr
78+
// CHECK: br label %dynamic_cast.postauth.complete
79+
// CHECK: dynamic_cast.postauth.complete:
80+
// CHECK: [[AUTHED_ADJUSTED_THIS:%.*]] = phi ptr [ [[ADJUSTED_THIS]], %dynamic_cast.postauth.success ], [ null, %dynamic_cast.notnull ]
81+
// CHECK: br i1 [[SUCCESS]], label %dynamic_cast.end, label %dynamic_cast.null
82+
// CHECK: dynamic_cast.null:
83+
// CHECK: br label %dynamic_cast.end
84+
// CHECK: dynamic_cast.end:
85+
// CHECK: [[RESULT:%.*]] = phi ptr [ [[AUTHED_ADJUSTED_THIS]], %dynamic_cast.postauth.complete ], [ null, %dynamic_cast.null ]
86+
// CHECK: ret ptr [[RESULT]]
87+
return dynamic_cast<D*>(a);
88+
}
89+
90+
// CHECK-LABEL: @_Z11exact_multiP1E
91+
L *exact_multi(E *e) {
92+
// CHECK: dynamic_cast.notnull:
93+
// CHECK: [[VTABLE_ADDR:%.*]] = load ptr, ptr %e, align 8
94+
// CHECK: [[THIS_ADDRI:%.*]] = ptrtoint ptr %e to i64
95+
// CHECK: [[VTABLE_DISC:%.*]] = tail call i64 @llvm.ptrauth.blend(i64 [[THIS_ADDRI]], i64 12810)
96+
// CHECK: [[VTABLE_ADDRI:%.*]] = ptrtoint ptr [[VTABLE_ADDR]] to i64
97+
// CHECK: [[AUTHED_VTABLEI:%.*]] = tail call i64 @llvm.ptrauth.auth(i64 [[VTABLE_ADDRI]], i32 2, i64 [[VTABLE_DISC]])
98+
// CHECK: [[AUTHED_VTABLE:%.*]] = inttoptr i64 [[AUTHED_VTABLEI]] to ptr
99+
// CHECK: [[PRIMARY_BASE_OFFSET:%.*]] = getelementptr inbounds i8, ptr [[AUTHED_VTABLE]], i64 -16
100+
// CHECK: %offset.to.top = load i64, ptr [[PRIMARY_BASE_OFFSET]]
101+
// CHECK: [[ADJUSTED_THIS:%.*]] = getelementptr inbounds i8, ptr %e, i64 %offset.to.top
102+
// CHECK: [[ADJUSTED_THIS_VTABLE:%.*]] = load ptr, ptr [[ADJUSTED_THIS]]
103+
// CHECK: [[ADJUSTED_THIS_VTABLEI:%.*]] = ptrtoint ptr [[ADJUSTED_THIS_VTABLE]] to i64
104+
// CHECK: [[ADJUSTED_THIS_STRIPPED_VTABLEI:%.*]] = tail call i64 @llvm.ptrauth.strip(i64 [[ADJUSTED_THIS_VTABLEI]], i32 0)
105+
// CHECK: [[SUCCESS:%.*]] = icmp eq i64 [[ADJUSTED_THIS_STRIPPED_VTABLEI]], ptrtoint (ptr getelementptr inbounds nuw inrange(-24, 16) (i8, ptr @_ZTV1L, i64 24) to i64)
106+
// CHECK: br i1 [[SUCCESS]], label %dynamic_cast.postauth.success, label %dynamic_cast.postauth.complete
107+
// CHECK: dynamic_cast.postauth.success:
108+
// CHECK: [[ADJUSTED_THISI:%.*]] = ptrtoint ptr [[ADJUSTED_THIS]] to i64
109+
// CHECK: [[DEST_DISC:%.*]] = tail call i64 @llvm.ptrauth.blend(i64 [[ADJUSTED_THISI]], i64 41434)
110+
// CHECK: tail call i64 @llvm.ptrauth.auth(i64 [[ADJUSTED_THIS_VTABLEI]], i32 2, i64 [[DEST_DISC]])
111+
// CHECK: br label %dynamic_cast.postauth.complete
112+
// CHECK: dynamic_cast.postauth.complete:
113+
// CHECK: [[AUTHED_ADJUSTED_THIS:%.*]] = phi ptr [ [[ADJUSTED_THIS]], %dynamic_cast.postauth.success ], [ null, %dynamic_cast.notnull ]
114+
// CHECK: br i1 [[SUCCESS]], label %dynamic_cast.end, label %dynamic_cast.null
115+
// CHECK: dynamic_cast.null:
116+
// CHECK: br label %dynamic_cast.end
117+
// CHECK: dynamic_cast.end:
118+
// CHECK: [[RESULT:%.*]] = phi ptr [ [[AUTHED_ADJUSTED_THIS]], %dynamic_cast.postauth.complete ], [ null, %dynamic_cast.null ]
119+
// CHECK: ret ptr [[RESULT]]
120+
return dynamic_cast<L*>(e);
121+
}
122+
123+
// CHECK-LABEL: @_Z19exact_invalid_multiP1H
124+
M *exact_invalid_multi(H* d) {
125+
// CHECK: entry:
126+
// CHECK-NEXT: ret ptr null
127+
return dynamic_cast<M*>(d);
128+
}

0 commit comments

Comments
 (0)