Provide an option to run run-tests with a local dalvikvm.

Instead of overwriting /system, use a temporary directory
to push the binaries and libraries, and use them for
running the tests.

Change-Id: I45fa2ac51f836686d8e8676907eb26629f7ab2ab
diff --git a/Android.mk b/Android.mk
index 22bf305..d11d011 100644
--- a/Android.mk
+++ b/Android.mk
@@ -137,10 +137,17 @@
 
 # Sync test files to the target, depends upon all things that must be pushed to the target.
 .PHONY: test-art-target-sync
+ifeq ($(ART_TEST_ANDROID_ROOT),)
 test-art-target-sync: $(TEST_ART_TARGET_SYNC_DEPS)
 	adb root
 	adb wait-for-device remount
 	adb sync
+else
+test-art-target-sync: $(TEST_ART_TARGET_SYNC_DEPS)
+	adb root
+	adb wait-for-device push $(ANDROID_PRODUCT_OUT)/system $(ART_TEST_ANDROID_ROOT)
+	adb push $(ANDROID_PRODUCT_OUT)/data /data
+endif
 
 # Undefine variable now its served its purpose.
 TEST_ART_TARGET_SYNC_DEPS :=
diff --git a/test/Android.run-test.mk b/test/Android.run-test.mk
index 2bff720..854de57 100644
--- a/test/Android.run-test.mk
+++ b/test/Android.run-test.mk
@@ -588,6 +588,9 @@
   run_test_rule_name := test-art-$(1)-run-test-$(2)-$(3)-$(4)-$(5)-$(6)-$(7)-$(8)-$(9)-$(10)-$(11)$(12)
   run_test_options := --output-path $(ART_HOST_TEST_DIR)/run-test-output/$$(run_test_rule_name) \
       $$(run_test_options)
+  ifneq ($(ART_TEST_ANDROID_ROOT),)
+    run_test_options := --android-root $(ART_TEST_ANDROID_ROOT) $$(run_test_options)
+  endif
 $$(run_test_rule_name): PRIVATE_RUN_TEST_OPTIONS := $$(run_test_options)
 .PHONY: $$(run_test_rule_name)
 $$(run_test_rule_name): $(DX) $(HOST_OUT_EXECUTABLES)/jasmin $(HOST_OUT_EXECUTABLES)/smali $(HOST_OUT_EXECUTABLES)/dexmerger $$(prereq_rule)
diff --git a/test/etc/run-test-jar b/test/etc/run-test-jar
index eec8930..bf79006 100755
--- a/test/etc/run-test-jar
+++ b/test/etc/run-test-jar
@@ -8,9 +8,11 @@
     fi
 }
 
+ANDROID_ROOT="/system"
 ARCHITECTURES_32="(arm|x86|mips|none)"
 ARCHITECTURES_64="(arm64|x86_64|none)"
 ARCHITECTURES_PATTERN="${ARCHITECTURES_32}"
+BOOT_IMAGE=""
 COMPILE_FLAGS=""
 DALVIKVM="dalvikvm32"
 DEBUGGER="n"
@@ -26,6 +28,8 @@
 INTERPRETER="n"
 INVOKE_WITH=""
 ISA=x86
+LIBRARY_DIRECTORY="lib"
+MAIN=""
 OPTIMIZE="y"
 PATCHOAT=""
 PREBUILD="y"
@@ -35,7 +39,6 @@
 USE_JVM="n"
 VERIFY="y"
 ZYGOTE=""
-MAIN=""
 
 while true; do
     if [ "x$1" = "x--quiet" ]; then
@@ -62,8 +65,7 @@
         shift
     elif [ "x$1" = "x--boot" ]; then
         shift
-        DALVIKVM_BOOT_OPT="$1"
-        DEX2OAT_BOOT_OPT="--boot-image=${1#-Ximage:}"
+        BOOT_IMAGE="$1"
         shift
     elif [ "x$1" = "x--no-dex2oat" ]; then
         DEX2OAT="-Xcompiler:${FALSE_BIN}"
@@ -127,6 +129,10 @@
     elif [ "x$1" = "x--no-optimize" ]; then
         OPTIMIZE="n"
         shift
+    elif [ "x$1" = "x--android-root" ]; then
+        shift
+        ANDROID_ROOT="$1"
+        shift
     elif [ "x$1" = "x--" ]; then
         shift
         break
@@ -134,6 +140,7 @@
         ISA="x86_64"
         GDB_SERVER="gdbserver64"
         DALVIKVM="dalvikvm64"
