Better verification: Detection of missing card marks and dead system weaks.

Added a few additional verification functions.

Verification of missing card marks. Checks that all objects on clean cards do not reference objects in live stack.

Verification of system weaks. Checks that all of the system weaks are live.

Verify objects seems to also be fixed now. It is however, rediculously slow and barely usable.

Change-Id: Ieb5765df59210faa8fdf937cadf6ee51532d8bcb
diff --git a/src/mark_sweep.cc b/src/mark_sweep.cc
index 12e0e47..4a3bcbc 100644
--- a/src/mark_sweep.cc
+++ b/src/mark_sweep.cc
@@ -409,6 +409,40 @@
   SweepJniWeakGlobals(swap_bitmaps);
 }
 
+bool MarkSweep::VerifyIsLiveCallback(const Object* obj, void* arg) {
+  reinterpret_cast<MarkSweep*>(arg)->VerifyIsLive(obj);
+  // We don't actually want to sweep the object, so lets return "marked"
+  return true;
+}
+
+void MarkSweep::VerifyIsLive(const Object* obj) {
+  Heap* heap = GetHeap();
+  if (!heap->GetLiveBitmap()->Test(obj)) {
+    if (std::find(heap->allocation_stack_->Begin(), heap->allocation_stack_->End(), obj) ==
+        heap->allocation_stack_->End()) {
+      // Object not found!
+      heap->DumpSpaces();
+      LOG(FATAL) << "Found dead object " << obj;
+    }
+  }
+}
+
+void MarkSweep::VerifySystemWeaks() {
+  Runtime* runtime = Runtime::Current();
+  // Verify system weaks, uses a special IsMarked callback which always returns true.
+  runtime->GetInternTable()->SweepInternTableWeaks(VerifyIsLiveCallback, this);
+  runtime->GetMonitorList()->SweepMonitorList(VerifyIsLiveCallback, this);
+
+  JavaVMExt* vm = runtime->GetJavaVM();
+  MutexLock mu(vm->weak_globals_lock);
+  IndirectReferenceTable* table = &vm->weak_globals;
+  typedef IndirectReferenceTable::iterator It;  // TODO: C++0x auto
+  for (It it = table->begin(), end = table->end(); it != end; ++it) {
+    const Object** entry = *it;
+    VerifyIsLive(*entry);
+  }
+}
+
 struct SweepCallbackContext {
   MarkSweep* mark_sweep;
   AllocSpace* space;