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}/${test_dir}"
41fi
42checker="${progdir}/../tools/checker/checker.py"
43export JAVA="java"
44export JAVAC="javac -g -Xlint:-options -source 1.8 -target 1.8"
45export RUN="${progdir}/etc/run-test-jar"
46export DEX_LOCATION=/data/run-test/${test_dir}
47export NEED_DEX="true"
48export USE_D8="true"
49export USE_DESUGAR="true"
50export SMALI_ARGS=""
51
52# If d8 was not set by the environment variable, assume it is in the path.
53if [ -z "$D8" ]; then
54  export D8="d8"
55fi
56
57# If dx was not set by the environment variable, assume it is in the path.
58if [ -z "$DX" ]; then
59  export DX="d8-compat-dx"
60fi
61
62export DEXMERGER="$D8"
63
64# If jasmin was not set by the environment variable, assume it is in the path.
65if [ -z "$JASMIN" ]; then
66  export JASMIN="jasmin"
67fi
68
69# If smali was not set by the environment variable, assume it is in the path.
70if [ -z "$SMALI" ]; then
71  export SMALI="smali"
72fi
73
74# ANDROID_BUILD_TOP is not set in a build environment.
75if [ -z "$ANDROID_BUILD_TOP" ]; then
76    export ANDROID_BUILD_TOP=$oldwd
77fi
78
79# OUT_DIR defaults to out, and may be relative to $ANDROID_BUILD_TOP.
80# Convert it to an absolute path, since we cd into the tmp_dir to run the tests.
81export OUT_DIR=${OUT_DIR:-out}
82if [[ "$OUT_DIR" != /* ]]; then
83    export OUT_DIR=$ANDROID_BUILD_TOP/$OUT_DIR
84fi
85
86# ANDROID_HOST_OUT is not set in a build environment.
87if [ -z "$ANDROID_HOST_OUT" ]; then
88    export ANDROID_HOST_OUT=${OUT_DIR}/host/linux-x86
89fi
90
91host_lib_root=${ANDROID_HOST_OUT}
92
93# Allow changing DESUGAR script to something else, or to disable it with DESUGAR=false.
94if [ -z "$DESUGAR" ]; then
95  export DESUGAR="$ANDROID_BUILD_TOP/art/tools/desugar.sh"
96fi
97
98# Zipalign is not on the PATH in some configs, auto-detect it.
99if [ -z "$ZIPALIGN" ]; then
100  if which zipalign >/dev/null; then
101    ZIPALIGN="zipalign";
102  else
103    # TODO: Add a dependency for zipalign in Android.run-test.mk
104    # once it doesn't depend on libandroidfw (b/35246701)
105    case "$OSTYPE" in
106      darwin*)  ZIPALIGN="$ANDROID_BUILD_TOP/prebuilts/sdk/tools/darwin/bin/zipalign" ;;
107      linux*)   ZIPALIGN="$ANDROID_BUILD_TOP/prebuilts/sdk/tools/linux/bin/zipalign" ;;
108      *)        echo "Can't find zipalign: unknown: $OSTYPE" >&2;;
109    esac
110  fi
111fi
112export ZIPALIGN
113
114# If hiddenapi was not set by the environment variable, assume it is in
115# ANDROID_HOST_OUT.
116if [ -z "$HIDDENAPI" ]; then
117  export HIDDENAPI="${ANDROID_HOST_OUT}/bin/hiddenapi"
118fi
119
120chroot=
121
122info="info.txt"
123build="build"
124run="run"
125expected="expected.txt"
126check_cmd="check"
127output="output.txt"
128build_output="build-output.txt"
129cfg_output="graph.cfg"
130strace_output="strace-output.txt"
131lib="libartd.so"
132testlib="arttestd"
133run_args=(--quiet)
134build_args=""
135
136quiet="no"
137debuggable="no"
138prebuild_mode="yes"
139target_mode="yes"
140dev_mode="no"
141create_runner="no"
142update_mode="no"
143debug_mode="no"
144relocate="no"
145runtime="art"
146usage="no"
147build_only="no"
148suffix64=""
149trace="false"
150trace_stream="false"
151basic_verify="false"
152gc_verify="false"
153gc_stress="false"
154jvmti_trace_stress="false"
155jvmti_field_stress="false"
156jvmti_step_stress="false"
157jvmti_redefine_stress="false"
158strace="false"
159always_clean="no"
160never_clean="no"
161have_image="yes"
162android_root="/system"
163bisection_search="no"
164timeout=""
165suspend_timeout="500000"
166run_optimizing="false"
167
168# To cause tests to fail fast, limit the file sizes created by dx, dex2oat and
169# ART output to approximately 128MB. This should be more than sufficient
170# for any test while still catching cases of runaway output.
171# Set a hard limit to encourage ART developers to increase the ulimit here if
172# needed to support a test case rather than resetting the limit in the run
173# script for the particular test in question. Adjust this if needed for
174# particular configurations.
175file_ulimit=128000
176
177
178while true; do
179    if [ "x$1" = "x--host" ]; then
180        target_mode="no"
181        DEX_LOCATION=$tmp_dir
182        run_args+=(--host)
183        shift
184    elif [ "x$1" = "x--quiet" ]; then
185        quiet="yes"
186        shift
187    elif [ "x$1" = "x--use-java-home" ]; then
188        if [ -n "${JAVA_HOME}" ]; then
189          export JAVA="${JAVA_HOME}/bin/java"
190          export JAVAC="${JAVA_HOME}/bin/javac -g"
191        else
192          echo "Passed --use-java-home without JAVA_HOME variable set!"
193          usage="yes"
194        fi
195        shift
196    elif [ "x$1" = "x--jvm" ]; then
197        target_mode="no"
198        DEX_LOCATION="$tmp_dir"
199        runtime="jvm"
200        prebuild_mode="no"
201        NEED_DEX="false"
202        run_args+=(--jvm)
203        shift
204    elif [ "x$1" = "x-O" ]; then
205        lib="libart.so"
206        testlib="arttest"
207        run_args+=(-O)
208        shift
209    elif [ "x$1" = "x--dalvik" ]; then
210        lib="libdvm.so"
211        runtime="dalvik"
212        shift
213    elif [ "x$1" = "x--no-image" ]; then
214        have_image="no"
215        shift
216    elif [ "x$1" = "x--relocate" ]; then
217        relocate="yes"
218        shift
219    elif [ "x$1" = "x--no-relocate" ]; then
220        relocate="no"
221        shift
222    elif [ "x$1" = "x--prebuild" ]; then
223        run_args+=(--prebuild)
224        prebuild_mode="yes"
225        shift;
226    elif [ "x$1" = "x--compact-dex-level" ]; then
227        option="$1"
228        shift
229        run_args+=("$option" "$1")
230        shift;
231    elif [ "x$1" = "x--strip-dex" ]; then
232        run_args+=(--strip-dex)
233        shift;
234    elif [ "x$1" = "x--debuggable" ]; then
235        run_args+=(-Xcompiler-option --debuggable)
236        debuggable="yes"
237        shift;
238    elif [ "x$1" = "x--no-prebuild" ]; then
239        run_args+=(--no-prebuild)
240        prebuild_mode="no"
241        shift;
242    elif [ "x$1" = "x--gcverify" ]; then
243        basic_verify="true"
244        gc_verify="true"
245        shift
246    elif [ "x$1" = "x--gcstress" ]; then
247        basic_verify="true"
248        gc_stress="true"
249        shift
250    elif [ "x$1" = "x--jvmti-step-stress" ]; then
251        jvmti_step_stress="true"
252        shift
253    elif [ "x$1" = "x--jvmti-redefine-stress" ]; then
254        jvmti_redefine_stress="true"
255        shift
256    elif [ "x$1" = "x--jvmti-field-stress" ]; then
257        jvmti_field_stress="true"
258        shift
259    elif [ "x$1" = "x--jvmti-trace-stress" ]; then
260        jvmti_trace_stress="true"
261        shift
262    elif [ "x$1" = "x--suspend-timeout" ]; then
263        shift
264        suspend_timeout="$1"
265        shift
266    elif [ "x$1" = "x--image" ]; then
267        shift
268        image="$1"
269        run_args+=(--image "$image")
270        shift
271    elif [ "x$1" = "x-Xcompiler-option" ]; then
272        shift
273        option="$1"
274        run_args+=(-Xcompiler-option "$option")
275        shift
276    elif [ "x$1" = "x--build-option" ]; then
277        shift
278        option="$1"
279        build_args="${build_args} $option"
280        shift
281    elif [ "x$1" = "x--runtime-option" ]; then
282        shift
283        option="$1"
284        run_args+=(--runtime-option "$option")
285        shift
286    elif [ "x$1" = "x--gdb-arg" ]; then
287        shift
288        gdb_arg="$1"
289        run_args+=(--gdb-arg "$gdb_arg")
290        shift
291    elif [ "x$1" = "x--debug" ]; then
292        run_args+=(--debug)
293        shift
294    elif [ "x$1" = "x--debug-wrap-agent" ]; then
295        run_args+=(--debug-wrap-agent)
296        shift
297    elif [ "x$1" = "x--with-agent" ]; then
298        shift
299        option="$1"
300        run_args+=(--with-agent "$1")
301        shift
302    elif [ "x$1" = "x--debug-agent" ]; then
303        shift
304        option="$1"
305        run_args+=(--debug-agent "$1")
306        shift
307    elif [ "x$1" = "x--gdb" ]; then
308        run_args+=(--gdb)
309        dev_mode="yes"
310        shift
311    elif [ "x$1" = "x--gdbserver-bin" ]; then
312        shift
313        run_args+=(--gdbserver-bin "$1")
314        shift
315    elif [ "x$1" = "x--gdbserver-port" ]; then
316        shift
317        run_args+=(--gdbserver-port "$1")
318        shift
319    elif [ "x$1" = "x--gdbserver" ]; then
320        run_args+=(--gdbserver)
321        dev_mode="yes"
322        shift
323    elif [ "x$1" = "x--strace" ]; then
324        strace="yes"
325        run_args+=(--invoke-with strace --invoke-with -o --invoke-with "$tmp_dir/$strace_output")
326        timeout="${timeout:-1800}"
327        shift
328    elif [ "x$1" = "x--zygote" ]; then
329        run_args+=(--zygote)
330        shift
331    elif [ "x$1" = "x--interpreter" ]; then
332        run_args+=(--interpreter)
333        shift
334    elif [ "x$1" = "x--jit" ]; then
335        run_args+=(--jit)
336        shift
337    elif [ "x$1" = "x--baseline" ]; then
338        run_args+=(--baseline)
339        shift
340    elif [ "x$1" = "x--optimizing" ]; then
341        run_optimizing="true"
342        shift
343    elif [ "x$1" = "x--no-verify" ]; then
344        run_args+=(--no-verify)
345        shift
346    elif [ "x$1" = "x--verify-soft-fail" ]; then
347        run_args+=(--verify-soft-fail)
348        shift
349    elif [ "x$1" = "x--no-optimize" ]; then
350        run_args+=(--no-optimize)
351        shift
352    elif [ "x$1" = "x--no-precise" ]; then
353        run_args+=(--no-precise)
354        shift
355    elif [ "x$1" = "x--invoke-with" ]; then
356        shift
357        what="$1"
358        if [ "x$what" = "x" ]; then
359            echo "$0 missing argument to --invoke-with" 1>&2
360            usage="yes"
361            break
362        fi
363        run_args+=(--invoke-with "${what}")
364        shift
365    elif [ "x$1" = "x--create-runner" ]; then
366        run_args+=(--create-runner --dry-run)
367        dev_mode="yes"
368        never_clean="yes"
369        create_runner="yes"
370        shift
371    elif [ "x$1" = "x--dev" ]; then
372        run_args+=(--dev)
373        dev_mode="yes"
374        shift
375    elif [ "x$1" = "x--build-only" ]; then
376        build_only="yes"
377        shift
378    elif [ "x$1" = "x--output-path" ]; then
379        shift
380        tmp_dir=$1
381        if [ "x$tmp_dir" = "x" ]; then
382            echo "$0 missing argument to --output-path" 1>&2
383            usage="yes"
384            break
385        fi
386        shift
387    elif [ "x$1" = "x--chroot" ]; then
388        shift
389        if [ "x$1" = "x" ]; then
390            echo "$0 missing argument to --chroot" 1>&2
391            usage="yes"
392            break
393        fi
394        chroot="$1"
395        run_args+=(--chroot "$1")
396        shift
397    elif [ "x$1" = "x--android-root" ]; then
398        shift
399        if [ "x$1" = "x" ]; then
400            echo "$0 missing argument to --android-root" 1>&2
401            usage="yes"
402            break
403        fi
404        android_root="$1"
405        run_args+=(--android-root "$1")
406        shift
407    elif [ "x$1" = "x--android-art-root" ]; then
408        shift
409        if [ "x$1" = "x" ]; then
410            echo "$0 missing argument to --android-art-root" 1>&2
411            usage="yes"
412            break
413        fi
414        run_args+=(--android-art-root "$1")
415        shift
416    elif [ "x$1" = "x--android-tzdata-root" ]; then
417        shift
418        if [ "x$1" = "x" ]; then
419            echo "$0 missing argument to --android-tzdata-root" 1>&2
420            usage="yes"
421            break
422        fi
423        run_args+=(--android-tzdata-root "$1")
424        shift
425    elif [ "x$1" = "x--update" ]; then
426        update_mode="yes"
427        shift
428    elif [ "x$1" = "x--help" ]; then
429        usage="yes"
430        shift
431    elif [ "x$1" = "x--64" ]; then
432        run_args+=(--64)
433        suffix64="64"
434        shift
435    elif [ "x$1" = "x--bionic" ]; then
436        # soong linux_bionic builds are 64bit only.
437        run_args+=(--bionic --host --64)
438        suffix64="64"
439        target_mode="no"
440        DEX_LOCATION=$tmp_dir
441        host_lib_root=$OUT_DIR/soong/host/linux_bionic-x86
442        shift
443    elif [ "x$1" = "x--runtime-extracted-zipapex" ]; then
444        shift
445        # TODO Should we allow the java.library.path to search the zipapex too?
446        # Not needed at the moment and adding it will be complicated so for now
447        # we'll ignore this.
448        run_args+=(--host --runtime-extracted-zipapex "$1")
449        target_mode="no"
450        DEX_LOCATION=$tmp_dir
451        shift
452    elif [ "x$1" = "x--runtime-zipapex" ]; then
453        shift
454        # TODO Should we allow the java.library.path to search the zipapex too?
455        # Not needed at the moment and adding it will be complicated so for now
456        # we'll ignore this.
457        run_args+=(--host --runtime-zipapex "$1")
458        target_mode="no"
459        DEX_LOCATION=$tmp_dir
460        # apex_payload.zip is quite large we need a high enough ulimit to
461        # extract it. 512mb should be good enough.
462        file_ulimit=512000
463        shift
464    elif [ "x$1" = "x--timeout" ]; then
465        shift
466        if [ "x$1" = "x" ]; then
467            echo "$0 missing argument to --timeout" 1>&2
468            usage="yes"
469            break
470        fi
471        timeout="$1"
472        shift
473    elif [ "x$1" = "x--trace" ]; then
474        trace="true"
475        shift
476    elif [ "x$1" = "x--stream" ]; then
477        trace_stream="true"
478        shift
479    elif [ "x$1" = "x--always-clean" ]; then
480        always_clean="yes"
481        shift
482    elif [ "x$1" = "x--never-clean" ]; then
483        never_clean="yes"
484        shift
485    elif [ "x$1" = "x--dex2oat-swap" ]; then
486        run_args+=(--dex2oat-swap)
487        shift
488    elif [ "x$1" = "x--instruction-set-features" ]; then
489        shift
490        run_args+=(--instruction-set-features "$1")
491        shift
492    elif [ "x$1" = "x--bisection-search" ]; then
493        bisection_search="yes"
494        shift
495    elif [ "x$1" = "x--vdex" ]; then
496        run_args+=(--vdex)
497        shift
498    elif [ "x$1" = "x--dm" ]; then
499        run_args+=(--dm)
500        shift
501    elif [ "x$1" = "x--vdex-filter" ]; then
502        shift
503        filter=$1
504        run_args+=(--vdex-filter "$filter")
505        shift
506    elif [ "x$1" = "x--random-profile" ]; then
507        run_args+=(--random-profile)
508        shift
509    elif [ "x$1" = "x--dex2oat-jobs" ]; then
510        shift
511        run_args+=(-Xcompiler-option "-j$1")
512        shift
513    elif expr "x$1" : "x--" >/dev/null 2>&1; then
514        echo "unknown $0 option: $1" 1>&2
515        usage="yes"
516        break
517    else
518        break
519    fi
520done
521
522if [ "$usage" = "no" -a "x$1" = "x" ]; then
523  echo "missing test to run" 1>&2
524  usage="yes"
525fi
526
527# The DEX_LOCATION with the chroot prefix, if any.
528chroot_dex_location="$chroot$DEX_LOCATION"
529
530# Allocate file descriptor real_stderr and redirect it to the shell's error
531# output (fd 2).
532if [ ${BASH_VERSINFO[1]} -ge 4 ] && [ ${BASH_VERSINFO[2]} -ge 1 ]; then
533  exec {real_stderr}>&2
534else
535  # In bash before version 4.1 we need to do a manual search for free file
536  # descriptors.
537  FD=3
538  while [ -e /dev/fd/$FD ]; do FD=$((FD + 1)); done
539  real_stderr=$FD
540  eval "exec ${real_stderr}>&2"
541fi
542if [ "$quiet" = "yes" ]; then
543  # Force the default standard output and error to go to /dev/null so we will
544  # not print them.
545  exec 1>/dev/null
546  exec 2>/dev/null
547fi
548
549function err_echo() {
550  echo "$@" 1>&${real_stderr}
551}
552
553# tmp_dir may be relative, resolve.
554#
555# Cannot use realpath, as it does not exist on Mac.
556# Cannot use a simple "cd", as the path might not be created yet.
557# Cannot use readlink -m, as it does not exist on Mac.
558# Fallback to nuclear option:
559noncanonical_tmp_dir=$tmp_dir
560tmp_dir="`cd $oldwd ; python -c "import os; import sys; sys.stdout.write(os.path.realpath('$tmp_dir'))"`"
561if [ -z $tmp_dir ] ; then
562  err_echo "Failed to resolve $tmp_dir"
563  exit 1
564fi
565mkdir -p $tmp_dir
566
567# Add thread suspend timeout flag
568if [ ! "$runtime" = "jvm" ]; then
569  run_args+=(--runtime-option "-XX:ThreadSuspendTimeout=$suspend_timeout")
570fi
571
572if [ "$basic_verify" = "true" ]; then
573  # Set HspaceCompactForOOMMinIntervalMs to zero to run hspace compaction for OOM more frequently in tests.
574  run_args+=(--runtime-option -Xgc:preverify --runtime-option -Xgc:postverify --runtime-option -XX:HspaceCompactForOOMMinIntervalMs=0)
575fi
576if [ "$gc_verify" = "true" ]; then
577  run_args+=(--runtime-option -Xgc:preverify_rosalloc --runtime-option -Xgc:postverify_rosalloc)
578fi
579if [ "$gc_stress" = "true" ]; then
580  run_args+=(--gc-stress --runtime-option -Xgc:gcstress --runtime-option -Xms2m --runtime-option -Xmx16m)
581fi
582if [ "$jvmti_redefine_stress" = "true" ]; then
583    run_args+=(--no-app-image --jvmti-redefine-stress)
584fi
585if [ "$jvmti_step_stress" = "true" ]; then
586    run_args+=(--no-app-image --jvmti-step-stress)
587fi
588if [ "$jvmti_field_stress" = "true" ]; then
589    run_args+=(--no-app-image --jvmti-field-stress)
590fi
591if [ "$jvmti_trace_stress" = "true" ]; then
592    run_args+=(--no-app-image --jvmti-trace-stress)
593fi
594if [ "$trace" = "true" ]; then
595    run_args+=(--runtime-option -Xmethod-trace --runtime-option -Xmethod-trace-file-size:2000000)
596    if [ "$trace_stream" = "true" ]; then
597        # Streaming mode uses the file size as the buffer size. So output gets really large. Drop
598        # the ability to analyze the file and just write to /dev/null.
599        run_args+=(--runtime-option -Xmethod-trace-file:/dev/null)
600        # Enable streaming mode.
601        run_args+=(--runtime-option -Xmethod-trace-stream)
602    else
603        run_args+=(--runtime-option "-Xmethod-trace-file:${DEX_LOCATION}/trace.bin")
604    fi
605elif [ "$trace_stream" = "true" ]; then
606    err_echo "Cannot use --stream without --trace."
607    exit 1
608fi
609if [ -n "$timeout" ]; then
610    run_args+=(--timeout "$timeout")
611fi
612
613# Most interesting target architecture variables are Makefile variables, not environment variables.
614# Try to map the suffix64 flag and what we find in ${ANDROID_PRODUCT_OUT}/data/art-test to an architecture name.
615function guess_target_arch_name() {
616    # Check whether this is a device with native bridge. Currently this is hardcoded
617    # to x86 + arm.
618    local guess_path=${ANDROID_PRODUCT_OUT}/system/apex/com.android.art.testing/javalib
619    local x86_arm=`ls ${guess_path} | sort | grep -E '^(arm|x86)$'`
620    # Collapse line-breaks into spaces
621    x86_arm=$(echo $x86_arm)
622    if [ "x$x86_arm" = "xarm x86" ] ; then
623        err_echo "Native-bridge configuration detected."
624        # We only support the main arch for tests.
625        if [ "x${suffix64}" = "x64" ]; then
626            target_arch_name=""
627        else
628            target_arch_name=x86
629        fi
630    else
631        local grep32bit=`ls ${guess_path} | grep -E '^(arm|x86)$'`
632        local grep64bit=`ls ${guess_path} | grep -E '^(arm64|x86_64)$'`
633        if [ "x${suffix64}" = "x64" ]; then
634            target_arch_name=${grep64bit}
635        else
636            target_arch_name=${grep32bit}
637        fi
638    fi
639}
640
641function guess_host_arch_name() {
642    if [ "x${suffix64}" = "x64" ]; then
643        host_arch_name="x86_64"
644    else
645        host_arch_name="x86"
646    fi
647}
648
649if [ "$target_mode" = "no" ]; then
650    if [ "$runtime" = "jvm" ]; then
651        if [ "$prebuild_mode" = "yes" ]; then
652            err_echo "--prebuild with --jvm is unsupported"
653            exit 1
654        fi
655    else
656        # ART/Dalvik host mode.
657        if [ -n "$chroot" ]; then
658            err_echo "--chroot with --host is unsupported"
659            exit 1
660        fi
661    fi
662fi
663
664if [ ! "$runtime" = "jvm" ]; then
665  run_args+=(--lib "$lib")
666fi
667
668if [ "$runtime" = "dalvik" ]; then
669    if [ "$target_mode" = "no" ]; then
670        framework="${ANDROID_PRODUCT_OUT}/system/framework"
671        bpath="${framework}/core-icu4j.jar:${framework}/core-libart.jar:${framework}/core-oj.jar:${framework}/conscrypt.jar:${framework}/okhttp.jar:${framework}/bouncycastle.jar:${framework}/ext.jar"
672        run_args+=(--boot --runtime-option "-Xbootclasspath:${bpath}")
673    else
674        true # defaults to using target BOOTCLASSPATH
675    fi
676elif [ "$runtime" = "art" ]; then
677    if [ "$target_mode" = "no" ]; then
678        guess_host_arch_name
679        run_args+=(--boot "${ANDROID_HOST_OUT}/apex/com.android.art/javalib/boot.art")
680        run_args+=(--runtime-option "-Djava.library.path=${host_lib_root}/lib${suffix64}:${host_lib_root}/nativetest${suffix64}")
681    else
682        guess_target_arch_name
683        run_args+=(--runtime-option "-Djava.library.path=/data/nativetest${suffix64}/art/${target_arch_name}")
684        run_args+=(--boot "/apex/com.android.art/javalib/boot.art")
685    fi
686    if [ "$relocate" = "yes" ]; then
687      run_args+=(--relocate)
688    else
689      run_args+=(--no-relocate)
690    fi
691elif [ "$runtime" = "jvm" ]; then
692    # TODO: Detect whether the host is 32-bit or 64-bit.
693    run_args+=(--runtime-option "-Djava.library.path=${ANDROID_HOST_OUT}/lib64:${ANDROID_HOST_OUT}/nativetest64")
694fi
695
696if [ "$have_image" = "no" ]; then
697    if [ "$runtime" != "art" ]; then
698        err_echo "--no-image is only supported on the art runtime"
699        exit 1
700    fi
701    run_args+=(--no-image)
702fi
703
704if [ "$create_runner" = "yes" -a "$target_mode" = "yes" ]; then
705    err_echo "--create-runner does not function for non --host tests"
706    usage="yes"
707fi
708
709if [ "$dev_mode" = "yes" -a "$update_mode" = "yes" ]; then
710    err_echo "--dev and --update are mutually exclusive"
711    usage="yes"
712fi
713
714if [ "$dev_mode" = "yes" -a "$quiet" = "yes" ]; then
715    err_echo "--dev and --quiet are mutually exclusive"
716    usage="yes"
717fi
718
719if [ "$bisection_search" = "yes" -a "$prebuild_mode" = "yes" ]; then
720    err_echo "--bisection-search and --prebuild are mutually exclusive"
721    usage="yes"
722fi
723
724# TODO: Chroot-based bisection search is not supported yet (see below); implement it.
725if [ "$bisection_search" = "yes" -a -n "$chroot" ]; then
726  err_echo "--chroot with --bisection-search is unsupported"
727  exit 1
728fi
729
730if [ "$usage" = "no" ]; then
731    if [ "x$1" = "x" -o "x$1" = "x-" ]; then
732        test_dir=`basename "$oldwd"`
733    else
734        test_dir="$1"
735    fi
736
737    if [ '!' -d "$test_dir" ]; then
738        td2=`echo ${test_dir}-*`
739        if [ '!' -d "$td2" ]; then
740            err_echo "${test_dir}: no such test directory"
741            usage="yes"
742        fi
743        test_dir="$td2"
744    fi
745    # Shift to get rid of the test name argument. The rest of the arguments
746    # will get passed to the test run.
747    shift
748fi
749
750if [ "$usage" = "yes" ]; then
751    prog=`basename $prog`
752    (
753        echo "usage:"
754        echo "  $prog --help                          Print this message."
755        echo "  $prog [options] [test-name]           Run test normally."
756        echo "  $prog --dev [options] [test-name]     Development mode" \
757             "(dumps to stdout)."
758        echo "  $prog --create-runner [options] [test-name]"
759        echo "              Creates a runner script for use with other " \
760             "tools (e.g. parallel_run.py)."
761        echo "              The script will only run the test portion, and " \
762             "share oat and dex files."
763        echo "  $prog --update [options] [test-name]  Update mode" \
764             "(replaces expected.txt)."
765        echo '  Omitting the test name or specifying "-" will use the' \
766             "current directory."
767        echo "  Runtime Options:"
768        echo "    -O                    Run non-debug rather than debug build (off by default)."
769        echo "    -Xcompiler-option     Pass an option to the compiler."
770        echo "    --build-option        Pass an option to the build script."
771        echo "    --runtime-option      Pass an option to the runtime."
772        echo "    --compact-dex-level   Specify a compact dex level to the compiler."
773        echo "    --debug               Wait for the default debugger to attach."
774        echo "    --debug-agent <agent-path>"
775        echo "                          Wait for the given debugger agent to attach. Currently"
776        echo "                          only supported on host."
777        echo "    --debug-wrap-agent    use libwrapagentproperties and tools/libjdwp-compat.props"
778        echo "                          to load the debugger agent specified by --debug-agent."
779        echo "    --with-agent <agent>  Run the test with the given agent loaded with -agentpath:"
780        echo "    --debuggable          Whether to compile Java code for a debugger."
781        echo "    --gdb                 Run under gdb; incompatible with some tests."
782        echo "    --gdbserver           Start gdbserver (defaults to port :5039)."
783        echo "    --gdbserver-port <port>"
784        echo "                          Start gdbserver with the given COMM (see man gdbserver)."
785        echo "    --gdbserver-bin <binary>"
786        echo "                          Use the given binary as gdbserver."
787        echo "    --gdb-arg             Pass an option to gdb or gdbserver."
788        echo "    --build-only          Build test files only (off by default)."
789        echo "    --interpreter         Enable interpreter only mode (off by default)."
790        echo "    --jit                 Enable jit (off by default)."
791        echo "    --optimizing          Enable optimizing compiler (default)."
792        echo "    --no-verify           Turn off verification (on by default)."
793        echo "    --verify-soft-fail    Force soft fail verification (off by default)."
794        echo "                          Verification is enabled if neither --no-verify"
795        echo "                          nor --verify-soft-fail is specified."
796        echo "    --no-optimize         Turn off optimization (on by default)."
797        echo "    --no-precise          Turn off precise GC (on by default)."
798        echo "    --zygote              Spawn the process from the Zygote." \
799             "If used, then the"
800        echo "                          other runtime options are ignored."
801        echo "    --prebuild            Run dex2oat on the files before starting test. (default)"
802        echo "    --no-prebuild         Do not run dex2oat on the files before starting"
803        echo "                          the test."
804        echo "    --strip-dex           Strip the dex files before starting test."
805        echo "    --relocate            Force the use of relocating in the test, making"
806        echo "                          the image and oat files be relocated to a random"
807        echo "                          address before running."
808        echo "    --no-relocate         Force the use of no relocating in the test. (default)"
809        echo "    --image               Run the test using a precompiled boot image. (default)"
810        echo "    --no-image            Run the test without a precompiled boot image."
811        echo "    --host                Use the host-mode virtual machine."
812        echo "    --invoke-with         Pass --invoke-with option to runtime."
813        echo "    --dalvik              Use Dalvik (off by default)."
814        echo "    --jvm                 Use a host-local RI virtual machine."
815        echo "    --use-java-home       Use the JAVA_HOME environment variable"
816        echo "                          to find the java compiler and runtime"
817        echo "                          (if applicable) to run the test with."
818        echo "    --output-path [path]  Location where to store the build" \
819             "files."
820        echo "    --64                  Run the test in 64-bit mode"
821        echo "    --bionic              Use the (host, 64-bit only) linux_bionic libc runtime"
822        echo "    --runtime-zipapex [file]"
823        echo "                          Use the given zipapex file to provide runtime binaries"
824        echo "    --runtime-extracted-zipapex [dir]"
825        echo "                          Use the given extracted zipapex directory to provide"
826        echo "                          runtime binaries"
827        echo "    --timeout n           Test timeout in seconds"
828        echo "    --trace               Run with method tracing"
829        echo "    --strace              Run with syscall tracing from strace."
830        echo "    --stream              Run method tracing in streaming mode (requires --trace)"
831        echo "    --gcstress            Run with gc stress testing"
832        echo "    --gcverify            Run with gc verification"
833        echo "    --jvmti-trace-stress  Run with jvmti method tracing stress testing"
834        echo "    --jvmti-step-stress   Run with jvmti single step stress testing"
835        echo "    --jvmti-redefine-stress"
836        echo "                          Run with jvmti method redefinition stress testing"
837        echo "    --always-clean        Delete the test files even if the test fails."
838        echo "    --never-clean         Keep the test files even if the test succeeds."
839        echo "    --chroot [newroot]    Run with root directory set to newroot."
840        echo "    --android-root [path] The path on target for the android root. (/system by default)."
841        echo "    --android-i18n-root [path]"
842        echo "                          The path on target for the i18n module root."
843        echo "                          (/apex/com.android.i18n by default)."
844        echo "    --android-art-root [path]"
845        echo "                          The path on target for the ART module root."
846        echo "                          (/apex/com.android.art by default)."
847        echo "    --android-tzdata-root [path]"
848        echo "                          The path on target for the Android Time Zone Data root."
849        echo "                          (/apex/com.android.tzdata by default)."
850        echo "    --dex2oat-swap        Use a dex2oat swap file."
851        echo "    --instruction-set-features [string]"
852        echo "                          Set instruction-set-features for compilation."
853        echo "    --quiet               Don't print anything except failure messages"
854        echo "    --bisection-search    Perform bisection bug search."
855        echo "    --vdex                Test using vdex as in input to dex2oat. Only works with --prebuild."
856        echo "    --suspend-timeout     Change thread suspend timeout ms (default 500000)."
857        echo "    --dex2oat-jobs        Number of dex2oat jobs."
858    ) 1>&2  # Direct to stderr so usage is not printed if --quiet is set.
859    exit 1
860fi
861
862cd "$test_dir"
863test_dir=`pwd`
864
865td_info="${test_dir}/${info}"
866td_expected="${test_dir}/${expected}"
867
868if [ ! -r $td_info ]; then
869    err_echo "${test_dir}: missing file $td_info"
870    exit 1
871fi
872
873if [ ! -r $td_expected ]; then
874    err_echo "${test_dir}: missing file $td_expected"
875    exit 1
876fi
877
878# copy the test to a temp dir and run it
879
880echo "${test_dir}: building..." 1>&2
881
882rm -rf "$tmp_dir"
883cp -LRp "$test_dir" "$tmp_dir"
884cd "$tmp_dir"
885
886if [ '!' -r "$build" ]; then
887    cp "${progdir}/etc/default-build" build
888else
889    cp "${progdir}/etc/default-build" .
890fi
891
892if [ '!' -r "$run" ]; then
893    cp "${progdir}/etc/default-run" run
894else
895    cp "${progdir}/etc/default-run" .
896fi
897
898if [ '!' -r "$check_cmd" ]; then
899    cp "${progdir}/etc/default-check" check
900else
901    cp "${progdir}/etc/default-check" .
902fi
903
904chmod 755 "$build"
905chmod 755 "$run"
906chmod 755 "$check_cmd"
907
908export TEST_NAME=`basename ${test_dir}`
909
910# Tests named '<number>-checker-*' will also have their CFGs verified with
911# Checker when compiled with Optimizing on host.
912if [[ "$TEST_NAME" =~ ^[0-9]+-checker- ]]; then
913  if [ "$runtime" = "art" -a "$run_optimizing" = "true" ]; then
914    # In no-prebuild or no-image mode, the compiler only quickens so disable the checker.
915    if [ "$prebuild_mode" = "yes" -a "$have_image" = "yes" ]; then
916      run_checker="yes"
917
918      if [ "$target_mode" = "no" ]; then
919        cfg_output_dir="$tmp_dir"
920        checker_args="--arch=${host_arch_name^^}"
921      else
922        cfg_output_dir="$DEX_LOCATION"
923        checker_args="--arch=${target_arch_name^^}"
924      fi
925
926      if [ "$debuggable" = "yes" ]; then
927        checker_args="$checker_args --debuggable"
928      fi
929
930      run_args+=(-Xcompiler-option "--dump-cfg=$cfg_output_dir/$cfg_output" -Xcompiler-option -j1)
931    fi
932  fi
933fi
934
935run_args+=(--testlib "${testlib}")
936
937if ! ulimit -f ${file_ulimit}; then
938  err_echo "ulimit file size setting failed"
939fi
940
941# Tell the build script which mode (target, host, jvm) we are building for
942# to determine the bootclasspath at build time.
943if [[ "$target_mode" == "yes" ]]; then
944  build_args="$build_args --target"
945else
946  if [[ $runtime == "jvm" ]]; then
947    build_args="$build_args --jvm"
948  else
949    build_args="$build_args --host"
950  fi
951fi
952
953if [[ "$dev_mode" == "yes" ]]; then
954  build_args="$build_args --dev"
955fi
956
957good="no"
958good_build="yes"
959good_run="yes"
960export TEST_RUNTIME="${runtime}"
961if [ "$dev_mode" = "yes" ]; then
962    "./${build}" $build_args 2>&1
963    build_exit="$?"
964    echo "build exit status: $build_exit" 1>&2
965    if [ "$build_exit" = '0' ]; then
966        echo "${test_dir}: running..." 1>&2
967        "./${run}" "${run_args[@]}" "$@" 2>&1
968        run_exit="$?"
969
970        if [ "$run_exit" = "0" ]; then
971            if [ "$run_checker" = "yes" ]; then
972                if [ "$target_mode" = "yes" ]; then
973                  adb pull "$chroot/$cfg_output_dir/$cfg_output" &> /dev/null
974                fi
975                "$checker" $checker_args "$cfg_output" "$tmp_dir" 2>&1
976                checker_exit="$?"
977                if [ "$checker_exit" = "0" ]; then
978                    good="yes"
979                fi
980                err_echo "checker exit status: $checker_exit"
981            else
982                good="yes"
983            fi
984        fi
985        echo "run exit status: $run_exit" 1>&2
986    fi
987elif [ "$update_mode" = "yes" ]; then
988    "./${build}" $build_args >"$build_output" 2>&1
989    build_exit="$?"
990    if [ "$build_exit" = '0' ]; then
991        echo "${test_dir}: running..." 1>&2
992        "./${run}" "${run_args[@]}" "$@" >"$output" 2>&1
993        if [ "$run_checker" = "yes" ]; then
994          if [ "$target_mode" = "yes" ]; then
995            adb pull "$chroot/$cfg_output_dir/$cfg_output" &> /dev/null
996          fi
997          "$checker" -q $checker_args "$cfg_output" "$tmp_dir" >> "$output" 2>&1
998        fi
999        sed -e 's/[[:cntrl:]]$//g' < "$output" >"${td_expected}"
1000        good="yes"
1001    else
1002        cat "$build_output" 1>&${real_stderr} 1>&2
1003        err_echo "build exit status: $build_exit"
1004    fi
1005elif [ "$build_only" = "yes" ]; then
1006    good="yes"
1007    "./${build}" $build_args >"$build_output" 2>&1
1008    build_exit="$?"
1009    if [ "$build_exit" '!=' '0' ]; then
1010        cp "$build_output" "$output"
1011        echo "build exit status: $build_exit" >>"$output"
1012        diff --strip-trailing-cr -q "$expected" "$output" >/dev/null
1013        if [ "$?" '!=' "0" ]; then
1014            good="no"
1015            err_echo "BUILD FAILED For ${TEST_NAME}"
1016        fi
1017    fi
1018    # Clean up extraneous files that are not used by tests.
1019    find $tmp_dir -mindepth 1  ! -regex ".*/\(.*jar\|$output\|$expected\)" | xargs rm -rf
1020    exit 0
1021else
1022    "./${build}" $build_args >"$build_output" 2>&1
1023    build_exit="$?"
1024    if [ "$build_exit" = '0' ]; then
1025        echo "${test_dir}: running..." 1>&2
1026        "./${run}" "${run_args[@]}" "$@" >"$output" 2>&1
1027        run_exit="$?"
1028        if [ "$run_exit" != "0" ]; then
1029            err_echo "run exit status: $run_exit"
1030            good_run="no"
1031        elif [ "$run_checker" = "yes" ]; then
1032            if [ "$target_mode" = "yes" ]; then
1033              adb pull "$chroot/$cfg_output_dir/$cfg_output" &> /dev/null
1034            fi
1035            "$checker" -q $checker_args "$cfg_output" "$tmp_dir" >> "$output" 2>&1
1036            checker_exit="$?"
1037            if [ "$checker_exit" != "0" ]; then
1038                err_echo "checker exit status: $checker_exit"
1039                good_run="no"
1040            else
1041                good_run="yes"
1042            fi
1043        else
1044            good_run="yes"
1045        fi
1046    else
1047        good_build="no"
1048        cp "$build_output" "$output"
1049        echo "Failed to build in tmpdir=${tmp_dir} from oldwd=${oldwd} and cwd=`pwd`" >> "$output"
1050        echo "Non-canonical tmpdir was ${noncanonical_tmp_dir}" >> "$output"
1051        echo "Args: ${args}" >> "$output"
1052        echo "build exit status: $build_exit" >> "$output"
1053        max_name_length=$(getconf NAME_MAX ${tmp_dir})
1054        echo "Max filename (NAME_MAX): ${max_name_length}" >> "$output"
1055        max_path_length=$(getconf PATH_MAX ${tmp_dir})
1056        echo "Max pathlength (PATH_MAX): ${max_path_length}" >> "$output"
1057    fi
1058    ./$check_cmd "$expected" "$output"
1059    if [ "$?" = "0" ]; then
1060        if [ "$good_build" = "no" -o "$good_run" = "yes" ]; then
1061          # output == expected
1062          good="yes"
1063          echo "${test_dir}: succeeded!" 1>&2
1064        fi
1065    fi
1066fi
1067
1068(
1069    if [ "$good" != "yes" -a "$update_mode" != "yes" ]; then
1070        echo "${test_dir}: FAILED!"
1071        echo ' '
1072        echo '#################### info'
1073        cat "${td_info}" | sed 's/^/# /g'
1074        echo '#################### diffs'
1075        if [ "$run_checker" == "yes" ]; then
1076          # Checker failures dump the whole CFG, so we output the whole diff.
1077          diff --strip-trailing-cr -u "$expected" "$output"
1078        else
1079          diff --strip-trailing-cr -u "$expected" "$output" | tail -n 10000
1080        fi
1081        echo '####################'
1082        if [ "$strace" = "yes" ]; then
1083            echo '#################### strace output'
1084            tail -n 3000 "$tmp_dir/$strace_output"
1085            echo '####################'
1086        fi
1087        if [ "x$target_mode" = "xno" -a "x$SANITIZE_HOST" = "xaddress" ]; then
1088            # Run the stack script to symbolize any ASAN aborts on the host for SANITIZE_HOST. The
1089            # tools used by the given ABI work for both x86 and x86-64.
1090            echo "ABI: 'x86_64'" | cat - "$output" | $ANDROID_BUILD_TOP/development/scripts/stack | tail -n 3000
1091        fi
1092        echo ' '
1093    fi
1094
1095) 2>&${real_stderr} 1>&2
1096
1097# Attempt bisection only if the test failed.
1098# TODO: Implement support for chroot-based bisection search.
1099if [ "$bisection_search" = "yes" -a "$good" != "yes" ]; then
1100    # Bisecting works by skipping different optimization passes which breaks checker assertions.
1101    if [ "$run_checker" == "yes" ]; then
1102      echo "${test_dir}: not bisecting, checker test." 1>&2
1103    else
1104      # Increase file size limit, bisection search can generate large logfiles.
1105      echo "${test_dir}: bisecting..." 1>&2
1106      cwd=`pwd`
1107      maybe_device_mode=""
1108      raw_cmd=""
1109      if [ "$target_mode" = "yes" ]; then
1110        # Produce cmdline.sh in $chroot_dex_location. "$@" is passed as a runtime option
1111        # so that cmdline.sh forwards its arguments to dalvikvm. invoke-with is set
1112        # to exec in order to preserve pid when calling dalvikvm. This is required
1113        # for bisection search to correctly retrieve logs from device.
1114        "./${run}" "${run_args[@]}" --runtime-option '"$@"' --invoke-with exec --dry-run "$@" &> /dev/null
1115        adb shell chmod u+x "$chroot_dex_location/cmdline.sh"
1116        maybe_device_mode="--device"
1117        raw_cmd="$DEX_LOCATION/cmdline.sh"
1118      else
1119        raw_cmd="$cwd/${run} --external-log-tags "${run_args[@]}" $@"
1120      fi
1121      # TODO: Pass a `--chroot` option to the bisection_search.py script and use it there.
1122      $ANDROID_BUILD_TOP/art/tools/bisection_search/bisection_search.py \
1123        $maybe_device_mode \
1124        --raw-cmd="$raw_cmd" \
1125        --check-script="$cwd/check" \
1126        --expected-output="$cwd/expected.txt" \
1127        --logfile="$cwd/bisection_log.txt" \
1128        --timeout=${timeout:-300}
1129    fi
1130fi
1131
1132# Clean up test files.
1133if [ "$always_clean" = "yes" -o "$good" = "yes" ] && [ "$never_clean" = "no" ]; then
1134    cd "$oldwd"
1135    rm -rf "$tmp_dir"
1136    if [ "$target_mode" = "yes" -a "$build_exit" = "0" ]; then
1137        adb shell rm -rf $chroot_dex_location
1138    fi
1139    if [ "$good" = "yes" ]; then
1140        exit 0
1141    fi
1142fi
1143
1144
1145(
1146    if [ "$always_clean" = "yes" ]; then
1147        echo "${TEST_NAME} files deleted from host "
1148        if [ "$target_mode" == "yes" ]; then
1149            echo "and from target"
1150        fi
1151    else
1152        echo "${TEST_NAME} files left in ${tmp_dir} on host"
1153        if [ "$target_mode" == "yes" ]; then
1154            echo "and in ${chroot_dex_location} on target"
1155        fi
1156    fi
1157
1158) 2>&${real_stderr} 1>&2
1159
1160if [ "$never_clean" = "yes" ] && [ "$good" = "yes" ]; then
1161  exit 0
1162else
1163  exit 1
1164fi
1165