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 | |
| 21 | #include <signal.h> |
| 22 | #include <vector> |
| 23 | #include <setjmp.h> |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | #include "base/mutex.h" // For annotalysis. |
| 27 | |
| 28 | namespace art { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 29 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 30 | class ArtMethod; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 31 | class FaultHandler; |
| 32 | |
| 33 | class FaultManager { |
| 34 | public: |
| 35 | FaultManager(); |
| 36 | ~FaultManager(); |
| 37 | |
| 38 | void Init(); |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 39 | |
| 40 | // Unclaim signals. |
| 41 | void Release(); |
| 42 | |
| 43 | // Unclaim signals and delete registered handlers. |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 44 | void Shutdown(); |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 45 | void EnsureArtActionInFrontOfSignalChain(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 46 | |
| 47 | void HandleFault(int sig, siginfo_t* info, void* context); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 48 | void HandleNestedSignal(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. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 58 | void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, ArtMethod** out_method, |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 59 | uintptr_t* out_return_pc, uintptr_t* out_sp) |
| 60 | NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 61 | bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc) |
| 62 | NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 63 | |
| 64 | private: |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 65 | // The HandleFaultByOtherHandlers function is only called by HandleFault function for generated code. |
| 66 | bool HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context) |
| 67 | NO_THREAD_SAFETY_ANALYSIS; |
| 68 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 69 | std::vector<FaultHandler*> generated_code_handlers_; |
| 70 | std::vector<FaultHandler*> other_handlers_; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 71 | struct sigaction oldaction_; |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 72 | bool initialized_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(FaultManager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | class FaultHandler { |
| 77 | public: |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 78 | explicit FaultHandler(FaultManager* manager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 79 | virtual ~FaultHandler() {} |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 80 | FaultManager* GetFaultManager() { |
| 81 | return manager_; |
| 82 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 83 | |
| 84 | virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 85 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 86 | protected: |
| 87 | FaultManager* const manager_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | DISALLOW_COPY_AND_ASSIGN(FaultHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | class NullPointerHandler FINAL : public FaultHandler { |
| 94 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 95 | explicit NullPointerHandler(FaultManager* manager); |
| 96 | |
| 97 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 98 | |
Nicolas Geoffray | e8e1127 | 2016-06-28 18:08:46 +0100 | [diff] [blame] | 99 | static bool IsValidImplicitCheck(siginfo_t* siginfo) { |
| 100 | // Our implicit NPE checks always limit the range to a page. |
| 101 | // Note that the runtime will do more exhaustive checks (that we cannot |
| 102 | // reasonably do in signal processing code) based on the dex instruction |
| 103 | // faulting. |
| 104 | return CanDoImplicitNullCheckOn(reinterpret_cast<uintptr_t>(siginfo->si_addr)); |
| 105 | } |
| 106 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 107 | private: |
| 108 | DISALLOW_COPY_AND_ASSIGN(NullPointerHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | class SuspensionHandler FINAL : public FaultHandler { |
| 112 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 113 | explicit SuspensionHandler(FaultManager* manager); |
| 114 | |
| 115 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 116 | |
| 117 | private: |
| 118 | DISALLOW_COPY_AND_ASSIGN(SuspensionHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | class StackOverflowHandler FINAL : public FaultHandler { |
| 122 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 123 | explicit StackOverflowHandler(FaultManager* manager); |
| 124 | |
| 125 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 126 | |
| 127 | private: |
| 128 | DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 131 | class JavaStackTraceHandler FINAL : public FaultHandler { |
| 132 | public: |
| 133 | explicit JavaStackTraceHandler(FaultManager* manager); |
| 134 | |
| 135 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS; |
| 136 | |
| 137 | private: |
| 138 | DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler); |
| 139 | }; |
| 140 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 141 | // Statically allocated so the the signal handler can Get access to it. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 142 | extern FaultManager fault_manager; |
| 143 | |
| 144 | } // namespace art |
| 145 | #endif // ART_RUNTIME_FAULT_HANDLER_H_ |
| 146 | |