mchalupa / dg

[LLVM Static Slicer] Various program analyses, construction of dependence graphs and program slicing of LLVM bitcode.
MIT License
474 stars 131 forks source link

How to `llvm-to-source` a `.sliced` file that is obtained from a linked bitcode file #429

Closed TimHe95 closed 2 years ago

TimHe95 commented 2 years ago

I want to slice a part of the PostgreSQL source, from fsync function, backward to an entry: RecordTransactionCommit. This anaylsis contains tree .c source files: xact.c xlog.c fd.c. Following the document, I obtain the .bc files repectively:

# get xact.bc
clang-6.0 -c -g -emit-llvm -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -I../../../../src/include -D_GNU_SOURCE -DHAVE_SYNC_FILE_RANGE -o xact.bc xact.c
# get fd.bc
clang-6.0 -c -g -emit-llvm -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -I../../../../src/include -D_GNU_SOURCE -DHAVE_SYNC_FILE_RANGE -o xlog.bc xlog.c
# get fd.bc
clang-6.0 -c -g -emit-llvm -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -I../../../../src/include -D_GNU_SOURCE -DHAVE_SYNC_FILE_RANGE -o fd.bc fd.c

And link them together:

llvm-link fd.bc xlog.bc xact.bc -o xact+xlog+fd.bc

Then, run the slicer.

llvm-slicer -cutoff-diverging=false -pta 'fs' -sc 'fsync' -entry=RecordTransactionCommit -annotate cd,slice,dd xact+xlog+fd.bc

I want to know what code (i.e., for me, variable names) remains after slicing. So I use llvm-to-source

# xact.c is the longest file among the three
llvm-to-source xact+xlog+fd.sliced xact.c

The resulting code is in a mess. It contains some random comments. It seems that llvm-to-source does not distinguish the line numbers of the three files. How can I solve this problem?

Thanks!

mchalupa commented 2 years ago

llvm-to-source cannot do something like that. It can work only with a single C file. It's just a very simple debugging tool (patches are welcome ;). You may try to compile the code with -g -fno-discard-value-names and use llvm2c or rellic to get C code from the sliced LLVM. The code will not look like the original code, but it will be C...

TimHe95 commented 2 years ago

Works well! Thanks.