From edbb3784070284cffc6ad6024b4245a757bef085 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Mon, 4 Dec 2017 10:04:57 +0100 Subject: [PATCH 1/2] Add valgrind target to check for memory leaks Signed-off-by: Christian Heimes --- CHANGES | 1 + Doc/contributing.rst | 4 +++- Doc/spelling_wordlist.txt | 1 + Makefile | 16 ++++++++++++++- Misc/python-ldap.supp | 41 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 Misc/python-ldap.supp diff --git a/CHANGES b/CHANGES index df35179..f75659d 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/Doc/contributing.rst b/Doc/contributing.rst index aa6097d..114541f 100644 --- a/Doc/contributing.rst +++ b/Doc/contributing.rst @@ -168,13 +168,15 @@ We use several specialized tools for debugging and maintenance. Make targets ------------ -``make lcov-open`` +``make lcov lcov-open`` Generate and view test coverage for C code. Requires ``make`` and ``lcov``. ``make scan-build`` Run static analysis. Requires ``clang``. +``make valgrind`` + Run Valgrind to check for memory leaks. Requires ``valgrind`` Reference leak tests -------------------- diff --git a/Doc/spelling_wordlist.txt b/Doc/spelling_wordlist.txt index 95a5df6..70c07e6 100644 --- a/Doc/spelling_wordlist.txt +++ b/Doc/spelling_wordlist.txt @@ -150,5 +150,6 @@ userApplications userPassword usr uuids +Valgrind whitespace workflow diff --git a/Makefile b/Makefile index 1ee3eda..f648d95 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -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: @@ -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) @@ -49,3 +53,13 @@ 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 +valgrind: build + valgrind --leak-check=full \ + --suppressions=$(PYTHON_SUPP) \ + --suppressions=Misc/python-ldap.supp \ + --gen-suppressions=all \ + --log-file=build/valgrind.log \ + $(PYTHON) setup.py test diff --git a/Misc/python-ldap.supp b/Misc/python-ldap.supp new file mode 100644 index 0000000..de70446 --- /dev/null +++ b/Misc/python-ldap.supp @@ -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 + ... +} From c85f4326eea13bdf6af13039c15447a416b3abb9 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Tue, 5 Dec 2017 14:03:24 +0100 Subject: [PATCH 2/2] Check suppression file in `make valgrind`; improve make target docs This should make things easier to use for people with different configurations. --- Doc/contributing.rst | 22 ++++++++++++++++++++-- Makefile | 8 +++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Doc/contributing.rst b/Doc/contributing.rst index 114541f..0cfb51b 100644 --- a/Doc/contributing.rst +++ b/Doc/contributing.rst @@ -168,15 +168,33 @@ We use several specialized tools for debugging and maintenance. Make targets ------------ +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`` + 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 -------------------- diff --git a/Makefile b/Makefile index f648d95..150981c 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,13 @@ scan-build: # valgrind memory checker .PHONY: valgrind -valgrind: build +$(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 \