Skip to content

Commit 5dc1e5f

Browse files
committed
[clang][CGObjC] Sign the v-table pointer in ObjC exception RTTI.
1 parent 91a2056 commit 5dc1e5f

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Non-comprehensive list of changes in this release
191191
- Support parsing the `cc` operand modifier and alias it to the `c` modifier (#GH127719).
192192
- Added `__builtin_elementwise_exp10`.
193193
- For AMDPGU targets, added `__builtin_v_cvt_off_f32_i4` that maps to the `v_cvt_off_f32_i4` instruction.
194+
- Support authenticated ``type_info`` vtable pointers in Objective-C++
194195

195196
New Compiler Flags
196197
------------------

clang/include/clang/CodeGen/ConstantInitBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ class ConstantAggregateBuilderBase {
206206
void addSignedPointer(llvm::Constant *Pointer,
207207
const PointerAuthSchema &Schema, GlobalDecl CalleeDecl,
208208
QualType CalleeType);
209+
void addSignedPointer(llvm::Constant *Pointer, unsigned Key,
210+
bool UseAddressDiscrimination,
211+
llvm::ConstantInt *OtherDiscriminator);
209212

210213
/// Add a null pointer of a specific type.
211214
void addNullPointer(llvm::PointerType *ptrTy) {

clang/lib/CodeGen/CGObjCMac.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7676,10 +7676,26 @@ CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
76767676
}
76777677

76787678
llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
7679+
llvm::Constant *VTablePtr = llvm::ConstantExpr::getInBoundsGetElementPtr(
7680+
VTableGV->getValueType(), VTableGV, VTableIdx);
7681+
76797682
ConstantInitBuilder builder(CGM);
76807683
auto values = builder.beginStruct(ObjCTypes.EHTypeTy);
7681-
values.add(llvm::ConstantExpr::getInBoundsGetElementPtr(
7682-
VTableGV->getValueType(), VTableGV, VTableIdx));
7684+
7685+
if (auto &Schema =
7686+
CGM.getCodeGenOpts().PointerAuth.CXXTypeInfoVTablePointer) {
7687+
uint32_t discrimination = 0;
7688+
if (Schema.hasOtherDiscrimination()) {
7689+
assert(Schema.getOtherDiscrimination() ==
7690+
PointerAuthSchema::Discrimination::Constant);
7691+
discrimination = Schema.getConstantDiscrimination();
7692+
}
7693+
values.addSignedPointer(
7694+
VTablePtr, Schema.getKey(), Schema.isAddressDiscriminated(),
7695+
llvm::ConstantInt::get(CGM.IntPtrTy, discrimination));
7696+
} else {
7697+
values.add(VTablePtr);
7698+
}
76837699
values.add(GetClassName(ClassName));
76847700
values.add(GetClassGlobal(ID, /*metaclass*/ false, NotForDefinition));
76857701

clang/lib/CodeGen/ConstantInitBuilder.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,15 @@ void ConstantAggregateBuilderBase::addSignedPointer(
314314
Pointer, Schema, StorageAddress, CalleeDecl, CalleeType);
315315
add(SignedPointer);
316316
}
317+
318+
void ConstantAggregateBuilderBase::addSignedPointer(
319+
llvm::Constant *Pointer, unsigned Key, bool UseAddressDiscrimination,
320+
llvm::ConstantInt *OtherDiscriminator) {
321+
llvm::Constant *StorageAddress = nullptr;
322+
if (UseAddressDiscrimination)
323+
StorageAddress = getAddrOfCurrentPosition(Pointer->getType());
324+
325+
llvm::Constant *SignedPointer = Builder.CGM.getConstantSignedPointer(
326+
Pointer, Key, StorageAddress, OtherDiscriminator);
327+
add(SignedPointer);
328+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-calls -emit-llvm -fexceptions -fobjc-exceptions -o - %s | FileCheck %s
2+
3+
__attribute__((objc_root_class))
4+
@interface Root {
5+
Class isa;
6+
}
7+
@end
8+
9+
__attribute__((objc_exception))
10+
@interface A : Root
11+
@end
12+
13+
@implementation A
14+
@end
15+
16+
// CHECK: @"OBJC_EHTYPE_$_A" = global {{%.*}} { ptr ptrauth (ptr getelementptr inbounds (ptr, ptr @objc_ehtype_vtable, i32 2), i32 2),

0 commit comments

Comments
 (0)