[clang] 716c2fd - [C++20][Modules] Preserve cleanups in default argument updates (#240633)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 22:29:46 PST 2026
Author: Davis Rollman
Date: 2026-11-30T14:29:40+08:00
New Revision: 716c2fdaea4d0daf924dadc34c086e16b88b07a5
URL: https://github.com/llvm/llvm-project/commit/716c2fdaea4d0daf924dadc34c086e16b88b07a5
DIFF: https://github.com/llvm/llvm-project/commit/716c2fdaea4d0daf924dadc34c086e16b88b07a5.diff
LOG: [C++20][Modules] Preserve cleanups in default argument updates (#240633)
Ok, this fixes a header units bug for us, but relates to all
serialization, incl PCH.
Clang can miscompile a call that uses a default argument instantiated in
another module or PCH. We hit this with a function-local `static
std::string`: an exception after initialization reset its guard, so the
next call constructed it again and registered a second destructor,
causing a double free at exit when using sanitizers.
The problem is in `CXXInstantiatedDefaultArgument` updates. These record
default arguments instantiated outside the AST file that declared the
parameter. The writer uses `getDefaultArg()`, which strips the outer
`ExprWithCleanups`. The importing TU then misses the full-expression
cleanup boundary. A temporary's cleanup stays on the stack too long, and
`EmitGuardedInit` pops it instead of the guard-abort cleanup. That
leaves `__cxa_guard_abort` active after the static has been initialized.
Use `getInit()` instead to preserve the stored expression, including its
cleanup marker. This matches the normal parameter-serialization path.
This also removes the FIXME left by #69076 for the serialization bug
behind #68702. The constant evaluator's `FullExpressionRAII` workaround
is unchanged.
### Reproducer
The first header declares the parameter, the second instantiates its
default argument, and the TU imports both:
```cpp
// a.h
struct Alloc { Alloc(); ~Alloc(); };
template <typename T> struct S {
S(const char *, const Alloc & = Alloc());
~S();
};
inline const unsigned sizeof_S_int = sizeof(S<int>);
// b.h
import "a.h";
inline void instantiate_default_arg() { S<int> s("in-b"); }
// t.cpp
import "a.h";
import "b.h";
void mayThrow();
S<int> &v0() {
static const S<int> s = "in-t";
mayThrow();
return const_cast<S<int> &>(s);
}
```
On x86-64 Linux:
```sh
clang++ -std=c++20 -xc++-user-header --precompile a.h -o a.pcm
clang++ -std=c++20 -fmodule-file=a.pcm -xc++-user-header --precompile b.h -o b.pcm
clang++ -std=c++20 -fmodule-file=a.pcm -fmodule-file=b.pcm -S -emit-llvm -o - t.cpp
```
Without the fix, `mayThrow()` is an `invoke` whose unwind path calls
`__cxa_guard_abort` for the already-initialized `s`. With the fix, it is
a plain `call`. The initializer also retains its `ExprWithCleanups` in
the AST.
### Testing
The new header-unit and chained-PCH tests cover the static guard and the
destruction order of a default-argument temporary in an ordinary local
declaration. Both fail without the writer change and pass with it;
`Modules/pr68702.cpp` still passes.
### AI usage
I used both gpt 6 astra + claude fable 5.1 while working on this PR.
Added:
clang/test/Modules/cxx20-hu-instantiated-default-arg-cleanups.cpp
Modified:
clang/docs/ReleaseNotes.md
clang/lib/AST/ExprConstant.cpp
clang/lib/Serialization/ASTWriter.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6bbc910d86b5aa..4f91549f85bd85 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1360,6 +1360,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
serialized PCH/AST files and `-Wunused-local-typedef` diagnostics
non-reproducible across runs. (#GH209639)
+- Fixed temporaries in a default argument being destroyed too late when the
+ default argument was instantiated in one module or PCH and used from another.
+ (#GH68702)
+
- `FunctionDecl::getReturnTypeSourceRange()` now returns correct source
location of a trailing return type. (#GH162649)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3ead1325191ea1..67647639beac99 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -22743,10 +22743,6 @@ static bool doEvaluateInitializer(const ASTContext &Ctx, SemaProxy *SProxy,
// mem-initializer.
// So we need to make sure temporary objects are destroyed after having
// evaluated the expression (per C++23 [class.temporary]/p4).
- //
- // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the
- // serialization code calls ParmVarDecl::getDefaultArg() which strips the
- // outermost FullExpr, such as ExprWithCleanups.
FullExpressionRAII Scope(Info);
if (!EvaluateInPlace(EStatus.Val, Info, LVal, E,
/*AllowNonLiteralTypes=*/true) ||
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 5dafb9aa6d8861..0051dd462700cf 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -6695,8 +6695,7 @@ void ASTWriter::WriteDeclUpdatesBlocks(ASTContext &Context,
break;
case DeclUpdateKind::CXXInstantiatedDefaultArgument:
- Record.writeStmtRef(
- cast<ParmVarDecl>(Update.getDecl())->getDefaultArg());
+ Record.writeStmtRef(cast<ParmVarDecl>(Update.getDecl())->getInit());
break;
case DeclUpdateKind::CXXInstantiatedDefaultMemberInitializer:
diff --git a/clang/test/Modules/cxx20-hu-instantiated-default-arg-cleanups.cpp b/clang/test/Modules/cxx20-hu-instantiated-default-arg-cleanups.cpp
new file mode 100644
index 00000000000000..ef47e4ba8acdf3
--- /dev/null
+++ b/clang/test/Modules/cxx20-hu-instantiated-default-arg-cleanups.cpp
@@ -0,0 +1,81 @@
+// A default argument instantiated for a parameter that another module declared
+// is written to the PCM as a declaration update. The update must keep the
+// initializer's ExprWithCleanups: importers (Sema::CheckCXXDefaultArgExpr) rely
+// on it to mark the full-expression of their own calls as needing cleanups.
+// Without it the temporary's destructor cleanup outlives the full-expression,
+// and for a guarded static local the guard-abort cleanup is left active for the
+// rest of the function.
+//
+// The copy-initialization below relies on C++17 guaranteed copy elision:
+// before that, the S<int> temporary has cleanups of its own that mask the
+// missing marker. Direct-initialization shows the bug in every standard.
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+// RUN: cd %t
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -fexceptions \
+// RUN: -fcxx-exceptions -emit-header-unit -xc++-user-header hu-a.h \
+// RUN: -o hu-a.pcm
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -fexceptions \
+// RUN: -fcxx-exceptions -emit-header-unit -xc++-user-header hu-b.h \
+// RUN: -fmodule-file=hu-a.pcm -o hu-b.pcm
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -fexceptions \
+// RUN: -fcxx-exceptions -fmodule-file=hu-a.pcm -fmodule-file=hu-b.pcm \
+// RUN: -ast-dump -ast-dump-filter=use_default_arg use.cpp | \
+// RUN: FileCheck --check-prefix=CHECK-AST %s
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -fexceptions \
+// RUN: -fcxx-exceptions -fmodule-file=hu-a.pcm -fmodule-file=hu-b.pcm \
+// RUN: -emit-llvm -o - use.cpp | FileCheck --check-prefix=CHECK-IR %s
+
+//--- hu-a.h
+struct Alloc {
+ Alloc();
+ ~Alloc();
+};
+template <typename T>
+struct S {
+ S(const char *, const Alloc & = Alloc());
+ ~S();
+};
+// Instantiate S<int> here, but not the default argument, so that the
+// parameter declaration belongs to this header unit.
+inline const unsigned sizeof_S_int = sizeof(S<int>);
+
+//--- hu-b.h
+import "hu-a.h";
+// Instantiates the default argument of a parameter owned by hu-a.h; the
+// instantiation is written to hu-b.pcm as a declaration update.
+inline void instantiate_default_arg() { S<int> s("in-b"); }
+
+//--- use.cpp
+import "hu-a.h";
+import "hu-b.h";
+void may_throw();
+S<int> &use_default_arg() {
+ static const S<int> s = "in-use";
+ may_throw();
+ return const_cast<S<int> &>(s);
+}
+void use_local() {
+ S<int> s("local");
+ may_throw();
+}
+
+// CHECK-AST: VarDecl {{.*}} s 'const S<int>' static cinit
+// CHECK-AST-NEXT: ExprWithCleanups
+
+// Static local: the temporary is destroyed inside the guarded region and the
+// guard-abort cleanup ends with it, so the next statement needs no landing pad.
+// CHECK-IR-LABEL: define {{.*}} @_Z15use_default_argv(
+// CHECK-IR: call void @_ZN5AllocD1Ev(
+// CHECK-IR-NEXT: call i32 @__cxa_atexit(
+// CHECK-IR-NEXT: call void @__cxa_guard_release(
+// CHECK-IR: call void @_Z9may_throwv()
+// CHECK-IR-NEXT: ret
+//
+// Plain local: the temporary is destroyed at the end of the declaration,
+// before the next statement.
+// CHECK-IR-LABEL: define {{.*}} @_Z9use_localv(
+// CHECK-IR: call void @_ZN5AllocD1Ev(
+// CHECK-IR-NEXT: invoke void @_Z9may_throwv()
More information about the cfe-commits
mailing list