+        LIBRARY_DIRECTORY="lib64"
         ARCHITECTURES_PATTERN="${ARCHITECTURES_64}"
         shift
     elif [ "x$1" = "x--pic-test" ]; then
@@ -201,7 +208,11 @@
 
 
 if [ "$HAVE_IMAGE" = "n" ]; then
-    BOOT_OPT="-Ximage:/system/non-existant/core.art"
+    DALVIKVM_BOOT_OPT="-Ximage:/system/non-existant/core.art"
+    DEX2OAT_BOOT_OPT="--boot-image=/system/non-existant/core.art"
+else
+    DALVIKVM_BOOT_OPT="-Ximage:${BOOT_IMAGE}"
+    DEX2OAT_BOOT_OPT="--boot-image=${BOOT_IMAGE}"
 fi
 
 
@@ -258,7 +269,7 @@
 mkdir_cmdline="mkdir -p ${DEX_LOCATION}/dalvik-cache/$ISA"
 
 if [ "$PREBUILD" = "y" ]; then
-  dex2oat_cmdline="$INVOKE_WITH dex2oatd \
+  dex2oat_cmdline="$INVOKE_WITH $ANDROID_ROOT/bin/dex2oatd \
                       $COMPILE_FLAGS \
                       $DEX2OAT_BOOT_OPT \
                       --dex-file=$DEX_LOCATION/$TEST_NAME.jar \
@@ -266,7 +277,7 @@
                       --instruction-set=$ISA"
 fi
 
-dalvikvm_cmdline="$INVOKE_WITH $GDB $DALVIKVM \
+dalvikvm_cmdline="$INVOKE_WITH $GDB $ANDROID_ROOT/bin/$DALVIKVM \
                   $GDB_ARGS \
                   $FLAGS \
                   -XXlib:$LIB \
@@ -301,6 +312,8 @@
     cmdline="cd $DEX_LOCATION && \
              export ANDROID_DATA=$DEX_LOCATION && \
              export DEX_LOCATION=$DEX_LOCATION && \
+             export ANDROID_ROOT=$ANDROID_ROOT && \
+             export LD_LIBRARY_PATH=$ANDROID_ROOT/$LIBRARY_DIRECTORY && \
              $mkdir_cmdline && \
              $dex2oat_cmdline && \
              $dalvikvm_cmdline"
diff --git a/test/run-test b/test/run-test
index b0a4bb1..b43668d 100755
--- a/test/run-test
+++ b/test/run-test
@@ -97,6 +97,7 @@
 have_image="yes"
 image_suffix=""
 pic_image_suffix=""
+android_root="/system"
 
 while true; do
     if [ "x$1" = "x--host" ]; then
@@ -223,6 +224,16 @@
             break
         fi
         shift
+    elif [ "x$1" = "x--android-root" ]; then
+        shift
+        if [ "x$1" = "x" ]; then
+            echo "$0 missing argument to --android-root" 1>&2
+            usage="yes"
+            break
+        fi
+        android_root="$1"
+        run_args="${run_args} --android-root $1"
+        shift
     elif [ "x$1" = "x--update" ]; then
         update_mode="yes"
         shift
@@ -321,12 +332,12 @@
         if [ -z "$ANDROID_HOST_OUT" ]; then
             export ANDROID_HOST_OUT=$ANDROID_BUILD_TOP/out/host/linux-x86
         fi
-        run_args="${run_args} --boot -Ximage:${ANDROID_HOST_OUT}/framework/core${image_suffix}${pic_image_suffix}.art"
+        run_args="${run_args} --boot ${ANDROID_HOST_OUT}/framework/core${image_suffix}${pic_image_suffix}.art"
         run_args="${run_args} --runtime-option -Djava.library.path=${ANDROID_HOST_OUT}/lib${suffix64}"
     else
         guess_arch_name
         run_args="${run_args} --runtime-option -Djava.library.path=/data/art-test/${target_arch_name}"
-        run_args="${run_args} --boot -Ximage:/data/art-test/core${image_suffix}${pic_image_suffix}.art"
+        run_args="${run_args} --boot /data/art-test/core${image_suffix}${pic_image_suffix}.art"
     fi
     if [ "$relocate" = "yes" ]; then
       run_args="${run_args} --relocate"
@@ -344,7 +355,7 @@
         framework="${ANDROID_HOST_OUT}/framework"
         bpath_suffix="-hostdex"
     else
