blob: c905b63c6c136ccc389f04b41cea05d32d5911c1 [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 Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070021#include "dex_instruction.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020022#include "entrypoints/entrypoint_utils.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070023#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070024#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "mirror/class-inl.h"
26#include "mirror/class_loader.h"
27#include "mirror/throwable.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070028#include "verifier/method_verifier.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010029
30namespace art {
31
Ian Rogers5cf98192014-05-29 21:31:50 -070032static constexpr bool kDebugExceptionDelivery = false;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070033static constexpr size_t kInvalidFrameDepth = 0xffffffff;
Ian Rogers5cf98192014-05-29 21:31:50 -070034
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020035QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
36 : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010037 method_tracing_active_(is_deoptimization ||
38 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
Ian Rogers5cf98192014-05-29 21:31:50 -070039 handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_method_(nullptr),
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070040 handler_dex_pc_(0), clear_exception_(false), handler_frame_depth_(kInvalidFrameDepth) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010041}
42
Sebastien Hertz520633b2015-09-08 17:03:36 +020043// Finds catch handler.
Ian Rogers5cf98192014-05-29 21:31:50 -070044class CatchBlockStackVisitor FINAL : public StackVisitor {
45 public:
46 CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception,
47 QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -070048 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010049 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
50 self_(self),
51 exception_(exception),
Ian Rogers5cf98192014-05-29 21:31:50 -070052 exception_handler_(exception_handler) {
53 }
54
Mathieu Chartier90443472015-07-16 20:32:27 -070055 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070056 ArtMethod* method = GetMethod();
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070057 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -070058 if (method == nullptr) {
59 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
60 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
61 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
62 uint32_t next_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 ArtMethod* next_art_method;
Ian Rogers5cf98192014-05-29 21:31:50 -070064 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
65 // Report the method that did the down call as the handler.
66 exception_handler_->SetHandlerDexPc(next_dex_pc);
67 exception_handler_->SetHandlerMethod(next_art_method);
68 if (!has_next) {
69 // No next method? Check exception handler is set up for the unhandled exception handler
70 // case.
71 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
72 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
73 }
74 return false; // End stack walk.
75 }
76 if (method->IsRuntimeMethod()) {
77 // Ignore callee save method.
78 DCHECK(method->IsCalleeSaveMethod());
79 return true;
80 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070081 return HandleTryItems(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070082 }
83
84 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -070085 bool HandleTryItems(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070086 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070087 uint32_t dex_pc = DexFile::kDexNoIndex;
88 if (!method->IsNative()) {
89 dex_pc = GetDexPc();
90 }
91 if (dex_pc != DexFile::kDexNoIndex) {
92 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 StackHandleScope<1> hs(self_);
Ian Rogers5cf98192014-05-29 21:31:50 -070094 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -070095 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -070096 exception_handler_->SetClearException(clear_exception);
97 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070098 exception_handler_->SetHandlerMethod(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070099 exception_handler_->SetHandlerDexPc(found_dex_pc);
David Brazdil72f7b882015-09-15 17:00:52 +0100100 exception_handler_->SetHandlerQuickFramePc(
101 method->ToNativeQuickPc(found_dex_pc, /* is_catch_handler */ true));
Ian Rogers5cf98192014-05-29 21:31:50 -0700102 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
103 return false; // End stack walk.
104 }
105 }
106 return true; // Continue stack walk.
107 }
108
109 Thread* const self_;
110 // The exception we're looking for the catch block of.
111 Handle<mirror::Throwable>* exception_;
112 // The quick exception handler we're visiting for.
113 QuickExceptionHandler* const exception_handler_;
114
115 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
116};
117
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000118void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200119 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700120 if (kDebugExceptionDelivery) {
121 mirror::String* msg = exception->GetDetailMessage();
122 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
123 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
124 << ": " << str_msg << "\n");
125 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700126 StackHandleScope<1> hs(self_);
127 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200128
Sebastien Hertz520633b2015-09-08 17:03:36 +0200129 // Walk the stack to find catch handler.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100131 visitor.WalkStack(true);
132
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200133 if (kDebugExceptionDelivery) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 if (*handler_quick_frame_ == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100135 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700136 }
137 if (handler_method_ != nullptr) {
138 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
139 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
140 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100141 }
142 }
143 if (clear_exception_) {
144 // Exception was cleared as part of delivery.
145 DCHECK(!self_->IsExceptionPending());
146 } else {
147 // Put exception back in root set with clear throw location.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000148 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100149 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000150 // If the handler is in optimized code, we need to set the catch environment.
151 if (*handler_quick_frame_ != nullptr &&
152 handler_method_ != nullptr &&
153 handler_method_->IsOptimized(sizeof(void*))) {
154 SetCatchEnvironmentForOptimizedHandler(&visitor);
155 }
156}
157
158static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) {
159 // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind
160 // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to
161 // distinguish between core/FPU registers and low/high bits on 64-bit.
162 switch (kind) {
163 case DexRegisterLocation::Kind::kConstant:
164 case DexRegisterLocation::Kind::kInStack:
165 // VRegKind is ignored.
166 return VRegKind::kUndefined;
167
168 case DexRegisterLocation::Kind::kInRegister:
169 // Selects core register. For 64-bit registers, selects low 32 bits.
170 return VRegKind::kLongLoVReg;
171
172 case DexRegisterLocation::Kind::kInRegisterHigh:
173 // Selects core register. For 64-bit registers, selects high 32 bits.
174 return VRegKind::kLongHiVReg;
175
176 case DexRegisterLocation::Kind::kInFpuRegister:
177 // Selects FPU register. For 64-bit registers, selects low 32 bits.
178 return VRegKind::kDoubleLoVReg;
179
180 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
181 // Selects FPU register. For 64-bit registers, selects high 32 bits.
182 return VRegKind::kDoubleHiVReg;
183
184 default:
185 LOG(FATAL) << "Unexpected vreg location "
186 << DexRegisterLocation::PrettyDescriptor(kind);
187 UNREACHABLE();
188 }
189}
190
191void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) {
192 DCHECK(!is_deoptimization_);
193 DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions";
194 DCHECK(handler_method_ != nullptr && handler_method_->IsOptimized(sizeof(void*)));
195
196 if (kDebugExceptionDelivery) {
197 self_->DumpStack(LOG(INFO) << "Setting catch phis: ");
198 }
199
200 const size_t number_of_vregs = handler_method_->GetCodeItem()->registers_size_;
201 CodeInfo code_info = handler_method_->GetOptimizedCodeInfo();
202 StackMapEncoding encoding = code_info.ExtractEncoding();
203
204 // Find stack map of the throwing instruction.
205 StackMap throw_stack_map =
206 code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset(), encoding);
207 DCHECK(throw_stack_map.IsValid());
208 DexRegisterMap throw_vreg_map =
209 code_info.GetDexRegisterMapOf(throw_stack_map, encoding, number_of_vregs);
210
211 // Find stack map of the catch block.
212 StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc(), encoding);
213 DCHECK(catch_stack_map.IsValid());
214 DexRegisterMap catch_vreg_map =
215 code_info.GetDexRegisterMapOf(catch_stack_map, encoding, number_of_vregs);
216
217 // Copy values between them.
218 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
219 DexRegisterLocation::Kind catch_location =
220 catch_vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
221 if (catch_location == DexRegisterLocation::Kind::kNone) {
222 continue;
223 }
224 DCHECK(catch_location == DexRegisterLocation::Kind::kInStack);
225
226 // Get vreg value from its current location.
227 uint32_t vreg_value;
228 VRegKind vreg_kind = ToVRegKind(throw_vreg_map.GetLocationKind(vreg,
229 number_of_vregs,
230 code_info,
231 encoding));
232 bool get_vreg_success = stack_visitor->GetVReg(stack_visitor->GetMethod(),
233 vreg,
234 vreg_kind,
235 &vreg_value);
236 CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out ("
237 << "method=" << PrettyMethod(stack_visitor->GetMethod()) << ", "
238 << "dex_pc=" << stack_visitor->GetDexPc() << ", "
239 << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")";
240
241 // Copy value to the catch phi's stack slot.
242 int32_t slot_offset = catch_vreg_map.GetStackOffsetInBytes(vreg,
243 number_of_vregs,
244 code_info,
245 encoding);
246 ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame();
247 uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset;
248 uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address);
249 *slot_ptr = vreg_value;
250 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200251}
252
Ian Rogers5cf98192014-05-29 21:31:50 -0700253// Prepares deoptimization.
254class DeoptimizeStackVisitor FINAL : public StackVisitor {
255 public:
256 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100258 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
259 self_(self),
260 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700261 prev_shadow_frame_(nullptr),
262 stacked_shadow_frame_pushed_(false) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700263 }
264
Mathieu Chartier90443472015-07-16 20:32:27 -0700265 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700266 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700268 if (method == nullptr) {
269 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
270 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
271 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700272 if (!stacked_shadow_frame_pushed_) {
273 // In case there is no deoptimized shadow frame for this upcall, we still
274 // need to push a nullptr to the stack since there is always a matching pop after
275 // the long jump.
Sebastien Hertzf7958692015-06-09 14:09:14 +0200276 self_->PushStackedShadowFrame(nullptr, StackedShadowFrameType::kDeoptimizationShadowFrame);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700277 stacked_shadow_frame_pushed_ = true;
278 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700279 return false; // End stack walk.
280 } else if (method->IsRuntimeMethod()) {
281 // Ignore callee save method.
282 DCHECK(method->IsCalleeSaveMethod());
283 return true;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200284 } else if (method->IsNative()) {
285 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
286 // the native method.
287 // The top method is a runtime method, the native method comes next.
288 CHECK_EQ(GetFrameDepth(), 1U);
289 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700290 } else {
291 return HandleDeoptimization(method);
292 }
293 }
294
295 private:
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200296 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
297 return static_cast<VRegKind>(kinds.at(reg * 2));
298 }
299
Mathieu Chartier90443472015-07-16 20:32:27 -0700300 bool HandleDeoptimization(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700301 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertz520633b2015-09-08 17:03:36 +0200302 CHECK(code_item != nullptr) << "No code item for " << PrettyMethod(m);
Ian Rogers5cf98192014-05-29 21:31:50 -0700303 uint16_t num_regs = code_item->registers_size_;
304 uint32_t dex_pc = GetDexPc();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305 StackHandleScope<2> hs(self_); // Dex cache, class loader and method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700306 mirror::Class* declaring_class = m->GetDeclaringClass();
307 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
308 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700309 verifier::MethodVerifier verifier(self_, h_dex_cache->GetDexFile(), h_dex_cache, h_class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700310 &m->GetClassDef(), code_item, m->GetDexMethodIndex(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 m, m->GetAccessFlags(), true, true, true, true);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800312 bool verifier_success = verifier.Verify();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 CHECK(verifier_success) << PrettyMethod(m);
314 ShadowFrame* new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, m, dex_pc);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700315 {
Sebastien Hertzf7958692015-06-09 14:09:14 +0200316 ScopedStackedShadowFramePusher pusher(self_, new_frame,
317 StackedShadowFrameType::kShadowFrameUnderConstruction);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700318 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000319
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700320 // Markers for dead values, used when the verifier knows a Dex register is undefined,
321 // or when the compiler knows the register has not been initialized, or is not used
322 // anymore in the method.
323 static constexpr uint32_t kDeadValue = 0xEBADDE09;
324 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
325 for (uint16_t reg = 0; reg < num_regs; ++reg) {
326 VRegKind kind = GetVRegKind(reg, kinds);
327 switch (kind) {
328 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000329 new_frame->SetVReg(reg, kDeadValue);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700330 break;
331 case kConstant:
332 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
333 break;
334 case kReferenceVReg: {
335 uint32_t value = 0;
336 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
337 // We don't want to copy a stale reference into the shadow frame as a reference.
338 // b/20736048
339 if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) {
340 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
341 } else {
342 new_frame->SetVReg(reg, kDeadValue);
343 }
344 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000345 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700346 case kLongLoVReg:
347 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
348 // Treat it as a "long" register pair.
349 uint64_t value = 0;
350 if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) {
351 new_frame->SetVRegLong(reg, value);
352 } else {
353 new_frame->SetVRegLong(reg, kLongDeadValue);
354 }
355 } else {
356 uint32_t value = 0;
357 if (GetVReg(m, reg, kind, &value)) {
358 new_frame->SetVReg(reg, value);
359 } else {
360 new_frame->SetVReg(reg, kDeadValue);
361 }
362 }
363 break;
364 case kLongHiVReg:
365 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
366 // Nothing to do: we treated it as a "long" register pair.
367 } else {
368 uint32_t value = 0;
369 if (GetVReg(m, reg, kind, &value)) {
370 new_frame->SetVReg(reg, value);
371 } else {
372 new_frame->SetVReg(reg, kDeadValue);
373 }
374 }
375 break;
376 case kDoubleLoVReg:
377 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
378 uint64_t value = 0;
379 if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
380 // Treat it as a "double" register pair.
381 new_frame->SetVRegLong(reg, value);
382 } else {
383 new_frame->SetVRegLong(reg, kLongDeadValue);
384 }
385 } else {
386 uint32_t value = 0;
387 if (GetVReg(m, reg, kind, &value)) {
388 new_frame->SetVReg(reg, value);
389 } else {
390 new_frame->SetVReg(reg, kDeadValue);
391 }
392 }
393 break;
394 case kDoubleHiVReg:
395 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
396 // Nothing to do: we treated it as a "double" register pair.
397 } else {
398 uint32_t value = 0;
399 if (GetVReg(m, reg, kind, &value)) {
400 new_frame->SetVReg(reg, value);
401 } else {
402 new_frame->SetVReg(reg, kDeadValue);
403 }
404 }
405 break;
406 default:
407 uint32_t value = 0;
408 if (GetVReg(m, reg, kind, &value)) {
409 new_frame->SetVReg(reg, value);
410 } else {
411 new_frame->SetVReg(reg, kDeadValue);
412 }
413 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000414 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700415 }
416 }
417 if (prev_shadow_frame_ != nullptr) {
418 prev_shadow_frame_->SetLink(new_frame);
419 } else {
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700420 // Will be popped after the long jump after DeoptimizeStack(),
421 // right before interpreter::EnterInterpreterFromDeoptimize().
422 stacked_shadow_frame_pushed_ = true;
Sebastien Hertzf7958692015-06-09 14:09:14 +0200423 self_->PushStackedShadowFrame(new_frame, StackedShadowFrameType::kDeoptimizationShadowFrame);
Ian Rogers5cf98192014-05-29 21:31:50 -0700424 }
425 prev_shadow_frame_ = new_frame;
426 return true;
427 }
428
429 Thread* const self_;
430 QuickExceptionHandler* const exception_handler_;
431 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700432 bool stacked_shadow_frame_pushed_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700433
434 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
435};
436
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200437void QuickExceptionHandler::DeoptimizeStack() {
438 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700439 if (kDebugExceptionDelivery) {
440 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
441 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200442
443 DeoptimizeStackVisitor visitor(self_, context_, this);
444 visitor.WalkStack(true);
445
446 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000447 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100448}
449
450// Unwinds all instrumentation stack frame prior to catch handler or upcall.
451class InstrumentationStackVisitor : public StackVisitor {
452 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700453 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -0700454 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100455 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700456 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100457 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700458 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100459 }
460
Mathieu Chartier90443472015-07-16 20:32:27 -0700461 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700462 size_t current_frame_depth = GetFrameDepth();
463 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100464 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700465 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100466 if (!IsInInlinedFrame()) {
467 // We do not count inlined frames, because we do not instrument them. The reason we
468 // include them in the stack walking is the check against `frame_depth_`, which is
469 // given to us by a visitor that visits inlined frames.
470 ++instrumentation_frames_to_pop_;
471 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100472 }
473 return true;
474 } else {
475 // We reached the frame of the catch handler or the upcall.
476 return false;
477 }
478 }
479
480 size_t GetInstrumentationFramesToPop() const {
481 return instrumentation_frames_to_pop_;
482 }
483
484 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700485 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100486 size_t instrumentation_frames_to_pop_;
487
488 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
489};
490
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200491void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100492 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700493 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100494 visitor.WalkStack(true);
495
496 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
497 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
498 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
499 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
500 }
501 }
502}
503
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200504void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100505 // Place context back on thread so it will be available when we continue.
506 self_->ReleaseLongJumpContext(context_);
507 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
508 CHECK_NE(handler_quick_frame_pc_, 0u);
509 context_->SetPC(handler_quick_frame_pc_);
510 context_->SmashCallerSaves();
511 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800512 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100513}
514
515} // namespace art