Skip to content

Detecting memory leaks

Akshat Kaushik edited this page May 18, 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 LLVM 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 LLVM to compile a C project to LLVM Bitcode (.bc) file:

Setup and Configure your project: (The project used in the example below is from freedesktop.org as used in the WLLVM wiki)

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

Build your project executable and generate the LLVM Bitcode file for Analysis:

CC=wllvm LLVM_COMPILER=clang make CFLAGS="-g -c -emit-llvm"
extract-bc pkg-config -o pkg-config.bc

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)