Skip to content

Commit 306481b

Browse files
ahatanaktru
authored andcommitted
[PAC] Fix codegen for polymorphic class variables with consteval constructors (#154858)
Fix a bug in CodeGen where such variables could cause a compilation error or be emitted with an undef initializer when the vtable was signed with address discrimination. rdar://155696134 (cherry picked from commit e612f37)
1 parent d618b40 commit 306481b

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,9 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
873873

874874
for (const BaseInfo &Base : Bases) {
875875
bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
876-
Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
877-
VTableClass, Offset + Base.Offset);
876+
if (!Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
877+
VTableClass, Offset + Base.Offset))
878+
return false;
878879
}
879880
}
880881

@@ -1620,7 +1621,7 @@ llvm::Constant *ConstantEmitter::tryEmitConstantExpr(const ConstantExpr *CE) {
16201621
if (CE->isGLValue())
16211622
RetType = CGM.getContext().getLValueReferenceType(RetType);
16221623

1623-
return emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(), RetType);
1624+
return tryEmitAbstract(CE->getAPValueResult(), RetType);
16241625
}
16251626

16261627
llvm::Constant *

clang/test/CodeGenCXX/ptrauth-explicit-vtable-pointer-control.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
1+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
22
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,NODISC %s
33

4-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
4+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
55
// RUN: -fptrauth-vtable-pointer-type-discrimination \
66
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,TYPE %s
77

8-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
8+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
99
// RUN: -fptrauth-vtable-pointer-address-discrimination \
1010
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,ADDR %s
1111

12-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
12+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple arm64-apple-ios -fptrauth-calls -fptrauth-intrinsics \
1313
// RUN: -fptrauth-vtable-pointer-type-discrimination \
1414
// RUN: -fptrauth-vtable-pointer-address-discrimination \
1515
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,BOTH %s
1616

17-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
17+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
1818
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,NODISC %s
1919

20-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
20+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
2121
// RUN: -fptrauth-vtable-pointer-type-discrimination \
2222
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,TYPE %s
2323

24-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
24+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
2525
// RUN: -fptrauth-vtable-pointer-address-discrimination \
2626
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,ADDR %s
2727

28-
// RUN: %clang_cc1 %s -x c++ -std=c++11 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
28+
// RUN: %clang_cc1 %s -x c++ -std=c++20 -triple aarch64-linux-gnu -fptrauth-calls -fptrauth-intrinsics \
2929
// RUN: -fptrauth-vtable-pointer-type-discrimination \
3030
// RUN: -fptrauth-vtable-pointer-address-discrimination \
3131
// RUN: -emit-llvm -o - | FileCheck --check-prefixes=CHECK,BOTH %s
@@ -78,6 +78,27 @@ struct authenticated(default_key, default_address_discrimination, custom_discrim
7878
virtual void g();
7979
};
8080

81+
// CHECK: @_ZTVN5test19ConstEvalE = external unnamed_addr constant { [3 x ptr] }, align 8
82+
// CHECK: @_ZN5test12ceE = global %{{.*}} { ptr ptrauth (ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTVN5test19ConstEvalE, i32 0, i32 0, i32 2), i32 2, i64 0, ptr @_ZN5test12ceE) }, align 8
83+
// CHECK: @_ZTVN5test116ConstEvalDerivedE = linkonce_odr unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTIN5test116ConstEvalDerivedE, ptr ptrauth (ptr @_ZN5test19ConstEval1fEv, i32 0, i64 26259, ptr getelementptr inbounds ({ [3 x ptr] }, ptr @_ZTVN5test116ConstEvalDerivedE, i32 0, i32 0, i32 2))] },{{.*}}align 8
84+
// CHECK: @_ZN5test13cedE = global { ptr } { ptr ptrauth (ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTVN5test116ConstEvalDerivedE, i32 0, i32 0, i32 2), i32 2, i64 0, ptr @_ZN5test13cedE) }, align 8
85+
86+
struct authenticated(default_key, address_discrimination, no_extra_discrimination) ConstEval {
87+
consteval ConstEval() {}
88+
virtual void f();
89+
};
90+
91+
// clang used to bail out with error message "could not emit constant value abstractly".
92+
ConstEval ce;
93+
94+
struct ConstEvalDerived : public ConstEval {
95+
public:
96+
consteval ConstEvalDerived() {}
97+
};
98+
99+
// clang used to emit an undef initializer.
100+
ConstEvalDerived ced;
101+
81102
template <typename T>
82103
struct SubClass : T {
83104
virtual void g();

0 commit comments

Comments
 (0)