Add low_4gb MemMap and remove ashmem support for Fuchsia

Test: Tested with Fuchsia build system go/fuchsia-android
Change-Id: I61b09614d6f4d24bf9c975baa1f34c6b5735ca3d
diff --git a/libartbase/base/mem_map.cc b/libartbase/base/mem_map.cc
index c455fed..3ce2b51 100644
--- a/libartbase/base/mem_map.cc
+++ b/libartbase/base/mem_map.cc
@@ -19,7 +19,7 @@
 #include <inttypes.h>
 #include <stdlib.h>
 #include <sys/mman.h>  // For the PROT_* and MAP_* constants.
-#ifndef ANDROID_OS
+#if !defined(ANDROID_OS) && !defined(__Fuchsia__)
 #include <sys/resource.h>
 #endif
 
@@ -29,7 +29,12 @@
 
 #include "android-base/stringprintf.h"
 #include "android-base/unique_fd.h"
+
+#if !defined(__Fuchsia__)
 #include "cutils/ashmem.h"
+#else
+#include "fuchsia_compat.h"
+#endif
 
 #include "allocator.h"
 #include "bit_utils.h"
@@ -161,7 +166,7 @@
 // non-null, we check that pointer is the actual_ptr == expected_ptr,
 // and if not, report in error_msg what the conflict mapping was if
 // found, or a generic error in other cases.
-static bool CheckMapRequest(uint8_t* expected_ptr, void* actual_ptr, size_t byte_count,
+bool MemMap::CheckMapRequest(uint8_t* expected_ptr, void* actual_ptr, size_t byte_count,
                             std::string* error_msg) {
   // Handled first by caller for more specific error messages.
   CHECK(actual_ptr != MAP_FAILED);
@@ -178,7 +183,7 @@
   }
 
   // We asked for an address but didn't get what we wanted, all paths below here should fail.
-  int result = munmap(actual_ptr, byte_count);
+  int result = TargetMUnmap(actual_ptr, byte_count);
   if (result == -1) {
     PLOG(WARNING) << StringPrintf("munmap(%p, %zd) failed", actual_ptr, byte_count);
   }
@@ -207,18 +212,18 @@
 }
 
 #if USE_ART_LOW_4G_ALLOCATOR
