Merge "Harden -Xcheck:jni so we can log non-classes passed as jclasses." into dalvik-dev
diff --git a/src/dex2oat.cc b/src/dex2oat.cc
index 61d445e..eb77c72 100644
--- a/src/dex2oat.cc
+++ b/src/dex2oat.cc
@@ -51,11 +51,13 @@
   fprintf(stderr,
           "  --boot-image=<file.art>: provide the image file for the boot class path.\n"
           "      Example: --boot-image=/data/art-cache/boot.art\n"
+          "      Default: <host-prefix>/data/art-cache/boot.art\n"
           "\n");
   fprintf(stderr,
           "  --host-prefix may be used to translate host paths to target paths during\n"
           "      cross compilation.\n"
           "      Example: --host-prefix=out/target/product/crespo\n"
+          "      Default: $ANDROID_PRODUCT_OUT\n"
           "\n");
   fprintf(stderr,
           "  --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
@@ -377,7 +379,7 @@
   std::string oat_filename;
   const char* image_filename = NULL;
   const char* image_classes_filename = NULL;
-  std::string boot_image_option;
+  std::string boot_image_filename;
   uintptr_t image_base = 0;
   std::string host_prefix;
   std::vector<const char*> runtime_args;
@@ -404,10 +406,7 @@
         usage();
       }
     } else if (option.starts_with("--boot-image=")) {
-      const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
-      boot_image_option.clear();
-      boot_image_option += "-Ximage:";
-      boot_image_option += boot_image_filename;
+      boot_image_filename = option.substr(strlen("--boot-image=")).data();
     } else if (option.starts_with("--host-prefix=")) {
       host_prefix = option.substr(strlen("--host-prefix=")).data();
     } else if (option == "--runtime-arg") {
@@ -427,10 +426,22 @@
     return EXIT_FAILURE;
   }
 
+  if (host_prefix.empty()) {
+    const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
+    if (android_product_out != NULL) {
+        host_prefix = android_product_out;
+    }
+  }
+
   bool image = (image_filename != NULL);
