Skip to content

Commit 0689d23

Browse files
authored
[C++20][Modules] Prevent premature calls to PassInterestingDeclsToConsumer() within FinishedDeserializing(). (#129982)
`ASTReader::FinishedDeserializing` uses `NumCurrentElementsDeserializing` to keep track of nested `Deserializing` RAII actions. The `FinishedDeserializing` only performs actions if it is the top-level `Deserializing` layer. This works fine in general, but there is a problematic edge case. If a call to `redecls()` in `FinishedDeserializing` performs deserialization, we re-enter `FinishedDeserializing` while in the middle of the previous `FinishedDeserializing` call. The known problematic part of this is that this inner `FinishedDeserializing` can go all the way to `PassInterestingDeclsToConsumer`, which operates on `PotentiallyInterestingDecls` data structure which contain decls that should be handled by the previous `FinishedDeserializing` stage. The other shared data structures are also somewhat concerning at a high-level in that the inner `FinishedDeserializing` would be handling pending actions that are not "within its scope", but this part is not known to be problematic. We already have a guard within `PassInterestingDeclsToConsumer` because we can end up with recursive deserialization within `PassInterestingDeclsToConsumer`. The implemented solution is to apply this guard to the portion of `FinishedDeserializing` that performs further deserialization as well. This ensures that recursive deserialization does not trigger `PassInterestingDeclsToConsumer` which may operate on entries that are not ready to be passed.
1 parent 508db53 commit 0689d23

File tree

6 files changed

+296
-56
lines changed

6 files changed

+296
-56
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ Bug Fixes in This Version
278278
considered an error in C23 mode and are allowed as an extension in earlier language modes.
279279

280280
- Remove the ``static`` specifier for the value of ``_FUNCTION_`` for static functions, in MSVC compatibility mode.
281+
- Fixed a modules crash where exception specifications were not propagated properly (#GH121245, relanded in #GH129982)
282+
- Fixed a problematic case with recursive deserialization within ``FinishedDeserializing()`` where
283+
``PassInterestingDeclsToConsumer()`` was called before the declarations were safe to be passed. (#GH129982)
281284

282285
Bug Fixes to Compiler Builtins
283286
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Serialization/ASTReader.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,11 @@ class ASTReader
11761176
/// Number of Decl/types that are currently deserializing.
11771177
unsigned NumCurrentElementsDeserializing = 0;
11781178

1179-
/// Set true while we are in the process of passing deserialized
1180-
/// "interesting" decls to consumer inside FinishedDeserializing().
1181-
/// This is used as a guard to avoid recursively repeating the process of
1179+
/// Set false while we are in a state where we cannot safely pass deserialized
1180+
/// "interesting" decls to the consumer inside FinishedDeserializing().
1181+
/// This is used as a guard to avoid recursively entering the process of
11821182
/// passing decls to consumer.
1183-
bool PassingDeclsToConsumer = false;
1183+
bool CanPassDeclsToConsumer = true;
11841184

11851185
/// The set of identifiers that were read while the AST reader was
11861186
/// (recursively) loading declarations.

clang/lib/Serialization/ASTReader.cpp

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10182,12 +10182,12 @@ void ASTReader::visitTopLevelModuleMaps(
1018210182
}
1018310183

1018410184
void ASTReader::finishPendingActions() {
10185-
while (
10186-
!PendingIdentifierInfos.empty() || !PendingDeducedFunctionTypes.empty() ||
10187-
!PendingDeducedVarTypes.empty() || !PendingIncompleteDeclChains.empty() ||
10188-
!PendingDeclChains.empty() || !PendingMacroIDs.empty() ||
10189-
!PendingDeclContextInfos.empty() || !PendingUpdateRecords.empty() ||
10190-
!PendingObjCExtensionIvarRedeclarations.empty()) {
10185+
while (!PendingIdentifierInfos.empty() ||
10186+
!PendingDeducedFunctionTypes.empty() ||
10187+
!PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10188+
!PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10189+
!PendingUpdateRecords.empty() ||
10190+
!PendingObjCExtensionIvarRedeclarations.empty()) {
1019110191
// If any identifiers with corresponding top-level declarations have
1019210192
// been loaded, load those declarations now.
1019310193
using TopLevelDeclsMap =
@@ -10235,13 +10235,6 @@ void ASTReader::finishPendingActions() {
1023510235
}
1023610236
PendingDeducedVarTypes.clear();
1023710237

10238-
// For each decl chain that we wanted to complete while deserializing, mark
10239-
// it as "still needs to be completed".
10240-
for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
10241-
markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
10242-
}
10243-
PendingIncompleteDeclChains.clear();
10244-
1024510238
// Load pending declaration chains.
1024610239
for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
1024710240
loadPendingDeclChain(PendingDeclChains[I].first,
@@ -10479,6 +10472,43 @@ void ASTReader::finishPendingActions() {
1047910472
for (auto *ND : PendingMergedDefinitionsToDeduplicate)
1048010473
getContext().deduplicateMergedDefinitonsFor(ND);
1048110474
PendingMergedDefinitionsToDeduplicate.clear();
10475+
10476+
// For each decl chain that we wanted to complete while deserializing, mark
10477+
// it as "still needs to be completed".
10478+
for (Decl *D : PendingIncompleteDeclChains)
10479+
markIncompleteDeclChain(D);
10480+
PendingIncompleteDeclChains.clear();
10481+
10482+
assert(PendingIdentifierInfos.empty() &&
10483+
"Should be empty at the end of finishPendingActions");
10484+
assert(PendingDeducedFunctionTypes.empty() &&
10485+
"Should be empty at the end of finishPendingActions");
10486+
assert(PendingDeducedVarTypes.empty() &&
10487+
"Should be empty at the end of finishPendingActions");
10488+
assert(PendingDeclChains.empty() &&
10489+
"Should be empty at the end of finishPendingActions");
10490+
assert(PendingMacroIDs.empty() &&
10491+
"Should be empty at the end of finishPendingActions");
10492+
assert(PendingDeclContextInfos.empty() &&
10493+
"Should be empty at the end of finishPendingActions");
10494+
assert(PendingUpdateRecords.empty() &&
10495+
"Should be empty at the end of finishPendingActions");
10496+
assert(PendingObjCExtensionIvarRedeclarations.empty() &&
10497+
"Should be empty at the end of finishPendingActions");
10498+
assert(PendingFakeDefinitionData.empty() &&
10499+
"Should be empty at the end of finishPendingActions");
10500+
assert(PendingDefinitions.empty() &&
10501+
"Should be empty at the end of finishPendingActions");
10502+
assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&
10503+
"Should be empty at the end of finishPendingActions");
10504+
assert(PendingBodies.empty() &&
10505+
"Should be empty at the end of finishPendingActions");
10506+
assert(PendingAddedClassMembers.empty() &&
10507+
"Should be empty at the end of finishPendingActions");
10508+
assert(PendingMergedDefinitionsToDeduplicate.empty() &&
10509+
"Should be empty at the end of finishPendingActions");
10510+
assert(PendingIncompleteDeclChains.empty() &&
10511+
"Should be empty at the end of finishPendingActions");
1048210512
}
1048310513

1048410514
void ASTReader::diagnoseOdrViolations() {
@@ -10792,47 +10822,54 @@ void ASTReader::FinishedDeserializing() {
1079210822
--NumCurrentElementsDeserializing;
1079310823

1079410824
if (NumCurrentElementsDeserializing == 0) {
10795-
// Propagate exception specification and deduced type updates along
10796-
// redeclaration chains.
10797-
//
10798-
// We do this now rather than in finishPendingActions because we want to
10799-
// be able to walk the complete redeclaration chains of the updated decls.
10800-
while (!PendingExceptionSpecUpdates.empty() ||
10801-
!PendingDeducedTypeUpdates.empty() ||
10802-
!PendingUndeducedFunctionDecls.empty()) {
10803-
auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10804-
PendingExceptionSpecUpdates.clear();
10805-
for (auto Update : ESUpdates) {
10806-
ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10807-
auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10808-
auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10809-
if (auto *Listener = getContext().getASTMutationListener())
10810-
Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
10811-
for (auto *Redecl : Update.second->redecls())
10812-
getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
10813-
}
10825+
{
10826+
// Guard variable to avoid recursively entering the process of passing
10827+
// decls to consumer.
10828+
SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer, false);
1081410829

10815-
auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10816-
PendingDeducedTypeUpdates.clear();
10817-
for (auto Update : DTUpdates) {
10818-
ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10819-
// FIXME: If the return type is already deduced, check that it matches.
10820-
getContext().adjustDeducedFunctionResultType(Update.first,
10821-
Update.second);
10822-
}
10830+
// Propagate exception specification and deduced type updates along
10831+
// redeclaration chains.
10832+
//
10833+
// We do this now rather than in finishPendingActions because we want to
10834+
// be able to walk the complete redeclaration chains of the updated decls.
10835+
while (!PendingExceptionSpecUpdates.empty() ||
10836+
!PendingDeducedTypeUpdates.empty() ||
10837+
!PendingUndeducedFunctionDecls.empty()) {
10838+
auto ESUpdates = std::move(PendingExceptionSpecUpdates);
10839+
PendingExceptionSpecUpdates.clear();
10840+
for (auto Update : ESUpdates) {
10841+
ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10842+
auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
10843+
auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
10844+
if (auto *Listener = getContext().getASTMutationListener())
10845+
Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
10846+
for (auto *Redecl : Update.second->redecls())
10847+
getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
10848+
}
1082310849

10824-
auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
10825-
PendingUndeducedFunctionDecls.clear();
10826-
// We hope we can find the deduced type for the functions by iterating
10827-
// redeclarations in other modules.
10828-
for (FunctionDecl *UndeducedFD : UDTUpdates)
10829-
(void)UndeducedFD->getMostRecentDecl();
10830-
}
10850+
auto DTUpdates = std::move(PendingDeducedTypeUpdates);
10851+
PendingDeducedTypeUpdates.clear();
10852+
for (auto Update : DTUpdates) {
10853+
ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
10854+
// FIXME: If the return type is already deduced, check that it
10855+
// matches.
10856+
getContext().adjustDeducedFunctionResultType(Update.first,
10857+
Update.second);
10858+
}
1083110859

10832-
if (ReadTimer)
10833-
ReadTimer->stopTimer();
10860+
auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);
10861+
PendingUndeducedFunctionDecls.clear();
10862+
// We hope we can find the deduced type for the functions by iterating
10863+
// redeclarations in other modules.
10864+
for (FunctionDecl *UndeducedFD : UDTUpdates)
10865+
(void)UndeducedFD->getMostRecentDecl();
10866+
}
10867+
10868+
if (ReadTimer)
10869+
ReadTimer->stopTimer();
1083410870

10835-
diagnoseOdrViolations();
10871+
diagnoseOdrViolations();
10872+
}
1083610873

1083710874
// We are not in recursive loading, so it's safe to pass the "interesting"
1083810875
// decls to the consumer.

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,12 +4309,12 @@ Decl *ASTReader::ReadDeclRecord(GlobalDeclID ID) {
43094309
void ASTReader::PassInterestingDeclsToConsumer() {
43104310
assert(Consumer);
43114311

4312-
if (PassingDeclsToConsumer)
4312+
if (!CanPassDeclsToConsumer)
43134313
return;
43144314

43154315
// Guard variable to avoid recursively redoing the process of passing
43164316
// decls to consumer.
4317-
SaveAndRestore GuardPassingDeclsToConsumer(PassingDeclsToConsumer, true);
4317+
SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer, false);
43184318

43194319
// Ensure that we've loaded all potentially-interesting declarations
43204320
// that need to be eagerly loaded.

clang/test/Modules/pr121245.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// If this test fails, it should be investigated under Debug builds.
2+
// Before the PR, this test was encountering an `llvm_unreachable()`.
3+
4+
// RUN: rm -rf %t
5+
// RUN: mkdir -p %t
6+
// RUN: split-file %s %t
7+
// RUN: cd %t
8+
9+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-01.h \
10+
// RUN: -fcxx-exceptions -o %t/hu-01.pcm
11+
12+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-02.h \
13+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
14+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-02.pcm
15+
16+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-03.h \
17+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
18+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-03.pcm
19+
20+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-04.h \
21+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
22+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-04.pcm
23+
24+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-05.h \
25+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
26+
// RUN: -fmodule-file=%t/hu-03.pcm -fmodule-file=%t/hu-04.pcm \
27+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-05.pcm
28+
29+
// RUN: %clang_cc1 -std=c++20 -emit-obj %t/main.cpp \
30+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
31+
// RUN: -fmodule-file=%t/hu-02.pcm -fmodule-file=%t/hu-05.pcm \
32+
// RUN: -fmodule-file=%t/hu-04.pcm -fmodule-file=%t/hu-03.pcm \
33+
// RUN: -fmodule-file=%t/hu-01.pcm
34+
35+
//--- hu-01.h
36+
template <typename T>
37+
struct A {
38+
A() {}
39+
~A() {}
40+
};
41+
42+
template <typename T>
43+
struct EBO : T {
44+
EBO() = default;
45+
};
46+
47+
template <typename T>
48+
struct HT : EBO<A<T>> {};
49+
50+
//--- hu-02.h
51+
import "hu-01.h";
52+
53+
inline void f() {
54+
HT<int>();
55+
}
56+
57+
//--- hu-03.h
58+
import "hu-01.h";
59+
60+
struct C {
61+
C();
62+
63+
HT<long> _;
64+
};
65+
66+
//--- hu-04.h
67+
import "hu-01.h";
68+
69+
void g(HT<long> = {});
70+
71+
//--- hu-05.h
72+
import "hu-03.h";
73+
import "hu-04.h";
74+
import "hu-01.h";
75+
76+
struct B {
77+
virtual ~B() = default;
78+
79+
virtual void f() {
80+
HT<long>();
81+
}
82+
};
83+
84+
//--- main.cpp
85+
import "hu-02.h";
86+
import "hu-05.h";
87+
import "hu-03.h";
88+
89+
int main() {
90+
f();
91+
C();
92+
B();
93+
}

0 commit comments

Comments
 (0)