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