Don't scan image space when starting runtime.

Bug 10432288.
Find Classes and Strings from dex caches lazily rather than when the image is
loaded.
Make class status changes do notifies when there can be waiters.
For Class lookup there's a pathology if we always search dex caches and
so after 1000 failures move all classes into the class table.
Be consistent in using "const char*" for class linker descriptors as this
most easily agrees with the type in the dex file.
Improve the intern run-test so that it has a case of a literal contained in the
image.
Modify image_test to allow any valid lock word rather than expecting 0, ideally
we wouldn't see inflated monitors but we do due to NotifyAll (see bug 6961405).

Change-Id: Ia9bfa748eeccb9b4498784b97c6823141b1f6db8
diff --git a/runtime/mirror/class.cc b/runtime/mirror/class.cc
index 19e134f..5e8b827 100644
--- a/runtime/mirror/class.cc
+++ b/runtime/mirror/class.cc
@@ -50,24 +50,26 @@
   java_lang_Class_ = NULL;
 }
 
-void Class::SetStatus(Status new_status) {
-  if (UNLIKELY(new_status <= GetStatus() && new_status != kStatusError)) {
-    bool class_linker_initialized = Runtime::Current()->GetClassLinker() != nullptr;
-    if (class_linker_initialized) {
+void Class::SetStatus(Status new_status, Thread* self) {
+  Status old_status = GetStatus();
+  bool class_linker_initialized = Runtime::Current()->GetClassLinker() != nullptr;
+  if (LIKELY(class_linker_initialized)) {
+    if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) {
       LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
-          << GetStatus() << " -> " << new_status;
+          << old_status << " -> " << new_status;
     }
-  }
-  if (new_status > kStatusResolved) {
-    CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId())
-        << "Attempt to change status of class while not holding its lock " << PrettyClass(this);
+    if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
+      // When classes are being resolved the resolution code should hold the lock.
+      CHECK_EQ(GetThinLockId(), self->GetThinLockId())
+            << "Attempt to change status of class while not holding its lock: "
+            << PrettyClass(this) << " " << old_status << " -> " << new_status;
+    }
   }
   if (new_status == kStatusError) {
     CHECK_NE(GetStatus(), kStatusError)
         << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
 
     // Stash current exception.
-    Thread* self = Thread::Current();
     SirtRef<mirror::Object> old_throw_this_object(self, NULL);
     SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
     SirtRef<mirror::Throwable> old_exception(self, NULL);
@@ -103,7 +105,13 @@
     self->SetException(gc_safe_throw_location, old_exception.get());
   }
   CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
-  return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
+  SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
+  // Classes that are being resolved or initialized need to notify waiters that the class status
+  // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
+  if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
+      class_linker_initialized) {
+    NotifyAll(self);
+  }
 }
 
 void Class::SetDexCache(DexCache* new_dex_cache) {