Skip to content

Commit 2fe5f9c

Browse files
committed
[clang] Sign the destructor pointer passed to __cxa_throw.
1 parent 329c38e commit 2fe5f9c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,8 +1472,16 @@ void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) {
14721472
if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
14731473
CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
14741474
if (!Record->hasTrivialDestructor()) {
1475+
// __cxa_throw is declared to take its destructor as void (*)(void *). We
1476+
// must match that if function pointers can be authenticated with a
1477+
// discriminator based on their type.
1478+
ASTContext &Ctx = getContext();
1479+
QualType DtorTy = Ctx.getFunctionType(Ctx.VoidTy, {Ctx.VoidPtrTy},
1480+
FunctionProtoType::ExtProtoInfo());
1481+
14751482
CXXDestructorDecl *DtorD = Record->getDestructor();
14761483
Dtor = CGM.getAddrOfCXXStructor(GlobalDecl(DtorD, Dtor_Complete));
1484+
Dtor = CGM.getFunctionPointer(Dtor, DtorTy);
14771485
Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy);
14781486
}
14791487
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -fptrauth-function-pointer-type-discrimination -triple arm64-apple-ios -fptrauth-calls -fcxx-exceptions -emit-llvm -no-enable-noundef-analysis %s -o - | FileCheck %s
2+
3+
class Foo {
4+
public:
5+
~Foo() {
6+
}
7+
};
8+
9+
void f() {
10+
throw Foo();
11+
}
12+
13+
// __cxa_throw is defined to take its destructor as "void (*)(void *)" in the ABI.
14+
void __cxa_throw(void *exception, void *, void (*dtor)(void *)) {
15+
dtor(exception);
16+
}
17+
18+
// CHECK: @_ZN3FooD1Ev.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @_ZN3FooD1Ev, i32 0, i64 0, i64 [[DISC:[0-9]+]] }, section "llvm.ptrauth", align 8
19+
20+
// CHECK: define void @_Z1fv()
21+
// CHECK: call void @__cxa_throw(ptr %{{.*}}, ptr @_ZTI3Foo, ptr @_ZN3FooD1Ev.ptrauth)
22+
23+
// CHECK: call void {{%.*}}(ptr {{%.*}}) [ "ptrauth"(i32 0, i64 [[DISC]]) ]

0 commit comments

Comments
 (0)