Fix FindCatchBlock to work in -Xverify:none mode.

FindCatchBlock now uses ResolveType to get the exception type,
since it might not be able to find it in the dex cache.

Bug: 13948502
Change-Id: Ia6f1c7dc743206ae1c8551bf6239f48ee4d3a784
diff --git a/runtime/catch_block_stack_visitor.cc b/runtime/catch_block_stack_visitor.cc
index f9acffb..410fff9 100644
--- a/runtime/catch_block_stack_visitor.cc
+++ b/runtime/catch_block_stack_visitor.cc
@@ -53,7 +53,9 @@
   }
   if (dex_pc != DexFile::kDexNoIndex) {
     bool clear_exception = false;
-    uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc, &clear_exception);
+    SirtRef<mirror::Class> sirt_method_to_find(Thread::Current(), to_find_);
+    uint32_t found_dex_pc = method->FindCatchBlock(sirt_method_to_find, dex_pc, &clear_exception);
+    to_find_ = sirt_method_to_find.get();
     catch_finder_->SetClearException(clear_exception);
     if (found_dex_pc != DexFile::kDexNoIndex) {
       catch_finder_->SetHandlerDexPc(found_dex_pc);
diff --git a/runtime/catch_block_stack_visitor.h b/runtime/catch_block_stack_visitor.h
index 175ad7d..ce67e27 100644
--- a/runtime/catch_block_stack_visitor.h
+++ b/runtime/catch_block_stack_visitor.h
@@ -45,7 +45,7 @@
   Thread* const self_;
   const bool is_deoptimization_;
   // The type of the exception catch block to find.
-  mirror::Class* const to_find_;
+  mirror::Class* to_find_;
   CatchFinder* const catch_finder_;
   // Number of native methods passed in crawl (equates to number of SIRTs to pop)
   uint32_t native_method_count_;
diff --git a/runtime/interpreter/interpreter_common.h b/runtime/interpreter/interpreter_common.h
index 21eeafa..65bdf0e 100644
--- a/runtime/interpreter/interpreter_common.h
+++ b/runtime/interpreter/interpreter_common.h
@@ -498,7 +498,8 @@
   ThrowLocation throw_location;
   mirror::Throwable* exception = self->GetException(&throw_location);
   bool clear_exception = false;
-  uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(exception->GetClass(), dex_pc,
+  SirtRef<mirror::Class> exception_class(self, exception->GetClass());
+  uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(exception_class, dex_pc,
                                                                    &clear_exception);
   if (found_dex_pc == DexFile::kDexNoIndex) {
     instrumentation->MethodUnwindEvent(self, this_object,
diff --git a/runtime/mirror/art_method.cc b/runtime/mirror/art_method.cc
index ee5a0a4..2151fc7 100644
--- a/runtime/mirror/art_method.cc
+++ b/runtime/mirror/art_method.cc
@@ -230,10 +230,15 @@
   return 0;
 }
 
-uint32_t ArtMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc,
+uint32_t ArtMethod::FindCatchBlock(SirtRef<Class>& exception_type, uint32_t dex_pc,
                                    bool* has_no_move_exception) {
   MethodHelper mh(this);
   const DexFile::CodeItem* code_item = mh.GetCodeItem();
+  // Set aside the exception while we resolve its type.
+  Thread* self = Thread::Current();
+  ThrowLocation throw_location;
+  SirtRef<mirror::Throwable> exception(self, self->GetException(&throw_location));
+  self->ClearException();
   // Default to handler not found.
   uint32_t found_dex_pc = DexFile::kDexNoIndex;
   // Iterate over the catch handlers associated with dex_pc.
@@ -245,21 +250,25 @@
       break;
     }
     // Does this catch exception type apply?
-    Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
-    if (iter_exception_type == NULL) {
-      // The verifier should take care of resolving all exception classes early
+    Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx);
+    if (exception_type.get() == nullptr) {
+      self->ClearException();
       LOG(WARNING) << "Unresolved exception class when finding catch block: "
         << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
-    } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
+    } else if (iter_exception_type->IsAssignableFrom(exception_type.get())) {
       found_dex_pc = it.GetHandlerAddress();
       break;
     }
   }
   if (found_dex_pc != DexFile::kDexNoIndex) {
     const Instruction* first_catch_instr =
-        Instruction::At(&mh.GetCodeItem()->insns_[found_dex_pc]);
+        Instruction::At(&code_item->insns_[found_dex_pc]);
     *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
   }
+  // Put the exception back.
+  if (exception.get() != nullptr) {
+    self->SetException(throw_location, exception.get());
+  }
   return found_dex_pc;
 }
 
diff --git a/runtime/mirror/art_method.h b/runtime/mirror/art_method.h
index fd5ac19..38e44be 100644
--- a/runtime/mirror/art_method.h
+++ b/runtime/mirror/art_method.h
@@ -418,7 +418,8 @@
   // Find the catch block for the given exception type and dex_pc. When a catch block is found,
   // indicates whether the found catch block is responsible for clearing the exception or whether
   // a move-exception instruction is present.
-  uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc, bool* has_no_move_exception)
+  uint32_t FindCatchBlock(SirtRef<Class>& exception_type, uint32_t dex_pc,
+                          bool* has_no_move_exception)
       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
   static void SetClass(Class* java_lang_reflect_ArtMethod);