项目作者: Lekensteyn

项目描述 :
Clang static analyzer plugin for checking memory issues in Wireshark/GLib applications (allocator mismatch and memleaks)
高级语言: C++
项目地址: git://github.com/Lekensteyn/clang-alloc-free-checker.git
创建时间: 2018-05-03T15:29:19Z
项目社区:https://github.com/Lekensteyn/clang-alloc-free-checker

开源协议:MIT License

下载


Build Status

Clang Static Analyzer plugin for memory issues

Clang Static Analyzer is a source code
analysis tool that can spot bugs in C and C++ projects such as Wireshark. This
repository contains a plugin that tries to detect additional domain-specific
issues.

A secondary reason to write this plugin is to learn how to extend the Clang
static analyzer. See below for developer resources if you want to do the same!

Tested with Clang 6.0.0 on Arch Linux.

Usage

Building the plugin requires LLVM and Clang development headers to be installed.
To build this plugin (AllocFreePlugin.so) and validate correctness:

  1. mkdir build && cd build
  2. cmake -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
  3. ninja
  4. ninja check

To test the Wireshark source tree (at /tmp/wireshark) with this plugin:

  1. mkdir /tmp/wsbuild && cd /tmp/wsbuild
  2. cmake -GNinja -DCMAKE_C_COMPILER=/usr/lib/clang/ccc-analyzer -DCMAKE_CXX_COMPILER=/usr/lib/clang/c++-analyzer \
  3. -DCMAKE_BUILD_TYPE=None /tmp/wireshark -DCMAKE_BUILD_WITH_INSTALL_RPATH=1
  4. CCC_CC=clang CCC_CXX=clang++ \
  5. scan-build -load-plugin path/to/AllocFreePlugin.so -enable-checker alpha.AllocFree ninja

Check an individual file:

  1. clang -cc1 -analyze -load ./AllocFreePlugin.so -analyzer-checker=alpha.AllocFree ../test/Analysis/wmem-alloc.c

Check an individual file and write a HTML report to the directory htmldir.

  1. clang -cc1 -analyze -load ./AllocFreePlugin.so -analyzer-checker=alpha.AllocFree -analyzer-output=html -o htmldir ../test/Analysis/wmem-alloc.c

To check a specific file within a project without having to specify all of its
compile options (such as macros and include directories) using a compilation
database:

  1. # Create compilation database (compile_commands.json) in build directory
  2. cmake [other options here] -DCMAKE_EXPORT_COMPILE_COMMANDS=1
  3. clang-check -analyze -extra-arg=-Xanalyzer -extra-arg=-load -extra-arg=-Xanalyzer -extra-arg=path/to/AllocFreePlugin.so -extra-arg=-Xanalyzer -extra-arg=-analyzer-checker=alpha.AllocFree /tmp/wireshark/dumpcap.c

Features

Detects issues such as:

  • Mismatch between allocation functions. Using g_free to release g_strsplit
    memory will result in a memory leak. Combining wmem_free with g_malloc is
    could result in crashes in the future.
  • Mismatch between wmem allocation scopes. Use of p = wmem_alloc(NULL, 1) with
    wmem_free(wmem_file_scope(), p) will result in memory corruption.

Helpful features:

  • Mark code in a bug report with Memory is allocated and Memory is released.

The checker is by far not complete. The default unix.Malloc checker is much
more sophisticated. Limitations of the plugin include:

  • False positives for memory leaks.
  • The reported path for memory leaks could be helpful, currently the end of the
    path sometimes point to arbitrary code while the real memory leak happens when
    the function returns. Search for Memory is allocated to see the original
    leaked memory.
  • If memory is passed as non-constant pointer to some other function, memory
    leaks can no longer be tracked in the same function.

Developer Resources

If you are interested in writing your own checker, be sure to read the Checker
Developer Manual
and
watch the Building a Checker in 24 Hours talk
(slides,
video). The sample mentioned in that material
has been updated in mean time, be sure to check the Clang Git
history
of
lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp
for changes and the reasoning behind them. In particular, a new PointerEscape
check was added that makes it possible to ignore false positive memory leaks.

Another rich source of information are other checkers in that directory, such as
MallocChecker.cpp.

While it is not always very detailed, the Doxygen documentation provides a
useful overview of methods that can be used:

Other potentially helpful links:

Random hints:

  • The command clang -cc1 -analyze -analyzer-checker=debug.ViewExplodedGraph
    (or equivalently, clang -cc1 -analyze -analyzer-viz-egraph-graphviz)
    requires Clang to be built in debug mode or nothing appears to happen.