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