Skip to content

Commit c2d7e96

Browse files
[clang] Fix non-deterministic infinite recursion... (#118288)
...in `ASTContext::getAutoTypeInternal` Given ```cpp template < typename > concept C1 = true; template < typename , auto > concept C2 = true; template < C1 auto V, C2< V > auto> struct S; ``` Both `C1 auto V` and `C2<V> auto` end on the set `AutoType`, the former being a template parameter for the latter. Since the hashing is not deterministic (i.e., pointers are hashed), every now and then, both will end on the same bucket. Given that `FoldingSet` recomputes the `FoldingSetID` for each node in the target bucket on lookup, this triggers an infinite recursion: 1. Look for `X` in `AutoTypes` 2. Let's assume it would be in bucket N, so it iterates over nodes in that bucket. Let's assume the first is `C2<V> auto`. 3. Computes the `FoldingSetID` for this one, which requires the profile of its template parameters, so they are visited. 4. In some frames below, we end on the same `FoldingSet`, and, by chance, `C1 auto V` would be in bucket N too. 5. But the first node in the bucket is `C2<V> auto` for which we need to profile `C1 auto V` 6. ... stack overflow! No step individually does anything wrong, but in general, `FoldingSet` seems not to be re-entrant, and this fact is hidden behind many nested calls. With this change, we store the `AutoType`s inside a `DenseMap` instead. The `FoldingSetID` is computed once only and then kept as the map's key, avoiding the need to do recursive lookups. We also now make sure the key for the inserted `AutoType` is the same as the key used for lookup. Before, this was not the case, and it caused also non-deterministic parsing errors. Fixes #110231
1 parent eadc0c9 commit c2d7e96

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,11 @@ class ASTContext : public RefCountedBase<ASTContext> {
245245
mutable llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes;
246246
mutable llvm::FoldingSet<DependentUnaryTransformType>
247247
DependentUnaryTransformTypes;
248-
mutable llvm::ContextualFoldingSet<AutoType, ASTContext&> AutoTypes;
248+
// An AutoType can have a dependency on another AutoType via its template
249+
// arguments. Since both dependent and dependency are on the same set,
250+
// we can end up in an infinite recursion when looking for a node if we used
251+
// a `FoldingSet`, since both could end up in the same bucket.
252+
mutable llvm::DenseMap<llvm::FoldingSetNodeID, AutoType *> AutoTypes;
249253
mutable llvm::FoldingSet<DeducedTemplateSpecializationType>
250254
DeducedTemplateSpecializationTypes;
251255
mutable llvm::FoldingSet<AtomicType> AtomicTypes;

clang/include/clang/AST/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6553,7 +6553,7 @@ class DeducedType : public Type {
65536553

65546554
/// Represents a C++11 auto or C++14 decltype(auto) type, possibly constrained
65556555
/// by a type-constraint.
6556-
class AutoType : public DeducedType, public llvm::FoldingSetNode {
6556+
class AutoType : public DeducedType {
65576557
friend class ASTContext; // ASTContext creates these
65586558

65596559
ConceptDecl *TypeConstraintConcept;

clang/lib/AST/ASTContext.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ enum FloatingRank {
112112
Ibm128Rank
113113
};
114114

115+
template <> struct llvm::DenseMapInfo<llvm::FoldingSetNodeID> {
116+
static FoldingSetNodeID getEmptyKey() { return FoldingSetNodeID{}; }
117+
118+
static FoldingSetNodeID getTombstoneKey() {
119+
FoldingSetNodeID id;
120+
for (size_t i = 0; i < sizeof(id) / sizeof(unsigned); ++i) {
121+
id.AddInteger(std::numeric_limits<unsigned>::max());
122+
}
123+
return id;
124+
}
125+
126+
static unsigned getHashValue(const FoldingSetNodeID &Val) {
127+
return Val.ComputeHash();
128+
}
129+
130+
static bool isEqual(const FoldingSetNodeID &LHS,
131+
const FoldingSetNodeID &RHS) {
132+
return LHS == RHS;
133+
}
134+
};
135+
115136
/// \returns The locations that are relevant when searching for Doc comments
116137
/// related to \p D.
117138
static SmallVector<SourceLocation, 2>
@@ -899,7 +920,7 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
899920
FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize),
900921
DependentTypeOfExprTypes(this_()), DependentDecltypeTypes(this_()),
901922
TemplateSpecializationTypes(this_()),
902-
DependentTemplateSpecializationTypes(this_()), AutoTypes(this_()),
923+
DependentTemplateSpecializationTypes(this_()),
903924
DependentBitIntTypes(this_()), SubstTemplateTemplateParmPacks(this_()),
904925
DeducedTemplates(this_()), ArrayParameterTypes(this_()),
905926
CanonTemplateTemplateParms(this_()), SourceMgr(SM), LangOpts(LOpts),
@@ -6294,12 +6315,14 @@ QualType ASTContext::getAutoTypeInternal(
62946315
return getAutoDeductType();
62956316

62966317
// Look in the folding set for an existing type.
6297-
void *InsertPos = nullptr;
62986318
llvm::FoldingSetNodeID ID;
6299-
AutoType::Profile(ID, *this, DeducedType, Keyword, IsDependent,
6300-
TypeConstraintConcept, TypeConstraintArgs);
6301-
if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
6302-
return QualType(AT, 0);
6319+
bool IsDeducedDependent =
6320+
!DeducedType.isNull() && DeducedType->isDependentType();
6321+
AutoType::Profile(ID, *this, DeducedType, Keyword,
6322+
IsDependent || IsDeducedDependent, TypeConstraintConcept,
6323+
TypeConstraintArgs);
6324+
if (auto const AT_iter = AutoTypes.find(ID); AT_iter != AutoTypes.end())
6325+
return QualType(AT_iter->getSecond(), 0);
63036326

63046327
QualType Canon;
63056328
if (!IsCanon) {
@@ -6314,10 +6337,6 @@ QualType ASTContext::getAutoTypeInternal(
63146337
Canon =
63156338
getAutoTypeInternal(QualType(), Keyword, IsDependent, IsPack,
63166339
CanonicalConcept, CanonicalConceptArgs, true);
6317-
// Find the insert position again.
6318-
[[maybe_unused]] auto *Nothing =
6319-
AutoTypes.FindNodeOrInsertPos(ID, InsertPos);
6320-
assert(!Nothing && "canonical type broken");
63216340
}
63226341
}
63236342
}
@@ -6331,8 +6350,13 @@ QualType ASTContext::getAutoTypeInternal(
63316350
: TypeDependence::None) |
63326351
(IsPack ? TypeDependence::UnexpandedPack : TypeDependence::None),
63336352
Canon, TypeConstraintConcept, TypeConstraintArgs);
6353+
#ifndef NDEBUG
6354+
llvm::FoldingSetNodeID InsertedID;
6355+
AT->Profile(InsertedID, *this);
6356+
assert(InsertedID == ID && "ID does not match");
6357+
#endif
63346358
Types.push_back(AT);
6335-
AutoTypes.InsertNode(AT, InsertPos);
6359+
AutoTypes.try_emplace(ID, AT);
63366360
return QualType(AT, 0);
63376361
}
63386362

clang/test/Parser/gh110231.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: seq 100 | xargs -Ifoo %clang_cc1 -std=c++20 -fsyntax-only -verify %s
2+
// expected-no-diagnostics
3+
// This is a regression test for a non-deterministic stack-overflow.
4+
5+
template < typename >
6+
concept C1 = true;
7+
8+
template < typename , auto >
9+
concept C2 = true;
10+
11+
template < C1 auto V, C2< V > auto>
12+
struct S;

0 commit comments

Comments
 (0)