blob: b9d76b491c004d33fd3acf25ed216cab1e0a61ed [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);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700100 exception_handler_->SetHandlerQuickFramePc(method->ToNativeQuickPc(found_dex_pc));
Ian Rogers5cf98192014-05-29 21:31:50 -0700101 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
102 return false; // End stack walk.
103 }
104 }
105 return true; // Continue stack walk.
106 }
107
108 Thread* const self_;
109 // The exception we're looking for the catch block of.
110 Handle<mirror::Throwable>* exception_;
111 // The quick exception handler we're visiting for.
112 QuickExceptionHandler* const exception_handler_;
113
114 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
115};
116
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000117void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
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 Hertz520633b2015-09-08 17:03:36 +0200128 // Walk the stack to find catch handler.
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) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 if (*handler_quick_frame_ == 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.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000147 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100148 }
David Brazdilb022fa12015-08-20 17:47:48 +0100149 // If the handler is in optimized code, we need to set the catch environment.
150 if (*handler_quick_frame_ != nullptr &&
151 handler_method_ != nullptr &&
152 handler_method_->IsOptimized(sizeof(void*))) {
153 SetCatchEnvironmentForOptimizedHandler(&visitor);
154 }
155}
156
157static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) {
158 // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind
159 // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to
160 // distinguish between core/FPU registers and low/high bits on 64-bit.
161 switch (kind) {
162 case DexRegisterLocation::Kind::kConstant:
163 case DexRegisterLocation::Kind::kInStack:
164 // VRegKind is ignored.
165 return VRegKind::kUndefined;
166
167 case DexRegisterLocation::Kind::kInRegister:
168 // Selects core register. For 64-bit registers, selects low 32 bits.
169 return VRegKind::kLongLoVReg;
170
171 case DexRegisterLocation::Kind::kInRegisterHigh:
172 // Selects core register. For 64-bit registers, selects high 32 bits.
173 return VRegKind::kLongHiVReg;
174
175 case DexRegisterLocation::Kind::kInFpuRegister:
176 // Selects FPU register. For 64-bit registers, selects low 32 bits.
177 return VRegKind::kDoubleLoVReg;
178
179 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
180 // Selects FPU register. For 64-bit registers, selects high 32 bits.
181 return VRegKind::kDoubleHiVReg;
182
183 default:
184 LOG(FATAL) << "Unexpected vreg location "
185 << DexRegisterLocation::PrettyDescriptor(kind);
186 UNREACHABLE();
187 }
188}
189
190void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) {
191 DCHECK(!is_deoptimization_);
192 DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions";
193 DCHECK(handler_method_ != nullptr && handler_method_->IsOptimized(sizeof(void*)));
194
195 if (kDebugExceptionDelivery) {
196 self_->DumpStack(LOG(INFO) << "Setting catch phis: ");
197 }
198
199 const size_t number_of_vregs = handler_method_->GetCodeItem()->registers_size_;
200 CodeInfo code_info = handler_method_->GetOptimizedCodeInfo();
201 StackMapEncoding encoding = code_info.ExtractEncoding();
202
203 // Find stack map of the throwing instruction.
204 StackMap throw_stack_map =
205 code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset(), encoding);
206 DCHECK(throw_stack_map.IsValid());
207 DexRegisterMap throw_vreg_map =
208 code_info.GetDexRegisterMapOf(throw_stack_map, encoding, number_of_vregs);
209
210 // Find stack map of the catch block.
211 StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc(), encoding);
212 DCHECK(catch_stack_map.IsValid());
213 DexRegisterMap catch_vreg_map =
214 code_info.GetDexRegisterMapOf(catch_stack_map, encoding, number_of_vregs);
215
216 // Copy values between them.
217 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
218 DexRegisterLocation::Kind catch_location =
219 catch_vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
220 if (catch_location == DexRegisterLocation::Kind::kNone) {
221 continue;
222 }
223 DCHECK(catch_location == DexRegisterLocation::Kind::kInStack);
224
225 // Get vreg value from its current location.
226 uint32_t vreg_value;
227 VRegKind vreg_kind = ToVRegKind(throw_vreg_map.GetLocationKind(vreg,
228 number_of_vregs,
229 code_info,
230 encoding));
231 bool get_vreg_success = stack_visitor->GetVReg(stack_visitor->GetMethod(),
232 vreg,
233 vreg_kind,
234 &vreg_value);
235 CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out ("
236 << "method=" << PrettyMethod(stack_visitor->GetMethod()) << ", "
237 << "dex_pc=" << stack_visitor->GetDexPc() << ", "
238 << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")";
239
240 // Copy value to the catch phi's stack slot.
241 int32_t slot_offset = catch_vreg_map.GetStackOffsetInBytes(vreg,
242 number_of_vregs,
243 code_info,
244 encoding);
245 ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame();
246 uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset;
247 uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address);
248 *slot_ptr = vreg_value;
249 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200250}
251
Ian Rogers5cf98192014-05-29 21:31:50 -0700252// Prepares deoptimization.
253class DeoptimizeStackVisitor FINAL : public StackVisitor {
254 public:
255 DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -0700256 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100257 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
258 self_(self),
259 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700260 prev_shadow_frame_(nullptr),
261 stacked_shadow_frame_pushed_(false) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700262 }
263
Mathieu Chartier90443472015-07-16 20:32:27 -0700264 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700265 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 ArtMethod* method = GetMethod();
Ian Rogers5cf98192014-05-29 21:31:50 -0700267 if (method == nullptr) {
268 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
269 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
270 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700271 if (!stacked_shadow_frame_pushed_) {
272 // In case there is no deoptimized shadow frame for this upcall, we still
273 // need to push a nullptr to the stack since there is always a matching pop after
274 // the long jump.
Sebastien Hertzf7958692015-06-09 14:09:14 +0200275 self_->PushStackedShadowFrame(nullptr, StackedShadowFrameType::kDeoptimizationShadowFrame);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700276 stacked_shadow_frame_pushed_ = true;
277 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700278 return false; // End stack walk.
279 } else if (method->IsRuntimeMethod()) {
280 // Ignore callee save method.
281 DCHECK(method->IsCalleeSaveMethod());
282 return true;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200283 } else if (method->IsNative()) {
284 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
285 // the native method.
286 // The top method is a runtime method, the native method comes next.
287 CHECK_EQ(GetFrameDepth(), 1U);
288 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700289 } else {
290 return HandleDeoptimization(method);
291 }
292 }
293
294 private:
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200295 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
296 return static_cast<VRegKind>(kinds.at(reg * 2));
297 }
298
Mathieu Chartier90443472015-07-16 20:32:27 -0700299 bool HandleDeoptimization(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700300 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertz520633b2015-09-08 17:03:36 +0200301 CHECK(code_item != nullptr) << "No code item for " << PrettyMethod(m);
Ian Rogers5cf98192014-05-29 21:31:50 -0700302 uint16_t num_regs = code_item->registers_size_;
303 uint32_t dex_pc = GetDexPc();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 StackHandleScope<2> hs(self_); // Dex cache, class loader and method.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700305 mirror::Class* declaring_class = m->GetDeclaringClass();
306 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
307 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700308 verifier::MethodVerifier verifier(self_, h_dex_cache->GetDexFile(), h_dex_cache, h_class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700309 &m->GetClassDef(), code_item, m->GetDexMethodIndex(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 m, m->GetAccessFlags(), true, true, true, true);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800311 bool verifier_success = verifier.Verify();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 CHECK(verifier_success) << PrettyMethod(m);
313 ShadowFrame* new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, m, dex_pc);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700314 {
Sebastien Hertzf7958692015-06-09 14:09:14 +0200315 ScopedStackedShadowFramePusher pusher(self_, new_frame,
316 StackedShadowFrameType::kShadowFrameUnderConstruction);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700317 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000318
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700319 // Markers for dead values, used when the verifier knows a Dex register is undefined,
320 // or when the compiler knows the register has not been initialized, or is not used
321 // anymore in the method.
322 static constexpr uint32_t kDeadValue = 0xEBADDE09;
323 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
324 for (uint16_t reg = 0; reg < num_regs; ++reg) {
325 VRegKind kind = GetVRegKind(reg, kinds);
326 switch (kind) {
327 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000328 new_frame->SetVReg(reg, kDeadValue);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700329 break;
330 case kConstant:
331 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
332 break;
333 case kReferenceVReg: {
334 uint32_t value = 0;
335 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
336 // We don't want to copy a stale reference into the shadow frame as a reference.
337 // b/20736048
338 if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) {
339 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
340 } else {
341 new_frame->SetVReg(reg, kDeadValue);
342 }
343 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000344 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700345 case kLongLoVReg:
346 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
347 // Treat it as a "long" register pair.
348 uint64_t value = 0;
349 if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) {
350 new_frame->SetVRegLong(reg, value);
351 } else {
352 new_frame->SetVRegLong(reg, kLongDeadValue);
353 }
354 } else {
355 uint32_t value = 0;
356 if (GetVReg(m, reg, kind, &value)) {
357 new_frame->SetVReg(reg, value);
358 } else {
359 new_frame->SetVReg(reg, kDeadValue);
360 }
361 }
362 break;
363 case kLongHiVReg:
364 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
365 // Nothing to do: we treated it as a "long" register pair.
366 } else {
367 uint32_t value = 0;
368 if (GetVReg(m, reg, kind, &value)) {
369 new_frame->SetVReg(reg, value);
370 } else {
371 new_frame->SetVReg(reg, kDeadValue);
372 }
373 }
374 break;
375 case kDoubleLoVReg:
376 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
377 uint64_t value = 0;
378 if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
379 // Treat it as a "double" register pair.
380 new_frame->SetVRegLong(reg, value);
381 } else {
382 new_frame->SetVRegLong(reg, kLongDeadValue);
383 }
384 } else {
385 uint32_t value = 0;
386 if (GetVReg(m, reg, kind, &value)) {
387 new_frame->SetVReg(reg, value);
388 } else {
389 new_frame->SetVReg(reg, kDeadValue);
390 }
391 }
392 break;
393 case kDoubleHiVReg:
394 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
395 // Nothing to do: we treated it as a "double" register pair.
396 } else {
397 uint32_t value = 0;
398 if (GetVReg(m, reg, kind, &value)) {
399 new_frame->SetVReg(reg, value);
400 } else {
401 new_frame->SetVReg(reg, kDeadValue);
402 }
403 }
404 break;
405 default:
406 uint32_t value = 0;
407 if (GetVReg(m, reg, kind, &value)) {
408 new_frame->SetVReg(reg, value);
409 } else {
410 new_frame->SetVReg(reg, kDeadValue);
411 }
412 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000413 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700414 }
415 }
416 if (prev_shadow_frame_ != nullptr) {
417 prev_shadow_frame_->SetLink(new_frame);
418 } else {
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700419 // Will be popped after the long jump after DeoptimizeStack(),
420 // right before interpreter::EnterInterpreterFromDeoptimize().
421 stacked_shadow_frame_pushed_ = true;
Sebastien Hertzf7958692015-06-09 14:09:14 +0200422 self_->PushStackedShadowFrame(new_frame, StackedShadowFrameType::kDeoptimizationShadowFrame);
Ian Rogers5cf98192014-05-29 21:31:50 -0700423 }
424 prev_shadow_frame_ = new_frame;
425 return true;
426 }
427
428 Thread* const self_;
429 QuickExceptionHandler* const exception_handler_;
430 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700431 bool stacked_shadow_frame_pushed_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700432
433 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
434};
435
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200436void QuickExceptionHandler::DeoptimizeStack() {
437 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700438 if (kDebugExceptionDelivery) {
439 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
440 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200441
442 DeoptimizeStackVisitor visitor(self_, context_, this);
443 visitor.WalkStack(true);
444
445 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000446 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100447}
448
449// Unwinds all instrumentation stack frame prior to catch handler or upcall.
450class InstrumentationStackVisitor : public StackVisitor {
451 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700452 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -0700453 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100454 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700455 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100456 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700457 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100458 }
459
Mathieu Chartier90443472015-07-16 20:32:27 -0700460 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700461 size_t current_frame_depth = GetFrameDepth();
462 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100463 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700464 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100465 if (!IsInInlinedFrame()) {
466 // We do not count inlined frames, because we do not instrument them. The reason we
467 // include them in the stack walking is the check against `frame_depth_`, which is
468 // given to us by a visitor that visits inlined frames.
469 ++instrumentation_frames_to_pop_;
470 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100471 }
472 return true;
473 } else {
474 // We reached the frame of the catch handler or the upcall.
475 return false;
476 }
477 }
478
479 size_t GetInstrumentationFramesToPop() const {
480 return instrumentation_frames_to_pop_;
481 }
482
483 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700484 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100485 size_t instrumentation_frames_to_pop_;
486
487 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
488};
489
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200490void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100491 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700492 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100493 visitor.WalkStack(true);
494
495 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
496 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
497 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
498 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
499 }
500 }
501}
502
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200503void QuickExceptionHandler::DoLongJump() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100504 // Place context back on thread so it will be available when we continue.
505 self_->ReleaseLongJumpContext(context_);
506 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
507 CHECK_NE(handler_quick_frame_pc_, 0u);
508 context_->SetPC(handler_quick_frame_pc_);
509 context_->SmashCallerSaves();
510 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800511 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100512}
513
514} // namespace art