Protect all of ART's region space from the start.

We used to only protect memory regions upon clearing them (meaning
they had been allocated before). This change protects all
regions (which are all initially unused) from the start.

Test: art/test.py
Bug: 74064045
Change-Id: I30e6b151063164783f20fecbfa9aa50a8a50327a
diff --git a/runtime/gc/space/region_space.cc b/runtime/gc/space/region_space.cc
index db8253c..9c6a73c 100644
--- a/runtime/gc/space/region_space.cc
+++ b/runtime/gc/space/region_space.cc
@@ -30,7 +30,7 @@
 // value of the region size, evaculate the region.
 static constexpr uint kEvacuateLivePercentThreshold = 75U;
 
-// Whether we protect the cleared regions.
+// Whether we protect the unused and cleared regions.
 // Only protect for target builds to prevent flaky test failures (b/63131961).
 static constexpr bool kProtectClearedRegions = kIsTargetBuild;
 
@@ -132,6 +132,8 @@
   DCHECK(full_region_.IsAllocated());
   size_t ignored;
   DCHECK(full_region_.Alloc(kAlignment, &ignored, nullptr, &ignored) == nullptr);
+  // Protect the whole region space from the start.
+  Protect();
 }
 
 size_t RegionSpace::FromSpaceSize() {
@@ -552,6 +554,12 @@
   evac_region_ = &full_region_;
 }
 
+void RegionSpace::Protect() {
+  if (kProtectClearedRegions) {
+    CheckedCall(mprotect, __FUNCTION__, Begin(), Size(), PROT_NONE);
+  }
+}
+
 void RegionSpace::Unprotect() {
   if (kProtectClearedRegions) {
     CheckedCall(mprotect, __FUNCTION__, Begin(), Size(), PROT_READ | PROT_WRITE);
diff --git a/runtime/gc/space/region_space.h b/runtime/gc/space/region_space.h
index eb570d2..ef2e137 100644
--- a/runtime/gc/space/region_space.h
+++ b/runtime/gc/space/region_space.h
@@ -108,6 +108,11 @@
 
   void Clear() OVERRIDE REQUIRES(!region_lock_);
 
+  // Remove read and write memory protection from the whole region space,
+  // i.e. make memory pages backing the region area not readable and not
+  // writable.
+  void Protect();
+
   // Remove memory protection from the whole region space, i.e. make memory
   // pages backing the region area readable and writable. This method is useful
   // to avoid page protection faults when dumping information about an invalid