-        framework="/system/framework"
+        framework="${android_root}/framework"
         bpath_suffix=""
     fi
     # TODO If the target was compiled WITH_DEXPREOPT=true then these tests will
@@ -397,40 +408,41 @@
         echo '  Omitting the test name or specifying "-" will use the' \
              "current directory."
         echo "  Runtime Options:"
-        echo "    -O                   Run non-debug rather than debug build (off by default)."
-        echo "    -Xcompiler-option    Pass an option to the compiler."
-        echo "    --runtime-option     Pass an option to the runtime."
-        echo "    --debug              Wait for a debugger to attach."
-        echo "    --gdb                Run under gdb; incompatible with some tests."
-        echo "    --build-only         Build test files only (off by default)."
-        echo "    --interpreter        Enable interpreter only mode (off by default)."
-        echo "    --optimizing         Enable optimizing compiler (off by default)."
-        echo "    --no-verify          Turn off verification (on by default)."
-        echo "    --no-optimize        Turn off optimization (on by default)."
-        echo "    --no-precise         Turn off precise GC (on by default)."
-        echo "    --zygote             Spawn the process from the Zygote." \
+        echo "    -O                    Run non-debug rather than debug build (off by default)."
+        echo "    -Xcompiler-option     Pass an option to the compiler."
+        echo "    --runtime-option      Pass an option to the runtime."
+        echo "    --debug               Wait for a debugger to attach."
+        echo "    --gdb                 Run under gdb; incompatible with some tests."
+        echo "    --build-only          Build test files only (off by default)."
+        echo "    --interpreter         Enable interpreter only mode (off by default)."
+        echo "    --optimizing          Enable optimizing compiler (off by default)."
+        echo "    --no-verify           Turn off verification (on by default)."
+        echo "    --no-optimize         Turn off optimization (on by default)."
+        echo "    --no-precise          Turn off precise GC (on by default)."
+        echo "    --zygote              Spawn the process from the Zygote." \
              "If used, then the"
-        echo "                         other runtime options are ignored."
-        echo "    --no-dex2oat         Run as though dex2oat was failing."
-        echo "    --no-patchoat        Run as though patchoat was failing."
-        echo "    --prebuild           Run dex2oat on the files before starting test. (default)"
-        echo "    --no-prebuild        Do not run dex2oat on the files before starting"
-        echo "                         the test."
-        echo "    --relocate           Force the use of relocating in the test, making"
-        echo "                         the image and oat files be relocated to a random"
-        echo "                         address before running. (default)"
-        echo "    --no-relocate        Force the use of no relocating in the test"
-        echo "    --host               Use the host-mode virtual machine."
-        echo "    --invoke-with        Pass --invoke-with option to runtime."
-        echo "    --dalvik             Use Dalvik (off by default)."
-        echo "    --jvm                Use a host-local RI virtual machine."
-        echo "    --output-path [path] Location where to store the build" \
+        echo "                          other runtime options are ignored."
+        echo "    --no-dex2oat          Run as though dex2oat was failing."
+        echo "    --no-patchoat         Run as though patchoat was failing."
+        echo "    --prebuild            Run dex2oat on the files before starting test. (default)"
+        echo "    --no-prebuild         Do not run dex2oat on the files before starting"
+        echo "                          the test."
+        echo "    --relocate            Force the use of relocating in the test, making"
+        echo "                          the image and oat files be relocated to a random"
+        echo "                          address before running. (default)"
+        echo "    --no-relocate         Force the use of no relocating in the test"
+        echo "    --host                Use the host-mode virtual machine."
+        echo "    --invoke-with         Pass --invoke-with option to runtime."
+        echo "    --dalvik              Use Dalvik (off by default)."
+        echo "    --jvm                 Use a host-local RI virtual machine."
+        echo "    --output-path [path]  Location where to store the build" \
              "files."
-        echo "    --64                 Run the test in 64-bit mode"
-        echo "    --trace              Run with method tracing"
-        echo "    --gcstress           Run with gc stress testing"
-        echo "    --gcverify           Run with gc verification"
-        echo "    --always-clean       Delete the test files even if the test fails."
+        echo "    --64                  Run the test in 64-bit mode"
+        echo "    --trace               Run with method tracing"
+        echo "    --gcstress            Run with gc stress testing"
+        echo "    --gcverify            Run with gc verification"
+        echo "    --always-clean        Delete the test files even if the test fails."
+        echo "    --android-root [path] The path on target for the android root. (/system by default)."
     ) 1>&2
     exit 1
 fi