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 | |
| 30 | namespace mirror { |
| 31 | class ArtMethod; |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 32 | } // namespace mirror |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 33 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 34 | class FaultHandler; |
| 35 | |
| 36 | class FaultManager { |
| 37 | public: |
| 38 | FaultManager(); |
| 39 | ~FaultManager(); |
| 40 | |
| 41 | void Init(); |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 42 | void Shutdown(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 43 | |
| 44 | void HandleFault(int sig, siginfo_t* info, void* context); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 45 | void AddHandler(FaultHandler* handler, bool generated_code); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 46 | void RemoveHandler(FaultHandler* handler); |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 47 | |
| 48 | // Note that the following two functions are called in the context of a signal handler. |
| 49 | // The IsInGeneratedCode() function checks that the mutator lock is held before it |
| 50 | // calls GetMethodAndReturnPCAndSP(). |
| 51 | // TODO: think about adding lock assertions and fake lock and unlock functions. |
| 52 | void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, mirror::ArtMethod** out_method, |
| 53 | uintptr_t* out_return_pc, uintptr_t* out_sp) |
| 54 | NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 55 | bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc) |
| 56 | NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 57 | |
| 58 | private: |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 59 | std::vector<FaultHandler*> generated_code_handlers_; |
| 60 | std::vector<FaultHandler*> other_handlers_; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 61 | struct sigaction oldaction_; |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 62 | bool initialized_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 63 | DISALLOW_COPY_AND_ASSIGN(FaultManager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | class FaultHandler { |
| 67 | public: |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 68 | explicit FaultHandler(FaultManager* manager); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 69 | virtual ~FaultHandler() {} |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 70 | FaultManager* GetFaultManager() { |
| 71 | return manager_; |
| 72 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 73 | |
| 74 | virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 75 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 76 | protected: |
| 77 | FaultManager* const manager_; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | DISALLOW_COPY_AND_ASSIGN(FaultHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | class NullPointerHandler FINAL : public FaultHandler { |
| 84 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 85 | explicit NullPointerHandler(FaultManager* manager); |
| 86 | |
| 87 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | DISALLOW_COPY_AND_ASSIGN(NullPointerHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | class SuspensionHandler FINAL : public FaultHandler { |
| 94 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 95 | explicit SuspensionHandler(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 | |
| 99 | private: |
| 100 | DISALLOW_COPY_AND_ASSIGN(SuspensionHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | class StackOverflowHandler FINAL : public FaultHandler { |
| 104 | public: |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 105 | explicit StackOverflowHandler(FaultManager* manager); |
| 106 | |
| 107 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 108 | |
| 109 | private: |
| 110 | DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 113 | class JavaStackTraceHandler FINAL : public FaultHandler { |
| 114 | public: |
| 115 | explicit JavaStackTraceHandler(FaultManager* manager); |
| 116 | |
| 117 | bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS; |
| 118 | |
| 119 | private: |
| 120 | DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler); |
| 121 | }; |
| 122 | |
| 123 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 124 | // Statically allocated so the the signal handler can Get access to it. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 125 | extern FaultManager fault_manager; |
| 126 | |
| 127 | } // namespace art |
| 128 | #endif // ART_RUNTIME_FAULT_HANDLER_H_ |
| 129 | |