1 /*
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
17 #include "quick_exception_handler.h"
18 #include <ios>
19
20 #include "arch/context.h"
21 #include "art_method-inl.h"
22 #include "base/enums.h"
23 #include "base/globals.h"
24 #include "base/logging.h" // For VLOG_IS_ON.
25 #include "base/systrace.h"
26 #include "dex/dex_file_types.h"
27 #include "dex/dex_instruction.h"
28 #include "entrypoints/entrypoint_utils.h"
29 #include "entrypoints/quick/quick_entrypoints_enum.h"
30 #include "entrypoints/runtime_asm_entrypoints.h"
31 #include "handle_scope-inl.h"
32 #include "interpreter/shadow_frame-inl.h"
33 #include "jit/jit.h"
34 #include "jit/jit_code_cache.h"
35 #include "mirror/class-inl.h"
36 #include "mirror/class_loader.h"
37 #include "mirror/throwable.h"
38 #include "nterp_helpers.h"
39 #include "oat_quick_method_header.h"
40 #include "stack.h"
41 #include "stack_map.h"
42
43 namespace art {
44
45 static constexpr bool kDebugExceptionDelivery = false;
46 static constexpr size_t kInvalidFrameDepth = 0xffffffff;
47
QuickExceptionHandler(Thread * self,bool is_deoptimization)48 QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
49 : self_(self),
50 context_(self->GetLongJumpContext()),
51 is_deoptimization_(is_deoptimization),
52 method_tracing_active_(is_deoptimization ||
53 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
54 handler_quick_frame_(nullptr),
55 handler_quick_frame_pc_(0),
56 handler_method_header_(nullptr),
57 handler_quick_arg0_(0),
58 handler_dex_pc_(0),
59 clear_exception_(false),
60 handler_frame_depth_(kInvalidFrameDepth),
61 full_fragment_done_(false) {}
62
63 // Finds catch handler.
64 class CatchBlockStackVisitor final : public StackVisitor {
65 public:
CatchBlockStackVisitor(Thread * self,Context * context,Handle<mirror::Throwable> * exception,QuickExceptionHandler * exception_handler,uint32_t skip_frames)66 CatchBlockStackVisitor(Thread* self,
67 Context* context,
68 Handle<mirror::Throwable>* exception,
69 QuickExceptionHandler* exception_handler,
70 uint32_t skip_frames)
71 REQUIRES_SHARED(Locks::mutator_lock_)
72 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
73 exception_(exception),
74 exception_handler_(exception_handler),
75 skip_frames_(skip_frames) {
76 }
77
VisitFrame()78 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
79 ArtMethod* method = GetMethod();
80 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
81 if (method == nullptr) {
82 DCHECK_EQ(skip_frames_, 0u)
83 << "We tried to skip an upcall! We should have returned to the upcall to finish delivery";
84 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
85 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
86 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
87 return false; // End stack walk.
88 }
89 if (skip_frames_ != 0) {
90 skip_frames_--;
91 return true;
92 }
93 if (method->IsRuntimeMethod()) {
94 // Ignore callee save method.
95 DCHECK(method->IsCalleeSaveMethod());
96 return true;
97 }
98 return HandleTryItems(method);
99 }
100
101 private:
HandleTryItems(ArtMethod * method)102 bool HandleTryItems(ArtMethod* method)
103 REQUIRES_SHARED(Locks::mutator_lock_) {
104 uint32_t dex_pc = dex::kDexNoIndex;
105 if (!method->IsNative()) {
106 dex_pc = GetDexPc();
107 }
108 if (dex_pc != dex::kDexNoIndex) {
109 bool clear_exception = false;
110 StackHandleScope<1> hs(GetThread());
111 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
112 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
113 exception_handler_->SetClearException(clear_exception);
114 if (found_dex_pc != dex::kDexNoIndex) {
115 exception_handler_->SetHandlerDexPc(found_dex_pc);
116 exception_handler_->SetHandlerQuickFramePc(
117 GetCurrentOatQuickMethodHeader()->ToNativeQuickPc(
118 method, found_dex_pc, /* is_for_catch_handler= */ true));
119 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
120 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
121 return false; // End stack walk.
122 } else if (UNLIKELY(GetThread()->HasDebuggerShadowFrames())) {
123 // We are going to unwind this frame. Did we prepare a shadow frame for debugging?
124 size_t frame_id = GetFrameId();
125 ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(frame_id);
126 if (frame != nullptr) {
127 // We will not execute this shadow frame so we can safely deallocate it.
128 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
129 ShadowFrame::DeleteDeoptimizedFrame(frame);
130 }
131 }
132 }
133 return true; // Continue stack walk.
134 }
135
136 // The exception we're looking for the catch block of.
137 Handle<mirror::Throwable>* exception_;
138 // The quick exception handler we're visiting for.
139 QuickExceptionHandler* const exception_handler_;
140 // The number of frames to skip searching for catches in.
141 uint32_t skip_frames_;
142
143 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
144 };
145
146 // Finds the appropriate exception catch after calling all method exit instrumentation functions.
147 // Note that this might change the exception being thrown.
FindCatch(ObjPtr<mirror::Throwable> exception)148 void QuickExceptionHandler::FindCatch(ObjPtr<mirror::Throwable> exception) {
149 DCHECK(!is_deoptimization_);
150 instrumentation::InstrumentationStackPopper popper(self_);
151 // The number of total frames we have so far popped.
152 uint32_t already_popped = 0;
153 bool popped_to_top = true;
154 StackHandleScope<1> hs(self_);
155 MutableHandle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
156 // Sending the instrumentation events (done by the InstrumentationStackPopper) can cause new
157 // exceptions to be thrown which will override the current exception. Therefore we need to perform
158 // the search for a catch in a loop until we have successfully popped all the way to a catch or
159 // the top of the stack.
160 do {
161 if (kDebugExceptionDelivery) {
162 ObjPtr<mirror::String> msg = exception_ref->GetDetailMessage();
163 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
164 self_->DumpStack(LOG_STREAM(INFO) << "Delivering exception: " << exception_ref->PrettyTypeOf()
165 << ": " << str_msg << "\n");
166 }
167
168 // Walk the stack to find catch handler.
169 CatchBlockStackVisitor visitor(self_, context_,
170 &exception_ref,
171 this,
172 /*skip_frames=*/already_popped);
173 visitor.WalkStack(true);
174 uint32_t new_pop_count = handler_frame_depth_;
175 DCHECK_GE(new_pop_count, already_popped);
176 already_popped = new_pop_count;
177
178 if (kDebugExceptionDelivery) {
179 if (*handler_quick_frame_ == nullptr) {
180 LOG(INFO) << "Handler is upcall";
181 }
182 if (GetHandlerMethod() != nullptr) {
183 const DexFile* dex_file = GetHandlerMethod()->GetDexFile();
184 int line_number =
185 annotations::GetLineNumFromPC(dex_file, GetHandlerMethod(), handler_dex_pc_);
186 LOG(INFO) << "Handler: " << GetHandlerMethod()->PrettyMethod() << " (line: "
187 << line_number << ")";
188 }
189 }
190 // Exception was cleared as part of delivery.
191 DCHECK(!self_->IsExceptionPending());
192 // If the handler is in optimized code, we need to set the catch environment.
193 if (*handler_quick_frame_ != nullptr &&
194 handler_method_header_ != nullptr &&
195 handler_method_header_->IsOptimized()) {
196 SetCatchEnvironmentForOptimizedHandler(&visitor);
197 }
198 popped_to_top =
199 popper.PopFramesTo(reinterpret_cast<uintptr_t>(handler_quick_frame_), exception_ref);
200 } while (!popped_to_top);
201 if (!clear_exception_) {
202 // Put exception back in root set with clear throw location.
203 self_->SetException(exception_ref.Get());
204 }
205 }
206
ToVRegKind(DexRegisterLocation::Kind kind)207 static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) {
208 // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind
209 // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to
210 // distinguish between core/FPU registers and low/high bits on 64-bit.
211 switch (kind) {
212 case DexRegisterLocation::Kind::kConstant:
213 case DexRegisterLocation::Kind::kInStack:
214 // VRegKind is ignored.
215 return VRegKind::kUndefined;
216
217 case DexRegisterLocation::Kind::kInRegister:
218 // Selects core register. For 64-bit registers, selects low 32 bits.
219 return VRegKind::kLongLoVReg;
220
221 case DexRegisterLocation::Kind::kInRegisterHigh:
222 // Selects core register. For 64-bit registers, selects high 32 bits.
223 return VRegKind::kLongHiVReg;
224
225 case DexRegisterLocation::Kind::kInFpuRegister:
226 // Selects FPU register. For 64-bit registers, selects low 32 bits.
227 return VRegKind::kDoubleLoVReg;
228
229 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
230 // Selects FPU register. For 64-bit registers, selects high 32 bits.
231 return VRegKind::kDoubleHiVReg;
232
233 default:
234 LOG(FATAL) << "Unexpected vreg location " << kind;
235 UNREACHABLE();
236 }
237 }
238
SetCatchEnvironmentForOptimizedHandler(StackVisitor * stack_visitor)239 void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) {
240 DCHECK(!is_deoptimization_);
241 DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions";
242 DCHECK(GetHandlerMethod() != nullptr && handler_method_header_->IsOptimized());
243
244 if (kDebugExceptionDelivery) {
245 self_->DumpStack(LOG_STREAM(INFO) << "Setting catch phis: ");
246 }
247
248 CodeItemDataAccessor accessor(GetHandlerMethod()->DexInstructionData());
249 const size_t number_of_vregs = accessor.RegistersSize();
250 CodeInfo code_info(handler_method_header_);
251
252 // Find stack map of the catch block.
253 StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc());
254 DCHECK(catch_stack_map.IsValid());
255 DexRegisterMap catch_vreg_map = code_info.GetDexRegisterMapOf(catch_stack_map);
256 DCHECK_EQ(catch_vreg_map.size(), number_of_vregs);
257
258 if (!catch_vreg_map.HasAnyLiveDexRegisters()) {
259 return;
260 }
261
262 // Find stack map of the throwing instruction.
263 StackMap throw_stack_map =
264 code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset());
265 DCHECK(throw_stack_map.IsValid());
266 DexRegisterMap throw_vreg_map = code_info.GetDexRegisterMapOf(throw_stack_map);
267 DCHECK_EQ(throw_vreg_map.size(), number_of_vregs);
268
269 // Copy values between them.
270 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
271 DexRegisterLocation::Kind catch_location = catch_vreg_map[vreg].GetKind();
272 if (catch_location == DexRegisterLocation::Kind::kNone) {
273 continue;
274 }
275 DCHECK(catch_location == DexRegisterLocation::Kind::kInStack);
276
277 // Get vreg value from its current location.
278 uint32_t vreg_value;
279 VRegKind vreg_kind = ToVRegKind(throw_vreg_map[vreg].GetKind());
280 bool get_vreg_success =
281 stack_visitor->GetVReg(stack_visitor->GetMethod(),
282 vreg,
283 vreg_kind,
284 &vreg_value,
285 throw_vreg_map[vreg]);
286 CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out ("
287 << "method=" << ArtMethod::PrettyMethod(stack_visitor->GetMethod())
288 << ", dex_pc=" << stack_visitor->GetDexPc() << ", "
289 << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")";
290
291 // Copy value to the catch phi's stack slot.
292 int32_t slot_offset = catch_vreg_map[vreg].GetStackOffsetInBytes();
293 ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame();
294 uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset;
295 uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address);
296 *slot_ptr = vreg_value;
297 }
298 }
299
300 // Prepares deoptimization.
301 class DeoptimizeStackVisitor final : public StackVisitor {
302 public:
DeoptimizeStackVisitor(Thread * self,Context * context,QuickExceptionHandler * exception_handler,bool single_frame)303 DeoptimizeStackVisitor(Thread* self,
304 Context* context,
305 QuickExceptionHandler* exception_handler,
306 bool single_frame)
307 REQUIRES_SHARED(Locks::mutator_lock_)
308 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
309 exception_handler_(exception_handler),
310 prev_shadow_frame_(nullptr),
311 stacked_shadow_frame_pushed_(false),
312 single_frame_deopt_(single_frame),
313 single_frame_done_(false),
314 single_frame_deopt_method_(nullptr),
315 single_frame_deopt_quick_method_header_(nullptr),
316 callee_method_(nullptr) {
317 }
318
GetSingleFrameDeoptMethod() const319 ArtMethod* GetSingleFrameDeoptMethod() const {
320 return single_frame_deopt_method_;
321 }
322
GetSingleFrameDeoptQuickMethodHeader() const323 const OatQuickMethodHeader* GetSingleFrameDeoptQuickMethodHeader() const {
324 return single_frame_deopt_quick_method_header_;
325 }
326
FinishStackWalk()327 void FinishStackWalk() REQUIRES_SHARED(Locks::mutator_lock_) {
328 // This is the upcall, or the next full frame in single-frame deopt, or the
329 // code isn't deoptimizeable. We remember the frame and last pc so that we
330 // may long jump to them.
331 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
332 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
333 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
334 if (!stacked_shadow_frame_pushed_) {
335 // In case there is no deoptimized shadow frame for this upcall, we still
336 // need to push a nullptr to the stack since there is always a matching pop after
337 // the long jump.
338 GetThread()->PushStackedShadowFrame(nullptr,
339 StackedShadowFrameType::kDeoptimizationShadowFrame);
340 stacked_shadow_frame_pushed_ = true;
341 }
342 if (GetMethod() == nullptr) {
343 exception_handler_->SetFullFragmentDone(true);
344 } else {
345 CHECK(callee_method_ != nullptr) << GetMethod()->PrettyMethod(false);
346 exception_handler_->SetHandlerQuickArg0(reinterpret_cast<uintptr_t>(callee_method_));
347 }
348 }
349
VisitFrame()350 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
351 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
352 ArtMethod* method = GetMethod();
353 VLOG(deopt) << "Deoptimizing stack: depth: " << GetFrameDepth()
354 << " at method " << ArtMethod::PrettyMethod(method);
355 if (method == nullptr || single_frame_done_) {
356 FinishStackWalk();
357 return false; // End stack walk.
358 } else if (method->IsRuntimeMethod()) {
359 // Ignore callee save method.
360 DCHECK(method->IsCalleeSaveMethod());
361 return true;
362 } else if (method->IsNative()) {
363 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
364 // the native method.
365 // The top method is a runtime method, the native method comes next.
366 CHECK_EQ(GetFrameDepth(), 1U);
367 callee_method_ = method;
368 return true;
369 } else if (!single_frame_deopt_ &&
370 !Runtime::Current()->IsAsyncDeoptimizeable(GetCurrentQuickFramePc())) {
371 // We hit some code that's not deoptimizeable. However, Single-frame deoptimization triggered
372 // from compiled code is always allowed since HDeoptimize always saves the full environment.
373 LOG(WARNING) << "Got request to deoptimize un-deoptimizable method "
374 << method->PrettyMethod();
375 FinishStackWalk();
376 return false; // End stack walk.
377 } else {
378 // Check if a shadow frame already exists for debugger's set-local-value purpose.
379 const size_t frame_id = GetFrameId();
380 ShadowFrame* new_frame = GetThread()->FindDebuggerShadowFrame(frame_id);
381 const bool* updated_vregs;
382 CodeItemDataAccessor accessor(method->DexInstructionData());
383 const size_t num_regs = accessor.RegistersSize();
384 if (new_frame == nullptr) {
385 new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, method, GetDexPc());
386 updated_vregs = nullptr;
387 } else {
388 updated_vregs = GetThread()->GetUpdatedVRegFlags(frame_id);
389 DCHECK(updated_vregs != nullptr);
390 }
391 if (GetCurrentOatQuickMethodHeader()->IsNterpMethodHeader()) {
392 HandleNterpDeoptimization(method, new_frame, updated_vregs);
393 } else {
394 HandleOptimizingDeoptimization(method, new_frame, updated_vregs);
395 }
396 if (updated_vregs != nullptr) {
397 // Calling Thread::RemoveDebuggerShadowFrameMapping will also delete the updated_vregs
398 // array so this must come after we processed the frame.
399 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
400 DCHECK(GetThread()->FindDebuggerShadowFrame(frame_id) == nullptr);
401 }
402 if (prev_shadow_frame_ != nullptr) {
403 prev_shadow_frame_->SetLink(new_frame);
404 } else {
405 // Will be popped after the long jump after DeoptimizeStack(),
406 // right before interpreter::EnterInterpreterFromDeoptimize().
407 stacked_shadow_frame_pushed_ = true;
408 GetThread()->PushStackedShadowFrame(
409 new_frame, StackedShadowFrameType::kDeoptimizationShadowFrame);
410 }
411 prev_shadow_frame_ = new_frame;
412
413 if (single_frame_deopt_ && !IsInInlinedFrame()) {
414 // Single-frame deopt ends at the first non-inlined frame and needs to store that method.
415 single_frame_done_ = true;
416 single_frame_deopt_method_ = method;
417 single_frame_deopt_quick_method_header_ = GetCurrentOatQuickMethodHeader();
418 }
419 callee_method_ = method;
420 return true;
421 }
422 }
423
424 private:
HandleNterpDeoptimization(ArtMethod * m,ShadowFrame * new_frame,const bool * updated_vregs)425 void HandleNterpDeoptimization(ArtMethod* m,
426 ShadowFrame* new_frame,
427 const bool* updated_vregs)
428 REQUIRES_SHARED(Locks::mutator_lock_) {
429 ArtMethod** cur_quick_frame = GetCurrentQuickFrame();
430 StackReference<mirror::Object>* vreg_ref_base =
431 reinterpret_cast<StackReference<mirror::Object>*>(NterpGetReferenceArray(cur_quick_frame));
432 int32_t* vreg_int_base =
433 reinterpret_cast<int32_t*>(NterpGetRegistersArray(cur_quick_frame));
434 CodeItemDataAccessor accessor(m->DexInstructionData());
435 const uint16_t num_regs = accessor.RegistersSize();
436 // An nterp frame has two arrays: a dex register array and a reference array
437 // that shadows the dex register array but only containing references
438 // (non-reference dex registers have nulls). See nterp_helpers.cc.
439 for (size_t reg = 0; reg < num_regs; ++reg) {
440 if (updated_vregs != nullptr && updated_vregs[reg]) {
441 // Keep the value set by debugger.
442 continue;
443 }
444 StackReference<mirror::Object>* ref_addr = vreg_ref_base + reg;
445 mirror::Object* ref = ref_addr->AsMirrorPtr();
446 if (ref != nullptr) {
447 new_frame->SetVRegReference(reg, ref);
448 } else {
449 new_frame->SetVReg(reg, vreg_int_base[reg]);
450 }
451 }
452 }
453
HandleOptimizingDeoptimization(ArtMethod * m,ShadowFrame * new_frame,const bool * updated_vregs)454 void HandleOptimizingDeoptimization(ArtMethod* m,
455 ShadowFrame* new_frame,
456 const bool* updated_vregs)
457 REQUIRES_SHARED(Locks::mutator_lock_) {
458 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
459 CodeInfo code_info(method_header);
460 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
461 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
462 CodeItemDataAccessor accessor(m->DexInstructionData());
463 const size_t number_of_vregs = accessor.RegistersSize();
464 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
465 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
466 DexRegisterMap vreg_map = IsInInlinedFrame()
467 ? code_info.GetInlineDexRegisterMapOf(stack_map, GetCurrentInlinedFrame())
468 : code_info.GetDexRegisterMapOf(stack_map);
469
470 if (kIsDebugBuild || UNLIKELY(Runtime::Current()->IsJavaDebuggable())) {
471 CHECK_EQ(vreg_map.size(), number_of_vregs) << *Thread::Current()
472 << "Deopting: " << m->PrettyMethod()
473 << " inlined? "
474 << std::boolalpha << IsInInlinedFrame();
475 }
476 if (vreg_map.empty()) {
477 return;
478 }
479
480 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
481 if (updated_vregs != nullptr && updated_vregs[vreg]) {
482 // Keep the value set by debugger.
483 continue;
484 }
485
486 DexRegisterLocation::Kind location = vreg_map[vreg].GetKind();
487 static constexpr uint32_t kDeadValue = 0xEBADDE09;
488 uint32_t value = kDeadValue;
489 bool is_reference = false;
490
491 switch (location) {
492 case DexRegisterLocation::Kind::kInStack: {
493 const int32_t offset = vreg_map[vreg].GetStackOffsetInBytes();
494 const uint8_t* addr = reinterpret_cast<const uint8_t*>(GetCurrentQuickFrame()) + offset;
495 value = *reinterpret_cast<const uint32_t*>(addr);
496 uint32_t bit = (offset >> 2);
497 if (bit < stack_mask.size_in_bits() && stack_mask.LoadBit(bit)) {
498 is_reference = true;
499 }
500 break;
501 }
502 case DexRegisterLocation::Kind::kInRegister:
503 case DexRegisterLocation::Kind::kInRegisterHigh:
504 case DexRegisterLocation::Kind::kInFpuRegister:
505 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
506 uint32_t reg = vreg_map[vreg].GetMachineRegister();
507 bool result = GetRegisterIfAccessible(reg, ToVRegKind(location), &value);
508 CHECK(result);
509 if (location == DexRegisterLocation::Kind::kInRegister) {
510 if (((1u << reg) & register_mask) != 0) {
511 is_reference = true;
512 }
513 }
514 break;
515 }
516 case DexRegisterLocation::Kind::kConstant: {
517 value = vreg_map[vreg].GetConstant();
518 if (value == 0) {
519 // Make it a reference for extra safety.
520 is_reference = true;
521 }
522 break;
523 }
524 case DexRegisterLocation::Kind::kNone: {
525 break;
526 }
527 default: {
528 LOG(FATAL) << "Unexpected location kind " << vreg_map[vreg].GetKind();
529 UNREACHABLE();
530 }
531 }
532 if (is_reference) {
533 new_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(value));
534 } else {
535 new_frame->SetVReg(vreg, value);
536 }
537 }
538 }
539
GetVRegKind(uint16_t reg,const std::vector<int32_t> & kinds)540 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
541 return static_cast<VRegKind>(kinds[reg * 2]);
542 }
543
544 QuickExceptionHandler* const exception_handler_;
545 ShadowFrame* prev_shadow_frame_;
546 bool stacked_shadow_frame_pushed_;
547 const bool single_frame_deopt_;
548 bool single_frame_done_;
549 ArtMethod* single_frame_deopt_method_;
550 const OatQuickMethodHeader* single_frame_deopt_quick_method_header_;
551 ArtMethod* callee_method_;
552
553 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
554 };
555
PrepareForLongJumpToInvokeStubOrInterpreterBridge()556 void QuickExceptionHandler::PrepareForLongJumpToInvokeStubOrInterpreterBridge() {
557 if (full_fragment_done_) {
558 // Restore deoptimization exception. When returning from the invoke stub,
559 // ArtMethod::Invoke() will see the special exception to know deoptimization
560 // is needed.
561 self_->SetException(Thread::GetDeoptimizationException());
562 } else {
563 // PC needs to be of the quick-to-interpreter bridge.
564 int32_t offset;
565 offset = GetThreadOffset<kRuntimePointerSize>(kQuickQuickToInterpreterBridge).Int32Value();
566 handler_quick_frame_pc_ = *reinterpret_cast<uintptr_t*>(
567 reinterpret_cast<uint8_t*>(self_) + offset);
568 }
569 }
570
DeoptimizeStack()571 void QuickExceptionHandler::DeoptimizeStack() {
572 DCHECK(is_deoptimization_);
573 if (kDebugExceptionDelivery) {
574 self_->DumpStack(LOG_STREAM(INFO) << "Deoptimizing: ");
575 }
576
577 DeoptimizeStackVisitor visitor(self_, context_, this, false);
578 visitor.WalkStack(true);
579 PrepareForLongJumpToInvokeStubOrInterpreterBridge();
580 }
581
DeoptimizeSingleFrame(DeoptimizationKind kind)582 void QuickExceptionHandler::DeoptimizeSingleFrame(DeoptimizationKind kind) {
583 DCHECK(is_deoptimization_);
584
585 DeoptimizeStackVisitor visitor(self_, context_, this, true);
586 visitor.WalkStack(true);
587
588 // Compiled code made an explicit deoptimization.
589 ArtMethod* deopt_method = visitor.GetSingleFrameDeoptMethod();
590 SCOPED_TRACE << "Deoptimizing "
591 << deopt_method->PrettyMethod()
592 << ": " << GetDeoptimizationKindName(kind);
593
594 DCHECK(deopt_method != nullptr);
595 if (VLOG_IS_ON(deopt) || kDebugExceptionDelivery) {
596 LOG(INFO) << "Single-frame deopting: "
597 << deopt_method->PrettyMethod()
598 << " due to "
599 << GetDeoptimizationKindName(kind);
600 DumpFramesWithType(self_, /* details= */ true);
601 }
602 if (Runtime::Current()->UseJitCompilation()) {
603 Runtime::Current()->GetJit()->GetCodeCache()->InvalidateCompiledCodeFor(
604 deopt_method, visitor.GetSingleFrameDeoptQuickMethodHeader());
605 } else {
606 // Transfer the code to interpreter.
607 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
608 deopt_method, GetQuickToInterpreterBridge());
609 }
610
611 PrepareForLongJumpToInvokeStubOrInterpreterBridge();
612 }
613
DeoptimizePartialFragmentFixup(uintptr_t return_pc)614 void QuickExceptionHandler::DeoptimizePartialFragmentFixup(uintptr_t return_pc) {
615 // At this point, the instrumentation stack has been updated. We need to install
616 // the real return pc on stack, in case instrumentation stub is stored there,
617 // so that the interpreter bridge code can return to the right place.
618 if (return_pc != 0) {
619 uintptr_t* pc_addr = reinterpret_cast<uintptr_t*>(handler_quick_frame_);
620 CHECK(pc_addr != nullptr);
621 pc_addr--;
622 *reinterpret_cast<uintptr_t*>(pc_addr) = return_pc;
623 }
624
625 // Architecture-dependent work. This is to get the LR right for x86 and x86-64.
626 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
627 // On x86, the return address is on the stack, so just reuse it. Otherwise we would have to
628 // change how longjump works.
629 handler_quick_frame_ = reinterpret_cast<ArtMethod**>(
630 reinterpret_cast<uintptr_t>(handler_quick_frame_) - sizeof(void*));
631 }
632 }
633
UpdateInstrumentationStack()634 uintptr_t QuickExceptionHandler::UpdateInstrumentationStack() {
635 DCHECK(is_deoptimization_) << "Non-deoptimization handlers should use FindCatch";
636 uintptr_t return_pc = 0;
637 if (method_tracing_active_) {
638 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
639 return_pc = instrumentation->PopFramesForDeoptimization(
640 self_, reinterpret_cast<uintptr_t>(handler_quick_frame_));
641 }
642 return return_pc;
643 }
644
DoLongJump(bool smash_caller_saves)645 void QuickExceptionHandler::DoLongJump(bool smash_caller_saves) {
646 // Place context back on thread so it will be available when we continue.
647 self_->ReleaseLongJumpContext(context_);
648 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
649 CHECK_NE(handler_quick_frame_pc_, 0u);
650 context_->SetPC(handler_quick_frame_pc_);
651 context_->SetArg0(handler_quick_arg0_);
652 if (smash_caller_saves) {
653 context_->SmashCallerSaves();
654 }
655 if (!is_deoptimization_ &&
656 handler_method_header_ != nullptr &&
657 handler_method_header_->IsNterpMethodHeader()) {
658 context_->SetNterpDexPC(reinterpret_cast<uintptr_t>(
659 GetHandlerMethod()->DexInstructions().Insns() + handler_dex_pc_));
660 }
661 context_->DoLongJump();
662 UNREACHABLE();
663 }
664
DumpFramesWithType(Thread * self,bool details)665 void QuickExceptionHandler::DumpFramesWithType(Thread* self, bool details) {
666 StackVisitor::WalkStack(
667 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
668 ArtMethod* method = stack_visitor->GetMethod();
669 if (details) {
670 LOG(INFO) << "|> pc = " << std::hex << stack_visitor->GetCurrentQuickFramePc();
671 LOG(INFO) << "|> addr = " << std::hex
672 << reinterpret_cast<uintptr_t>(stack_visitor->GetCurrentQuickFrame());
673 if (stack_visitor->GetCurrentQuickFrame() != nullptr && method != nullptr) {
674 LOG(INFO) << "|> ret = " << std::hex << stack_visitor->GetReturnPc();
675 }
676 }
677 if (method == nullptr) {
678 // Transition, do go on, we want to unwind over bridges, all the way.
679 if (details) {
680 LOG(INFO) << "N <transition>";
681 }
682 return true;
683 } else if (method->IsRuntimeMethod()) {
684 if (details) {
685 LOG(INFO) << "R " << method->PrettyMethod(true);
686 }
687 return true;
688 } else {
689 bool is_shadow = stack_visitor->GetCurrentShadowFrame() != nullptr;
690 LOG(INFO) << (is_shadow ? "S" : "Q")
691 << ((!is_shadow && stack_visitor->IsInInlinedFrame()) ? "i" : " ")
692 << " "
693 << method->PrettyMethod(true);
694 return true; // Go on.
695 }
696 },
697 self,
698 /* context= */ nullptr,
699 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
700 }
701
702 } // namespace art
703