blob: 103492334c2c9dbded82c3161fce1c32ff195c79 [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 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070074 StackHandleScope<1> hs(self_);
75 return HandleTryItems(hs.NewHandle(method));
Ian Rogers5cf98192014-05-29 21:31:50 -070076 }
77
78 private:
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070079 bool HandleTryItems(Handle<mirror::ArtMethod> method)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070081 uint32_t dex_pc = DexFile::kDexNoIndex;
82 if (!method->IsNative()) {
83 dex_pc = GetDexPc();
84 }
85 if (dex_pc != DexFile::kDexNoIndex) {
86 bool clear_exception = false;
87 StackHandleScope<1> hs(Thread::Current());
88 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070089 uint32_t found_dex_pc = mirror::ArtMethod::FindCatchBlock(method, to_find, dex_pc,
90 &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -070091 exception_handler_->SetClearException(clear_exception);
92 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070093 exception_handler_->SetHandlerMethod(method.Get());
Ian Rogers5cf98192014-05-29 21:31:50 -070094 exception_handler_->SetHandlerDexPc(found_dex_pc);
95 exception_handler_->SetHandlerQuickFramePc(method->ToNativePc(found_dex_pc));
96 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
97 return false; // End stack walk.
98 }
99 }
100 return true; // Continue stack walk.
101 }
102
103 Thread* const self_;
104 // The exception we're looking for the catch block of.
105 Handle<mirror::Throwable>* exception_;
106 // The quick exception handler we're visiting for.
107 QuickExceptionHandler* const exception_handler_;
108
109 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
110};
111
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200112void QuickExceptionHandler::FindCatch(const ThrowLocation& throw_location,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200113 mirror::Throwable* exception,
114 bool is_exception_reported) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200115 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700116 if (kDebugExceptionDelivery) {
117 mirror::String* msg = exception->GetDetailMessage();
118 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
119 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
120 << ": " << str_msg << "\n");
121 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 StackHandleScope<1> hs(self_);
123 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200124
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100125 // Walk the stack to find catch handler or prepare for deoptimization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700126 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100127 visitor.WalkStack(true);
128
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200129 if (kDebugExceptionDelivery) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700130 if (handler_quick_frame_->AsMirrorPtr() == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100131 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700132 }
133 if (handler_method_ != nullptr) {
134 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
135 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
136 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100137 }
138 }
139 if (clear_exception_) {
140 // Exception was cleared as part of delivery.
141 DCHECK(!self_->IsExceptionPending());
142 } else {
143 // Put exception back in root set with clear throw location.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 self_->SetException(ThrowLocation(), exception_ref.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200145 self_->SetExceptionReportedToInstrumentation(is_exception_reported);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100146 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200147 // The debugger may suspend this thread and walk its stack. Let's do this before popping
148 // instrumentation frames.
Sebastien Hertz9f102032014-05-23 08:59:42 +0200149 if (!is_exception_reported) {
150 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
151 instrumentation->ExceptionCaughtEvent(self_, throw_location, handler_method_, handler_dex_pc_,
152 exception_ref.Get());
153 // We're not catching this exception but let's remind we already reported the exception above
154 // to avoid reporting it twice.
155 self_->SetExceptionReportedToInstrumentation(true);
156 }
157 bool caught_exception = (handler_method_ != nullptr && handler_dex_pc_ != DexFile::kDexNoIndex);
158 if (caught_exception) {
159 // We're catching this exception so we finish reporting it. We do it here to avoid doing it
160 // in the compiled code.
161 self_->SetExceptionReportedToInstrumentation(false);
162 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200163}
164
Ian Rogers5cf98192014-05-29 21:31:50 -0700165// Prepares deoptimization.
166class DeoptimizeStackVisitor FINAL : public StackVisitor {
167 public:
168 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
170 : StackVisitor(self, context), self_(self), exception_handler_(exception_handler),
171 prev_shadow_frame_(nullptr) {
172 CHECK(!self_->HasDeoptimizationShadowFrame());
173 }
174
175 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
176 exception_handler_->SetHandlerFrameId(GetFrameId());
177 mirror::ArtMethod* method = GetMethod();
178 if (method == nullptr) {
179 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
180 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
181 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
182 return false; // End stack walk.
183 } else if (method->IsRuntimeMethod()) {
184 // Ignore callee save method.
185 DCHECK(method->IsCalleeSaveMethod());
186 return true;
187 } else {
188 return HandleDeoptimization(method);
189 }
190 }
191
192 private:
193 bool HandleDeoptimization(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700194 const DexFile::CodeItem* code_item = m->GetCodeItem();
Ian Rogers5cf98192014-05-29 21:31:50 -0700195 CHECK(code_item != nullptr);
196 uint16_t num_regs = code_item->registers_size_;
197 uint32_t dex_pc = GetDexPc();
198 const Instruction* inst = Instruction::At(code_item->insns_ + dex_pc);
199 uint32_t new_dex_pc = dex_pc + inst->SizeInCodeUnits();
200 ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, m, new_dex_pc);
201 StackHandleScope<2> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700202 mirror::Class* declaring_class = m->GetDeclaringClass();
203 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
204 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
205 verifier::MethodVerifier verifier(h_dex_cache->GetDexFile(), &h_dex_cache, &h_class_loader,
206 &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m,
Ian Rogers5cf98192014-05-29 21:31:50 -0700207 m->GetAccessFlags(), false, true, true);
208 verifier.Verify();
209 std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc);
210 for (uint16_t reg = 0; reg < num_regs; ++reg) {
211 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
212 switch (kind) {
213 case kUndefined:
214 new_frame->SetVReg(reg, 0xEBADDE09);
215 break;
216 case kConstant:
217 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
218 break;
219 case kReferenceVReg:
220 new_frame->SetVRegReference(reg,
221 reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind)));
222 break;
223 default:
224 new_frame->SetVReg(reg, GetVReg(m, reg, kind));
225 break;
226 }
227 }
228 if (prev_shadow_frame_ != nullptr) {
229 prev_shadow_frame_->SetLink(new_frame);
230 } else {
231 self_->SetDeoptimizationShadowFrame(new_frame);
232 }
233 prev_shadow_frame_ = new_frame;
234 return true;
235 }
236
237 Thread* const self_;
238 QuickExceptionHandler* const exception_handler_;
239 ShadowFrame* prev_shadow_frame_;
240
241 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
242};
243
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200244void QuickExceptionHandler::DeoptimizeStack() {
245 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700246 if (kDebugExceptionDelivery) {
247 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
248 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200249
250 DeoptimizeStackVisitor visitor(self_, context_, this);
251 visitor.WalkStack(true);
252
253 // Restore deoptimization exception
254 self_->SetException(ThrowLocation(), Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100255}
256
257// Unwinds all instrumentation stack frame prior to catch handler or upcall.
258class InstrumentationStackVisitor : public StackVisitor {
259 public:
260 InstrumentationStackVisitor(Thread* self, bool is_deoptimization, size_t frame_id)
261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
262 : StackVisitor(self, nullptr),
263 self_(self), frame_id_(frame_id),
264 instrumentation_frames_to_pop_(0) {
265 CHECK_NE(frame_id_, kInvalidFrameId);
266 }
267
268 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
269 size_t current_frame_id = GetFrameId();
270 if (current_frame_id > frame_id_) {
271 CHECK(GetMethod() != nullptr);
272 if (UNLIKELY(GetQuickInstrumentationExitPc() == GetReturnPc())) {
273 ++instrumentation_frames_to_pop_;
274 }
275 return true;
276 } else {
277 // We reached the frame of the catch handler or the upcall.
278 return false;
279 }
280 }
281
282 size_t GetInstrumentationFramesToPop() const {
283 return instrumentation_frames_to_pop_;
284 }
285
286 private:
287 Thread* const self_;
288 const size_t frame_id_;
289 size_t instrumentation_frames_to_pop_;
290
291 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
292};
293
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200294void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100295 if (method_tracing_active_) {
296 InstrumentationStackVisitor visitor(self_, is_deoptimization_, handler_frame_id_);
297 visitor.WalkStack(true);
298
299 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
300 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
301 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
302 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
303 }
304 }
305}
306
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200307void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100308 // Place context back on thread so it will be available when we continue.
309 self_->ReleaseLongJumpContext(context_);
310 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
311 CHECK_NE(handler_quick_frame_pc_, 0u);
312 context_->SetPC(handler_quick_frame_pc_);
313 context_->SmashCallerSaves();
314 context_->DoLongJump();
315}
316
317} // namespace art