Use MarkIfNotInToSpace for SemiSpace::MarkObject

SemiSpace::MarkObject is called from ModUnionScanImageRootVisitor
which. These roots may be visited multiple times by the GC. One
example is the allocation stack traces and a class visiting the same
ArtMethod twice.

Bug: 27435111
Change-Id: I721b8bcf7fe43f4c351d902c2f3422d3bb585295
diff --git a/runtime/gc/collector/semi_space-inl.h b/runtime/gc/collector/semi_space-inl.h
index 12cf3db..e87b5ff 100644
--- a/runtime/gc/collector/semi_space-inl.h
+++ b/runtime/gc/collector/semi_space-inl.h
@@ -75,6 +75,7 @@
     }
     obj_ptr->Assign(forward_address);
   } else if (!collect_from_space_only_ && !immune_spaces_.IsInImmuneRegion(obj)) {
+    DCHECK(!to_space_->HasAddress(obj)) << "Tried to mark " << obj << " in to-space";
     BitmapSetSlowPathVisitor visitor(this);
     if (!mark_bitmap_->Set(obj, visitor)) {
       // This object was not previously marked.
diff --git a/runtime/gc/collector/semi_space.cc b/runtime/gc/collector/semi_space.cc
index 2784693..f37daa5 100644
--- a/runtime/gc/collector/semi_space.cc
+++ b/runtime/gc/collector/semi_space.cc
@@ -619,7 +619,7 @@
 
 mirror::Object* SemiSpace::MarkObject(mirror::Object* root) {
   auto ref = StackReference<mirror::Object>::FromMirrorPtr(root);
-  MarkObject(&ref);
+  MarkObjectIfNotInToSpace(&ref);
   return ref.AsMirrorPtr();
 }