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