blob: 8b66a6f3233ce2aed14ab72c22efa389cb3ea61a [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
30namespace mirror {
31class ArtMethod;
Ian Rogers576ca0c2014-06-06 15:58:22 -070032} // namespace mirror
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070033
Dave Allisonb373e092014-02-20 16:06:36 -080034class FaultHandler;
35
36class FaultManager {
37 public:
38 FaultManager();
39 ~FaultManager();
40
41 void Init();
Andreas Gampe928f72b2014-09-09 19:53:48 -070042
43 // Unclaim signals.
44 void Release();
45
46 // Unclaim signals and delete registered handlers.
Dave Allison1f8ef6f2014-08-20 17:38:41 -070047 void Shutdown();
Dave Allisonb373e092014-02-20 16:06:36 -080048
49 void HandleFault(int sig, siginfo_t* info, void* context);
Dave Allison8ce6b902014-08-26 11:07:58 -070050 void HandleNestedSignal(int sig, siginfo_t* info, void* context);
Andreas Gampe928f72b2014-09-09 19:53:48 -070051
52 // Added handlers are owned by the fault handler and will be freed on Shutdown().
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070053 void AddHandler(FaultHandler* handler, bool generated_code);
Dave Allisonb373e092014-02-20 16:06:36 -080054 void RemoveHandler(FaultHandler* handler);
Dave Allisondfd3b472014-07-16 16:04:32 -070055
56 // Note that the following two functions are called in the context of a signal handler.
57 // The IsInGeneratedCode() function checks that the mutator lock is held before it
58 // calls GetMethodAndReturnPCAndSP().
59 // TODO: think about adding lock assertions and fake lock and unlock functions.
60 void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, mirror::ArtMethod** out_method,
61 uintptr_t* out_return_pc, uintptr_t* out_sp)
62 NO_THREAD_SAFETY_ANALYSIS;
Dave Allison69dfe512014-07-11 17:11:58 +000063 bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc)
64 NO_THREAD_SAFETY_ANALYSIS;
Dave Allisonb373e092014-02-20 16:06:36 -080065
66 private:
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070067 std::vector<FaultHandler*> generated_code_handlers_;
68 std::vector<FaultHandler*> other_handlers_;
Dave Allisonb373e092014-02-20 16:06:36 -080069 struct sigaction oldaction_;
Dave Allison1f8ef6f2014-08-20 17:38:41 -070070 bool initialized_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070071 DISALLOW_COPY_AND_ASSIGN(FaultManager);
Dave Allisonb373e092014-02-20 16:06:36 -080072};
73
74class FaultHandler {
75 public:
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070076 explicit FaultHandler(FaultManager* manager);
Dave Allisonb373e092014-02-20 16:06:36 -080077 virtual ~FaultHandler() {}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070078 FaultManager* GetFaultManager() {
79 return manager_;
80 }
Dave Allisonb373e092014-02-20 16:06:36 -080081
82 virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070083
Dave Allisonb373e092014-02-20 16:06:36 -080084 protected:
85 FaultManager* const manager_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070086
87 private:
88 DISALLOW_COPY_AND_ASSIGN(FaultHandler);
Dave Allisonb373e092014-02-20 16:06:36 -080089};
90
91class NullPointerHandler FINAL : public FaultHandler {
92 public:
Dave Allisonb373e092014-02-20 16:06:36 -080093 explicit NullPointerHandler(FaultManager* manager);
94
95 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070096
97 private:
98 DISALLOW_COPY_AND_ASSIGN(NullPointerHandler);
Dave Allisonb373e092014-02-20 16:06:36 -080099};
100
101class SuspensionHandler FINAL : public FaultHandler {
102 public:
Dave Allisonb373e092014-02-20 16:06:36 -0800103 explicit SuspensionHandler(FaultManager* manager);
104
105 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700106
107 private:
108 DISALLOW_COPY_AND_ASSIGN(SuspensionHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800109};
110
111class StackOverflowHandler FINAL : public FaultHandler {
112 public:
Dave Allisonb373e092014-02-20 16:06:36 -0800113 explicit StackOverflowHandler(FaultManager* manager);
114
115 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700116
117 private:
118 DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800119};
120
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700121class JavaStackTraceHandler FINAL : public FaultHandler {
122 public:
123 explicit JavaStackTraceHandler(FaultManager* manager);
124
125 bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
126
127 private:
128 DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler);
129};
130
131
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132// Statically allocated so the the signal handler can Get access to it.
Dave Allisonb373e092014-02-20 16:06:36 -0800133extern FaultManager fault_manager;
134
135} // namespace art
136#endif // ART_RUNTIME_FAULT_HANDLER_H_
137