-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrt.sh
460 lines (436 loc) · 15.9 KB
/
rt.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
#!/bin/bash
#--------------------------------------------------------------------------
#
# Copyright (C) 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# Run the icFuzz tool to generate a series of tests; compile each of them with javac,
# make dex file, run the test by java and Dalvik in host mode and compare results.
# Parameter:
# -b,-bw,-bb <value> - use given build defined by branch, ww, or/and bits.
# -u <dir name> - update Ruby files with those from $FILER_DIR/<dir name>
# -r <path> - directory to put results into
# -p <prefix> - prefix to add to the test dir names
# -kd - keep old dirs with failures alive if they exist - don't require removing them
# -co <opt> - compiler additional options
# -oc - run optimizing compiler
# -dp <pass> - disable optimization pass
# -i - compare test results with interpreter mode instead of Java
# -no - run the tests without optimizations
# -t <integer> - timeout in seconds
# -tn <name> - name of the test file and main class (Test by default)
# -v <path> - verify build with existing tests rather than generate new ones
# -f <path> - save summary of the run to the given file in the directory with results
# -tl <integer>- time limit of the run in minutes
# -sp - save the tests that passed (they are removed by default)
# -arg <arg> - pass an arbitrary argument to VM
# -gc - vary GC options during testing
# -extreme - Additional option to use with CG mode. Disables output comparison
# -conf <file> - Pass config file to Fuzzer
# -apk - Enable apk mode
# -st <number> - Number of subtests for apk-mode
# -si <number> - Number of iterations for apk-mode
# -mt <number> - Min milliseconds to execute apk
# -o - Pass -o option to Fuzzer and control the execution from outside
# <integer> - number of tests to generate
#--------------------------------------------------------------------------------
source common.sh
ORIG_RUBY_CODE_DIR="$FILER_DIR/rb"
RUBY_CODE_DIR="$RUN_DIR/rb"
FILES_OF_INTEREST="*.java *.class rt_cmd rt_apk rt_out* rt_err* core* tmp classes.dex src"
FILES_OF_INTEREST_PASSED="$FILES_OF_INTEREST"
GC_TYPES=("GSS" "CMS" "SS")
GC_HSPACE=("-XX:EnableHSpaceCompactForOOM" "-XX:DisableHSpaceCompactForOOM")
GC_VERIFICATION="-Xgc:preverify -Xgc:postverify -Xgc:preverify_rosalloc -Xgc:postverify_rosalloc -Xgc:presweepingverify -Xgc:verifycardtable"
GC_EXP=off
EXTREME=off
conf_file=config.yml
#--------------------------------------------------------------------------------
function Save_passed_res {
test_dir_path=$res_path/$1/$prefix$2
mkdir -p $test_dir_path
chmod 775 "$res_path/$1" 2>&1 >> /dev/null
chmod -R 775 "$test_dir_path" 2>&1 >> /dev/null
cp -r $FILES_OF_INTEREST_PASSED $test_dir_path > /dev/null 2>&1
[[ -n "$3" ]] && echo " $3!"
}
#--------------------------------------------------------------------------------
function Save_res {
test_dir_path=$res_path/$1/$prefix$2
mkdir -p $test_dir_path
chmod 775 "$res_path/$1" 2>&1 >> /dev/null
chmod -R 775 "$test_dir_path" 2>&1 >> /dev/null
cp -r $FILES_OF_INTEREST $test_dir_path > /dev/null 2>&1
echo " $3!"
}
#--------------------------------------------------------------------------------
# returns: 0 - test complete, 1 - no JIT, 2 - VerifyError, 3 - Java crash, 4 - should never been returned, 5 - OOM, 6 - Interpreter/Java timeout, 7 - NPE during class initialization
function Run_test_apk {
# Run in interpreter mode
if [[ ${#outer_control} -ne 0 ]]
then
i=0
while [[ $i -lt $subiters ]]
do
Run_apk_VM Fuzzer.apk "int$i" $i $INT_ARGS
if [ $? -eq 124 ]; then
echo " Interpreter timeout!"
return 6
fi
i=$(($i + 1))
done
else
Run_apk_VM Fuzzer.apk 'int' 0 $INT_ARGS
if [ $? -eq 124 ]; then
echo " Interpreter timeout!"
return 6
fi
fi
Run_apk Fuzzer.apk 0 0 $MIN_TIME
empty=0
timeouts=0
i=0
while [[ $i -lt $subiters ]]
do
Run_apk " " $i $i $MIN_TIME compile
res=$?
[[ $res -eq 124 ]] && timeouts=$(( $timeouts + 1 ))
[[ ! -s rt_out$i ]] && empty=$(( $empty + 1 ))
i=$(($i + 1))
done
if grep -E 'VerifyError' rt_out* rt_err* > /dev/null; then # workaround for DEX's bug
let vrferr=vrferr+1
return 2
elif grep 'OutOfMemory' rt_out* rt_err* > /dev/null; then # workaround for OOM
return 5
elif [[ $timeouts -gt 0 ]] || grep TIMOUT rt_out* rt_err* &>/dev/null; then
Save_res hangs $1 Timeout
let timeouts=timeouts+1
elif grep 'java.lang.ExceptionInInitializerError' rt_err* &> /dev/null; then # NPE during class initialization - too complex dependencies
return 7
elif [[ $empty -ne 0 ]]; then
Save_res crashes $1 Crash
let crashes=crashes+1
elif grep "Fatal signal" rt_err* | grep -v 'libc' > /dev/null; then # compiler crash, filter out known bluetooth crashes
Save_res crashes $1 Crash
let crashes=crashes+1
elif grep "fatal error" rt_outint* > /dev/null; then # Java crash
let jcrash=jcrash+1
return 3
else
ret=0
i=0
while [[ $i -lt $subiters ]]
do
[[ ${#outer_control} -eq 0 ]] && cp rt_outint rt_outint$i
diff rt_outint$i rt_out$i &> /dev/null
ret=$(( $ret + $? ))
i=$(( $i + 1 ))
done
if [[ $ret -eq 0 ]]
then
[[ "$SAVE_PASSED" = "true" ]] && Save_passed_res passes $1
return 0 # Passed
else
Save_res fails $1 Failed
let fails=fails+1
fi
fi
}
#--------------------------------------------------------------------------------
# returns: 0 - test complete, 1 - no JIT, 2 - VerifyError, 3 - Java crash, 4 - should never been returned, 5 - OOM, 6 - Interpreter/Java timeout, 7 - NPE during class initialization
function Run_test {
gc_opts=""
if [[ "$GC_EXP" == "on" ]]
then
back_gc=${GC_TYPES[$(($RANDOM % 3))]}
gc=${GC_TYPES[$(($RANDOM % 3))]}
gc_opts=$gc_opts" -Xgc:$gc -XX:BackgroundGC=$back_gc"
gc_opts=$gc_opts" "${GC_HSPACE[$(($RANDOM % 2))]}
[[ $(($RANDOM % 2)) -eq 0 ]] && gc_opts=$gc_opts" $GC_VERIFICATION"
gc_thr1=$((($RANDOM % 11) + 1))
gc_thr2=$((($RANDOM % 11) + 1))
[[ $(($RANDOM % 2)) -eq 0 ]] && gc_opts=$gc_opts" -XX:ParallelGCThreads=$gc_thr1"
[[ $(($RANDOM % 2)) -eq 0 ]] && gc_opts=$gc_opts" -XX:ConcGCThreads=$gc_thr2"
fi
if [[ "$EXTREME" != "on" ]]
then
if [ "$comp_fast" = "y" ]; then
RunVM $INT_ARGS >rt_out_ref 2>rt_err_ref
if [ $? -eq 124 ]; then
echo " Interpreter timeout!"
return 6
fi
else
timeout $TIME_OUT java $test_name >rt_out_ref 2>&1 # 2>rt_err_ref not working: java outputs err msgs to stdout
if [ $? -eq 124 ]; then
echo " Java timeout!"
return 6
fi
fi
fi
#echo "RunVM $VM_LIB $args $add_opts $gc_opts" > rt_cmd
RunVM $VM_LIB $args $add_opts $gc_opts >rt_out 2>rt_err
[[ "$EXTREME" == "on" ]] && cp rt_out rt_out_ref
res_code=$?
if grep -E 'VerifyError' rt_out rt_err > /dev/null; then # workaround for DEX's bug
let vrferr=vrferr+1
return 2
elif grep 'OutOfMemory' rt_out rt_err > /dev/null; then # workaround for OOM
return 5
elif [ $res_code -eq 124 ] || grep 'TIMEOUT' rt_out &>/dev/null; then
Save_res hangs $1 Timeout
let timeouts=timeouts+1
elif grep 'java.lang.ExceptionInInitializerError' rt_err &> /dev/null; then # NPE during class initialization - too complex dependencies
return 7
elif [ ! -s rt_out ]; then
Save_res crashes $1 Crash
let crashes=crashes+1
elif grep "Fatal signal" rt_err > /dev/null; then # compiler crash
Save_res crashes $1 Crash
let crashes=crashes+1
elif grep "fatal error" rt_out_ref > /dev/null; then # Java crash
let jcrash=jcrash+1
return 3
elif diff rt_out_ref rt_out > /dev/null; then
[[ "$SAVE_PASSED" = "true" ]] && Save_passed_res passes $1
return 0 # Passed
else
Save_res fails $1 Failed
let fails=fails+1
fi
return 4 # the test did not pass
}
#--------------------------------------------------------------------------------
function Finish {
cd ..
rm -r $work_dir
#rm -r $ANDROID_DATA
[[ "$1" = "" ]] || Err $1
exit
}
#--------------------------------------------------------------------------------
update=n
verify=n
res_dir=""
res_file=""
keep_dirs=n
prefix=""
comp_fast=n
test_name=Test
time_limit=0
total=0
new_build=n
add_opts=""
subtests=10
subiters=2
args=$OPT_ARGS
ulimit -c 0
apk=n
outer_control=""
MIN_TIME=20000
while [ "$1" != "" ]; do
case $1 in
-b|-bw|-bb|-bs)
new_build=y
New_build_args $1 $2
shift;;
-u)
update=y
ORIG_RUBY_CODE_DIR=$FILER_DIR/$2
shift;;
-v)
[[ -d $2 ]] || Err "no directory to verify: $2"
verify=y
source_dir=`pwd`/$2
shift;;
-r)
res_dir=$2
shift;;
-kd) keep_dirs=y;;
-p)
prefix=$2
shift;;
-co)
if [ "$2" != "" ]; then
add_opts="$add_opts -Xcompiler-option $2"
shift
fi
;;
-oc) add_opts="$add_opts -Xcompiler-option --compiler-backend=Optimizing";;
-dp) add_opts="$add_opts -Xcompiler-option --disable-passes=$2"
shift;;
-extreme) EXTREME=on;;
-jit) add_opts="$add_opts -Xusejit:true -XOatFileManagerCompilerFilter:interpret-only -Xjit-block-mode";;
-i) comp_fast=y;;
-no) args=$NOPT_ARGS;;
-gc) GC_EXP=on;;
-t) SetTimeout $2
shift;;
-tn) test_name=$2
shift;;
-arg) add_opts="$add_opts $2"
shift;;
-f) res_file=$2
shift;;
-tl) time_limit=$2
shift;;
-conf) conf_file=$2
shift;;
-st) subtests=$2
shift;;
-si) subiters=$2
shift;;
-sp) SAVE_PASSED="true";;
-mt) MIN_TIME=$2
shift;;
-apk) apk=y;;
-o) outer_control="-o";;
*) total=$1;;
esac
shift
done
[[ $new_build = "y" ]] && Set_build
#--------------------------------------------------------------------------------
passes=0
vrferr=0
jcrash=0
crashes=0
fails=0
timeouts=0
invalids=0
res_path=$FILER_DIR/$res_dir
if [ "$update" = "y" ]; then
if [ ! -d $ORIG_RUBY_CODE_DIR ]; then
Err "rt.sh: dir to copy Ruby files from not found: $ORIG_RUBY_CODE_DIR"
fi
rm -r "$RUBY_CODE_DIR" &> /dev/null
mkdir -p "$RUN_DIR"
cp -r "$ORIG_RUBY_CODE_DIR" "$RUBY_CODE_DIR"
echo "Ruby files updated"
fi
if [ -d $res_path/hangs -o -d $res_path/crashes -o -d $res_path/fails -o -d $res_path/errors ]; then
if [ "$keep_dirs" = "n" ]; then
Err "rt.sh: remove dirs: hangs, crashes, fails and errors from $res_path"
fi
fi
echo --------------------------------------
echo Build under test: $BRANCH $BUILD $BITS
echo --------------------------------------
prefix_="/tmp"
[[ -d /export/ram ]] && prefix_="/export/ram/tmp"
mkdir -p $prefix_ &> /dev/null
work_dir=$(mktemp -d --tmpdir=$prefix_)
cd $work_dir
if [ "$verify" = "y" ]; then
ulimit -c 0
for dir in `ls $source_dir`; do
[[ -d $source_dir/$dir ]] || continue
rm $FILES_OF_INTEREST > /dev/null 2>&1
cp $source_dir/$dir/*.java $source_dir/$dir/*.class $source_dir/$dir/*.dex .
Run_test $dir
echo test $dir done
done
Finish
fi
if [ $total -lt 1 -a $time_limit -lt 1 ]; then
Finish "invalid number of iterations: $total or time limit: $time_limit"
fi
iters=0
fails_in_a_row=0
while [ $iters != $total ]; do
problem=no
let iters=iters+1
let perc=$iters*100/$total
rm $FILES_OF_INTEREST > /dev/null 2>&1
if [[ "$apk" == "y" ]]
then
rm -f `find "$APK_DIR/Fuzzer/src/com/intel/fuzzer" -name 'Test*java' | grep -E 'Test[0-9]*.java'` &>> rt_apk
lines=0
counter=0
while [[ $counter -lt $subtests ]]
do
counter=$(($counter + 1))
echo "ruby -I$RUBY_CODE_DIR $RUBY_CODE_DIR/Fuzzer.rb -f $RUBY_CODE_DIR/$conf_file" -p 'com.intel.fuzzer' -n "Test$counter" >> rt_cmd
ruby -I$RUBY_CODE_DIR $RUBY_CODE_DIR/Fuzzer.rb -f $RUBY_CODE_DIR/$conf_file -p 'com.intel.fuzzer' -n "Test$counter" $outer_control > "$APK_DIR/Fuzzer/src/com/intel/fuzzer/Test$counter.java"
cntr=`cat "$APK_DIR/Fuzzer/src/com/intel/fuzzer/Test$counter.java" | wc -l`
lines=$(( $lines + $cntr ))
done
cp -r "$APK_DIR/Fuzzer/src/com/intel/fuzzer" ./src
bash "$FILER_DIR/build-apk.sh" "$FILER_DIR" &>> rt_apk
if [ $? -ne 0 ]; then
Save_res errors $iters Invalid
let invalids=invalids+1
run_res=1
else
Run_test_apk $iters
run_res=$?
if [ $run_res -eq 2 ]; then # invalid test run: VerifyError
problem="VerifyError"
elif [ $run_res -eq 3 ]; then # invalid test run: Java crash
problem="Java crash"
elif [ $run_res -eq 5 ]; then # invalid test run: OOM
problem="OOM"
elif [ $run_res -eq 6 ]; then # invalid test run: Timeout
problem="Reference run timeout"
elif [ $run_res -eq 7 ]; then # invalid test run: java.lang.ExceptionInInitializerError
problem="NPE during initialization"
fi
fi
else
echo "ruby -I$RUBY_CODE_DIR $RUBY_CODE_DIR/Fuzzer.rb -f $RUBY_CODE_DIR/$conf_file" >> rt_cmd
ruby -I$RUBY_CODE_DIR $RUBY_CODE_DIR/Fuzzer.rb -f $RUBY_CODE_DIR/$conf_file > $test_name.java
lines=`cat $test_name.java | wc -l`
cp $RUBY_CODE_DIR/FuzzerUtils*.class .
javac $test_name.java
if [ $? -ne 0 ]; then
Save_res errors $iters Invalid
let invalids=invalids+1
run_res=1
else
$DX --dex --output=classes.dex *.class
Run_test $iters
run_res=$?
if [ $run_res -eq 2 ]; then # invalid test run: VerifyError
problem="VerifyError"
elif [ $run_res -eq 3 ]; then # invalid test run: Java crash
problem="Java crash"
elif [ $run_res -eq 5 ]; then # invalid test run: OOM
problem="OOM"
elif [ $run_res -eq 6 ]; then # invalid test run: Timeout
problem="Reference run timeout"
elif [ $run_res -eq 7 ]; then # invalid test run: java.lang.ExceptionInInitializerError
problem="NPE during initialization"
fi
fi
fi
if [ $run_res -eq 0 ]; then
fails_in_a_row=0
else
let fails_in_a_row=fails_in_a_row+1
[[ $fails_in_a_row -eq 10 ]] && Finish "10 failures in a row"
fi
if [ "$problem" != "no" ]; then
echo " $prefix$iters: $problem ($lines) [$vrferr VerifyError, $jcrash Java crashes]"
let iters=iters-1
else
echo "$prefix$iters ($lines) [$crashes crashes, $fails fails, $timeouts hangs, $invalids errors] - ${perc}%/$total"
fi
done
if [ "$res_file" != "" ]; then
echo "$prefix $total: $crashes crashes, $fails fails, $timeouts hangs, $invalids errors, $vrferr VerifyError, $jcrash Java crashes" >>$res_path/$res_file
fi
Finish