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