Merge "Fix the Mac build on x86-64."
diff --git a/Android.mk b/Android.mk
index 18e1c64..7d31382 100644
--- a/Android.mk
+++ b/Android.mk
@@ -105,9 +105,13 @@
 
 
 # ART_HOST_DEPENDENCIES depends on Android.executable.mk above for ART_HOST_EXECUTABLES
-ART_HOST_DEPENDENCIES := $(ART_HOST_EXECUTABLES) $(HOST_OUT_JAVA_LIBRARIES)/core-libart-hostdex.jar \
+ART_HOST_DEPENDENCIES := \
+	$(ART_HOST_EXECUTABLES) \
+	$(HOST_OUT_JAVA_LIBRARIES)/core-libart-hostdex.jar \
 	$(HOST_LIBRARY_PATH)/libjavacore$(ART_HOST_SHLIB_EXTENSION)
-ART_TARGET_DEPENDENCIES := $(ART_TARGET_EXECUTABLES) $(TARGET_OUT_JAVA_LIBRARIES)/core-libart.jar \
+ART_TARGET_DEPENDENCIES := \
+	$(ART_TARGET_EXECUTABLES) \
+	$(TARGET_OUT_JAVA_LIBRARIES)/core-libart.jar \
 	$(TARGET_OUT_SHARED_LIBRARIES)/libjavacore.so
 ifdef TARGET_2ND_ARCH
 ART_TARGET_DEPENDENCIES += $(2ND_TARGET_OUT_SHARED_LIBRARIES)/libjavacore.so
diff --git a/build/Android.common_build.mk b/build/Android.common_build.mk
index 21ba962..5cfdbfd 100644
--- a/build/Android.common_build.mk
+++ b/build/Android.common_build.mk
@@ -121,10 +121,10 @@
   ART_HOST_CLANG := true
 endif
 
-# Clang on the target: only enabled for ARM64. Target builds use GCC by default.
+# Clang on the target. Target builds use GCC by default.
 ART_TARGET_CLANG :=
 ART_TARGET_CLANG_arm :=
-ART_TARGET_CLANG_arm64 := true
+ART_TARGET_CLANG_arm64 :=
 ART_TARGET_CLANG_mips :=
 ART_TARGET_CLANG_x86 :=
 ART_TARGET_CLANG_x86_64 :=
diff --git a/build/Android.common_path.mk b/build/Android.common_path.mk
index d672393..8c0d9f2 100644
--- a/build/Android.common_path.mk
+++ b/build/Android.common_path.mk
@@ -72,5 +72,4 @@
 
 HOST_CORE_DEX_FILES   := $(foreach jar,$(HOST_CORE_JARS),  $(call intermediates-dir-for,JAVA_LIBRARIES,$(jar),t,COMMON)/javalib.jar)
 TARGET_CORE_DEX_FILES := $(foreach jar,$(TARGET_CORE_JARS),$(call intermediates-dir-for,JAVA_LIBRARIES,$(jar), ,COMMON)/javalib.jar)
-
 endif # ANDROID_COMMON_PATH_MK
diff --git a/build/Android.common_test.mk b/build/Android.common_test.mk
index 1815f50..21a8931 100644
--- a/build/Android.common_test.mk
+++ b/build/Android.common_test.mk
@@ -99,6 +99,7 @@
     LOCAL_MODULE_PATH := $(3)
     LOCAL_DEX_PREOPT_IMAGE_LOCATION := $(TARGET_CORE_IMG_OUT)
     include $(BUILD_JAVA_LIBRARY)
+    $(5) := $$(LOCAL_INSTALLED_MODULE)
   endif
   ifeq ($(ART_BUILD_HOST),true)
     include $(CLEAR_VARS)
@@ -110,8 +111,8 @@
     LOCAL_JAVA_LIBRARIES := $(HOST_CORE_JARS)
     LOCAL_DEX_PREOPT_IMAGE := $(HOST_CORE_IMG_LOCATION)
     include $(BUILD_HOST_DALVIK_JAVA_LIBRARY)
+    $(5)-host := $$(LOCAL_INSTALLED_MODULE)
   endif
-  $(5) := $$(LOCAL_INSTALLED_MODULE)
 endef
 
 endif # ANDROID_COMMON_TEST_MK
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index a6f6298..69d6c5a 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -45,20 +45,17 @@
   $(ART_TARGET_NATIVETEST_OUT),art/build/Android.gtest.mk,ART_GTEST_$(dir)_DEX)))
 
 # Dex file dependencies for each gtest.
