-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBugAssist.sh
executable file
·491 lines (463 loc) · 12.7 KB
/
BugAssist.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env bash
#Title : BugAssist.sh
#Usage : bash BugAssist.sh -h
#Author : pmorvalho
#Date : January 28, 2024
#Description : Simulates BugAssist (CAV 2011)
#Notes :
# (C) Copyright 2024 Pedro Orvalho.
#==============================================================================
dataset="C-Pack-IPAs"
dataset_dir="C-Pack-IPAs"
labs=("lab02")
years=("year-1" "year-2" "year-3")
tests_dir="tests_updated"
TIMEOUT=3600s
MEMOUT=32000
ftype="wcnf"
tmp_dir="results"
CPACKIPAS=1
TCAS=0
sanity_check(){
if [[ $VERBOSE ]];
then
echo "Starting sanity check"
fi
if [[ $CORRECT_PROG == 1 ]];
then
tests_flags=" -upt -hw "
if [[ $TCAS == 1 ]];
then
tests_flags=$tests_flags" --test_dir $dataset_dir --traces_dir $traces_dir/$p"
else
tests_flags=$tests_flags" -e $IPA "
fi
else
if [[ $TCAS == 1 ]];
then
tests_flags=" --test_dir $dataset_dir --traces_dir $traces_dir/$p -hw "
else
tests_flags=" -t $tests_2_use -e $IPA -hw "
# tests_flags=" -upt -e $IPA -hw "
fi
fi
mkdir -p $d/faults
cp prints.* $d/faults/.
for ((faults_index=0; ; faults_index++));
do
if [[ $VERBOSE ]];
then
echo "Faults index: $faults_index"
fi
new_p=$d/"faults"/$p"_faults-$faults_index.c"
stmts_map=$d/"faults/$faults_index.pkl.gz"
# if [[ $TCAS == 1 ]];
# then
python3 program_instrumentalizer.py -ip $prog -o $new_p $tests_flags -msi $stmts_map -nu $NUM_UNROLL -ssv $faults_dict -ba -fi $faults_index >& $d/"faults"/$p.pi-$faults_index-out
# else
# python3 program_instrumentalizer.py -ip $prog -o $new_p -upt -e $IPA -hw -msi $stmts_map -nu $NUM_UNROLL -ssv $faults_dict -ba -fi $faults_index >& $d/"faults"/$p.pi-$faults_index-out
# fi
if [[ ! ($? -eq 0) && $(grep "No more faults to relax!" $d/"faults"/$p.pi-$faults_index-out) != "" ]];
then
echo "Could not find any faulty statements!"
echo
if [[ $VERBOSE ]];
then
echo "#Attempts: $faults_index"
if [[ $CORRECT_PROG == 1 && $faults_index -eq 0 ]];
then
echo "After checking $faults_index possible sets of faults."
echo "SUCCESS"
exit
fi
echo "FAILED"
fi
echo
return;
fi
aux_dir=$d"/faults/cbmc-$faults_index"
mkdir -p $aux_dir
runsolver/src/runsolver -o $aux_dir/out.o -w $aux_dir/watcher.w -v $aux_dir/var.v -W $TIMEOUT --rss-swap-limit $MEMOUT cbmc/src/cbmc/cbmc $new_p prints.c --16 --unwind $NUM_UNROLL
if [[ $(grep "FAILURE" $aux_dir/out.o) == "" ]];
then
continue;
fi
if [[ $VERBOSE ]];
then
flag_verb=" -v "
echo "After checking $faults_index possible sets of faults."
echo "#Attempts: $faults_index"
fi
python3 get_faulty_statements.py -d $faults_dict -fi $faults_index -id $p $flag_verb -ba | tee -a $output_dir/$p.fdbk
if [[ $VERBOSE ]];
then
echo "SUCCESS"
fi
exit
done
echo
}
CBMC_bounds_check(){
# $1 program on which we are going to run CBMC
# $2 dir
# $3 filename
aux_dir=$2"/cbmc-2nd"
if [[ ! -d $aux_dir ]];
then
mkdir -p $aux_dir
fi
runsolver/src/runsolver -o $aux_dir/out.o -w $aux_dir/watcher.w -v $aux_dir/var.v -W $TIMEOUT --rss-swap-limit $MEMOUT \
timeout $TIMEOUT cbmc/src/cbmc/cbmc $1 --16 --unwind $NUM_UNROLL --bounds-check
if [[ $(grep "TIMEOUT=t" $aux_dir"/var.v" ) ]]; then
echo "Timeout running CBMC on $1 (Checking Memory Accesses)" | tee -a $3.fdbk
exit
fi
out_of_bounds=$(grep -r ".*array_bounds.* FAILURE" $aux_dir"/out.o" | grep -v "output")
if [[ $out_of_bounds ]];
then
if [[ $(echo $out_of_bounds | grep "__input") ]];
then
echo "$2: Scanf is accessing out-of-bound memory!" | tee -a $3.fdbk
else
echo "$2: Some array is accessing out-of-bound memory!" | tee -a $3.fdbk
fi
#exit
fi
}
Oracle(){
# $1 filename
# $2 mapping from instumentalized instructions to the students' instructions (pickle gzip)
# $3 dir
# $4 previous dir (to store the localized faults dict)
# $5 t_num
aux_dir=$3"/oracle"
if [[ ! -d $aux_dir ]];
then
mkdir -p $aux_dir
fi
runsolver/src/runsolver -o $aux_dir/out.o -w $aux_dir/watcher.w -v $aux_dir/var.v -W $TIMEOUT --rss-swap-limit $MEMOUT python3 oracle.py --$ftype $1"."$ftype -msi $2 -ba -t $t_num --faults_dict $4
if [[ $? = 0 ]]; then
if [[ $(grep "TIMEOUT=t" $aux_dir/var.v) ]]; then
gzip -f $1"."$ftype
echo "Timeout solving $ftype formulae $1."$ftype
echo "Timeout running RC2" > $1.fdbk
exit
fi
else
echo "Error while running RC2 on formulae $1."$ftype
echo "Error while running RC2" > $1.fdbk
exit
fi
gzip -f $1"."$ftype &
if [[ $(grep "UNSAT" $aux_dir"/out.o") ]]; then
echo "UNSAT formulae $1."$ftype
echo "UNSAT WCNF formula" > $1.fdbk
exit
else
cost=$(cat $aux_dir"/out.o" | grep -oh "\#Bugs: [0-9]*" | head -1 | grep -oh "[0-9]*" )
if [[ $cost == "" ]];
then
cost="0"
fi
echo $cost
fi
}
cnf_2_wcnf_translation(){
# $1 CNF filename
# $2 mapping from instumentalized instructions to the students' instructions (pickle gzip)
if [[ $ftype == "pwcnf" ]];
then
python3 cnf_2_relaxed_wcnf.py -i $1".cnf" -p -o $1"."$ftype -msi $2 -nu $NUM_UNROLL
else
python3 cnf_2_relaxed_wcnf.py -i $1".cnf" -o $1"."$ftype -msi $2 -nu $NUM_UNROLL
fi
gzip -f $1".cnf" &
}
CBMC(){
# $1 program on which we are going to run CBMC
# $2 output filename
# $3 dir
aux_dir=$3"/cbmc"
if [[ ! -d $aux_dir ]];
then
mkdir -p $aux_dir
fi
runsolver/src/runsolver -o $aux_dir/out.o -w $aux_dir/watcher.w -v $aux_dir/var.v -W $TIMEOUT --rss-swap-limit $MEMOUT \
cbmc/src/cbmc/cbmc $1 prints.c --dimacs --16 --unwind $NUM_UNROLL --outfile $2".cnf"
if [[ $(grep "TIMEOUT=t" $aux_dir/var.v) ]]; then
echo "Timeout running CBMC on $1"
echo "Timeout running CBMC" > $2.fdbk
exit
fi
}
instrumentalization_step(){
# instrumentalization step, and this functions also checks which tests are failing in the test suite and next unrolls and instrumentalizes the program
t_num=$1
d=$2
new_p=$3
stmts_map=$4
tests_flags="-t "$t_num
if [[ $CORRECT_PROG == 1 ]];
then
tests_flags=$tests_flags" -upt -hw "
if [[ $TCAS == 1 ]];
then
tests_flags=$tests_flags" --test_dir $dataset_dir --traces_dir $traces_dir/$p"
else
tests_flags=$tests_flags" -e $IPA "
fi
else
if [[ $TCAS == 1 ]];
then
tests_flags=$tests_flags" --test_dir $dataset_dir --traces_dir $traces_dir/$p -hw "
else
tests_flags=$tests_flags" -e $IPA -hw "
fi
fi
python3 program_instrumentalizer.py -ip $prog -o $new_p $tests_flags -msi $stmts_map -nu $NUM_UNROLL >& $d/$t_num.pi-out
if [[ $? = 0 ]]; then
gcc $d/../prints.c $d/../nondet_vals.c $new_p -o $d/$t_num.gcc-out >& $d/$t_num.pi-gcc
if [[ $? = 0 ]]; then
# Next BugAssist will call CBMC
return
else
echo "ERROR"
echo "Error compiling $new_p" | tee -a $d/$t_num.fdbk
exit
fi
rm $d/$t_num.gcc-out
else
if [[ $(grep "This program is correct according to the given set of IO tests." $d/$t_num.pi-out) != "" ]];
then
echo "FIXED $d/$p after unrolling"
FIXED=1
return
else
if [[ $(grep "Warning" $d/$t_num.pi-out) ]];
then
echo "FIXED: $new_p "$(grep "Warning" $d/$t_num.pi-out)
echo $(grep "Warning" $d/$t_num.pi-out) > $d/$t_num.fdbk
FIXED=1
return
else
echo "ERROR"
echo "Error instrumentalizing $new_p" | tee -a $d/$t_num.fdbk
exit
fi
fi
fi
}
BugAssist_pipeline(){
if [[ $VERBOSE ]];
then
echo "Fault Localization Method: BugAssist"
fi
if [[ $TCAS == 0 ]];
then
p=$(echo $prog | rev | cut -d '/' -f 1 | rev | sed "s/\.c//g")
else
if [[ $VERBOSE ]];
then
echo "Using TCAS benchmark"
fi
p=$(echo $prog | rev | cut -d '/' -f 2 | rev )
fi
d=$output_dir
if [[ ! -d $d ]];
then
mkdir -p $d
fi
stmts_map=$d/$p".pkl.gz"
faults_dict=$d"/localized_faults.pkl.gz"
cp prints* $d/.
cp nondet_vals.c $d/.
tests_list=$(find $dataset_dir/tests/$IPA/*.in -maxdepth 0 -type f);
if [[ $CORRECT_PROG == 1 ]];
then
if [[ $TCAS == 1 ]];
then
tests_list=$(find $dataset_dir/tests/*.in)
fi
else
if [[ $TCAS == 1 ]];
then
tests_list=$(find $traces_dir/$p/*.out -maxdepth 0 -type f);
else
tests_2_use=$(bash check_unrolled_tests.sh $prog $dataset_dir/tests/$IPA 2> err.txt | grep -v "timeout" )
if [[ -f err.txt ]];
then
rm err.txt
fi
if echo $tests_2_use | grep -v -q "\[" ;
then
echo "FIXED $d/$p "$tests_2_use
echo $tests_2_use > $d/$p.fdbk
echo
echo "SUCCESS"
echo
exit
fi
fi
fi
# for each failed IO tests
for t in $tests_list;
do
if [[ $TCAS == 1 ]];
then
t_id=$(echo $t | rev | cut -d '/' -f 1 | rev | sed -e "s/\.out//")
t_num=$(echo $t_id | rev | cut -d 't' -f 1 | rev)
else
t_id=$(echo $t | rev | cut -d '/' -f 1 | rev | sed -e "s/\.in//")
t_num=$(echo $t_id | rev | cut -d '_' -f 1 | rev)
# ignore passed tests
if [[ $CORRECT_PROG != 1 ]];
then
if !(echo $tests_2_use | grep -q "$t_num") ;
then
continue;
fi
fi
fi
if [[ $VERBOSE ]];
then
echo "Checking IO test: $t_num"
fi
d=$output_dir
mkdir -p $output_dir/$t_num
cp prints* $output_dir/$t_num/.
cp nondet_vals.c $output_dir/$t_num/.
if [[ -f $output_dir/$t_num/$t_num.fdbk ]];
then
rm $output_dir/$t_num/$t_num.fdbk
fi
new_p=$output_dir/$t_num/$t_num"_instrumentalized.c"
if [[ $VERBOSE ]];
then
echo "Instrumentalizing $p using test $t_num"
fi
FIXED=0
instrumentalization_step $t_num $output_dir/$t_num $new_p $stmts_map
if [[ $FIXED -eq 1 ]];
then
FIXED=0
continue
fi
if [[ $VERBOSE ]];
then
echo "Calling CBMC on $p using tests $t_num"
fi
CBMC $new_p $output_dir/$t_num/$t_num $output_dir/$t_num
if [[ $VERBOSE ]];
then
echo "Translating the CNF into a WCNF"
fi
cnf_2_wcnf_translation $output_dir/$t_num/$t_num $stmts_map
if [[ $VERBOSE ]];
then
echo "Calling the MaxSAT Oracle on $p"
fi
cost=$(Oracle $output_dir/$t_num/$t_num $stmts_map $output_dir/$t_num $faults_dict $t_num)
if [[ -f $output_dir/$t_num/oracle/out.o && $VERBOSE -eq 1 ]];
then
echo $(grep "Diagnoses" $output_dir/$t_num/oracle/out.o)
fi
if [[ $cost == "UNSAT" ]];
then
echo "UNSAT $p" | tee -a $output_dir/$p.fdbk
exit
fi
if [[ $cost == "0" && $CORRECT_PROG == "" ]];
then
CBMC_bounds_check $new_p $output_dir/$t_num $output_dir/$t_num/$t_num
fi
rm $output_dir/$t_num/prints* $output_dir/$t_num/nondet_vals.c
done
d=$output_dir
sanity_check
echo
}
tests_2_use=""
FIXED=0
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-c|--cpack_ipas)
CPACKIPAS=1
TCAS=0
# ISCAS=0
shift
;;
-cp|--correct_progs)
CORRECT_PROG=1
shift
;;
-i|--input)
prog=$2
shift
shift
;;
-e|--ipa)
IPA=$2
shift
shift
;;
-nu|--num_unroll)
NUM_UNROLL=$2
shift
shift
;;
-o|--output_dir)
output_dir=$2
shift
shift
;;
-t|--tcas)
TCAS=1
CPACKIPAS=0
# ISCAS=0
dataset_dir="tcas"
traces_dir=$dataset_dir/"traces"
shift
;;
-td|--test_dir)
test_dir=$2
shift
shift
;;
-to|--timeout)
TIMEOUT=$2
shift
shift
;;
-v|--verbose)
VERBOSE=1
shift
;;
-h|--help)
echo "USAGE: $0 [-cp|--correct_progs] [-e|--enum_all] [-ipa|--ipa labX/exY] [-o|--output_dir out_dir/] [-c|--cpack_ipas] [-t|--tcas] [-td|--test_dir dir/] [-to|--timeout] [-v|--verbose] [-h|--help] [-i|--input program.c]
Options:
--> -c|--cpack_ipas -- to use C-Pack-IPAs dataset (Default).
--> -t|--tcas -- to run BugAssist on TCAS benchmark.
--> -cp|--correct_progs -- to run BugAssist on correct programs. With this option, BugAssist uses the entire test suite.
--> -a|--enum_all -- Enumerates all the WCNF solutions, even the ones with nonoptimum cost.
--> -e|--ipa labX/exY -- The name of C-Pack-IPA's lab and exercise. E.g. lab02/ex01
--> -o|--output_dir out_dir/ -- path to output directory.
--> -nu|--num_unroll NUM -- Number to unroll the program's loops/functions. E.g. -nu 3.
--> -td|--test_dir test_dir/ -- to specify the path to the test suite
--> -to|--timeout 3600s -- to specify the timeout for CBMC, RC2, in seconds. The default is 3600s.
--> -v|--verbose -- Set level of verbosity
--> -h|--help -- to print this message
--> -i|--input program.c|circuit.wcnf -- the path to the input program/wcnf formula"
exit
shift
;;
*)
echo "BugAssist.sh: error: unrecognized arguments: $key"
echo "Try [-h|--help] for help"
shift
exit
;;
esac
done
BugAssist_pipeline