Skip to content

Commit 5599e6f

Browse files
committed
[PAC][clang] Do not attempt to get definition of non-instantiated template
This patch fixes a crash which we hit after llvm#154490. The reproducer is provided in a test case. The root cause of the crash is as follows. `primaryBaseHasAddressDiscriminatedVTableAuthentication` requires a non-null CXX record decl definition which might not be available if we are dealing with a non-instantiated template. It might be the case if the template was not instantiated due to a prior fatal error. See the corresponding check in the `Sema::InstantiatingTemplate` constructor. Previously, we tried to call `isPolymorphic()` on a `CXXRecordDecl` with null `DefinitionData`, which led to a crash. With this patch, we abort execution of `ASTContext::findPointerAuthContent` before call to `primaryBaseHasAddressDiscriminatedVTableAuthentication` if we have a prior fatal error which might have led to non-instantiated templates.
1 parent 7b190b7 commit 5599e6f

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ void ASTContext::setRelocationInfoForCXXRecord(
16181618
RelocatableClasses.insert({D, Info});
16191619
}
16201620

1621-
static bool primaryBaseHaseAddressDiscriminatedVTableAuthentication(
1621+
static bool primaryBaseHasAddressDiscriminatedVTableAuthentication(
16221622
const ASTContext &Context, const CXXRecordDecl *Class) {
16231623
if (!Class->isPolymorphic())
16241624
return false;
@@ -1672,7 +1672,14 @@ ASTContext::findPointerAuthContent(QualType T) const {
16721672
return Result != PointerAuthContent::AddressDiscriminatedData;
16731673
};
16741674
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1675-
if (primaryBaseHaseAddressDiscriminatedVTableAuthentication(*this, CXXRD) &&
1675+
// `primaryBaseHasAddressDiscriminatedVTableAuthentication` requires a
1676+
// non-null CXX record decl definition which might not be available if we
1677+
// are dealing with a non-instantiated template. It might be the case if the
1678+
// template was not instantiated due to a prior fatal error. See the
1679+
// corresponding check in the `Sema::InstantiatingTemplate` constructor.
1680+
if (getDiagnostics().hasFatalErrorOccurred())
1681+
return PointerAuthContent::None;
1682+
if (primaryBaseHasAddressDiscriminatedVTableAuthentication(*this, CXXRD) &&
16761683
!ShouldContinueAfterUpdate(
16771684
PointerAuthContent::AddressDiscriminatedVTable))
16781685
return SaveResultAndReturn();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clang_cc1 -fptrauth-intrinsics -fsyntax-only -ferror-limit 1 -verify -std=c++03 %s
2+
3+
/// Force two errors so we hit the error limit leading to skip of template instantiation
4+
# "" // expected-error {{invalid preprocessing directive}}
5+
# ""
6+
// expected-error@* {{too many errors emitted}}
7+
8+
template <typename>
9+
struct a {};
10+
11+
struct b {
12+
b(int) {}
13+
void c() {
14+
/// Trigger the following call stack:
15+
/// ...
16+
/// clang::ASTContext::findPointerAuthContent(clang::QualType) const /path/to/llvm-project/clang/lib/AST/ASTContext.cpp
17+
/// clang::ASTContext::containsAddressDiscriminatedPointerAuth(clang::QualType) const /path/to/llvm-project/clang/lib/AST/ASTContext.cpp
18+
/// clang::QualType::isCXX{11|98}PODType(clang::ASTContext const&) const /path/to/llvm-project/clang/lib/AST/Type.cpp
19+
/// clang::QualType::isPODType(clang::ASTContext const&) const /path/to/llvm-project/clang/lib/AST/Type.cpp
20+
/// SelfReferenceChecker /path/to/llvm-project/clang/lib/Sema/SemaDecl.cpp
21+
/// CheckSelfReference /path/to/llvm-project/clang/lib/Sema/SemaDecl.cpp
22+
/// clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) /path/to/llvm-project/clang/lib/Sema/SemaDecl.cpp
23+
/// ...
24+
b d(0);
25+
}
26+
a<int> e;
27+
};
28+

0 commit comments

Comments
 (0)