Disable GC in Get*Critical() for SS and CMS

The logic to disable GC in GetStringCritical() and
GetPrimitiveArrayCritical() was removed when userfaultfd GC CLs were
merged. Only thread-flip disable logic was kept, which is used by CC and
userfaultfd GC.
In this CL that issue is fixed as the other GCs are still used in ART.

This CL also fixes another issue in userfaultfd GC related to memory
range bitmap usage.

Bug: 243181908
Test: ART_TEST_DEBUG_GC=true ART_USE_READ_BARRIER=false
art/test/testrunner/testrunner.py --host --interpreter -t 004-JniTest

Change-Id: I50bb2f03374e9aa6deec550f2284d9bbd736ed72
diff --git a/runtime/gc/collector/mark_compact-inl.h b/runtime/gc/collector/mark_compact-inl.h
index 3db51bf..8fdf0f3 100644
--- a/runtime/gc/collector/mark_compact-inl.h
+++ b/runtime/gc/collector/mark_compact-inl.h
@@ -30,7 +30,11 @@
                                                                         size_t size) {
   const uintptr_t begin_bit_idx = MemRangeBitmap::BitIndexFromAddr(begin);
   DCHECK(!Bitmap::TestBit(begin_bit_idx));
-  const uintptr_t end_bit_idx = MemRangeBitmap::BitIndexFromAddr(begin + size);
+  uintptr_t end = begin + size;
+  // We have to use the unchecked version of BitIndexFromAddr() as 'end' could
+  // be outside the range. Do explicit check here.
+  DCHECK_LE(end, MemRangeBitmap::CoverEnd());
+  const uintptr_t end_bit_idx = MemRangeBitmap::BitIndexFromAddrUnchecked(end);
   uintptr_t* bm_address = Bitmap::Begin() + Bitmap::BitIndexToWordIndex(begin_bit_idx);
   uintptr_t* const end_bm_address = Bitmap::Begin() + Bitmap::BitIndexToWordIndex(end_bit_idx);
   uintptr_t mask = Bitmap::BitIndexToMask(begin_bit_idx);
diff --git a/runtime/jni/jni_internal.cc b/runtime/jni/jni_internal.cc
index 7a7644e..5763787 100644
--- a/runtime/jni/jni_internal.cc
+++ b/runtime/jni/jni_internal.cc
@@ -2178,9 +2178,13 @@
       if (heap->IsMovableObject(s)) {
         StackHandleScope<1> hs(soa.Self());
         HandleWrapperObjPtr<mirror::String> h(hs.NewHandleWrapper(&s));
-        // For the CC and CMC collector, we only need to wait for the thread flip rather
-        // than the whole GC to occur thanks to the to-space invariant.
-        heap->IncrementDisableThreadFlip(soa.Self());
+        if (!gUseReadBarrier && !gUseUserfaultfd) {
+          heap->IncrementDisableMovingGC(soa.Self());
+        } else {
+          // For the CC and CMC collector, we only need to wait for the thread flip rather
+          // than the whole GC to occur thanks to the to-space invariant.
+          heap->IncrementDisableThreadFlip(soa.Self());
+        }
       }
       // Ensure that the string doesn't cause userfaults in case passed on to
       // the kernel.
@@ -2200,7 +2204,11 @@
     gc::Heap* heap = Runtime::Current()->GetHeap();
     ObjPtr<mirror::String> s = soa.Decode<mirror::String>(java_string);
     if (!s->IsCompressed() && heap->IsMovableObject(s)) {
-      heap->DecrementDisableThreadFlip(soa.Self());
+      if (!gUseReadBarrier && !gUseUserfaultfd) {
+        heap->DecrementDisableMovingGC(soa.Self());
+      } else {
+        heap->DecrementDisableThreadFlip(soa.Self());
+      }
     }
     // TODO: For uncompressed strings GetStringCritical() always returns `s->GetValue()`.
     // Should we report an error if the user passes a different `chars`?
@@ -2363,9 +2371,13 @@
     }
     gc::Heap* heap = Runtime::Current()->GetHeap();
     if (heap->IsMovableObject(array)) {
-      // For the CC and CMC collector, we only need to wait for the thread flip rather
-      // than the whole GC to occur thanks to the to-space invariant.
-      heap->IncrementDisableThreadFlip(soa.Self());
+      if (!gUseReadBarrier && !gUseUserfaultfd) {
+        heap->IncrementDisableMovingGC(soa.Self());
+      } else {
+        // For the CC and CMC collector, we only need to wait for the thread flip rather
+        // than the whole GC to occur thanks to the to-space invariant.
+        heap->IncrementDisableThreadFlip(soa.Self());
+      }
       // Re-decode in case the object moved since IncrementDisableGC waits for GC to complete.
       array = soa.Decode<mirror::Array>(java_array);
     }
@@ -2962,7 +2974,11 @@
         delete[] reinterpret_cast<uint64_t*>(elements);
       } else if (heap->IsMovableObject(array)) {
         // Non copy to a movable object must means that we had disabled the moving GC.
-        heap->DecrementDisableThreadFlip(soa.Self());
+        if (!gUseReadBarrier && !gUseUserfaultfd) {
+          heap->DecrementDisableMovingGC(soa.Self());
+        } else {
+          heap->DecrementDisableThreadFlip(soa.Self());
+        }
       }
     }
   }