1#!/bin/bash
2#
3# Runner for an individual run-test.
4
5msg() {
6    if [ "$QUIET" = "n" ]; then
7        echo "$@"
8    fi
9}
10
11ANDROID_ROOT="/system"
12ARCHITECTURES_32="(arm|x86|mips|none)"
13ARCHITECTURES_64="(arm64|x86_64|mips64|none)"
14ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
15BOOT_IMAGE=""
16COMPILE_FLAGS=""
17DALVIKVM="dalvikvm32"
18DEBUGGER="n"
19DEV_MODE="n"
20DEX2OAT=""
21EXPERIMENTAL=""
22FALSE_BIN="false"
23FLAGS=""
24ANDROID_FLAGS=""
25GDB=""
26GDB_ARGS=""
27GDB_SERVER="gdbserver"
28HAVE_IMAGE="y"
29HOST="n"
30INTERPRETER="n"
31JIT="n"
32INVOKE_WITH=""
33IS_JVMTI_TEST="n"
34ISA=x86
35LIBRARY_DIRECTORY="lib"
36TEST_DIRECTORY="nativetest"
37MAIN=""
38OPTIMIZE="y"
39PATCHOAT=""
40PREBUILD="y"
41QUIET="n"
42RELOCATE="n"
43STRIP_DEX="n"
44SECONDARY_DEX=""
45TIME_OUT="gdb"  # "n" (disabled), "timeout" (use timeout), "gdb" (use gdb)
46# Value in seconds
47if [ "$ART_USE_READ_BARRIER" != "false" ]; then
48  TIME_OUT_VALUE=2400  # 40 minutes.
49else
50  TIME_OUT_VALUE=1200  # 20 minutes.
51fi
52USE_GDB="n"
53USE_JVM="n"
54VERIFY="y" # y=yes,n=no,s=softfail
55ZYGOTE=""
56DEX_VERIFY=""
57USE_DEX2OAT_AND_PATCHOAT="y"
58INSTRUCTION_SET_FEATURES=""
59ARGS=""
60EXTERNAL_LOG_TAGS="n" # if y respect externally set ANDROID_LOG_TAGS.
61DRY_RUN="n" # if y prepare to run the test but don't run it.
62TEST_VDEX="n"
63TEST_IS_NDEBUG="n"
64APP_IMAGE="y"
65JVMTI_STRESS="n"
66VDEX_FILTER=""
67PROFILE="n"
68RANDOM_PROFILE="n"
69
70# if "y", run 'sync' before dalvikvm to make sure all files from
71# build step (e.g. dex2oat) were finished writing.
72SYNC_BEFORE_RUN="n"
73
74while true; do
75    if [ "x$1" = "x--quiet" ]; then
76        QUIET="y"
77        shift
78    elif [ "x$1" = "x--jvmti" ]; then
79        IS_JVMTI_TEST="y"
80        shift
81    elif [ "x$1" = "x-O" ]; then
82        TEST_IS_NDEBUG="y"
83        shift
84    elif [ "x$1" = "x--lib" ]; then
85        shift
86        if [ "x$1" = "x" ]; then
87            echo "$0 missing argument to --lib" 1>&2
88            exit 1
89        fi
90        LIB="$1"
91        shift
92    elif [ "x$1" = "x--gc-stress" ]; then
93        # Give an extra 5 mins if we are gc-stress.
94        TIME_OUT_VALUE=$((${TIME_OUT_VALUE} + 300))
95        shift
96    elif [ "x$1" = "x--testlib" ]; then
97        shift
98        if [ "x$1" = "x" ]; then
99            echo "$0 missing argument to --testlib" 1>&2
100            exit 1
101        fi
102        ARGS="${ARGS} $1"
103        shift
104    elif [ "x$1" = "x--args" ]; then
105        shift
106        if [ "x$1" = "x" ]; then
107            echo "$0 missing argument to --args" 1>&2
108            exit 1
109        fi
110        ARGS="${ARGS} $1"
111        shift
112    elif [ "x$1" = "x-Xcompiler-option" ]; then
113        shift
114        option="$1"
115        FLAGS="${FLAGS} -Xcompiler-option $option"
116        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
117        shift
118    elif [ "x$1" = "x--android-runtime-option" ]; then
119        shift
120        option="$1"
121        ANDROID_FLAGS="${ANDROID_FLAGS} $option"
122        shift
123    elif [ "x$1" = "x--runtime-option" ]; then
124        shift
125        option="$1"
126        FLAGS="${FLAGS} $option"
127        shift
128    elif [ "x$1" = "x--boot" ]; then
129        shift
130        BOOT_IMAGE="$1"
131        shift
132    elif [ "x$1" = "x--no-dex2oat" ]; then
133        DEX2OAT="-Xcompiler:${FALSE_BIN}"
134        USE_DEX2OAT_AND_PATCHOAT="n"
135        shift
136    elif [ "x$1" = "x--no-patchoat" ]; then
137        PATCHOAT="-Xpatchoat:${FALSE_BIN}"
138        USE_DEX2OAT_AND_PATCHOAT="n"
139        shift
140    elif [ "x$1" = "x--relocate" ]; then
141        RELOCATE="y"
142        shift
143    elif [ "x$1" = "x--no-relocate" ]; then
144        RELOCATE="n"
145        shift
146    elif [ "x$1" = "x--prebuild" ]; then
147        PREBUILD="y"
148        shift
149    elif [ "x$1" = "x--jvmti-stress" ]; then
150        # APP_IMAGE doesn't really work with jvmti-torture
151        APP_IMAGE="n"
152        JVMTI_STRESS="y"
153        shift
154    elif [ "x$1" = "x--no-app-image" ]; then
155        APP_IMAGE="n"
156        shift
157    elif [ "x$1" = "x--strip-dex" ]; then
158        STRIP_DEX="y"
159        shift
160    elif [ "x$1" = "x--host" ]; then
161        HOST="y"
162        ANDROID_ROOT="$ANDROID_HOST_OUT"
163        shift
164    elif [ "x$1" = "x--no-prebuild" ]; then
165        PREBUILD="n"
166        shift
167    elif [ "x$1" = "x--no-image" ]; then
168        HAVE_IMAGE="n"
169        shift
170    elif [ "x$1" = "x--secondary" ]; then
171        SECONDARY_DEX=":$DEX_LOCATION/$TEST_NAME-ex.jar"
172        # Enable cfg-append to make sure we get the dump for both dex files.
173        # (otherwise the runtime compilation of the secondary dex will overwrite
174        # the dump of the first one).
175        FLAGS="${FLAGS} -Xcompiler-option --dump-cfg-append"
176        COMPILE_FLAGS="${COMPILE_FLAGS} --dump-cfg-append"
177        shift
178    elif [ "x$1" = "x--debug" ]; then
179        DEBUGGER="y"
180        TIME_OUT="n"
181        shift
182    elif [ "x$1" = "x--gdb" ]; then
183        USE_GDB="y"
184        DEV_MODE="y"
185        TIME_OUT="n"
186        shift
187    elif [ "x$1" = "x--gdb-arg" ]; then
188        shift
189        gdb_arg="$1"
190        GDB_ARGS="${GDB_ARGS} $gdb_arg"
191        shift
192    elif [ "x$1" = "x--zygote" ]; then
193        ZYGOTE="-Xzygote"
194        msg "Spawning from zygote"
195        shift
196    elif [ "x$1" = "x--dev" ]; then
197        DEV_MODE="y"
198        shift
199    elif [ "x$1" = "x--interpreter" ]; then
200        INTERPRETER="y"
201        shift
202    elif [ "x$1" = "x--jit" ]; then
203        JIT="y"
204        shift
205    elif [ "x$1" = "x--jvm" ]; then
206        USE_JVM="y"
207        shift
208    elif [ "x$1" = "x--invoke-with" ]; then
209        shift
210        if [ "x$1" = "x" ]; then
211            echo "$0 missing argument to --invoke-with" 1>&2
212            exit 1
213        fi
214        if [ "x$INVOKE_WITH" = "x" ]; then
215            INVOKE_WITH="$1"
216        else
217            INVOKE_WITH="$INVOKE_WITH $1"
218        fi
219        shift
220    elif [ "x$1" = "x--no-verify" ]; then
221        VERIFY="n"
222        shift
223    elif [ "x$1" = "x--verify-soft-fail" ]; then
224        VERIFY="s"
225        shift
226    elif [ "x$1" = "x--no-optimize" ]; then
227        OPTIMIZE="n"
228        shift
229    elif [ "x$1" = "x--android-root" ]; then
230        shift
231        ANDROID_ROOT="$1"
232        shift
233    elif [ "x$1" = "x--instruction-set-features" ]; then
234        shift
235        INSTRUCTION_SET_FEATURES="$1"
236        shift
237    elif [ "x$1" = "x--timeout" ]; then
238        shift
239        TIME_OUT_VALUE="$1"
240        shift
241    elif [ "x$1" = "x--" ]; then
242        shift
243        break
244    elif [ "x$1" = "x--64" ]; then
245        ISA="x86_64"
246        GDB_SERVER="gdbserver64"
247        DALVIKVM="dalvikvm64"
248        LIBRARY_DIRECTORY="lib64"
249        TEST_DIRECTORY="nativetest64"
250        ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
251        shift
252    elif [ "x$1" = "x--pic-test" ]; then
253        FLAGS="${FLAGS} -Xcompiler-option --compile-pic"
254        COMPILE_FLAGS="${COMPILE_FLAGS} --compile-pic"
255        shift
256    elif [ "x$1" = "x--experimental" ]; then
257        if [ "$#" -lt 2 ]; then
258            echo "missing --experimental option" 1>&2
259            exit 1
260        fi
261        EXPERIMENTAL="$EXPERIMENTAL $2"
262        shift 2
263    elif [ "x$1" = "x--external-log-tags" ]; then
264        EXTERNAL_LOG_TAGS="y"
265        shift
266    elif [ "x$1" = "x--dry-run" ]; then
267        DRY_RUN="y"
268        shift
269    elif [ "x$1" = "x--vdex" ]; then
270        TEST_VDEX="y"
271        shift
272    elif [ "x$1" = "x--vdex-filter" ]; then
273        shift
274        option="$1"
275        VDEX_FILTER="--compiler-filter=$option"
276        shift
277    elif [ "x$1" = "x--sync" ]; then
278        SYNC_BEFORE_RUN="y"
279        shift
280    elif [ "x$1" = "x--profile" ]; then
281        PROFILE="y"
282        shift
283    elif [ "x$1" = "x--random-profile" ]; then
284        RANDOM_PROFILE="y"
285        shift
286    elif expr "x$1" : "x--" >/dev/null 2>&1; then
287        echo "unknown $0 option: $1" 1>&2
288        exit 1
289    else
290        break
291    fi
292done
293
294if [ "$USE_JVM" = "n" ]; then
295    FLAGS="${FLAGS} ${ANDROID_FLAGS}"
296    for feature in ${EXPERIMENTAL}; do
297        FLAGS="${FLAGS} -Xexperimental:${feature} -Xcompiler-option --runtime-arg -Xcompiler-option -Xexperimental:${feature}"
298        COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xexperimental:${feature}"
299    done
300fi
301
302if [ "x$1" = "x" ] ; then
303  MAIN="Main"
304else
305  MAIN="$1"
306  shift
307fi
308
309if [ "$ZYGOTE" = "" ]; then
310    if [ "$OPTIMIZE" = "y" ]; then
311        if [ "$VERIFY" = "y" ]; then
312            DEX_OPTIMIZE="-Xdexopt:verified"
313        else
314            DEX_OPTIMIZE="-Xdexopt:all"
315        fi
316        msg "Performing optimizations"
317    else
318        DEX_OPTIMIZE="-Xdexopt:none"
319        msg "Skipping optimizations"
320    fi
321
322    if [ "$VERIFY" = "y" ]; then
323        JVM_VERIFY_ARG="-Xverify:all"
324        msg "Performing verification"
325    elif [ "$VERIFY" = "s" ]; then
326        JVM_VERIFY_ARG="Xverify:all"
327        DEX_VERIFY="-Xverify:softfail"
328        msg "Forcing verification to be soft fail"
329    else # VERIFY = "n"
330        DEX_VERIFY="-Xverify:none"
331        JVM_VERIFY_ARG="-Xverify:none"
332        msg "Skipping verification"
333    fi
334fi
335
336msg "------------------------------"
337
338if [ "$DEBUGGER" = "y" ]; then
339  # Use this instead for ddms and connect by running 'ddms':
340  # DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_android_adb,server=y,suspend=y"
341  # TODO: add a separate --ddms option?
342
343  PORT=12345
344  msg "Waiting for jdb to connect:"
345  if [ "$HOST" = "n" ]; then
346    msg "    adb forward tcp:$PORT tcp:$PORT"
347  fi
348  msg "    jdb -attach localhost:$PORT"
349  DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
350fi
351
352if [ "$IS_JVMTI_TEST" = "y" ]; then
353  plugin=libopenjdkjvmtid.so
354  agent=libtiagentd.so
355  lib=tiagentd
356  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
357    agent=libtiagent.so
358    plugin=libopenjdkjvmti.so
359    lib=tiagent
360  fi
361
362  ARGS="${ARGS} ${lib}"
363  if [[ "$USE_JVM" = "y" ]]; then
364    FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${TEST_NAME},jvm"
365  else
366    FLAGS="${FLAGS} -agentpath:${agent}=${TEST_NAME},art"
367    FLAGS="${FLAGS} -Xplugin:${plugin}"
368    FLAGS="${FLAGS} -Xcompiler-option --debuggable"
369    # Always make the compilation be debuggable.
370    COMPILE_FLAGS="${COMPILE_FLAGS} --debuggable"
371  fi
372fi
373
374if [[ "$JVMTI_STRESS" = "y" ]]; then
375  plugin=libopenjdkjvmtid.so
376  agent=libtistressd.so
377  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
378    agent=libtistress.so
379    plugin=libopenjdkjvmti.so
380  fi
381
382  file_1=$(mktemp --tmpdir=${DEX_LOCATION})
383  file_2=$(mktemp --tmpdir=${DEX_LOCATION})
384  if [[ "$USE_JVM" = "y" ]]; then
385    FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=/bin/false,${file_1},${file_2}"
386  else
387    # TODO Remove need for DEXTER_BINARY!
388    FLAGS="${FLAGS} -agentpath:${agent}=${DEXTER_BINARY},${file_1},${file_2}"
389    if [ "$IS_JVMTI_TEST" = "n" ]; then
390      FLAGS="${FLAGS} -Xplugin:${plugin}"
391      FLAGS="${FLAGS} -Xcompiler-option --debuggable"
392      # Always make the compilation be debuggable.
393      COMPILE_FLAGS="${COMPILE_FLAGS} --debuggable"
394    fi
395  fi
396fi
397
398if [ "$USE_JVM" = "y" ]; then
399  export LD_LIBRARY_PATH=${ANDROID_HOST_OUT}/lib64
400  # Xmx is necessary since we don't pass down the ART flags to JVM.
401  # We pass the classes2 path whether it's used (src-multidex) or not.
402  cmdline="${JAVA} ${DEBUGGER_OPTS} ${JVM_VERIFY_ARG} -Xmx256m -classpath classes:classes2 ${FLAGS} $MAIN $@ ${ARGS}"
403  if [ "$DEV_MODE" = "y" ]; then
404    echo $cmdline
405  fi
406  $cmdline
407  exit
408fi
409
410
411if [ "$HAVE_IMAGE" = "n" ]; then
412    if [ "${HOST}" = "y" ]; then
413        framework="${ANDROID_HOST_OUT}/framework"
414        bpath_suffix="-hostdex"
415    else
416        framework="${ANDROID_ROOT}/framework"
417        bpath_suffix="-testdex"
418    fi
419    bpath="${framework}/core-libart${bpath_suffix}.jar"
420    bpath="${bpath}:${framework}/core-oj${bpath_suffix}.jar"
421    bpath="${bpath}:${framework}/conscrypt${bpath_suffix}.jar"
422    bpath="${bpath}:${framework}/okhttp${bpath_suffix}.jar"
423    bpath="${bpath}:${framework}/bouncycastle${bpath_suffix}.jar"
424    # Pass down the bootclasspath
425    FLAGS="${FLAGS} -Xbootclasspath:${bpath}"
426    # Add 5 minutes to give some time to generate the boot image.
427    TIME_OUT_VALUE=$((${TIME_OUT_VALUE} + 300))
428    DALVIKVM_BOOT_OPT="-Ximage:/system/non-existant/core.art"
429else
430    DALVIKVM_BOOT_OPT="-Ximage:${BOOT_IMAGE}"
431fi
432
433
434if [ "$USE_GDB" = "y" ]; then
435  if [ "$HOST" = "n" ]; then
436    GDB="$GDB_SERVER :5039"
437  else
438    if [ `uname` = "Darwin" ]; then
439        GDB=lldb
440        GDB_ARGS="$GDB_ARGS -- $DALVIKVM"
441        DALVIKVM=
442    else
443        GDB=gdb
444        GDB_ARGS="$GDB_ARGS --args $DALVIKVM"
445        # Enable for Emacs "M-x gdb" support. TODO: allow extra gdb arguments on command line.
446        # gdbargs="--annotate=3 $gdbargs"
447    fi
448  fi
449fi
450
451if [ "$INTERPRETER" = "y" ]; then
452    INT_OPTS="-Xint"
453    if [ "$VERIFY" = "y" ] ; then
454      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=quicken"
455      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=quicken"
456    elif [ "$VERIFY" = "s" ]; then
457      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=extract"
458      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=extract"
459      DEX_VERIFY="${DEX_VERIFY} -Xverify:softfail"
460    else # VERIFY = "n"
461      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=assume-verified"
462      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=assume-verified"
463      DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
464    fi
465fi
466
467if [ "$JIT" = "y" ]; then
468    INT_OPTS="-Xusejit:true"
469    if [ "$VERIFY" = "y" ] ; then
470      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=quicken"
471      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=quicken"
472    else
473      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=assume-verified"
474      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=assume-verified"
475      DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
476    fi
477fi
478
479JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
480
481COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xnorelocate"
482if [ "$RELOCATE" = "y" ]; then
483    FLAGS="${FLAGS} -Xrelocate"
484else
485    FLAGS="$FLAGS -Xnorelocate"
486fi
487
488if [ "$HOST" = "n" ]; then
489  # Need to be root to query /data/dalvik-cache
490  adb root > /dev/null
491  adb wait-for-device
492  ISA=
493  ISA_adb_invocation=
494  ISA_outcome=
495  # We iterate a few times to workaround an adb issue. b/32655576
496  for i in {1..10}; do
497    ISA_adb_invocation=$(adb shell ls -F /data/dalvik-cache)
498    ISA_outcome=$?
499    ISA=$(echo $ISA_adb_invocation | grep -Ewo "${ARCHITECTURES_PATTERN}")
500    if [ x"$ISA" != "x" ]; then
501      break;
502    fi
503  done
504  if [ x"$ISA" = "x" ]; then
505    echo "Unable to determine architecture"
506    # Print a few things for helping diagnosing the problem.
507    echo "adb invocation output: $ISA_adb_invocation"
508    echo "adb invocation outcome: $ISA_outcome"
509    echo $(adb shell ls -F /data/dalvik-cache)
510    echo $(adb shell ls /data/dalvik-cache)
511    echo ${ARCHITECTURES_PATTERN}
512    echo $(adb shell ls -F /data/dalvik-cache | grep -Ewo "${ARCHITECTURES_PATTERN}")
513    exit 1
514  fi
515fi
516
517# Prevent test from silently falling back to interpreter in no-prebuild mode. This happens
518# when DEX_LOCATION path is too long, because vdex/odex filename is constructed by taking
519# full path to dex, stripping leading '/', appending '@classes.vdex' and changing every
520# remaining '/' into '@'.
521if [ "$HOST" = "y" ]; then
522  max_filename_size=$(getconf NAME_MAX $DEX_LOCATION)
523else
524  # There is no getconf on device, fallback to standard value. See NAME_MAX in kernel <linux/limits.h>
525  max_filename_size=255
526fi
527# Compute VDEX_NAME.
528DEX_LOCATION_STRIPPED="${DEX_LOCATION#/}"
529VDEX_NAME="${DEX_LOCATION_STRIPPED//\//@}@$TEST_NAME.jar@classes.vdex"
530if [ ${#VDEX_NAME} -gt $max_filename_size ]; then
531    echo "Dex location path too long:"
532    echo "$VDEX_NAME is ${#VDEX_NAME} character long, and the limit is $max_filename_size."
533    exit 1
534fi
535
536profman_cmdline="true"
537dex2oat_cmdline="true"
538vdex_cmdline="true"
539mkdir_locations="${DEX_LOCATION}/dalvik-cache/$ISA"
540strip_cmdline="true"
541sync_cmdline="true"
542
543# PROFILE takes precedence over RANDOM_PROFILE, since PROFILE tests require a
544# specific profile to run properly.
545if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
546  profman_cmdline="${ANDROID_ROOT}/bin/profman  \
547    --apk=$DEX_LOCATION/$TEST_NAME.jar \
548    --dex-location=$DEX_LOCATION/$TEST_NAME.jar"
549  if [ -f $DEX_LOCATION/$TEST_NAME-ex.jar ]; then
550    profman_cmdline="${profman_cmdline} \
551      --apk=$DEX_LOCATION/$TEST_NAME-ex.jar \
552      --dex-location=$DEX_LOCATION/$TEST_NAME-ex.jar"
553  fi
554  COMPILE_FLAGS="${COMPILE_FLAGS} --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
555  FLAGS="${FLAGS} -Xcompiler-option --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
556  if [ "$PROFILE" = "y" ]; then
557    profman_cmdline="${profman_cmdline} --create-profile-from=$DEX_LOCATION/profile \
558        --reference-profile-file=$DEX_LOCATION/$TEST_NAME.prof"
559  else
560    profman_cmdline="${profman_cmdline} --generate-test-profile=$DEX_LOCATION/$TEST_NAME.prof \
561        --generate-test-profile-seed=0"
562  fi
563fi
564
565if [ "$PREBUILD" = "y" ]; then
566  mkdir_locations="${mkdir_locations} ${DEX_LOCATION}/oat/$ISA"
567  if [ "$APP_IMAGE" = "y" ]; then
568    # Pick a base that will force the app image to get relocated.
569    app_image="--base=0x4000 --app-image-file=$DEX_LOCATION/oat/$ISA/$TEST_NAME.art"
570  fi
571
572  dex2oat_cmdline="$INVOKE_WITH $ANDROID_ROOT/bin/dex2oatd \
573                      $COMPILE_FLAGS \
574                      --boot-image=${BOOT_IMAGE} \
575                      --dex-file=$DEX_LOCATION/$TEST_NAME.jar \
576                      --oat-file=$DEX_LOCATION/oat/$ISA/$TEST_NAME.odex \
577                      ${app_image} \
578                      --instruction-set=$ISA"
579  if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
580    dex2oat_cmdline="${dex2oat_cmdline} --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
581  fi
582
583  # Add in a timeout. This is important for testing the compilation/verification time of
584  # pathological cases.
585  # Note: as we don't know how decent targets are (e.g., emulator), only do this on the host for
586  #       now. We should try to improve this.
587  #       The current value is rather arbitrary. run-tests should compile quickly.
588  if [ "$HOST" != "n" ]; then
589    # Use SIGRTMIN+2 to try to dump threads.
590    # Use -k 1m to SIGKILL it a minute later if it hasn't ended.
591    dex2oat_cmdline="timeout -k 1m -s SIGRTMIN+2 1m ${dex2oat_cmdline}"
592  fi
593  if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
594    vdex_cmdline="${dex2oat_cmdline} ${VDEX_FILTER} --input-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex --output-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex"
595  elif [ "$TEST_VDEX" = "y" ]; then
596    vdex_cmdline="${dex2oat_cmdline} ${VDEX_FILTER} --input-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex"
597  elif [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
598    vdex_cmdline="${dex2oat_cmdline} --input-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex --output-vdex=$DEX_LOCATION/oat/$ISA/$TEST_NAME.vdex"
599  fi
600fi
601
602if [ "$STRIP_DEX" = "y" ]; then
603  strip_cmdline="zip --quiet --delete $DEX_LOCATION/$TEST_NAME.jar classes.dex"
604fi
605
606if [ "$SYNC_BEFORE_RUN" = "y" ]; then
607  sync_cmdline="sync"
608fi
609
610DALVIKVM_ISA_FEATURES_ARGS=""
611if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
612  DALVIKVM_ISA_FEATURES_ARGS="-Xcompiler-option --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
613fi
614
615# java.io.tmpdir can only be set at launch time.
616TMP_DIR_OPTION=""
617if [ "$HOST" = "n" ]; then
618  TMP_DIR_OPTION="-Djava.io.tmpdir=/data/local/tmp"
619fi
620
621# We set DumpNativeStackOnSigQuit to false to avoid stressing libunwind.
622# b/27185632
623# b/24664297
624dalvikvm_cmdline="$INVOKE_WITH $GDB $ANDROID_ROOT/bin/$DALVIKVM \
625                  $GDB_ARGS \
626                  $FLAGS \
627                  $DEX_VERIFY \
628                  -XXlib:$LIB \
629                  $PATCHOAT \
630                  $DEX2OAT \
631                  $DALVIKVM_ISA_FEATURES_ARGS \
632                  $ZYGOTE \
633                  $JNI_OPTS \
634                  $INT_OPTS \
635                  $DEBUGGER_OPTS \
636                  $DALVIKVM_BOOT_OPT \
637                  $TMP_DIR_OPTION \
638                  -XX:DumpNativeStackOnSigQuit:false \
639                  -cp $DEX_LOCATION/$TEST_NAME.jar$SECONDARY_DEX $MAIN $ARGS"
640
641# Remove whitespace.
642dex2oat_cmdline=$(echo $dex2oat_cmdline)
643dalvikvm_cmdline=$(echo $dalvikvm_cmdline)
644vdex_cmdline=$(echo $vdex_cmdline)
645profman_cmdline=$(echo $profman_cmdline)
646
647if [ "$HOST" = "n" ]; then
648    adb root > /dev/null
649    adb wait-for-device
650    if [ "$QUIET" = "n" ]; then
651      adb shell rm -rf $DEX_LOCATION
652      adb shell mkdir -p $DEX_LOCATION
653      adb push $TEST_NAME.jar $DEX_LOCATION
654      adb push $TEST_NAME-ex.jar $DEX_LOCATION
655      if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
656        adb push profile $DEX_LOCATION
657      fi
658    else
659      adb shell rm -r $DEX_LOCATION >/dev/null 2>&1
660      adb shell mkdir -p $DEX_LOCATION >/dev/null 2>&1
661      adb push $TEST_NAME.jar $DEX_LOCATION >/dev/null 2>&1
662      adb push $TEST_NAME-ex.jar $DEX_LOCATION >/dev/null 2>&1
663      if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
664        adb push profile $DEX_LOCATION >/dev/null 2>&1
665      fi
666
667    fi
668
669    LD_LIBRARY_PATH=/data/$TEST_DIRECTORY/art/$ISA
670    if [ "$ANDROID_ROOT" != "/system" ]; then
671      # Current default installation is dalvikvm 64bits and dex2oat 32bits,
672      # so we can only use LD_LIBRARY_PATH when testing on a local
673      # installation.
674      LD_LIBRARY_PATH=$ANDROID_ROOT/$LIBRARY_DIRECTORY:$LD_LIBRARY_PATH
675    fi
676
677    # System libraries needed by libarttestd.so
678    PUBLIC_LIBS=libart.so:libartd.so:libc++.so:libbacktrace.so:libbase.so:libnativehelper.so
679
680    # Create a script with the command. The command can get longer than the longest
681    # allowed adb command and there is no way to get the exit status from a adb shell
682    # command. Dalvik cache is cleaned before running to make subsequent executions
683    # of the script follow the same runtime path.
684    cmdline="cd $DEX_LOCATION && \
685             export ANDROID_DATA=$DEX_LOCATION && \
686             export ANDROID_ADDITIONAL_PUBLIC_LIBRARIES=$PUBLIC_LIBS && \
687             export DEX_LOCATION=$DEX_LOCATION && \
688             export ANDROID_ROOT=$ANDROID_ROOT && \
689             rm -rf ${DEX_LOCATION}/dalvik-cache/ && \
690             mkdir -p ${mkdir_locations} && \
691             export LD_LIBRARY_PATH=$LD_LIBRARY_PATH && \
692             export PATH=$ANDROID_ROOT/bin:$PATH && \
693             $profman_cmdline && \
694             $dex2oat_cmdline && \
695             $vdex_cmdline && \
696             $strip_cmdline && \
697             $sync_cmdline && \
698             $dalvikvm_cmdline"
699
700    cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME")
701    echo "$cmdline" > $cmdfile
702
703    if [ "$DEV_MODE" = "y" ]; then
704      echo $cmdline
705    fi
706
707    if [ "$QUIET" = "n" ]; then
708      adb push $cmdfile $DEX_LOCATION/cmdline.sh
709    else
710      adb push $cmdfile $DEX_LOCATION/cmdline.sh > /dev/null 2>&1
711    fi
712
713    if [ "$DRY_RUN" != "y" ]; then
714      adb shell sh $DEX_LOCATION/cmdline.sh
715    fi
716
717    rm -f $cmdfile
718else
719    # Host run.
720    export ANDROID_PRINTF_LOG=brief
721
722    # By default, and for prebuild dex2oat, we are interested in errors being logged. In dev mode
723    # we want debug messages.
724    if [ "$EXTERNAL_LOG_TAGS" = "n" ]; then
725      if [ "$DEV_MODE" = "y" ]; then
726          export ANDROID_LOG_TAGS='*:d'
727      else
728          export ANDROID_LOG_TAGS='*:e'
729      fi
730    fi
731
732    export ANDROID_DATA="$DEX_LOCATION"
733    export ANDROID_ROOT="${ANDROID_ROOT}"
734    export LD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
735    export DYLD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
736    export PATH="$PATH:${ANDROID_ROOT}/bin"
737
738    # Temporarily disable address space layout randomization (ASLR).
739    # This is needed on the host so that the linker loads core.oat at the necessary address.
740    export LD_USE_LOAD_BIAS=1
741
742    cmdline="$dalvikvm_cmdline"
743
744    if [ "$TIME_OUT" = "gdb" ]; then
745      if [ `uname` = "Darwin" ]; then
746        # Fall back to timeout on Mac.
747        TIME_OUT="timeout"
748      elif [ "$ISA" = "x86" ]; then
749        # prctl call may fail in 32-bit on an older (3.2) 64-bit Linux kernel. Fall back to timeout.
750        TIME_OUT="timeout"
751      else
752        # Check if gdb is available.
753        gdb --eval-command="quit" > /dev/null 2>&1
754        if [ $? != 0 ]; then
755          # gdb isn't available. Fall back to timeout.
756          TIME_OUT="timeout"
757        fi
758      fi
759    fi
760
761    if [ "$TIME_OUT" = "timeout" ]; then
762      # Add timeout command if time out is desired.
763      #
764      # Note: We first send SIGRTMIN+2 (usually 36) to ART, which will induce a full thread dump
765      #       before abort. However, dumping threads might deadlock, so we also use the "-k"
766      #       option to definitely kill the child.
767      cmdline="timeout -k 120s -s SIGRTMIN+2 ${TIME_OUT_VALUE}s $cmdline"
768    fi
769
770    if [ "$DEV_MODE" = "y" ]; then
771      echo "mkdir -p ${mkdir_locations} && $profman_cmdline && $dex2oat_cmdline && $vdex_cmdline && $strip_cmdline && $sync_cmdline && $cmdline"
772    fi
773
774    cd $ANDROID_BUILD_TOP
775
776    # Make sure we delete any existing compiler artifacts.
777    # This enables tests to call the RUN script multiple times in a row
778    # without worrying about interference.
779    rm -rf ${DEX_LOCATION}/oat
780    rm -rf ${DEX_LOCATION}/dalvik-cache/
781
782    mkdir -p ${mkdir_locations} || exit 1
783    $profman_cmdline || { echo "Profman failed." >&2 ; exit 2; }
784    $dex2oat_cmdline || { echo "Dex2oat failed." >&2 ; exit 2; }
785    $vdex_cmdline || { echo "Dex2oat failed." >&2 ; exit 2; }
786    $strip_cmdline || { echo "Strip failed." >&2 ; exit 3; }
787    $sync_cmdline || { echo "Sync failed." >&2 ; exit 4; }
788
789    # For running, we must turn off logging when dex2oat or patchoat are missing. Otherwise we use
790    # the same defaults as for prebuilt: everything when --dev, otherwise errors and above only.
791    if [ "$EXTERNAL_LOG_TAGS" = "n" ]; then
792      if [ "$DEV_MODE" = "y" ]; then
793          export ANDROID_LOG_TAGS='*:d'
794      elif [ "$USE_DEX2OAT_AND_PATCHOAT" = "n" ]; then
795          # All tests would log the error of failing dex2oat/patchoat. Be silent here and only
796          # log fatal events.
797          export ANDROID_LOG_TAGS='*:s'
798      else
799          # We are interested in LOG(ERROR) output.
800          export ANDROID_LOG_TAGS='*:e'
801      fi
802    fi
803
804    if [ "$DRY_RUN" = "y" ]; then
805      exit 0
806    fi
807
808    if [ "$USE_GDB" = "y" ]; then
809      # When running under gdb, we cannot do piping and grepping...
810      $cmdline "$@"
811    else
812      if [ "$TIME_OUT" != "gdb" ]; then
813        trap 'kill -INT -$pid' INT
814        $cmdline "$@" 2>&1 & pid=$!
815        wait $pid
816        # Add extra detail if time out is enabled.
817        if [ ${PIPESTATUS[0]} = 124 ] && [ "$TIME_OUT" = "timeout" ]; then
818          echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
819        fi
820      else
821        # With a thread dump that uses gdb if a timeout.
822        trap 'kill -INT -$pid' INT
823        $cmdline "$@" 2>&1 & pid=$!
824        # Spawn a watcher process.
825        ( sleep $TIME_OUT_VALUE && \
826          echo "##### Thread dump using gdb on test timeout" && \
827          ( gdb -q -p $pid --eval-command="info thread" --eval-command="thread apply all bt" \
828                           --eval-command="call exit(124)" --eval-command=quit || \
829            kill $pid )) 2> /dev/null & watcher=$!
830        wait $pid
831        test_exit_status=$?
832        pkill -P $watcher 2> /dev/null # kill the sleep which will in turn end the watcher as well
833        if [ $test_exit_status = 0 ]; then
834          # The test finished normally.
835          exit 0
836        else
837          # The test failed or timed out.
838          if [ $test_exit_status = 124 ]; then
839            # The test timed out.
840            echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
841          fi
842        fi
843      fi
844    fi
845fi
846