Fix and optimize verify object.

VerifyObject no longer resides in heap. You can now enable
VerifyObject for non-debug builds. VerifyStack is still slow, so it
is now guarded by its own flag.

Fixed the image writer to not use verification at places where
verification fails due to invalid reads.

Fixed RosAlloc to use SizeOf which doesn't call verify object.

Added a flag paremeter to some of the mirror getters / setters to
be able to selectively disable VerifyObject on certain calls.

Optimized the GC to not verify each object multiple times during
object scanning if verify object is enabled.

Added 3 verification options: verify reads, verify this, and verify
writes so that you can select how much verification you want for
mirror getters and setters.

Removed some useless DCHECKs which would slow debug builds without
providing any benefits.

TODO: RosAlloc verification doesn't currently work with verify
objects.

Bug: 12934910
Bug: 12879358

Change-Id: Ic61033104dfc334543f89b0fc0ad8cd4f4015d69
diff --git a/runtime/stack.h b/runtime/stack.h
index 7e9889e..6a62922 100644
--- a/runtime/stack.h
+++ b/runtime/stack.h
@@ -22,7 +22,9 @@
 #include "base/casts.h"
 #include "base/macros.h"
 #include "arch/context.h"
+#include "mirror/object.h"
 #include "mirror/object_reference.h"
+#include "verify_object.h"
 
 #include <stdint.h>
 #include <string>
@@ -213,26 +215,20 @@
     return *reinterpret_cast<unaligned_double*>(vreg);
   }
 
-  template <bool kChecked = false>
+  template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
   mirror::Object* GetVRegReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     DCHECK_LT(i, NumberOfVRegs());
+    mirror::Object* ref;
     if (HasReferenceArray()) {
-      mirror::Object* ref = References()[i].AsMirrorPtr();
-      if (kChecked) {
-        CHECK(VerifyReference(ref)) << "VReg " << i << "(" << ref
-                                    << ") is in protected space, reference array " << true;
-      }
-      return ref;
+      ref = References()[i].AsMirrorPtr();
     } else {
       const uint32_t* vreg_ptr = &vregs_[i];
-      mirror::Object* ref =
-          reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
-      if (kChecked) {
-        CHECK(VerifyReference(ref)) << "VReg " << i
-            << "(" << ref << ") is in protected space, reference array " << false;
-      }
-      return ref;
+      ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
     }
+    if (kVerifyFlags & kVerifyReads) {
+      VerifyObject(ref);
+    }
+    return ref;
   }
 
   // Get view of vregs as range of consecutive arguments starting at i.
@@ -290,10 +286,12 @@
     }
   }
 
+  template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
   void SetVRegReference(size_t i, mirror::Object* val) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     DCHECK_LT(i, NumberOfVRegs());
-    DCHECK(!kMovingCollector || VerifyReference(val))
-        << "VReg " << i << "(" << val << ") is in protected space";
+    if (kVerifyFlags & kVerifyWrites) {
+      VerifyObject(val);
+    }
     uint32_t* vreg = &vregs_[i];
     reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
     if (HasReferenceArray()) {
@@ -374,8 +372,6 @@
     return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
   }
 
-  bool VerifyReference(const mirror::Object* val) const;
-
   StackReference<mirror::Object>* References() {
     return const_cast<StackReference<mirror::Object>*>(const_cast<const ShadowFrame*>(this)->References());
   }