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