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