Skip to content

Commit

Permalink
Merge pull request #83 – Add valgrind target to check for memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Viktorin authored and GitHub committed Dec 5, 2017
2 parents eb9dbc1 + c85f432 commit 084ffe0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Infrastructure:
* Remove distclean.sh in favor of make clean
* Use `package`, `depends`, `install_requires` in setup.py
* Add make target for scan-build (static analysis using clang)
* Add make target and suppression file for Valgrind (memory checker)

Modules/
* Remove unused LDAPberval helper functions
Expand Down
24 changes: 22 additions & 2 deletions Doc/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,33 @@ We use several specialized tools for debugging and maintenance.
Make targets
------------

``make lcov-open``
Make targets currently use the ``python3`` executable.
Specify a different one using, for example::

make PYTHON=/usr/local/bin/python

Notable targets are:

``make lcov lcov-open``
Generate and view test coverage for C code.
Requires ``make`` and ``lcov``.
Requires LCOV_.

``make scan-build``
Run static analysis. Requires ``clang``.

``make valgrind``
Run Valgrind_ to check for memory leaks. Requires ``valgrind`` and
a Python suppression file, which you can specify as ``PYTHON_SUPP``, e.g.::

make valgrind PYTHON_SUPP=/your/path/to/valgrind-python.supp

The suppression file is ``Misc/valgrind-python.supp`` in the Python
source distribution, and it's frequently packaged together with
Python development headers.

.. _LCOV: https://github.com/linux-test-project/lcov
.. _Valgrind: http://valgrind.org/


Reference leak tests
--------------------
Expand Down
1 change: 1 addition & 0 deletions Doc/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,6 @@ userApplications
userPassword
usr
uuids
Valgrind
whitespace
workflow
22 changes: 21 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ LCOV_REPORT=build/lcov_report
LCOV_REPORT_OPTIONS=--show-details -no-branch-coverage \
--title "python-ldap LCOV report"
SCAN_REPORT=build/scan_report
PYTHON_SUPP=/usr/share/doc/python3-devel/valgrind-python.supp

.NOTPARALLEL:

Expand All @@ -18,6 +19,9 @@ clean:
-delete
find . -depth -name __pycache__ -exec rm -rf {} \;

build:
mkdir -p build

# LCOV report (measuring test coverage for C code)
.PHONY: lcov-clean lcov-coverage lcov-report lcov-open lcov
lcov-clean:
Expand All @@ -27,7 +31,7 @@ lcov-clean:
lcov-coverage:
WITH_GCOV=1 tox -e py27,py36

$(LCOV_INFO):
$(LCOV_INFO): build
lcov --capture --directory build --output-file $(LCOV_INFO)

$(LCOV_REPORT): $(LCOV_INFO)
Expand All @@ -49,3 +53,19 @@ scan-build:
scan-build -o $(SCAN_REPORT) --html-title="python-ldap scan report" \
-analyze-headers --view \
$(PYTHON) setup.py clean --all build

# valgrind memory checker
.PHONY: valgrind
$(PYTHON_SUPP):
@ >&2 echo "valgrind-python.supp not found"
@ >&2 echo "install Python development files and run:"
@ >&2 echo " $(MAKE) valgrind PYTHON_SUPP=/your/path/to/valgrind-python.supp"
exit 1;

valgrind: build $(PYTHON_SUPP)
valgrind --leak-check=full \
--suppressions=$(PYTHON_SUPP) \
--suppressions=Misc/python-ldap.supp \
--gen-suppressions=all \
--log-file=build/valgrind.log \
$(PYTHON) setup.py test
41 changes: 41 additions & 0 deletions Misc/python-ldap.supp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Valgrind suppression file for Python 3.6.

{
Ignore libldap memory leak, https://github.com/python-ldap/python-ldap/issues/82
Memcheck:Leak
match-leak-kinds: definite
fun:malloc
fun:ber_memalloc_x
fun:ber_flatten
fun:ldap_cancel
fun:l_ldap_cancel
...
}

{
Known leak in SASL interaction, https://github.com/python-ldap/python-ldap/issues/81
Memcheck:Leak
match-leak-kinds: definite
fun:malloc
fun:strdup
fun:interaction
fun:py_ldap_sasl_interaction
fun:ldap_int_sasl_bind
fun:ldap_sasl_interactive_bind
fun:ldap_sasl_interactive_bind_s
fun:l_ldap_sasl_interactive_bind_s
...
}

{
Ignore possible leaks in exception initialization
Memcheck:Leak
match-leak-kinds: possible
fun:malloc
fun:PyObject_Malloc
...
fun:PyErr_NewException
fun:LDAPinit_constants
fun:init_ldap_module
...
}

0 comments on commit 084ffe0

Please sign in to comment.