grame-cncm / faust

Functional programming language for signal processing and sound synthesis
http://faust.grame.fr
Other
2.58k stars 322 forks source link

libfaust: memory leak when creating DSP factory (LLVM C API) #841

Closed edward-ly closed 1 year ago

edward-ly commented 1 year ago

I'm running Arch Linux (x86_64) with FAUST 2.54.9 (LLVM 14.0.6 backend), GCC 12.2.0, and valgrind 3.19.0 (all installed with pacman). I tried just compiling the C API test example with gcc -lfaust -o llvm-test tests/llvm-tests/llvm-test.c, followed by running valgrind --leak-check=full ./llvm-test. Here's the output I got:

==34439== Memcheck, a memory error detector
==34439== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==34439== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==34439== Command: ./llvm-test
==34439== 
Libfaust version : 2.54.9 (LLVM 14.0.6)
getDSPMachineTarget x86_64-pc-linux-gnu:haswell
==34439== Thread 2:
==34439== Conditional jump or move depends on uninitialised value(s)
==34439==    at 0x4847D18: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==34439==    by 0x4E34F08: UnknownInlinedFun (char_traits.h:395)
==34439==    by 0x4E34F08: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) [clone .constprop.0] (basic_string.h:641)
==34439==    by 0x4E2975F: initFaustDirectories(int, char const**) [clone .constprop.0] (libcode.cpp:1208)
==34439==    by 0x4C10D90: createFactoryAux1(void*) (libcode.cpp:2292)
==34439==    by 0x53E08FC: start_thread (pthread_create.c:442)
==34439==    by 0x5461C73: clone (clone.S:100)
==34439== 
getNumInputs : 0
getNumOutputs : 2
meta_declare compile_options, -lang llvm 14.0.6 -es 1 -mcd 16 -single -ftz 0 -vec -lv 0 -vs 32
meta_declare filename, score
meta_declare maths.lib/author, GRAME
meta_declare maths.lib/copyright, GRAME
meta_declare maths.lib/license, LGPL with exception
meta_declare maths.lib/name, Faust Math Library
meta_declare maths.lib/version, 2.5
meta_declare name, score
meta_declare oscillators.lib/name, Faust Oscillator Library
meta_declare oscillators.lib/version, 0.3
meta_declare platform.lib/name, Generic Platform Library
meta_declare platform.lib/version, 0.3
ui_open_horizontal_box score 1 0.500000
ui_declare foo, bar
ui_add_vertical_slider f0 110.000000 110.000000 880.000000 1.000000
ui_close_box
==34439== 
==34439== HEAP SUMMARY:
==34439==     in use at exit: 682,164 bytes in 3,240 blocks
==34439==   total heap usage: 185,821 allocs, 182,581 frees, 51,877,070 bytes allocated
==34439== 
==34439== Thread 1:
==34439== 28 bytes in 1 blocks are definitely lost in loss record 276 of 2,970
==34439==    at 0x4841888: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==34439==    by 0x53F681E: strdup (strdup.c:42)
==34439==    by 0x4E0482F: getCDSPMachineTarget (llvm_dsp_aux.cpp:710)
==34439==    by 0x109791: main (in /home/edward/faust/llvm-test)
==34439== 
==34439== 372,574 (1,136 direct, 371,438 indirect) bytes in 1 blocks are definitely lost in loss record 2,970 of 2,970
==34439==    at 0x4842003: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==34439==    by 0x82C86A7: llvm::MCJIT::createJIT(std::unique_ptr<llvm::Module, std::default_delete<llvm::Module> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::shared_ptr<llvm::MCJITMemoryManager>, std::shared_ptr<llvm::LegacyJITSymbolResolver>, std::unique_ptr<llvm::TargetMachine, std::default_delete<llvm::TargetMachine> >) (MCJIT.cpp:62)
==34439==    by 0x8241638: llvm::EngineBuilder::create(llvm::TargetMachine*) (ExecutionEngine.cpp:538)
==34439==    by 0x4E0C180: llvm_dynamic_dsp_factory_aux::initJIT(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (llvm_dynamic_dsp_aux.cpp:316)
==34439==    by 0x4E0FAAE: createDSPFactoryFromString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, char const**, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int) (llvm_dynamic_dsp_aux.cpp:641)
==34439==    by 0x4E10215: createCDSPFactoryFromString (llvm_dynamic_dsp_aux.cpp:804)
==34439==    by 0x1097E5: main (in /home/edward/faust/llvm-test)
==34439== 
==34439== LEAK SUMMARY:
==34439==    definitely lost: 1,164 bytes in 2 blocks
==34439==    indirectly lost: 371,438 bytes in 420 blocks
==34439==      possibly lost: 0 bytes in 0 blocks
==34439==    still reachable: 309,562 bytes in 2,818 blocks
==34439==         suppressed: 0 bytes in 0 blocks
==34439== Reachable blocks (those to which a pointer was found) are not shown.
==34439== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==34439== 
==34439== Use --track-origins=yes to see where uninitialised values come from
==34439== For lists of detected and suppressed errors, rerun with: -s
==34439== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
sletz commented 1 year ago

