1#!/bin/bash
2#
3# Runner for an individual run-test.
4
5if [[ -z "$ANDROID_BUILD_TOP" ]]; then
6  echo 'ANDROID_BUILD_TOP environment variable is empty; did you forget to run `lunch`?'
7  exit 1
8fi
9
10msg() {
11    if [ "$QUIET" = "n" ]; then
12        echo "$@"
13    fi
14}
15
16ANDROID_ROOT="/system"
17ANDROID_ART_ROOT="/apex/com.android.art"
18ANDROID_I18N_ROOT="/apex/com.android.i18n"
19ANDROID_TZDATA_ROOT="/apex/com.android.tzdata"
20ARCHITECTURES_32="(arm|x86|none)"
21ARCHITECTURES_64="(arm64|x86_64|none)"
22ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
23GET_DEVICE_ISA_BITNESS_FLAG="--32"
24BOOT_IMAGE=""
25CHROOT=
26COMPILE_FLAGS=""
27DALVIKVM="dalvikvm32"
28DEBUGGER="n"
29WITH_AGENT=()
30DEBUGGER_AGENT=""
31WRAP_DEBUGGER_AGENT="n"
32DEV_MODE="n"
33DEX2OAT_NDEBUG_BINARY="dex2oat32"
34DEX2OAT_DEBUG_BINARY="dex2oatd32"
35EXPERIMENTAL=""
36FALSE_BIN="false"
37FLAGS=""
38ANDROID_FLAGS=""
39GDB=""
40GDB_ARGS=""
41GDBSERVER_DEVICE="gdbserver"
42GDBSERVER_HOST="gdbserver"
43HAVE_IMAGE="y"
44HOST="n"
45BIONIC="n"
46CREATE_ANDROID_ROOT="n"
47USE_ZIPAPEX="n"
48ZIPAPEX_LOC=""
49USE_EXTRACTED_ZIPAPEX="n"
50EXTRACTED_ZIPAPEX_LOC=""
51INTERPRETER="n"
52JIT="n"
53INVOKE_WITH=""
54IS_JVMTI_TEST="n"
55ADD_LIBDIR_ARGUMENTS="n"
56ISA=x86
57LIBRARY_DIRECTORY="lib"
58TEST_DIRECTORY="nativetest"
59MAIN=""
60OPTIMIZE="y"
61PREBUILD="y"
62QUIET="n"
63RELOCATE="n"
64SECONDARY_DEX=""
65TIME_OUT="n"  # "n" (disabled), "timeout" (use timeout), "gdb" (use gdb)
66TIMEOUT_DUMPER=signal_dumper
67# Values in seconds.
68TIME_OUT_EXTRA=0
69TIME_OUT_VALUE=
70USE_GDB="n"
71USE_GDBSERVER="n"
72GDBSERVER_PORT=":5039"
73USE_JVM="n"
74USE_JVMTI="n"
75VERIFY="y" # y=yes,n=no,s=softfail
76ZYGOTE=""
77DEX_VERIFY=""
78INSTRUCTION_SET_FEATURES=""
79ARGS=""
80VDEX_ARGS=""
81EXTERNAL_LOG_TAGS="n" # if y respect externally set ANDROID_LOG_TAGS.
82DRY_RUN="n" # if y prepare to run the test but don't run it.
83TEST_VDEX="n"
84TEST_DM="n"
85TEST_IS_NDEBUG="n"
86APP_IMAGE="y"
87SECONDARY_APP_IMAGE="y"
88SECONDARY_CLASS_LOADER_CONTEXT=""
89SECONDARY_COMPILATION="y"
90JVMTI_STRESS="n"
91JVMTI_STEP_STRESS="n"
92JVMTI_FIELD_STRESS="n"
93JVMTI_TRACE_STRESS="n"
94JVMTI_REDEFINE_STRESS="n"
95PROFILE="n"
96RANDOM_PROFILE="n"
97# The normal dex2oat timeout.
98DEX2OAT_TIMEOUT="300" # 5 mins
99# The *hard* timeout where we really start trying to kill the dex2oat.
100DEX2OAT_RT_TIMEOUT="360" # 6 mins
101CREATE_RUNNER="n"
102
103# if "y", run 'sync' before dalvikvm to make sure all files from
104# build step (e.g. dex2oat) were finished writing.
105SYNC_BEFORE_RUN="n"
106
107# When running a debug build, we want to run with all checks.
108ANDROID_FLAGS="${ANDROID_FLAGS} -XX:SlowDebug=true"
109# The same for dex2oatd, both prebuild and runtime-driven.
110ANDROID_FLAGS="${ANDROID_FLAGS} -Xcompiler-option --runtime-arg -Xcompiler-option -XX:SlowDebug=true"
111COMPILER_FLAGS="${COMPILER_FLAGS} --runtime-arg -XX:SlowDebug=true"
112
113# Let the compiler and runtime know that we are running tests.
114COMPILE_FLAGS="${COMPILE_FLAGS} --compile-art-test"
115ANDROID_FLAGS="${ANDROID_FLAGS} -Xcompiler-option --compile-art-test"
116
117while true; do
118    if [ "x$1" = "x--quiet" ]; then
119        QUIET="y"
120        shift
121    elif [ "x$1" = "x--dex2oat-rt-timeout" ]; then
122        shift
123        if [ "x$1" = "x" ]; then
124            echo "$0 missing argument to --dex2oat-rt-timeout" 1>&2
125            exit 1
126        fi
127        DEX2OAT_RT_TIMEOUT="$1"
128        shift
129    elif [ "x$1" = "x--dex2oat-timeout" ]; then
130        shift
131        if [ "x$1" = "x" ]; then
132            echo "$0 missing argument to --dex2oat-timeout" 1>&2
133            exit 1
134        fi
135        DEX2OAT_TIMEOUT="$1"
136        shift
137    elif [ "x$1" = "x--jvmti" ]; then
138        USE_JVMTI="y"
139        IS_JVMTI_TEST="y"
140        # Secondary images block some tested behavior.
141        SECONDARY_APP_IMAGE="n"
142        shift
143    elif [ "x$1" = "x--add-libdir-argument" ]; then
144        ADD_LIBDIR_ARGUMENTS="y"
145        shift
146    elif [ "x$1" = "x-O" ]; then
147        TEST_IS_NDEBUG="y"
148        shift
149    elif [ "x$1" = "x--lib" ]; then
150        shift
151        if [ "x$1" = "x" ]; then
152            echo "$0 missing argument to --lib" 1>&2
153            exit 1
154        fi
155        LIB="$1"
156        shift
157    elif [ "x$1" = "x--gc-stress" ]; then
158        # Give an extra 20 mins if we are gc-stress.
159        TIME_OUT_EXTRA=$((${TIME_OUT_EXTRA} + 1200))
160        shift
161    elif [ "x$1" = "x--testlib" ]; then
162        shift
163        if [ "x$1" = "x" ]; then
164            echo "$0 missing argument to --testlib" 1>&2
165            exit 1
166        fi
167        ARGS="${ARGS} $1"
168        shift
169    elif [ "x$1" = "x--args" ]; then
170        shift
171        if [ "x$1" = "x" ]; then
172            echo "$0 missing argument to --args" 1>&2
173            exit 1
174        fi
175        ARGS="${ARGS} $1"
176        shift
177    elif [ "x$1" = "x--compiler-only-option" ]; then
178        shift
179        option="$1"
180        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
181        shift
182    elif [ "x$1" = "x-Xcompiler-option" ]; then
183        shift
184        option="$1"
185        FLAGS="${FLAGS} -Xcompiler-option $option"
186        COMPILE_FLAGS="${COMPILE_FLAGS} $option"
187        shift
188    elif [ "x$1" = "x--create-runner" ]; then
189        CREATE_RUNNER="y"
190        shift
191    elif [ "x$1" = "x--android-runtime-option" ]; then
192        shift
193        option="$1"
194        ANDROID_FLAGS="${ANDROID_FLAGS} $option"
195        shift
196    elif [ "x$1" = "x--runtime-option" ]; then
197        shift
198        option="$1"
199        FLAGS="${FLAGS} $option"
200        if [ "x$option" = "x-Xmethod-trace" ]; then
201            # Method tracing can slow some tests down a lot, in particular
202            # 530-checker-lse2.
203            TIME_OUT_EXTRA=$((${TIME_OUT_EXTRA} + 1200))
204        fi
205        shift
206    elif [ "x$1" = "x--boot" ]; then
207        shift
208        BOOT_IMAGE="$1"
209        shift
210    elif [ "x$1" = "x--relocate" ]; then
211        RELOCATE="y"
212        shift
213    elif [ "x$1" = "x--no-relocate" ]; then
214        RELOCATE="n"
215        shift
216    elif [ "x$1" = "x--prebuild" ]; then
217        PREBUILD="y"
218        shift
219    elif [ "x$1" = "x--compact-dex-level" ]; then
220        shift
221        COMPILE_FLAGS="${COMPILE_FLAGS} --compact-dex-level=$1"
222        shift
223    elif [ "x$1" = "x--jvmti-redefine-stress" ]; then
224        # APP_IMAGE doesn't really work with jvmti redefine stress
225        USE_JVMTI="y"
226        APP_IMAGE="n"
227        SECONDARY_APP_IMAGE="n"
228        JVMTI_STRESS="y"
229        JVMTI_REDEFINE_STRESS="y"
230        shift
231    elif [ "x$1" = "x--jvmti-step-stress" ]; then
232        USE_JVMTI="y"
233        JVMTI_STRESS="y"
234        JVMTI_STEP_STRESS="y"
235        shift
236    elif [ "x$1" = "x--jvmti-field-stress" ]; then
237        USE_JVMTI="y"
238        JVMTI_STRESS="y"
239        JVMTI_FIELD_STRESS="y"
240        shift
241    elif [ "x$1" = "x--jvmti-trace-stress" ]; then
242        USE_JVMTI="y"
243        JVMTI_STRESS="y"
244        JVMTI_TRACE_STRESS="y"
245        shift
246    elif [ "x$1" = "x--no-app-image" ]; then
247        APP_IMAGE="n"
248        shift
249    elif [ "x$1" = "x--no-secondary-app-image" ]; then
250        SECONDARY_APP_IMAGE="n"
251        shift
252    elif [ "x$1" = "x--secondary-class-loader-context" ]; then
253        shift
254        SECONDARY_CLASS_LOADER_CONTEXT="$1"
255        shift
256    elif [ "x$1" = "x--no-secondary-compilation" ]; then
257        SECONDARY_COMPILATION="n"
258        shift
259    elif [ "x$1" = "x--host" ]; then
260        HOST="y"
261        ANDROID_ROOT="${ANDROID_HOST_OUT}"
262        ANDROID_ART_ROOT="${ANDROID_HOST_OUT}/com.android.art"
263        ANDROID_I18N_ROOT="${ANDROID_HOST_OUT}/com.android.i18n"
264        ANDROID_TZDATA_ROOT="${ANDROID_HOST_OUT}/com.android.tzdata"
265        # On host, we default to using the symlink, as the PREFER_32BIT
266        # configuration is the only configuration building a 32bit version of
267        # dex2oat.
268        DEX2OAT_DEBUG_BINARY="dex2oatd"
269        DEX2OAT_NDEBUG_BINARY="dex2oat"
270        shift
271    elif [ "x$1" = "x--bionic" ]; then
272        BIONIC="y"
273        # We need to create an ANDROID_ROOT because currently we cannot create
274        # the frameworks/libcore with linux_bionic so we need to use the normal
275        # host ones which are in a different location.
276        CREATE_ANDROID_ROOT="y"
277        shift
278    elif [ "x$1" = "x--runtime-extracted-zipapex" ]; then
279        shift
280        USE_EXTRACTED_ZIPAPEX="y"
281        EXTRACTED_ZIPAPEX_LOC="$1"
282        shift
283    elif [ "x$1" = "x--runtime-zipapex" ]; then
284        shift
285        USE_ZIPAPEX="y"
286        ZIPAPEX_LOC="$1"
287        # TODO (b/119942078): Currently apex does not support
288        # symlink_preferred_arch so we will not have a dex2oatd to execute and
289        # need to manually provide
290        # dex2oatd64.
291        DEX2OAT_DEBUG_BINARY="dex2oatd64"
292        shift
293    elif [ "x$1" = "x--no-prebuild" ]; then
294        PREBUILD="n"
295        shift
296    elif [ "x$1" = "x--no-image" ]; then
297        HAVE_IMAGE="n"
298        shift
299    elif [ "x$1" = "x--secondary" ]; then
300        SECONDARY_DEX=":$DEX_LOCATION/$TEST_NAME-ex.jar"
301        # Enable cfg-append to make sure we get the dump for both dex files.
302        # (otherwise the runtime compilation of the secondary dex will overwrite
303        # the dump of the first one).
304        FLAGS="${FLAGS} -Xcompiler-option --dump-cfg-append"
305        COMPILE_FLAGS="${COMPILE_FLAGS} --dump-cfg-append"
306        shift
307    elif [ "x$1" = "x--with-agent" ]; then
308        shift
309        USE_JVMTI="y"
310        WITH_AGENT+=("$1")
311        shift
312    elif [ "x$1" = "x--debug-wrap-agent" ]; then
313        WRAP_DEBUGGER_AGENT="y"
314        shift
315    elif [ "x$1" = "x--debug-agent" ]; then
316        shift
317        DEBUGGER="agent"
318        USE_JVMTI="y"
319        DEBUGGER_AGENT="$1"
320        TIME_OUT="n"
321        shift
322    elif [ "x$1" = "x--debug" ]; then
323        USE_JVMTI="y"
324        DEBUGGER="y"
325        TIME_OUT="n"
326        shift
327    elif [ "x$1" = "x--gdbserver-port" ]; then
328        shift
329        GDBSERVER_PORT=$1
330        shift
331    elif [ "x$1" = "x--gdbserver-bin" ]; then
332        shift
333        GDBSERVER_HOST=$1
334        GDBSERVER_DEVICE=$1
335        shift
336    elif [ "x$1" = "x--gdbserver" ]; then
337        USE_GDBSERVER="y"
338        DEV_MODE="y"
339        TIME_OUT="n"
340        shift
341    elif [ "x$1" = "x--gdb" ]; then
342        USE_GDB="y"
343        DEV_MODE="y"
344        TIME_OUT="n"
345        shift
346    elif [ "x$1" = "x--gdb-arg" ]; then
347        shift
348        gdb_arg="$1"
349        GDB_ARGS="${GDB_ARGS} $gdb_arg"
350        shift
351    elif [ "x$1" = "x--zygote" ]; then
352        ZYGOTE="-Xzygote"
353        msg "Spawning from zygote"
354        shift
355    elif [ "x$1" = "x--dev" ]; then
356        DEV_MODE="y"
357        shift
358    elif [ "x$1" = "x--interpreter" ]; then
359        INTERPRETER="y"
360        shift
361    elif [ "x$1" = "x--jit" ]; then
362        JIT="y"
363        shift
364    elif [ "x$1" = "x--baseline" ]; then
365        FLAGS="${FLAGS} -Xcompiler-option --baseline"
366        COMPILE_FLAGS="${COMPILE_FLAGS} --baseline"
367        shift
368    elif [ "x$1" = "x--jvm" ]; then
369        USE_JVM="y"
370        shift
371    elif [ "x$1" = "x--invoke-with" ]; then
372        shift
373        if [ "x$1" = "x" ]; then
374            echo "$0 missing argument to --invoke-with" 1>&2
375            exit 1
376        fi
377        if [ "x$INVOKE_WITH" = "x" ]; then
378            INVOKE_WITH="$1"
379        else
380            INVOKE_WITH="$INVOKE_WITH $1"
381        fi
382        shift
383    elif [ "x$1" = "x--no-verify" ]; then
384        VERIFY="n"
385        shift
386    elif [ "x$1" = "x--verify-soft-fail" ]; then
387        VERIFY="s"
388        shift
389    elif [ "x$1" = "x--no-optimize" ]; then
390        OPTIMIZE="n"
391        shift
392    elif [ "x$1" = "x--chroot" ]; then
393        shift
394        CHROOT="$1"
395        shift
396    elif [ "x$1" = "x--android-root" ]; then
397        shift
398        ANDROID_ROOT="$1"
399        shift
400    elif [ "x$1" = "x--android-i18n-root" ]; then
401        shift
402        ANDROID_I18N_ROOT="$1"
403        shift
404    elif [ "x$1" = "x--android-art-root" ]; then
405        shift
406        ANDROID_ART_ROOT="$1"
407        shift
408    elif [ "x$1" = "x--android-tzdata-root" ]; then
409        shift
410        ANDROID_TZDATA_ROOT="$1"
411        shift
412    elif [ "x$1" = "x--instruction-set-features" ]; then
413        shift
414        INSTRUCTION_SET_FEATURES="$1"
415        shift
416    elif [ "x$1" = "x--timeout" ]; then
417        shift
418        TIME_OUT_VALUE="$1"
419        shift
420    elif [ "x$1" = "x--" ]; then
421        shift
422        break
423    elif [ "x$1" = "x--64" ]; then
424        ISA="x86_64"
425        GDBSERVER_DEVICE="gdbserver64"
426        DALVIKVM="dalvikvm64"
427        LIBRARY_DIRECTORY="lib64"
428        TEST_DIRECTORY="nativetest64"
429        ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
430        GET_DEVICE_ISA_BITNESS_FLAG="--64"
431        DEX2OAT_NDEBUG_BINARY="dex2oat64"
432        DEX2OAT_DEBUG_BINARY="dex2oatd64"
433        shift
434    elif [ "x$1" = "x--experimental" ]; then
435        if [ "$#" -lt 2 ]; then
436            echo "missing --experimental option" 1>&2
437            exit 1
438        fi
439        EXPERIMENTAL="$EXPERIMENTAL $2"
440        shift 2
441    elif [ "x$1" = "x--external-log-tags" ]; then
442        EXTERNAL_LOG_TAGS="y"
443        shift
444    elif [ "x$1" = "x--dry-run" ]; then
445        DRY_RUN="y"
446        shift
447    elif [ "x$1" = "x--vdex" ]; then
448        TEST_VDEX="y"
449        shift
450    elif [ "x$1" = "x--dm" ]; then
451        TEST_DM="y"
452        shift
453    elif [ "x$1" = "x--vdex-filter" ]; then
454        shift
455        option="$1"
456        VDEX_ARGS="${VDEX_ARGS} --compiler-filter=$option"
457        shift
458    elif [ "x$1" = "x--vdex-arg" ]; then
459        shift
460        VDEX_ARGS="${VDEX_ARGS} $1"
461        shift
462    elif [ "x$1" = "x--sync" ]; then
463        SYNC_BEFORE_RUN="y"
464        shift
465    elif [ "x$1" = "x--profile" ]; then
466        PROFILE="y"
467        shift
468    elif [ "x$1" = "x--random-profile" ]; then
469        RANDOM_PROFILE="y"
470        shift
471    elif expr "x$1" : "x--" >/dev/null 2>&1; then
472        echo "unknown $0 option: $1" 1>&2
473        exit 1
474    else
475        break
476    fi
477done
478
479# HACK: Force the use of `signal_dumper` on host.
480if [[ "$HOST" = "y" ]]; then
481  TIME_OUT="timeout"
482fi
483
484# If you change this, update the timeout in testrunner.py as well.
485if [ -z "$TIME_OUT_VALUE" ] ; then
486  # 10 minutes is the default.
487  TIME_OUT_VALUE=600
488
489  # For sanitized builds use a larger base.
490  # TODO: Consider sanitized target builds?
491  if [ "x$SANITIZE_HOST" != "x" ] ; then
492    TIME_OUT_VALUE=1500  # 25 minutes.
493  fi
494
495  TIME_OUT_VALUE=$((${TIME_OUT_VALUE} + ${TIME_OUT_EXTRA}))
496fi
497
498# Escape hatch for slow hosts or devices. Accept an environment variable as a timeout factor.
499if [ ! -z "$ART_TIME_OUT_MULTIPLIER" ] ; then
500  TIME_OUT_VALUE=$((${TIME_OUT_VALUE} * ${ART_TIME_OUT_MULTIPLIER}))
501fi
502
503# The DEX_LOCATION with the chroot prefix, if any.
504CHROOT_DEX_LOCATION="$CHROOT$DEX_LOCATION"
505
506# If running on device, determine the ISA of the device.
507if [ "$HOST" = "n" -a "$USE_JVM" = "n" ]; then
508  ISA=$("$ANDROID_BUILD_TOP/art/test/utils/get-device-isa" "$GET_DEVICE_ISA_BITNESS_FLAG")
509fi
510
511if [ "$USE_JVM" = "n" ]; then
512    FLAGS="${FLAGS} ${ANDROID_FLAGS}"
513    # we don't want to be trying to get adbconnections since the plugin might
514    # not have been built.
515    FLAGS="${FLAGS} -XjdwpProvider:none"
516    for feature in ${EXPERIMENTAL}; do
517        FLAGS="${FLAGS} -Xexperimental:${feature} -Xcompiler-option --runtime-arg -Xcompiler-option -Xexperimental:${feature}"
518        COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xexperimental:${feature}"
519    done
520fi
521
522if [ "$CREATE_ANDROID_ROOT" = "y" ]; then
523    ANDROID_ROOT=$DEX_LOCATION/android-root
524fi
525
526if [ "x$1" = "x" ] ; then
527  MAIN="Main"
528else
529  MAIN="$1"
530  shift
531fi
532
533if [ "$ZYGOTE" = "" ]; then
534    if [ "$OPTIMIZE" = "y" ]; then
535        if [ "$VERIFY" = "y" ]; then
536            DEX_OPTIMIZE="-Xdexopt:verified"
537        else
538            DEX_OPTIMIZE="-Xdexopt:all"
539        fi
540        msg "Performing optimizations"
541    else
542        DEX_OPTIMIZE="-Xdexopt:none"
543        msg "Skipping optimizations"
544    fi
545
546    if [ "$VERIFY" = "y" ]; then
547        JVM_VERIFY_ARG="-Xverify:all"
548        msg "Performing verification"
549    elif [ "$VERIFY" = "s" ]; then
550        JVM_VERIFY_ARG="Xverify:all"
551        DEX_VERIFY="-Xverify:softfail"
552        msg "Forcing verification to be soft fail"
553    else # VERIFY = "n"
554        DEX_VERIFY="-Xverify:none"
555        JVM_VERIFY_ARG="-Xverify:none"
556        msg "Skipping verification"
557    fi
558fi
559
560msg "------------------------------"
561
562if [ "$DEBUGGER" = "y" ]; then
563  # Use this instead for ddms and connect by running 'ddms':
564  # DEBUGGER_OPTS="-XjdwpOptions=server=y,suspend=y -XjdwpProvider:adbconnection"
565  # TODO: add a separate --ddms option?
566
567  PORT=12345
568  msg "Waiting for jdb to connect:"
569  if [ "$HOST" = "n" ]; then
570    msg "    adb forward tcp:$PORT tcp:$PORT"
571  fi
572  msg "    jdb -attach localhost:$PORT"
573  if [ "$USE_JVM" = "n" ]; then
574    # Use the default libjdwp agent. Use --debug-agent to use a custom one.
575    DEBUGGER_OPTS="-agentpath:libjdwp.so=transport=dt_socket,address=$PORT,server=y,suspend=y -XjdwpProvider:internal"
576  else
577    DEBUGGER_OPTS="-agentlib:jdwp=transport=dt_socket,address=$PORT,server=y,suspend=y"
578  fi
579elif [ "$DEBUGGER" = "agent" ]; then
580  PORT=12345
581  # TODO Support ddms connection and support target.
582  if [ "$HOST" = "n" ]; then
583    echo "--debug-agent not supported yet for target!"
584    exit 1
585  fi
586  AGENTPATH=${DEBUGGER_AGENT}
587  if [ "$WRAP_DEBUGGER_AGENT" = "y" ]; then
588    WRAPPROPS="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}/libwrapagentpropertiesd.so"
589    if [ "$TEST_IS_NDEBUG" = "y" ]; then
590      WRAPPROPS="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}/libwrapagentproperties.so"
591    fi
592    AGENTPATH="${WRAPPROPS}=${ANDROID_BUILD_TOP}/art/tools/libjdwp-compat.props,${AGENTPATH}"
593  fi
594  msg "Connect to localhost:$PORT"
595  DEBUGGER_OPTS="-agentpath:${AGENTPATH}=transport=dt_socket,address=$PORT,server=y,suspend=y"
596fi
597
598for agent in "${WITH_AGENT[@]}"; do
599  FLAGS="${FLAGS} -agentpath:${agent}"
600done
601
602if [ "$USE_JVMTI" = "y" ]; then
603  if [ "$USE_JVM" = "n" ]; then
604    plugin=libopenjdkjvmtid.so
605    if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
606      plugin=libopenjdkjvmti.so
607    fi
608    # We used to add flags here that made the runtime debuggable but that is not
609    # needed anymore since the plugin can do it for us now.
610    FLAGS="${FLAGS} -Xplugin:${plugin}"
611  fi
612fi
613
614# Add the libdir to the argv passed to the main function.
615if [ "$ADD_LIBDIR_ARGUMENTS" = "y" ]; then
616  if [[ "$HOST" = "y" ]]; then
617    ARGS="${ARGS} ${ANDROID_HOST_OUT}/${TEST_DIRECTORY}/"
618  else
619    ARGS="${ARGS} /data/${TEST_DIRECTORY}/art/${ISA}/"
620  fi
621fi
622if [ "$IS_JVMTI_TEST" = "y" ]; then
623  agent=libtiagentd.so
624  lib=tiagentd
625  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
626    agent=libtiagent.so
627    lib=tiagent
628  fi
629
630  ARGS="${ARGS} ${lib}"
631  if [[ "$USE_JVM" = "y" ]]; then
632    FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${TEST_NAME},jvm"
633  else
634    if [[ "$HOST" = "y" ]]; then
635      FLAGS="${FLAGS} -agentpath:${agent}=${TEST_NAME},art"
636    else
637      # The linker configuration used for dalvikvm(64) in the ART APEX
638      # requires us to pass the full path to the agent to the runtime when
639      # running on device.
640      FLAGS="${FLAGS} -agentpath:/data/${TEST_DIRECTORY}/art/${ISA}/${agent}=${TEST_NAME},art"
641    fi
642  fi
643fi
644
645if [[ "$JVMTI_STRESS" = "y" ]]; then
646  agent=libtistressd.so
647  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
648    agent=libtistress.so
649  fi
650
651  # Just give it a default start so we can always add ',' to it.
652  agent_args="jvmti-stress"
653  if [[ "$JVMTI_REDEFINE_STRESS" = "y" ]]; then
654    # We really cannot do this on RI so don't both passing it in that case.
655    if [[ "$USE_JVM" = "n" ]]; then
656      agent_args="${agent_args},redefine"
657    fi
658  fi
659  if [[ "$JVMTI_FIELD_STRESS" = "y" ]]; then
660    agent_args="${agent_args},field"
661  fi
662  if [[ "$JVMTI_STEP_STRESS" = "y" ]]; then
663    agent_args="${agent_args},step"
664  fi
665  if [[ "$JVMTI_TRACE_STRESS" = "y" ]]; then
666    agent_args="${agent_args},trace"
667  fi
668  # In the future add onto this;
669  if [[ "$USE_JVM" = "y" ]]; then
670    FLAGS="${FLAGS} -agentpath:${ANDROID_HOST_OUT}/nativetest64/${agent}=${agent_args}"
671  else
672    if [[ "$HOST" = "y" ]]; then
673      FLAGS="${FLAGS} -agentpath:${agent}=${agent_args}"
674    else
675      # The linker configuration used for dalvikvm(64) in the ART APEX
676      # requires us to pass the full path to the agent to the runtime when
677      # running on device.
678      FLAGS="${FLAGS} -agentpath:/data/${TEST_DIRECTORY}/art/${ISA}/${agent}=${agent_args}"
679    fi
680  fi
681fi
682
683if [ "$USE_JVM" = "y" ]; then
684  export LD_LIBRARY_PATH=${ANDROID_HOST_OUT}/lib64
685  # Some jvmti tests are flaky without -Xint on the RI.
686  if [ "$IS_JVMTI_TEST" = "y" ]; then
687    FLAGS="${FLAGS} -Xint"
688  fi
689  # Xmx is necessary since we don't pass down the ART flags to JVM.
690  # We pass the classes2 path whether it's used (src-multidex) or not.
691  cmdline="${JAVA} ${DEBUGGER_OPTS} ${JVM_VERIFY_ARG} -Xmx256m -classpath classes:classes2 ${FLAGS} $MAIN $@ ${ARGS}"
692  if [ "$DEV_MODE" = "y" ]; then
693    echo $cmdline
694  fi
695  if [ "$CREATE_RUNNER" = "y" ]; then
696    echo "#!/bin/bash" > runit.sh
697    echo "export LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH\""
698    echo $cmdline "2>&1" >> runit.sh
699    chmod u+x runit.sh
700    echo "Runnable test script written to $PWD/runit.sh"
701  else
702    $cmdline
703  fi
704  exit
705fi
706
707# Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
708# because that's what we use for compiling the boot.art image.
709# It may contain additional modules from TEST_CORE_JARS.
710bpath_modules="core-oj core-libart okhttp bouncycastle apache-xml core-icu4j conscrypt"
711bpath=""
712bpath_locations=""
713bpath_separator=""
714bpath_prefix=""
715bpath_location_prefix=""
716if [ "${HOST}" = "y" ]; then
717  bpath_prefix="${ANDROID_HOST_OUT}"
718  if [ "${ANDROID_HOST_OUT:0:${#ANDROID_BUILD_TOP}+1}" = "${ANDROID_BUILD_TOP}/" ]; then
719    bpath_location_prefix="${ANDROID_HOST_OUT:${#ANDROID_BUILD_TOP}+1}"
720  else
721    echo "error: ANDROID_BUILD_TOP/ is not a prefix of ANDROID_HOST_OUT"
722    echo "ANDROID_BUILD_TOP=${ANDROID_BUILD_TOP}"
723    echo "ANDROID_HOST_OUT=${ANDROID_HOST_OUT}"
724    exit
725  fi
726fi
727for bpath_module in ${bpath_modules}; do
728  apex_module="com.android.art"
729  case "$bpath_module" in
730    (conscrypt)  apex_module="com.android.conscrypt";;
731    (core-icu4j) apex_module="com.android.i18n";;
732    (*)          apex_module="com.android.art";;
733  esac
734  bpath_jar="/apex/${apex_module}/javalib/${bpath_module}.jar"
735  bpath+="${bpath_separator}${bpath_prefix}${bpath_jar}"
736  bpath_locations+="${bpath_separator}${bpath_location_prefix}${bpath_jar}"
737  bpath_separator=":"
738done
739# Pass down the bootclasspath
740FLAGS="${FLAGS} -Xbootclasspath:${bpath}"
741FLAGS="${FLAGS} -Xbootclasspath-locations:${bpath_locations}"
742COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath:${bpath}"
743COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xbootclasspath-locations:${bpath_locations}"
744
745if [ "$HAVE_IMAGE" = "n" ]; then
746    # Disable image dex2oat - this will forbid the runtime to patch or compile an image.
747    FLAGS="${FLAGS} -Xnoimage-dex2oat"
748
749    # We'll abuse a second flag here to test different behavior. If --relocate, use the
750    # existing image - relocation will fail as patching is disallowed. If --no-relocate,
751    # pass a non-existent image - compilation will fail as dex2oat is disallowed.
752    if [ "${RELOCATE}" = "y" ] ; then
753      DALVIKVM_BOOT_OPT="-Ximage:${BOOT_IMAGE}"
754    else
755      DALVIKVM_BOOT_OPT="-Ximage:/system/non-existent/boot.art"
756    fi
757else
758    DALVIKVM_BOOT_OPT="-Ximage:${BOOT_IMAGE}"
759fi
760
761
762if [ "$USE_GDB" = "y" ]; then
763  if [ "$USE_GDBSERVER" = "y" ]; then
764    echo "Cannot pass both --gdb and --gdbserver at the same time!" >&2
765    exit 1
766  elif [ "$HOST" = "n" ]; then
767    # We might not have any hostname resolution if we are using a chroot.
768    GDB="$GDBSERVER_DEVICE --no-startup-with-shell 127.0.0.1$GDBSERVER_PORT"
769  else
770    if [ `uname` = "Darwin" ]; then
771        GDB=lldb
772        GDB_ARGS="$GDB_ARGS -- $DALVIKVM"
773        DALVIKVM=
774    else
775        GDB=gdb
776        GDB_ARGS="$GDB_ARGS --args $DALVIKVM"
777        # Enable for Emacs "M-x gdb" support. TODO: allow extra gdb arguments on command line.
778        # gdbargs="--annotate=3 $gdbargs"
779    fi
780  fi
781elif [ "$USE_GDBSERVER" = "y" ]; then
782  if [ "$HOST" = "n" ]; then
783    # We might not have any hostname resolution if we are using a chroot.
784    GDB="$GDBSERVER_DEVICE --no-startup-with-shell 127.0.0.1$GDBSERVER_PORT"
785  else
786    GDB="$GDBSERVER_HOST $GDBSERVER_PORT"
787  fi
788fi
789
790if [ "$INTERPRETER" = "y" ]; then
791    INT_OPTS="${INT_OPTS} -Xint"
792    if [ "$VERIFY" = "y" ] ; then
793      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=quicken"
794      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=quicken"
795    elif [ "$VERIFY" = "s" ]; then
796      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=extract"
797      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=extract"
798      DEX_VERIFY="${DEX_VERIFY} -Xverify:softfail"
799    else # VERIFY = "n"
800      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=assume-verified"
801      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=assume-verified"
802      DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
803    fi
804fi
805
806if [ "$JIT" = "y" ]; then
807    INT_OPTS="${INT_OPTS} -Xusejit:true"
808    if [ "$VERIFY" = "y" ] ; then
809      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=quicken"
810      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=quicken"
811    else
812      INT_OPTS="${INT_OPTS} -Xcompiler-option --compiler-filter=assume-verified"
813      COMPILE_FLAGS="${COMPILE_FLAGS} --compiler-filter=assume-verified"
814      DEX_VERIFY="${DEX_VERIFY} -Xverify:none"
815    fi
816else
817    INT_OPTS="${INT_OPTS} -Xusejit:false"
818fi
819
820JNI_OPTS="-Xjnigreflimit:512 -Xcheck:jni"
821
822COMPILE_FLAGS="${COMPILE_FLAGS} --runtime-arg -Xnorelocate"
823if [ "$RELOCATE" = "y" ]; then
824    FLAGS="${FLAGS} -Xrelocate"
825else
826    FLAGS="$FLAGS -Xnorelocate"
827fi
828
829if [ "$BIONIC" = "y" ]; then
830  # This is the location that soong drops linux_bionic builds. Despite being
831  # called linux_bionic-x86 the build is actually amd64 (x86_64) only.
832  if [ ! -e "$OUT_DIR/soong/host/linux_bionic-x86" ]; then
833    echo "linux_bionic-x86 target doesn't seem to have been built!" >&2
834    exit 1
835  fi
836  # Set TIMEOUT_DUMPER manually so it works even with apex's
837  TIMEOUT_DUMPER=$OUT_DIR/soong/host/linux_bionic-x86/bin/signal_dumper
838fi
839
840# Prevent test from silently falling back to interpreter in no-prebuild mode. This happens
841# when DEX_LOCATION path is too long, because vdex/odex filename is constructed by taking
842# full path to dex, stripping leading '/', appending '@classes.vdex' and changing every
843# remaining '/' into '@'.
844if [ "$HOST" = "y" ]; then
845  max_filename_size=$(getconf NAME_MAX $DEX_LOCATION)
846else
847  # There is no getconf on device, fallback to standard value.
848  # See NAME_MAX in kernel <linux/limits.h>
849  max_filename_size=255
850fi
851# Compute VDEX_NAME.
852DEX_LOCATION_STRIPPED="${DEX_LOCATION#/}"
853VDEX_NAME="${DEX_LOCATION_STRIPPED//\//@}@$TEST_NAME.jar@classes.vdex"
854if [ ${#VDEX_NAME} -gt $max_filename_size ]; then
855    echo "Dex location path too long:"
856    echo "$VDEX_NAME is ${#VDEX_NAME} character long, and the limit is $max_filename_size."
857    exit 1
858fi
859
860if [ "$HOST" = "y" ]; then
861  # On host, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the `bin`
862  # directory under the "Android Root" (usually `out/host/linux-x86`).
863  #
864  # TODO(b/130295968): Adjust this if/when ART host artifacts are installed
865  # under the ART root (usually `out/host/linux-x86/com.android.art`).
866  ANDROID_ART_BIN_DIR=$ANDROID_ROOT/bin
867else
868  # On target, run binaries (`dex2oat(d)`, `dalvikvm`, `profman`) from the ART
869  # APEX's `bin` directory. This means the linker will observe the ART APEX
870  # linker configuration file (`/apex/com.android.art/etc/ld.config.txt`) for
871  # these binaries.
872  ANDROID_ART_BIN_DIR=$ANDROID_ART_ROOT/bin
873fi
874
875profman_cmdline="true"
876dex2oat_cmdline="true"
877vdex_cmdline="true"
878dm_cmdline="true"
879mkdir_locations="${DEX_LOCATION}/dalvik-cache/$ISA"
880strip_cmdline="true"
881sync_cmdline="true"
882linkroot_cmdline="true"
883linkroot_overlay_cmdline="true"
884setupapex_cmdline="true"
885installapex_cmdline="true"
886
887linkdirs() {
888  find "$1" -maxdepth 1 -mindepth 1 -type d | xargs -i ln -sf '{}' "$2"
889}
890
891if [ "$CREATE_ANDROID_ROOT" = "y" ]; then
892  mkdir_locations="${mkdir_locations} ${ANDROID_ROOT}"
893  linkroot_cmdline="linkdirs ${ANDROID_HOST_OUT} ${ANDROID_ROOT}"
894  if [ "${BIONIC}" = "y" ]; then
895    # TODO Make this overlay more generic.
896    linkroot_overlay_cmdline="linkdirs $OUT_DIR/soong/host/linux_bionic-x86 ${ANDROID_ROOT}"
897  fi
898fi
899
900if [ "$USE_ZIPAPEX" = "y" ]; then
901  # TODO Currently this only works for linux_bionic zipapexes because those are
902  # stripped and so small enough that the ulimit doesn't kill us.
903  mkdir_locations="${mkdir_locations} $DEX_LOCATION/zipapex"
904  zip_options="-qq"
905  if [ "$DEV_MODE" = "y" ]; then
906    zip_options=""
907  fi
908  setupapex_cmdline="unzip -o -u ${zip_options} ${ZIPAPEX_LOC} apex_payload.zip -d ${DEX_LOCATION}"
909  installapex_cmdline="unzip -o -u ${zip_options} ${DEX_LOCATION}/apex_payload.zip -d ${DEX_LOCATION}/zipapex"
910  ANDROID_ART_BIN_DIR=$DEX_LOCATION/zipapex/bin
911elif [ "$USE_EXTRACTED_ZIPAPEX" = "y" ]; then
912  # Just symlink the zipapex binaries
913  ANDROID_ART_BIN_DIR=$DEX_LOCATION/zipapex/bin
914  # Force since some tests manually run this file twice.
915  ln_options=""
916  if [ "$DEV_MODE" = "y" ]; then
917    ln_options="--verbose"
918  fi
919  installapex_cmdline="ln -s -f ${ln_options} ${EXTRACTED_ZIPAPEX_LOC} ${DEX_LOCATION}/zipapex"
920fi
921
922# PROFILE takes precedence over RANDOM_PROFILE, since PROFILE tests require a
923# specific profile to run properly.
924if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
925  profman_cmdline="$ANDROID_ART_BIN_DIR/profman  \
926    --apk=$DEX_LOCATION/$TEST_NAME.jar \
927    --dex-location=$DEX_LOCATION/$TEST_NAME.jar"
928  if [ -f "$TEST_NAME-ex.jar" ] && [ "$SECONDARY_COMPILATION" = "y" ] ; then
929    profman_cmdline="${profman_cmdline} \
930      --apk=$DEX_LOCATION/$TEST_NAME-ex.jar \
931      --dex-location=$DEX_LOCATION/$TEST_NAME-ex.jar"
932  fi
933  COMPILE_FLAGS="${COMPILE_FLAGS} --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
934  FLAGS="${FLAGS} -Xcompiler-option --profile-file=$DEX_LOCATION/$TEST_NAME.prof"
935  if [ "$PROFILE" = "y" ]; then
936    profman_cmdline="${profman_cmdline} --create-profile-from=$DEX_LOCATION/profile \
937        --reference-profile-file=$DEX_LOCATION/$TEST_NAME.prof"
938  else
939    profman_cmdline="${profman_cmdline} --generate-test-profile=$DEX_LOCATION/$TEST_NAME.prof \
940        --generate-test-profile-seed=0"
941  fi
942fi
943
944function write_dex2oat_cmdlines {
945  local name="$1"
946
947  local class_loader_context=""
948  local enable_app_image=false
949  if [ "$APP_IMAGE" = "y" ]; then
950    enable_app_image=true
951  fi
952
953  # If the name ends in -ex then this is a secondary dex file
954  if [ "${name:${#name}-3}" = "-ex" ]; then
955    # Lazily realize the default value in case DEX_LOCATION/TEST_NAME change
956    [ -z "$SECONDARY_CLASS_LOADER_CONTEXT" ] && SECONDARY_CLASS_LOADER_CONTEXT="PCL[];PCL[$DEX_LOCATION/$TEST_NAME.jar]"
957    class_loader_context="'--class-loader-context=$SECONDARY_CLASS_LOADER_CONTEXT'"
958    $enable_app_image && [ "$SECONDARY_APP_IMAGE" = "y" ] || enable_app_image=false
959  fi
960
961  local app_image=""
962  $enable_app_image && app_image="--app-image-file=$DEX_LOCATION/oat/$ISA/$name.art --resolve-startup-const-strings=true"
963
964  local dex2oat_binary
965  dex2oat_binary=${DEX2OAT_DEBUG_BINARY}
966  if  [[ "$TEST_IS_NDEBUG" = "y" ]]; then
967    dex2oat_binary=${DEX2OAT_NDEBUG_BINARY}
968  fi
969  dex2oat_cmdline="$INVOKE_WITH $ANDROID_ART_BIN_DIR/$dex2oat_binary \
970                      $COMPILE_FLAGS \
971                      --boot-image=${BOOT_IMAGE} \
972                      --dex-file=$DEX_LOCATION/$name.jar \
973                      --oat-file=$DEX_LOCATION/oat/$ISA/$name.odex \
974                      "$app_image" \
975                      --generate-mini-debug-info \
976                      --instruction-set=$ISA \
977                      $class_loader_context"
978  if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
979    dex2oat_cmdline="${dex2oat_cmdline} --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
980  fi
981
982  # Add in a timeout. This is important for testing the compilation/verification time of
983  # pathological cases.
984  # Note: as we don't know how decent targets are (e.g., emulator), only do this on the host for
985  #       now. We should try to improve this.
986  #       The current value is rather arbitrary. run-tests should compile quickly.
987  # Watchdog timeout is in milliseconds so add 3 '0's to the dex2oat timeout.
988  if [ "$HOST" != "n" ]; then
989    # Use SIGRTMIN+2 to try to dump threads.
990    # Use -k 1m to SIGKILL it a minute later if it hasn't ended.
991    dex2oat_cmdline="timeout -k ${DEX2OAT_TIMEOUT}s -s SIGRTMIN+2 ${DEX2OAT_RT_TIMEOUT}s ${dex2oat_cmdline} --watchdog-timeout=${DEX2OAT_TIMEOUT}000"
992  fi
993  if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
994    vdex_cmdline="${dex2oat_cmdline} ${VDEX_ARGS} --input-vdex=$DEX_LOCATION/oat/$ISA/$name.vdex --output-vdex=$DEX_LOCATION/oat/$ISA/$name.vdex"
995  elif [ "$TEST_VDEX" = "y" ]; then
996    vdex_cmdline="${dex2oat_cmdline} ${VDEX_ARGS} --input-vdex=$DEX_LOCATION/oat/$ISA/$name.vdex"
997  elif [ "$TEST_DM" = "y" ]; then
998    dex2oat_cmdline="${dex2oat_cmdline} --output-vdex=$DEX_LOCATION/oat/$ISA/primary.vdex"
999    dm_cmdline="zip -qj $DEX_LOCATION/oat/$ISA/$name.dm $DEX_LOCATION/oat/$ISA/primary.vdex"
1000    vdex_cmdline="${dex2oat_cmdline} ${VDEX_ARGS} --dump-timings --dm-file=$DEX_LOCATION/oat/$ISA/$name.dm"
1001  fi
1002}
1003
1004# Enable mini-debug-info for JIT (if JIT is used).
1005FLAGS="$FLAGS -Xcompiler-option --generate-mini-debug-info"
1006
1007if [ "$PREBUILD" = "y" ]; then
1008  mkdir_locations="${mkdir_locations} ${DEX_LOCATION}/oat/$ISA"
1009
1010  # "Primary".
1011  write_dex2oat_cmdlines "$TEST_NAME"
1012  dex2oat_cmdline=$(echo $dex2oat_cmdline)
1013  dm_cmdline=$(echo $dm_cmdline)
1014  vdex_cmdline=$(echo $vdex_cmdline)
1015
1016  # Enable mini-debug-info for JIT (if JIT is used).
1017  FLAGS="$FLAGS -Xcompiler-option --generate-mini-debug-info"
1018
1019  if [ -f "$TEST_NAME-ex.jar" ] && [ "$SECONDARY_COMPILATION" = "y" ] ; then
1020    # "Secondary" for test coverage.
1021
1022    # Store primary values.
1023    base_dex2oat_cmdline="$dex2oat_cmdline"
1024    base_dm_cmdline="$dm_cmdline"
1025    base_vdex_cmdline="$vdex_cmdline"
1026
1027    write_dex2oat_cmdlines "$TEST_NAME-ex"
1028    dex2oat_cmdline=$(echo $dex2oat_cmdline)
1029    dm_cmdline=$(echo $dm_cmdline)
1030    vdex_cmdline=$(echo $vdex_cmdline)
1031
1032    # Concatenate.
1033    dex2oat_cmdline="$base_dex2oat_cmdline && $dex2oat_cmdline"
1034    dm_cmdline="$base_dm_cmdline" # Only use primary dm.
1035    vdex_cmdline="$base_vdex_cmdline && $vdex_cmdline"
1036  fi
1037fi
1038
1039if [ "$SYNC_BEFORE_RUN" = "y" ]; then
1040  sync_cmdline="sync"
1041fi
1042
1043DALVIKVM_ISA_FEATURES_ARGS=""
1044if [ "x$INSTRUCTION_SET_FEATURES" != "x" ] ; then
1045  DALVIKVM_ISA_FEATURES_ARGS="-Xcompiler-option --instruction-set-features=${INSTRUCTION_SET_FEATURES}"
1046fi
1047
1048# java.io.tmpdir can only be set at launch time.
1049TMP_DIR_OPTION=""
1050if [ "$HOST" = "n" ]; then
1051  TMP_DIR_OPTION="-Djava.io.tmpdir=/data/local/tmp"
1052fi
1053
1054# The build servers have an ancient version of bash so we cannot use @Q.
1055if [ "$USE_GDBSERVER" == "y" ]; then
1056  printf -v QUOTED_DALVIKVM_BOOT_OPT "%q" "$DALVIKVM_BOOT_OPT"
1057else
1058  QUOTED_DALVIKVM_BOOT_OPT="$DALVIKVM_BOOT_OPT"
1059fi
1060
1061# We set DumpNativeStackOnSigQuit to false to avoid stressing libunwind.
1062# b/27185632
1063# b/24664297
1064dalvikvm_cmdline="$INVOKE_WITH $GDB $ANDROID_ART_BIN_DIR/$DALVIKVM \
1065                  $GDB_ARGS \
1066                  $FLAGS \
1067                  $DEX_VERIFY \
1068                  -XXlib:$LIB \
1069                  $DEX2OAT \
1070                  $DALVIKVM_ISA_FEATURES_ARGS \
1071                  $ZYGOTE \
1072                  $JNI_OPTS \
1073                  $INT_OPTS \
1074                  $DEBUGGER_OPTS \
1075                  ${QUOTED_DALVIKVM_BOOT_OPT} \
1076                  $TMP_DIR_OPTION \
1077                  -XX:DumpNativeStackOnSigQuit:false \
1078                  -cp $DEX_LOCATION/$TEST_NAME.jar$SECONDARY_DEX $MAIN $ARGS"
1079
1080sanitize_dex2oat_cmdline() {
1081  local args=()
1082  for arg in "$@"; do
1083    if [ "$arg" = "--class-loader-context=&" ]; then
1084      arg="--class-loader-context=\&"
1085    fi
1086    args+=("$arg")
1087  done
1088  echo -n "${args[@]}"
1089}
1090
1091# Remove whitespace.
1092dex2oat_cmdline=$(sanitize_dex2oat_cmdline $(echo $dex2oat_cmdline))
1093dalvikvm_cmdline=$(echo $dalvikvm_cmdline)
1094dm_cmdline=$(echo $dm_cmdline)
1095vdex_cmdline=$(sanitize_dex2oat_cmdline $(echo $vdex_cmdline))
1096profman_cmdline=$(echo $profman_cmdline)
1097
1098# Use an empty ASAN_OPTIONS to enable defaults.
1099# Note: this is required as envsetup right now exports detect_leaks=0.
1100RUN_TEST_ASAN_OPTIONS=""
1101
1102# Multiple shutdown leaks. b/38341789
1103if [ "x$RUN_TEST_ASAN_OPTIONS" != "x" ] ; then
1104  RUN_TEST_ASAN_OPTIONS="${RUN_TEST_ASAN_OPTIONS}:"
1105fi
1106RUN_TEST_ASAN_OPTIONS="${RUN_TEST_ASAN_OPTIONS}detect_leaks=0"
1107
1108# For running, we must turn off logging when dex2oat is missing. Otherwise we use
1109# the same defaults as for prebuilt: everything when --dev, otherwise errors and above only.
1110if [ "$EXTERNAL_LOG_TAGS" = "n" ]; then
1111  if [ "$DEV_MODE" = "y" ]; then
1112      export ANDROID_LOG_TAGS='*:d'
1113  elif [ "$HAVE_IMAGE" = "n" ]; then
1114      # All tests would log the error of missing image. Be silent here and only log fatal
1115      # events.
1116      export ANDROID_LOG_TAGS='*:s'
1117  else
1118      # We are interested in LOG(ERROR) output.
1119      export ANDROID_LOG_TAGS='*:e'
1120  fi
1121fi
1122
1123if [ "$HOST" = "n" ]; then
1124    adb root > /dev/null
1125    adb wait-for-device
1126    if [ "$QUIET" = "n" ]; then
1127      adb shell rm -rf $CHROOT_DEX_LOCATION
1128      adb shell mkdir -p $CHROOT_DEX_LOCATION
1129      adb push $TEST_NAME.jar $CHROOT_DEX_LOCATION
1130      adb push $TEST_NAME-ex.jar $CHROOT_DEX_LOCATION
1131      if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
1132        adb push profile $CHROOT_DEX_LOCATION
1133      fi
1134      # Copy resource folder
1135      if [ -d res ]; then
1136        adb push res $CHROOT_DEX_LOCATION
1137      fi
1138    else
1139      adb shell rm -rf $CHROOT_DEX_LOCATION >/dev/null 2>&1
1140      adb shell mkdir -p $CHROOT_DEX_LOCATION >/dev/null 2>&1
1141      adb push $TEST_NAME.jar $CHROOT_DEX_LOCATION >/dev/null 2>&1
1142      adb push $TEST_NAME-ex.jar $CHROOT_DEX_LOCATION >/dev/null 2>&1
1143      if [ "$PROFILE" = "y" ] || [ "$RANDOM_PROFILE" = "y" ]; then
1144        adb push profile $CHROOT_DEX_LOCATION >/dev/null 2>&1
1145      fi
1146      # Copy resource folder
1147      if [ -d res ]; then
1148        adb push res $CHROOT_DEX_LOCATION >/dev/null 2>&1
1149      fi
1150    fi
1151
1152    # Populate LD_LIBRARY_PATH.
1153    LD_LIBRARY_PATH=
1154    if [ "$ANDROID_ROOT" != "/system" ]; then
1155      # Current default installation is dalvikvm 64bits and dex2oat 32bits,
1156      # so we can only use LD_LIBRARY_PATH when testing on a local
1157      # installation.
1158      LD_LIBRARY_PATH="$ANDROID_ROOT/$LIBRARY_DIRECTORY"
1159    fi
1160    # Needed to access libarttest(d).so and JVMTI agent libraries.
1161    LD_LIBRARY_PATH="/data/$TEST_DIRECTORY/art/$ISA:$LD_LIBRARY_PATH"
1162    # Needed to access the boot (core) image files.
1163    LD_LIBRARY_PATH="/apex/com.android.art/javalib/$ISA:$LD_LIBRARY_PATH"
1164    # Needed to access the test's Odex files.
1165    LD_LIBRARY_PATH="$DEX_LOCATION/oat/$ISA:$LD_LIBRARY_PATH"
1166    # Needed to access the test's native libraries (see e.g. 674-hiddenapi,
1167    # which generates `libhiddenapitest_*.so` libraries in `$DEX_LOCATION`).
1168    LD_LIBRARY_PATH="$DEX_LOCATION:$LD_LIBRARY_PATH"
1169
1170    # System libraries needed by libarttestd.so
1171    PUBLIC_LIBS=libc++.so:libbacktrace.so:libbase.so:libnativehelper.so
1172    if [ "$TEST_IS_NDEBUG" = "y" ]; then
1173      PUBLIC_LIBS=$PUBLIC_LIBS:libart.so:libdexfile.so:libprofile.so:libartbase.so
1174    else
1175      PUBLIC_LIBS=$PUBLIC_LIBS:libartd.so:libdexfiled.so:libprofiled.so:libartbased.so
1176    fi
1177
1178    # Prepend directories to the path on device.
1179    PREPEND_TARGET_PATH=$ANDROID_ART_BIN_DIR
1180    if [ "$ANDROID_ROOT" != "/system" ]; then
1181      PREPEND_TARGET_PATH="$PREPEND_TARGET_PATH:$ANDROID_ROOT/bin"
1182    fi
1183
1184    timeout_dumper_cmd=
1185
1186    # Check whether signal_dumper is available.
1187    if [ "$TIMEOUT_DUMPER" = signal_dumper ] ; then
1188      # Chroot? Use as prefix for tests.
1189      TIMEOUT_DUMPER_PATH_PREFIX=
1190      if [ -n "$CHROOT" ]; then
1191        TIMEOUT_DUMPER_PATH_PREFIX="$CHROOT/"
1192      fi
1193
1194      # Testing APEX?
1195      if adb shell "test -x ${TIMEOUT_DUMPER_PATH_PREFIX}/apex/com.android.art/bin/signal_dumper" ; then
1196        TIMEOUT_DUMPER="/apex/com.android.art/bin/signal_dumper"
1197      # Is it in /system/bin?
1198      elif adb shell "test -x ${TIMEOUT_DUMPER_PATH_PREFIX}/system/bin/signal_dumper" ; then
1199        TIMEOUT_DUMPER="/system/bin/signal_dumper"
1200      else
1201        TIMEOUT_DUMPER=
1202      fi
1203    else
1204      TIMEOUT_DUMPER=
1205    fi
1206
1207    if [ ! -z "$TIMEOUT_DUMPER" ] ; then
1208      # Use "-l" to dump to logcat. That is convenience for the build bot crash symbolization.
1209      # Use exit code 124 for toybox timeout (b/141007616).
1210      timeout_dumper_cmd="${TIMEOUT_DUMPER} -l -s 15 -e 124"
1211    fi
1212
1213    timeout_prefix=
1214    if [ "$TIME_OUT" = "timeout" ]; then
1215      # Add timeout command if time out is desired.
1216      #
1217      # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1218      #       will induce a full thread dump before killing the process. To ensure any issues in
1219      #       dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1220      #       child.
1221      # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
1222      timeout_prefix="timeout --foreground -k 120s ${TIME_OUT_VALUE}s ${timeout_dumper_cmd} $cmdline"
1223    fi
1224
1225    # Create a script with the command. The command can get longer than the longest
1226    # allowed adb command and there is no way to get the exit status from a adb shell
1227    # command. Dalvik cache is cleaned before running to make subsequent executions
1228    # of the script follow the same runtime path.
1229    cmdline="cd $DEX_LOCATION && \
1230             export ASAN_OPTIONS=$RUN_TEST_ASAN_OPTIONS && \
1231             export ANDROID_DATA=$DEX_LOCATION && \
1232             export ANDROID_ADDITIONAL_PUBLIC_LIBRARIES=$PUBLIC_LIBS && \
1233             export DEX_LOCATION=$DEX_LOCATION && \
1234             export ANDROID_ROOT=$ANDROID_ROOT && \
1235             export ANDROID_I18N_ROOT=$ANDROID_I18N_ROOT && \
1236             export ANDROID_ART_ROOT=$ANDROID_ART_ROOT && \
1237             export ANDROID_TZDATA_ROOT=$ANDROID_TZDATA_ROOT && \
1238             export ANDROID_LOG_TAGS=$ANDROID_LOG_TAGS && \
1239             rm -rf ${DEX_LOCATION}/dalvik-cache/ && \
1240             mkdir -p ${mkdir_locations} && \
1241             export LD_LIBRARY_PATH=$LD_LIBRARY_PATH && \
1242             export PATH=$PREPEND_TARGET_PATH:\$PATH && \
1243             $profman_cmdline && \
1244             $dex2oat_cmdline && \
1245             $dm_cmdline && \
1246             $vdex_cmdline && \
1247             $strip_cmdline && \
1248             $sync_cmdline && \
1249             $timeout_prefix $dalvikvm_cmdline"
1250
1251    cmdfile=$(tempfile -p "cmd-" -s "-$TEST_NAME")
1252    echo "$cmdline" > $cmdfile
1253
1254    if [ "$DEV_MODE" = "y" ]; then
1255      echo $cmdline
1256      if [ "$USE_GDB" = "y" ] || [ "$USE_GDBSERVER" = "y" ]; then
1257        echo "Forward ${GDBSERVER_PORT} to local port and connect GDB"
1258      fi
1259    fi
1260
1261    if [ "$QUIET" = "n" ]; then
1262      adb push $cmdfile $CHROOT_DEX_LOCATION/cmdline.sh
1263    else
1264      adb push $cmdfile $CHROOT_DEX_LOCATION/cmdline.sh >/dev/null 2>&1
1265    fi
1266
1267    exit_status=0
1268    if [ "$DRY_RUN" != "y" ]; then
1269      if [ -n "$CHROOT" ]; then
1270        adb shell chroot "$CHROOT" sh $DEX_LOCATION/cmdline.sh
1271      else
1272        adb shell sh $DEX_LOCATION/cmdline.sh
1273      fi
1274      exit_status=$?
1275    fi
1276
1277    rm -f $cmdfile
1278    exit $exit_status
1279else
1280    # Host run.
1281    export ANDROID_PRINTF_LOG=brief
1282
1283    export ANDROID_DATA="$DEX_LOCATION"
1284    export ANDROID_ROOT="${ANDROID_ROOT}"
1285    export ANDROID_I18N_ROOT="${ANDROID_I18N_ROOT}"
1286    export ANDROID_ART_ROOT="${ANDROID_ART_ROOT}"
1287    export ANDROID_TZDATA_ROOT="${ANDROID_TZDATA_ROOT}"
1288    if [ "$USE_ZIPAPEX" = "y" ] || [ "$USE_EXRACTED_ZIPAPEX" = "y" ]; then
1289      # Put the zipapex files in front of the ld-library-path
1290      export LD_LIBRARY_PATH="${ANDROID_DATA}/zipapex/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1291      export DYLD_LIBRARY_PATH="${ANDROID_DATA}/zipapex/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1292    else
1293      export LD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1294      export DYLD_LIBRARY_PATH="${ANDROID_ROOT}/${LIBRARY_DIRECTORY}:${ANDROID_ROOT}/${TEST_DIRECTORY}"
1295    fi
1296    export PATH="$PATH:$ANDROID_ART_BIN_DIR"
1297
1298    # Temporarily disable address space layout randomization (ASLR).
1299    # This is needed on the host so that the linker loads core.oat at the necessary address.
1300    export LD_USE_LOAD_BIAS=1
1301
1302    cmdline="$dalvikvm_cmdline"
1303
1304    if [ "$TIME_OUT" = "gdb" ]; then
1305      if [ `uname` = "Darwin" ]; then
1306        # Fall back to timeout on Mac.
1307        TIME_OUT="timeout"
1308      elif [ "$ISA" = "x86" ]; then
1309        # prctl call may fail in 32-bit on an older (3.2) 64-bit Linux kernel. Fall back to timeout.
1310        TIME_OUT="timeout"
1311      else
1312        # Check if gdb is available.
1313        gdb --eval-command="quit" > /dev/null 2>&1
1314        if [ $? != 0 ]; then
1315          # gdb isn't available. Fall back to timeout.
1316          TIME_OUT="timeout"
1317        fi
1318      fi
1319    fi
1320
1321    if [ "$TIME_OUT" = "timeout" ]; then
1322      # Add timeout command if time out is desired.
1323      #
1324      # Note: We first send SIGTERM (the timeout default, signal 15) to the signal dumper, which
1325      #       will induce a full thread dump before killing the process. To ensure any issues in
1326      #       dumping do not lead to a deadlock, we also use the "-k" option to definitely kill the
1327      #       child.
1328      # Note: Using "--foreground" to not propagate the signal to children, i.e., the runtime.
1329      cmdline="timeout --foreground -k 120s ${TIME_OUT_VALUE}s ${TIMEOUT_DUMPER} -s 15 $cmdline"
1330    fi
1331
1332    if [ "$DEV_MODE" = "y" ]; then
1333      for var in ANDROID_PRINTF_LOG ANDROID_DATA ANDROID_ROOT ANDROID_I18N_ROOT ANDROID_TZDATA_ROOT ANDROID_ART_ROOT LD_LIBRARY_PATH DYLD_LIBRARY_PATH PATH LD_USE_LOAD_BIAS; do
1334        echo EXPORT $var=${!var}
1335      done
1336      echo "$(declare -f linkdirs)"
1337      echo "mkdir -p ${mkdir_locations} && $setupapex_cmdline && $installapex_cmdline && $linkroot_cmdline && $linkroot_overlay_cmdline && $profman_cmdline && $dex2oat_cmdline && $dm_cmdline && $vdex_cmdline && $strip_cmdline && $sync_cmdline && $cmdline"
1338    fi
1339
1340    cd $ANDROID_BUILD_TOP
1341
1342    # Make sure we delete any existing compiler artifacts.
1343    # This enables tests to call the RUN script multiple times in a row
1344    # without worrying about interference.
1345    rm -rf ${DEX_LOCATION}/oat
1346    rm -rf ${DEX_LOCATION}/dalvik-cache/
1347
1348    export ASAN_OPTIONS=$RUN_TEST_ASAN_OPTIONS
1349
1350    mkdir -p ${mkdir_locations} || exit 1
1351    $setupapex_cmdline || { echo "zipapex extraction failed." >&2 ; exit 2; }
1352    $installapex_cmdline || { echo "zipapex install failed. cmd was: ${installapex_cmdline}." >&2; find ${mkdir_locations} -type f >&2; exit 2; }
1353    $linkroot_cmdline || { echo "create symlink android-root failed." >&2 ; exit 2; }
1354    $linkroot_overlay_cmdline || { echo "overlay android-root failed." >&2 ; exit 2; }
1355    $profman_cmdline || { echo "Profman failed." >&2 ; exit 2; }
1356    eval "$dex2oat_cmdline" || { echo "Dex2oat failed." >&2 ; exit 2; }
1357    eval "$dm_cmdline" || { echo "Dex2oat failed." >&2 ; exit 2; }
1358    eval "$vdex_cmdline" || { echo "Dex2oat failed." >&2 ; exit 2; }
1359    $strip_cmdline || { echo "Strip failed." >&2 ; exit 3; }
1360    $sync_cmdline || { echo "Sync failed." >&2 ; exit 4; }
1361
1362    if [ "$CREATE_RUNNER" = "y" ]; then
1363      echo "#!/bin/bash" > ${DEX_LOCATION}/runit.sh
1364      for var in ANDROID_PRINTF_LOG ANDROID_DATA ANDROID_ROOT ANDROID_I18N_ROOT ANDROID_TZDATA_ROOT ANDROID_ART_ROOT LD_LIBRARY_PATH DYLD_LIBRARY_PATH PATH LD_USE_LOAD_BIAS; do
1365        echo export $var="${!var}" >> ${DEX_LOCATION}/runit.sh
1366      done
1367      if [ "$DEV_MODE" = "y" ]; then
1368        echo $cmdline "2>&1" >> ${DEX_LOCATION}/runit.sh
1369      else
1370        echo 'STDERR=$(mktemp)' >> ${DEX_LOCATION}/runit.sh
1371        echo 'STDOUT=$(mktemp)' >> ${DEX_LOCATION}/runit.sh
1372        echo $cmdline '2>${STDERR} >${STDOUT}' >> ${DEX_LOCATION}/runit.sh
1373        echo 'if diff ${STDOUT} $ANDROID_DATA/expected.txt; then' >> ${DEX_LOCATION}/runit.sh
1374        echo '  rm -f ${STDOUT} ${STDERR}' >> ${DEX_LOCATION}/runit.sh
1375        echo '  exit 0' >> ${DEX_LOCATION}/runit.sh
1376        echo 'else' >> ${DEX_LOCATION}/runit.sh
1377        echo '  echo  STDOUT:' >> ${DEX_LOCATION}/runit.sh
1378        echo '  cat ${STDOUT}' >> ${DEX_LOCATION}/runit.sh
1379        echo '  echo  STDERR:' >> ${DEX_LOCATION}/runit.sh
1380        echo '  cat ${STDERR}' >> ${DEX_LOCATION}/runit.sh
1381        echo '  rm -f ${STDOUT} ${STDERR}' >> ${DEX_LOCATION}/runit.sh
1382        echo '  exit 1' >> ${DEX_LOCATION}/runit.sh
1383        echo 'fi' >> ${DEX_LOCATION}/runit.sh
1384      fi
1385      chmod u+x $DEX_LOCATION/runit.sh
1386      echo "Runnable test script written to ${DEX_LOCATION}/runit.sh"
1387    fi
1388    if [ "$DRY_RUN" = "y" ]; then
1389      exit 0
1390    fi
1391
1392    if [ "$USE_GDB" = "y" ]; then
1393      # When running under gdb, we cannot do piping and grepping...
1394      $cmdline "$@"
1395    elif [ "$USE_GDBSERVER" = "y" ]; then
1396      echo "Connect to $GDBSERVER_PORT"
1397      # When running under gdb, we cannot do piping and grepping...
1398      $cmdline "$@"
1399    else
1400      if [ "$TIME_OUT" != "gdb" ]; then
1401        trap 'kill -INT -$pid' INT
1402        $cmdline "$@" 2>&1 & pid=$!
1403        wait $pid
1404        exit_value=$?
1405        # Add extra detail if time out is enabled.
1406        if [ $exit_value = 124 ] && [ "$TIME_OUT" = "timeout" ]; then
1407          echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
1408        fi
1409        exit $exit_value
1410      else
1411        # With a thread dump that uses gdb if a timeout.
1412        trap 'kill -INT -$pid' INT
1413        $cmdline "$@" 2>&1 & pid=$!
1414        # Spawn a watcher process.
1415        ( sleep $TIME_OUT_VALUE && \
1416          echo "##### Thread dump using gdb on test timeout" && \
1417          ( gdb -q -p $pid --eval-command="info thread" --eval-command="thread apply all bt" \
1418                           --eval-command="call exit(124)" --eval-command=quit || \
1419            kill $pid )) 2> /dev/null & watcher=$!
1420        wait $pid
1421        test_exit_status=$?
1422        pkill -P $watcher 2> /dev/null # kill the sleep which will in turn end the watcher as well
1423        if [ $test_exit_status = 0 ]; then
1424          # The test finished normally.
1425          exit 0
1426        else
1427          # The test failed or timed out.
1428          if [ $test_exit_status = 124 ]; then
1429            # The test timed out.
1430            echo -e "\e[91mTEST TIMED OUT!\e[0m" >&2
1431          fi
1432          exit $test_exit_status
1433        fi
1434      fi
1435    fi
1436fi
1437