Skip to content

Commit 05c46fa

Browse files
authored
Merge pull request #11 from hxuhack/dev-master
source directory decoupled
2 parents 1e724ad + 12661e2 commit 05c46fa

File tree

10 files changed

+77
-29
lines changed

10 files changed

+77
-29
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Then, compile the dependencies: `python3 compile.py -l`
3434

3535
To change our default compiler, please visit `config/compile.json` to modify the `CC` to your preferred compiler.
3636

37-
To change the test cases' root folder, please visit `config/test_settings.py` to change the `src_dirs`.
37+
To change the test cases' root folder, please visit `config/test_settings.py` to change the 4th element of corresponding record in `switches`.
3838

3939
**Pay attention!** If you are using Triton, please change the Triton installation path at the end of `config/test_settings.py`
4040

config/test_settings.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# ============ run_tests Setting ==============
22
FUNC_NAME = 'logic_bomb'
33

4-
src_dirs = [
5-
'src/',
6-
]
7-
84
cmds_tp_angr = ["clang -Iinclude -Lbuild -o angr/%s.out -xc - -lutils -lpthread -lcrypto -lm",
95
"python script/angr_run.py -r -l%d angr/%s.out"]
106

@@ -13,7 +9,7 @@
139

1410
cmds_tp_klee = [
1511
"clang -Iinclude -Lbuild -Wno-unused-parameter -emit-llvm -o klee/%s.bc -c -g klee/a.c -lpthread -lutils -lcrypto -lm",
16-
"klee klee/%s.bc",
12+
"klee --libc=uclibc --posix-runtime klee/%s.bc",
1713
"python3 script/klee_run.py -e%d -p%s"
1814
]
1915

@@ -32,11 +28,11 @@
3228
klee_tp_path = 'templates/klee.c'
3329

3430
switches = {
35-
'angr': [cmds_tp_angr, angr_tp_path, 'angr'],
36-
'angr_cpp': [cmds_tp_angr_cpp, angr_tp_path, 'angr'],
37-
'triton': [cmds_tp_triton, triton_tp_path, 'triton'],
38-
'triton_cpp': [cmds_tp_triton_cpp, triton_tp_path, 'triton'],
39-
'klee': [cmds_tp_klee, klee_tp_path, 'klee'],
31+
'angr': [cmds_tp_angr, angr_tp_path, 'angr', ('src/', )],
32+
'angr_cpp': [cmds_tp_angr_cpp, angr_tp_path, 'angr', ('src_cpp/', )],
33+
'triton': [cmds_tp_triton, triton_tp_path, 'triton', ('src/', )],
34+
'triton_cpp': [cmds_tp_triton_cpp, triton_tp_path, 'triton', ('src_cpp/', )],
35+
'klee': [cmds_tp_klee, klee_tp_path, 'klee', ('src/', )],
4036
}
4137

4238
# ============ triton Setting ==============

include/utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <string.h>
1010
#include <time.h>
1111
#include <sys/time.h>
12+
#include <sys/stat.h>
13+
#include <fcntl.h>
1214
#include <errno.h>
1315
#include <unistd.h>
1416
#include <err.h>

run_tests.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def kill_all(process):
1717
parent.kill()
1818

1919

20-
def ATKrun(target , src_dirs, func_name='logic_bomb', default_stdin_len=10, maxtime=60):
20+
def ATKrun(target, func_name='logic_bomb', default_stdin_len=10, maxtime=60, source=None, skip=False):
2121
def params_list_parser(params):
2222
if len(params.strip()) == 0:
2323
return []
@@ -36,12 +36,12 @@ def params_list_parser(params):
3636
res.append((var_type, var_name))
3737
return res
3838

39-
cmds_tp, tp_path, prefix = target
39+
cmds_tp, tp_path, prefix, src_dirs = target
4040
if not os.path.exists(prefix):
4141
os.mkdir(prefix)
4242

43-
if not os.path.exists('tmp'):
44-
os.mkdir('tmp')
43+
if source and not os.path.exists(source):
44+
os.mkdir(source)
4545

4646
ERROR = 0
4747
CORRECT = 1
@@ -93,8 +93,11 @@ def params_list_parser(params):
9393
print(res)
9494
res = '\n'.join([content, res])
9595
outname = file if len(file.split('.')) == 1 else file.split('.')[0]
96-
with open('tmp/' + file, 'w') as f:
96+
if source:
97+
with open(os.path.join(source, file), 'w') as f:
9798
f.write(res)
99+
if skip:
100+
continue
98101
if prefix == 'angr':
99102
cmds.append(cmds_tp[0] % outname)
100103
cmds.append(cmds_tp[1] % (default_stdin_len, outname))
@@ -176,17 +179,24 @@ def params_list_parser(params):
176179