-ART_GTEST_class_linker_test_DEPS := $(ART_GTEST_Interfaces_DEX) $(ART_GTEST_MyClass_DEX) \
-  $(ART_GTEST_Nested_DEX) $(ART_GTEST_Statics_DEX) $(ART_GTEST_StaticsFromCode_DEX)
-ART_GTEST_compiler_driver_test_DEPS := $(ART_GTEST_AbstractMethod_DEX)
-ART_GTEST_dex_file_test_DEPS := $(ART_GTEST_GetMethodSignature_DEX)
-ART_GTEST_exception_test_DEPS := $(ART_GTEST_ExceptionHandle_DEX)
-ART_GTEST_jni_compiler_test_DEPS := $(ART_GTEST_MyClassNatives_DEX)
-ART_GTEST_jni_internal_test_DEPS := $(ART_GTEST_AllFields_DEX)
-ART_GTEST_object_test_DEPS := $(ART_GTEST_ProtoCompare_DEX) $(ART_GTEST_ProtoCompare2_DEX) \
-   $(ART_GTEST_StaticsFromCode_DEX) $(ART_GTEST_XandY_DEX)
-ART_GTEST_proxy_test_DEPS := $(ART_GTEST_Interfaces_DEX)
-ART_GTEST_reflection_test_DEPS := $(ART_GTEST_Main_DEX) $(ART_GTEST_NonStaticLeafMethods_DEX) \
-  $(ART_GTEST_StaticLeafMethods_DEX)
-ART_GTEST_stub_test_DEPS := $(ART_GTEST_AllFields_DEX)
-ART_GTEST_transaction_test_DEPS := $(ART_GTEST_Transaction_DEX)
+ART_GTEST_class_linker_test_DEPS := Interfaces MyClass Nested Statics StaticsFromCode
+ART_GTEST_compiler_driver_test_DEPS := AbstractMethod
+ART_GTEST_dex_file_test_DEPS := GetMethodSignature
+ART_GTEST_exception_test_DEPS := ExceptionHandle
+ART_GTEST_jni_compiler_test_DEPS := MyClassNatives
+ART_GTEST_jni_internal_test_DEPS := AllFields StaticLeafMethods
+ART_GTEST_object_test_DEPS := ProtoCompare ProtoCompare2 StaticsFromCode XandY
+ART_GTEST_proxy_test_DEPS := Interfaces
+ART_GTEST_reflection_test_DEPS := Main NonStaticLeafMethods StaticLeafMethods
+ART_GTEST_stub_test_DEPS := AllFields
+ART_GTEST_transaction_test_DEPS := Transaction
 
 # The elf writer test has dependencies on core.oat.
 ART_GTEST_elf_writer_test_DEPS := $(HOST_CORE_OAT_OUT) $(2ND_HOST_CORE_OAT_OUT) \
@@ -194,12 +191,11 @@
 define define-art-gtest-rule-target
   gtest_rule := test-art-target-gtest-$(1)$$($(2)ART_PHONY_TEST_TARGET_SUFFIX)
 
-  # Add the test dependencies to test-art-target-sync, which will be a prerequisit for the test
+  # Add the test dependencies to test-art-target-sync, which will be a prerequisite for the test
   # to ensure files are pushed to the device.
   TEST_ART_TARGET_SYNC_DEPS += \
-    $$(ART_GTEST_$(1)_DEPS) \
+    $(foreach file,$(ART_GTEST_$(1)_DEPS),$(ART_GTEST_$(file)_DEX)) \
     $$(ART_TARGET_NATIVETEST_OUT)/$$(TARGET_$(2)ARCH)/$(1) \
-    $$(TARGET_CORE_DEX_LOCATIONS) \
     $$($(2)TARGET_OUT_SHARED_LIBRARIES)/libjavacore.so
 
 .PHONY: $$(gtest_rule)
@@ -234,7 +230,7 @@
 
 
 .PHONY: $$(gtest_rule)
-$$(gtest_rule): $$(gtest_exe) $$(ART_GTEST_$(1)_DEPS) $$(gtest_deps)
+$$(gtest_rule): $$(gtest_exe) $(foreach file,$(ART_GTEST_$(1)_DEPS),$(ART_GTEST_$(file)_DEX-host)) $$(gtest_deps)
 	$(hide) ($$(call ART_TEST_SKIP,$$@) && $$< && $$(call ART_TEST_PASSED,$$@)) \
 	  || $$(call ART_TEST_FAILED,$$@)
 
