Categories
python

Python: Running Valgrind on a C extension

This is how I use Valgrind to check for memory bugs in pygit2.

UPDATED 2019-10-09

Glibc with debug symbols

The first issue I run into is that Valgrind refused to work if Glibc was not compiled with debug symbols.

This is how I did in my Gentoo notebook, I edited the /etc/portage/make.conf file to enable the splitdebug feature:

/etc/portage/make.conf
FEATURES="${FEATURES} splitdebug compressdebug -nostrip"

Then re-emerged the glibc:

$ sudo emerge glibc

Then commented out the splitdebug feature to avoid emerging other packages with debug symbols (there is likely a better way to do this).

Valgrind 3.7

The big problem is that running Valgrind with a Python C extension raises tons of false positives. There are a number of things you need to do to avoid all these false positives.

The first one is to use latest version of Valgrind 3.7, because of some bug I forgot about present in version 3.6

Python, compiling for a memory debugger

Now, you need to install a version of Python to be used with Valgrind:

$ ./configure --prefix=/.../Python-3.7.4-valgrind --without-pymalloc --with-pydebug --with-valgrind
$ make
$ make install

Install pytest and pygit2

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ /.../Python-3.7.4-valgrind/bin/python3 get-pip.py
$ /.../Python-3.7.4-valgrind/bin/pip install pytest
[...]
$ /.../Python-3.7.4-valgrind/bin/python3 setup.py install

The suppression file

Now you need to use the suppression file that you will find in the Python sources, at Misc/valgrind-python.supp. This is how I run the unit tests in pygit2 to find issues:

$ valgrind --trace-children=yes --suppressions=valgrind-python.supp \
    /.../Python-3.7.4-valgrind/bin/pytest

That’s it.

Leave a Reply