-static inline void* TryMemMapLow4GB(void* ptr,
+void* MemMap::TryMemMapLow4GB(void* ptr,
                                     size_t page_aligned_byte_count,
                                     int prot,
                                     int flags,
                                     int fd,
                                     off_t offset) {
-  void* actual = mmap(ptr, page_aligned_byte_count, prot, flags, fd, offset);
+  void* actual = TargetMMap(ptr, page_aligned_byte_count, prot, flags, fd, offset);
   if (actual != MAP_FAILED) {
     // Since we didn't use MAP_FIXED the kernel may have mapped it somewhere not in the low
     // 4GB. If this is the case, unmap and retry.
     if (reinterpret_cast<uintptr_t>(actual) + page_aligned_byte_count >= 4 * GB) {
-      munmap(actual, page_aligned_byte_count);
+      TargetMUnmap(actual, page_aligned_byte_count);
       actual = MAP_FAILED;
     }
   }
@@ -237,7 +242,7 @@
 #ifndef __LP64__
   UNUSED(low_4gb);
 #endif
-  use_ashmem = use_ashmem && !kIsTargetLinux;
+  use_ashmem = use_ashmem && !kIsTargetLinux && !kIsTargetFuchsia;
   if (byte_count == 0) {
     return new MemMap(name, nullptr, 0, nullptr, 0, prot, false);
   }
@@ -521,7 +526,7 @@
   if (!reuse_) {
     MEMORY_TOOL_MAKE_UNDEFINED(base_begin_, base_size_);
     if (!already_unmapped_) {
-      int result = munmap(base_begin_, base_size_);
+      int result = TargetMUnmap(base_begin_, base_size_);
       if (result == -1) {
         PLOG(FATAL) << "munmap failed";
       }
@@ -565,7 +570,7 @@
 
 MemMap* MemMap::RemapAtEnd(uint8_t* new_end, const char* tail_name, int tail_prot,
                            std::string* error_msg, bool use_ashmem) {
-  use_ashmem = use_ashmem && !kIsTargetLinux;
+  use_ashmem = use_ashmem && !kIsTargetLinux && !kIsTargetFuchsia;
   DCHECK_GE(new_end, Begin());
   DCHECK_LE(new_end, End());
   DCHECK_LE(begin_ + size_, reinterpret_cast<uint8_t*>(base_begin_) + base_size_);
@@ -607,7 +612,7 @@
 
   MEMORY_TOOL_MAKE_UNDEFINED(tail_base_begin, tail_base_size);
   // Unmap/map the tail region.
-  int result = munmap(tail_base_begin, tail_base_size);
+  int result = TargetMUnmap(tail_base_begin, tail_base_size);
   if (result == -1) {
     PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
     *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'. See process maps in the log.",
@@ -618,12 +623,12 @@
   // calls. Otherwise, libc (or something else) might take this memory
   // region. Note this isn't perfect as there's no way to prevent
   // other threads to try to take this memory region here.
-  uint8_t* actual = reinterpret_cast<uint8_t*>(mmap(tail_base_begin,
-                                                    tail_base_size,
-                                                    tail_prot,
-                                                    flags,
-                                                    fd.get(),
-                                                    0));
+  uint8_t* actual = reinterpret_cast<uint8_t*>(TargetMMap(tail_base_begin,
+                                                          tail_base_size,
+                                                          tail_prot,
+                                                          flags,
+                                                          fd.get(),
+                                                          0));
   if (actual == MAP_FAILED) {
     PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
     *error_msg = StringPrintf("anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0) failed. See process "
@@ -798,6 +803,8 @@
   std::lock_guard<std::mutex> mu(*mem_maps_lock_);
   DCHECK(gMaps == nullptr);
   gMaps = new Maps;
+
+  TargetMMapInit();
 }
 
 void MemMap::Shutdown() {
@@ -829,8 +836,10 @@
       reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BaseBegin()) +
                               new_base_size),
       base_size_ - new_base_size);
-  CHECK_EQ(munmap(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(BaseBegin()) + new_base_size),
-                  base_size_ - new_base_size), 0) << new_base_size << " " << base_size_;
+  CHECK_EQ(TargetMUnmap(reinterpret_cast<void*>(
+                        reinterpret_cast<uintptr_t>(BaseBegin()) + new_base_size),
+                        base_size_ - new_base_size), 0)
+                        << new_base_size << " " << base_size_;
   base_size_ = new_base_size;
   size_ = new_size;
 }
@@ -976,7 +985,7 @@
     if (orig_prot != prot_non_exec) {
       if (mprotect(actual, length, orig_prot) != 0) {
         PLOG(ERROR) << "Could not protect to requested prot: " << orig_prot;
-        munmap(actual, length);
+        TargetMUnmap(actual, length);
         errno = ENOMEM;
         return MAP_FAILED;
       }
@@ -984,14 +993,14 @@
     return actual;
   }
 
-  actual = mmap(addr, length, prot, flags, fd, offset);
+  actual = TargetMMap(addr, length, prot, flags, fd, offset);
 #else
 #if defined(__LP64__)
   if (low_4gb && addr == nullptr) {
     flags |= MAP_32BIT;
   }
 #endif
-  actual = mmap(addr, length, prot, flags, fd, offset);
+  actual = TargetMMap(addr, length, prot, flags, fd, offset);
 #endif
   return actual;
 }
@@ -1067,13 +1076,13 @@
   // Unmap the unaligned parts.
   if (base_begin < aligned_base_begin) {
     MEMORY_TOOL_MAKE_UNDEFINED(base_begin, aligned_base_begin - base_begin);
-    CHECK_EQ(munmap(base_begin, aligned_base_begin - base_begin), 0)
+    CHECK_EQ(TargetMUnmap(base_begin, aligned_base_begin - base_begin), 0)
         << "base_begin=" << reinterpret_cast<void*>(base_begin)
         << " aligned_base_begin=" << reinterpret_cast<void*>(aligned_base_begin);
   }
   if (aligned_base_end < base_end) {
     MEMORY_TOOL_MAKE_UNDEFINED(aligned_base_end, base_end - aligned_base_end);
-    CHECK_EQ(munmap(aligned_base_end, base_end - aligned_base_end), 0)
+    CHECK_EQ(TargetMUnmap(aligned_base_end, base_end - aligned_base_end), 0)
         << "base_end=" << reinterpret_cast<void*>(base_end)
         << " aligned_base_end=" << reinterpret_cast<void*>(aligned_base_end);
   }