Use handles for GetReferringObjects

Required since VisitObjects may cause thread suspension. Fixes
failing jdwp tests.

Test: test-art-host
Test: art/tools/run-jdwp-tests.sh '--mode=host' '--variant=X32' --debug

Bug: 31113334
Change-Id: Ic11780b3778c83e7812bc90c0723a293537f376c
diff --git a/runtime/debugger.cc b/runtime/debugger.cc
index 502ce4b..073750e4 100644
--- a/runtime/debugger.cc
+++ b/runtime/debugger.cc
@@ -936,10 +936,11 @@
   if (o == nullptr) {
     return JDWP::ERR_INVALID_OBJECT;
   }
-  std::vector<ObjPtr<mirror::Object>> raw_instances;
-  heap->GetReferringObjects(o, max_count, raw_instances);
+  VariableSizedHandleScope hs(Thread::Current());
+  std::vector<Handle<mirror::Object>> raw_instances;
+  heap->GetReferringObjects(hs, hs.NewHandle(o), max_count, raw_instances);
   for (size_t i = 0; i < raw_instances.size(); ++i) {
-    referring_objects->push_back(gRegistry->Add(raw_instances[i]));
+    referring_objects->push_back(gRegistry->Add(raw_instances[i].Get()));
   }
   return JDWP::ERR_NONE;
 }
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 770a55d..ddb5be1 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -936,10 +936,14 @@
     }
     DecrementDisableMovingGC(self);
   } else {
+    // Since concurrent moving GC has thread suspension, also poison ObjPtr the normal case to
+    // catch bugs.
+    self->PoisonObjectPointers();
     // GCs can move objects, so don't allow this.
     ScopedAssertNoThreadSuspension ants("Visiting objects");
     DCHECK(region_space_ == nullptr);
     VisitObjectsInternal(callback, arg);
+    self->PoisonObjectPointers();
   }
 }
 
@@ -1953,11 +1957,13 @@
 
 class ReferringObjectsFinder {
  public:
-  ReferringObjectsFinder(ObjPtr<mirror::Object> object,
+  ReferringObjectsFinder(VariableSizedHandleScope& scope,
+                         Handle<mirror::Object> object,
                          int32_t max_count,
-                         std::vector<ObjPtr<mirror::Object>>& referring_objects)
+                         std::vector<Handle<mirror::Object>>& referring_objects)
       REQUIRES_SHARED(Locks::mutator_lock_)
-      : object_(object),
+      : scope_(scope),
+        object_(object),
         max_count_(max_count),
         referring_objects_(referring_objects) {}
 
@@ -1979,8 +1985,8 @@
                   bool is_static ATTRIBUTE_UNUSED) const
       REQUIRES_SHARED(Locks::mutator_lock_) {
     mirror::Object* ref = obj->GetFieldObject<mirror::Object>(offset);
-    if (ref == object_ && (max_count_ == 0 || referring_objects_.size() < max_count_)) {
-      referring_objects_.push_back(obj);
+    if (ref == object_.Get() && (max_count_ == 0 || referring_objects_.size() < max_count_)) {
+      referring_objects_.push_back(scope_.NewHandle(obj));
     }
   }
 
@@ -1989,16 +1995,18 @@
   void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
 
  private:
-  ObjPtr<mirror::Object> const object_;
+  VariableSizedHandleScope& scope_;
+  Handle<mirror::Object> const object_;
   const uint32_t max_count_;
-  std::vector<ObjPtr<mirror::Object>>& referring_objects_;
+  std::vector<Handle<mirror::Object>>& referring_objects_;
   DISALLOW_COPY_AND_ASSIGN(ReferringObjectsFinder);
 };
 
-void Heap::GetReferringObjects(ObjPtr<mirror::Object> o,
+void Heap::GetReferringObjects(VariableSizedHandleScope& scope,
+                               Handle<mirror::Object> o,
                                int32_t max_count,
-                               std::vector<ObjPtr<mirror::Object>>& referring_objects) {
-  ReferringObjectsFinder finder(o, max_count, referring_objects);
+                               std::vector<Handle<mirror::Object>>& referring_objects) {
+  ReferringObjectsFinder finder(scope, o, max_count, referring_objects);
   VisitObjects(&ReferringObjectsFinder::Callback, &finder);
 }
 
diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h
index 95db4dd..796b51d 100644
--- a/runtime/gc/heap.h
+++ b/runtime/gc/heap.h
@@ -50,6 +50,7 @@
 class Thread;
 class ThreadPool;
 class TimingLogger;
+class VariableSizedHandleScope;
 
 namespace mirror {
   class Class;
@@ -348,9 +349,10 @@
       REQUIRES_SHARED(Locks::mutator_lock_);
 
   // Implements JDWP OR_ReferringObjects.
-  void GetReferringObjects(ObjPtr<mirror::Object> o,
+  void GetReferringObjects(VariableSizedHandleScope& scope,
+                           Handle<mirror::Object> o,
                            int32_t max_count,
-                           std::vector<ObjPtr<mirror::Object>>& referring_objects)
+                           std::vector<Handle<mirror::Object>>& referring_objects)
       REQUIRES(!Locks::heap_bitmap_lock_, !*gc_complete_lock_)
       REQUIRES_SHARED(Locks::mutator_lock_);