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