blob: e3f9afc7872ae3fe14771bfc16b6f04ad5730b09 [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,
113 mirror::Throwable* exception) {
114 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700115 if (kDebugExceptionDelivery) {
116 mirror::String* msg = exception->GetDetailMessage();
117 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
118 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
119 << ": " << str_msg << "\n");
120 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121 StackHandleScope<1> hs(self_);
122 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200123
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100124 // Walk the stack to find catch handler or prepare for deoptimization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700125 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100126 visitor.WalkStack(true);
127
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200128 if (kDebugExceptionDelivery) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700129 if (handler_quick_frame_->AsMirrorPtr() == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100130 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700131 }
132 if (handler_method_ != nullptr) {
133 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
134 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
135 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100136 }
137 }
138 if (clear_exception_) {
139 // Exception was cleared as part of delivery.
140 DCHECK(!self_->IsExceptionPending());
141 } else {
142 // Put exception back in root set with clear throw location.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700143 self_->SetException(ThrowLocation(), exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100144 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200145 // The debugger may suspend this thread and walk its stack. Let's do this before popping
146 // instrumentation frames.
147 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Ian Rogers5cf98192014-05-29 21:31:50 -0700148 instrumentation->ExceptionCaughtEvent(self_, throw_location, handler_method_, handler_dex_pc_,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700149 exception_ref.Get());
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200150}
151
Ian Rogers5cf98192014-05-29 21:31:50 -0700152// Prepares deoptimization.
153class DeoptimizeStackVisitor FINAL : public StackVisitor {
154 public:
155 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
157 : StackVisitor(self, context), self_(self), exception_handler_(exception_handler),
158 prev_shadow_frame_(nullptr) {
159 CHECK(!self_->HasDeoptimizationShadowFrame());
160 }
161
162 bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
163 exception_handler_->SetHandlerFrameId(GetFrameId());
164 mirror::ArtMethod* method = GetMethod();
165 if (method == nullptr) {
166 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
167 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
168 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
169 return false; // End stack walk.
170 } else if (method->IsRuntimeMethod()) {
171 // Ignore callee save method.
172 DCHECK(method->IsCalleeSaveMethod());
173 return true;
174 } else {
175 return HandleDeoptimization(method);
176 }
177 }
178
179 private:
180 bool HandleDeoptimization(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700181 const DexFile::CodeItem* code_item = m->GetCodeItem();
Ian Rogers5cf98192014-05-29 21:31:50 -0700182 CHECK(code_item != nullptr);
183 uint16_t num_regs = code_item->registers_size_;
184 uint32_t dex_pc = GetDexPc();
185 const Instruction* inst = Instruction::At(code_item->insns_ + dex_pc);
186 uint32_t new_dex_pc = dex_pc + inst->SizeInCodeUnits();
187 ShadowFrame* new_frame = ShadowFrame::Create(num_regs, nullptr, m, new_dex_pc);
188 StackHandleScope<2> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700189 mirror::Class* declaring_class = m->GetDeclaringClass();
190 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
191 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
192 verifier::MethodVerifier verifier(h_dex_cache->GetDexFile(), &h_dex_cache, &h_class_loader,
193 &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m,
Ian Rogers5cf98192014-05-29 21:31:50 -0700194 m->GetAccessFlags(), false, true, true);
195 verifier.Verify();
196 std::vector<int32_t> kinds = verifier.DescribeVRegs(dex_pc);
197 for (uint16_t reg = 0; reg < num_regs; ++reg) {
198 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
199 switch (kind) {
200 case kUndefined:
201 new_frame->SetVReg(reg, 0xEBADDE09);
202 break;
203 case kConstant:
204 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
205 break;
206 case kReferenceVReg:
207 new_frame->SetVRegReference(reg,
208 reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kind)));
209 break;
210 default:
211 new_frame->SetVReg(reg, GetVReg(m, reg, kind));
212 break;
213 }
214 }
215 if (prev_shadow_frame_ != nullptr) {
216 prev_shadow_frame_->SetLink(new_frame);
217 } else {
218 self_->SetDeoptimizationShadowFrame(new_frame);
219 }
220 prev_shadow_frame_ = new_frame;
221 return true;
222 }
223
224 Thread* const self_;
225 QuickExceptionHandler* const exception_handler_;
226 ShadowFrame* prev_shadow_frame_;
227
228 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
229};
230
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200231void QuickExceptionHandler::DeoptimizeStack() {
232 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700233 if (kDebugExceptionDelivery) {
234 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
235 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200236
237 DeoptimizeStackVisitor visitor(self_, context_, this);
238 visitor.WalkStack(true);
239
240 // Restore deoptimization exception
241 self_->SetException(ThrowLocation(), Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100242}
243
244// Unwinds all instrumentation stack frame prior to catch handler or upcall.
245class InstrumentationStackVisitor : public StackVisitor {
246 public:
247 InstrumentationStackVisitor(Thread* self, bool is_deoptimization, size_t frame_id)
248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
249 : StackVisitor(self, nullptr),
250 self_(self), frame_id_(frame_id),
251 instrumentation_frames_to_pop_(0) {
252 CHECK_NE(frame_id_, kInvalidFrameId);
253 }
254
255 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
256 size_t current_frame_id = GetFrameId();
257 if (current_frame_id > frame_id_) {
258 CHECK(GetMethod() != nullptr);
259 if (UNLIKELY(GetQuickInstrumentationExitPc() == GetReturnPc())) {
260 ++instrumentation_frames_to_pop_;
261 }
262 return true;
263 } else {
264 // We reached the frame of the catch handler or the upcall.
265 return false;
266 }
267 }
268
269 size_t GetInstrumentationFramesToPop() const {
270 return instrumentation_frames_to_pop_;
271 }
272
273 private:
274 Thread* const self_;
275 const size_t frame_id_;
276 size_t instrumentation_frames_to_pop_;
277
278 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
279};
280
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200281void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100282 if (method_tracing_active_) {
283 InstrumentationStackVisitor visitor(self_, is_deoptimization_, handler_frame_id_);
284 visitor.WalkStack(true);
285
286 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
287 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
288 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
289 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
290 }
291 }
292}
293
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200294void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100295 // Place context back on thread so it will be available when we continue.
296 self_->ReleaseLongJumpContext(context_);
297 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
298 CHECK_NE(handler_quick_frame_pc_, 0u);
299 context_->SetPC(handler_quick_frame_pc_);
300 context_->SmashCallerSaves();
301 context_->DoLongJump();
302}
303
304} // namespace art