Fixes for gtests on gPrecise.

Also make a similar reservation in heap.cc use PROT_NONE rather than
PROT_READ which should be more efficient.

Change-Id: I648ef5b1bf2906094e92253b30d0a5a7554d1af6
diff --git a/src/heap.cc b/src/heap.cc
index 1cdc9c9..fa84e49 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -222,7 +222,7 @@
     uintptr_t reserve_end = RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), kPageSize);
     oat_file_map_.reset(MemMap::MapAnonymous("oat file reserve",
                                              reinterpret_cast<byte*>(reserve_begin),
-                                             reserve_end - reserve_begin, PROT_READ));
+                                             reserve_end - reserve_begin, PROT_NONE));
 
     if (oat_end_addr > requested_begin) {
       requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr),
diff --git a/src/image_test.cc b/src/image_test.cc
index a250b13..dc81587 100644
--- a/src/image_test.cc
+++ b/src/image_test.cc
@@ -23,11 +23,24 @@
 #include "oat_writer.h"
 #include "signal_catcher.h"
 #include "gc/space.h"
+#include "UniquePtr.h"
 #include "utils.h"
 
 namespace art {
 
-class ImageTest : public CommonTest {};
+class ImageTest : public CommonTest {
+
+ protected:
+  virtual void SetUp() {
+    // Reserve where the image will be loaded up front so that other parts of test set up don't
+    // accidentally end up colliding with the fixed memory address when we need to load the image.
+    image_reservation_.reset(MemMap::MapAnonymous("Image reservation", (byte*)ART_BASE_ADDRESS,
+                                                  (size_t)100 * 1024 *1024,  // 100MB
+                                                  PROT_NONE));
+    CommonTest::SetUp();
+  }
+  UniquePtr<MemMap> image_reservation_;
+};
 
 TEST_F(ImageTest, WriteRead) {
   ScratchFile tmp_oat;
@@ -74,15 +87,18 @@
   }
 
   // Need to delete the compiler since it has worker threads which are attached to runtime.
-  delete compiler_.release();
+  compiler_.reset();
 
-  // tear down old runtime before making a new one, clearing out misc state
-  delete runtime_.release();
+  // Tear down old runtime before making a new one, clearing out misc state.
+  runtime_.reset();
   java_lang_dex_file_ = NULL;
 
   UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName()));
   ASSERT_TRUE(dex.get() != NULL);
 
+  // Remove the reservation of the memory for use to load the image.
+  image_reservation_.reset();
+
   Runtime::Options options;
   std::string image("-Ximage:");
   image.append(tmp_image.GetFilename());
diff --git a/src/mem_map.cc b/src/mem_map.cc
index 800f274..a2ddf3c 100644
--- a/src/mem_map.cc
+++ b/src/mem_map.cc
@@ -42,7 +42,7 @@
   return os;
 }
 
-void CheckMapRequest(byte* addr, size_t byte_count) {
+static void CheckMapRequest(byte* addr, size_t byte_count) {
   if (addr == NULL) {
     return;
   }
@@ -69,7 +69,6 @@
 
 MemMap* MemMap::MapAnonymous(const char* name, byte* addr, size_t byte_count, int prot) {
   CHECK_NE(0U, byte_count);
-  CHECK_NE(0, prot);
   size_t page_aligned_byte_count = RoundUp(byte_count, kPageSize);
   CheckMapRequest(addr, page_aligned_byte_count);