blob: a81458fded0d5f3919de5b6ae8f3db4f5dcf56be [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"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070022#include "dex_instruction.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020023#include "entrypoints/entrypoint_utils.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070024#include "entrypoints/quick/quick_entrypoints_enum.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070025#include "entrypoints/runtime_asm_entrypoints.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070026#include "handle_scope-inl.h"
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +000027#include "jit/jit.h"
28#include "jit/jit_code_cache.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070029#include "mirror/class-inl.h"
30#include "mirror/class_loader.h"
31#include "mirror/throwable.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010032#include "oat_quick_method_header.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010033#include "stack.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010034#include "stack_map.h"
Ian Rogers5cf98192014-05-29 21:31:50 -070035#include "verifier/method_verifier.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010036
37namespace art {
38
Ian Rogers5cf98192014-05-29 21:31:50 -070039static constexpr bool kDebugExceptionDelivery = false;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070040static constexpr size_t kInvalidFrameDepth = 0xffffffff;
Ian Rogers5cf98192014-05-29 21:31:50 -070041
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020042QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010043 : self_(self),
44 context_(self->GetLongJumpContext()),
45 is_deoptimization_(is_deoptimization),
46 method_tracing_active_(is_deoptimization ||
47 Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()),
48 handler_quick_frame_(nullptr),
49 handler_quick_frame_pc_(0),
50 handler_method_header_(nullptr),
51 handler_quick_arg0_(0),
52 handler_method_(nullptr),
53 handler_dex_pc_(0),
54 clear_exception_(false),
Mingyao Yangf711f2c2016-05-23 12:29:39 -070055 handler_frame_depth_(kInvalidFrameDepth),
56 full_fragment_done_(false) {}
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010057
Sebastien Hertz520633b2015-09-08 17:03:36 +020058// Finds catch handler.
Ian Rogers5cf98192014-05-29 21:31:50 -070059class CatchBlockStackVisitor FINAL : public StackVisitor {
60 public:
61 CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception,
62 QuickExceptionHandler* exception_handler)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010064 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010065 exception_(exception),
Ian Rogers5cf98192014-05-29 21:31:50 -070066 exception_handler_(exception_handler) {
67 }
68
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 ArtMethod* method = GetMethod();
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -070071 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Ian Rogers5cf98192014-05-29 21:31:50 -070072 if (method == nullptr) {
73 // This is the upcall, we remember the frame and last pc so that we may long jump to them.
74 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
75 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010076 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -070077 uint32_t next_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -070078 ArtMethod* next_art_method;
Ian Rogers5cf98192014-05-29 21:31:50 -070079 bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc);
80 // Report the method that did the down call as the handler.
81 exception_handler_->SetHandlerDexPc(next_dex_pc);
82 exception_handler_->SetHandlerMethod(next_art_method);
83 if (!has_next) {
84 // No next method? Check exception handler is set up for the unhandled exception handler
85 // case.
86 DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc());
87 DCHECK(nullptr == exception_handler_->GetHandlerMethod());
88 }
89 return false; // End stack walk.
90 }
91 if (method->IsRuntimeMethod()) {
92 // Ignore callee save method.
93 DCHECK(method->IsCalleeSaveMethod());
94 return true;
95 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070096 return HandleTryItems(method);
Ian Rogers5cf98192014-05-29 21:31:50 -070097 }
98
99 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 bool HandleTryItems(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700102 uint32_t dex_pc = DexFile::kDexNoIndex;
103 if (!method->IsNative()) {
104 dex_pc = GetDexPc();
105 }
106 if (dex_pc != DexFile::kDexNoIndex) {
107 bool clear_exception = false;
Sebastien Hertz26f72862015-09-15 09:52:07 +0200108 StackHandleScope<1> hs(GetThread());
Ian Rogers5cf98192014-05-29 21:31:50 -0700109 Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700110 uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception);
Ian Rogers5cf98192014-05-29 21:31:50 -0700111 exception_handler_->SetClearException(clear_exception);
112 if (found_dex_pc != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700113 exception_handler_->SetHandlerMethod(method);
Ian Rogers5cf98192014-05-29 21:31:50 -0700114 exception_handler_->SetHandlerDexPc(found_dex_pc);
David Brazdil72f7b882015-09-15 17:00:52 +0100115 exception_handler_->SetHandlerQuickFramePc(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100116 GetCurrentOatQuickMethodHeader()->ToNativeQuickPc(
117 method, found_dex_pc, /* is_catch_handler */ true));
Ian Rogers5cf98192014-05-29 21:31:50 -0700118 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100119 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
Ian Rogers5cf98192014-05-29 21:31:50 -0700120 return false; // End stack walk.
Mingyao Yang99170c62015-07-06 11:10:37 -0700121 } else if (UNLIKELY(GetThread()->HasDebuggerShadowFrames())) {
122 // We are going to unwind this frame. Did we prepare a shadow frame for debugging?
123 size_t frame_id = GetFrameId();
124 ShadowFrame* frame = GetThread()->FindDebuggerShadowFrame(frame_id);
125 if (frame != nullptr) {
126 // We will not execute this shadow frame so we can safely deallocate it.
127 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
128 ShadowFrame::DeleteDeoptimizedFrame(frame);
129 }
Ian Rogers5cf98192014-05-29 21:31:50 -0700130 }
131 }
132 return true; // Continue stack walk.
133 }
134
Ian Rogers5cf98192014-05-29 21:31:50 -0700135 // The exception we're looking for the catch block of.
136 Handle<mirror::Throwable>* exception_;
137 // The quick exception handler we're visiting for.
138 QuickExceptionHandler* const exception_handler_;
139
140 DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor);
141};
142
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000143void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200144 DCHECK(!is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700145 if (kDebugExceptionDelivery) {
146 mirror::String* msg = exception->GetDetailMessage();
147 std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : "");
David Sehr709b0702016-10-13 09:12:37 -0700148 self_->DumpStack(LOG_STREAM(INFO) << "Delivering exception: " << exception->PrettyTypeOf()
Ian Rogers5cf98192014-05-29 21:31:50 -0700149 << ": " << str_msg << "\n");
150 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700151 StackHandleScope<1> hs(self_);
152 Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception));
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200153
Sebastien Hertz520633b2015-09-08 17:03:36 +0200154 // Walk the stack to find catch handler.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700155 CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100156 visitor.WalkStack(true);
157
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200158 if (kDebugExceptionDelivery) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 if (*handler_quick_frame_ == nullptr) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100160 LOG(INFO) << "Handler is upcall";
Ian Rogers5cf98192014-05-29 21:31:50 -0700161 }
162 if (handler_method_ != nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700163 const DexFile* dex_file = handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile();
164 int line_number = annotations::GetLineNumFromPC(dex_file, handler_method_, handler_dex_pc_);
David Sehr709b0702016-10-13 09:12:37 -0700165 LOG(INFO) << "Handler: " << handler_method_->PrettyMethod() << " (line: "
166 << line_number << ")";
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100167 }
168 }
169 if (clear_exception_) {
170 // Exception was cleared as part of delivery.
171 DCHECK(!self_->IsExceptionPending());
172 } else {
173 // Put exception back in root set with clear throw location.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000174 self_->SetException(exception_ref.Get());
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100175 }
David Brazdil77a48ae2015-09-15 12:34:04 +0000176 // If the handler is in optimized code, we need to set the catch environment.
177 if (*handler_quick_frame_ != nullptr &&
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100178 handler_method_header_ != nullptr &&
179 handler_method_header_->IsOptimized()) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000180 SetCatchEnvironmentForOptimizedHandler(&visitor);
181 }
182}
183
184static VRegKind ToVRegKind(DexRegisterLocation::Kind kind) {
185 // Slightly hacky since we cannot map DexRegisterLocationKind and VRegKind
186 // one to one. However, StackVisitor::GetVRegFromOptimizedCode only needs to
187 // distinguish between core/FPU registers and low/high bits on 64-bit.
188 switch (kind) {
189 case DexRegisterLocation::Kind::kConstant:
190 case DexRegisterLocation::Kind::kInStack:
191 // VRegKind is ignored.
192 return VRegKind::kUndefined;
193
194 case DexRegisterLocation::Kind::kInRegister:
195 // Selects core register. For 64-bit registers, selects low 32 bits.
196 return VRegKind::kLongLoVReg;
197
198 case DexRegisterLocation::Kind::kInRegisterHigh:
199 // Selects core register. For 64-bit registers, selects high 32 bits.
200 return VRegKind::kLongHiVReg;
201
202 case DexRegisterLocation::Kind::kInFpuRegister:
203 // Selects FPU register. For 64-bit registers, selects low 32 bits.
204 return VRegKind::kDoubleLoVReg;
205
206 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
207 // Selects FPU register. For 64-bit registers, selects high 32 bits.
208 return VRegKind::kDoubleHiVReg;
209
210 default:
David Srbecky7dc11782016-02-25 13:23:56 +0000211 LOG(FATAL) << "Unexpected vreg location " << kind;
David Brazdil77a48ae2015-09-15 12:34:04 +0000212 UNREACHABLE();
213 }
214}
215
216void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) {
217 DCHECK(!is_deoptimization_);
218 DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions";
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100219 DCHECK(handler_method_ != nullptr && handler_method_header_->IsOptimized());
David Brazdil77a48ae2015-09-15 12:34:04 +0000220
221 if (kDebugExceptionDelivery) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700222 self_->DumpStack(LOG_STREAM(INFO) << "Setting catch phis: ");
David Brazdil77a48ae2015-09-15 12:34:04 +0000223 }
224
225 const size_t number_of_vregs = handler_method_->GetCodeItem()->registers_size_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100226 CodeInfo code_info = handler_method_header_->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000227 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdil77a48ae2015-09-15 12:34:04 +0000228
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000229 // Find stack map of the catch block.
230 StackMap catch_stack_map = code_info.GetCatchStackMapForDexPc(GetHandlerDexPc(), encoding);
231 DCHECK(catch_stack_map.IsValid());
232 DexRegisterMap catch_vreg_map =
233 code_info.GetDexRegisterMapOf(catch_stack_map, encoding, number_of_vregs);
234 if (!catch_vreg_map.IsValid()) {
235 return;
236 }
237
David Brazdil77a48ae2015-09-15 12:34:04 +0000238 // Find stack map of the throwing instruction.
239 StackMap throw_stack_map =
240 code_info.GetStackMapForNativePcOffset(stack_visitor->GetNativePcOffset(), encoding);
241 DCHECK(throw_stack_map.IsValid());
242 DexRegisterMap throw_vreg_map =
243 code_info.GetDexRegisterMapOf(throw_stack_map, encoding, number_of_vregs);
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000244 DCHECK(throw_vreg_map.IsValid());
David Brazdil77a48ae2015-09-15 12:34:04 +0000245
246 // Copy values between them.
247 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
248 DexRegisterLocation::Kind catch_location =
249 catch_vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
250 if (catch_location == DexRegisterLocation::Kind::kNone) {
251 continue;
252 }
253 DCHECK(catch_location == DexRegisterLocation::Kind::kInStack);
254
255 // Get vreg value from its current location.
256 uint32_t vreg_value;
257 VRegKind vreg_kind = ToVRegKind(throw_vreg_map.GetLocationKind(vreg,
258 number_of_vregs,
259 code_info,
260 encoding));
261 bool get_vreg_success = stack_visitor->GetVReg(stack_visitor->GetMethod(),
262 vreg,
263 vreg_kind,
264 &vreg_value);
265 CHECK(get_vreg_success) << "VReg " << vreg << " was optimized out ("
David Sehr709b0702016-10-13 09:12:37 -0700266 << "method=" << ArtMethod::PrettyMethod(stack_visitor->GetMethod())
267 << ", dex_pc=" << stack_visitor->GetDexPc() << ", "
David Brazdil77a48ae2015-09-15 12:34:04 +0000268 << "native_pc_offset=" << stack_visitor->GetNativePcOffset() << ")";
269
270 // Copy value to the catch phi's stack slot.
271 int32_t slot_offset = catch_vreg_map.GetStackOffsetInBytes(vreg,
272 number_of_vregs,
273 code_info,
274 encoding);
275 ArtMethod** frame_top = stack_visitor->GetCurrentQuickFrame();
276 uint8_t* slot_address = reinterpret_cast<uint8_t*>(frame_top) + slot_offset;
277 uint32_t* slot_ptr = reinterpret_cast<uint32_t*>(slot_address);
278 *slot_ptr = vreg_value;
279 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200280}
281
Ian Rogers5cf98192014-05-29 21:31:50 -0700282// Prepares deoptimization.
283class DeoptimizeStackVisitor FINAL : public StackVisitor {
284 public:
Andreas Gampe639bdd12015-06-03 11:22:45 -0700285 DeoptimizeStackVisitor(Thread* self,
286 Context* context,
287 QuickExceptionHandler* exception_handler,
288 bool single_frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700289 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100290 : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100291 exception_handler_(exception_handler),
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700292 prev_shadow_frame_(nullptr),
Andreas Gampe639bdd12015-06-03 11:22:45 -0700293 stacked_shadow_frame_pushed_(false),
294 single_frame_deopt_(single_frame),
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100295 single_frame_done_(false),
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000296 single_frame_deopt_method_(nullptr),
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700297 single_frame_deopt_quick_method_header_(nullptr),
298 callee_method_(nullptr) {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100299 }
300
301 ArtMethod* GetSingleFrameDeoptMethod() const {
302 return single_frame_deopt_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700303 }
304
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000305 const OatQuickMethodHeader* GetSingleFrameDeoptQuickMethodHeader() const {
306 return single_frame_deopt_quick_method_header_;
307 }
308
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700309 void FinishStackWalk() REQUIRES_SHARED(Locks::mutator_lock_) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700310 // This is the upcall, or the next full frame in single-frame deopt, or the
311 // code isn't deoptimizeable. We remember the frame and last pc so that we
312 // may long jump to them.
313 exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc());
314 exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame());
315 exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader());
316 if (!stacked_shadow_frame_pushed_) {
317 // In case there is no deoptimized shadow frame for this upcall, we still
318 // need to push a nullptr to the stack since there is always a matching pop after
319 // the long jump.
320 GetThread()->PushStackedShadowFrame(nullptr,
321 StackedShadowFrameType::kDeoptimizationShadowFrame);
322 stacked_shadow_frame_pushed_ = true;
323 }
324 if (GetMethod() == nullptr) {
325 exception_handler_->SetFullFragmentDone(true);
326 } else {
David Sehr709b0702016-10-13 09:12:37 -0700327 CHECK(callee_method_ != nullptr) << GetMethod()->PrettyMethod(false);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700328 exception_handler_->SetHandlerQuickArg0(reinterpret_cast<uintptr_t>(callee_method_));
329 }
330 }
331
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700332 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700333 exception_handler_->SetHandlerFrameDepth(GetFrameDepth());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700334 ArtMethod* method = GetMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700335 if (method == nullptr || single_frame_done_) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700336 FinishStackWalk();
Ian Rogers5cf98192014-05-29 21:31:50 -0700337 return false; // End stack walk.
338 } else if (method->IsRuntimeMethod()) {
339 // Ignore callee save method.
340 DCHECK(method->IsCalleeSaveMethod());
341 return true;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200342 } else if (method->IsNative()) {
343 // If we return from JNI with a pending exception and want to deoptimize, we need to skip
344 // the native method.
345 // The top method is a runtime method, the native method comes next.
346 CHECK_EQ(GetFrameDepth(), 1U);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700347 callee_method_ = method;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200348 return true;
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700349 } else if (!single_frame_deopt_ &&
350 !Runtime::Current()->IsDeoptimizeable(GetCurrentQuickFramePc())) {
351 // We hit some code that's not deoptimizeable. However, Single-frame deoptimization triggered
352 // from compiled code is always allowed since HDeoptimize always saves the full environment.
353 FinishStackWalk();
354 return false; // End stack walk.
Ian Rogers5cf98192014-05-29 21:31:50 -0700355 } else {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100356 // Check if a shadow frame already exists for debugger's set-local-value purpose.
357 const size_t frame_id = GetFrameId();
358 ShadowFrame* new_frame = GetThread()->FindDebuggerShadowFrame(frame_id);
359 const bool* updated_vregs;
360 const size_t num_regs = method->GetCodeItem()->registers_size_;
361 if (new_frame == nullptr) {
362 new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, method, GetDexPc());
363 updated_vregs = nullptr;
364 } else {
365 updated_vregs = GetThread()->GetUpdatedVRegFlags(frame_id);
366 DCHECK(updated_vregs != nullptr);
367 }
Andreas Gampebf9611f2016-03-25 16:58:00 -0700368 HandleOptimizingDeoptimization(method, new_frame, updated_vregs);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100369 if (updated_vregs != nullptr) {
370 // Calling Thread::RemoveDebuggerShadowFrameMapping will also delete the updated_vregs
371 // array so this must come after we processed the frame.
372 GetThread()->RemoveDebuggerShadowFrameMapping(frame_id);
373 DCHECK(GetThread()->FindDebuggerShadowFrame(frame_id) == nullptr);
374 }
375 if (prev_shadow_frame_ != nullptr) {
376 prev_shadow_frame_->SetLink(new_frame);
377 } else {
378 // Will be popped after the long jump after DeoptimizeStack(),
379 // right before interpreter::EnterInterpreterFromDeoptimize().
380 stacked_shadow_frame_pushed_ = true;
381 GetThread()->PushStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700382 new_frame, StackedShadowFrameType::kDeoptimizationShadowFrame);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100383 }
384 prev_shadow_frame_ = new_frame;
385
Andreas Gampe639bdd12015-06-03 11:22:45 -0700386 if (single_frame_deopt_ && !IsInInlinedFrame()) {
387 // Single-frame deopt ends at the first non-inlined frame and needs to store that method.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700388 single_frame_done_ = true;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100389 single_frame_deopt_method_ = method;
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000390 single_frame_deopt_quick_method_header_ = GetCurrentOatQuickMethodHeader();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700391 }
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700392 callee_method_ = method;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700393 return true;
Ian Rogers5cf98192014-05-29 21:31:50 -0700394 }
395 }
396
397 private:
Nicolas Geoffray33856502015-10-20 15:52:58 +0100398 void HandleOptimizingDeoptimization(ArtMethod* m,
399 ShadowFrame* new_frame,
400 const bool* updated_vregs)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700401 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100402 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader();
403 CodeInfo code_info = method_header->GetOptimizedCodeInfo();
404 uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc());
David Srbecky09ed0982016-02-12 21:58:43 +0000405 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Nicolas Geoffray33856502015-10-20 15:52:58 +0100406 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
407 const size_t number_of_vregs = m->GetCodeItem()->registers_size_;
David Srbecky09ed0982016-02-12 21:58:43 +0000408 uint32_t register_mask = stack_map.GetRegisterMask(encoding.stack_map_encoding);
David Brazdilefc3f022015-10-28 12:19:06 -0500409 DexRegisterMap vreg_map = IsInInlinedFrame()
410 ? code_info.GetDexRegisterMapAtDepth(GetCurrentInliningDepth() - 1,
411 code_info.GetInlineInfoOf(stack_map, encoding),
412 encoding,
413 number_of_vregs)
414 : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100415
Nicolas Geoffray012fc4e2016-01-08 15:58:19 +0000416 if (!vreg_map.IsValid()) {
417 return;
418 }
419
Nicolas Geoffray33856502015-10-20 15:52:58 +0100420 for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) {
421 if (updated_vregs != nullptr && updated_vregs[vreg]) {
422 // Keep the value set by debugger.
423 continue;
424 }
425
426 DexRegisterLocation::Kind location =
427 vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding);
428 static constexpr uint32_t kDeadValue = 0xEBADDE09;
429 uint32_t value = kDeadValue;
430 bool is_reference = false;
431
432 switch (location) {
433 case DexRegisterLocation::Kind::kInStack: {
434 const int32_t offset = vreg_map.GetStackOffsetInBytes(vreg,
435 number_of_vregs,
436 code_info,
437 encoding);
438 const uint8_t* addr = reinterpret_cast<const uint8_t*>(GetCurrentQuickFrame()) + offset;
439 value = *reinterpret_cast<const uint32_t*>(addr);
440 uint32_t bit = (offset >> 2);
David Srbecky09ed0982016-02-12 21:58:43 +0000441 if (stack_map.GetNumberOfStackMaskBits(encoding.stack_map_encoding) > bit &&
442 stack_map.GetStackMaskBit(encoding.stack_map_encoding, bit)) {
Nicolas Geoffray33856502015-10-20 15:52:58 +0100443 is_reference = true;
444 }
445 break;
446 }
447 case DexRegisterLocation::Kind::kInRegister:
448 case DexRegisterLocation::Kind::kInRegisterHigh:
449 case DexRegisterLocation::Kind::kInFpuRegister:
450 case DexRegisterLocation::Kind::kInFpuRegisterHigh: {
451 uint32_t reg = vreg_map.GetMachineRegister(vreg, number_of_vregs, code_info, encoding);
452 bool result = GetRegisterIfAccessible(reg, ToVRegKind(location), &value);
453 CHECK(result);
454 if (location == DexRegisterLocation::Kind::kInRegister) {
455 if (((1u << reg) & register_mask) != 0) {
456 is_reference = true;
457 }
458 }
459 break;
460 }
461 case DexRegisterLocation::Kind::kConstant: {
462 value = vreg_map.GetConstant(vreg, number_of_vregs, code_info, encoding);
463 if (value == 0) {
464 // Make it a reference for extra safety.
465 is_reference = true;
466 }
467 break;
468 }
469 case DexRegisterLocation::Kind::kNone: {
470 break;
471 }
472 default: {
473 LOG(FATAL)
David Srbecky7dc11782016-02-25 13:23:56 +0000474 << "Unexpected location kind "
475 << vreg_map.GetLocationInternalKind(vreg,
476 number_of_vregs,
477 code_info,
478 encoding);
Nicolas Geoffray33856502015-10-20 15:52:58 +0100479 UNREACHABLE();
480 }
481 }
482 if (is_reference) {
483 new_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(value));
484 } else {
485 new_frame->SetVReg(vreg, value);
486 }
487 }
488 }
489
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200490 static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) {
491 return static_cast<VRegKind>(kinds.at(reg * 2));
492 }
493
Ian Rogers5cf98192014-05-29 21:31:50 -0700494 QuickExceptionHandler* const exception_handler_;
495 ShadowFrame* prev_shadow_frame_;
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -0700496 bool stacked_shadow_frame_pushed_;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700497 const bool single_frame_deopt_;
498 bool single_frame_done_;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100499 ArtMethod* single_frame_deopt_method_;
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000500 const OatQuickMethodHeader* single_frame_deopt_quick_method_header_;
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700501 ArtMethod* callee_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700502
503 DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor);
504};
505
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700506void QuickExceptionHandler::PrepareForLongJumpToInvokeStubOrInterpreterBridge() {
507 if (full_fragment_done_) {
508 // Restore deoptimization exception. When returning from the invoke stub,
509 // ArtMethod::Invoke() will see the special exception to know deoptimization
510 // is needed.
511 self_->SetException(Thread::GetDeoptimizationException());
512 } else {
513 // PC needs to be of the quick-to-interpreter bridge.
514 int32_t offset;
Andreas Gampe542451c2016-07-26 09:02:02 -0700515 offset = GetThreadOffset<kRuntimePointerSize>(kQuickQuickToInterpreterBridge).Int32Value();
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700516 handler_quick_frame_pc_ = *reinterpret_cast<uintptr_t*>(
517 reinterpret_cast<uint8_t*>(self_) + offset);
518 }
519}
520
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200521void QuickExceptionHandler::DeoptimizeStack() {
522 DCHECK(is_deoptimization_);
Ian Rogers5cf98192014-05-29 21:31:50 -0700523 if (kDebugExceptionDelivery) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700524 self_->DumpStack(LOG_STREAM(INFO) << "Deoptimizing: ");
Ian Rogers5cf98192014-05-29 21:31:50 -0700525 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200526
Andreas Gampe639bdd12015-06-03 11:22:45 -0700527 DeoptimizeStackVisitor visitor(self_, context_, this, false);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200528 visitor.WalkStack(true);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700529 PrepareForLongJumpToInvokeStubOrInterpreterBridge();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100530}
531
Andreas Gampe639bdd12015-06-03 11:22:45 -0700532void QuickExceptionHandler::DeoptimizeSingleFrame() {
533 DCHECK(is_deoptimization_);
534
535 if (VLOG_IS_ON(deopt) || kDebugExceptionDelivery) {
536 LOG(INFO) << "Single-frame deopting:";
537 DumpFramesWithType(self_, true);
538 }
539
540 DeoptimizeStackVisitor visitor(self_, context_, this, true);
541 visitor.WalkStack(true);
542
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000543 // Compiled code made an explicit deoptimization.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100544 ArtMethod* deopt_method = visitor.GetSingleFrameDeoptMethod();
545 DCHECK(deopt_method != nullptr);
Calin Juravleffc87072016-04-20 14:22:09 +0100546 if (Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000547 Runtime::Current()->GetJit()->GetCodeCache()->InvalidateCompiledCodeFor(
Nicolas Geoffrayb52de242016-02-19 12:43:12 +0000548 deopt_method, visitor.GetSingleFrameDeoptQuickMethodHeader());
Nicolas Geoffrayb88d59e2016-02-17 11:31:49 +0000549 } else {
550 // Transfer the code to interpreter.
551 Runtime::Current()->GetInstrumentation()->UpdateMethodsCode(
552 deopt_method, GetQuickToInterpreterBridge());
553 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100554
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700555 PrepareForLongJumpToInvokeStubOrInterpreterBridge();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700556}
557
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700558void QuickExceptionHandler::DeoptimizePartialFragmentFixup(uintptr_t return_pc) {
559 // At this point, the instrumentation stack has been updated. We need to install
560 // the real return pc on stack, in case instrumentation stub is stored there,
561 // so that the interpreter bridge code can return to the right place.
562 if (return_pc != 0) {
563 uintptr_t* pc_addr = reinterpret_cast<uintptr_t*>(handler_quick_frame_);
564 CHECK(pc_addr != nullptr);
565 pc_addr--;
566 *reinterpret_cast<uintptr_t*>(pc_addr) = return_pc;
567 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700568
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700569 // Architecture-dependent work. This is to get the LR right for x86 and x86-64.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700570 if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) {
571 // On x86, the return address is on the stack, so just reuse it. Otherwise we would have to
572 // change how longjump works.
573 handler_quick_frame_ = reinterpret_cast<ArtMethod**>(
574 reinterpret_cast<uintptr_t>(handler_quick_frame_) - sizeof(void*));
575 }
576}
577
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100578// Unwinds all instrumentation stack frame prior to catch handler or upcall.
579class InstrumentationStackVisitor : public StackVisitor {
580 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700581 InstrumentationStackVisitor(Thread* self, size_t frame_depth)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700582 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100583 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Ian Rogerscf7f1912014-10-22 22:06:39 -0700584 frame_depth_(frame_depth),
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100585 instrumentation_frames_to_pop_(0) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700586 CHECK_NE(frame_depth_, kInvalidFrameDepth);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100587 }
588
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700589 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700590 size_t current_frame_depth = GetFrameDepth();
591 if (current_frame_depth < frame_depth_) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100592 CHECK(GetMethod() != nullptr);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700593 if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) {
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +0100594 if (!IsInInlinedFrame()) {
595 // We do not count inlined frames, because we do not instrument them. The reason we
596 // include them in the stack walking is the check against `frame_depth_`, which is
597 // given to us by a visitor that visits inlined frames.
598 ++instrumentation_frames_to_pop_;
599 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100600 }
601 return true;
602 } else {
603 // We reached the frame of the catch handler or the upcall.
604 return false;
605 }
606 }
607
608 size_t GetInstrumentationFramesToPop() const {
609 return instrumentation_frames_to_pop_;
610 }
611
612 private:
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700613 const size_t frame_depth_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100614 size_t instrumentation_frames_to_pop_;
615
616 DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor);
617};
618
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700619uintptr_t QuickExceptionHandler::UpdateInstrumentationStack() {
620 uintptr_t return_pc = 0;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100621 if (method_tracing_active_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700622 InstrumentationStackVisitor visitor(self_, handler_frame_depth_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100623 visitor.WalkStack(true);
624
625 size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop();
626 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
627 for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700628 return_pc = instrumentation->PopMethodForUnwind(self_, is_deoptimization_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100629 }
630 }
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700631 return return_pc;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100632}
633
Andreas Gampe639bdd12015-06-03 11:22:45 -0700634void QuickExceptionHandler::DoLongJump(bool smash_caller_saves) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100635 // Place context back on thread so it will be available when we continue.
636 self_->ReleaseLongJumpContext(context_);
637 context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_));
638 CHECK_NE(handler_quick_frame_pc_, 0u);
639 context_->SetPC(handler_quick_frame_pc_);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700640 context_->SetArg0(handler_quick_arg0_);
641 if (smash_caller_saves) {
642 context_->SmashCallerSaves();
643 }
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100644 context_->DoLongJump();
Andreas Gampe794ad762015-02-23 08:12:24 -0800645 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100646}
647
Andreas Gampe639bdd12015-06-03 11:22:45 -0700648// Prints out methods with their type of frame.
649class DumpFramesWithTypeStackVisitor FINAL : public StackVisitor {
650 public:
Chih-Hung Hsieh471118e2016-04-29 14:27:41 -0700651 explicit DumpFramesWithTypeStackVisitor(Thread* self, bool show_details = false)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700652 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700653 : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
654 show_details_(show_details) {}
655
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700656 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700657 ArtMethod* method = GetMethod();
658 if (show_details_) {
659 LOG(INFO) << "|> pc = " << std::hex << GetCurrentQuickFramePc();
660 LOG(INFO) << "|> addr = " << std::hex << reinterpret_cast<uintptr_t>(GetCurrentQuickFrame());
661 if (GetCurrentQuickFrame() != nullptr && method != nullptr) {
662 LOG(INFO) << "|> ret = " << std::hex << GetReturnPc();
663 }
664 }
665 if (method == nullptr) {
666 // Transition, do go on, we want to unwind over bridges, all the way.
667 if (show_details_) {
668 LOG(INFO) << "N <transition>";
669 }
670 return true;
671 } else if (method->IsRuntimeMethod()) {
672 if (show_details_) {
David Sehr709b0702016-10-13 09:12:37 -0700673 LOG(INFO) << "R " << method->PrettyMethod(true);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700674 }
675 return true;
676 } else {
677 bool is_shadow = GetCurrentShadowFrame() != nullptr;
678 LOG(INFO) << (is_shadow ? "S" : "Q")
679 << ((!is_shadow && IsInInlinedFrame()) ? "i" : " ")
680 << " "
David Sehr709b0702016-10-13 09:12:37 -0700681 << method->PrettyMethod(true);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700682 return true; // Go on.
683 }
684 }
685
686 private:
687 bool show_details_;
688
689 DISALLOW_COPY_AND_ASSIGN(DumpFramesWithTypeStackVisitor);
690};
691
692void QuickExceptionHandler::DumpFramesWithType(Thread* self, bool details) {
693 DumpFramesWithTypeStackVisitor visitor(self, details);
694 visitor.WalkStack(true);
695}
696
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100697} // namespace art