blob: 49f6fe015ebf807192630ce7193de94737453be4 [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "mirror/class-inl.h"
24#include "mirror/class_loader.h"
25#include "mirror/throwable.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070026#include "verifier/method_verifier.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010027
28namespace art {
29
Ian Rogers5cf98192014-05-29 21:31:50 -070030static constexpr bool kDebugExceptionDelivery = false;
31static constexpr size_t kInvalidFrameId = 0xffffffff;
32
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020033QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
34 : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010035 method_tracing_active_(is_deoptimization ||
36 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
Ian Rogers5cf98192014-05-29 21:31:50 -070037 handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_method_(nullptr),
38 handler_dex_pc_(0), clear_exception_(false), handler_frame_id_(kInvalidFrameId) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010039}
40
Ian Rogers5cf98192014-05-29 21:31:50 -070041// Finds catch handler or prepares for deoptimization.
42class CatchBlockStackVisitor FINAL : public StackVisitor {
43 public:
44 CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception,
45 QuickExceptionHandler* exception_handler)
46 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
47 : StackVisitor(self, context), self_(self), exception_(exception),
48 exception_handler_(exception_handler) {
49 }
50
51 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
52 mirror::ArtMethod* method = GetMethod();
53 exception_handler_->SetHandlerFrameId(GetFrameId());
54 if (method == nullptr) {
55 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
56 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
57 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
58 uint32_t next_dex_pc;
59 mirror::ArtMethod* next_art_method;
60 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
61 // Report the method that did the down call as the handler.
62 exception_handler_->SetHandlerDexPc(next_dex_pc);
63 exception_handler_->SetHandlerMethod(next_art_method);
64 if (!has_next) {
65 // No next method? Check exception handler is set up for the unhandled exception handler
66 // case.
67 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
68 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
69 }
70 return false; // End stack walk.
71 }
72 if (method->IsRuntimeMethod()) {
73 // Ignore callee save method.
74 DCHECK(method->IsCalleeSaveMethod());
75 return true;
76 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070077 StackHandleScope<1> hs(self_);
78 return HandleTryItems(hs.NewHandle(method));
Ian Rogers5cf98192014-05-29 21:31:50 -070079 }
80
81 private:
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070082 bool HandleTryItems(Handle<mirror::ArtMethod> method)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070084 uint32_t dex_pc = DexFile::kDexNoIndex;
85 if (!method->IsNative()) {
86 dex_pc = GetDexPc();
87 }
88 if (dex_pc != DexFile::kDexNoIndex) {
89 bool clear_exception = false;
90 StackHandleScope<1> hs(Thread::Current());
91 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070092 uint32_t found_dex_pc = mirror::ArtMethod::FindCatchBlock(method, to_find, dex_pc,
93 &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -070094 exception_handler_->SetClearException(clear_exception);
95 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070096 exception_handler_->SetHandlerMethod(method.Get());
Ian Rogers5cf98192014-05-29 21:31:50 -070097 exception_handler_->SetHandlerDexPc(found_dex_pc);
98 exception_handler_->SetHandlerQuickFramePc(method->ToNativePc(found_dex_pc));
99 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
100 return false; // End stack walk.
101 }
102 }
103 return true; // Continue stack walk.
104 }
105
106 Thread* const self_;
107 // The exception we're looking for the catch block of.
108 Handle<mirror::Throwable>* exception_;
109 // The quick exception handler we're visiting for.
110 QuickExceptionHandler* const exception_handler_;
111
112 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
113};
114
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200115void QuickExceptionHandler::FindCatch(const ThrowLocation& throw_location,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200116 mirror::Throwable* exception,
117 bool is_exception_reported) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200118 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700119 if (kDebugExceptionDelivery) {
120 mirror::String* msg = exception->GetDetailMessage();
121 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
122 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
123 << ": " << str_msg << "\n");
124 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700125 StackHandleScope<1> hs(self_);
126 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200127
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100128 // Walk the stack to find catch handler or prepare for deoptimization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100130 visitor.WalkStack(true);
131
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200132 if (kDebugExceptionDelivery) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700133 if (handler_quick_frame_->AsMirrorPtr() == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100134 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700135 }
136 if (handler_method_ != nullptr) {
137 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
138 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
139 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100140 }
141 }
142 if (clear_exception_) {
143 // Exception was cleared as part of delivery.
144 DCHECK(!self_->IsExceptionPending());
145 } else {
146 // Put exception back in root set with clear throw location.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 self_->SetException(ThrowLocation(), exception_ref.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200148 self_->SetExceptionReportedToInstrumentation(is_exception_reported);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100149 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200150 // The debugger may suspend this thread and walk its stack. Let's do this before popping
151 // instrumentation frames.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200152 if (!is_exception_reported) {
153 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
154 instrumentation->ExceptionCaughtEvent(self_, throw_location, handler_method_, handler_dex_pc_,
155 exception_ref.Get());
156 // We're not catching this exception but let's remind we already reported the exception above
157 // to avoid reporting it twice.
158 self_->SetExceptionReportedToInstrumentation(true);
159 }
160 bool caught_exception = (handler_method_ != nullptr && handler_dex_pc_ != DexFile::kDexNoIndex);
161 if (caught_exception) {
162 // We're catching this exception so we finish reporting it. We do it here to avoid doing it
163 // in the compiled code.
164 self_->SetExceptionReportedToInstrumentation(false);
165 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200166}
167
Ian Rogers5cf98192014-05-29 21:31:50 -0700168// Prepares deoptimization.
169class DeoptimizeStackVisitor FINAL : public StackVisitor {
170 public:
171 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
173 : StackVisitor(self, context), self_(self), exception_handler_(exception_handler),
174 prev_shadow_frame_(nullptr) {
175 CHECK(!self_->HasDeoptimizationShadowFrame());
176 }
177
178 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
179 exception_handler_->SetHandlerFrameId(GetFrameId());
180 mirror::ArtMethod* method = GetMethod();
181 if (method == nullptr) {
182 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
183 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
184 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
185 return false; // End stack walk.
186 } else if (method->IsRuntimeMethod()) {
187 // Ignore callee save method.
188 DCHECK(method->IsCalleeSaveMethod());
189 return true;
190 } else {
191 return HandleDeoptimization(method);
192 }
193 }
194
195 private:
196 bool HandleDeoptimization(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700197 const DexFile::CodeItem* code_item = m->GetCodeItem();
Ian Rogers5cf98192014-05-29 21:31:50 -0700198 CHECK(code_item != nullptr);
199 uint16_t num_regs = code_item->registers_size_;
200 uint32_t dex_pc = GetDexPc();
201 const Instruction* inst = Instruction::At(code_item->insns_ + dex_pc);
202 uint32_t new_dex_pc = dex_pc + inst->SizeInCodeUnits();
203 ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, m, new_dex_pc);
204 StackHandleScope<2> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700205 mirror::Class* declaring_class = m->GetDeclaringClass();
206 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
207 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
208 verifier::MethodVerifier verifier(h_dex_cache->GetDexFile(), &h_dex_cache, &h_class_loader,
209 &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m,
Ian Rogers5cf98192014-05-29 21:31:50 -0700210 m->GetAccessFlags(), false, true, true);
211 verifier.Verify();
212 std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc);
213 for (uint16_t reg = 0; reg < num_regs; ++reg) {
214 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
215 switch (kind) {
216 case kUndefined:
217 new_frame->SetVReg(reg, 0xEBADDE09);
218 break;
219 case kConstant:
220 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
221 break;
222 case kReferenceVReg:
223 new_frame->SetVRegReference(reg,
224 reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind)));
225 break;
226 default:
227 new_frame->SetVReg(reg, GetVReg(m, reg, kind));
228 break;
229 }
230 }
231 if (prev_shadow_frame_ != nullptr) {
232 prev_shadow_frame_->SetLink(new_frame);
233 } else {
234 self_->SetDeoptimizationShadowFrame(new_frame);
235 }
236 prev_shadow_frame_ = new_frame;
237 return true;
238 }
239
240 Thread* const self_;
241 QuickExceptionHandler* const exception_handler_;
242 ShadowFrame* prev_shadow_frame_;
243
244 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
245};
246
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200247void QuickExceptionHandler::DeoptimizeStack() {
248 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700249 if (kDebugExceptionDelivery) {
250 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
251 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200252
253 DeoptimizeStackVisitor visitor(self_, context_, this);
254 visitor.WalkStack(true);
255
256 // Restore deoptimization exception
257 self_->SetException(ThrowLocation(), Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100258}
259
260// Unwinds all instrumentation stack frame prior to catch handler or upcall.
261class InstrumentationStackVisitor : public StackVisitor {
262 public:
263 InstrumentationStackVisitor(Thread* self, bool is_deoptimization, size_t frame_id)
264 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
265 : StackVisitor(self, nullptr),
266 self_(self), frame_id_(frame_id),
267 instrumentation_frames_to_pop_(0) {
268 CHECK_NE(frame_id_, kInvalidFrameId);
269 }
270
271 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
272 size_t current_frame_id = GetFrameId();
273 if (current_frame_id > frame_id_) {
274 CHECK(GetMethod() != nullptr);
275 if (UNLIKELY(GetQuickInstrumentationExitPc() == GetReturnPc())) {
276 ++instrumentation_frames_to_pop_;
277 }
278 return true;
279 } else {
280 // We reached the frame of the catch handler or the upcall.
281 return false;
282 }
283 }
284
285 size_t GetInstrumentationFramesToPop() const {
286 return instrumentation_frames_to_pop_;
287 }
288
289 private:
290 Thread* const self_;
291 const size_t frame_id_;
292 size_t instrumentation_frames_to_pop_;
293
294 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
295};
296
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200297void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100298 if (method_tracing_active_) {
299 InstrumentationStackVisitor visitor(self_, is_deoptimization_, handler_frame_id_);
300 visitor.WalkStack(true);
301
302 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
303 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
304 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
305 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
306 }
307 }
308}
309
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200310void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100311 // Place context back on thread so it will be available when we continue.
312 self_->ReleaseLongJumpContext(context_);
313 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
314 CHECK_NE(handler_quick_frame_pc_, 0u);
315 context_->SetPC(handler_quick_frame_pc_);
316 context_->SmashCallerSaves();
317 context_->DoLongJump();
318}
319
320} // namespace art