chipsalliance / Surelog

SystemVerilog 2017 Pre-processor, Parser, Elaborator, UHDM Compiler. Provides IEEE Design/TB C/C++ VPI and Python AST & UHDM APIs. Compiles on Linux gcc, Windows msys2-gcc & msvc, OsX
Apache License 2.0
353 stars 67 forks source link

Surelog (library) leaks memory. #3661

Open mglb opened 1 year ago

mglb commented 1 year ago

Surelog, at least when used as a library, has significant memory leaks. First discovered in systemverilog-plugin, but later verified with basic Surelog examples (e.g. hellouhdm). Brief look at logs tells that leaking memory is being allocated in both Surelog and UHDM.

Build

# export CXX='clang++'
# cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DCMAKE_CXX_FLAGS="-gdwarf-4" \
    -S . -B build
# cmake --build build -j $(nproc)

(-DCMAKE_CXX_FLAGS="-gdwarf-4" is for Valgrind compatibility when compiling with Clang 15+)

Run

# cd build/bin
# valgrind ./hellouhdm ../../tests/UnitElabBlock/top.v

Result:

...
==2== HEAP SUMMARY:
==2==     in use at exit: 6,839,877 bytes in 94,007 blocks
==2==   total heap usage: 1,443,800 allocs, 1,349,793 frees, 98,733,639 bytes allocated
==2==
==2== LEAK SUMMARY:
==2==    definitely lost: 66,818 bytes in 276 blocks
==2==    indirectly lost: 401,047 bytes in 2,469 blocks
==2==      possibly lost: 0 bytes in 0 blocks
==2==    still reachable: 6,372,012 bytes in 91,262 blocks
==2==         suppressed: 0 bytes in 0 blocks

Second run, with already generated preprocessing data (slpp_all):

==2== HEAP SUMMARY:
==2==     in use at exit: 4,613,893 bytes in 69,870 blocks
==2==   total heap usage: 206,939 allocs, 137,069 frees, 17,785,433 bytes allocated
==2==
==2== LEAK SUMMARY:
==2==    definitely lost: 68,066 bytes in 275 blocks
==2==    indirectly lost: 399,799 bytes in 2,470 blocks
==2==      possibly lost: 0 bytes in 0 blocks
==2==    still reachable: 4,146,028 bytes in 67,125 blocks
==2==         suppressed: 0 bytes in 0 blocks
alaindargelas commented 1 year ago

Antlr is responsible for most of the leaks. UHDM memory leaks is going to be solved with the Garbage collection WIP