blob: d56cf17861d943f6c8bb2d6d29aaf6a326e8e749 [file] [log] [blame]
Dave Allisonb373e092014-02-20 16:06:36 -08001/*
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
28namespace art {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070029
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070030class ArtMethod;
Dave Allisonb373e092014-02-20 16:06:36 -080031class FaultHandler;
32
33class FaultManager {
34 public:
35 FaultManager();
36 ~FaultManager();
37
38 void Init();
Andreas Gampe928f72b2014-09-09 19:53:48 -070039
40 // Unclaim signals.
41 void Release();
42
43 // Unclaim signals and delete registered handlers.
Dave Allison1f8ef6f2014-08-20 17:38:41 -070044 void Shutdown();
Dave Allisonb373e092014-02-20 16:06:36 -080045
Josh Gao85a78cf2017-03-20 16:26:42 -070046 // Try to handle a fault, returns true if successful.
47 bool HandleFault(int sig, siginfo_t* info, void* context);
Andreas Gampe928f72b2014-09-09 19:53:48 -070048
49 // Added handlers are owned by the fault handler and will be freed on Shutdown().
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070050 void AddHandler(FaultHandler* handler, bool generated_code);
Dave Allisonb373e092014-02-20 16:06:36 -080051 void RemoveHandler(FaultHandler* handler);
Dave Allisondfd3b472014-07-16 16:04:32 -070052
53 // Note that the following two functions are called in the context of a signal handler.
54 // The IsInGeneratedCode() function checks that the mutator lock is held before it
55 // calls GetMethodAndReturnPCAndSP().
56 // TODO: think about adding lock assertions and fake lock and unlock functions.
Mathieu Chartiere401d142015-04-22 13:56:20 -070057 void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, ArtMethod** out_method,
Dave Allisondfd3b472014-07-16 16:04:32 -070058 uintptr_t* out_return_pc, uintptr_t* out_sp)
59 NO_THREAD_SAFETY_ANALYSIS;
Dave Allison69dfe512014-07-11 17:11:58 +000060 bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc)
61 NO_THREAD_SAFETY_ANALYSIS;
Dave Allisonb373e092014-02-20 16:06:36 -080062
63 private:
jgu211376bdf2016-01-18 09:12:33 -050064 // The HandleFaultByOtherHandlers function is only called by HandleFault function for generated code.
65 bool HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context)
66 NO_THREAD_SAFETY_ANALYSIS;
67
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070068 std::vector<FaultHandler*> generated_code_handlers_;
69 std::vector<FaultHandler*> other_handlers_;
Dave Allisonb373e092014-02-20 16:06:36 -080070 struct sigaction oldaction_;
Dave Allison1f8ef6f2014-08-20 17:38:41 -070071 bool initialized_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070072 DISALLOW_COPY_AND_ASSIGN(FaultManager);
Dave Allisonb373e092014-02-20 16:06:36 -080073};
74
75class FaultHandler {
76 public:
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070077 explicit FaultHandler(FaultManager* manager);
Dave Allisonb373e092014-02-20 16:06:36 -080078 virtual ~FaultHandler() {}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070079 FaultManager* GetFaultManager() {
80 return manager_;
81 }
Dave Allisonb373e092014-02-20 16:06:36 -080082
83 virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070084
Dave Allisonb373e092014-02-20 16:06:36 -080085 protected:
86 FaultManager* const manager_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070087
88 private:
89 DISALLOW_COPY_AND_ASSIGN(FaultHandler);
Dave Allisonb373e092014-02-20 16:06:36 -080090};
91
92class NullPointerHandler FINAL : public FaultHandler {
93 public:
Dave Allisonb373e092014-02-20 16:06:36 -080094 explicit NullPointerHandler(FaultManager* manager);
95
96 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070097
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010098 static bool IsValidImplicitCheck(siginfo_t* siginfo) {
99 // Our implicit NPE checks always limit the range to a page.
100 // Note that the runtime will do more exhaustive checks (that we cannot
101 // reasonably do in signal processing code) based on the dex instruction
102 // faulting.
103 return CanDoImplicitNullCheckOn(reinterpret_cast<uintptr_t>(siginfo->si_addr));
104 }
105
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700106 private:
107 DISALLOW_COPY_AND_ASSIGN(NullPointerHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800108};
109
110class SuspensionHandler FINAL : public FaultHandler {
111 public:
Dave Allisonb373e092014-02-20 16:06:36 -0800112 explicit SuspensionHandler(FaultManager* manager);
113
114 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700115
116 private:
117 DISALLOW_COPY_AND_ASSIGN(SuspensionHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800118};
119
120class StackOverflowHandler FINAL : public FaultHandler {
121 public:
Dave Allisonb373e092014-02-20 16:06:36 -0800122 explicit StackOverflowHandler(FaultManager* manager);
123
124 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800128};
129
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700130class JavaStackTraceHandler FINAL : public FaultHandler {
131 public:
132 explicit JavaStackTraceHandler(FaultManager* manager);
133
134 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
135
136 private:
137 DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler);
138};
139
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140// Statically allocated so the the signal handler can Get access to it.
Dave Allisonb373e092014-02-20 16:06:36 -0800141extern FaultManager fault_manager;
142
143} // namespace art
144#endif // ART_RUNTIME_FAULT_HANDLER_H_
145