Throw IOException at source of failing to open a dex file.

Before is:
java.lang.ClassNotFoundException: Didn't find class "GCBench" on path: DexPathList[[zip file "/disk2/dalvik-dev/out/host/linux-x86/framework/GCBench.jar"],nativeLibraryDirectories=[/disk2/dalvik-dev/out/host/linux-x86/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        Suppressed: java.lang.ClassNotFoundException: GCBench
                at java.lang.Class.classForName(Native Method)
                at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 1 more
        Caused by: java.lang.NoClassDefFoundError: Class "LGCBench;" not found
                ... 5 more
And after is:
java.lang.ClassNotFoundException: Didn't find class "GCBench" on path: DexPathList[[zip file "/disk2/dalvik-dev/out/host/linux-x86/framework/GCBench.jar"],nativeLibraryDirectories=[/disk2/dalvik-dev/out/host/linux-x86/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        Suppressed: java.io.IOException: Zip archive '/disk2/dalvik-dev/out/host/linux-x86/framework/GCBench.jar' doesn't contain classes.dex
                at dalvik.system.DexFile.openDexFile(Native Method)
                at dalvik.system.DexFile.<init>(DexFile.java:80)
                at dalvik.system.DexFile.<init>(DexFile.java:59)
                at dalvik.system.DexPathList.loadDexFile(DexPathList.java:268)
                at dalvik.system.DexPathList.makeDexElements(DexPathList.java:235)
                at dalvik.system.DexPathList.<init>(DexPathList.java:113)
                at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
                at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:38)
                at java.lang.ClassLoader.createSystemClassLoader(ClassLoader.java:128)
                at java.lang.ClassLoader.access$000(ClassLoader.java:65)
                at java.lang.ClassLoader$SystemClassLoader.<clinit>(ClassLoader.java:81)
                at java.lang.ClassLoader.getSystemClassLoader(ClassLoader.java:137)
        Suppressed: java.lang.ClassNotFoundException: GCBench
                at java.lang.Class.classForName(Native Method)
                at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 1 more
        Caused by: java.lang.NoClassDefFoundError: Class "LGCBench;" not found
                ... 5 more

Also, move dex file verifier messages out of logs.
In the process the ClassLinker::dex_lock_ needed tidying to cover a smaller
scope. Bug 11301553.

Change-Id: I80058652e11e7ea63457cc01a0cb48afe1c15543
diff --git a/compiler/dex/arena_allocator.cc b/compiler/dex/arena_allocator.cc
index 2da8064..95e44b3 100644
--- a/compiler/dex/arena_allocator.cc
+++ b/compiler/dex/arena_allocator.cc
@@ -50,7 +50,9 @@
       map_(nullptr),
       next_(nullptr) {
   if (kUseMemMap) {
-    map_ = MemMap::MapAnonymous("dalvik-arena", NULL, size, PROT_READ | PROT_WRITE);
+    std::string error_msg;
+    map_ = MemMap::MapAnonymous("dalvik-arena", NULL, size, PROT_READ | PROT_WRITE, &error_msg);
+    CHECK(map_ != nullptr) << error_msg;
     memory_ = map_->Begin();
     size_ = map_->Size();
   } else {
diff --git a/compiler/elf_fixup.cc b/compiler/elf_fixup.cc
index 359c493..c571288 100644
--- a/compiler/elf_fixup.cc
+++ b/compiler/elf_fixup.cc
@@ -27,8 +27,9 @@
 static const bool DEBUG_FIXUP = false;
 
 bool ElfFixup::Fixup(File* file, uintptr_t oat_data_begin) {
-  UniquePtr<ElfFile> elf_file(ElfFile::Open(file, true, false));
-  CHECK(elf_file.get() != NULL);
+  std::string error_msg;
+  UniquePtr<ElfFile> elf_file(ElfFile::Open(file, true, false, &error_msg));
+  CHECK(elf_file.get() != nullptr) << error_msg;
 
   // Lookup "oatdata" symbol address.
   ::llvm::ELF::Elf32_Addr oatdata_address = ElfWriter::GetOatDataAddress(elf_file.get());
diff --git a/compiler/elf_stripper.cc b/compiler/elf_stripper.cc
index 7fc662c..7ee8d3c 100644
--- a/compiler/elf_stripper.cc
+++ b/compiler/elf_stripper.cc
@@ -27,9 +27,11 @@
 
 namespace art {
 
-bool ElfStripper::Strip(File* file) {
-  UniquePtr<ElfFile> elf_file(ElfFile::Open(file, true, false));
-  CHECK(elf_file.get() != NULL);
+bool ElfStripper::Strip(File* file, std::string* error_msg) {
+  UniquePtr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg));
+  if (elf_file.get() == nullptr) {
+    return false;
+  }
 
   // ELF files produced by MCLinker look roughly like this
   //
@@ -120,7 +122,8 @@
   elf_file->GetHeader().e_shoff = shoff;
   int result = ftruncate(file->Fd(), offset);
   if (result != 0) {
-    PLOG(ERROR) << "Failed to truncate while stripping ELF file: " << file->GetPath();
+    *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s",
+                              file->GetPath().c_str(), strerror(errno));
     return false;
   }
   return true;
diff --git a/compiler/elf_stripper.h b/compiler/elf_stripper.h
index 6015b30..f1a1d46 100644
--- a/compiler/elf_stripper.h
+++ b/compiler/elf_stripper.h
@@ -17,6 +17,8 @@
 #ifndef ART_COMPILER_ELF_STRIPPER_H_
 #define ART_COMPILER_ELF_STRIPPER_H_
 
+#include <string>
+
 #include "base/macros.h"
 #include "os.h"
 
@@ -26,7 +28,7 @@
  public:
   // Strip an ELF file of unneeded debugging information.
   // Returns true on success, false on failure.
-  static bool Strip(File* file);
+  static bool Strip(File* file, std::string* error_msg);
 
  private:
   DISALLOW_IMPLICIT_CONSTRUCTORS(ElfStripper);
diff --git a/compiler/elf_writer.cc b/compiler/elf_writer.cc
index d3c13dd..0bfe4a4 100644
--- a/compiler/elf_writer.cc
+++ b/compiler/elf_writer.cc
@@ -47,8 +47,9 @@
 void ElfWriter::GetOatElfInformation(File* file,
                                      size_t& oat_loaded_size,
                                      size_t& oat_data_offset) {
-  UniquePtr<ElfFile> elf_file(ElfFile::Open(file, false, false));
-  CHECK(elf_file.get() != NULL);
+  std::string error_msg;
+  UniquePtr<ElfFile> elf_file(ElfFile::Open(file, false, false, &error_msg));
+  CHECK(elf_file.get() != NULL) << error_msg;
 
   oat_loaded_size = elf_file->GetLoadedSize();
   CHECK_NE(0U, oat_loaded_size);
diff --git a/compiler/elf_writer_test.cc b/compiler/elf_writer_test.cc
index ffe1f72..eca67a8 100644
--- a/compiler/elf_writer_test.cc
+++ b/compiler/elf_writer_test.cc
@@ -65,23 +65,26 @@
   UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
   ASSERT_TRUE(file.get() != NULL);
   {
-    UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false));
-    CHECK(ef.get() != NULL);
+    std::string error_msg;
+    UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
+    CHECK(ef.get() != nullptr) << error_msg;
     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", false);
     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", false);
     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", false);
   }
   {
-    UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false));
-    CHECK(ef.get() != NULL);
+    std::string error_msg;
+    UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false, &error_msg));
+    CHECK(ef.get() != nullptr) << error_msg;
     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatdata, "oatdata", true);
     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatexec, "oatexec", true);
     EXPECT_ELF_FILE_ADDRESS(ef, dl_oatlastword, "oatlastword", true);
   }
   {
-    UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true));
-    CHECK(ef.get() != NULL);
-    ef->Load(false);
+    std::string error_msg;
+    UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, true, &error_msg));
+    CHECK(ef.get() != nullptr) << error_msg;
+    CHECK(ef->Load(false, &error_msg)) << error_msg;
     EXPECT_EQ(dl_oatdata, ef->FindDynamicSymbolAddress("oatdata"));
     EXPECT_EQ(dl_oatexec, ef->FindDynamicSymbolAddress("oatexec"));
     EXPECT_EQ(dl_oatlastword, ef->FindDynamicSymbolAddress("oatlastword"));
