[Contents] [TitleIndex] [WordIndex

Use valgrind to fix Scilab code

Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail.

Scilab have some integration with valgrind to ease the tracking of Invalid Read / Write or Memleak on our code. It also allow you to track any kind of issue related to multi-thread and concurrent memory access.

As valgrind emulates the code, we strongly recommend to reduce your test-case to a valid NWNI / CLI only script.

Scilab NWNI/CLI mode

To track the issue in NWNI mode, you can just start Scilab with the -nwni -profiling option and the binary will be emulated on valgrind. Some well known issues are ignored (eg. suppressed on the log) due to the suppression file at SCI/tools/profiling/valgrind.supp.

As a reference to understand the displayed log, you can read the explanation of error messages from Memcheck. Basically there is no false-positive others than an unknown x86 opcode.

/!\ valgrind does not enforce a rebuild but without debug information the log will be harder to understand.

Scilab Graphical mode

In general, this is hard to track memory issues when using both native code and JVM ones but using some valgrind tricks this is possible. Note that you will need some expertise on the JVM internals to have a good understanding of how native calls from Java and JNI interactions will be logged. The outputted valgrind.log file will also contains a lot of false-positive due to the JVM implementation (for instance to manage NullPointerException), please ignore them.

The trick is to only track our own memory issue by enforcing the malloc implementation for Scilab (but not the JVM), I chose tcmalloc that is quiet common these days.

  1. install the gperftools/tcmalloc library (both runtime and devel) on your system
  2. enforce the rebuild of all DSO : make clean

  3. rebuild scilab linking against the tcmalloc library make LDFLAGS='-ltcmalloc'

  4. execute Scilab with : _JAVA_OPTIONS='-Xint' SCILAB_VALGRIND_OPT='--soname-synonyms=somalloc=*tcmalloc* --error-limit=no --log-file=valgrind.log' bin/scilab -profiling

Note the soname-synonyms options to override the default malloc and use tcmalloc instead.


2022-09-08 09:27