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