@@ -461,4 +457,4 @@
 ART_GTEST_transaction_test_DEPS :=
 $(foreach dir,$(GTEST_DEX_DIRECTORIES), $(eval ART_GTEST_TEST_$(dir)_DEX :=))
 GTEST_DEX_DIRECTORIES :=
-LOCAL_PATH :=
\ No newline at end of file
+LOCAL_PATH :=
diff --git a/compiler/optimizing/register_allocator_test.cc b/compiler/optimizing/register_allocator_test.cc
index bfabc5a..a7283ab 100644
--- a/compiler/optimizing/register_allocator_test.cc
+++ b/compiler/optimizing/register_allocator_test.cc
@@ -318,4 +318,42 @@
   ASSERT_EQ(phi_interval->GetRegister(), ret->InputAt(0)->GetLiveInterval()->GetRegister());
 }
 
+TEST(RegisterAllocatorTest, FirstRegisterUse) {
+  const uint16_t data[] = THREE_REGISTERS_CODE_ITEM(
+    Instruction::CONST_4 | 0 | 0,
+    Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8,
+    Instruction::ADD_INT_LIT8 | 0 << 8, 1 << 8,
+    Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8 | 1,
+    Instruction::RETURN_VOID);
+
+  ArenaPool pool;
+  ArenaAllocator allocator(&pool);
+  HGraph* graph = BuildSSAGraph(data, &allocator);
+  CodeGenerator* codegen = CodeGenerator::Create(&allocator, graph, kArm);
+  SsaLivenessAnalysis liveness(*graph, codegen);
+  liveness.Analyze();
+
+  HAdd* first_add = graph->GetBlocks().Get(1)->GetFirstInstruction()->AsAdd();
+  HAdd* last_add = graph->GetBlocks().Get(1)->GetLastInstruction()->GetPrevious()->AsAdd();
+  ASSERT_EQ(last_add->InputAt(0), first_add);
+  LiveInterval* interval = first_add->GetLiveInterval();
+  ASSERT_EQ(interval->GetEnd(), last_add->GetLifetimePosition() + 1);
+  ASSERT_TRUE(interval->GetNextSibling() == nullptr);
+
+  // We need a register for the output of the instruction.
+  ASSERT_EQ(interval->FirstRegisterUse(), first_add->GetLifetimePosition());
+
+  // Split at the next instruction.
+  interval = interval->SplitAt(first_add->GetLifetimePosition() + 2);
+  // The user of the split is the last add.
+  ASSERT_EQ(interval->FirstRegisterUse(), last_add->GetLifetimePosition() + 1);
+
+  // Split before the last add.
+  LiveInterval* new_interval = interval->SplitAt(last_add->GetLifetimePosition() - 1);
+  // Ensure the current interval has no register use...
+  ASSERT_EQ(interval->FirstRegisterUse(), kNoLifetime);
+  // And the new interval has it for the last add.
+  ASSERT_EQ(new_interval->FirstRegisterUse(), last_add->GetLifetimePosition() + 1);
+}
+
 }  // namespace art
