Use null error_msg for pic images

Reduces 20ms of overhead for reading proc maps when the map can't be
placed at the optimal address.

Bug: 34927277

Test: test-art-host && device boot

Change-Id: Ib91fff2b832f61c49e67edcd2b7a222ab4409984
diff --git a/runtime/gc/space/image_space.cc b/runtime/gc/space/image_space.cc
index ffbca52..fe1c8e8 100644
--- a/runtime/gc/space/image_space.cc
+++ b/runtime/gc/space/image_space.cc
@@ -587,15 +587,18 @@
     }
 
     std::unique_ptr<MemMap> map;
+
     // GetImageBegin is the preferred address to map the image. If we manage to map the
     // image at the image begin, the amount of fixup work required is minimized.
+    // If it is pic we will retry with error_msg for the failure case. Pass a null error_msg to
+    // avoid reading proc maps for a mapping failure and slowing everything down.
     map.reset(LoadImageFile(image_filename,
                             image_location,
                             *image_header,
                             image_header->GetImageBegin(),
                             file->Fd(),
                             logger,
-                            error_msg));
+                            image_header->IsPic() ? nullptr : error_msg));
     // If the header specifies PIC mode, we can also map at a random low_4gb address since we can
     // relocate in-place.
     if (map == nullptr && image_header->IsPic()) {
@@ -765,8 +768,10 @@
 
     if (storage_mode != ImageHeader::kStorageModeLZ4 &&
         storage_mode != ImageHeader::kStorageModeLZ4HC) {
-      *error_msg = StringPrintf("Invalid storage mode in image header %d",
-                                static_cast<int>(storage_mode));
+      if (error_msg != nullptr) {
+        *error_msg = StringPrintf("Invalid storage mode in image header %d",
+                                  static_cast<int>(storage_mode));
+      }
       return nullptr;
     }
 
@@ -790,7 +795,7 @@
                                                        image_filename,
                                                        error_msg));
       if (temp_map == nullptr) {
-        DCHECK(!error_msg->empty());
+        DCHECK(error_msg == nullptr || !error_msg->empty());
         return nullptr;
       }
       memcpy(map->Begin(), &image_header, sizeof(ImageHeader));
@@ -804,10 +809,12 @@
           map->Size() - decompress_offset);
       VLOG(image) << "Decompressing image took " << PrettyDuration(NanoTime() - start);
       if (decompressed_size + sizeof(ImageHeader) != image_header.GetImageSize()) {
-        *error_msg = StringPrintf(
-            "Decompressed size does not match expected image size %zu vs %zu",
-            decompressed_size + sizeof(ImageHeader),
-            image_header.GetImageSize());
+        if (error_msg != nullptr) {
+          *error_msg = StringPrintf(
+              "Decompressed size does not match expected image size %zu vs %zu",
+              decompressed_size + sizeof(ImageHeader),
+              image_header.GetImageSize());
+        }
         return nullptr;
       }
     }
diff --git a/runtime/mem_map.cc b/runtime/mem_map.cc
index dce56b3..93c212b 100644
--- a/runtime/mem_map.cc
+++ b/runtime/mem_map.cc
@@ -400,7 +400,7 @@
     // reuse means it is okay that it overlaps an existing page mapping.
     // Only use this if you actually made the page reservation yourself.
     CHECK(expected_ptr != nullptr);
-
+    DCHECK(error_msg != nullptr);
     DCHECK(ContainedWithinExistingMap(expected_ptr, byte_count, error_msg))
         << ((error_msg != nullptr) ? *error_msg : std::string());
     flags |= MAP_FIXED;