-  if (!image && boot_image_option.empty()) {
-    fprintf(stderr, "Either --image or --boot-image must be specified\n");
-    return EXIT_FAILURE;
+  if (!image && boot_image_filename.empty()) {
+    boot_image_filename += host_prefix;
+    boot_image_filename += "/data/art-cache/boot.art";
+  }
+  std::string boot_image_option;
+  if (boot_image_filename != NULL) {
+    boot_image_option += "-Ximage:";
+    boot_image_option += boot_image_filename;
   }
 
   if (image_classes_filename != NULL && !image) {
diff --git a/src/dex_file.cc b/src/dex_file.cc
index 01bb6d5..59b92d6 100644
--- a/src/dex_file.cc
+++ b/src/dex_file.cc
@@ -398,7 +398,7 @@
 }
 
 bool DexFile::CheckMagic(const byte* magic) {
-  CHECK(magic != NULL);
+  CHECK(magic != NULL) << GetLocation();
   if (memcmp(magic, kDexMagic, sizeof(kDexMagic)) != 0) {
     LOG(ERROR) << "Unrecognized magic number:"
             << " " << magic[0]
@@ -431,14 +431,14 @@
 
 // Returns a pointer to the UTF-8 string data referred to by the given string_id.
 const char* DexFile::GetStringDataAndLength(const StringId& string_id, int32_t* length) const {
-  CHECK(length != NULL);
+  CHECK(length != NULL) << GetLocation();
   const byte* ptr = base_ + string_id.string_data_off_;
   *length = DecodeUnsignedLeb128(&ptr);
   return reinterpret_cast<const char*>(ptr);
 }
 
 void DexFile::InitIndex() {
-  CHECK_EQ(index_.size(), 0U);
+  CHECK_EQ(index_.size(), 0U) << GetLocation();
   for (size_t i = 0; i < NumClassDefs(); ++i) {
     const ClassDef& class_def = GetClassDef(i);
     const char* descriptor = GetClassDescriptor(class_def);
@@ -700,7 +700,7 @@
   }
 
   const CodeItem* code_item = GetCodeItem(method->GetCodeItemOffset());
-  DCHECK(code_item != NULL);
+  DCHECK(code_item != NULL) << GetLocation();
 
   // A method with no line number info should return -1
   LineNumFromPcContext context(rel_pc, -1);
diff --git a/src/dex_file.h b/src/dex_file.h
index 7f0f975..1356c8c 100644
--- a/src/dex_file.h
+++ b/src/dex_file.h
@@ -196,7 +196,7 @@
   jobject GetDexObject(JNIEnv* env) const;
 
   const Header& GetHeader() const {
-    CHECK(header_ != NULL);
+    CHECK(header_ != NULL) << GetLocation();
     return *header_;
   }
 
@@ -205,19 +205,19 @@
 
   // Returns the number of string identifiers in the .dex file.
   size_t NumStringIds() const {
-    CHECK(header_ != NULL);
+    CHECK(header_ != NULL) << GetLocation();
     return header_->string_ids_size_;
   }
 
   // Returns the StringId at the specified index.
   const StringId& GetStringId(uint32_t idx) const {
-    CHECK_LT(idx, NumStringIds());
+    CHECK_LT(idx, NumStringIds()) << GetLocation();
     return string_ids_[idx];
   }
 
   uint32_t GetIndexForStringId(const StringId& string_id) const {
-    CHECK_GE(&string_id, string_ids_);
-    CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_);
+    CHECK_GE(&string_id, string_ids_) << GetLocation();
+    CHECK_LT(&string_id, string_ids_ + header_->string_ids_size_) << GetLocation();
     return &string_id - string_ids_;
   }
 
@@ -251,21 +251,21 @@
 
   // Returns the number of type identifiers in the .dex file.
   size_t NumTypeIds() const {
-    CHECK(header_ != NULL);
+    CHECK(header_ != NULL) << GetLocation();
     return header_->type_ids_size_;
   }
 
   // Returns the TypeId at the specified index.
   const TypeId& GetTypeId(uint32_t idx) const {
-    CHECK_LT(idx, NumTypeIds());
+    CHECK_LT(idx, NumTypeIds()) << GetLocation();
     return type_ids_[idx];
   }
 
   uint16_t GetIndexForTypeId(const TypeId& type_id) const {
-    CHECK_GE(&type_id, type_ids_);
-    CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_);
+    CHECK_GE(&type_id, type_ids_) << GetLocation();
+    CHECK_LT(&type_id, type_ids_ + header_->type_ids_size_) << GetLocation();
     size_t result = &type_id - type_ids_;
-    DCHECK(result < 65536);
+    DCHECK_LT(result, 65536U) << GetLocation();
     return static_cast<uint16_t>(result);
   }
 
@@ -290,19 +290,19 @@
 
   // Returns the number of field identifiers in the .dex file.
   size_t NumFieldIds() const {
-    CHECK(header_ != NULL);
+    CHECK(header_ != NULL) << GetLocation();
     return header_->field_ids_size_;
   }
 
   // Returns the FieldId at the specified index.
   const FieldId& GetFieldId(uint32_t idx) const {
-    CHECK_LT(idx, NumFieldIds());
+    CHECK_LT(idx, NumFieldIds()) << GetLocation();
     return field_ids_[idx];
   }
 
   uint32_t GetIndexForFieldId(const FieldId& field_id) const {
-    CHECK_GE(&field_id, field_ids_);
-    CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_);
+    CHECK_GE(&field_id, field_ids_) << GetLocation();
+    CHECK_LT(&field_id, field_ids_ + header_->field_ids_size_) << GetLocation();
     return &field_id - field_ids_;
   }
 
@@ -330,19 +330,19 @@
 
   // Returns the number of method identifiers in the .dex file.
   size_t NumMethodIds() const {
-    CHECK(header_ != NULL);
+    CHECK(header_ != NULL) << GetLocation();
     return header_->method_ids_size_;
   }
 
   // Returns the MethodId at the specified index.
   const MethodId& GetMethodId(uint32_t idx) const {
-    CHECK_LT(idx, NumMethodIds());
+    CHECK_LT(idx, NumMethodIds()) << GetLocation();
     return method_ids_[idx];
   }
 
   uint32_t GetIndexForMethodId(const MethodId& method_id) const {
-    CHECK_GE(&method_id, method_ids_);
-    CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_);
+    CHECK_GE(&method_id, method_ids_) << GetLocation();
+    CHECK_LT(&method_id, method_ids_ + header_->method_ids_size_) << GetLocation();
     return &method_id - method_ids_;
   }
 
