Skip to content

Detecting memory leaks

Akshat Kaushik edited this page Jul 9, 2020 · 22 revisions

1. Build SVF

Please refer to SVF setup

2. Compile your project into LLVM bc files

clang -c -emit-llvm -g example.c -o example.bc

To compile a complicated real-world project you may wish to use WLLVM guide to install it.

Use WLLVM to compile a single C/C++ file to LLVM Bitcode (.bc) file:

Compile your example.c into example.bc

export LLVM_COMPILER=clang

wllvm example.c -c -emit-llvm -g -o example.bc

Use WLLVM to compile a C project to LLVM Bitcode (.bc) file:

Configure and build your project:

sudo apt install libglib2.0-dev libncurses5 libtool
wget https://pkgconfig.freedesktop.org/releases/pkg-config-0.26.tar.gz
tar xf pkg-config-0.26.tar.gz
cd pkg-config-0.26

export LLVM_COMPILER=clang
CC=wllvm ./configure
make

Generate the LLVM Bitcode file for Analysis:

extract-bc pkg-config -o pkg-config.bc

(The project used in the example above is from freedesktop.org as used in the WLLVM wiki)

3. Run SVF's memory leak detector on the generated bc file

cd SVF
. ./setup
saber -leak -stat=false example.bc

4. The printed memory leak results when analyzing this test case

	 PartialLeak : memory allocation at : (ln: 23 fl: sp1.c)
		 conditional free path: 
		  --> (ln: 12 fl: sp1.c) 
Clone this wiki locally