Reland "Update non-moving space references in reverse"

This reverts commit da91abf4ec6686fce3e17668743ee8f85191e1f3.

Reason for revert: Use signed integer for iterator

Bug: 160737021
Bug: 259643149
Test: art/test/testrunnner/testrunner.py
Change-Id: If11aec8b9734dbee7b2df4030c5fc48e53c80898
diff --git a/runtime/gc/collector/mark_compact.cc b/runtime/gc/collector/mark_compact.cc
index 25be59f..be8bebf 100644
--- a/runtime/gc/collector/mark_compact.cc
+++ b/runtime/gc/collector/mark_compact.cc
@@ -1894,14 +1894,20 @@
 
 void MarkCompact::UpdateNonMovingSpace() {
   TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
-  uint8_t* page = non_moving_space_->Begin();
-  for (size_t i = 0; i < non_moving_first_objs_count_; i++) {
+  // Iterating in reverse ensures that the class pointer in objects which span
+  // across more than one page gets updated in the end. This is necessary for
+  // VisitRefsForCompaction() to work correctly.
+  // TODO: If and when we make non-moving space update concurrent, implement a
+  // mechanism to remember class pointers for such objects off-heap and pass it
+  // to VisitRefsForCompaction().
+  uint8_t* page = non_moving_space_->Begin() + non_moving_first_objs_count_ * kPageSize;
+  for (ssize_t i = non_moving_first_objs_count_ - 1; i >= 0; i--) {
     mirror::Object* obj = first_objs_non_moving_space_[i].AsMirrorPtr();
+    page -= kPageSize;
     // null means there are no objects on the page to update references.
     if (obj != nullptr) {
       UpdateNonMovingPage(obj, page);
     }
-    page += kPageSize;
   }
 }