Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| 17 | |
| 18 | #ifndef ART_RUNTIME_FAULT_HANDLER_H_ |
| 19 | #define ART_RUNTIME_FAULT_HANDLER_H_ |
| 20 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <signal.h> |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 26 | #include "base/locks.h" // For annotalysis. |
Andreas Gampe | 5a0430d | 2019-01-04 14:33:57 -0800 | [diff] [blame] | 27 | #include "runtime_globals.h" // For CanDoImplicitNullCheckOn. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 30 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 31 | class ArtMethod; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 32 | class FaultHandler; |
| 33 | |
| 34 | class FaultManager { |
| 35 | public: |
| 36 | FaultManager(); |
| 37 | ~FaultManager(); |
| 38 | |
| 39 | void Init(); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 40 | |
| 41 | // Unclaim signals. |
| 42 | void Release(); |
| 43 | |
| 44 | // Unclaim signals and delete registered handlers. |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 45 | void Shutdown(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 46 | |
Josh Gao | 85a78cf | 2017-03-20 16:26:42 -0700 | [diff] [blame] | 47 | // Try to handle a fault, returns true if successful. |
| 48 | bool HandleFault(int sig, siginfo_t* info, void* context); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 49 | |
| 50 | // Added handlers are owned by the fault handler and will be freed on Shutdown(). |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 51 | void AddHandler(FaultHandler* handler, bool generated_code); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 52 | void RemoveHandler(FaultHandler* handler); |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 53 | |
| 54 | // Note that the following two functions are called in the context of a signal handler. |
| 55 | // The IsInGeneratedCode() function checks that the mutator lock is held before it |
| 56 | // calls GetMethodAndReturnPCAndSP(). |
| 57 | // TODO: think about adding lock assertions and fake lock and unlock functions. |
Nicolas Geoffray | a00b54b | 2019-12-03 14:36:42 +0000 | [diff] [blame] | 58 | void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, |
| 59 | void* context, |
| 60 | ArtMethod** out_method, |
| 61 | uintptr_t* out_return_pc, |
| 62 | uintptr_t* out_sp, |
| 63 | bool* out_is_stack_overflow) |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 64 | NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 65 | bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc) |
| 66 | NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 67 | |
| 68 | private: |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 69 | // The HandleFaultByOtherHandlers function is only called by HandleFault function for generated code. |
| 70 | bool HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context) |
| 71 | NO_THREAD_SAFETY_ANALYSIS; |
| 72 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 73 | std::vector<FaultHandler*> generated_code_handlers_; |
| 74 | std::vector<FaultHandler*> other_handlers_; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 75 | struct sigaction oldaction_; |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 76 | bool initialized_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 77 | DISALLOW_COPY_AND_ASSIGN(FaultManager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | class FaultHandler { |
| 81 | public: |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 82 | explicit FaultHandler(FaultManager* manager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 83 | virtual ~FaultHandler() {} |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 84 | FaultManager* GetFaultManager() { |
| 85 | return manager_; |
| 86 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 87 | |
| 88 | virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 89 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 90 | protected: |
| 91 | FaultManager* const manager_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | DISALLOW_COPY_AND_ASSIGN(FaultHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 97 | class NullPointerHandler final : public FaultHandler { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 98 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 99 | explicit NullPointerHandler(FaultManager* manager); |
| 100 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 101 | bool Action(int sig, siginfo_t* siginfo, void* context) override; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 102 | |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 103 | static bool IsValidImplicitCheck(siginfo_t* siginfo) { |
| 104 | // Our implicit NPE checks always limit the range to a page. |
| 105 | // Note that the runtime will do more exhaustive checks (that we cannot |
| 106 | // reasonably do in signal processing code) based on the dex instruction |
| 107 | // faulting. |
| 108 | return CanDoImplicitNullCheckOn(reinterpret_cast<uintptr_t>(siginfo->si_addr)); |
| 109 | } |
| 110 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 111 | private: |
| 112 | DISALLOW_COPY_AND_ASSIGN(NullPointerHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 115 | class SuspensionHandler final : public FaultHandler { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 116 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 117 | explicit SuspensionHandler(FaultManager* manager); |
| 118 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 119 | bool Action(int sig, siginfo_t* siginfo, void* context) override; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | DISALLOW_COPY_AND_ASSIGN(SuspensionHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 125 | class StackOverflowHandler final : public FaultHandler { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 126 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 127 | explicit StackOverflowHandler(FaultManager* manager); |
| 128 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 129 | bool Action(int sig, siginfo_t* siginfo, void* context) override; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 130 | |
| 131 | private: |
| 132 | DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 135 | class JavaStackTraceHandler final : public FaultHandler { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 136 | public: |
| 137 | explicit JavaStackTraceHandler(FaultManager* manager); |
| 138 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 139 | bool Action(int sig, siginfo_t* siginfo, void* context) override NO_THREAD_SAFETY_ANALYSIS; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 140 | |
| 141 | private: |
| 142 | DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler); |
| 143 | }; |
| 144 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 145 | // Statically allocated so the the signal handler can Get access to it. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 146 | extern FaultManager fault_manager; |
| 147 | |
| 148 | } // namespace art |
| 149 | #endif // ART_RUNTIME_FAULT_HANDLER_H_ |
| 150 | |