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