blob: 5579d368a3be54e0d66b03af53394da6f5b5b3a9 [file] [log] [blame]
Sebastien Hertzd45a1f52014-01-09 14:56:54 +01001/*
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 Hertzfd3077e2014-04-23 10:32:43 +020017#ifndef ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_
18#define ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010019
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21
Andreas Gampe794ad762015-02-23 08:12:24 -080022#include "base/macros.h"
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020023#include "base/mutex.h"
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +010024#include "deoptimization_kind.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "stack_reference.h"
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010026
27namespace art {
28
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020029namespace mirror {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020030class Throwable;
31} // namespace mirror
Mathieu Chartiere401d142015-04-22 13:56:20 -070032class ArtMethod;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020033class Context;
Vladimir Marko3a21e382016-09-02 12:38:38 +010034class OatQuickMethodHeader;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020035class Thread;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020036class ShadowFrame;
Vladimir Marko3a21e382016-09-02 12:38:38 +010037class StackVisitor;
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020038
Elliott Hughes956af0f2014-12-11 14:34:28 -080039// Manages exception delivery for Quick backend.
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020040class QuickExceptionHandler {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010041 public:
Sebastien Hertzfd3077e2014-04-23 10:32:43 +020042 QuickExceptionHandler(Thread* self, bool is_deoptimization)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070043 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010044
Andreas Gampe65b798e2015-04-06 09:35:22 -070045 NO_RETURN ~QuickExceptionHandler() {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010046 LOG(FATAL) << "UNREACHABLE"; // Expected to take long jump.
Ian Rogers2c4257b2014-10-24 14:20:06 -070047 UNREACHABLE();
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010048 }
49
Alex Light2c8206f2018-06-08 14:51:09 -070050 // Find the catch handler for the given exception and call all required Instrumentation methods.
51 // Note this might result in the exception being caught being different from 'exception'.
Mathieu Chartierf5769e12017-01-10 15:54:41 -080052 void FindCatch(ObjPtr<mirror::Throwable> exception) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz520633b2015-09-08 17:03:36 +020053
Mingyao Yangf711f2c2016-05-23 12:29:39 -070054 // Deoptimize the stack to the upcall/some code that's not deoptimizeable. For
55 // every compiled frame, we create a "copy" shadow frame that will be executed
56 // with the interpreter.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070057 void DeoptimizeStack() REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yangf711f2c2016-05-23 12:29:39 -070058
59 // Deoptimize a single frame. It's directly triggered from compiled code. It
60 // has the following properties:
61 // - It deoptimizes a single frame, which can include multiple inlined frames.
62 // - It doesn't have return result or pending exception at the deoptimization point.
63 // - It always deoptimizes, even if IsDeoptimizeable() returns false for the
64 // code, since HDeoptimize always saves the full environment. So it overrides
65 // the result of IsDeoptimizeable().
66 // - It can be either full-fragment, or partial-fragment deoptimization, depending
67 // on whether that single frame covers full or partial fragment.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +010068 void DeoptimizeSingleFrame(DeoptimizationKind kind) REQUIRES_SHARED(Locks::mutator_lock_);
Mingyao Yangf711f2c2016-05-23 12:29:39 -070069
70 void DeoptimizePartialFragmentFixup(uintptr_t return_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe639bdd12015-06-03 11:22:45 -070072
Sebastien Hertz520633b2015-09-08 17:03:36 +020073 // Update the instrumentation stack by removing all methods that will be unwound
74 // by the exception being thrown.
Mingyao Yangf711f2c2016-05-23 12:29:39 -070075 // Return the return pc of the last frame that's unwound.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070076 uintptr_t UpdateInstrumentationStack() REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz520633b2015-09-08 17:03:36 +020077
David Brazdil77a48ae2015-09-15 12:34:04 +000078 // Set up environment before delivering an exception to optimized code.
79 void SetCatchEnvironmentForOptimizedHandler(StackVisitor* stack_visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070080 REQUIRES_SHARED(Locks::mutator_lock_);
David Brazdil77a48ae2015-09-15 12:34:04 +000081
Sebastien Hertz520633b2015-09-08 17:03:36 +020082 // Long jump either to a catch handler or to the upcall.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 NO_RETURN void DoLongJump(bool smash_caller_saves = true) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010084
Mathieu Chartiere401d142015-04-22 13:56:20 -070085 void SetHandlerQuickFrame(ArtMethod** handler_quick_frame) {
Sebastien Hertzd45a1f52014-01-09 14:56:54 +010086 handler_quick_frame_ = handler_quick_frame;
87 }
88
89 void SetHandlerQuickFramePc(uintptr_t handler_quick_frame_pc) {
90 handler_quick_frame_pc_ = handler_quick_frame_pc;
91 }
92
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010093 void SetHandlerMethodHeader(const OatQuickMethodHeader* handler_method_header) {
94 handler_method_header_ = handler_method_header;
95 }
96
Andreas Gampe639bdd12015-06-03 11:22:45 -070097 void SetHandlerQuickArg0(uintptr_t handler_quick_arg0) {
98 handler_quick_arg0_ = handler_quick_arg0;
99 }
100
Mathieu Chartiere401d142015-04-22 13:56:20 -0700101 ArtMethod* GetHandlerMethod() const {
Ian Rogers5cf98192014-05-29 21:31:50 -0700102 return handler_method_;
103 }
104
Mathieu Chartiere401d142015-04-22 13:56:20 -0700105 void SetHandlerMethod(ArtMethod* handler_quick_method) {
Ian Rogers5cf98192014-05-29 21:31:50 -0700106 handler_method_ = handler_quick_method;
107 }
108
109 uint32_t GetHandlerDexPc() const {
110 return handler_dex_pc_;
111 }
112
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100113 void SetHandlerDexPc(uint32_t dex_pc) {
114 handler_dex_pc_ = dex_pc;
115 }
116
Roland Levillainb77b6982017-06-08 18:03:48 +0100117 bool GetClearException() const {
118 return clear_exception_;
119 }
120
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100121 void SetClearException(bool clear_exception) {
122 clear_exception_ = clear_exception;
123 }
124
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700125 void SetHandlerFrameDepth(size_t frame_depth) {
126 handler_frame_depth_ = frame_depth;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100127 }
128
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700129 bool IsFullFragmentDone() const {
130 return full_fragment_done_;
131 }
132
133 void SetFullFragmentDone(bool full_fragment_done) {
134 full_fragment_done_ = full_fragment_done;
135 }
136
Andreas Gampe639bdd12015-06-03 11:22:45 -0700137 // Walk the stack frames of the given thread, printing out non-runtime methods with their types
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700138 // of frames. Helps to verify that partial-fragment deopt really works as expected.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700139 static void DumpFramesWithType(Thread* self, bool details = false)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700140 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700141
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100142 private:
143 Thread* const self_;
144 Context* const context_;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200145 // Should we deoptimize the stack?
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100146 const bool is_deoptimization_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100147 // Is method tracing active?
Sebastien Hertz520633b2015-09-08 17:03:36 +0200148 const bool method_tracing_active_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100149 // Quick frame with found handler or last frame if no handler found.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 ArtMethod** handler_quick_frame_;
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100151 // PC to branch to for the handler.
152 uintptr_t handler_quick_frame_pc_;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100153 // Quick code of the handler.
154 const OatQuickMethodHeader* handler_method_header_;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700155 // The value for argument 0.
156 uintptr_t handler_quick_arg0_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700157 // The handler method to report to the debugger.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700158 ArtMethod* handler_method_;
Ian Rogers5cf98192014-05-29 21:31:50 -0700159 // The handler's dex PC, zero implies an uncaught exception.
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100160 uint32_t handler_dex_pc_;
161 // Should the exception be cleared as the catch block has no move-exception?
162 bool clear_exception_;
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700163 // Frame depth of the catch handler or the upcall.
164 size_t handler_frame_depth_;
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700165 // Does the handler successfully walk the full fragment (not stopped
166 // by some code that's not deoptimizeable)? Even single-frame deoptimization
167 // can set this to true if the fragment contains only one quick frame.
168 bool full_fragment_done_;
169
170 void PrepareForLongJumpToInvokeStubOrInterpreterBridge()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100172
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200173 DISALLOW_COPY_AND_ASSIGN(QuickExceptionHandler);
Sebastien Hertzd45a1f52014-01-09 14:56:54 +0100174};
175
176} // namespace art
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200177#endif // ART_RUNTIME_QUICK_EXCEPTION_HANDLER_H_