Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 17 | #include "quick_exception_handler.h" |
| 18 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 19 | #include "arch/context.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame^] | 22 | #include "dex_file_types.h" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 23 | #include "dex_instruction.h" |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 24 | #include "entrypoints/entrypoint_utils.h" |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 25 | #include "entrypoints/quick/quick_entrypoints_enum.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 26 | #include "entrypoints/runtime_asm_entrypoints.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 27 | #include "handle_scope-inl.h" |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 28 | #include "jit/jit.h" |
| 29 | #include "jit/jit_code_cache.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
| 31 | #include "mirror/class_loader.h" |
| 32 | #include "mirror/throwable.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 33 | #include "oat_quick_method_header.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 34 | #include "stack.h" |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 35 | #include "stack_map.h" |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 39 | static constexpr bool kDebugExceptionDelivery = false; |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 40 | static constexpr size_t kInvalidFrameDepth = 0xffffffff; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 41 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 42 | QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization) |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 43 | : 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 Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 55 | handler_frame_depth_(kInvalidFrameDepth), |
| 56 | full_fragment_done_(false) {} |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 57 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 58 | // Finds catch handler. |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 59 | class CatchBlockStackVisitor FINAL : public StackVisitor { |
| 60 | public: |
| 61 | CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception, |
| 62 | QuickExceptionHandler* exception_handler) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 64 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 65 | exception_(exception), |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 66 | exception_handler_(exception_handler) { |
| 67 | } |
| 68 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 69 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 70 | ArtMethod* method = GetMethod(); |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 71 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 72 | 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 Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 76 | exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 77 | uint32_t next_dex_pc; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 78 | ArtMethod* next_art_method; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 79 | 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 96 | return HandleTryItems(method); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 100 | bool HandleTryItems(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 101 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame^] | 102 | uint32_t dex_pc = dex::kDexNoIndex; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 103 | if (!method->IsNative()) { |
| 104 | dex_pc = GetDexPc(); |
| 105 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame^] | 106 | if (dex_pc != dex::kDexNoIndex) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 107 | bool clear_exception = false; |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame] | 108 | StackHandleScope<1> hs(GetThread()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 109 | Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 110 | uint32_t found_dex_pc = method->FindCatchBlock(to_find, dex_pc, &clear_exception); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 111 | exception_handler_->SetClearException(clear_exception); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame^] | 112 | if (found_dex_pc != dex::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 113 | exception_handler_->SetHandlerMethod(method); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 114 | exception_handler_->SetHandlerDexPc(found_dex_pc); |
David Brazdil | 72f7b88 | 2015-09-15 17:00:52 +0100 | [diff] [blame] | 115 | exception_handler_->SetHandlerQuickFramePc( |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 116 | GetCurrentOatQuickMethodHeader()->ToNativeQuickPc( |
| 117 | method, found_dex_pc, /* is_catch_handler */ true)); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 118 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 119 | exception_handler_->SetHandlerMethodHeader(GetCurrentOatQuickMethodHeader()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 120 | return false; // End stack walk. |
Mingyao Yang | 99170c6 | 2015-07-06 11:10:37 -0700 | [diff] [blame] | 121 | } 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 Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | return true; // Continue stack walk. |
| 133 | } |
| 134 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 135 | // 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 | |
Mathieu Chartier | f5769e1 | 2017-01-10 15:54:41 -0800 | [diff] [blame] | 143 | void QuickExceptionHandler::FindCatch(ObjPtr<mirror::Throwable> exception) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 144 | DCHECK(!is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 145 | if (kDebugExceptionDelivery) { |
| 146 | mirror::String* msg = exception->GetDetailMessage(); |
| 147 | std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : ""); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 148 | self_->DumpStack(LOG_STREAM(INFO) << "Delivering exception: " << exception->PrettyTypeOf() |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 149 | << ": " << str_msg << "\n"); |
| 150 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 151 | StackHandleScope<1> hs(self_); |
| 152 | Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception)); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 153 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 154 | // Walk the stack to find catch handler. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 155 | CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 156 | visitor.WalkStack(true); |
| 157 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 158 | if (kDebugExceptionDelivery) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 159 | if (*handler_quick_frame_ == nullptr) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 160 | LOG(INFO) << "Handler is upcall"; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 161 | } |
| 162 | if (handler_method_ != nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 163 | const DexFile* dex_file = handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 164 | int line_number = annotations::GetLineNumFromPC(dex_file, handler_method_, handler_dex_pc_); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 165 | LOG(INFO) << "Handler: " << handler_method_->PrettyMethod() << " (line: " |
| 166 | << line_number << ")"; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 167 | } |
| 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 Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 174 | self_->SetException(exception_ref.Get()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 175 | } |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 176 | // If the handler is in optimized code, we need to set the catch environment. |
| 177 | if (*handler_quick_frame_ != nullptr && |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 178 | handler_method_header_ != nullptr && |
| 179 | handler_method_header_->IsOptimized()) { |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 180 | SetCatchEnvironmentForOptimizedHandler(&visitor); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static 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 Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 211 | LOG(FATAL) << "Unexpected vreg location " << kind; |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 212 | UNREACHABLE(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void QuickExceptionHandler::SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) { |
| 217 | DCHECK(!is_deoptimization_); |
| 218 | DCHECK(*handler_quick_frame_ != nullptr) << "Method should not be called on upcall exceptions"; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 219 | DCHECK(handler_method_ != nullptr && handler_method_header_->IsOptimized()); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 220 | |
| 221 | if (kDebugExceptionDelivery) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 222 | self_->DumpStack(LOG_STREAM(INFO) << "Setting catch phis: "); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | const size_t number_of_vregs = handler_method_->GetCodeItem()->registers_size_; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 226 | CodeInfo code_info = handler_method_header_->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 227 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 228 | |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 229 | // 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 Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 238 | // 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 Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 244 | DCHECK(throw_vreg_map.IsValid()); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 245 | |
| 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 266 | << "method=" << ArtMethod::PrettyMethod(stack_visitor->GetMethod()) |
| 267 | << ", dex_pc=" << stack_visitor->GetDexPc() << ", " |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 268 | << "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 Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 282 | // Prepares deoptimization. |
| 283 | class DeoptimizeStackVisitor FINAL : public StackVisitor { |
| 284 | public: |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 285 | DeoptimizeStackVisitor(Thread* self, |
| 286 | Context* context, |
| 287 | QuickExceptionHandler* exception_handler, |
| 288 | bool single_frame) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 289 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 290 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 291 | exception_handler_(exception_handler), |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 292 | prev_shadow_frame_(nullptr), |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 293 | stacked_shadow_frame_pushed_(false), |
| 294 | single_frame_deopt_(single_frame), |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 295 | single_frame_done_(false), |
Nicolas Geoffray | b52de24 | 2016-02-19 12:43:12 +0000 | [diff] [blame] | 296 | single_frame_deopt_method_(nullptr), |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 297 | single_frame_deopt_quick_method_header_(nullptr), |
| 298 | callee_method_(nullptr) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | ArtMethod* GetSingleFrameDeoptMethod() const { |
| 302 | return single_frame_deopt_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Nicolas Geoffray | b52de24 | 2016-02-19 12:43:12 +0000 | [diff] [blame] | 305 | const OatQuickMethodHeader* GetSingleFrameDeoptQuickMethodHeader() const { |
| 306 | return single_frame_deopt_quick_method_header_; |
| 307 | } |
| 308 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 309 | void FinishStackWalk() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 310 | // 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 327 | CHECK(callee_method_ != nullptr) << GetMethod()->PrettyMethod(false); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 328 | exception_handler_->SetHandlerQuickArg0(reinterpret_cast<uintptr_t>(callee_method_)); |
| 329 | } |
| 330 | } |
| 331 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 332 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 333 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 334 | ArtMethod* method = GetMethod(); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 335 | if (method == nullptr || single_frame_done_) { |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 336 | FinishStackWalk(); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 337 | return false; // End stack walk. |
| 338 | } else if (method->IsRuntimeMethod()) { |
| 339 | // Ignore callee save method. |
| 340 | DCHECK(method->IsCalleeSaveMethod()); |
| 341 | return true; |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 342 | } 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 Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 347 | callee_method_ = method; |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 348 | return true; |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 349 | } else if (!single_frame_deopt_ && |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 350 | !Runtime::Current()->IsAsyncDeoptimizeable(GetCurrentQuickFramePc())) { |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 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. |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 353 | LOG(WARNING) << "Got request to deoptimize un-deoptimizable method " |
| 354 | << method->PrettyMethod(); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 355 | FinishStackWalk(); |
| 356 | return false; // End stack walk. |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 357 | } else { |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 358 | // Check if a shadow frame already exists for debugger's set-local-value purpose. |
| 359 | const size_t frame_id = GetFrameId(); |
| 360 | ShadowFrame* new_frame = GetThread()->FindDebuggerShadowFrame(frame_id); |
| 361 | const bool* updated_vregs; |
| 362 | const size_t num_regs = method->GetCodeItem()->registers_size_; |
| 363 | if (new_frame == nullptr) { |
| 364 | new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, method, GetDexPc()); |
| 365 | updated_vregs = nullptr; |
| 366 | } else { |
| 367 | updated_vregs = GetThread()->GetUpdatedVRegFlags(frame_id); |
| 368 | DCHECK(updated_vregs != nullptr); |
| 369 | } |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 370 | HandleOptimizingDeoptimization(method, new_frame, updated_vregs); |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 371 | if (updated_vregs != nullptr) { |
| 372 | // Calling Thread::RemoveDebuggerShadowFrameMapping will also delete the updated_vregs |
| 373 | // array so this must come after we processed the frame. |
| 374 | GetThread()->RemoveDebuggerShadowFrameMapping(frame_id); |
| 375 | DCHECK(GetThread()->FindDebuggerShadowFrame(frame_id) == nullptr); |
| 376 | } |
| 377 | if (prev_shadow_frame_ != nullptr) { |
| 378 | prev_shadow_frame_->SetLink(new_frame); |
| 379 | } else { |
| 380 | // Will be popped after the long jump after DeoptimizeStack(), |
| 381 | // right before interpreter::EnterInterpreterFromDeoptimize(). |
| 382 | stacked_shadow_frame_pushed_ = true; |
| 383 | GetThread()->PushStackedShadowFrame( |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 384 | new_frame, StackedShadowFrameType::kDeoptimizationShadowFrame); |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 385 | } |
| 386 | prev_shadow_frame_ = new_frame; |
| 387 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 388 | if (single_frame_deopt_ && !IsInInlinedFrame()) { |
| 389 | // Single-frame deopt ends at the first non-inlined frame and needs to store that method. |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 390 | single_frame_done_ = true; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 391 | single_frame_deopt_method_ = method; |
Nicolas Geoffray | b52de24 | 2016-02-19 12:43:12 +0000 | [diff] [blame] | 392 | single_frame_deopt_quick_method_header_ = GetCurrentOatQuickMethodHeader(); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 393 | } |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 394 | callee_method_ = method; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 395 | return true; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | private: |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 400 | void HandleOptimizingDeoptimization(ArtMethod* m, |
| 401 | ShadowFrame* new_frame, |
| 402 | const bool* updated_vregs) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 403 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 404 | const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); |
| 405 | CodeInfo code_info = method_header->GetOptimizedCodeInfo(); |
| 406 | uintptr_t native_pc_offset = method_header->NativeQuickPcOffset(GetCurrentQuickFramePc()); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 407 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 408 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
| 409 | const size_t number_of_vregs = m->GetCodeItem()->registers_size_; |
Mathieu Chartier | 1a20b68 | 2017-01-31 14:25:16 -0800 | [diff] [blame] | 410 | uint32_t register_mask = code_info.GetRegisterMaskOf(encoding, stack_map); |
David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 411 | BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map); |
David Brazdil | efc3f02 | 2015-10-28 12:19:06 -0500 | [diff] [blame] | 412 | DexRegisterMap vreg_map = IsInInlinedFrame() |
| 413 | ? code_info.GetDexRegisterMapAtDepth(GetCurrentInliningDepth() - 1, |
| 414 | code_info.GetInlineInfoOf(stack_map, encoding), |
| 415 | encoding, |
| 416 | number_of_vregs) |
| 417 | : code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_vregs); |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 418 | |
Nicolas Geoffray | 012fc4e | 2016-01-08 15:58:19 +0000 | [diff] [blame] | 419 | if (!vreg_map.IsValid()) { |
| 420 | return; |
| 421 | } |
| 422 | |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 423 | for (uint16_t vreg = 0; vreg < number_of_vregs; ++vreg) { |
| 424 | if (updated_vregs != nullptr && updated_vregs[vreg]) { |
| 425 | // Keep the value set by debugger. |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | DexRegisterLocation::Kind location = |
| 430 | vreg_map.GetLocationKind(vreg, number_of_vregs, code_info, encoding); |
| 431 | static constexpr uint32_t kDeadValue = 0xEBADDE09; |
| 432 | uint32_t value = kDeadValue; |
| 433 | bool is_reference = false; |
| 434 | |
| 435 | switch (location) { |
| 436 | case DexRegisterLocation::Kind::kInStack: { |
| 437 | const int32_t offset = vreg_map.GetStackOffsetInBytes(vreg, |
| 438 | number_of_vregs, |
| 439 | code_info, |
| 440 | encoding); |
| 441 | const uint8_t* addr = reinterpret_cast<const uint8_t*>(GetCurrentQuickFrame()) + offset; |
| 442 | value = *reinterpret_cast<const uint32_t*>(addr); |
| 443 | uint32_t bit = (offset >> 2); |
Mathieu Chartier | 575d3e6 | 2017-02-06 11:00:40 -0800 | [diff] [blame] | 444 | if (bit < encoding.stack_mask.encoding.BitSize() && stack_mask.LoadBit(bit)) { |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 445 | is_reference = true; |
| 446 | } |
| 447 | break; |
| 448 | } |
| 449 | case DexRegisterLocation::Kind::kInRegister: |
| 450 | case DexRegisterLocation::Kind::kInRegisterHigh: |
| 451 | case DexRegisterLocation::Kind::kInFpuRegister: |
| 452 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: { |
| 453 | uint32_t reg = vreg_map.GetMachineRegister(vreg, number_of_vregs, code_info, encoding); |
| 454 | bool result = GetRegisterIfAccessible(reg, ToVRegKind(location), &value); |
| 455 | CHECK(result); |
| 456 | if (location == DexRegisterLocation::Kind::kInRegister) { |
| 457 | if (((1u << reg) & register_mask) != 0) { |
| 458 | is_reference = true; |
| 459 | } |
| 460 | } |
| 461 | break; |
| 462 | } |
| 463 | case DexRegisterLocation::Kind::kConstant: { |
| 464 | value = vreg_map.GetConstant(vreg, number_of_vregs, code_info, encoding); |
| 465 | if (value == 0) { |
| 466 | // Make it a reference for extra safety. |
| 467 | is_reference = true; |
| 468 | } |
| 469 | break; |
| 470 | } |
| 471 | case DexRegisterLocation::Kind::kNone: { |
| 472 | break; |
| 473 | } |
| 474 | default: { |
| 475 | LOG(FATAL) |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 476 | << "Unexpected location kind " |
| 477 | << vreg_map.GetLocationInternalKind(vreg, |
| 478 | number_of_vregs, |
| 479 | code_info, |
| 480 | encoding); |
Nicolas Geoffray | 3385650 | 2015-10-20 15:52:58 +0100 | [diff] [blame] | 481 | UNREACHABLE(); |
| 482 | } |
| 483 | } |
| 484 | if (is_reference) { |
| 485 | new_frame->SetVRegReference(vreg, reinterpret_cast<mirror::Object*>(value)); |
| 486 | } else { |
| 487 | new_frame->SetVReg(vreg, value); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 492 | static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) { |
| 493 | return static_cast<VRegKind>(kinds.at(reg * 2)); |
| 494 | } |
| 495 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 496 | QuickExceptionHandler* const exception_handler_; |
| 497 | ShadowFrame* prev_shadow_frame_; |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 498 | bool stacked_shadow_frame_pushed_; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 499 | const bool single_frame_deopt_; |
| 500 | bool single_frame_done_; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 501 | ArtMethod* single_frame_deopt_method_; |
Nicolas Geoffray | b52de24 | 2016-02-19 12:43:12 +0000 | [diff] [blame] | 502 | const OatQuickMethodHeader* single_frame_deopt_quick_method_header_; |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 503 | ArtMethod* callee_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 504 | |
| 505 | DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor); |
| 506 | }; |
| 507 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 508 | void QuickExceptionHandler::PrepareForLongJumpToInvokeStubOrInterpreterBridge() { |
| 509 | if (full_fragment_done_) { |
| 510 | // Restore deoptimization exception. When returning from the invoke stub, |
| 511 | // ArtMethod::Invoke() will see the special exception to know deoptimization |
| 512 | // is needed. |
| 513 | self_->SetException(Thread::GetDeoptimizationException()); |
| 514 | } else { |
| 515 | // PC needs to be of the quick-to-interpreter bridge. |
| 516 | int32_t offset; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 517 | offset = GetThreadOffset<kRuntimePointerSize>(kQuickQuickToInterpreterBridge).Int32Value(); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 518 | handler_quick_frame_pc_ = *reinterpret_cast<uintptr_t*>( |
| 519 | reinterpret_cast<uint8_t*>(self_) + offset); |
| 520 | } |
| 521 | } |
| 522 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 523 | void QuickExceptionHandler::DeoptimizeStack() { |
| 524 | DCHECK(is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 525 | if (kDebugExceptionDelivery) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 526 | self_->DumpStack(LOG_STREAM(INFO) << "Deoptimizing: "); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 527 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 528 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 529 | DeoptimizeStackVisitor visitor(self_, context_, this, false); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 530 | visitor.WalkStack(true); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 531 | PrepareForLongJumpToInvokeStubOrInterpreterBridge(); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame] | 534 | void QuickExceptionHandler::DeoptimizeSingleFrame(DeoptimizationKind kind) { |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 535 | DCHECK(is_deoptimization_); |
| 536 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 537 | DeoptimizeStackVisitor visitor(self_, context_, this, true); |
| 538 | visitor.WalkStack(true); |
| 539 | |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 540 | // Compiled code made an explicit deoptimization. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 541 | ArtMethod* deopt_method = visitor.GetSingleFrameDeoptMethod(); |
| 542 | DCHECK(deopt_method != nullptr); |
Nicolas Geoffray | 646d638 | 2017-08-09 10:50:00 +0100 | [diff] [blame] | 543 | if (VLOG_IS_ON(deopt) || kDebugExceptionDelivery) { |
| 544 | LOG(INFO) << "Single-frame deopting: " |
| 545 | << deopt_method->PrettyMethod() |
| 546 | << " due to " |
| 547 | << GetDeoptimizationKindName(kind); |
| 548 | DumpFramesWithType(self_, /* details */ true); |
| 549 | } |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 550 | if (Runtime::Current()->UseJitCompilation()) { |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 551 | Runtime::Current()->GetJit()->GetCodeCache()->InvalidateCompiledCodeFor( |
Nicolas Geoffray | b52de24 | 2016-02-19 12:43:12 +0000 | [diff] [blame] | 552 | deopt_method, visitor.GetSingleFrameDeoptQuickMethodHeader()); |
Nicolas Geoffray | b88d59e | 2016-02-17 11:31:49 +0000 | [diff] [blame] | 553 | } else { |
| 554 | // Transfer the code to interpreter. |
| 555 | Runtime::Current()->GetInstrumentation()->UpdateMethodsCode( |
| 556 | deopt_method, GetQuickToInterpreterBridge()); |
| 557 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 558 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 559 | PrepareForLongJumpToInvokeStubOrInterpreterBridge(); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 562 | void QuickExceptionHandler::DeoptimizePartialFragmentFixup(uintptr_t return_pc) { |
| 563 | // At this point, the instrumentation stack has been updated. We need to install |
| 564 | // the real return pc on stack, in case instrumentation stub is stored there, |
| 565 | // so that the interpreter bridge code can return to the right place. |
| 566 | if (return_pc != 0) { |
| 567 | uintptr_t* pc_addr = reinterpret_cast<uintptr_t*>(handler_quick_frame_); |
| 568 | CHECK(pc_addr != nullptr); |
| 569 | pc_addr--; |
| 570 | *reinterpret_cast<uintptr_t*>(pc_addr) = return_pc; |
| 571 | } |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 572 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 573 | // Architecture-dependent work. This is to get the LR right for x86 and x86-64. |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 574 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 575 | // On x86, the return address is on the stack, so just reuse it. Otherwise we would have to |
| 576 | // change how longjump works. |
| 577 | handler_quick_frame_ = reinterpret_cast<ArtMethod**>( |
| 578 | reinterpret_cast<uintptr_t>(handler_quick_frame_) - sizeof(void*)); |
| 579 | } |
| 580 | } |
| 581 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 582 | // Unwinds all instrumentation stack frame prior to catch handler or upcall. |
| 583 | class InstrumentationStackVisitor : public StackVisitor { |
| 584 | public: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 585 | InstrumentationStackVisitor(Thread* self, size_t frame_depth) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 586 | REQUIRES_SHARED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 587 | : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 588 | frame_depth_(frame_depth), |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 589 | instrumentation_frames_to_pop_(0) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 590 | CHECK_NE(frame_depth_, kInvalidFrameDepth); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 593 | bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 594 | size_t current_frame_depth = GetFrameDepth(); |
| 595 | if (current_frame_depth < frame_depth_) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 596 | CHECK(GetMethod() != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 597 | if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 598 | if (!IsInInlinedFrame()) { |
| 599 | // We do not count inlined frames, because we do not instrument them. The reason we |
| 600 | // include them in the stack walking is the check against `frame_depth_`, which is |
| 601 | // given to us by a visitor that visits inlined frames. |
| 602 | ++instrumentation_frames_to_pop_; |
| 603 | } |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 604 | } |
| 605 | return true; |
| 606 | } else { |
| 607 | // We reached the frame of the catch handler or the upcall. |
| 608 | return false; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | size_t GetInstrumentationFramesToPop() const { |
| 613 | return instrumentation_frames_to_pop_; |
| 614 | } |
| 615 | |
| 616 | private: |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 617 | const size_t frame_depth_; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 618 | size_t instrumentation_frames_to_pop_; |
| 619 | |
| 620 | DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor); |
| 621 | }; |
| 622 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 623 | uintptr_t QuickExceptionHandler::UpdateInstrumentationStack() { |
| 624 | uintptr_t return_pc = 0; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 625 | if (method_tracing_active_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 626 | InstrumentationStackVisitor visitor(self_, handler_frame_depth_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 627 | visitor.WalkStack(true); |
| 628 | |
| 629 | size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop(); |
| 630 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 631 | for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) { |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 632 | return_pc = instrumentation->PopMethodForUnwind(self_, is_deoptimization_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 633 | } |
| 634 | } |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 635 | return return_pc; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 636 | } |
| 637 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 638 | void QuickExceptionHandler::DoLongJump(bool smash_caller_saves) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 639 | // Place context back on thread so it will be available when we continue. |
| 640 | self_->ReleaseLongJumpContext(context_); |
| 641 | context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); |
| 642 | CHECK_NE(handler_quick_frame_pc_, 0u); |
| 643 | context_->SetPC(handler_quick_frame_pc_); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 644 | context_->SetArg0(handler_quick_arg0_); |
| 645 | if (smash_caller_saves) { |
| 646 | context_->SmashCallerSaves(); |
| 647 | } |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 648 | context_->DoLongJump(); |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 649 | UNREACHABLE(); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 652 | // Prints out methods with their type of frame. |
| 653 | class DumpFramesWithTypeStackVisitor FINAL : public StackVisitor { |
| 654 | public: |
Chih-Hung Hsieh | 471118e | 2016-04-29 14:27:41 -0700 | [diff] [blame] | 655 | explicit DumpFramesWithTypeStackVisitor(Thread* self, bool show_details = false) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 656 | REQUIRES_SHARED(Locks::mutator_lock_) |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 657 | : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 658 | show_details_(show_details) {} |
| 659 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 660 | bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 661 | ArtMethod* method = GetMethod(); |
| 662 | if (show_details_) { |
| 663 | LOG(INFO) << "|> pc = " << std::hex << GetCurrentQuickFramePc(); |
| 664 | LOG(INFO) << "|> addr = " << std::hex << reinterpret_cast<uintptr_t>(GetCurrentQuickFrame()); |
| 665 | if (GetCurrentQuickFrame() != nullptr && method != nullptr) { |
| 666 | LOG(INFO) << "|> ret = " << std::hex << GetReturnPc(); |
| 667 | } |
| 668 | } |
| 669 | if (method == nullptr) { |
| 670 | // Transition, do go on, we want to unwind over bridges, all the way. |
| 671 | if (show_details_) { |
| 672 | LOG(INFO) << "N <transition>"; |
| 673 | } |
| 674 | return true; |
| 675 | } else if (method->IsRuntimeMethod()) { |
| 676 | if (show_details_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 677 | LOG(INFO) << "R " << method->PrettyMethod(true); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 678 | } |
| 679 | return true; |
| 680 | } else { |
| 681 | bool is_shadow = GetCurrentShadowFrame() != nullptr; |
| 682 | LOG(INFO) << (is_shadow ? "S" : "Q") |
| 683 | << ((!is_shadow && IsInInlinedFrame()) ? "i" : " ") |
| 684 | << " " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 685 | << method->PrettyMethod(true); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 686 | return true; // Go on. |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | private: |
| 691 | bool show_details_; |
| 692 | |
| 693 | DISALLOW_COPY_AND_ASSIGN(DumpFramesWithTypeStackVisitor); |
| 694 | }; |
| 695 | |
| 696 | void QuickExceptionHandler::DumpFramesWithType(Thread* self, bool details) { |
| 697 | DumpFramesWithTypeStackVisitor visitor(self, details); |
| 698 | visitor.WalkStack(true); |
| 699 | } |
| 700 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 701 | } // namespace art |