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