run-test revision 8a4064e2c0987637b11afcc656478d3113d81618
1#!/bin/bash
2#
3# Copyright (C) 2007 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Set up prog to be the path of this script, including following symlinks,
18# and set up progdir to be the fully-qualified pathname of its directory.
19prog="$0"
20while [ -h "${prog}" ]; do
21    newProg=`/bin/ls -ld "${prog}"`
22    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
23    if expr "x${newProg}" : 'x/' >/dev/null; then
24        prog="${newProg}"
25    else
26        progdir=`dirname "${prog}"`
27        prog="${progdir}/${newProg}"
28    fi
29done
30oldwd=`pwd`
31progdir=`dirname "${prog}"`
32cd "${progdir}"
33progdir=`pwd`
34prog="${progdir}"/`basename "${prog}"`
35test_dir="test-$$"
36if [ -z "$TMPDIR" ]; then
37  tmp_dir="/tmp/$USER/${test_dir}"
38else
39  tmp_dir="${TMPDIR}/$USER/${test_dir}"
40fi
41
42export JAVA="java"
43export JAVAC="javac -g"
44export RUN="${progdir}/etc/push-and-run-test-jar"
45export DEX_LOCATION=/data/run-test/${test_dir}
46export NEED_DEX="true"
47
48# If dx was not set by the environment variable, assume it is in the path.
49if [ -z "$DX" ]; then
50  export DX="dx"
51fi
52
53# If jasmin was not set by the environment variable, assume it is in the path.
54if [ -z "$JASMIN" ]; then
55  export JASMIN="jasmin"
56fi
57
58
59info="info.txt"
60build="build"
61run="run"
62expected="expected.txt"
63check_cmd="check"
64output="output.txt"
65build_output="build-output.txt"
66lib="libartd.so"
67run_args="--quiet"
68
69prebuild_mode="yes"
70target_mode="yes"
71dev_mode="no"
72update_mode="no"
73debug_mode="no"
74relocate="yes"
75runtime="art"
76usage="no"
77build_only="no"
78suffix64=""
79trace="false"
80always_clean="no"
81
82while true; do
83    if [ "x$1" = "x--host" ]; then
84        target_mode="no"
85        DEX_LOCATION=$tmp_dir
86        shift
87    elif [ "x$1" = "x--jvm" ]; then
88        target_mode="no"
89        runtime="jvm"
90        NEED_DEX="false"
91        shift
92    elif [ "x$1" = "x-O" ]; then
93        lib="libart.so"
94        shift
95    elif [ "x$1" = "x--dalvik" ]; then
96        lib="libdvm.so"
97        runtime="dalvik"
98        shift
99    elif [ "x$1" = "x--relocate" ]; then
100        relocate="yes"
101        shift
102    elif [ "x$1" = "x--no-relocate" ]; then
103        relocate="no"
104        shift
105    elif [ "x$1" = "x--prebuild" ]; then
106        prebuild_mode="yes"
107        shift;
108    elif [ "x$1" = "x--no-prebuild" ]; then
109        prebuild_mode="no"
110        shift;
111    elif [ "x$1" = "x--image" ]; then
112        shift
113        image="$1"
114        run_args="${run_args} --image $image"
115        shift
116    elif [ "x$1" = "x-Xcompiler-option" ]; then
117        shift
118        option="$1"
119        run_args="${run_args} -Xcompiler-option $option"
120        shift
121    elif [ "x$1" = "x--runtime-option" ]; then
122        shift
123        option="$1"
124        run_args="${run_args} --runtime-option $option"
125        shift
126    elif [ "x$1" = "x--debug" ]; then
127        run_args="${run_args} --debug"
128        shift
129    elif [ "x$1" = "x--gdb" ]; then
130        run_args="${run_args} --gdb"
131        dev_mode="yes"
132        shift
133    elif [ "x$1" = "x--zygote" ]; then
134        run_args="${run_args} --zygote"
135        shift
136    elif [ "x$1" = "x--interpreter" ]; then
137        run_args="${run_args} --interpreter"
138        shift
139    elif [ "x$1" = "x--no-verify" ]; then
140        run_args="${run_args} --no-verify"
141        shift
142    elif [ "x$1" = "x--no-optimize" ]; then
143        run_args="${run_args} --no-optimize"
144        shift
145    elif [ "x$1" = "x--no-precise" ]; then
146        run_args="${run_args} --no-precise"
147        shift
148    elif [ "x$1" = "x--invoke-with" ]; then
149        shift
150        what="$1"
151        if [ "x$what" = "x" ]; then
152            echo "$0 missing argument to --invoke-with" 1>&2
153            usage="yes"
154            break
155        fi
156        run_args="${run_args} --invoke-with ${what}"
157        shift
158    elif [ "x$1" = "x--dev" ]; then
159        run_args="${run_args} --dev"
160        dev_mode="yes"
161        shift
162    elif [ "x$1" = "x--build-only" ]; then
163        build_only="yes"
164        shift
165    elif [ "x$1" = "x--output-path" ]; then
166        shift
167        tmp_dir=$1
168        if [ "x$tmp_dir" = "x" ]; then
169            echo "$0 missing argument to --output-path" 1>&2
170            usage="yes"
171            break
172        fi
173        shift
174    elif [ "x$1" = "x--update" ]; then
175        update_mode="yes"
176        shift
177    elif [ "x$1" = "x--help" ]; then
178        usage="yes"
179        shift
180    elif [ "x$1" = "x--64" ]; then
181        run_args="${run_args} --64"
182        suffix64="64"
183        shift
184    elif [ "x$1" = "x--trace" ]; then
185        trace="true"
186        shift
187    elif [ "x$1" = "x--always-clean" ]; then
188        always_clean="yes"
189        shift
190    elif expr "x$1" : "x--" >/dev/null 2>&1; then
191        echo "unknown $0 option: $1" 1>&2
192        usage="yes"
193        break
194    else
195        break
196    fi
197done
198
199# tmp_dir may be relative, resolve.
200#
201# Cannot use realpath, as it does not exist on Mac.
202# Cannot us a simple "cd", as the path might not be created yet.
203# Cannot use readlink -m, as it does not exist on Mac.
204# Fallback to nuclear option:
205tmp_dir="`cd $oldwd ; python -c "import os; print os.path.realpath('$tmp_dir')"`"
206mkdir -p $tmp_dir
207
208if [ "$trace" = "true" ]; then
209    run_args="${run_args} --runtime-option -Xmethod-trace --runtime-option -Xmethod-trace-file:${DEX_LOCATION}/trace.bin --runtime-option -Xmethod-trace-file-size:2000000"
210fi
211
212# Most interesting target architecture variables are Makefile variables, not environment variables.
213# Try to map the suffix64 flag and what we find in ${ANDROID_PRODUCT_OUT}/data/art-test to an architecture name.
214function guess_arch_name() {
215    grep32bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm|x86|mips)$'`
216    grep64bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm64|x86_64)$'`
217    if [ "x${suffix64}" = "x64" ]; then
218        target_arch_name=${grep64bit}
219    else
220        target_arch_name=${grep32bit}
221    fi
222}
223
224if [ "$target_mode" = "no" ]; then
225    if [ "$runtime" = "jvm" ]; then
226        RUN="${progdir}/etc/reference-run-test-classes"
227        if [ "$prebuild_mode" = "yes" ]; then
228            echo "--prebuild with --jvm is unsupported";
229            exit 1;
230        fi
231    else
232        RUN="${progdir}/etc/host-run-test-jar"
233        if [ "$prebuild_mode" = "yes" ]; then
234            run_args="${run_args} --prebuild"
235        fi
236    fi
237else
238    if [ "$prebuild_mode" = "yes" ]; then
239        RUN="${progdir}/etc/push-and-run-prebuilt-test-jar"
240    fi
241fi
242
243if [ ! "$runtime" = "jvm" ]; then
244  run_args="${run_args} --lib $lib"
245fi
246
247if [ "$runtime" = "dalvik" ]; then
248    if [ "$target_mode" = "no" ]; then
249        framework="${ANDROID_PRODUCT_OUT}/system/framework"
250        bpath="${framework}/core.jar:${framework}/conscrypt.jar:${framework}/okhttp.jar:${framework}/core-junit.jar:${framework}/bouncycastle.jar:${framework}/ext.jar"
251        run_args="${run_args} --boot -Xbootclasspath:${bpath}"
252    else
253        true # defaults to using target BOOTCLASSPATH
254    fi
255elif [ "$runtime" = "art" ]; then
256    if [ "$target_mode" = "no" ]; then
257	# ANDROID_BUILD_TOP and ANDROID_HOST_OUT are not set in a build environment.
258        if [ -z "$ANDROID_BUILD_TOP" ]; then
259	    export ANDROID_BUILD_TOP=$oldwd
260        fi
261        if [ -z "$ANDROID_HOST_OUT" ]; then
262	    export ANDROID_HOST_OUT=$ANDROID_BUILD_TOP/out/host/linux-x86
263        fi
264        run_args="${run_args} --boot -Ximage:${ANDROID_HOST_OUT}/framework/core.art"
265        run_args="${run_args} --runtime-option -Djava.library.path=${ANDROID_HOST_OUT}/lib${suffix64}"
266    else
267        guess_arch_name
268        run_args="${run_args} --runtime-option -Djava.library.path=/data/art-test/${target_arch_name}"
269        run_args="${run_args} --boot -Ximage:/data/art-test/core.art"
270    fi
271    if [ "$relocate" = "yes" ]; then
272      run_args="${run_args} --relocate"
273    else
274      run_args="${run_args} --no-relocate"
275    fi
276fi
277
278if [ "$dev_mode" = "yes" -a "$update_mode" = "yes" ]; then
279    echo "--dev and --update are mutually exclusive" 1>&2
280    usage="yes"
281fi
282
283if [ "$usage" = "no" ]; then
284    if [ "x$1" = "x" -o "x$1" = "x-" ]; then
285        test_dir=`basename "$oldwd"`
286    else
287        test_dir="$1"
288    fi
289
290    if [ '!' -d "$test_dir" ]; then
291        td2=`echo ${test_dir}-*`
292        if [ '!' -d "$td2" ]; then
293            echo "${test_dir}: no such test directory" 1>&2
294            usage="yes"
295        fi
296        test_dir="$td2"
297    fi
298    # Shift to get rid of the test name argument. The rest of the arguments
299    # will get passed to the test run.
300    shift
301fi
302
303if [ "$usage" = "yes" ]; then
304    prog=`basename $prog`
305    (
306        echo "usage:"
307        echo "  $prog --help                          Print this message."
308        echo "  $prog [options] [test-name]           Run test normally."
309        echo "  $prog --dev [options] [test-name]     Development mode" \
310             "(dumps to stdout)."
311        echo "  $prog --update [options] [test-name]  Update mode" \
312             "(replaces expected.txt)."
313        echo '  Omitting the test name or specifying "-" will use the' \
314             "current directory."
315        echo "  Runtime Options:"
316        echo "    -O                   Run non-debug rather than debug build (off by default)."
317        echo "    -Xcompiler-option    Pass an option to the compiler."
318        echo "    --runtime-option     Pass an option to the runtime."
319        echo "    --debug              Wait for a debugger to attach."
320        echo "    --gdb                Run under gdb; incompatible with some tests."
321        echo "    --build-only         Build test files only (off by default)."
322        echo "    --interpreter        Enable interpreter only mode (off by default)."
323        echo "    --no-verify          Turn off verification (on by default)."
324        echo "    --no-optimize        Turn off optimization (on by default)."
325        echo "    --no-precise         Turn off precise GC (on by default)."
326        echo "    --zygote             Spawn the process from the Zygote." \
327             "If used, then the"
328        echo "                         other runtime options are ignored."
329        echo "    --prebuild           Run dex2oat on the files before starting test. (default)"
330        echo "    --no-prebuild        Do not run dex2oat on the files before starting"
331        echo "                         the test."
332        echo "    --relocate           Force the use of relocating in the test, making"
333        echo "                         the image and oat files be relocated to a random"
334        echo "                         address before running. (default)"
335        echo "    --no-relocate        Force the use of no relocating in the test"
336        echo "    --host               Use the host-mode virtual machine."
337        echo "    --invoke-with        Pass --invoke-with option to runtime."
338        echo "    --dalvik             Use Dalvik (off by default)."
339        echo "    --jvm                Use a host-local RI virtual machine."
340        echo "    --output-path [path] Location where to store the build" \
341             "files."
342        echo "    --64                 Run the test in 64-bit mode"
343        echo "    --trace              Run with method tracing"
344        echo "    --always-clean       Delete the test files even if the test fails."
345    ) 1>&2
346    exit 1
347fi
348
349cd "$test_dir"
350test_dir=`pwd`
351
352td_info="${test_dir}/${info}"
353td_expected="${test_dir}/${expected}"
354
355if [ ! -r $td_info ]; then
356    echo "${test_dir}: missing file $td_info" 1>&2
357    exit 1
358fi
359
360if [ ! -r $td_expected ]; then
361    echo "${test_dir}: missing file $td_expected" 1>&2
362    exit 1
363fi
364
365# copy the test to a temp dir and run it
366
367echo "${test_dir}: building..." 1>&2
368
369rm -rf "$tmp_dir"
370cp -Rp "$test_dir" "$tmp_dir"
371cd "$tmp_dir"
372
373if [ '!' -r "$build" ]; then
374    cp "${progdir}/etc/default-build" build
375fi
376
377if [ '!' -r "$run" ]; then
378    cp "${progdir}/etc/default-run" run
379fi
380
381if [ '!' -r "$check_cmd" ]; then
382    cp "${progdir}/etc/default-check" check
383fi
384
385chmod 755 "$build"
386chmod 755 "$run"
387chmod 755 "$check_cmd"
388
389export TEST_NAME=`basename ${test_dir}`
390
391# To cause tests to fail fast, limit the file sizes created by dx, dex2oat and ART output to 2MB.
392file_size_limit=2048
393if echo "$test_dir" | grep 089; then
394  file_size_limit=5120
395elif echo "$test_dir" | grep 083; then
396  file_size_limit=5120
397elif echo "$test_dir" | grep 115; then
398# Native bridge test copies libarttest.so into its directory, which needs 2MB already.
399  file_size_limit=5120
400fi
401if ! ulimit -S "$file_size_limit"; then
402   echo "ulimit file size setting failed"
403fi
404
405good="no"
406if [ "$dev_mode" = "yes" ]; then
407    "./${build}" 2>&1
408    build_exit="$?"
409    echo "build exit status: $build_exit" 1>&2
410    if [ "$build_exit" = '0' ]; then
411        echo "${test_dir}: running..." 1>&2
412        "./${run}" $run_args "$@" 2>&1
413        run_exit="$?"
414        echo "run exit status: $run_exit" 1>&2
415        if [ "$run_exit" = "0" ]; then
416            good="yes"
417        fi
418    fi
419elif [ "$update_mode" = "yes" ]; then
420    "./${build}" >"$build_output" 2>&1
421    build_exit="$?"
422    if [ "$build_exit" = '0' ]; then
423        echo "${test_dir}: running..." 1>&2
424        "./${run}" $run_args "$@" >"$output" 2>&1
425        sed -e 's/[[:cntrl:]]$//g' < "$output" >"${td_expected}"
426        good="yes"
427    else
428        cat "$build_output" 1>&2
429        echo "build exit status: $build_exit" 1>&2
430    fi
431elif [ "$build_only" = "yes" ]; then
432    good="yes"
433    "./${build}" >"$build_output" 2>&1
434    build_exit="$?"
435    if [ "$build_exit" '!=' '0' ]; then
436        cp "$build_output" "$output"
437        echo "build exit status: $build_exit" >>"$output"
438        diff --strip-trailing-cr -q "$expected" "$output" >/dev/null
439        if [ "$?" '!=' "0" ]; then
440            good="no"
441            echo "BUILD FAILED For ${TEST_NAME}"
442        fi
443    fi
444    # Clean up extraneous files that are not used by tests.
445    find $tmp_dir -mindepth 1  ! -regex ".*/\(.*jar\|$output\|$expected\)" | xargs rm -rf
446    exit 0
447else
448    "./${build}" >"$build_output" 2>&1
449    build_exit="$?"
450    if [ "$build_exit" = '0' ]; then
451        echo "${test_dir}: running..." 1>&2
452        "./${run}" $run_args "$@" >"$output" 2>&1
453    else
454        cp "$build_output" "$output"
455        echo "build exit status: $build_exit" >>"$output"
456    fi
457    ./$check_cmd "$expected" "$output"
458    if [ "$?" = "0" ]; then
459        # output == expected
460        good="yes"
461        echo "${test_dir}: succeeded!" 1>&2
462    fi
463fi
464
465(
466    if [ "$good" != "yes" -a "$update_mode" != "yes" ]; then
467        echo "${test_dir}: FAILED!"
468        echo ' '
469        echo '#################### info'
470        cat "${td_info}" | sed 's/^/# /g'
471        echo '#################### diffs'
472        diff --strip-trailing-cr -u "$expected" "$output" | tail -n 500
473        echo '####################'
474        echo ' '
475    fi
476
477) 1>&2
478
479# Clean up test files.
480if [ "$always_clean" = "yes" -o "$good" = "yes" ]; then
481    cd "$oldwd"
482    rm -rf "$tmp_dir"
483    if [ "$target_mode" = "yes" -a "$build_exit" = "0" ]; then
484        adb shell rm -rf $DEX_LOCATION
485    fi
486    if [ "$good" = "yes" ]; then
487        exit 0
488    fi
489fi
490
491
492(
493    if [ "$always_clean" = "yes" ]; then
494        echo "${TEST_NAME} files deleted from host "
495        if [ "$target_mode" == "yes" ]; then
496            echo "and from target"
497        fi
498    else
499        echo "${TEST_NAME} files left in ${tmp_dir} on host"
500        if [ "$target_mode" == "yes" ]; then
501            echo "and in ${DEX_LOCATION} on target"
502        fi
503    fi
504
505) 1>&2
506
507exit 1
508