Prevent object allocation related races in VisitObjects.

Prevents the following race conditions:
Someone is in the process of pushing a reference in the allocation
stack but hasn't yet written the reference. This caused VisitObjects
to occasionally send null objects to the visitor.
Fixed another race where the object had just been allocated but the
class had not been written. We now skip objects in the allocation
stack which have a null class.

Bug: 13004631
Change-Id: Iad789c5e277a7717ce595c7124f0d65b44392fd8
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index a324925..5c174f8 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -381,7 +381,11 @@
   for (mirror::Object** it = allocation_stack_->Begin(), **end = allocation_stack_->End();
       it < end; ++it) {
     mirror::Object* obj = *it;
-    callback(obj, arg);
+    if (obj != nullptr && obj->GetClass() != nullptr) {
+      // Avoid the race condition caused by the object not yet being written into the allocation
+      // stack or the class not yet being written in the object.
+      callback(obj, arg);
+    }
   }
   GetLiveBitmap()->Walk(callback, arg);
   self->EndAssertNoThreadSuspension(old_cause);