[clang++] Crash in CodeGen getVLASize (DenseMap<const Expr*, Value*>) when a local static inside a lambda in a template function is used as a reference NTTP
Description
Clang crashes in code generation (release build without assertions) on the
following valid C++ program (GCC 13 accepts it cleanly). A function-local
static declared inside a lambda nested in a function template is bound to a
reference non-type template parameter; the crash occurs in
clang::CodeGen::CodeGenFunction::getVLASize while growing the
DenseMap<const clang::Expr*, llvm::Value*> VLA-size map — the inserted
Expr* key appears to dangle after template substitution.
Reproducer
template<int& T>
void FuncTemplate() { (void)T; }
template<int i> class A {};
template<int i> void g(A<i> &) {
[]()
{
static int InternalVar = 43;
FuncTemplate<InternalVar>();
}();
}
int main() {
A<1> a;
g(a);
}
clang++ -c repro.cpp
Expected / Observed
Expected: compiles successfully. Observed: crash with stack dump at every optimization level and every language mode (default, c++17/20/23). Top frames:
#7 llvm::DenseMapBase<llvm::DenseMap<clang::Expr const*, llvm::Value*, ...>>::moveFromOldBuckets(...) llvm/include/llvm/ADT/DenseMap.h:452:32
#8 llvm::DenseMap<clang::Expr const*, llvm::Value*, ...>::grow(unsigned int) llvm/include/llvm/ADT/DenseMap.h:856:29
#10 llvm::DenseMapBase<...>::InsertIntoBucketImpl<clang::Expr const*>(...) llvm/include/llvm/ADT/DenseMap.h:624:17
#14 clang::CodeGen::CodeGenFunction::getVLASize(clang::VariableArrayType const*) clang/lib/CodeGen/CodeGenFunction.cpp:2322:58
Versions
- Clang 19.1.0 (x86_64-unknown-linux-gnu, assertions off) — crashes
- Clang 19.1.7 (Ubuntu) — crashes
- GCC 13.1 — compiles successfully
Related existing issues
#63438 (crash when a lambda fails to capture a VLA bound, open since 2023) is in the same getVLASize/lambda-capture area but a different mechanism — there is no template/NTTP substitution involved there. No existing report matches this stack (DenseMap<const Expr*, Value*> corruption during instantiation of a reference NTTP from a lambda-local static).
@llvm/issue-subscribers-clang-codegen
Author: ICSE-27
Clang crashes in code generation (release build without assertions) on the
following valid C++ program (GCC 13 accepts it cleanly). A function-local
static declared inside a lambda nested in a function template is bound to a
reference non-type template parameter; the crash occurs in
clang::CodeGen::CodeGenFunction::getVLASize while growing the
DenseMap<const clang::Expr*, llvm::Value*> VLA-size map — the inserted
Expr* key appears to dangle after template substitution.
Reproducer
template<int& T>
void FuncTemplate() { (void)T; }
template<int i> class A {};
template<int i> void g(A<i> &) {
[]()
{
static int InternalVar = 43;
FuncTemplate<InternalVar>();
}();
}
int main() {
A<1> a;
g(a);
}
clang++ -c repro.cpp
Expected / Observed
Expected: compiles successfully. Observed: crash with stack dump at every optimization level and every language mode (default, c++17/20/23). Top frames:
#<!-- -->7 llvm::DenseMapBase<llvm::DenseMap<clang::Expr const*, llvm::Value*, ...>>::moveFromOldBuckets(...) llvm/include/llvm/ADT/DenseMap.h:452:32
#<!-- -->8 llvm::DenseMap<clang::Expr const*, llvm::Value*, ...>::grow(unsigned int) llvm/include/llvm/ADT/DenseMap.h:856:29
#<!-- -->10 llvm::DenseMapBase<...>::InsertIntoBucketImpl<clang::Expr const*>(...) llvm/include/llvm/ADT/DenseMap.h:624:17
#<!-- -->14 clang::CodeGen::CodeGenFunction::getVLASize(clang::VariableArrayType const*) clang/lib/CodeGen/CodeGenFunction.cpp:2322:58
Versions
- Clang 19.1.0 (x86_64-unknown-linux-gnu, assertions off) — crashes
- Clang 19.1.7 (Ubuntu) — crashes
- GCC 13.1 — compiles successfully
Related existing issues
#63438 (crash when a lambda fails to capture a VLA bound, open since 2023) is in the same getVLASize/lambda-capture area but a different mechanism — there is no template/NTTP substitution involved there. No existing report matches this stack (DenseMap<const Expr*, Value*> corruption during instantiation of a reference NTTP from a lambda-local static).
@ICSE-27: Is problem exist in 23 or main? Only most recent release (23 as of now) is maintained.
Crashes in main. Also reduced a bit:
template<int& T>
void FuncTemplate() { (void)T; }
template<class> auto g = [] {
static int InternalVar = 43;
FuncTemplate<InternalVar>();
};
int main() {
g<int>();
}
See https://compiler-explorer.com/z/95d7fG1G7.