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" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 21 | #include "dex_instruction.h" |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 22 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 23 | #include "entrypoints/runtime_asm_entrypoints.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 24 | #include "handle_scope-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
| 26 | #include "mirror/class_loader.h" |
| 27 | #include "mirror/throwable.h" |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 28 | #include "verifier/method_verifier.h" |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 32 | static constexpr bool kDebugExceptionDelivery = false; |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 33 | static constexpr size_t kInvalidFrameDepth = 0xffffffff; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 34 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 35 | QuickExceptionHandler::QuickExceptionHandler(Thread* self, bool is_deoptimization) |
| 36 | : self_(self), context_(self->GetLongJumpContext()), is_deoptimization_(is_deoptimization), |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 37 | method_tracing_active_(is_deoptimization || |
| 38 | Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()), |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 39 | handler_quick_frame_(nullptr), handler_quick_frame_pc_(0), handler_method_(nullptr), |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 40 | handler_dex_pc_(0), clear_exception_(false), handler_frame_depth_(kInvalidFrameDepth) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 43 | // Finds catch handler. |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 44 | class CatchBlockStackVisitor FINAL : public StackVisitor { |
| 45 | public: |
| 46 | CatchBlockStackVisitor(Thread* self, Context* context, Handle<mirror::Throwable>* exception, |
| 47 | QuickExceptionHandler* exception_handler) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 48 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 49 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 50 | exception_(exception), |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 51 | exception_handler_(exception_handler) { |
| 52 | } |
| 53 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 54 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 55 | ArtMethod* method = GetMethod(); |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 56 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 57 | if (method == nullptr) { |
| 58 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 59 | exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 60 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 61 | uint32_t next_dex_pc; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 62 | ArtMethod* next_art_method; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 63 | bool has_next = GetNextMethodAndDexPc(&next_art_method, &next_dex_pc); |
| 64 | // Report the method that did the down call as the handler. |
| 65 | exception_handler_->SetHandlerDexPc(next_dex_pc); |
| 66 | exception_handler_->SetHandlerMethod(next_art_method); |
| 67 | if (!has_next) { |
| 68 | // No next method? Check exception handler is set up for the unhandled exception handler |
| 69 | // case. |
| 70 | DCHECK_EQ(0U, exception_handler_->GetHandlerDexPc()); |
| 71 | DCHECK(nullptr == exception_handler_->GetHandlerMethod()); |
| 72 | } |
| 73 | return false; // End stack walk. |
| 74 | } |
| 75 | if (method->IsRuntimeMethod()) { |
| 76 | // Ignore callee save method. |
| 77 | DCHECK(method->IsCalleeSaveMethod()); |
| 78 | return true; |
| 79 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 80 | return HandleTryItems(method); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 84 | bool HandleTryItems(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 85 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 86 | uint32_t dex_pc = DexFile::kDexNoIndex; |
| 87 | if (!method->IsNative()) { |
| 88 | dex_pc = GetDexPc(); |
| 89 | } |
| 90 | if (dex_pc != DexFile::kDexNoIndex) { |
| 91 | bool clear_exception = false; |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame^] | 92 | StackHandleScope<1> hs(GetThread()); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 93 | Handle<mirror::Class> to_find(hs.NewHandle((*exception_)->GetClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 94 | 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] | 95 | exception_handler_->SetClearException(clear_exception); |
| 96 | if (found_dex_pc != DexFile::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 97 | exception_handler_->SetHandlerMethod(method); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 98 | exception_handler_->SetHandlerDexPc(found_dex_pc); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 99 | exception_handler_->SetHandlerQuickFramePc(method->ToNativeQuickPc(found_dex_pc)); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 100 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
| 101 | return false; // End stack walk. |
| 102 | } |
| 103 | } |
| 104 | return true; // Continue stack walk. |
| 105 | } |
| 106 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 107 | // The exception we're looking for the catch block of. |
| 108 | Handle<mirror::Throwable>* exception_; |
| 109 | // The quick exception handler we're visiting for. |
| 110 | QuickExceptionHandler* const exception_handler_; |
| 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(CatchBlockStackVisitor); |
| 113 | }; |
| 114 | |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 115 | void QuickExceptionHandler::FindCatch(mirror::Throwable* exception) { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 116 | DCHECK(!is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 117 | if (kDebugExceptionDelivery) { |
| 118 | mirror::String* msg = exception->GetDetailMessage(); |
| 119 | std::string str_msg(msg != nullptr ? msg->ToModifiedUtf8() : ""); |
| 120 | self_->DumpStack(LOG(INFO) << "Delivering exception: " << PrettyTypeOf(exception) |
| 121 | << ": " << str_msg << "\n"); |
| 122 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 123 | StackHandleScope<1> hs(self_); |
| 124 | Handle<mirror::Throwable> exception_ref(hs.NewHandle(exception)); |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 125 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 126 | // Walk the stack to find catch handler. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 127 | CatchBlockStackVisitor visitor(self_, context_, &exception_ref, this); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 128 | visitor.WalkStack(true); |
| 129 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 130 | if (kDebugExceptionDelivery) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 131 | if (*handler_quick_frame_ == nullptr) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 132 | LOG(INFO) << "Handler is upcall"; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 133 | } |
| 134 | if (handler_method_ != nullptr) { |
| 135 | const DexFile& dex_file = *handler_method_->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 136 | int line_number = dex_file.GetLineNumFromPC(handler_method_, handler_dex_pc_); |
| 137 | LOG(INFO) << "Handler: " << PrettyMethod(handler_method_) << " (line: " << line_number << ")"; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | if (clear_exception_) { |
| 141 | // Exception was cleared as part of delivery. |
| 142 | DCHECK(!self_->IsExceptionPending()); |
| 143 | } else { |
| 144 | // Put exception back in root set with clear throw location. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 145 | self_->SetException(exception_ref.Get()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 146 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 149 | // Prepares deoptimization. |
| 150 | class DeoptimizeStackVisitor FINAL : public StackVisitor { |
| 151 | public: |
| 152 | DeoptimizeStackVisitor(Thread* self, Context* context, QuickExceptionHandler* exception_handler) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 153 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 154 | : StackVisitor(self, context, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 155 | exception_handler_(exception_handler), |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 156 | prev_shadow_frame_(nullptr), |
| 157 | stacked_shadow_frame_pushed_(false) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 160 | bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 161 | exception_handler_->SetHandlerFrameDepth(GetFrameDepth()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 162 | ArtMethod* method = GetMethod(); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 163 | if (method == nullptr) { |
| 164 | // This is the upcall, we remember the frame and last pc so that we may long jump to them. |
| 165 | exception_handler_->SetHandlerQuickFramePc(GetCurrentQuickFramePc()); |
| 166 | exception_handler_->SetHandlerQuickFrame(GetCurrentQuickFrame()); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 167 | if (!stacked_shadow_frame_pushed_) { |
| 168 | // In case there is no deoptimized shadow frame for this upcall, we still |
| 169 | // need to push a nullptr to the stack since there is always a matching pop after |
| 170 | // the long jump. |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame^] | 171 | GetThread()->PushStackedShadowFrame(nullptr, |
| 172 | StackedShadowFrameType::kDeoptimizationShadowFrame); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 173 | stacked_shadow_frame_pushed_ = true; |
| 174 | } |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 175 | return false; // End stack walk. |
| 176 | } else if (method->IsRuntimeMethod()) { |
| 177 | // Ignore callee save method. |
| 178 | DCHECK(method->IsCalleeSaveMethod()); |
| 179 | return true; |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 180 | } else if (method->IsNative()) { |
| 181 | // If we return from JNI with a pending exception and want to deoptimize, we need to skip |
| 182 | // the native method. |
| 183 | // The top method is a runtime method, the native method comes next. |
| 184 | CHECK_EQ(GetFrameDepth(), 1U); |
| 185 | return true; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 186 | } else { |
| 187 | return HandleDeoptimization(method); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private: |
Sebastien Hertz | c901dd7 | 2014-07-16 11:56:07 +0200 | [diff] [blame] | 192 | static VRegKind GetVRegKind(uint16_t reg, const std::vector<int32_t>& kinds) { |
| 193 | return static_cast<VRegKind>(kinds.at(reg * 2)); |
| 194 | } |
| 195 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 196 | bool HandleDeoptimization(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 197 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 198 | CHECK(code_item != nullptr) << "No code item for " << PrettyMethod(m); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 199 | uint16_t num_regs = code_item->registers_size_; |
| 200 | uint32_t dex_pc = GetDexPc(); |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame^] | 201 | StackHandleScope<2> hs(GetThread()); // Dex cache, class loader and method. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 202 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 203 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
| 204 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(declaring_class->GetClassLoader())); |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame^] | 205 | verifier::MethodVerifier verifier(GetThread(), h_dex_cache->GetDexFile(), h_dex_cache, |
| 206 | h_class_loader, &m->GetClassDef(), code_item, |
| 207 | m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, true, |
| 208 | true, true); |
Andreas Gampe | 2e04bb2 | 2015-02-10 15:37:27 -0800 | [diff] [blame] | 209 | bool verifier_success = verifier.Verify(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 210 | CHECK(verifier_success) << PrettyMethod(m); |
| 211 | ShadowFrame* new_frame = ShadowFrame::CreateDeoptimizedFrame(num_regs, nullptr, m, dex_pc); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 212 | { |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame^] | 213 | ScopedStackedShadowFramePusher pusher(GetThread(), new_frame, |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 214 | StackedShadowFrameType::kShadowFrameUnderConstruction); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 215 | const std::vector<int32_t> kinds(verifier.DescribeVRegs(dex_pc)); |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 216 | |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 217 | // Markers for dead values, used when the verifier knows a Dex register is undefined, |
| 218 | // or when the compiler knows the register has not been initialized, or is not used |
| 219 | // anymore in the method. |
| 220 | static constexpr uint32_t kDeadValue = 0xEBADDE09; |
| 221 | static constexpr uint64_t kLongDeadValue = 0xEBADDE09EBADDE09; |
| 222 | for (uint16_t reg = 0; reg < num_regs; ++reg) { |
| 223 | VRegKind kind = GetVRegKind(reg, kinds); |
| 224 | switch (kind) { |
| 225 | case kUndefined: |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 226 | new_frame->SetVReg(reg, kDeadValue); |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 227 | break; |
| 228 | case kConstant: |
| 229 | new_frame->SetVReg(reg, kinds.at((reg * 2) + 1)); |
| 230 | break; |
| 231 | case kReferenceVReg: { |
| 232 | uint32_t value = 0; |
| 233 | // Check IsReferenceVReg in case the compiled GC map doesn't agree with the verifier. |
| 234 | // We don't want to copy a stale reference into the shadow frame as a reference. |
| 235 | // b/20736048 |
| 236 | if (GetVReg(m, reg, kind, &value) && IsReferenceVReg(m, reg)) { |
| 237 | new_frame->SetVRegReference(reg, reinterpret_cast<mirror::Object*>(value)); |
| 238 | } else { |
| 239 | new_frame->SetVReg(reg, kDeadValue); |
| 240 | } |
| 241 | break; |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 242 | } |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 243 | case kLongLoVReg: |
| 244 | if (GetVRegKind(reg + 1, kinds) == kLongHiVReg) { |
| 245 | // Treat it as a "long" register pair. |
| 246 | uint64_t value = 0; |
| 247 | if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &value)) { |
| 248 | new_frame->SetVRegLong(reg, value); |
| 249 | } else { |
| 250 | new_frame->SetVRegLong(reg, kLongDeadValue); |
| 251 | } |
| 252 | } else { |
| 253 | uint32_t value = 0; |
| 254 | if (GetVReg(m, reg, kind, &value)) { |
| 255 | new_frame->SetVReg(reg, value); |
| 256 | } else { |
| 257 | new_frame->SetVReg(reg, kDeadValue); |
| 258 | } |
| 259 | } |
| 260 | break; |
| 261 | case kLongHiVReg: |
| 262 | if (GetVRegKind(reg - 1, kinds) == kLongLoVReg) { |
| 263 | // Nothing to do: we treated it as a "long" register pair. |
| 264 | } else { |
| 265 | uint32_t value = 0; |
| 266 | if (GetVReg(m, reg, kind, &value)) { |
| 267 | new_frame->SetVReg(reg, value); |
| 268 | } else { |
| 269 | new_frame->SetVReg(reg, kDeadValue); |
| 270 | } |
| 271 | } |
| 272 | break; |
| 273 | case kDoubleLoVReg: |
| 274 | if (GetVRegKind(reg + 1, kinds) == kDoubleHiVReg) { |
| 275 | uint64_t value = 0; |
| 276 | if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &value)) { |
| 277 | // Treat it as a "double" register pair. |
| 278 | new_frame->SetVRegLong(reg, value); |
| 279 | } else { |
| 280 | new_frame->SetVRegLong(reg, kLongDeadValue); |
| 281 | } |
| 282 | } else { |
| 283 | uint32_t value = 0; |
| 284 | if (GetVReg(m, reg, kind, &value)) { |
| 285 | new_frame->SetVReg(reg, value); |
| 286 | } else { |
| 287 | new_frame->SetVReg(reg, kDeadValue); |
| 288 | } |
| 289 | } |
| 290 | break; |
| 291 | case kDoubleHiVReg: |
| 292 | if (GetVRegKind(reg - 1, kinds) == kDoubleLoVReg) { |
| 293 | // Nothing to do: we treated it as a "double" register pair. |
| 294 | } else { |
| 295 | uint32_t value = 0; |
| 296 | if (GetVReg(m, reg, kind, &value)) { |
| 297 | new_frame->SetVReg(reg, value); |
| 298 | } else { |
| 299 | new_frame->SetVReg(reg, kDeadValue); |
| 300 | } |
| 301 | } |
| 302 | break; |
| 303 | default: |
| 304 | uint32_t value = 0; |
| 305 | if (GetVReg(m, reg, kind, &value)) { |
| 306 | new_frame->SetVReg(reg, value); |
| 307 | } else { |
| 308 | new_frame->SetVReg(reg, kDeadValue); |
| 309 | } |
| 310 | break; |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 311 | } |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | if (prev_shadow_frame_ != nullptr) { |
| 315 | prev_shadow_frame_->SetLink(new_frame); |
| 316 | } else { |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 317 | // Will be popped after the long jump after DeoptimizeStack(), |
| 318 | // right before interpreter::EnterInterpreterFromDeoptimize(). |
| 319 | stacked_shadow_frame_pushed_ = true; |
Sebastien Hertz | 26f7286 | 2015-09-15 09:52:07 +0200 | [diff] [blame^] | 320 | GetThread()->PushStackedShadowFrame(new_frame, |
| 321 | StackedShadowFrameType::kDeoptimizationShadowFrame); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 322 | } |
| 323 | prev_shadow_frame_ = new_frame; |
| 324 | return true; |
| 325 | } |
| 326 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 327 | QuickExceptionHandler* const exception_handler_; |
| 328 | ShadowFrame* prev_shadow_frame_; |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 329 | bool stacked_shadow_frame_pushed_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 330 | |
| 331 | DISALLOW_COPY_AND_ASSIGN(DeoptimizeStackVisitor); |
| 332 | }; |
| 333 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 334 | void QuickExceptionHandler::DeoptimizeStack() { |
| 335 | DCHECK(is_deoptimization_); |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 336 | if (kDebugExceptionDelivery) { |
| 337 | self_->DumpStack(LOG(INFO) << "Deoptimizing: "); |
| 338 | } |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 339 | |
| 340 | DeoptimizeStackVisitor visitor(self_, context_, this); |
| 341 | visitor.WalkStack(true); |
| 342 | |
| 343 | // Restore deoptimization exception |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 344 | self_->SetException(Thread::GetDeoptimizationException()); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // Unwinds all instrumentation stack frame prior to catch handler or upcall. |
| 348 | class InstrumentationStackVisitor : public StackVisitor { |
| 349 | public: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 350 | InstrumentationStackVisitor(Thread* self, size_t frame_depth) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 351 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 352 | : StackVisitor(self, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 353 | frame_depth_(frame_depth), |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 354 | instrumentation_frames_to_pop_(0) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 355 | CHECK_NE(frame_depth_, kInvalidFrameDepth); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 358 | bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 359 | size_t current_frame_depth = GetFrameDepth(); |
| 360 | if (current_frame_depth < frame_depth_) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 361 | CHECK(GetMethod() != nullptr); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 362 | if (UNLIKELY(reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) == GetReturnPc())) { |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 363 | if (!IsInInlinedFrame()) { |
| 364 | // We do not count inlined frames, because we do not instrument them. The reason we |
| 365 | // include them in the stack walking is the check against `frame_depth_`, which is |
| 366 | // given to us by a visitor that visits inlined frames. |
| 367 | ++instrumentation_frames_to_pop_; |
| 368 | } |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 369 | } |
| 370 | return true; |
| 371 | } else { |
| 372 | // We reached the frame of the catch handler or the upcall. |
| 373 | return false; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | size_t GetInstrumentationFramesToPop() const { |
| 378 | return instrumentation_frames_to_pop_; |
| 379 | } |
| 380 | |
| 381 | private: |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 382 | const size_t frame_depth_; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 383 | size_t instrumentation_frames_to_pop_; |
| 384 | |
| 385 | DISALLOW_COPY_AND_ASSIGN(InstrumentationStackVisitor); |
| 386 | }; |
| 387 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 388 | void QuickExceptionHandler::UpdateInstrumentationStack() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 389 | if (method_tracing_active_) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 390 | InstrumentationStackVisitor visitor(self_, handler_frame_depth_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 391 | visitor.WalkStack(true); |
| 392 | |
| 393 | size_t instrumentation_frames_to_pop = visitor.GetInstrumentationFramesToPop(); |
| 394 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 395 | for (size_t i = 0; i < instrumentation_frames_to_pop; ++i) { |
| 396 | instrumentation->PopMethodForUnwind(self_, is_deoptimization_); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 401 | void QuickExceptionHandler::DoLongJump() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 402 | // Place context back on thread so it will be available when we continue. |
| 403 | self_->ReleaseLongJumpContext(context_); |
| 404 | context_->SetSP(reinterpret_cast<uintptr_t>(handler_quick_frame_)); |
| 405 | CHECK_NE(handler_quick_frame_pc_, 0u); |
| 406 | context_->SetPC(handler_quick_frame_pc_); |
| 407 | context_->SmashCallerSaves(); |
| 408 | context_->DoLongJump(); |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 409 | UNREACHABLE(); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | } // namespace art |