ART: Fix 004-ReferenceMap run test

This patch adds a new option to ArtMethod::ToNativeQuickPc to select
the order of iteration over stack maps. The method is only used by
the runtime to find native_pc of catch blocks, but also by the
004-ReferenceMap test which uses it to find native_pc of a safepoint.

Change-Id: Idb2b34aabf1ac7249c30a00806af7d63d7e682dd
diff --git a/runtime/art_method.cc b/runtime/art_method.cc
index 65f41cc..26839ec 100644
--- a/runtime/art_method.cc
+++ b/runtime/art_method.cc
@@ -223,7 +223,9 @@
   return DexFile::kDexNoIndex;
 }
 
-uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc, bool abort_on_failure) {
+uintptr_t ArtMethod::ToNativeQuickPc(const uint32_t dex_pc,
+                                     bool is_catch_handler,
+                                     bool abort_on_failure) {
   const void* entry_point = GetQuickOatEntryPoint(sizeof(void*));
   if (IsOptimized(sizeof(void*))) {
     // Optimized code does not have a mapping table. Search for the dex-to-pc
@@ -231,9 +233,12 @@
     CodeInfo code_info = GetOptimizedCodeInfo();
     StackMapEncoding encoding = code_info.ExtractEncoding();
 
-    // Assume the caller needs the mapping for a catch handler. If there are
-    // multiple stack maps for this dex_pc, it will hit the catch stack map first.
-    StackMap stack_map = code_info.GetCatchStackMapForDexPc(dex_pc, encoding);
+    // All stack maps are stored in the same CodeItem section, safepoint stack
+    // maps first, then catch stack maps. We use `is_catch_dex_pc` to select the
+    // order of iteration.
+    StackMap stack_map =
+        LIKELY(is_catch_handler) ? code_info.GetCatchStackMapForDexPc(dex_pc, encoding)
+                                 : code_info.GetStackMapForDexPc(dex_pc, encoding);
     if (stack_map.IsValid()) {
       return reinterpret_cast<uintptr_t>(entry_point) + stack_map.GetNativePcOffset(encoding);
     }