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