Wireshark-dev: [Wireshark-dev] Memory leak debugging - current master passes all tests!
From: Peter Wu <[email protected]>
Date: Mon, 28 Jan 2019 22:02:12 +0100
Hi!

I am happy to share that current git master passes all tests without
failing on memory leaks (as reported by AddressSanitizer)!
This does not mean that there are no more memory leaks though (we have
pretty bad test coverage), but at least it allows enabling ASAN without
disabling memory leak detection in CI configurations.

To reproduce, build wireshark with the cmake -DENABLE_ASAN=1 option.
(I additionally use -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang
-DCMAKE_CXX_COMPILER=clang++):

    git clone https://code.wireshark.org/review/wireshark
    mkdir wsbuild && cd wsbuild
    cmake -GNinja ../wireshark -DENABLE_ASAN=1
    ninja all test-programs

For improved debugging, set some extra runtime environment variables:

    # Reduce false negatives by forcing malloc for every allocation.
    export WIRESHARK_DEBUG_WMEM_OVERRIDE=simple
    export G_SLICE=always-malloc
    # The following options are only recommended when investigating
    # memory leaks. The first option slows down a lot but results in
    # more precise backtraces. The second option prints addresses of
    # leaked objects which can be inspected in a debugger.
    export ASAN_OPTIONS=fast_unwind_on_malloc=0:report_objects=1

Then run tests with:

    pytest

Since v2.9.1rc0-558-geec3ce3bb2 this shows:

    ========================= test session starts ==========================
    platform linux -- Python 3.7.2, pytest-4.1.0, py-1.7.0, pluggy-0.8.1
    rootdir: /tmp/wsbuild, inifile: pytest.ini
    plugins: xdist-1.25.0, forked-0.2
    gw0 [526] / ... / gw7 [526]
    ................................................................ [ 12%]
    ....................s.........s................................. [ 24%]
    ................................................................ [ 36%]
    ................................................................ [ 48%]
    ................................................................ [ 60%]
    ................................................................ [ 73%]
    ............s................................................... [ 85%]
    ........................................................s.s..... [ 97%]
    ..............                                                   [100%]
    ======================= short test summary info ========================
    SKIP [1] .../test/fixtures.py:369: SoftHSM is not found
    SKIP [1] .../test/suite_dissection.py:57: MSP splitting is not implemented yet
    SKIP [1] .../test/suite_wslua.py:261: GRegex tests are broken since PCRE 8.34, see bug 12997.
    SKIP [1] .../test/suite_dfilter/group_tvb.py:33: This doesn't work yet in Wireshark
    SKIP [1] .../test/suite_dfilter/group_tvb.py:38: This doesn't work yet in Wireshark
    =============== 521 passed, 5 skipped in 505.13 seconds ================

The above test included capture tests which requires either capabilities
on the dumpcap executable or ambient capabilities as described at
https://www.wireshark.org/lists/wireshark-dev/201711/msg00025.html

Tested on Arch Linux with v2.9.1rc0-566-g90cf0750:

    Compiled (64-bit) with libpcap, with POSIX capabilities (Linux), with libnl 3,
    with GLib 2.58.2, with zlib 1.2.11, without SMI, with c-ares 1.15.0, with Lua
    5.2.4, with GnuTLS 3.6.5 and PKCS #11 support, with Gcrypt 1.8.4, with MIT
    Kerberos, with MaxMind DB resolver, with nghttp2 1.35.1, with LZ4, with Snappy,
    with libxml2 2.9.8.

    Running on Linux 4.19.12-arch1-1-ARCH, with Intel(R) Core(TM) i7-6700HQ CPU @
    2.60GHz (with SSE4.2), with 31984 MB of physical memory, with locale
    en_GB.UTF-8, with libpcap version 1.9.0-PRE-GIT (with TPACKET_V3), with GnuTLS
    3.6.5, with Gcrypt 1.8.4, with zlib 1.2.11, binary plugins supported (0 loaded).

    Built using clang 4.2.1 Compatible Clang 7.0.1 (tags/RELEASE_701/final).

If you have not already, consider enabling ASAN by default in your
development builds (cmake -DENABLE_ASAN=1). It works on Linux and macOS,
but not with MSVC. ASAN detect memory safety issues (use-after-free,
double-free, buffer overflows, etc.) and is faster than valgrind.

If you do need valgrind, note that ASAN is not compatible with it. In
this case, simply create another build directory without ASAN.

If you are just interested in memory safety checks, but not memory leak
debugging, simply disable the latter with:

    export ASAN_OPTIONS=detect_leaks=0

These ASAN options are documented at
https://github.com/google/sanitizers/wiki/AddressSanitizerFlags
-- 
Kind regards,
Peter Wu
https://lekensteyn.nl