blob: 6317f5e40147d27b89a6fd930fb29c677a12b80d [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"
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +000026#include "jit/jit.h"
27#include "jit/jit_code_cache.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "mirror/class-inl.h"
29#include "mirror/class_loader.h"
30#include "mirror/throwable.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010031#include "oat_quick_method_header.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010032#include "stack_map.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070033#include "verifier/method_verifier.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010034
35namespace art {
36
Ian Rogers5cf98192014-05-29 21:31:50 -070037static constexpr bool kDebugExceptionDelivery = false;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070038static constexpr size_t kInvalidFrameDepth = 0xffffffff;
Ian Rogers5cf98192014-05-29 21:31:50 -070039
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020040QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010041 : self_(self),
42 context_(self->GetLongJumpContext()),
43 is_deoptimization_(is_deoptimization),
44 method_tracing_active_(is_deoptimization ||
45 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
46 handler_quick_frame_(nullptr),
47 handler_quick_frame_pc_(0),
48 handler_method_header_(nullptr),
49 handler_quick_arg0_(0),
50 handler_method_(nullptr),
51 handler_dex_pc_(0),
52 clear_exception_(false),
53 handler_frame_depth_(kInvalidFrameDepth) {}
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010054
Sebastien Hertz520633b2015-09-08 17:03:36 +020055// Finds catch handler.
Ian Rogers5cf98192014-05-29 21:31:50 -070056class CatchBlockStackVisitor FINAL : public StackVisitor {
57 public:
58 CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception,
59 QuickExceptionHandler* exception_handler)
Mathieu Chartier90443472015-07-16 20:32:27 -070060 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010061 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010062 exception_(exception),
Ian Rogers5cf98192014-05-29 21:31:50 -070063 exception_handler_(exception_handler) {
64 }
65
Mathieu Chartier90443472015-07-16 20:32:27 -070066 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070067 ArtMethod* method = GetMethod();
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070068 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -070069 if (method == nullptr) {
70 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
71 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
72 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010073 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -070074 uint32_t next_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -070075 ArtMethod* next_art_method;
Ian Rogers5cf98192014-05-29 21:31:50 -070076 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
77 // Report the method that did the down call as the handler.
78 exception_handler_->SetHandlerDexPc(next_dex_pc);
79 exception_handler_->SetHandlerMethod(next_art_method);
80 if (!has_next) {
81 // No next method? Check exception handler is set up for the unhandled exception handler
82 // case.
83 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
84 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
85 }
86 return false; // End stack walk.
87 }
88 if (method->IsRuntimeMethod()) {
89 // Ignore callee save method.
90 DCHECK(method->IsCalleeSaveMethod());
91 return true;
92 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 return HandleTryItems(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070094 }
95
96 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -070097 bool HandleTryItems(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -070099 uint32_t dex_pc = DexFile::kDexNoIndex;
100 if (!method->IsNative()) {
101 dex_pc = GetDexPc();
102 }
103 if (dex_pc != DexFile::kDexNoIndex) {
104 bool clear_exception = false;
Sebastien Hertz26f72862015-09-15 09:52:07 +0200105 StackHandleScope<1> hs(GetThread());
Ian Rogers5cf98192014-05-29 21:31:50 -0700106 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700107 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -0700108 exception_handler_->SetClearException(clear_exception);
109 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 exception_handler_->SetHandlerMethod(method);
Ian Rogers5cf98192014-05-29 21:31:50 -0700111 exception_handler_->SetHandlerDexPc(found_dex_pc);
David Brazdil72f7b882015-09-15 17:00:52 +0100112 exception_handler_->SetHandlerQuickFramePc(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100113 GetCurrentOatQuickMethodHeader()->ToNativeQuickPc(
114 method, found_dex_pc, /* is_catch_handler */ true));
Ian Rogers5cf98192014-05-29 21:31:50 -0700115 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100116 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -0700117 return false; // End stack walk.
Mingyao Yang99170c62015-07-06 11:10:37 -0700118 } else if (UNLIKELY(GetThread()->HasDebuggerShadowFrames())) {
119 // We are going to unwind this frame. Did we prepare a shadow frame for debugging?
120 size_t frame_id = GetFrameId();
121 ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(frame_id);
122 if (frame != nullptr) {
123 // We will not execute this shadow frame so we can safely deallocate it.
124 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
125 ShadowFrame::DeleteDeoptimizedFrame(frame);
126 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700127 }
128 }
129 return true; // Continue stack walk.
130 }
131
Ian Rogers5cf98192014-05-29 21:31:50 -0700132 // The exception we're looking for the catch block of.
133 Handle<mirror::Throwable>* exception_;
134 // The quick exception handler we're visiting for.
135 QuickExceptionHandler* const exception_handler_;
136
137 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
138};
139
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000140void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200141 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700142 if (kDebugExceptionDelivery) {
143 mirror::String* msg = exception->GetDetailMessage();
144 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
145 self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception)
146 << ": " << str_msg << "\n");
147 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700148 StackHandleScope<1> hs(self_);
149 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200150
Sebastien Hertz520633b2015-09-08 17:03:36 +0200151 // Walk the stack to find catch handler.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100153 visitor.WalkStack(true);
154
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200155 if (kDebugExceptionDelivery) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156 if (*handler_quick_frame_ == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100157 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700158 }
159 if (handler_method_ != nullptr) {
160 const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
161 int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_);
162 LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100163 }
164 }
165 if (clear_exception_) {
166 // Exception was cleared as part of delivery.
167 DCHECK(!self_->IsExceptionPending());
168 } else {
169 // Put exception back in root set with clear throw location.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000170 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100171 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000172 // If the handler is in optimized code, we need to set the catch environment.
173 if (*handler_quick_frame_ != nullptr &&
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100174 handler_method_header_ != nullptr &&
175 handler_method_header_->IsOptimized()) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000176 SetCatchEnvironmentForOptimizedHandler(&visitor);
177 }
178}
179
180static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) {
181 // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind
182 // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to
183 // distinguish between core/FPU registers and low/high bits on 64-bit.
184 switch (kind) {
185 case DexRegisterLocation::Kind::kConstant:
186 case DexRegisterLocation::Kind::kInStack:
187 // VRegKind is ignored.
188 return VRegKind::kUndefined;
189
190 case DexRegisterLocation::Kind::kInRegister:
191 // Selects core register. For 64-bit registers, selects low 32 bits.
192 return VRegKind::kLongLoVReg;
193
194 case DexRegisterLocation::Kind::kInRegisterHigh:
195 // Selects core register. For 64-bit registers, selects high 32 bits.
196 return VRegKind::kLongHiVReg;
197
198 case DexRegisterLocation::Kind::kInFpuRegister:
199 // Selects FPU register. For 64-bit registers, selects low 32 bits.
200 return VRegKind::kDoubleLoVReg;
201
202 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
203 // Selects FPU register. For 64-bit registers, selects high 32 bits.
204 return VRegKind::kDoubleHiVReg;
205
206 default:
David Srbecky7dc11782016-02-25 13:23:56 +0000207 LOG(FATAL) << "Unexpected vreg location " << kind;
David Brazdil77a48ae2015-09-15 12:34:04 +0000208 UNREACHABLE();
209 }
210}
211
212void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) {
213 DCHECK(!is_deoptimization_);
214 DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions";
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100215 DCHECK(handler_method_ != nullptr && handler_method_header_->IsOptimized());
David Brazdil77a48ae2015-09-15 12:34:04 +0000216
217 if (kDebugExceptionDelivery) {
218 self_->DumpStack(LOG(INFO) << "Setting catch phis: ");
219 }
220
221 const size_t number_of_vregs = handler_method_->GetCodeItem()->registers_size_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100222 CodeInfo code_info = handler_method_header_->GetOptimizedCodeInfo();
David Brazdil77a48ae2015-09-15 12:34:04 +0000223 StackMapEncoding encoding = code_info.ExtractEncoding();
224
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000225 // Find stack map of the catch block.
226 StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc(), encoding);
227 DCHECK(catch_stack_map.IsValid());
228 DexRegisterMap catch_vreg_map =
229 code_info.GetDexRegisterMapOf(catch_stack_map, encoding, number_of_vregs);
230 if (!catch_vreg_map.IsValid()) {
231 return;
232 }
233
David Brazdil77a48ae2015-09-15 12:34:04 +0000234 // Find stack map of the throwing instruction.
235 StackMap throw_stack_map =
236 code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset(), encoding);
237 DCHECK(throw_stack_map.IsValid());
238 DexRegisterMap throw_vreg_map =
239 code_info.GetDexRegisterMapOf(throw_stack_map, encoding, number_of_vregs);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000240 DCHECK(throw_vreg_map.IsValid());
David Brazdil77a48ae2015-09-15 12:34:04 +0000241
242 // Copy values between them.
243 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
244 DexRegisterLocation::Kind catch_location =
245 catch_vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
246 if (catch_location == DexRegisterLocation::Kind::kNone) {
247 continue;
248 }
249 DCHECK(catch_location == DexRegisterLocation::Kind::kInStack);
250
251 // Get vreg value from its current location.
252 uint32_t vreg_value;
253 VRegKind vreg_kind = ToVRegKind(throw_vreg_map.GetLocationKind(vreg,
254 number_of_vregs,
255 code_info,
256 encoding));
257 bool get_vreg_success = stack_visitor->GetVReg(stack_visitor->GetMethod(),
258 vreg,
259 vreg_kind,
260 &vreg_value);
261 CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out ("
262 << "method=" << PrettyMethod(stack_visitor->GetMethod()) << ", "
263 << "dex_pc=" << stack_visitor->GetDexPc() << ", "
264 << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")";
265
266 // Copy value to the catch phi's stack slot.
267 int32_t slot_offset = catch_vreg_map.GetStackOffsetInBytes(vreg,
268 number_of_vregs,
269 code_info,
270 encoding);
271 ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame();
272 uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset;
273 uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address);
274 *slot_ptr = vreg_value;
275 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200276}
277
Ian Rogers5cf98192014-05-29 21:31:50 -0700278// Prepares deoptimization.
279class DeoptimizeStackVisitor FINAL : public StackVisitor {
280 public:
Andreas Gampe639bdd12015-06-03 11:22:45 -0700281 DeoptimizeStackVisitor(Thread* self,
282 Context* context,
283 QuickExceptionHandler* exception_handler,
284 bool single_frame)
Mathieu Chartier90443472015-07-16 20:32:27 -0700285 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100286 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100287 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700288 prev_shadow_frame_(nullptr),
Andreas Gampe639bdd12015-06-03 11:22:45 -0700289 stacked_shadow_frame_pushed_(false),
290 single_frame_deopt_(single_frame),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100291 single_frame_done_(false),
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000292 single_frame_deopt_method_(nullptr),
293 single_frame_deopt_quick_method_header_(nullptr) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100294 }
295
296 ArtMethod* GetSingleFrameDeoptMethod() const {
297 return single_frame_deopt_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700298 }
299
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000300 const OatQuickMethodHeader* GetSingleFrameDeoptQuickMethodHeader() const {
301 return single_frame_deopt_quick_method_header_;
302 }
303
Mathieu Chartier90443472015-07-16 20:32:27 -0700304 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700305 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 ArtMethod* method = GetMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700307 if (method == nullptr || single_frame_done_) {
308 // This is the upcall (or the next full frame in single-frame deopt), we remember the frame
309 // and last pc so that we may long jump to them.
Ian Rogers5cf98192014-05-29 21:31:50 -0700310 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
311 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100312 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700313 if (!stacked_shadow_frame_pushed_) {
314 // In case there is no deoptimized shadow frame for this upcall, we still
315 // need to push a nullptr to the stack since there is always a matching pop after
316 // the long jump.
Sebastien Hertz26f72862015-09-15 09:52:07 +0200317 GetThread()->PushStackedShadowFrame(nullptr,
318 StackedShadowFrameType::kDeoptimizationShadowFrame);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700319 stacked_shadow_frame_pushed_ = true;
320 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700321 return false; // End stack walk.
322 } else if (method->IsRuntimeMethod()) {
323 // Ignore callee save method.
324 DCHECK(method->IsCalleeSaveMethod());
325 return true;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200326 } else if (method->IsNative()) {
327 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
328 // the native method.
329 // The top method is a runtime method, the native method comes next.
330 CHECK_EQ(GetFrameDepth(), 1U);
331 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700332 } else {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100333 // Check if a shadow frame already exists for debugger's set-local-value purpose.
334 const size_t frame_id = GetFrameId();
335 ShadowFrame* new_frame = GetThread()->FindDebuggerShadowFrame(frame_id);
336 const bool* updated_vregs;
337 const size_t num_regs = method->GetCodeItem()->registers_size_;
338 if (new_frame == nullptr) {
339 new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, method, GetDexPc());
340 updated_vregs = nullptr;
341 } else {
342 updated_vregs = GetThread()->GetUpdatedVRegFlags(frame_id);
343 DCHECK(updated_vregs != nullptr);
344 }
345 if (GetCurrentOatQuickMethodHeader()->IsOptimized()) {
346 HandleOptimizingDeoptimization(method, new_frame, updated_vregs);
347 } else {
348 HandleQuickDeoptimization(method, new_frame, updated_vregs);
349 }
350 if (updated_vregs != nullptr) {
351 // Calling Thread::RemoveDebuggerShadowFrameMapping will also delete the updated_vregs
352 // array so this must come after we processed the frame.
353 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
354 DCHECK(GetThread()->FindDebuggerShadowFrame(frame_id) == nullptr);
355 }
356 if (prev_shadow_frame_ != nullptr) {
357 prev_shadow_frame_->SetLink(new_frame);
358 } else {
359 // Will be popped after the long jump after DeoptimizeStack(),
360 // right before interpreter::EnterInterpreterFromDeoptimize().
361 stacked_shadow_frame_pushed_ = true;
362 GetThread()->PushStackedShadowFrame(
363 new_frame,
364 single_frame_deopt_
365 ? StackedShadowFrameType::kSingleFrameDeoptimizationShadowFrame
366 : StackedShadowFrameType::kDeoptimizationShadowFrame);
367 }
368 prev_shadow_frame_ = new_frame;
369
Andreas Gampe639bdd12015-06-03 11:22:45 -0700370 if (single_frame_deopt_ && !IsInInlinedFrame()) {
371 // Single-frame deopt ends at the first non-inlined frame and needs to store that method.
372 exception_handler_->SetHandlerQuickArg0(reinterpret_cast<uintptr_t>(method));
373 single_frame_done_ = true;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100374 single_frame_deopt_method_ = method;
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000375 single_frame_deopt_quick_method_header_ = GetCurrentOatQuickMethodHeader();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700376 }
377 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700378 }
379 }
380
381 private:
Nicolas Geoffray33856502015-10-20 15:52:58 +0100382 void HandleOptimizingDeoptimization(ArtMethod* m,
383 ShadowFrame* new_frame,
384 const bool* updated_vregs)
385 SHARED_REQUIRES(Locks::mutator_lock_) {
386 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
387 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
388 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
389 StackMapEncoding encoding = code_info.ExtractEncoding();
390 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
391 const size_t number_of_vregs = m->GetCodeItem()->registers_size_;
Nicolas Geoffray33856502015-10-20 15:52:58 +0100392 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
393 uint32_t register_mask = stack_map.GetRegisterMask(encoding);
David Brazdilefc3f022015-10-28 12:19:06 -0500394 DexRegisterMap vreg_map = IsInInlinedFrame()
395 ? code_info.GetDexRegisterMapAtDepth(GetCurrentInliningDepth() - 1,
396 code_info.GetInlineInfoOf(stack_map, encoding),
397 encoding,
398 number_of_vregs)
399 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100400
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000401 if (!vreg_map.IsValid()) {
402 return;
403 }
404
Nicolas Geoffray33856502015-10-20 15:52:58 +0100405 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
406 if (updated_vregs != nullptr && updated_vregs[vreg]) {
407 // Keep the value set by debugger.
408 continue;
409 }
410
411 DexRegisterLocation::Kind location =
412 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
413 static constexpr uint32_t kDeadValue = 0xEBADDE09;
414 uint32_t value = kDeadValue;
415 bool is_reference = false;
416
417 switch (location) {
418 case DexRegisterLocation::Kind::kInStack: {
419 const int32_t offset = vreg_map.GetStackOffsetInBytes(vreg,
420 number_of_vregs,
421 code_info,
422 encoding);
423 const uint8_t* addr = reinterpret_cast<const uint8_t*>(GetCurrentQuickFrame()) + offset;
424 value = *reinterpret_cast<const uint32_t*>(addr);
425 uint32_t bit = (offset >> 2);
426 if (stack_mask.size_in_bits() > bit && stack_mask.LoadBit(bit)) {
427 is_reference = true;
428 }
429 break;
430 }
431 case DexRegisterLocation::Kind::kInRegister:
432 case DexRegisterLocation::Kind::kInRegisterHigh:
433 case DexRegisterLocation::Kind::kInFpuRegister:
434 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
435 uint32_t reg = vreg_map.GetMachineRegister(vreg, number_of_vregs, code_info, encoding);
436 bool result = GetRegisterIfAccessible(reg, ToVRegKind(location), &value);
437 CHECK(result);
438 if (location == DexRegisterLocation::Kind::kInRegister) {
439 if (((1u << reg) & register_mask) != 0) {
440 is_reference = true;
441 }
442 }
443 break;
444 }
445 case DexRegisterLocation::Kind::kConstant: {
446 value = vreg_map.GetConstant(vreg, number_of_vregs, code_info, encoding);
447 if (value == 0) {
448 // Make it a reference for extra safety.
449 is_reference = true;
450 }
451 break;
452 }
453 case DexRegisterLocation::Kind::kNone: {
454 break;
455 }
456 default: {
457 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000458 << "Unexpected location kind "
459 << vreg_map.GetLocationInternalKind(vreg,
460 number_of_vregs,
461 code_info,
462 encoding);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100463 UNREACHABLE();
464 }
465 }
466 if (is_reference) {
467 new_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(value));
468 } else {
469 new_frame->SetVReg(vreg, value);
470 }
471 }
472 }
473
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200474 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
475 return static_cast<VRegKind>(kinds.at(reg * 2));
476 }
477
Nicolas Geoffray33856502015-10-20 15:52:58 +0100478 void HandleQuickDeoptimization(ArtMethod* m,
479 ShadowFrame* new_frame,
480 const bool* updated_vregs)
481 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700482 const DexFile::CodeItem* code_item = m->GetCodeItem();
Sebastien Hertz520633b2015-09-08 17:03:36 +0200483 CHECK(code_item != nullptr) << "No code item for " << PrettyMethod(m);
Ian Rogers5cf98192014-05-29 21:31:50 -0700484 uint16_t num_regs = code_item->registers_size_;
485 uint32_t dex_pc = GetDexPc();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100486 StackHandleScope<2> hs(GetThread()); // Dex cache and class loader.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700487 mirror::Class* declaring_class = m->GetDeclaringClass();
488 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
489 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
Sebastien Hertz26f72862015-09-15 09:52:07 +0200490 verifier::MethodVerifier verifier(GetThread(), h_dex_cache->GetDexFile(), h_dex_cache,
491 h_class_loader, &m->GetClassDef(), code_item,
492 m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, true,
493 true, true);
Andreas Gampe2e04bb22015-02-10 15:37:27 -0800494 bool verifier_success = verifier.Verify();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700495 CHECK(verifier_success) << PrettyMethod(m);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700496 {
Sebastien Hertz26f72862015-09-15 09:52:07 +0200497 ScopedStackedShadowFramePusher pusher(GetThread(), new_frame,
Sebastien Hertzf7958692015-06-09 14:09:14 +0200498 StackedShadowFrameType::kShadowFrameUnderConstruction);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700499 const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc));
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000500
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700501 // Markers for dead values, used when the verifier knows a Dex register is undefined,
502 // or when the compiler knows the register has not been initialized, or is not used
503 // anymore in the method.
504 static constexpr uint32_t kDeadValue = 0xEBADDE09;
505 static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09;
506 for (uint16_t reg = 0; reg < num_regs; ++reg) {
Mingyao Yang99170c62015-07-06 11:10:37 -0700507 if (updated_vregs != nullptr && updated_vregs[reg]) {
508 // Keep the value set by debugger.
509 continue;
510 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700511 VRegKind kind = GetVRegKind(reg, kinds);
512 switch (kind) {
513 case kUndefined:
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000514 new_frame->SetVReg(reg, kDeadValue);
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700515 break;
516 case kConstant:
517 new_frame->SetVReg(reg, kinds.at((reg * 2) + 1));
518 break;
519 case kReferenceVReg: {
520 uint32_t value = 0;
521 // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier.
522 // We don't want to copy a stale reference into the shadow frame as a reference.
523 // b/20736048
524 if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) {
525 new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value));
526 } else {
527 new_frame->SetVReg(reg, kDeadValue);
528 }
529 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000530 }
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700531 case kLongLoVReg:
532 if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) {
533 // Treat it as a "long" register pair.
534 uint64_t value = 0;
535 if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) {
536 new_frame->SetVRegLong(reg, value);
537 } else {
538 new_frame->SetVRegLong(reg, kLongDeadValue);
539 }
540 } else {
541 uint32_t value = 0;
542 if (GetVReg(m, reg, kind, &value)) {
543 new_frame->SetVReg(reg, value);
544 } else {
545 new_frame->SetVReg(reg, kDeadValue);
546 }
547 }
548 break;
549 case kLongHiVReg:
550 if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) {
551 // Nothing to do: we treated it as a "long" register pair.
552 } else {
553 uint32_t value = 0;
554 if (GetVReg(m, reg, kind, &value)) {
555 new_frame->SetVReg(reg, value);
556 } else {
557 new_frame->SetVReg(reg, kDeadValue);
558 }
559 }
560 break;
561 case kDoubleLoVReg:
562 if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) {
563 uint64_t value = 0;
564 if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) {
565 // Treat it as a "double" register pair.
566 new_frame->SetVRegLong(reg, value);
567 } else {
568 new_frame->SetVRegLong(reg, kLongDeadValue);
569 }
570 } else {
571 uint32_t value = 0;
572 if (GetVReg(m, reg, kind, &value)) {
573 new_frame->SetVReg(reg, value);
574 } else {
575 new_frame->SetVReg(reg, kDeadValue);
576 }
577 }
578 break;
579 case kDoubleHiVReg:
580 if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) {
581 // Nothing to do: we treated it as a "double" register pair.
582 } else {
583 uint32_t value = 0;
584 if (GetVReg(m, reg, kind, &value)) {
585 new_frame->SetVReg(reg, value);
586 } else {
587 new_frame->SetVReg(reg, kDeadValue);
588 }
589 }
590 break;
591 default:
592 uint32_t value = 0;
593 if (GetVReg(m, reg, kind, &value)) {
594 new_frame->SetVReg(reg, value);
595 } else {
596 new_frame->SetVReg(reg, kDeadValue);
597 }
598 break;
Nicolas Geoffray15b9d522015-03-12 15:05:13 +0000599 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700600 }
601 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700602 }
603
Ian Rogers5cf98192014-05-29 21:31:50 -0700604 QuickExceptionHandler* const exception_handler_;
605 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700606 bool stacked_shadow_frame_pushed_;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700607 const bool single_frame_deopt_;
608 bool single_frame_done_;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100609 ArtMethod* single_frame_deopt_method_;
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000610 const OatQuickMethodHeader* single_frame_deopt_quick_method_header_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700611
612 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
613};
614
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200615void QuickExceptionHandler::DeoptimizeStack() {
616 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700617 if (kDebugExceptionDelivery) {
618 self_->DumpStack(LOG(INFO) << "Deoptimizing: ");
619 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200620
Andreas Gampe639bdd12015-06-03 11:22:45 -0700621 DeoptimizeStackVisitor visitor(self_, context_, this, false);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200622 visitor.WalkStack(true);
623
624 // Restore deoptimization exception
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000625 self_->SetException(Thread::GetDeoptimizationException());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100626}
627
Andreas Gampe639bdd12015-06-03 11:22:45 -0700628void QuickExceptionHandler::DeoptimizeSingleFrame() {
629 DCHECK(is_deoptimization_);
630
631 if (VLOG_IS_ON(deopt) || kDebugExceptionDelivery) {
632 LOG(INFO) << "Single-frame deopting:";
633 DumpFramesWithType(self_, true);
634 }
635
636 DeoptimizeStackVisitor visitor(self_, context_, this, true);
637 visitor.WalkStack(true);
638
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000639 // Compiled code made an explicit deoptimization.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100640 ArtMethod* deopt_method = visitor.GetSingleFrameDeoptMethod();
641 DCHECK(deopt_method != nullptr);
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000642 if (Runtime::Current()->UseJit()) {
643 Runtime::Current()->GetJit()->GetCodeCache()->InvalidateCompiledCodeFor(
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000644 deopt_method, visitor.GetSingleFrameDeoptQuickMethodHeader());
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000645 } else {
646 // Transfer the code to interpreter.
647 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
648 deopt_method, GetQuickToInterpreterBridge());
649 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100650
Andreas Gampe639bdd12015-06-03 11:22:45 -0700651 // PC needs to be of the quick-to-interpreter bridge.
652 int32_t offset;
653 #ifdef __LP64__
654 offset = GetThreadOffset<8>(kQuickQuickToInterpreterBridge).Int32Value();
655 #else
656 offset = GetThreadOffset<4>(kQuickQuickToInterpreterBridge).Int32Value();
657 #endif
658 handler_quick_frame_pc_ = *reinterpret_cast<uintptr_t*>(
659 reinterpret_cast<uint8_t*>(self_) + offset);
660}
661
662void QuickExceptionHandler::DeoptimizeSingleFrameArchDependentFixup() {
663 // Architecture-dependent work. This is to get the LR right for x86 and x86-64.
664
665 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
666 // On x86, the return address is on the stack, so just reuse it. Otherwise we would have to
667 // change how longjump works.
668 handler_quick_frame_ = reinterpret_cast<ArtMethod**>(
669 reinterpret_cast<uintptr_t>(handler_quick_frame_) - sizeof(void*));
670 }
671}
672
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100673// Unwinds all instrumentation stack frame prior to catch handler or upcall.
674class InstrumentationStackVisitor : public StackVisitor {
675 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700676 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Mathieu Chartier90443472015-07-16 20:32:27 -0700677 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100678 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700679 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100680 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700681 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100682 }
683
Mathieu Chartier90443472015-07-16 20:32:27 -0700684 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700685 size_t current_frame_depth = GetFrameDepth();
686 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100687 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700688 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100689 if (!IsInInlinedFrame()) {
690 // We do not count inlined frames, because we do not instrument them. The reason we
691 // include them in the stack walking is the check against `frame_depth_`, which is
692 // given to us by a visitor that visits inlined frames.
693 ++instrumentation_frames_to_pop_;
694 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100695 }
696 return true;
697 } else {
698 // We reached the frame of the catch handler or the upcall.
699 return false;
700 }
701 }
702
703 size_t GetInstrumentationFramesToPop() const {
704 return instrumentation_frames_to_pop_;
705 }
706
707 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700708 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100709 size_t instrumentation_frames_to_pop_;
710
711 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
712};
713
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200714void QuickExceptionHandler::UpdateInstrumentationStack() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100715 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700716 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100717 visitor.WalkStack(true);
718
719 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
720 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
721 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
722 instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
723 }
724 }
725}
726
Andreas Gampe639bdd12015-06-03 11:22:45 -0700727void QuickExceptionHandler::DoLongJump(bool smash_caller_saves) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100728 // Place context back on thread so it will be available when we continue.
729 self_->ReleaseLongJumpContext(context_);
730 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
731 CHECK_NE(handler_quick_frame_pc_, 0u);
732 context_->SetPC(handler_quick_frame_pc_);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700733 context_->SetArg0(handler_quick_arg0_);
734 if (smash_caller_saves) {
735 context_->SmashCallerSaves();
736 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100737 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800738 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100739}
740
Andreas Gampe639bdd12015-06-03 11:22:45 -0700741// Prints out methods with their type of frame.
742class DumpFramesWithTypeStackVisitor FINAL : public StackVisitor {
743 public:
744 DumpFramesWithTypeStackVisitor(Thread* self, bool show_details = false)
745 SHARED_REQUIRES(Locks::mutator_lock_)
746 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
747 show_details_(show_details) {}
748
749 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
750 ArtMethod* method = GetMethod();
751 if (show_details_) {
752 LOG(INFO) << "|> pc = " << std::hex << GetCurrentQuickFramePc();
753 LOG(INFO) << "|> addr = " << std::hex << reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
754 if (GetCurrentQuickFrame() != nullptr && method != nullptr) {
755 LOG(INFO) << "|> ret = " << std::hex << GetReturnPc();
756 }
757 }
758 if (method == nullptr) {
759 // Transition, do go on, we want to unwind over bridges, all the way.
760 if (show_details_) {
761 LOG(INFO) << "N <transition>";
762 }
763 return true;
764 } else if (method->IsRuntimeMethod()) {
765 if (show_details_) {
766 LOG(INFO) << "R " << PrettyMethod(method, true);
767 }
768 return true;
769 } else {
770 bool is_shadow = GetCurrentShadowFrame() != nullptr;
771 LOG(INFO) << (is_shadow ? "S" : "Q")
772 << ((!is_shadow && IsInInlinedFrame()) ? "i" : " ")
773 << " "
774 << PrettyMethod(method, true);
775 return true; // Go on.
776 }
777 }
778
779 private:
780 bool show_details_;
781
782 DISALLOW_COPY_AND_ASSIGN(DumpFramesWithTypeStackVisitor);
783};
784
785void QuickExceptionHandler::DumpFramesWithType(Thread* self, bool details) {
786 DumpFramesWithTypeStackVisitor visitor(self, details);
787 visitor.WalkStack(true);
788}
789
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100790} // namespace art