[llvm-bugs] [Bug 47832] New: Memory leak when `clang_parsetranslationunit2fullargv` fails due to bad compiler flags

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 13 11:02:18 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=47832

            Bug ID: 47832
           Summary: Memory leak when `clang_parsetranslationunit2fullargv`
                    fails due to bad compiler flags
           Product: clang
           Version: 10.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: libclang
          Assignee: unassignedclangbugs at nondot.org
          Reporter: boris.staletic at gmail.com
                CC: klimek at google.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Created attachment 24051
  --> https://bugs.llvm.org/attachment.cgi?id=24051&action=edit
C file leaking memory

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)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201013/a865b6f5/attachment-0001.html>


More information about the llvm-bugs mailing list