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 | #ifndef ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_ |
| 18 | #define ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_ |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 19 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 20 | #include "base/logging.h" |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 22 | #include "base/mutex.h" |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame^] | 23 | #include "deoptimization_kind.h" |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 24 | #include "stack_reference.h" |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 28 | namespace mirror { |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 29 | class Throwable; |
| 30 | } // namespace mirror |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 31 | class ArtMethod; |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 32 | class Context; |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 33 | class OatQuickMethodHeader; |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 34 | class Thread; |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 35 | class ShadowFrame; |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 36 | class StackVisitor; |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 37 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 38 | // Manages exception delivery for Quick backend. |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 39 | class QuickExceptionHandler { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 40 | public: |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 41 | QuickExceptionHandler(Thread* self, bool is_deoptimization) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 42 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 43 | |
Andreas Gampe | 65b798e | 2015-04-06 09:35:22 -0700 | [diff] [blame] | 44 | NO_RETURN ~QuickExceptionHandler() { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 45 | LOG(FATAL) << "UNREACHABLE"; // Expected to take long jump. |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 46 | UNREACHABLE(); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 49 | // Find the catch handler for the given exception. |
Mathieu Chartier | f5769e1 | 2017-01-10 15:54:41 -0800 | [diff] [blame] | 50 | void FindCatch(ObjPtr<mirror::Throwable> exception) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 51 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 52 | // Deoptimize the stack to the upcall/some code that's not deoptimizeable. For |
| 53 | // every compiled frame, we create a "copy" shadow frame that will be executed |
| 54 | // with the interpreter. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 55 | void DeoptimizeStack() REQUIRES_SHARED(Locks::mutator_lock_); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 56 | |
| 57 | // Deoptimize a single frame. It's directly triggered from compiled code. It |
| 58 | // has the following properties: |
| 59 | // - It deoptimizes a single frame, which can include multiple inlined frames. |
| 60 | // - It doesn't have return result or pending exception at the deoptimization point. |
| 61 | // - It always deoptimizes, even if IsDeoptimizeable() returns false for the |
| 62 | // code, since HDeoptimize always saves the full environment. So it overrides |
| 63 | // the result of IsDeoptimizeable(). |
| 64 | // - It can be either full-fragment, or partial-fragment deoptimization, depending |
| 65 | // on whether that single frame covers full or partial fragment. |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame^] | 66 | void DeoptimizeSingleFrame(DeoptimizationKind kind) REQUIRES_SHARED(Locks::mutator_lock_); |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 67 | |
| 68 | void DeoptimizePartialFragmentFixup(uintptr_t return_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 69 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 70 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 71 | // Update the instrumentation stack by removing all methods that will be unwound |
| 72 | // by the exception being thrown. |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 73 | // Return the return pc of the last frame that's unwound. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 74 | uintptr_t UpdateInstrumentationStack() REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 75 | |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 76 | // Set up environment before delivering an exception to optimized code. |
| 77 | void SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 78 | REQUIRES_SHARED(Locks::mutator_lock_); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 79 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 80 | // Long jump either to a catch handler or to the upcall. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 81 | NO_RETURN void DoLongJump(bool smash_caller_saves = true) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 82 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 83 | void SetHandlerQuickFrame(ArtMethod** handler_quick_frame) { |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 84 | handler_quick_frame_ = handler_quick_frame; |
| 85 | } |
| 86 | |
| 87 | void SetHandlerQuickFramePc(uintptr_t handler_quick_frame_pc) { |
| 88 | handler_quick_frame_pc_ = handler_quick_frame_pc; |
| 89 | } |
| 90 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 91 | void SetHandlerMethodHeader(const OatQuickMethodHeader* handler_method_header) { |
| 92 | handler_method_header_ = handler_method_header; |
| 93 | } |
| 94 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 95 | void SetHandlerQuickArg0(uintptr_t handler_quick_arg0) { |
| 96 | handler_quick_arg0_ = handler_quick_arg0; |
| 97 | } |
| 98 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 99 | ArtMethod* GetHandlerMethod() const { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 100 | return handler_method_; |
| 101 | } |
| 102 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 103 | void SetHandlerMethod(ArtMethod* handler_quick_method) { |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 104 | handler_method_ = handler_quick_method; |
| 105 | } |
| 106 | |
| 107 | uint32_t GetHandlerDexPc() const { |
| 108 | return handler_dex_pc_; |
| 109 | } |
| 110 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 111 | void SetHandlerDexPc(uint32_t dex_pc) { |
| 112 | handler_dex_pc_ = dex_pc; |
| 113 | } |
| 114 | |
| 115 | void SetClearException(bool clear_exception) { |
| 116 | clear_exception_ = clear_exception; |
| 117 | } |
| 118 | |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 119 | void SetHandlerFrameDepth(size_t frame_depth) { |
| 120 | handler_frame_depth_ = frame_depth; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 123 | bool IsFullFragmentDone() const { |
| 124 | return full_fragment_done_; |
| 125 | } |
| 126 | |
| 127 | void SetFullFragmentDone(bool full_fragment_done) { |
| 128 | full_fragment_done_ = full_fragment_done; |
| 129 | } |
| 130 | |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 131 | // Walk the stack frames of the given thread, printing out non-runtime methods with their types |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 132 | // of frames. Helps to verify that partial-fragment deopt really works as expected. |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 133 | static void DumpFramesWithType(Thread* self, bool details = false) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 134 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 135 | |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 136 | private: |
| 137 | Thread* const self_; |
| 138 | Context* const context_; |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 139 | // Should we deoptimize the stack? |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 140 | const bool is_deoptimization_; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 141 | // Is method tracing active? |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 142 | const bool method_tracing_active_; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 143 | // Quick frame with found handler or last frame if no handler found. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 144 | ArtMethod** handler_quick_frame_; |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 145 | // PC to branch to for the handler. |
| 146 | uintptr_t handler_quick_frame_pc_; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 147 | // Quick code of the handler. |
| 148 | const OatQuickMethodHeader* handler_method_header_; |
Andreas Gampe | 639bdd1 | 2015-06-03 11:22:45 -0700 | [diff] [blame] | 149 | // The value for argument 0. |
| 150 | uintptr_t handler_quick_arg0_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 151 | // The handler method to report to the debugger. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 152 | ArtMethod* handler_method_; |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 153 | // The handler's dex PC, zero implies an uncaught exception. |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 154 | uint32_t handler_dex_pc_; |
| 155 | // Should the exception be cleared as the catch block has no move-exception? |
| 156 | bool clear_exception_; |
Hiroshi Yamauchi | 649278c | 2014-08-13 11:12:22 -0700 | [diff] [blame] | 157 | // Frame depth of the catch handler or the upcall. |
| 158 | size_t handler_frame_depth_; |
Mingyao Yang | f711f2c | 2016-05-23 12:29:39 -0700 | [diff] [blame] | 159 | // Does the handler successfully walk the full fragment (not stopped |
| 160 | // by some code that's not deoptimizeable)? Even single-frame deoptimization |
| 161 | // can set this to true if the fragment contains only one quick frame. |
| 162 | bool full_fragment_done_; |
| 163 | |
| 164 | void PrepareForLongJumpToInvokeStubOrInterpreterBridge() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 165 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 166 | |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 167 | DISALLOW_COPY_AND_ASSIGN(QuickExceptionHandler); |
Sebastien Hertz | d45a1f5 | 2014-01-09 14:56:54 +0100 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace art |
Sebastien Hertz | fd3077e | 2014-04-23 10:32:43 +0200 | [diff] [blame] | 171 | #endif // ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_ |