1#!/bin/bash
2#
3# Copyright (C) 2007 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Set up prog to be the path of this script, including following symlinks,
18# and set up progdir to be the fully-qualified pathname of its directory.
19prog="$0"
20args="$@"
21while [ -h "${prog}" ]; do
22    newProg=`/bin/ls -ld "${prog}"`
23    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
24    if expr "x${newProg}" : 'x/' >/dev/null; then
25        prog="${newProg}"
26    else
27        progdir=`dirname "${prog}"`
28        prog="${progdir}/${newProg}"
29    fi
30done
31oldwd=`pwd`
32progdir=`dirname "${prog}"`
33cd "${progdir}"
34progdir=`pwd`
35prog="${progdir}"/`basename "${prog}"`
36test_dir="test-$$"
37if [ -z "$TMPDIR" ]; then
38  tmp_dir="/tmp/$USER/${test_dir}"
39else
40  tmp_dir="${TMPDIR}/$USER/${test_dir}"
41fi
42checker="${progdir}/../tools/checker/checker.py"
43export JAVA="java"
44export JAVAC="javac -g -source 1.7 -target 1.7 -Xlint:-options"
45export RUN="${progdir}/etc/run-test-jar"
46export DEX_LOCATION=/data/run-test/${test_dir}
47export NEED_DEX="true"
48export USE_JACK="true"
49export SMALI_ARGS="--experimental"
50
51# If dx was not set by the environment variable, assume it is in the path.
52if [ -z "$DX" ]; then
53  export DX="dx"
54fi
55
56# If jasmin was not set by the environment variable, assume it is in the path.
57if [ -z "$JASMIN" ]; then
58  export JASMIN="jasmin"
59fi
60
61# If smali was not set by the environment variable, assume it is in the path.
62if [ -z "$SMALI" ]; then
63  export SMALI="smali"
64fi
65
66# If dexmerger was not set by the environment variable, assume it is in the path.
67if [ -z "$DXMERGER" ]; then
68  export DXMERGER="dexmerger"
69fi
70
71# If jack was not set by the environment variable, assume it is in the path.
72if [ -z "$JACK" ]; then
73  export JACK="jack"
74fi
75
76# ANDROID_BUILD_TOP is not set in a build environment.
77if [ -z "$ANDROID_BUILD_TOP" ]; then
78    export ANDROID_BUILD_TOP=$oldwd
79fi
80
81# If JACK_CLASSPATH is not set, assume it only contains core-libart.
82if [ -z "$JACK_CLASSPATH" ]; then
83  export JACK_CLASSPATH="${OUT_DIR:-$ANDROID_BUILD_TOP/out}/host/common/obj/JAVA_LIBRARIES/core-libart-hostdex_intermediates/classes.jack:${OUT_DIR:-$ANDROID_BUILD_TOP/out}/host/common/obj/JAVA_LIBRARIES/core-oj-hostdex_intermediates/classes.jack"
84fi
85
86export JACK="$JACK -g -cp $JACK_CLASSPATH"
87
88info="info.txt"
89build="build"
90run="run"
91expected="expected.txt"
92check_cmd="check"
93output="output.txt"
94build_output="build-output.txt"
95cfg_output="graph.cfg"
96strace_output="strace-output.txt"
97lib="libartd.so"
98testlib="arttestd"
99run_args="--quiet"
100build_args=""
101
102quiet="no"
103debuggable="no"
104prebuild_mode="yes"
105target_mode="yes"
106dev_mode="no"
107update_mode="no"
108debug_mode="no"
109relocate="yes"
110runtime="art"
111usage="no"
112build_only="no"
113suffix64=""
114trace="false"
115trace_stream="false"
116basic_verify="false"
117gc_verify="false"
118gc_stress="false"
119strace="false"
120always_clean="no"
121never_clean="no"
122have_dex2oat="yes"
123have_patchoat="yes"
124have_image="yes"
125image_suffix=""
126pic_image_suffix=""
127multi_image_suffix=""
128android_root="/system"
129
130while true; do
131    if [ "x$1" = "x--host" ]; then
132        target_mode="no"
133        DEX_LOCATION=$tmp_dir
134        run_args="${run_args} --host"
135        shift
136    elif [ "x$1" = "x--quiet" ]; then
137        quiet="yes"
138        shift
139    elif [ "x$1" = "x--use-java-home" ]; then
140        if [ -n "${JAVA_HOME}" ]; then
141          export JAVA="${JAVA_HOME}/bin/java"
142          export JAVAC="${JAVA_HOME}/bin/javac -g"
143        else
144          echo "Passed --use-java-home without JAVA_HOME variable set!"
145          usage="yes"
146        fi
147        shift
148    elif [ "x$1" = "x--jvm" ]; then
149        target_mode="no"
150        runtime="jvm"
151        prebuild_mode="no"
152        NEED_DEX="false"
153        USE_JACK="false"
154        run_args="${run_args} --jvm"
155        build_args="${build_args} --jvm"
156        shift
157    elif [ "x$1" = "x-O" ]; then
158        lib="libart.so"
159        testlib="arttest"
160        shift
161    elif [ "x$1" = "x--dalvik" ]; then
162        lib="libdvm.so"
163        runtime="dalvik"
164        shift
165    elif [ "x$1" = "x--no-dex2oat" ]; then
166        have_dex2oat="no"
167        shift
168    elif [ "x$1" = "x--no-patchoat" ]; then
169        have_patchoat="no"
170        shift
171    elif [ "x$1" = "x--no-image" ]; then
172        have_image="no"
173        shift
174    elif [ "x$1" = "x--pic-image" ]; then
175        pic_image_suffix="-pic"
176        shift
177    elif [ "x$1" = "x--multi-image" ]; then
178        multi_image_suffix="-multi"
179        shift
180    elif [ "x$1" = "x--pic-test" ]; then
181        run_args="${run_args} --pic-test"
182        shift
183    elif [ "x$1" = "x--relocate" ]; then
184        relocate="yes"
185        shift
186    elif [ "x$1" = "x--no-relocate" ]; then
187        relocate="no"
188        shift
189    elif [ "x$1" = "x--prebuild" ]; then
190        run_args="${run_args} --prebuild"
191        prebuild_mode="yes"
192        shift;
193    elif [ "x$1" = "x--strip-dex" ]; then
194        run_args="${run_args} --strip-dex"
195        shift;
196    elif [ "x$1" = "x--debuggable" ]; then
197        run_args="${run_args} -Xcompiler-option --debuggable"
198        debuggable="yes"
199        shift;
200    elif [ "x$1" = "x--no-prebuild" ]; then
201        run_args="${run_args} --no-prebuild"
202        prebuild_mode="no"
203        shift;
204    elif [ "x$1" = "x--gcverify" ]; then
205        basic_verify="true"
206        gc_verify="true"
207        shift
208    elif [ "x$1" = "x--gcstress" ]; then
209        basic_verify="true"
210        gc_stress="true"
211        shift
212    elif [ "x$1" = "x--image" ]; then
213        shift
214        image="$1"
215        run_args="${run_args} --image $image"
216        shift
217    elif [ "x$1" = "x-Xcompiler-option" ]; then
218        shift
219        option="$1"
220        run_args="${run_args} -Xcompiler-option $option"
221        shift
222    elif [ "x$1" = "x--runtime-option" ]; then
223        shift
224        option="$1"
225        run_args="${run_args} --runtime-option $option"
226        shift
227    elif [ "x$1" = "x--gdb-arg" ]; then
228        shift
229        gdb_arg="$1"
230        run_args="${run_args} --gdb-arg $gdb_arg"
231        shift
232    elif [ "x$1" = "x--debug" ]; then
233        run_args="${run_args} --debug"
234        shift
235    elif [ "x$1" = "x--gdb" ]; then
236        run_args="${run_args} --gdb"
237        dev_mode="yes"
238        shift
239    elif [ "x$1" = "x--strace" ]; then
240        strace="yes"
241        run_args="${run_args} --invoke-with strace --invoke-with -o --invoke-with $tmp_dir/$strace_output"
242        shift
243    elif [ "x$1" = "x--zygote" ]; then
244        run_args="${run_args} --zygote"
245        shift
246    elif [ "x$1" = "x--interpreter" ]; then
247        run_args="${run_args} --interpreter --runtime-option -XOatFileManagerCompilerFilter:verify-at-runtime"
248        image_suffix="-interpreter"
249        shift
250    elif [ "x$1" = "x--jit" ]; then
251        run_args="${run_args} --jit --runtime-option -XOatFileManagerCompilerFilter:verify-at-runtime"
252        image_suffix="-jit"
253        shift
254    elif [ "x$1" = "x--optimizing" ]; then
255        run_args="${run_args} -Xcompiler-option --compiler-backend=Optimizing"
256        image_suffix="-optimizing"
257        shift
258    elif [ "x$1" = "x--no-verify" ]; then
259        run_args="${run_args} --no-verify --runtime-option -XOatFileManagerCompilerFilter:verify-none"
260        shift
261    elif [ "x$1" = "x--verify-soft-fail" ]; then
262        run_args="${run_args} --verify-soft-fail --runtime-option -XOatFileManagerCompilerFilter:verify-at-runtime"
263        image_suffix="-interp-ac"
264        shift
265    elif [ "x$1" = "x--no-optimize" ]; then
266        run_args="${run_args} --no-optimize"
267        shift
268    elif [ "x$1" = "x--no-precise" ]; then
269        run_args="${run_args} --no-precise"
270        shift
271    elif [ "x$1" = "x--invoke-with" ]; then
272        shift
273        what="$1"
274        if [ "x$what" = "x" ]; then
275            echo "$0 missing argument to --invoke-with" 1>&2
276            usage="yes"
277            break
278        fi
279        run_args="${run_args} --invoke-with ${what}"
280        shift
281    elif [ "x$1" = "x--dev" ]; then
282        run_args="${run_args} --dev"
283        dev_mode="yes"
284        shift
285    elif [ "x$1" = "x--build-only" ]; then
286        build_only="yes"
287        shift
288    elif [ "x$1" = "x--build-with-javac-dx" ]; then
289        USE_JACK="false"
290        shift
291    elif [ "x$1" = "x--build-with-jack" ]; then
292        USE_JACK="true"
293        shift
294    elif [ "x$1" = "x--output-path" ]; then
295        shift
296        tmp_dir=$1
297        if [ "x$tmp_dir" = "x" ]; then
298            echo "$0 missing argument to --output-path" 1>&2
299            usage="yes"
300            break
301        fi
302        shift
303    elif [ "x$1" = "x--android-root" ]; then
304        shift
305        if [ "x$1" = "x" ]; then
306            echo "$0 missing argument to --android-root" 1>&2
307            usage="yes"
308            break
309        fi
310        android_root="$1"
311        run_args="${run_args} --android-root $1"
312        shift
313    elif [ "x$1" = "x--update" ]; then
314        update_mode="yes"
315        shift
316    elif [ "x$1" = "x--help" ]; then
317        usage="yes"
318        shift
319    elif [ "x$1" = "x--64" ]; then
320        run_args="${run_args} --64"
321        suffix64="64"
322        shift
323    elif [ "x$1" = "x--trace" ]; then
324        trace="true"
325        shift
326    elif [ "x$1" = "x--stream" ]; then
327        trace_stream="true"
328        shift
329    elif [ "x$1" = "x--always-clean" ]; then
330        always_clean="yes"
331        shift
332    elif [ "x$1" = "x--never-clean" ]; then
333        never_clean="yes"
334        shift
335    elif [ "x$1" = "x--dex2oat-swap" ]; then
336        run_args="${run_args} --dex2oat-swap"
337        shift
338    elif [ "x$1" = "x--instruction-set-features" ]; then
339        shift
340        run_args="${run_args} --instruction-set-features $1"
341        shift
342    elif expr "x$1" : "x--" >/dev/null 2>&1; then
343        echo "unknown $0 option: $1" 1>&2
344        usage="yes"
345        break
346    else
347        break
348    fi
349done
350
351# Allocate file descriptor real_stderr and redirect it to the shell's error
352# output (fd 2).
353if [ ${BASH_VERSINFO[1]} -ge 4 ] && [ ${BASH_VERSINFO[2]} -ge 1 ]; then
354  exec {real_stderr}>&2
355else
356  # In bash before version 4.1 we need to do a manual search for free file
357  # descriptors.
358  FD=3
359  while [ -e /dev/fd/$FD ]; do FD=$((FD + 1)); done
360  real_stderr=$FD
361  eval "exec ${real_stderr}>&2"
362fi
363if [ "$quiet" = "yes" ]; then
364  # Force the default standard output and error to go to /dev/null so we will
365  # not print them.
366  exec 1>/dev/null
367  exec 2>/dev/null
368fi
369
370function err_echo() {
371  echo "$@" 1>&${real_stderr}
372}
373
374# tmp_dir may be relative, resolve.
375#
376# Cannot use realpath, as it does not exist on Mac.
377# Cannot us a simple "cd", as the path might not be created yet.
378# Cannot use readlink -m, as it does not exist on Mac.
379# Fallback to nuclear option:
380noncanonical_tmp_dir=$tmp_dir
381tmp_dir="`cd $oldwd ; python -c "import os; print os.path.realpath('$tmp_dir')"`"
382mkdir -p $tmp_dir
383
384if [ "$basic_verify" = "true" ]; then
385  # Set HspaceCompactForOOMMinIntervalMs to zero to run hspace compaction for OOM more frequently in tests.
386  run_args="${run_args} --runtime-option -Xgc:preverify --runtime-option -Xgc:postverify --runtime-option -XX:HspaceCompactForOOMMinIntervalMs=0"
387fi
388if [ "$gc_verify" = "true" ]; then
389  run_args="${run_args} --runtime-option -Xgc:preverify_rosalloc --runtime-option -Xgc:postverify_rosalloc"
390fi
391if [ "$gc_stress" = "true" ]; then
392  run_args="${run_args} --gc-stress --runtime-option -Xgc:SS,gcstress --runtime-option -Xms2m --runtime-option -Xmx16m"
393fi
394if [ "$trace" = "true" ]; then
395    run_args="${run_args} --runtime-option -Xmethod-trace --runtime-option -Xmethod-trace-file-size:2000000"
396    if [ "$trace_stream" = "true" ]; then
397        # Streaming mode uses the file size as the buffer size. So output gets really large. Drop
398        # the ability to analyze the file and just write to /dev/null.
399        run_args="${run_args} --runtime-option -Xmethod-trace-file:/dev/null"
400        # Enable streaming mode.
401        run_args="${run_args} --runtime-option -Xmethod-trace-stream"
402    else
403        run_args="${run_args} --runtime-option -Xmethod-trace-file:${DEX_LOCATION}/trace.bin"
404    fi
405elif [ "$trace_stream" = "true" ]; then
406    err_echo "Cannot use --stream without --trace."
407    exit 1
408fi
409
410# Most interesting target architecture variables are Makefile variables, not environment variables.
411# Try to map the suffix64 flag and what we find in ${ANDROID_PRODUCT_OUT}/data/art-test to an architecture name.
412function guess_target_arch_name() {
413    grep32bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm|x86|mips)$'`
414    grep64bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm64|x86_64|mips64)$'`
415    if [ "x${suffix64}" = "x64" ]; then
416        target_arch_name=${grep64bit}
417    else
418        target_arch_name=${grep32bit}
419    fi
420}
421
422function guess_host_arch_name() {
423    if [ "x${suffix64}" = "x64" ]; then
424        host_arch_name="x86_64"
425    else
426        host_arch_name="x86"
427    fi
428}
429
430if [ "$target_mode" = "no" ]; then
431    if [ "$runtime" = "jvm" ]; then
432        if [ "$prebuild_mode" = "yes" ]; then
433            err_echo "--prebuild with --jvm is unsupported"
434            exit 1;
435        fi
436    fi
437fi
438
439if [ "$have_patchoat" = "no" ]; then
440  run_args="${run_args} --no-patchoat"
441fi
442
443if [ "$have_dex2oat" = "no" ]; then
444  run_args="${run_args} --no-dex2oat"
445fi
446
447if [ ! "$runtime" = "jvm" ]; then
448  run_args="${run_args} --lib $lib"
449fi
450
451if [ "$runtime" = "dalvik" ]; then
452    if [ "$target_mode" = "no" ]; then
453        framework="${ANDROID_PRODUCT_OUT}/system/framework"
454        bpath="${framework}/core-libart.jar:${framework}/core-oj.jar:${framework}/conscrypt.jar:${framework}/okhttp.jar:${framework}/bouncycastle.jar:${framework}/ext.jar"
455        run_args="${run_args} --boot --runtime-option -Xbootclasspath:${bpath}"
456    else
457        true # defaults to using target BOOTCLASSPATH
458    fi
459elif [ "$runtime" = "art" ]; then
460    if [ "$target_mode" = "no" ]; then
461        # ANDROID_HOST_OUT is not set in a build environment.
462        if [ -z "$ANDROID_HOST_OUT" ]; then
463            export ANDROID_HOST_OUT=${OUT_DIR:-$ANDROID_BUILD_TOP/out/}host/linux-x86
464        fi
465        guess_host_arch_name
466        run_args="${run_args} --boot ${ANDROID_HOST_OUT}/framework/core${image_suffix}${pic_image_suffix}${multi_image_suffix}.art"
467        run_args="${run_args} --runtime-option -Djava.library.path=${ANDROID_HOST_OUT}/lib${suffix64}"
468    else
469        guess_target_arch_name
470        run_args="${run_args} --runtime-option -Djava.library.path=/data/art-test/${target_arch_name}:/system/lib${suffix64}"
471        run_args="${run_args} --boot /data/art-test/core${image_suffix}${pic_image_suffix}${multi_image_suffix}.art"
472    fi
473    if [ "$relocate" = "yes" ]; then
474      run_args="${run_args} --relocate"
475    else
476      run_args="${run_args} --no-relocate"
477    fi
478elif [ "$runtime" = "jvm" ]; then
479    # TODO: Detect whether the host is 32-bit or 64-bit.
480    run_args="${run_args} --runtime-option -Djava.library.path=${ANDROID_HOST_OUT}/lib64"
481fi
482
483if [ "$have_image" = "no" ]; then
484    if [ "$runtime" != "art" ]; then
485        err_echo "--no-image is only supported on the art runtime"
486        exit 1
487    fi
488    if [ "$target_mode" = "no" ]; then
489        framework="${ANDROID_HOST_OUT}/framework"
490        bpath_suffix="-hostdex"
491    else
492        framework="${android_root}/framework"
493        bpath_suffix=""
494    fi
495    # TODO If the target was compiled WITH_DEXPREOPT=true then these tests will
496    # fail since these jar files will be stripped.
497    bpath="${framework}/core-libart${bpath_suffix}.jar"
498    bpath="${bpath}:${framework}/core-oj${bpath_suffix}.jar"
499    bpath="${bpath}:${framework}/conscrypt${bpath_suffix}.jar"
500    bpath="${bpath}:${framework}/okhttp${bpath_suffix}.jar"
501    bpath="${bpath}:${framework}/bouncycastle${bpath_suffix}.jar"
502    # Pass down the bootclasspath
503    run_args="${run_args} --runtime-option -Xbootclasspath:${bpath}"
504    run_args="${run_args} --no-image"
505fi
506
507if [ "$dev_mode" = "yes" -a "$update_mode" = "yes" ]; then
508    err_echo "--dev and --update are mutually exclusive"
509    usage="yes"
510fi
511
512if [ "$dev_mode" = "yes" -a "$quiet" = "yes" ]; then
513    err_echo "--dev and --quiet are mutually exclusive"
514    usage="yes"
515fi
516
517if [ "$usage" = "no" ]; then
518    if [ "x$1" = "x" -o "x$1" = "x-" ]; then
519        test_dir=`basename "$oldwd"`
520    else
521        test_dir="$1"
522    fi
523
524    if [ '!' -d "$test_dir" ]; then
525        td2=`echo ${test_dir}-*`
526        if [ '!' -d "$td2" ]; then
527            err_echo "${test_dir}: no such test directory"
528            usage="yes"
529        fi
530        test_dir="$td2"
531    fi
532    # Shift to get rid of the test name argument. The rest of the arguments
533    # will get passed to the test run.
534    shift
535fi
536
537if [ "$usage" = "yes" ]; then
538    prog=`basename $prog`
539    (
540        echo "usage:"
541        echo "  $prog --help                          Print this message."
542        echo "  $prog [options] [test-name]           Run test normally."
543        echo "  $prog --dev [options] [test-name]     Development mode" \
544             "(dumps to stdout)."
545        echo "  $prog --update [options] [test-name]  Update mode" \
546             "(replaces expected.txt)."
547        echo '  Omitting the test name or specifying "-" will use the' \
548             "current directory."
549        echo "  Runtime Options:"
550        echo "    -O                    Run non-debug rather than debug build (off by default)."
551        echo "    -Xcompiler-option     Pass an option to the compiler."
552        echo "    --runtime-option      Pass an option to the runtime."
553        echo "    --debug               Wait for a debugger to attach."
554        echo "    --debuggable          Whether to compile Java code for a debugger."
555        echo "    --gdb                 Run under gdb; incompatible with some tests."
556        echo "    --gdb-arg             Pass an option to gdb."
557        echo "    --build-only          Build test files only (off by default)."
558        echo "    --build-with-javac-dx Build test files with javac and dx (on by default)."
559        echo "    --build-with-jack     Build test files with jack and jill (off by default)."
560        echo "    --interpreter         Enable interpreter only mode (off by default)."
561        echo "    --jit                 Enable jit (off by default)."
562        echo "    --optimizing          Enable optimizing compiler (default)."
563        echo "    --no-verify           Turn off verification (on by default)."
564        echo "    --verify-soft-fail    Force soft fail verification (off by default)."
565        echo "                          Verification is enabled if neither --no-verify"
566        echo "                          nor --verify-soft-fail is specified."
567        echo "    --no-optimize         Turn off optimization (on by default)."
568        echo "    --no-precise          Turn off precise GC (on by default)."
569        echo "    --zygote              Spawn the process from the Zygote." \
570             "If used, then the"
571        echo "                          other runtime options are ignored."
572        echo "    --no-dex2oat          Run as though dex2oat was failing."
573        echo "    --no-patchoat         Run as though patchoat was failing."
574        echo "    --prebuild            Run dex2oat on the files before starting test. (default)"
575        echo "    --no-prebuild         Do not run dex2oat on the files before starting"
576        echo "                          the test."
577        echo "    --strip-dex           Strip the dex files before starting test."
578        echo "    --relocate            Force the use of relocating in the test, making"
579        echo "                          the image and oat files be relocated to a random"
580        echo "                          address before running. (default)"
581        echo "    --no-relocate         Force the use of no relocating in the test"
582        echo "    --image               Run the test using a precompiled boot image. (default)"
583        echo "    --no-image            Run the test without a precompiled boot image."
584        echo "    --host                Use the host-mode virtual machine."
585        echo "    --invoke-with         Pass --invoke-with option to runtime."
586        echo "    --dalvik              Use Dalvik (off by default)."
587        echo "    --jvm                 Use a host-local RI virtual machine."
588        echo "    --use-java-home       Use the JAVA_HOME environment variable"
589        echo "                          to find the java compiler and runtime"
590        echo "                          (if applicable) to run the test with."
591        echo "    --output-path [path]  Location where to store the build" \
592             "files."
593        echo "    --64                  Run the test in 64-bit mode"
594        echo "    --trace               Run with method tracing"
595        echo "    --strace              Run with syscall tracing from strace."
596        echo "    --stream              Run method tracing in streaming mode (requires --trace)"
597        echo "    --gcstress            Run with gc stress testing"
598        echo "    --gcverify            Run with gc verification"
599        echo "    --always-clean        Delete the test files even if the test fails."
600        echo "    --never-clean         Keep the test files even if the test succeeds."
601        echo "    --android-root [path] The path on target for the android root. (/system by default)."
602        echo "    --dex2oat-swap        Use a dex2oat swap file."
603        echo "    --instruction-set-features [string]"
604        echo "                          Set instruction-set-features for compilation."
605        echo "    --pic-image           Use an image compiled with position independent code for the"
606        echo "                          boot class path."
607        echo "    --multi-image         Use a set of images compiled with dex2oat multi-image for"
608        echo "                          the boot class path."
609        echo "    --pic-test            Compile the test code position independent."
610        echo "    --quiet               Don't print anything except failure messages"
611    ) 1>&2  # Direct to stderr so usage is not printed if --quiet is set.
612    exit 1
613fi
614
615cd "$test_dir"
616test_dir=`pwd`
617
618td_info="${test_dir}/${info}"
619td_expected="${test_dir}/${expected}"
620
621if [ ! -r $td_info ]; then
622    err_echo "${test_dir}: missing file $td_info"
623    exit 1
624fi
625
626if [ ! -r $td_expected ]; then
627    err_echo "${test_dir}: missing file $td_expected"
628    exit 1
629fi
630
631# copy the test to a temp dir and run it
632
633echo "${test_dir}: building..." 1>&2
634
635rm -rf "$tmp_dir"
636cp -Rp "$test_dir" "$tmp_dir"
637cd "$tmp_dir"
638
639if [ '!' -r "$build" ]; then
640    cp "${progdir}/etc/default-build" build
641else
642    cp "${progdir}/etc/default-build" .
643fi
644
645if [ '!' -r "$run" ]; then
646    cp "${progdir}/etc/default-run" run
647else
648    cp "${progdir}/etc/default-run" .
649fi
650
651if [ '!' -r "$check_cmd" ]; then
652    cp "${progdir}/etc/default-check" check
653else
654    cp "${progdir}/etc/default-check" .
655fi
656
657chmod 755 "$build"
658chmod 755 "$run"
659chmod 755 "$check_cmd"
660
661export TEST_NAME=`basename ${test_dir}`
662
663# arch_supports_read_barrier ARCH
664# -------------------------------
665# Return whether the Optimizing compiler has read barrier support for ARCH.
666function arch_supports_read_barrier() {
667  # Optimizing has read barrier support for ARM, ARM64, x86 and x86-64 at the
668  # moment.
669  [ "x$1" = xarm ] || [ "x$1" = xarm64 ] || [ "x$1" = xx86 ] || [ "x$1" = xx86_64 ]
670}
671
672# Tests named '<number>-checker-*' will also have their CFGs verified with
673# Checker when compiled with Optimizing on host.
674if [[ "$TEST_NAME" =~ ^[0-9]+-checker- ]]; then
675  if [ "$runtime" = "art" -a "$image_suffix" = "-optimizing" -a "$USE_JACK" = "true" ]; then
676    # Optimizing has read barrier support for certain architectures
677    # only. On other architectures, compiling is disabled when read
678    # barriers are enabled, meaning that we do not produce a CFG file
679    # as a side-effect of compilation, thus the Checker assertions
680    # cannot be checked. Disable Checker for those cases.
681    #
682    # TODO: Enable Checker when read barrier support is added to more
683    # architectures (b/12687968).
684    if [ "x$ART_USE_READ_BARRIER" = xtrue ]                    \
685       && (([ "x$host_mode" = "xyes" ]                         \
686            && ! arch_supports_read_barrier "$host_arch_name") \
687           || ([ "x$target_mode" = "xyes" ]                    \
688               && ! arch_supports_read_barrier "$target_arch_name")); then
689      run_checker="no"
690    # In no-prebuild mode, the compiler is only invoked if both dex2oat and
691    # patchoat are available. Disable Checker otherwise (b/22552692).
692    elif [ "$prebuild_mode" = "yes" ] \
693         || [ "$have_patchoat" = "yes" -a "$have_dex2oat" = "yes" ]; then
694      run_checker="yes"
695
696      if [ "$target_mode" = "no" ]; then
697        cfg_output_dir="$tmp_dir"
698        checker_args="--arch=${host_arch_name^^}"
699      else
700        cfg_output_dir="$DEX_LOCATION"
701        checker_args="--arch=${target_arch_name^^}"
702      fi
703
704      if [ "$debuggable" = "yes" ]; then
705        checker_args="$checker_args --debuggable"
706      fi
707
708      run_args="${run_args} -Xcompiler-option --dump-cfg=$cfg_output_dir/$cfg_output \
709                            -Xcompiler-option -j1"
710    fi
711  fi
712fi
713
714if [ "$runtime" != "jvm" ]; then
715  run_args="${run_args} --testlib ${testlib}"
716fi
717
718# To cause tests to fail fast, limit the file sizes created by dx, dex2oat and ART output to 2MB.
719build_file_size_limit=2048
720run_file_size_limit=2048
721
722# Add tests requiring a higher ulimit to this list. Ulimits might need to be raised to deal with
723# large amounts of expected output or large generated files.
724if echo "$test_dir" | grep -Eq "(083|089|961|964|971)" > /dev/null; then
725  build_file_size_limit=5120
726  run_file_size_limit=5120
727fi
728if [ "$run_checker" = "yes" -a "$target_mode" = "yes" ]; then
729  # We will need to `adb pull` the .cfg output from the target onto the host to
730  # run checker on it. This file can be big.
731  build_file_size_limit=24576
732  run_file_size_limit=24576
733fi
734if [ ${USE_JACK} = "false" ]; then
735  # Set ulimit if we build with dx only, Jack can generate big temp files.
736  if ! ulimit -S "$build_file_size_limit"; then
737    err_echo "ulimit file size setting failed"
738  fi
739fi
740
741good="no"
742good_build="yes"
743good_run="yes"
744if [ "$dev_mode" = "yes" ]; then
745    "./${build}" $build_args 2>&1
746    build_exit="$?"
747    echo "build exit status: $build_exit" 1>&2
748    if [ "$build_exit" = '0' ]; then
749        if ! ulimit -S "$run_file_size_limit"; then
750          err_echo "ulimit file size setting failed"
751        fi
752        echo "${test_dir}: running..." 1>&2
753        "./${run}" $run_args "$@" 2>&1
754        run_exit="$?"
755
756        if [ "$run_exit" = "0" ]; then
757            if [ "$run_checker" = "yes" ]; then
758                if [ "$target_mode" = "yes" ]; then
759                  adb pull $cfg_output_dir/$cfg_output &> /dev/null
760                fi
761                "$checker" $checker_args "$cfg_output" "$tmp_dir" 2>&1
762                checker_exit="$?"
763                if [ "$checker_exit" = "0" ]; then
764                    good="yes"
765                fi
766                err_echo "checker exit status: $checker_exit"
767            else
768                good="yes"
769            fi
770        fi
771        echo "run exit status: $run_exit" 1>&2
772    fi
773elif [ "$update_mode" = "yes" ]; then
774    "./${build}" $build_args >"$build_output" 2>&1
775    build_exit="$?"
776    if [ "$build_exit" = '0' ]; then
777        if ! ulimit -S "$run_file_size_limit"; then
778          err_echo "ulimit file size setting failed"
779        fi
780        echo "${test_dir}: running..." 1>&2
781        "./${run}" $run_args "$@" >"$output" 2>&1
782        if [ "$run_checker" = "yes" ]; then
783          if [ "$target_mode" = "yes" ]; then
784            adb pull $cfg_output_dir/$cfg_output &> /dev/null
785          fi
786          "$checker" -q $checker_args "$cfg_output" "$tmp_dir" >> "$output" 2>&1
787        fi
788        sed -e 's/[[:cntrl:]]$//g' < "$output" >"${td_expected}"
789        good="yes"
790    else
791        cat "$build_output" 1>&${real_stderr} 1>&2
792        err_echo "build exit status: $build_exit"
793    fi
794elif [ "$build_only" = "yes" ]; then
795    good="yes"
796    "./${build}" $build_args >"$build_output" 2>&1
797    build_exit="$?"
798    if [ "$build_exit" '!=' '0' ]; then
799        cp "$build_output" "$output"
800        echo "build exit status: $build_exit" >>"$output"
801        diff --strip-trailing-cr -q "$expected" "$output" >/dev/null
802        if [ "$?" '!=' "0" ]; then
803            good="no"
804            err_echo "BUILD FAILED For ${TEST_NAME}"
805        fi
806    fi
807    # Clean up extraneous files that are not used by tests.
808    find $tmp_dir -mindepth 1  ! -regex ".*/\(.*jar\|$output\|$expected\)" | xargs rm -rf
809    exit 0
810else
811    "./${build}" $build_args >"$build_output" 2>&1
812    build_exit="$?"
813    if [ "$build_exit" = '0' ]; then
814        if ! ulimit -S "$run_file_size_limit"; then
815          err_echo "ulimit file size setting failed"
816        fi
817        echo "${test_dir}: running..." 1>&2
818        "./${run}" $run_args "$@" >"$output" 2>&1
819        run_exit="$?"
820        if [ "$run_exit" != "0" ]; then
821            err_echo "run exit status: $run_exit"
822            good_run="no"
823        elif [ "$run_checker" = "yes" ]; then
824            if [ "$target_mode" = "yes" ]; then
825              adb pull $cfg_output_dir/$cfg_output &> /dev/null
826            fi
827            "$checker" -q $checker_args "$cfg_output" "$tmp_dir" >> "$output" 2>&1
828            checker_exit="$?"
829            if [ "$checker_exit" != "0" ]; then
830                err_echo "checker exit status: $checker_exit"
831                good_run="no"
832            else
833                good_run="yes"
834            fi
835        else
836            good_run="yes"
837        fi
838    else
839        good_build="no"
840        cp "$build_output" "$output"
841        echo "Failed to build in tmpdir=${tmp_dir} from oldwd=${oldwd} and cwd=`pwd`" >> "$output"
842        echo "Non-canonical tmpdir was ${noncanonical_tmp_dir}" >> "$output"
843        echo "Args: ${args}" >> "$output"
844        echo "build exit status: $build_exit" >> "$output"
845        max_name_length=$(getconf NAME_MAX ${tmp_dir})
846        echo "Max filename (NAME_MAX): ${max_name_length}" >> "$output"
847        max_path_length=$(getconf PATH_MAX ${tmp_dir})
848        echo "Max pathlength (PATH_MAX): ${max_path_length}" >> "$output"
849    fi
850    ./$check_cmd "$expected" "$output"
851    if [ "$?" = "0" ]; then
852        if [ "$good_build" = "no" -o "$good_run" = "yes" ]; then
853          # output == expected
854          good="yes"
855          echo "${test_dir}: succeeded!" 1>&2
856        fi
857    fi
858fi
859
860(
861    if [ "$good" != "yes" -a "$update_mode" != "yes" ]; then
862        echo "${test_dir}: FAILED!"
863        echo ' '
864        echo '#################### info'
865        cat "${td_info}" | sed 's/^/# /g'
866        echo '#################### diffs'
867        diff --strip-trailing-cr -u "$expected" "$output" | tail -n 3000
868        echo '####################'
869        if [ "$strace" = "yes" ]; then
870            echo '#################### strace output'
871            tail -n 3000 "$tmp_dir/$strace_output"
872            echo '####################'
873        fi
874        echo ' '
875    fi
876
877) 2>&${real_stderr} 1>&2
878
879# Clean up test files.
880if [ "$always_clean" = "yes" -o "$good" = "yes" ] && [ "$never_clean" = "no" ]; then
881    cd "$oldwd"
882    rm -rf "$tmp_dir"
883    if [ "$target_mode" = "yes" -a "$build_exit" = "0" ]; then
884        adb shell rm -rf $DEX_LOCATION
885    fi
886    if [ "$good" = "yes" ]; then
887        exit 0
888    fi
889fi
890
891
892(
893    if [ "$always_clean" = "yes" ]; then
894        echo "${TEST_NAME} files deleted from host "
895        if [ "$target_mode" == "yes" ]; then
896            echo "and from target"
897        fi
898    else
899        echo "${TEST_NAME} files left in ${tmp_dir} on host"
900        if [ "$target_mode" == "yes" ]; then
901            echo "and in ${DEX_LOCATION} on target"
902        fi
903    fi
904
905) 2>&${real_stderr} 1>&2
906
907exit 1
908