llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.44k stars 12.16k forks source link

Memory leak when `clang_parsetranslationunit2fullargv` fails due to bad compiler flags #47176

Open llvmbot opened 4 years ago

llvmbot commented 4 years ago
Bugzilla Link 47832
Version 10.0
OS Linux
Attachments C file leaking memory, Leak patch
Reporter LLVM Bugzilla Contributor
CC @zygoloid

Extended Description

Since LLVM10, if compiler flags are bad and result in a failed compiler invokation, and if the CXUnsavedFile array isn't empty, clang_parsetranslationunit2fullargv() leaks memory.

Attached is a C file that leaks due to the above.

The amount of leaked memory seems to be proportional to the CXUnsavedFile[] that is passed to clang_parsetranslationunit2fullargv().

When run with valgrind, the attached snippet has the following output:

==25857== Memcheck, a memory error detector
==25857== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25857== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==25857== Command: ./a.out
==25857==
Clang Parse Error: 4
==25857==
==25857== HEAP SUMMARY:
==25857==     in use at exit: 288,195 bytes in 1,711 blocks
==25857==   total heap usage: 7,257 allocs, 5,546 frees, 865,848 bytes allocated
==25857==
==25857== 49 bytes in 1 blocks are definitely lost in loss record 1,355 of 1,706
==25857==    at 0x483B07F: operator new(unsigned long, std::nothrow_t const&) (vg_replace_malloc.c:385)
==25857==    by 0x5D6B409: llvm::WritableMemoryBuffer::getNewUninitMemBuffer(unsigned long, llvm::Twine const&) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5D6B78E: llvm::MemoryBuffer::getMemBufferCopy(llvm::StringRef, llvm::Twine const&) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x506A402: clang_parseTranslationUnit_Impl(void*, char const*, char const* const*, int, llvm::ArrayRef<CXUnsavedFile>, unsigned int, CXTranslationUnitImpl**) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x506B203: void llvm::function_ref<void ()>::callback_fn<clang_parseTranslationUnit2FullArgv::{lambda()#1}>(long) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5D59C3F: llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5D59CA3: RunSafelyOnThread_Dispatch(void*) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0x5DD042C: threadFuncSync(void*) (in /home/bstaletic/work/ycmd/third_party/clang/lib/libclang.so.10)
==25857==    by 0xA4C43E8: start_thread (in /usr/lib/libpthread-2.32.so)
==25857==    by 0xA3EC292: clone (in /usr/lib/libc-2.32.so)
==25857==
==25857== LEAK SUMMARY:
==25857==    definitely lost: 49 bytes in 1 blocks
==25857==    indirectly lost: 0 bytes in 0 blocks
==25857==      possibly lost: 0 bytes in 0 blocks
==25857==    still reachable: 288,146 bytes in 1,710 blocks
==25857==         suppressed: 0 bytes in 0 blocks
==25857== Reachable blocks (those to which a pointer was found) are not shown.
==25857== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==25857==
==25857== For lists of detected and suppressed errors, rerun with: -s
==25857== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
llvmbot commented 3 years ago

changed the description