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