blob: b9cec40ebfe4c2fe24755bc5858526d64c6067e4 [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
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020017#include "quick_exception_handler.h"
18
Ian Rogers5cf98192014-05-29 21:31:50 -070019#include "dex_instruction.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020020#include "entrypoints/entrypoint_utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070021#include "handle_scope-inl.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070022#include "mirror/art_method-inl.h"
23#include "verifier/method_verifier.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010024
25namespace art {
26
Ian Rogers5cf98192014-05-29 21:31:50 -070027static constexpr bool kDebugExceptionDelivery = false;
28static constexpr size_t kInvalidFrameId = 0xffffffff;
29
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020030QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
31 : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010032 method_tracing_active_(is_deoptimization ||
33 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
Ian Rogers5cf98192014-05-29 21:31:50 -070034 handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_method_(nullptr),
35 handler_dex_pc_(0), clear_exception_(false), handler_frame_id_(kInvalidFrameId) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010036}
37
Ian Rogers5cf98192014-05-29 21:31:50 -070038// Finds catch handler or prepares for deoptimization.
39class CatchBlockStackVisitor FINAL : public StackVisitor {
40 public:
41 CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception,
42 QuickExceptionHandler* exception_handler)
43 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
44 : StackVisitor(self, context), self_(self), exception_(exception),
45 exception_handler_(exception_handler) {
46 }
47
48 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
49 mirror::ArtMethod* method = GetMethod();
50 exception_handler_->SetHandlerFrameId(GetFrameId());
51 if (method == nullptr) {
52 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
53 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
54 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
55 uint32_t next_dex_pc;
56 mirror::ArtMethod* next_art_method;
57 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
58 // Report the method that did the down call as the handler.
59 exception_handler_->SetHandlerDexPc(next_dex_pc);
60 exception_handler_->SetHandlerMethod(next_art_method);
61 if (!has_next) {
62 // No next method? Check exception handler is set up for the unhandled exception handler
63 // case.
64 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
65 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
66 }
67 return false; // End stack walk.
68 }
69 if (method->IsRuntimeMethod()) {
70 // Ignore callee save method.
71 DCHECK(method->IsCalleeSaveMethod());
72 return true;
73 }
74 return HandleTryItems(method);
75 }
76
77 private:
78 bool HandleTryItems(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
79 uint32_t dex_pc = DexFile::kDexNoIndex;
80 if (!method->IsNative()) {
81 dex_pc = GetDexPc();
82 }
83 if (dex_pc != DexFile::kDexNoIndex) {
84 bool clear_exception = false;
85 StackHandleScope<1> hs(Thread::Current());
86 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
87 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
88 exception_handler_->SetClearException(clear_exception);
89 if (found_dex_pc != DexFile::kDexNoIndex) {
90 exception_handler_->SetHandlerMethod(method);
91 exception_handler_->SetHandlerDexPc(found_dex_pc);
92 exception_handler_->SetHandlerQuickFramePc(method->ToNativePc(found_dex_pc));
93 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
94 return false; // End stack walk.
95 }
96 }
97 return true; // Continue stack walk.
98 }
99
100 Thread* const self_;
101 // The exception we're looking for the catch block of.
102 Handle<mirror::Throwable>* exception_;
103 // The quick exception handler we're visiting for.
104 QuickExceptionHandler* const exception_handler_;
105
106 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
107};
108
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200109void QuickExceptionHandler::FindCatch(const ThrowLocation& throw_location,
110 mirror::Throwable* exception) {
111 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700112 if (kDebugExceptionDelivery) {
113 mirror::String* msg = exception->GetDetailMessage();
114 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
115 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
116 << ": " << str_msg << "\n");
117 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 StackHandleScope<1> hs(self_);
119 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200120
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100121 // Walk the stack to find catch handler or prepare for deoptimization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100123 visitor.WalkStack(true);
124
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200125 if (kDebugExceptionDelivery) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700126 if (handler_quick_frame_->AsMirrorPtr() == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100127 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700128 }
129 if (handler_method_ != nullptr) {
130 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
131 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
132 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100133 }
134 }
135 if (clear_exception_) {
136 // Exception was cleared as part of delivery.
137 DCHECK(!self_->IsExceptionPending());
138 } else {
139 // Put exception back in root set with clear throw location.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 self_->SetException(ThrowLocation(), exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100141 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200142 // The debugger may suspend this thread and walk its stack. Let's do this before popping
143 // instrumentation frames.
144 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Ian Rogers5cf98192014-05-29 21:31:50 -0700145 instrumentation->ExceptionCaughtEvent(self_, throw_location, handler_method_, handler_dex_pc_,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 exception_ref.Get());
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200147}
148
Ian Rogers5cf98192014-05-29 21:31:50 -0700149// Prepares deoptimization.
150class DeoptimizeStackVisitor FINAL : public StackVisitor {
151 public:
152 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
154 : StackVisitor(self, context), self_(self), exception_handler_(exception_handler),
155 prev_shadow_frame_(nullptr) {
156 CHECK(!self_->HasDeoptimizationShadowFrame());
157 }
158
159 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
160 exception_handler_->SetHandlerFrameId(GetFrameId());
161 mirror::ArtMethod* method = GetMethod();
162 if (method == nullptr) {
163 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
164 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
165 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
166 return false; // End stack walk.
167 } else if (method->IsRuntimeMethod()) {
168 // Ignore callee save method.
169 DCHECK(method->IsCalleeSaveMethod());
170 return true;
171 } else {
172 return HandleDeoptimization(method);
173 }
174 }
175
176 private:
177 bool HandleDeoptimization(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
178 MethodHelper mh(m);
179 const DexFile::CodeItem* code_item = mh.GetCodeItem();
180 CHECK(code_item != nullptr);
181 uint16_t num_regs = code_item->registers_size_;
182 uint32_t dex_pc = GetDexPc();
183 const Instruction* inst = Instruction::At(code_item->insns_ + dex_pc);
184 uint32_t new_dex_pc = dex_pc + inst->SizeInCodeUnits();
185 ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, m, new_dex_pc);
186 StackHandleScope<2> hs(self_);
187 Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
188 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
189 verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader,
190 &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m,
191 m->GetAccessFlags(), false, true, true);
192 verifier.Verify();
193 std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc);
194 for (uint16_t reg = 0; reg < num_regs; ++reg) {
195 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
196 switch (kind) {
197 case kUndefined:
198 new_frame->SetVReg(reg, 0xEBADDE09);
199 break;
200 case kConstant:
201 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
202 break;
203 case kReferenceVReg:
204 new_frame->SetVRegReference(reg,
205 reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind)));
206 break;
207 default:
208 new_frame->SetVReg(reg, GetVReg(m, reg, kind));
209 break;
210 }
211 }
212 if (prev_shadow_frame_ != nullptr) {
213 prev_shadow_frame_->SetLink(new_frame);
214 } else {
215 self_->SetDeoptimizationShadowFrame(new_frame);
216 }
217 prev_shadow_frame_ = new_frame;
218 return true;
219 }
220
221 Thread* const self_;
222 QuickExceptionHandler* const exception_handler_;
223 ShadowFrame* prev_shadow_frame_;
224
225 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
226};
227
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200228void QuickExceptionHandler::DeoptimizeStack() {
229 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700230 if (kDebugExceptionDelivery) {
231 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
232 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200233
234 DeoptimizeStackVisitor visitor(self_, context_, this);
235 visitor.WalkStack(true);
236
237 // Restore deoptimization exception
238 self_->SetException(ThrowLocation(), Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100239}
240
241// Unwinds all instrumentation stack frame prior to catch handler or upcall.
242class InstrumentationStackVisitor : public StackVisitor {
243 public:
244 InstrumentationStackVisitor(Thread* self, bool is_deoptimization, size_t frame_id)
245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
246 : StackVisitor(self, nullptr),
247 self_(self), frame_id_(frame_id),
248 instrumentation_frames_to_pop_(0) {
249 CHECK_NE(frame_id_, kInvalidFrameId);
250 }
251
252 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
253 size_t current_frame_id = GetFrameId();
254 if (current_frame_id > frame_id_) {
255 CHECK(GetMethod() != nullptr);
256 if (UNLIKELY(GetQuickInstrumentationExitPc() == GetReturnPc())) {
257 ++instrumentation_frames_to_pop_;
258 }
259 return true;
260 } else {
261 // We reached the frame of the catch handler or the upcall.
262 return false;
263 }
264 }
265
266 size_t GetInstrumentationFramesToPop() const {
267 return instrumentation_frames_to_pop_;
268 }
269
270 private:
271 Thread* const self_;
272 const size_t frame_id_;
273 size_t instrumentation_frames_to_pop_;
274
275 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
276};
277
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200278void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100279 if (method_tracing_active_) {
280 InstrumentationStackVisitor visitor(self_, is_deoptimization_, handler_frame_id_);
281 visitor.WalkStack(true);
282
283 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
284 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
285 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
286 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
287 }
288 }
289}
290
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200291void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100292 // Place context back on thread so it will be available when we continue.
293 self_->ReleaseLongJumpContext(context_);
294 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
295 CHECK_NE(handler_quick_frame_pc_, 0u);
296 context_->SetPC(handler_quick_frame_pc_);
297 context_->SmashCallerSaves();
298 context_->DoLongJump();
299}
300
301} // namespace art