Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "catch_block_stack_visitor.h" |
| 18 | |
| 19 | #include "dex_instruction.h" |
| 20 | #include "catch_finder.h" |
| 21 | #include "sirt_ref.h" |
| 22 | #include "verifier/method_verifier.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | bool CatchBlockStackVisitor::VisitFrame() { |
| 27 | catch_finder_->SetHandlerFrameId(GetFrameId()); |
| 28 | mirror::ArtMethod* method = GetMethod(); |
| 29 | if (method == nullptr) { |
| 30 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 31 | catch_finder_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 32 | catch_finder_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 33 | return false; // End stack walk. |
| 34 | } else { |
| 35 | if (method->IsRuntimeMethod()) { |
| 36 | // Ignore callee save method. |
| 37 | DCHECK(method->IsCalleeSaveMethod()); |
| 38 | return true; |
| 39 | } else if (is_deoptimization_) { |
| 40 | return HandleDeoptimization(method); |
| 41 | } else { |
| 42 | return HandleTryItems(method); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool CatchBlockStackVisitor::HandleTryItems(mirror::ArtMethod* method) { |
| 48 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 49 | if (method->IsNative()) { |
| 50 | ++native_method_count_; |
| 51 | } else { |
| 52 | dex_pc = GetDexPc(); |
| 53 | } |
| 54 | if (dex_pc != DexFile::kDexNoIndex) { |
| 55 | bool clear_exception = false; |
| 56 | uint32_t found_dex_pc = method->FindCatchBlock(to_find_, dex_pc, &clear_exception); |
| 57 | catch_finder_->SetClearException(clear_exception); |
| 58 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 59 | catch_finder_->SetHandlerDexPc(found_dex_pc); |
| 60 | catch_finder_->SetHandlerQuickFramePc(method->ToNativePc(found_dex_pc)); |
| 61 | catch_finder_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 62 | return false; // End stack walk. |
| 63 | } |
| 64 | } |
| 65 | return true; // Continue stack walk. |
| 66 | } |
| 67 | |
| 68 | bool CatchBlockStackVisitor::HandleDeoptimization(mirror::ArtMethod* m) { |
| 69 | MethodHelper mh(m); |
| 70 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
| 71 | CHECK(code_item != nullptr); |
| 72 | uint16_t num_regs = code_item->registers_size_; |
| 73 | uint32_t dex_pc = GetDexPc(); |
| 74 | const Instruction* inst = Instruction::At(code_item->insns_ + dex_pc); |
| 75 | uint32_t new_dex_pc = dex_pc + inst->SizeInCodeUnits(); |
| 76 | ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, m, new_dex_pc); |
| 77 | SirtRef<mirror::DexCache> dex_cache(self_, mh.GetDexCache()); |
| 78 | SirtRef<mirror::ClassLoader> class_loader(self_, mh.GetClassLoader()); |
| 79 | verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, |
| 80 | &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m, |
| 81 | m->GetAccessFlags(), false, true); |
| 82 | verifier.Verify(); |
| 83 | std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc); |
| 84 | for (uint16_t reg = 0; reg < num_regs; ++reg) { |
| 85 | VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2)); |
| 86 | switch (kind) { |
| 87 | case kUndefined: |
| 88 | new_frame->SetVReg(reg, 0xEBADDE09); |
| 89 | break; |
| 90 | case kConstant: |
| 91 | new_frame->SetVReg(reg, kinds.at((reg * 2) + 1)); |
| 92 | break; |
| 93 | case kReferenceVReg: |
| 94 | new_frame->SetVRegReference(reg, |
| 95 | reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind))); |
| 96 | break; |
| 97 | default: |
| 98 | new_frame->SetVReg(reg, GetVReg(m, reg, kind)); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | if (prev_shadow_frame_ != nullptr) { |
| 103 | prev_shadow_frame_->SetLink(new_frame); |
| 104 | } else { |
| 105 | catch_finder_->SetTopShadowFrame(new_frame); |
| 106 | } |
| 107 | prev_shadow_frame_ = new_frame; |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | } // namespace art |