Should be improved in https://github.com/grame-cncm/faust/commit/b1c08546007752eb4a8f0502b5f4375f4c3c8bea. Can you test and report ?

edward-ly commented 1 year ago

After re-compiling/installing FAUST from source, yes. Thank you!

==42270== Memcheck, a memory error detector
==42270== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==42270== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==42270== Command: ./llvm-test
==42270== 
Libfaust version : 2.54.9 (LLVM 14.0.6)
getDSPMachineTarget x86_64-pc-linux-gnu:haswell
==42270== Thread 2:
==42270== Conditional jump or move depends on uninitialised value(s)
==42270==    at 0x4847D18: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==42270==    by 0x4C1FEA7: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) [clone .constprop.0] (in /usr/local/lib/libfaust.so.2.54.9)
==42270==    by 0x4C27D39: initFaustDirectories(int, char const**) [clone .constprop.0] (in /usr/local/lib/libfaust.so.2.54.9)
==42270==    by 0x4C317D4: createFactoryAux1(void*) (in /usr/local/lib/libfaust.so.2.54.9)
==42270==    by 0x550B8FC: start_thread (pthread_create.c:442)
==42270==    by 0x558CC73: clone (clone.S:100)
==42270== 
getNumInputs : 0
getNumOutputs : 2
meta_declare compile_options, -lang llvm 14.0.6 -es 1 -mcd 16 -single -ftz 0 -vec -lv 0 -vs 32
meta_declare filename, score
meta_declare maths.lib/author, GRAME
meta_declare maths.lib/copyright, GRAME
meta_declare maths.lib/license, LGPL with exception
meta_declare maths.lib/name, Faust Math Library
meta_declare maths.lib/version, 2.5
meta_declare name, score
meta_declare oscillators.lib/name, Faust Oscillator Library
meta_declare oscillators.lib/version, 0.3
meta_declare platform.lib/name, Generic Platform Library
meta_declare platform.lib/version, 0.3
ui_open_horizontal_box score 1 0.500000
ui_declare foo, bar
ui_add_vertical_slider f0 110.000000 110.000000 880.000000 1.000000
ui_close_box
==42270== 
==42270== HEAP SUMMARY:
==42270==     in use at exit: 284,526 bytes in 2,814 blocks
==42270==   total heap usage: 185,988 allocs, 183,174 frees, 51,827,153 bytes allocated
==42270== 
==42270== LEAK SUMMARY:
==42270==    definitely lost: 0 bytes in 0 blocks
==42270==    indirectly lost: 0 bytes in 0 blocks
==42270==      possibly lost: 0 bytes in 0 blocks
==42270==    still reachable: 284,526 bytes in 2,814 blocks
==42270==         suppressed: 0 bytes in 0 blocks
==42270== Reachable blocks (those to which a pointer was found) are not shown.
==42270== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==42270== 
==42270== Use --track-origins=yes to see where uninitialised values come from
==42270== For lists of detected and suppressed errors, rerun with: -s
==42270== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
sletz commented 1 year ago

Thanks ! BTW what are you using libfaust for ?

edward-ly commented 1 year ago

It's for my academic research. I can't share much more than that just yet, but I will say that it involves compiling a ton of FAUST programs all at once, which is how I discovered the memory leaks.

sletz commented 1 year ago

Another cleanup here: https://github.com/grame-cncm/faust/commit/cc6dfca9224caf3c1b8486ec09f8649f40857641