blob: 410fff9f96103c4bbecaadeec3ef7c599c39d9ae [file] [log] [blame]
Sebastien Hertzd45a1f52014-01-09 14:56:54 +01001/*
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
24namespace art {
25
26bool 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
47bool 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;
Jeff Haoaa961912014-04-22 13:54:32 -070056 SirtRef<mirror::Class> sirt_method_to_find(Thread::Current(), to_find_);
57 uint32_t found_dex_pc = method->FindCatchBlock(sirt_method_to_find, dex_pc, &clear_exception);
58 to_find_ = sirt_method_to_find.get();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010059 catch_finder_->SetClearException(clear_exception);
60 if (found_dex_pc != DexFile::kDexNoIndex) {
61 catch_finder_->SetHandlerDexPc(found_dex_pc);
62 catch_finder_->SetHandlerQuickFramePc(method->ToNativePc(found_dex_pc));
63 catch_finder_->SetHandlerQuickFrame(GetCurrentQuickFrame());
64 return false; // End stack walk.
65 }
66 }
67 return true; // Continue stack walk.
68}
69
70bool CatchBlockStackVisitor::HandleDeoptimization(mirror::ArtMethod* m) {
71 MethodHelper mh(m);
72 const DexFile::CodeItem* code_item = mh.GetCodeItem();
73 CHECK(code_item != nullptr);
74 uint16_t num_regs = code_item->registers_size_;
75 uint32_t dex_pc = GetDexPc();
76 const Instruction* inst = Instruction::At(code_item->insns_ + dex_pc);
77 uint32_t new_dex_pc = dex_pc + inst->SizeInCodeUnits();
78 ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, m, new_dex_pc);
79 SirtRef<mirror::DexCache> dex_cache(self_, mh.GetDexCache());
80 SirtRef<mirror::ClassLoader> class_loader(self_, mh.GetClassLoader());
81 verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader,
82 &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m,
83 m->GetAccessFlags(), false, true);
84 verifier.Verify();
85 std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc);
86 for (uint16_t reg = 0; reg < num_regs; ++reg) {
87 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
88 switch (kind) {
89 case kUndefined:
90 new_frame->SetVReg(reg, 0xEBADDE09);
91 break;
92 case kConstant:
93 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
94 break;
95 case kReferenceVReg:
96 new_frame->SetVRegReference(reg,
97 reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind)));
98 break;
99 default:
100 new_frame->SetVReg(reg, GetVReg(m, reg, kind));
101 break;
102 }
103 }
104 if (prev_shadow_frame_ != nullptr) {
105 prev_shadow_frame_->SetLink(new_frame);
106 } else {
107 catch_finder_->SetTopShadowFrame(new_frame);
108 }
109 prev_shadow_frame_ = new_frame;
110 return true;
111}
112
113} // namespace art