diff --git a/compiler/image_test.cc b/compiler/image_test.cc
index d4be7c0..a8b7c88 100644
--- a/compiler/image_test.cc
+++ b/compiler/image_test.cc
@@ -112,8 +112,11 @@
   runtime_.reset();
   java_lang_dex_file_ = NULL;
 
-  UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName()));
-  ASSERT_TRUE(dex.get() != NULL);
+  std::string error_msg;
+  UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName().c_str(),
+                                             GetLibCoreDexFileName().c_str(),
+                                             &error_msg));
+  ASSERT_TRUE(dex.get() != nullptr) << error_msg;
 
   // Remove the reservation of the memory for use to load the image.
   UnreserveImageSpace();
diff --git a/compiler/image_writer.cc b/compiler/image_writer.cc
index bcdc1c1..871cfd5 100644
--- a/compiler/image_writer.cc
+++ b/compiler/image_writer.cc
@@ -82,12 +82,14 @@
     LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
     return false;
   }
-  oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location);
-  if (oat_file_ == NULL) {
-    LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location;
+  std::string error_msg;
+  oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg);
+  if (oat_file_ == nullptr) {
+    LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location
+        << ": " << error_msg;
     return false;
   }
-  class_linker->RegisterOatFile(*oat_file_);
+  CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_);
 
   interpreter_to_interpreter_bridge_offset_ =
       oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset();
@@ -192,9 +194,10 @@
 
   int prot = PROT_READ | PROT_WRITE;
   size_t length = RoundUp(size, kPageSize);
-  image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, prot));
-  if (image_.get() == NULL) {
-    LOG(ERROR) << "Failed to allocate memory for image file generation";
+  std::string error_msg;
+  image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, prot, &error_msg));
+  if (UNLIKELY(image_.get() == nullptr)) {
+    LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg;
     return false;
   }
   return true;
diff --git a/compiler/oat_test.cc b/compiler/oat_test.cc
index 6ac5d6a..634a160 100644
--- a/compiler/oat_test.cc
+++ b/compiler/oat_test.cc
@@ -100,8 +100,10 @@
     base::TimingLogger timings("CommonTest::WriteRead", false, false);
     compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), timings);
   }
-  UniquePtr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), NULL, false));
-  ASSERT_TRUE(oat_file.get() != NULL);
+  std::string error_msg;
+  UniquePtr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), NULL, false,
+                                            &error_msg));
+  ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
   const OatHeader& oat_header = oat_file->GetOatHeader();
   ASSERT_TRUE(oat_header.IsValid());
   ASSERT_EQ(1U, oat_header.GetDexFileCount());  // core
@@ -111,8 +113,9 @@
 
   const DexFile* dex_file = java_lang_dex_file_;
   uint32_t dex_file_checksum = dex_file->GetLocationChecksum();
-  const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation(),
+  const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file->GetLocation().c_str(),
                                                                     &dex_file_checksum);
+  ASSERT_TRUE(oat_dex_file != nullptr);
   CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
   for (size_t i = 0; i < dex_file->NumClassDefs(); i++) {
     const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);