@@ -381,19 +381,19 @@
   }
   // Returns the number of class definitions in the .dex file.
   size_t NumClassDefs() const {
-    CHECK(header_ != NULL);
+    CHECK(header_ != NULL) << GetLocation();
     return header_->class_defs_size_;
   }
 
   // Returns the ClassDef at the specified index.
   const ClassDef& GetClassDef(uint32_t idx) const {
-    CHECK_LT(idx, NumClassDefs());
+    CHECK_LT(idx, NumClassDefs()) << GetLocation();
     return class_defs_[idx];
   }
 
   uint32_t GetIndexForClassDef(const ClassDef& class_def) const {
-    CHECK_GE(&class_def, class_defs_);
-    CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_);
+    CHECK_GE(&class_def, class_defs_) << GetLocation();
+    CHECK_LT(&class_def, class_defs_ + header_->class_defs_size_) << GetLocation();
     return &class_def - class_defs_;
   }
 
@@ -442,19 +442,19 @@
 
   // Returns the number of prototype identifiers in the .dex file.
   size_t NumProtoIds() const {
-    CHECK(header_ != NULL);
+    CHECK(header_ != NULL) << GetLocation();
     return header_->proto_ids_size_;
   }
 
   // Returns the ProtoId at the specified index.
   const ProtoId& GetProtoId(uint32_t idx) const {
-    CHECK_LT(idx, NumProtoIds());
+    CHECK_LT(idx, NumProtoIds()) << GetLocation();
     return proto_ids_[idx];
   }
 
   uint16_t GetIndexForProtoId(const ProtoId& proto_id) const {
-    CHECK_GE(&proto_id, proto_ids_);
-    CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_);
+    CHECK_GE(&proto_id, proto_ids_) << GetLocation();
+    CHECK_LT(&proto_id, proto_ids_ + header_->proto_ids_size_) << GetLocation();
     return &proto_id - proto_ids_;
   }
 
@@ -639,9 +639,9 @@
         method_ids_(0),
         proto_ids_(0),
         class_defs_(0) {
-    CHECK(addr != NULL);
-    CHECK_GT(length, 0U);
-    CHECK(mem_map != NULL);
+    CHECK(addr != NULL) << GetLocation();
+    CHECK_GT(length, 0U) << GetLocation();
+    CHECK(mem_map != NULL) << GetLocation();
   }
 
   // Top-level initializer that calls other Init methods.
diff --git a/src/jni_internal_test.cc b/src/jni_internal_test.cc
index 0d0cfb4..a9c9a41 100644
--- a/src/jni_internal_test.cc
+++ b/src/jni_internal_test.cc
@@ -504,12 +504,10 @@
   EXPECT_EQ(0, env_->GetStringLength(s));
 }
 
-// TODO: fix gtest death tests on host http://b/5690440
-#if __arm__
-TEST_F(JniInternalTest, NewStringNullCharsNonzeroLength) {
+// TODO: fix gtest death tests on host http://b/5690440 (and target)
+TEST_F(JniInternalTest, DISABLED_NewStringNullCharsNonzeroLength) {
   ASSERT_DEATH(env_->NewString(NULL, 1), "");
 }
-#endif
 
 TEST_F(JniInternalTest, GetStringLength_GetStringUTFLength) {
   // Already tested in the NewString/NewStringUTF tests.