Additional heap verification for the Gc

There are now two verification booleans which can be enabled. When these get enabled, it verifies that each live object only references other live objects.

Changed SetClass to use SetFieldPtr to avoid having an extra card mark. This is safe since all classes are held live by the class linker.

Change-Id: I005bb59e5cc8153a79d3ccb3d7b5cabd29fb4051
diff --git a/src/heap.h b/src/heap.h
index 6f6cd67..23f2ac3 100644
--- a/src/heap.h
+++ b/src/heap.h
@@ -58,6 +58,7 @@
   // Partial GC, over only the alloc space
   GC_PARTIAL,
 };
+std::ostream& operator<<(std::ostream& os, const GcType& policy);
 
 class LOCKABLE Heap {
  public:
@@ -100,6 +101,10 @@
 
   // Check sanity of all live references. Requires the heap lock.
   void VerifyHeap();
+  static void RootMatchesObjectVisitor(const Object* root, void* arg);
+  void VerifyHeapReferences(const std::string& phase)
+      EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_)
+      SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
 
   // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
   // and doesn't abort on error, allowing the caller to report more
@@ -237,7 +242,7 @@
 
   void DumpForSigQuit(std::ostream& os);
 
-  void Trim(AllocSpace* alloc_space);
+  void Trim();
 
   HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_) {
     return live_bitmap_.get();
@@ -259,6 +264,13 @@
   // Un-mark all the objects in the allocation stack.
   void UnMarkStack(MarkStack* alloc_stack);
 
+  // Un-mark all live objects in the allocation stack.
+  void UnMarkStackAsLive(MarkStack* alloc_stack);
+
+  // Update and mark mod union table based on gc type.
+  void UpdateAndMarkModUnion(TimingLogger& timings, GcType gc_type)
+      EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
+
   // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
   // Assumes there is only one image space.
   ImageSpace* GetImageSpace();
@@ -309,6 +321,9 @@
   static void VerificationCallback(Object* obj, void* arg)
       SHARED_LOCKS_REQUIRED(GlobalSychronization::heap_bitmap_lock_);
 
+  // Swpa bitmaps (if we are a full Gc then we swap the zygote bitmap too).
+  void SwapBitmaps();
+
   Spaces spaces_;
 
   // The alloc space which we are currently allocating into.
@@ -353,6 +368,11 @@
   // Number of objects allocated.  Adjusted after each allocation and free.
   volatile size_t num_objects_allocated_;
 
+  // Heap verification flags.
+  const bool pre_gc_verify_heap_;
+  const bool post_gc_verify_heap_;
+  const bool verify_mod_union_table_;
+
   // Last trim time
   uint64_t last_trim_time_;
 
@@ -397,6 +417,8 @@
 
   bool verify_objects_;
 
+  friend class VerifyReferenceVisitor;
+  friend class VerifyObjectVisitor;
   friend class ScopedHeapLock;
   FRIEND_TEST(SpaceTest, AllocAndFree);
   FRIEND_TEST(SpaceTest, AllocAndFreeList);