llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[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

Open ICSE-27 opened this issue 2 months ago • 3 comments

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).

ICSE-27 avatar Sep 28 '26 04:09 ICSE-27

@llvm/issue-subscribers-clang-codegen

Author: ICSE-27

## 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&lt;const clang::Expr*, llvm::Value*&gt; VLA-size map — the inserted Expr* key appears to dangle after template substitution.

Reproducer

template&lt;int&amp; T&gt;
void FuncTemplate() { (void)T; }

template&lt;int i&gt; class A {};
template&lt;int i&gt; void g(A&lt;i&gt; &amp;) {
    []()
    {
        static int InternalVar = 43;
        FuncTemplate&lt;InternalVar&gt;();
    }();
}

int main() {
  A&lt;1&gt; 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&lt;llvm::DenseMap&lt;clang::Expr const*, llvm::Value*, ...&gt;&gt;::moveFromOldBuckets(...) llvm/include/llvm/ADT/DenseMap.h:452:32
#<!-- -->8  llvm::DenseMap&lt;clang::Expr const*, llvm::Value*, ...&gt;::grow(unsigned int) llvm/include/llvm/ADT/DenseMap.h:856:29
#<!-- -->10 llvm::DenseMapBase&lt;...&gt;::InsertIntoBucketImpl&lt;clang::Expr const*&gt;(...) 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.

EugeneZelenko avatar Sep 28 '26 04:09 EugeneZelenko

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.

MagentaTreehouse avatar Sep 28 '26 13:09 MagentaTreehouse