Skip to content

CodeGraphs

Xiao edited this page Mar 28, 2024 · 8 revisions
  • CodeGraph is the demo program sample to understand LLVM IR and their graph representations, i.e., ICFG and PAG

CodeGraph Layout

CodeGraph/

|-- CMakeLists.txt
|-- CodeGraph.cpp
`-- src
    |-- swap.c
    `-- example.c
    

1. Get the latest code template

* Before coding, please type cd $HOME/Teaching-Software-Analysis and git pull in your terminal to make sure you always have the latest version of the code template.

2. LLVM IR Generation

* Use control + ~ to call out command line

Option 1: Generate LLVM IR of swap.c via the following command line

cd /home/SVF-tools/Teaching-Software-Analysis/CodeGraph
clang -S -c -O0 -fno-discard-value-names -emit-llvm src/swap.c -o swap.ll
opt -S -p=mem2reg swap.ll -o swap.ll

Option 2: Generate LLVM IR via compile.sh

cd /home/SVF-tools/Teaching-Software-Analysis/CodeGraph
sh compile.sh src/swap.c

The two .ll file (i.e., swap.ll) will be generated under the /home/SVF-tools/Teaching-Software-Analysis/CodeGraph folder

3. Run and Debug your CodeGraph

You need to set the "program" to be the executable file of CodeGraph, i.e., "${workspaceFolder}/bin/codegraph" in launch.json in order to run and debug

You need to set the "args" to be the .ll file you have just generated, i.e., "${workspaceFolder}/CodeGraph/swap.ll" in launch.json in order to run and debug

3.2 Click the run button

  • The two dot files (i.e, swap.ll.icfg.dot and swap.ll.pag.dot) will be generated under the /home/SVF-tools/Teaching-Software-Analysis/CodeGraph folder

4. Visualize ICFG and PAG (SVFIR graph)

4.1 VSCode Graphviz Interactive Preview extensions

4.2 Open the dot file and graph visualization

  • Open the dot file under, for example, /home/SVF-tools/Teaching-Software-Analysis/CodeGraph/swap.ll.icfg.dot which is the ICFG of the program and call out the command palette

  • Enter Graphviz and select `Interactive Preview (beside)

  • You will see the ICFG on the right hand side