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