LeakSanitizer detected memory leak of ConstraintSatisfaction placement new-ed by clang::MakeDeductionFailureInfo
LeakSanitizer: detected memory leaks . Vulnerability location: malloc (/home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/llvm/bin/clang-20+0x105632ae)
Steps to reproduce:
- Download the source code git clone https://github.com/ARM-software/LLVM-embedded-toolchain-for-Arm.git
- Give commands:
cd LLVM-embedded-toolchain-for-Arm mkdir build_sanitizer; cd build_sanitizer; export CCACHE_DISABLE=1 cmake .. -GNinja -DFETCHCONTENT_QUIET=OFF -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER="Address;Undefined" -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ninja llvm-toolchain ninja check-llvm-toolchain ninja check-cxx
Expected behavior We do not observe any memory leaks.
Observed result We observe memory leaks. Complete snippet is:
`================================================================ ==4061127==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 2808 byte(s) in 13 object(s) allocated from:
#0 0x55b9889852ae in malloc (/home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/llvm/bin/clang-20+0x105632ae) (BuildId: a3166faab09d81d8)
#1 0x55b98c3b00e3 in safe_malloc /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/llvm/include/llvm/Support/MemAlloc.h:26:18
#2 0x55b98c3b00e3 in llvm::SmallVectorBase
SUMMARY: AddressSanitizer: 2808 byte(s) leaked in 13 allocation(s). error: command failed with exit status: 1`
@llvm/issue-subscribers-clang-frontend
Author: Vrukesh Panse (vrukesh)
Steps to reproduce:
- Download the source code git clone https://github.com/ARM-software/LLVM-embedded-toolchain-for-Arm.git
- Give commands:
cd LLVM-embedded-toolchain-for-Arm mkdir build_sanitizer; cd build_sanitizer; export CCACHE_DISABLE=1 cmake .. -GNinja -DFETCHCONTENT_QUIET=OFF -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER="Address;Undefined" -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ninja llvm-toolchain ninja check-llvm-toolchain ninja check-cxx
Expected behavior We do not observe any memory leaks.
Observed result We observe memory leaks. Complete snippet is:
`================================================================ ==4061127==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 2808 byte(s) in 13 object(s) allocated from: #0 0x55b9889852ae in malloc (/home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/llvm/bin/clang-20+0x105632ae) (BuildId: a3166faab09d81d8) #1 0x55b98c3b00e3 in safe_malloc /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/llvm/include/llvm/Support/MemAlloc.h:26:18 #2 0x55b98c3b00e3 in llvm::SmallVectorBase<unsigned int>::grow_pod(void*, unsigned long, unsigned long) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/llvm/lib/Support/SmallVector.cpp:143:15 #3 0x55b993a2e90c in grow_pod /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/llvm/include/llvm/ADT/SmallVector.h:151:11 #4 0x55b993a2e90c in grow /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/llvm/include/llvm/ADT/SmallVector.h:538:41 #5 0x55b993a2e90c in llvm::SmallVectorImpl<clang::TemplateArgument>::operator=(llvm::SmallVectorImpl<clang::TemplateArgument> const&) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/llvm/include/llvm/ADT/SmallVector.h:1049:11 #6 0x55b993a11c5e in operator= /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/llvm/include/llvm/ADT/SmallVector.h:1255:25 #7 0x55b993a11c5e in clang::ConstraintSatisfaction::operator=(clang::ConstraintSatisfaction const&) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/include/clang/AST/ASTConcept.h:35:7 #8 0x55b994e9b798 in clang::MakeDeductionFailureInfo(clang::ASTContext&, clang::TemplateDeductionResult, clang::sema::TemplateDeductionInfo&) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Sema/SemaOverload.cpp:777:25 #9 0x55b994ed3fa9 in clang::Sema::AddMethodTemplateCandidate(clang::FunctionTemplateDecl*, clang::DeclAccessPair, clang::CXXRecordDecl*, clang::TemplateArgumentListInfo*, clang::QualType, clang::Expr::Classification, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool, bool, clang::OverloadCandidateParamOrder) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Sema/SemaOverload.cpp:7621:36 #10 0x55b994ed5818 in clang::Sema::AddMethodCandidate(clang::DeclAccessPair, clang::QualType, clang::Expr::Classification, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool, clang::OverloadCandidateParamOrder) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Sema/SemaOverload.cpp:7375:5 #11 0x55b994f26ffb in clang::Sema::BuildCallToObjectOfClassType(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Sema/SemaOverload.cpp:15584:5 #12 0x55b99421b195 in clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Sema/SemaExpr.cpp:6447:14 #13 0x55b99425cb8a in clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Sema/SemaExpr.cpp:6360:7 #14 0x55b993301764 in clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult<clang::Expr*, true>) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:2243:23 #15 0x55b99330813d in clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:1945:9 #16 0x55b993308558 in clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:1232:18 #17 0x55b9932ff216 in clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, clang::Parser::TypeCastState, bool, bool*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:712:20 #18 0x55b993314424 in clang::Parser::ParseParenExpression(clang::Parser::ParenParseOption&, bool, bool, clang::OpaquePtr<clang::QualType>&, clang::SourceLocation&) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:3265:18 #19 0x55b99330aa78 in clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:1093:11 #20 0x55b9932ff216 in clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, clang::Parser::TypeCastState, bool, bool*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:712:20 #21 0x55b9932fb8a1 in clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:182:20 #22 0x55b9932fb722 in clang::Parser::ParseExpression(clang::Parser::TypeCastState) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseExpr.cpp:133:18 #23 0x55b99344d64c in clang::Parser::ParseExprStatement(clang::Parser::ParsedStmtContext) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseStmt.cpp:563:19 #24 0x55b9934497ef in clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseStmt.cpp:292:14 #25 0x55b9934487ab in clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseStmt.cpp:124:20 #26 0x55b99345d5e1 in clang::Parser::ParseCompoundStatementBody(bool) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseStmt.cpp:1257:11 #27 0x55b993460f7a in clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseStmt.cpp:2535:21 #28 0x55b9932a99ef in clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/Parser.cpp:1525:10 #29 0x55b9933b6364 in clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/ParseDecl.cpp:2471:17 #30 0x55b9932a6bc6 in clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/Parser.cpp:1249:10 #31 0x55b9932a597d in clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/Parser.cpp:1271:12 #32 0x55b9932a31d0 in clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/Parser.cpp:1074:14 #33 0x55b99329edcf in clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) /home/vrukesh/LLVM-embedded-toolchain-for-Arm/build_sanitizer/_deps/llvmproject-src/clang/lib/Parse/Parser.cpp:763:12
SUMMARY: AddressSanitizer: 2808 byte(s) leaked in 13 allocation(s). error: command failed with exit status: 1`
Log snippet from latest code:
==3763537==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 5832 byte(s) in 27 object(s) allocated from:
#0 0xae5147e3f0f4 in malloc /home/ubuntu/arm-toolchain/compiler-rt/lib/asan/asan_malloc_linux.cpp:67:3
#1 0xae514b0e8328 in safe_malloc /home/ubuntu/arm-toolchain/llvm/include/llvm/Support/MemAlloc.h:26:18
#2 0xae514b0e8328 in llvm::SmallVectorBase
SUMMARY: AddressSanitizer: 5832 byte(s) leaked in 27 allocation(s). ==3763500==LeakSanitizer: checking for leaks ==3765630==SuspendAllThreads retry: 0
error: command failed with exit status: 1
Attaching the detailed logs.
Issue is reproducible with llvm-project and libcxx tests.
`cd /home/ubuntu/llvm-project/ git pull
Build the compiler rm -Rf build_sanitizer; mkdir build_sanitizer; cd build_sanitizer;
cmake -G Ninja ../llvm -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_ENABLE_ASSERTIONS=True -DLLVM_LIT_ARGS="-v" -DCMAKE_INSTALL_PREFIX=../stage1.install -DLLVM_TARGETS_TO_BUILD=AArch64 -DLLVM_ENABLE_PROJECTS="clang-tools-extra;clang;llvm" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt"
ninja -j64
Build libcpp cd /home/ubuntu/llvm-project/ rm -Rf build_libcxx; mkdir build_libcxx; cd build_libcxx; cmake -G Ninja ../runtimes -DLLVM_TARGETS_TO_BUILD=AArch64 -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" -DCMAKE_C_COMPILER=/home/ubuntu/llvm-project/build_sanitizer/bin/clang -DCMAKE_CXX_COMPILER=/home/ubuntu/llvm-project/build_sanitizer/bin/clang++
ninja -j64
Compile clang with sanitizers cd /home/ubuntu/llvm-project/ rm -Rf build_clang_with_libcxx; mkdir build_clang_with_libcxx; cd build_clang_with_libcxx;
export LD_LIBRARY_PATH="/home/ubuntu/llvm-project/build_libcxx/lib:$LD_LIBRARY_PATH"
cmake -G Ninja ../llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_SANITIZER="Address;Undefined" -DCMAKE_C_COMPILER=/home/ubuntu/llvm-project/build_sanitizer/bin/clang -DCMAKE_CXX_COMPILER=/home/ubuntu/llvm-project/build_sanitizer/bin/clang++ -DLLVM_ENABLE_ASSERTIONS=True -DLLVM_LIT_ARGS="-v" -DCMAKE_INSTALL_PREFIX=../stage1.install -DLLVM_TARGETS_TO_BUILD=AArch64 -DLLVM_ENABLE_PROJECTS="clang-tools-extra;clang;llvm" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt"
ninja -j64 ninja -j64 check-cxx `
Please reduce this down to single input to clang that will trigger this problem. Start by finding a single test file, then cut it down from there. From the traceback, it's something in clang's parser which is probably why it'll happen in some libcxx tests.
Took another look at this and I think I know the problem.
#8 0xae515112822c in clang::MakeDeductionFailureInfo(<...>
This function does this:
case TemplateDeductionResult::ConstraintsNotSatisfied: {
CNSInfo *Saved = new (Context) CNSInfo;
Saved->TemplateArgs = Info.takeSugared();
Saved->Satisfaction = Info.AssociatedConstraintsSatisfaction;
Result.Data = Saved;
break;
}
Saved->Satisfaction = Info.AssociatedConstraintsSatisfaction; is assigning a ConstraintSatisfaction. Which is in the backtrace being assigned:
#7 0x55b993a11c5e in clang::ConstraintSatisfaction::operator=(clang::ConstraintSatisfaction const&)
I don't know exactly what the assignment operator does but I see that class has a smallvector member:
llvm::SmallVector<TemplateArgument, 4> TemplateArgs;
MakeDeductionFailureInfo ends by returning some object containing all this and there are a bunch of comments in there like:
// FIXME: Should allocate from normal heap so that we can free this later.
Which explains the leak.
This function placement news things that are returned to...someone, but are never freed. Why they are placement new'd I'm not sure, probably something to do with replacing the ASTContext &Context passed to the function. This may be a memory saving tactic, or they just didn't want to deal with a pointer and variable latency allocating memory.
This means that the destructor of smallvector is never called, so the heap allocation leaks.
The usual state of FIXMEs is that they never get fixed, so I recommend you find an ASAN option that can ignore this.
There is another smallvector in ConstraintSatisfaction so it could be either of them but the principle is the same.
In fact, this was reported before in #143129. Same traceback, also when running libcxx tests.