diff --git a/compiler/optimizing/ssa_liveness_analysis.h b/compiler/optimizing/ssa_liveness_analysis.h
index fc3eb66..83035b5 100644
--- a/compiler/optimizing/ssa_liveness_analysis.h
+++ b/compiler/optimizing/ssa_liveness_analysis.h
@@ -289,21 +289,23 @@
 
   size_t FirstRegisterUseAfter(size_t position) const {
     if (position == GetStart() && defined_by_ != nullptr) {
-      Location location = defined_by_->GetLocations()->Out();
+      LocationSummary* locations = defined_by_->GetLocations();
+      Location location = locations->Out();
       // This interval is the first interval of the instruction. If the output
       // of the instruction requires a register, we return the position of that instruction
       // as the first register use.
       if (location.IsUnallocated()) {
         if ((location.GetPolicy() == Location::kRequiresRegister)
              || (location.GetPolicy() == Location::kSameAsFirstInput
-                && defined_by_->GetLocations()->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
+                 && locations->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
           return position;
         }
       }
     }
 
     UsePosition* use = first_use_;
-    while (use != nullptr) {
+    size_t end = GetEnd();
+    while (use != nullptr && use->GetPosition() <= end) {
       size_t use_position = use->GetPosition();
       if (use_position >= position && !use->GetIsEnvironment()) {
         Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
diff --git a/runtime/check_jni.cc b/runtime/check_jni.cc
index a816489..fefb907 100644
--- a/runtime/check_jni.cc
+++ b/runtime/check_jni.cc
@@ -1757,9 +1757,7 @@
     CHECK_JNI_ENTRY(kFlag_Default, "EpJ", env, address, capacity);
     if (address == nullptr) {
       JniAbortF(__FUNCTION__, "non-nullable address is NULL");
-    }
-    if (capacity < 0) {
-      JniAbortF(__FUNCTION__, "capacity must be non-negative: %" PRId64, capacity);
+      return nullptr;
     }
     return CHECK_JNI_EXIT("L", baseEnv(env)->NewDirectByteBuffer(env, address, capacity));
   }
diff --git a/runtime/jni_internal.cc b/runtime/jni_internal.cc
index 513b409..8842f59 100644
--- a/runtime/jni_internal.cc
+++ b/runtime/jni_internal.cc
@@ -2447,13 +2447,18 @@
   static jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity) {
     if (capacity < 0) {
       JniAbortF("NewDirectByteBuffer", "negative buffer capacity: %" PRId64, capacity);
+      return nullptr;
     }
     if (address == nullptr && capacity != 0) {
       JniAbortF("NewDirectByteBuffer", "non-zero capacity for nullptr pointer: %" PRId64, capacity);
+      return nullptr;
     }
 
-    // At the moment, the capacity is limited to 32 bits.
-    CHECK_LE(capacity, 0xffffffff);
+    // At the moment, the capacity of DirectByteBuffer is limited to a signed int.
+    if (capacity > INT_MAX) {
+      JniAbortF("NewDirectByteBuffer", "buffer capacity greater than maximum jint: %" PRId64, capacity);
+      return nullptr;
+    }
     jlong address_arg = reinterpret_cast<jlong>(address);
     jint capacity_arg = static_cast<jint>(capacity);
 
diff --git a/runtime/jni_internal_test.cc b/runtime/jni_internal_test.cc
index 5e46c57..a933f86 100644
--- a/runtime/jni_internal_test.cc
+++ b/runtime/jni_internal_test.cc
@@ -1515,6 +1515,12 @@
   ASSERT_TRUE(env_->IsInstanceOf(buffer, buffer_class));
   ASSERT_EQ(env_->GetDirectBufferAddress(buffer), bytes);
   ASSERT_EQ(env_->GetDirectBufferCapacity(buffer), static_cast<jlong>(sizeof(bytes)));
+
+  {
+    CheckJniAbortCatcher check_jni_abort_catcher;
+    env_->NewDirectByteBuffer(bytes, static_cast<jlong>(INT_MAX) + 1);
+    check_jni_abort_catcher.Check("in call to NewDirectByteBuffer");
+  }
 }
 
 TEST_F(JniInternalTest, MonitorEnterExit) {
@@ -1568,7 +1574,6 @@
     CheckJniAbortCatcher check_jni_abort_catcher;
     env_->MonitorEnter(nullptr);
     check_jni_abort_catcher.Check("in call to MonitorEnter");
-
     env_->MonitorExit(nullptr);
     check_jni_abort_catcher.Check("in call to MonitorExit");
   }
diff --git a/test/Android.oat.mk b/test/Android.oat.mk
index 4e4f62c..3cf9f61 100644
--- a/test/Android.oat.mk
+++ b/test/Android.oat.mk
@@ -60,7 +60,10 @@
 ART_TEST_TARGET_OAT_RULES :=
 
 # We need dex2oat and dalvikvm on the target as well as the core image.
-TEST_ART_TARGET_SYNC_DEPS += $(ART_TARGET_EXECUTABLES) $(TARGET_CORE_IMG_OUT) $(2ND_TARGET_CORE_IMG_OUT)
+TEST_ART_TARGET_SYNC_DEPS += $(ART_TARGET_EXECUTABLES) $(TARGET_CORE_IMG_OUT) $(2ND_TARGET_CORE_IMG_OUT) $(ART_TARGET_TEST_OUT)/$(TARGET_ARCH)/libarttest.so
+ifdef TARGET_2ND_ARCH
+TEST_ART_TARGET_SYNC_DEPS += $(ART_TARGET_TEST_OUT)/$(TARGET_2ND_ARCH)/libarttest.so
+endif
 
 # Define rule to run an individual oat test on the host. Output from the test is written to the
 # host in /tmp/android-data in a directory named after test's rule name (its target) and the parent
@@ -72,7 +75,7 @@
 # $(3): the target (rule name), e.g. test-art-target-oat-default-HelloWorld64
 # $(4): -Xint or undefined - do we want to run with the interpreter or default.
 define define-test-art-oat-rule-target
-  # Add the test dependencies to test-art-target-sync, which will be a prerequisit for the test
+  # Add the test dependencies to test-art-target-sync, which will be a prerequisite for the test
   # to ensure files are pushed to the device.
   TEST_ART_TARGET_SYNC_DEPS += $$(ART_OAT_TEST_$(1)_DEX)
 
@@ -164,11 +167,13 @@
 ART_TEST_HOST_OAT_DEPENDENCIES := \
   $(ART_HOST_EXECUTABLES) \
   $(ART_HOST_LIBRARY_PATH)/libarttest$(ART_HOST_SHLIB_EXTENSION) \
+  $(ART_HOST_LIBRARY_PATH)/libjavacore$(ART_HOST_SHLIB_EXTENSION) \
   $(HOST_CORE_IMG_OUT)
 
 ifneq ($(HOST_PREFER_32_BIT),true)
 ART_TEST_HOST_OAT_DEPENDENCIES += \
   $(2ND_ART_HOST_LIBRARY_PATH)/libarttest$(ART_HOST_SHLIB_EXTENSION) \
+  $(2ND_ART_HOST_LIBRARY_PATH)/libjavacore$(ART_HOST_SHLIB_EXTENSION) \
   $(2ND_HOST_CORE_IMG_OUT)
 endif
 
diff --git a/test/Android.run-test.mk b/test/Android.run-test.mk
index e76e03b..c2ff98f 100644
--- a/test/Android.run-test.mk
+++ b/test/Android.run-test.mk
@@ -23,18 +23,18 @@
 
 # Tests that are timing sensitive and flaky on heavily loaded systems.
 TEST_ART_TIMING_SENSITIVE_RUN_TESTS := \
-  test-art-host-default-053-wait-some32 \
-  test-art-host-default-053-wait-some64 \
-  test-art-host-interpreter-053-wait-some32 \
-  test-art-host-interpreter-053-wait-some64 \
-  test-art-host-optimizing-053-wait-some32 \
-  test-art-host-optimizing-053-wait-some64 \
-  test-art-host-default-055-enum-performance32 \
-  test-art-host-default-055-enum-performance64 \
-  test-art-host-interpreter-055-enum-performance32 \
-  test-art-host-interpreter-055-enum-performance64 \
-  test-art-host-optimizing-055-enum-performance32 \
-  test-art-host-optimizing-055-enum-performance64
+  test-art-host-run-test-default-053-wait-some32 \
+  test-art-host-run-test-default-053-wait-some64 \
+  test-art-host-run-test-interpreter-053-wait-some32 \
+  test-art-host-run-test-interpreter-053-wait-some64 \
+  test-art-host-run-test-optimizing-053-wait-some32 \
+  test-art-host-run-test-optimizing-053-wait-some64 \
+  test-art-host-run-test-default-055-enum-performance32 \
+  test-art-host-run-test-default-055-enum-performance64 \
+  test-art-host-run-test-interpreter-055-enum-performance32 \
+  test-art-host-run-test-interpreter-055-enum-performance64 \
+  test-art-host-run-test-optimizing-055-enum-performance32 \
+  test-art-host-run-test-optimizing-055-enum-performance64
 
  # disable timing sensitive tests on "dist" builds.
 ifdef dist_goal
@@ -109,8 +109,8 @@
 # All tests require the host executables and the core images.
 ART_TEST_HOST_RUN_TEST_DEPENDENCIES := \
   $(ART_HOST_EXECUTABLES) \
-  $(HOST_CORE_IMG_OUT) \
-  $(ART_HOST_LIBRARY_PATH)/libjavacore$(ART_HOST_SHLIB_EXTENSION)
+  $(ART_HOST_LIBRARY_PATH)/libjavacore$(ART_HOST_SHLIB_EXTENSION) \
+  $(HOST_CORE_IMG_OUT)
 
 ifneq ($(HOST_PREFER_32_BIT),true)
 ART_TEST_HOST_RUN_TEST_DEPENDENCIES += \