177180

178181
if __name__ == '__main__':
179-
from config.test_settings import src_dirs, switches, FUNC_NAME
182+
from config.test_settings import switches, FUNC_NAME
180183
from collections import OrderedDict
181184
import argparse
182185

183186
parser = argparse.ArgumentParser()
184187
parser.add_argument("-e", "--engine", required=True, type=str, help="Symbolic execution engine")
185-
parser.add_argument("-t", "--maxtime", required=True, type=int, help="Max running time for a program")
188+
parser.add_argument("-t", "--maxtime", required=False, default=60, type=int, help="Max running time for a program")
189+
parser.add_argument("-s", "--source", required=False, type=str, help="Output source code into a directory")
190+
parser.add_argument("-n", "--no_test", action="store_true", help="Don't do the test")
186191
args = parser.parse_args()
192+
193+
if args.source:
194+
print("Saving output results in ", args.source)
187195

188196
try:
189-
res = ATKrun(switches[args.engine], src_dirs, func_name=FUNC_NAME, maxtime=args.maxtime)
197+
res = ATKrun(switches[args.engine], func_name=FUNC_NAME, maxtime=args.maxtime, source=args.source, skip=args.no_test)
198+
if args.source and args.no_test:
199+
exit(0)
190200
except KeyError:
191201
print('Invalid symbolic engine!')
192202
exit(1)

script/klee_run.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
pattern = re.compile(r"data:(.*)\n")
2727
tests = []
2828
running_res = set()
29-
for file in os.listdir(os.path.join('klee', 'klee-last')):
29+
for file in sorted(os.listdir(os.path.join('klee', 'klee-last'))):
3030
if file.endswith('.ktest'):
3131
cmd = 'KTEST_FILE=klee/klee-last/%s' % file
3232
res = os.system(cmd + ' klee/a.out') >> 8
@@ -38,11 +38,6 @@
3838
res = pattern.findall(out)[0].strip()
3939
tests.append(res)
4040

41-
# tohex = lambda x: ''.join(['\\x%02x' % ord(c) for c in x])
42-
with open('klee_outputs.csv', 'a', newline='', encoding='utf-8-sig') as csvfile:
43-
writer = csv.writer(csvfile)
44-
writer.writerow([args.program, ] + [_ for _ in tests])
45-
4641
tests = running_res
4742

4843
if 1 in tests:

script_runner.py

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def run(self, stms: list, index=0, expected_indent=None):
8383
if i == len(stms) - 1:
8484
raise SyntaxError(stm.stm)
8585
tmp_iter = self.evaluate(stm.parsed[-1])
86-
print(stm.parsed)
8786
for tmp in tmp_iter:
8887
try:
8988
self.variables.append({key: tmp[index] for index, key in enumerate(stm.parsed[1])})

src/buffer_overflow/heap_bo_l1.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ int n = 5;
77
// {"symvar":{"length": 256}}
88
int logic_bomb(char* symvar) {
99
char *p, *q;
10-
p = malloc(16);
11-
q = malloc(16);
10+
p = (char*)malloc(16);
11+
q = (char*)malloc(16);
1212
strcpy(p, symvar);
1313
free(q);
1414
if (n != 5){
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <string.h>
2+
#include "utils.h"
3+
4+
#include "a_tester.h"
5+
6+
// {"s":{"length": 16}}
7+
int logic_bomb(char* s) {
8+
int trigger = 0;
9+
int fd = open(s, O_RDONLY);
10+
if(fd != -1) {
11+
trigger = 1;
12+
close(fd);
13+
}
14+
15+
if(trigger) {
16+
return BOMB_ENDING;
17+
} else {
18+
return NORMAL_ENDING;
19+
}
20+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <string.h>
2+
3+
#include "utils.h"
4+
#include "a_tester.h"
5+
6+
// {"s": {"length": 4}}
7+
int logic_bomb(char* s) {
8+
int symvar = s[0] - 48;
9+
int j;
10+
char file[] = "tmp.covpro";
11+
int fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
12+
if(fd < 0)
13+
{
14+
exit(-1);
15+
}
16+
write(fd, &symvar, sizeof symvar);
17+
close(fd);
18+
fd = open(file, O_RDONLY);
19+
read(fd, &j, sizeof j);
20+
close(fd);
21+
if(j == 7){
22+
return 1;
23+
} else{
24+
return 0;
25+
}
26+
}

templates/klee.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
int main() {
1+
int main(int argc, char** argv) {
22
{%
33
for {<type>}, {<var>}, {<size>} in {<vp>}:
44
if {<type>} == {<"char*">}:

0 commit comments

Comments
 (0)