blob: 1552318c1e7b9aff1b4704e07533d960348bd836 [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"
Andreas Gampe639bdd12015-06-03 11:22:45 -070023#include "entrypoints/quick/quick_entrypoints_enum.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070024#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070025#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "mirror/class-inl.h"
27#include "mirror/class_loader.h"
28#include "mirror/throwable.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010029#include "oat_quick_method_header.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010030#include "stack_map.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070031#include "verifier/method_verifier.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010032
33namespace art {
34
Ian Rogers5cf98192014-05-29 21:31:50 -070035static constexpr bool kDebugExceptionDelivery = false;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070036static constexpr size_t kInvalidFrameDepth = 0xffffffff;
Ian Rogers5cf98192014-05-29 21:31:50 -070037
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020038QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039 : self_(self),
40 context_(self->GetLongJumpContext()),
41 is_deoptimization_(is_deoptimization),
42 method_tracing_active_(is_deoptimization ||
43 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
44 handler_quick_frame_(nullptr),
45 handler_quick_frame_pc_(0),
46 handler_method_header_(nullptr),
47 handler_quick_arg0_(0),
48 handler_method_(nullptr),
49 handler_dex_pc_(0),
50 clear_exception_(false),
51 handler_frame_depth_(kInvalidFrameDepth) {}
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010052
Sebastien Hertz520633b2015-09-08 17:03:36 +020053// Finds catch handler.
Ian Rogers5cf98192014-05-29 21:31:50 -070054class CatchBlockStackVisitor FINAL : public StackVisitor {
55 public:
56 CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception,
57 QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -070058 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010059 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010060 exception_(exception),
Ian Rogers5cf98192014-05-29 21:31:50 -070061 exception_handler_(exception_handler) {
62 }
63
Mathieu Chartier90443472015-07-16 20:32:27 -070064 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 ArtMethod* method = GetMethod();
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070066 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -070067 if (method == nullptr) {
68 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
69 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
70 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010071 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -070072 uint32_t next_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 ArtMethod* next_art_method;
Ian Rogers5cf98192014-05-29 21:31:50 -070074 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
75 // Report the method that did the down call as the handler.
76 exception_handler_->SetHandlerDexPc(next_dex_pc);
77 exception_handler_->SetHandlerMethod(next_art_method);
78 if (!has_next) {
79 // No next method? Check exception handler is set up for the unhandled exception handler
80 // case.
81 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
82 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
83 }
84 return false; // End stack walk.
85 }
86 if (method->IsRuntimeMethod()) {
87 // Ignore callee save method.
88 DCHECK(method->IsCalleeSaveMethod());
89 return true;
90 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070091 return HandleTryItems(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070092 }
93
94 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -070095 bool HandleTryItems(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070096 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070097 uint32_t dex_pc = DexFile::kDexNoIndex;
98 if (!method->IsNative()) {
99 dex_pc = GetDexPc();
100 }
101 if (dex_pc != DexFile::kDexNoIndex) {
102 bool clear_exception = false;
Sebastien Hertz26f72862015-09-15 09:52:07 +0200103 StackHandleScope<1> hs(GetThread());
Ian Rogers5cf98192014-05-29 21:31:50 -0700104 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -0700106 exception_handler_->SetClearException(clear_exception);
107 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 exception_handler_->SetHandlerMethod(method);
Ian Rogers5cf98192014-05-29 21:31:50 -0700109 exception_handler_->SetHandlerDexPc(found_dex_pc);
David Brazdil72f7b882015-09-15 17:00:52 +0100110 exception_handler_->SetHandlerQuickFramePc(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100111 GetCurrentOatQuickMethodHeader()->ToNativeQuickPc(
112 method, found_dex_pc, /* is_catch_handler */ true));
Ian Rogers5cf98192014-05-29 21:31:50 -0700113 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100114 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -0700115 return false; // End stack walk.
Mingyao Yang99170c62015-07-06 11:10:37 -0700116 } else if (UNLIKELY(GetThread()->HasDebuggerShadowFrames())) {
117 // We are going to unwind this frame. Did we prepare a shadow frame for debugging?
118 size_t frame_id = GetFrameId();
119 ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(frame_id);
120 if (frame != nullptr) {
121 // We will not execute this shadow frame so we can safely deallocate it.
122 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
123 ShadowFrame::DeleteDeoptimizedFrame(frame);
124 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700125 }
126 }
127 return true; // Continue stack walk.
128 }
129
Ian Rogers5cf98192014-05-29 21:31:50 -0700130 // The exception we're looking for the catch block of.
131 Handle<mirror::Throwable>* exception_;
132 // The quick exception handler we're visiting for.
133 QuickExceptionHandler* const exception_handler_;
134
135 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
136};
137
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000138void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200139 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700140 if (kDebugExceptionDelivery) {
141 mirror::String* msg = exception->GetDetailMessage();
142 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
143 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
144 << ": " << str_msg << "\n");
145 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 StackHandleScope<1> hs(self_);
147 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200148
Sebastien Hertz520633b2015-09-08 17:03:36 +0200149 // Walk the stack to find catch handler.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100151 visitor.WalkStack(true);
152
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200153 if (kDebugExceptionDelivery) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700154 if (*handler_quick_frame_ == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100155 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700156 }
157 if (handler_method_ != nullptr) {
158 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
159 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
160 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100161 }
162 }
163 if (clear_exception_) {
164 // Exception was cleared as part of delivery.
165 DCHECK(!self_->IsExceptionPending());
166 } else {
167 // Put exception back in root set with clear throw location.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000168 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100169 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000170 // If the handler is in optimized code, we need to set the catch environment.
171 if (*handler_quick_frame_ != nullptr &&
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100172 handler_method_header_ != nullptr &&
173 handler_method_header_->IsOptimized()) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000174 SetCatchEnvironmentForOptimizedHandler(&visitor);
175 }
176}
177
178static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) {
179 // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind
180 // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to
181 // distinguish between core/FPU registers and low/high bits on 64-bit.
182 switch (kind) {
183 case DexRegisterLocation::Kind::kConstant:
184 case DexRegisterLocation::Kind::kInStack:
185 // VRegKind is ignored.
186 return VRegKind::kUndefined;
187
188 case DexRegisterLocation::Kind::kInRegister:
189 // Selects core register. For 64-bit registers, selects low 32 bits.
190 return VRegKind::kLongLoVReg;
191
192 case DexRegisterLocation::Kind::kInRegisterHigh:
193 // Selects core register. For 64-bit registers, selects high 32 bits.
194 return VRegKind::kLongHiVReg;
195
196 case DexRegisterLocation::Kind::kInFpuRegister:
197 // Selects FPU register. For 64-bit registers, selects low 32 bits.
198 return VRegKind::kDoubleLoVReg;
199
200 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
201 // Selects FPU register. For 64-bit registers, selects high 32 bits.
202 return VRegKind::kDoubleHiVReg;
203
204 default:
205 LOG(FATAL) << "Unexpected vreg location "
206 << DexRegisterLocation::PrettyDescriptor(kind);
207 UNREACHABLE();
208 }
209}
210
211void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) {
212 DCHECK(!is_deoptimization_);
213 DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions";
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100214 DCHECK(handler_method_ != nullptr && handler_method_header_->IsOptimized());
David Brazdil77a48ae2015-09-15 12:34:04 +0000215
216 if (kDebugExceptionDelivery) {
217 self_->DumpStack(LOG(INFO) << "Setting catch phis: ");
218 }
219
220 const size_t number_of_vregs = handler_method_->GetCodeItem()->registers_size_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100221 CodeInfo code_info = handler_method_header_->GetOptimizedCodeInfo();
David Brazdil77a48ae2015-09-15 12:34:04 +0000222 StackMapEncoding encoding = code_info.ExtractEncoding();
223
224 // Find stack map of the throwing instruction.
225 StackMap throw_stack_map =
226 code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset(), encoding);
227 DCHECK(throw_stack_map.IsValid());
228 DexRegisterMap throw_vreg_map =
229 code_info.GetDexRegisterMapOf(throw_stack_map, encoding, number_of_vregs);
230
231 // Find stack map of the catch block.
232 StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc(), encoding);
233 DCHECK(catch_stack_map.IsValid());
234 DexRegisterMap catch_vreg_map =
235 code_info.GetDexRegisterMapOf(catch_stack_map, encoding, number_of_vregs);
236
237 // Copy values between them.
238 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
239 DexRegisterLocation::Kind catch_location =
240 catch_vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
241 if (catch_location == DexRegisterLocation::Kind::kNone) {
242 continue;
243 }
244 DCHECK(catch_location == DexRegisterLocation::Kind::kInStack);
245
246 // Get vreg value from its current location.
247 uint32_t vreg_value;
248 VRegKind vreg_kind = ToVRegKind(throw_vreg_map.GetLocationKind(vreg,
249 number_of_vregs,
250 code_info,
251 encoding));
252 bool get_vreg_success = stack_visitor->GetVReg(stack_visitor->GetMethod(),
253 vreg,
254 vreg_kind,
255 &vreg_value);
256 CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out ("
257 << "method=" << PrettyMethod(stack_visitor->GetMethod()) << ", "
258 << "dex_pc=" << stack_visitor->GetDexPc() << ", "
259 << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")";
260
261 // Copy value to the catch phi's stack slot.
262 int32_t slot_offset = catch_vreg_map.GetStackOffsetInBytes(vreg,
263 number_of_vregs,
264 code_info,
265 encoding);
266 ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame();
267 uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset;
268 uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address);
269 *slot_ptr = vreg_value;
270 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200271}
272
Ian Rogers5cf98192014-05-29 21:31:50 -0700273// Prepares deoptimization.
274class DeoptimizeStackVisitor FINAL : public StackVisitor {
275 public:
Andreas Gampe639bdd12015-06-03 11:22:45 -0700276 DeoptimizeStackVisitor(Thread* self,
277 Context* context,
278 QuickExceptionHandler* exception_handler,
279 bool single_frame)
Mathieu Chartier90443472015-07-16 20:32:27 -0700280 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100281 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100282 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700283 prev_shadow_frame_(nullptr),
Andreas Gampe639bdd12015-06-03 11:22:45 -0700284 stacked_shadow_frame_pushed_(false),
285 single_frame_deopt_(single_frame),
286 single_frame_done_(false) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700287 }
288
Mathieu Chartier90443472015-07-16 20:32:27 -0700289 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700290 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700291 ArtMethod* method = GetMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700292 if (method == nullptr || single_frame_done_) {
293 // This is the upcall (or the next full frame in single-frame deopt), we remember the frame
294 // and last pc so that we may long jump to them.
Ian Rogers5cf98192014-05-29 21:31:50 -0700295 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
296 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100297 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700298 if (!stacked_shadow_frame_pushed_) {
299 // In case there is no deoptimized shadow frame for this upcall, we still
300 // need to push a nullptr to the stack since there is always a matching pop after
301 // the long jump.
Sebastien Hertz26f72862015-09-15 09:52:07 +0200302 GetThread()->PushStackedShadowFrame(nullptr,
303 StackedShadowFrameType::kDeoptimizationShadowFrame);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700304 stacked_shadow_frame_pushed_ = true;
305 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700306 return false; // End stack walk.
307 } else if (method->IsRuntimeMethod()) {
308 // Ignore callee save method.
309 DCHECK(method->IsCalleeSaveMethod());
310 return true;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200311 } else if (method->IsNative()) {
312 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
313 // the native method.
314 // The top method is a runtime method, the native method comes next.
315 CHECK_EQ(GetFrameDepth(), 1U);
316 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700317 } else {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100318 // Check if a shadow frame already exists for debugger's set-local-value purpose.
319 const size_t frame_id = GetFrameId();
320 ShadowFrame* new_frame = GetThread()->FindDebuggerShadowFrame(frame_id);
321 const bool* updated_vregs;
322 const size_t num_regs = method->GetCodeItem()->registers_size_;
323 if (new_frame == nullptr) {
324 new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, method, GetDexPc());
325 updated_vregs = nullptr;
326 } else {
327 updated_vregs = GetThread()->GetUpdatedVRegFlags(frame_id);
328 DCHECK(updated_vregs != nullptr);
329 }
330 if (GetCurrentOatQuickMethodHeader()->IsOptimized()) {
331 HandleOptimizingDeoptimization(method, new_frame, updated_vregs);
332 } else {
333 HandleQuickDeoptimization(method, new_frame, updated_vregs);
334 }
335 if (updated_vregs != nullptr) {
336 // Calling Thread::RemoveDebuggerShadowFrameMapping will also delete the updated_vregs
337 // array so this must come after we processed the frame.
338 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
339 DCHECK(GetThread()->FindDebuggerShadowFrame(frame_id) == nullptr);
340 }
341 if (prev_shadow_frame_ != nullptr) {
342 prev_shadow_frame_->SetLink(new_frame);
343 } else {
344 // Will be popped after the long jump after DeoptimizeStack(),
345 // right before interpreter::EnterInterpreterFromDeoptimize().
346 stacked_shadow_frame_pushed_ = true;
347 GetThread()->PushStackedShadowFrame(
348 new_frame,
349 single_frame_deopt_
350 ? StackedShadowFrameType::kSingleFrameDeoptimizationShadowFrame
351 : StackedShadowFrameType::kDeoptimizationShadowFrame);
352 }
353 prev_shadow_frame_ = new_frame;
354
Andreas Gampe639bdd12015-06-03 11:22:45 -0700355 if (single_frame_deopt_ && !IsInInlinedFrame()) {
356 // Single-frame deopt ends at the first non-inlined frame and needs to store that method.
357 exception_handler_->SetHandlerQuickArg0(reinterpret_cast<uintptr_t>(method));
358 single_frame_done_ = true;
359 }
360 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700361 }
362 }
363
364 private:
Nicolas Geoffray33856502015-10-20 15:52:58 +0100365 void HandleOptimizingDeoptimization(ArtMethod* m,
366 ShadowFrame* new_frame,
367 const bool* updated_vregs)
368 SHARED_REQUIRES(Locks::mutator_lock_) {
369 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
370 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
371 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
372 StackMapEncoding encoding = code_info.ExtractEncoding();
373 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
374 const size_t number_of_vregs = m->GetCodeItem()->registers_size_;
Nicolas Geoffray33856502015-10-20 15:52:58 +0100375 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
376 uint32_t register_mask = stack_map.GetRegisterMask(encoding);
David Brazdilefc3f022015-10-28 12:19:06 -0500377 DexRegisterMap vreg_map = IsInInlinedFrame()
378 ? code_info.GetDexRegisterMapAtDepth(GetCurrentInliningDepth() - 1,
379 code_info.GetInlineInfoOf(stack_map, encoding),
380 encoding,
381 number_of_vregs)
382 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100383
384 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
385 if (updated_vregs != nullptr && updated_vregs[vreg]) {
386 // Keep the value set by debugger.
387 continue;
388 }
389
390 DexRegisterLocation::Kind location =
391 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
392 static constexpr uint32_t kDeadValue = 0xEBADDE09;
393 uint32_t value = kDeadValue;
394 bool is_reference = false;
395
396 switch (location) {
397 case DexRegisterLocation::Kind::kInStack: {
398 const int32_t offset = vreg_map.GetStackOffsetInBytes(vreg,
399 number_of_vregs,
400 code_info,
401 encoding);
402 const uint8_t* addr = reinterpret_cast<const uint8_t*>(GetCurrentQuickFrame()) + offset;
403 value = *reinterpret_cast<const uint32_t*>(addr);
404 uint32_t bit = (offset >> 2);
405 if (stack_mask.size_in_bits() > bit && stack_mask.LoadBit(bit)) {
406 is_reference = true;
407 }
408 break;
409 }
410 case DexRegisterLocation::Kind::kInRegister:
411 case DexRegisterLocation::Kind::kInRegisterHigh:
412 case DexRegisterLocation::Kind::kInFpuRegister:
413 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
414 uint32_t reg = vreg_map.GetMachineRegister(vreg, number_of_vregs, code_info, encoding);
415 bool result = GetRegisterIfAccessible(reg, ToVRegKind(location), &value);
416 CHECK(result);
417 if (location == DexRegisterLocation::Kind::kInRegister) {
418 if (((1u << reg) & register_mask) != 0) {
419 is_reference = true;
420 }
421 }
422 break;
423 }
424 case DexRegisterLocation::Kind::kConstant: {
425 value = vreg_map.GetConstant(vreg, number_of_vregs, code_info, encoding);
426 if (value == 0) {
427 // Make it a reference for extra safety.
428 is_reference = true;
429 }
430 break;
431 }
432 case DexRegisterLocation::Kind::kNone: {
433 break;
434 }
435 default: {
436 LOG(FATAL)
437 << "Unexpected location kind"
438 << DexRegisterLocation::PrettyDescriptor(
439 vreg_map.GetLocationInternalKind(vreg,
440 number_of_vregs,
441 code_info,
442 encoding));
443 UNREACHABLE();
444 }
445 }
446 if (is_reference) {
447 new_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(value));
448 } else {
449 new_frame->SetVReg(vreg, value);
450 }
451 }
452 }
453
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200454 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
455 return static_cast<VRegKind>(kinds.at(reg * 2));
456 }
457
Nicolas Geoffray33856502015-10-20 15:52:58 +0100458 void HandleQuickDeoptimization(ArtMethod* m,
459 ShadowFrame* new_frame,
460 const bool* updated_vregs)
461 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700462 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertz520633b2015-09-08 17:03:36 +0200463 CHECK(code_item != nullptr) << "No code item for " << PrettyMethod(m);
Ian Rogers5cf98192014-05-29 21:31:50 -0700464 uint16_t num_regs = code_item->registers_size_;
465 uint32_t dex_pc = GetDexPc();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100466 StackHandleScope<2> hs(GetThread()); // Dex cache and class loader.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700467 mirror::Class* declaring_class = m->GetDeclaringClass();
468 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
469 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Sebastien Hertz26f72862015-09-15 09:52:07 +0200470 verifier::MethodVerifier verifier(GetThread(), h_dex_cache->GetDexFile(), h_dex_cache,
471 h_class_loader, &m->GetClassDef(), code_item,
472 m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, true,
473 true, true);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800474 bool verifier_success = verifier.Verify();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475 CHECK(verifier_success) << PrettyMethod(m);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700476 {
Sebastien Hertz26f72862015-09-15 09:52:07 +0200477 ScopedStackedShadowFramePusher pusher(GetThread(), new_frame,
Sebastien Hertzf7958692015-06-09 14:09:14 +0200478 StackedShadowFrameType::kShadowFrameUnderConstruction);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700479 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000480
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700481 // Markers for dead values, used when the verifier knows a Dex register is undefined,
482 // or when the compiler knows the register has not been initialized, or is not used
483 // anymore in the method.
484 static constexpr uint32_t kDeadValue = 0xEBADDE09;
485 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
486 for (uint16_t reg = 0; reg < num_regs; ++reg) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700487 if (updated_vregs != nullptr && updated_vregs[reg]) {
488 // Keep the value set by debugger.
489 continue;
490 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700491 VRegKind kind = GetVRegKind(reg, kinds);
492 switch (kind) {
493 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000494 new_frame->SetVReg(reg, kDeadValue);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700495 break;
496 case kConstant:
497 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
498 break;
499 case kReferenceVReg: {
500 uint32_t value = 0;
501 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
502 // We don't want to copy a stale reference into the shadow frame as a reference.
503 // b/20736048
504 if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) {
505 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
506 } else {
507 new_frame->SetVReg(reg, kDeadValue);
508 }
509 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000510 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700511 case kLongLoVReg:
512 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
513 // Treat it as a "long" register pair.
514 uint64_t value = 0;
515 if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) {
516 new_frame->SetVRegLong(reg, value);
517 } else {
518 new_frame->SetVRegLong(reg, kLongDeadValue);
519 }
520 } else {
521 uint32_t value = 0;
522 if (GetVReg(m, reg, kind, &value)) {
523 new_frame->SetVReg(reg, value);
524 } else {
525 new_frame->SetVReg(reg, kDeadValue);
526 }
527 }
528 break;
529 case kLongHiVReg:
530 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
531 // Nothing to do: we treated it as a "long" register pair.
532 } else {
533 uint32_t value = 0;
534 if (GetVReg(m, reg, kind, &value)) {
535 new_frame->SetVReg(reg, value);
536 } else {
537 new_frame->SetVReg(reg, kDeadValue);
538 }
539 }
540 break;
541 case kDoubleLoVReg:
542 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
543 uint64_t value = 0;
544 if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
545 // Treat it as a "double" register pair.
546 new_frame->SetVRegLong(reg, value);
547 } else {
548 new_frame->SetVRegLong(reg, kLongDeadValue);
549 }
550 } else {
551 uint32_t value = 0;
552 if (GetVReg(m, reg, kind, &value)) {
553 new_frame->SetVReg(reg, value);
554 } else {
555 new_frame->SetVReg(reg, kDeadValue);
556 }
557 }
558 break;
559 case kDoubleHiVReg:
560 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
561 // Nothing to do: we treated it as a "double" register pair.
562 } else {
563 uint32_t value = 0;
564 if (GetVReg(m, reg, kind, &value)) {
565 new_frame->SetVReg(reg, value);
566 } else {
567 new_frame->SetVReg(reg, kDeadValue);
568 }
569 }
570 break;
571 default:
572 uint32_t value = 0;
573 if (GetVReg(m, reg, kind, &value)) {
574 new_frame->SetVReg(reg, value);
575 } else {
576 new_frame->SetVReg(reg, kDeadValue);
577 }
578 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000579 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700580 }
581 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700582 }
583
Ian Rogers5cf98192014-05-29 21:31:50 -0700584 QuickExceptionHandler* const exception_handler_;
585 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700586 bool stacked_shadow_frame_pushed_;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700587 const bool single_frame_deopt_;
588 bool single_frame_done_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700589
590 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
591};
592
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200593void QuickExceptionHandler::DeoptimizeStack() {
594 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700595 if (kDebugExceptionDelivery) {
596 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
597 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200598
Andreas Gampe639bdd12015-06-03 11:22:45 -0700599 DeoptimizeStackVisitor visitor(self_, context_, this, false);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200600 visitor.WalkStack(true);
601
602 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000603 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100604}
605
Andreas Gampe639bdd12015-06-03 11:22:45 -0700606void QuickExceptionHandler::DeoptimizeSingleFrame() {
607 DCHECK(is_deoptimization_);
608
609 if (VLOG_IS_ON(deopt) || kDebugExceptionDelivery) {
610 LOG(INFO) << "Single-frame deopting:";
611 DumpFramesWithType(self_, true);
612 }
613
614 DeoptimizeStackVisitor visitor(self_, context_, this, true);
615 visitor.WalkStack(true);
616
617 // PC needs to be of the quick-to-interpreter bridge.
618 int32_t offset;
619 #ifdef __LP64__
620 offset = GetThreadOffset<8>(kQuickQuickToInterpreterBridge).Int32Value();
621 #else
622 offset = GetThreadOffset<4>(kQuickQuickToInterpreterBridge).Int32Value();
623 #endif
624 handler_quick_frame_pc_ = *reinterpret_cast<uintptr_t*>(
625 reinterpret_cast<uint8_t*>(self_) + offset);
626}
627
628void QuickExceptionHandler::DeoptimizeSingleFrameArchDependentFixup() {
629 // Architecture-dependent work. This is to get the LR right for x86 and x86-64.
630
631 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
632 // On x86, the return address is on the stack, so just reuse it. Otherwise we would have to
633 // change how longjump works.
634 handler_quick_frame_ = reinterpret_cast<ArtMethod**>(
635 reinterpret_cast<uintptr_t>(handler_quick_frame_) - sizeof(void*));
636 }
637}
638
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100639// Unwinds all instrumentation stack frame prior to catch handler or upcall.
640class InstrumentationStackVisitor : public StackVisitor {
641 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700642 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -0700643 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100644 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700645 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100646 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700647 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100648 }
649
Mathieu Chartier90443472015-07-16 20:32:27 -0700650 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700651 size_t current_frame_depth = GetFrameDepth();
652 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100653 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700654 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100655 if (!IsInInlinedFrame()) {
656 // We do not count inlined frames, because we do not instrument them. The reason we
657 // include them in the stack walking is the check against `frame_depth_`, which is
658 // given to us by a visitor that visits inlined frames.
659 ++instrumentation_frames_to_pop_;
660 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100661 }
662 return true;
663 } else {
664 // We reached the frame of the catch handler or the upcall.
665 return false;
666 }
667 }
668
669 size_t GetInstrumentationFramesToPop() const {
670 return instrumentation_frames_to_pop_;
671 }
672
673 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700674 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100675 size_t instrumentation_frames_to_pop_;
676
677 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
678};
679
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200680void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100681 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700682 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100683 visitor.WalkStack(true);
684
685 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
686 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
687 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
688 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
689 }
690 }
691}
692
Andreas Gampe639bdd12015-06-03 11:22:45 -0700693void QuickExceptionHandler::DoLongJump(bool smash_caller_saves) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100694 // Place context back on thread so it will be available when we continue.
695 self_->ReleaseLongJumpContext(context_);
696 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
697 CHECK_NE(handler_quick_frame_pc_, 0u);
698 context_->SetPC(handler_quick_frame_pc_);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700699 context_->SetArg0(handler_quick_arg0_);
700 if (smash_caller_saves) {
701 context_->SmashCallerSaves();
702 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100703 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800704 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100705}
706
Andreas Gampe639bdd12015-06-03 11:22:45 -0700707// Prints out methods with their type of frame.
708class DumpFramesWithTypeStackVisitor FINAL : public StackVisitor {
709 public:
710 DumpFramesWithTypeStackVisitor(Thread* self, bool show_details = false)
711 SHARED_REQUIRES(Locks::mutator_lock_)
712 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
713 show_details_(show_details) {}
714
715 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
716 ArtMethod* method = GetMethod();
717 if (show_details_) {
718 LOG(INFO) << "|> pc = " << std::hex << GetCurrentQuickFramePc();
719 LOG(INFO) << "|> addr = " << std::hex << reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
720 if (GetCurrentQuickFrame() != nullptr && method != nullptr) {
721 LOG(INFO) << "|> ret = " << std::hex << GetReturnPc();
722 }
723 }
724 if (method == nullptr) {
725 // Transition, do go on, we want to unwind over bridges, all the way.
726 if (show_details_) {
727 LOG(INFO) << "N <transition>";
728 }
729 return true;
730 } else if (method->IsRuntimeMethod()) {
731 if (show_details_) {
732 LOG(INFO) << "R " << PrettyMethod(method, true);
733 }
734 return true;
735 } else {
736 bool is_shadow = GetCurrentShadowFrame() != nullptr;
737 LOG(INFO) << (is_shadow ? "S" : "Q")
738 << ((!is_shadow && IsInInlinedFrame()) ? "i" : " ")
739 << " "
740 << PrettyMethod(method, true);
741 return true; // Go on.
742 }
743 }
744
745 private:
746 bool show_details_;
747
748 DISALLOW_COPY_AND_ASSIGN(DumpFramesWithTypeStackVisitor);
749};
750
751void QuickExceptionHandler::DumpFramesWithType(Thread* self, bool details) {
752 DumpFramesWithTypeStackVisitor visitor(self, details);
753 visitor.WalkStack(true);
754}
755
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100756} // namespace art