Handle OOMEs in class linker with grace.

Check for OOMEs and then fail due to them in class loading.
Make the compiler driver spot OOMEs during resolution and abort compilation to
avoid needless GC thrash then eventual death.
Allocate the pre-allocated OOME during Runtime::Init as Runtime::Start isn't
called in the context of the compiler/tools.

Change-Id: Id72199d0fe82001b5bf22758b3cdc9cc4b8efbb9
diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc
index 634d3bc..3368e96 100644
--- a/compiler/driver/compiler_driver.cc
+++ b/compiler/driver/compiler_driver.cc
@@ -671,7 +671,7 @@
     if (klass.get() == NULL) {
       image_classes_->erase(it++);
       VLOG(compiler) << "Failed to find class " << descriptor;
-      Thread::Current()->ClearException();
+      self->ClearException();
     } else {
       ++it;
     }
@@ -1472,7 +1472,7 @@
         // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
         // attempt to resolve methods and fields when there is no declaring class.
         CHECK(soa.Self()->IsExceptionPending());
-        Thread::Current()->ClearException();
+        soa.Self()->ClearException();
         resolve_fields_and_methods = false;
       } else {
         resolve_fields_and_methods = manager->GetCompiler()->IsImage();
@@ -1547,7 +1547,14 @@
 
   if (klass == NULL) {
     CHECK(soa.Self()->IsExceptionPending());
-    Thread::Current()->ClearException();
+    mirror::Throwable* exception = soa.Self()->GetException(NULL);
+    VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
+    if (strcmp(ClassHelper(exception->GetClass()).GetDescriptor(),
+               "Ljava/lang/OutOfMemoryError;") == 0) {
+      // There's little point continuing compilation if the heap is exhausted.
+      LOG(FATAL) << "Out of memory during type resolution for compilation";
+    }
+    soa.Self()->ClearException();
   }
 }