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
28namespace art {
29
30class ArtMethod;
31class FaultHandler;
32
33class FaultManager {
34 public:
35  FaultManager();
36  ~FaultManager();
37
38  void Init();
39
40  // Unclaim signals.
41  void Release();
42
43  // Unclaim signals and delete registered handlers.
44  void Shutdown();
45  void EnsureArtActionInFrontOfSignalChain();
46
47  void HandleFault(int sig, siginfo_t* info, void* context);
48  void HandleNestedSignal(int sig, siginfo_t* info, void* context);
49
50  // Added handlers are owned by the fault handler and will be freed on Shutdown().
51  void AddHandler(FaultHandler* handler, bool generated_code);
52  void RemoveHandler(FaultHandler* handler);
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.
58  void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, ArtMethod** out_method,
59                                 uintptr_t* out_return_pc, uintptr_t* out_sp)
60                                 NO_THREAD_SAFETY_ANALYSIS;
61  bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc)
62                         NO_THREAD_SAFETY_ANALYSIS;
63
64 private:
65  std::vector<FaultHandler*> generated_code_handlers_;
66  std::vector<FaultHandler*> other_handlers_;
67  struct sigaction oldaction_;
68  bool initialized_;
69  DISALLOW_COPY_AND_ASSIGN(FaultManager);
70};
71
72class FaultHandler {
73 public:
74  explicit FaultHandler(FaultManager* manager);
75  virtual ~FaultHandler() {}
76  FaultManager* GetFaultManager() {
77    return manager_;
78  }
79
80  virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0;
81
82 protected:
83  FaultManager* const manager_;
84
85 private:
86  DISALLOW_COPY_AND_ASSIGN(FaultHandler);
87};
88
89class NullPointerHandler FINAL : public FaultHandler {
90 public:
91  explicit NullPointerHandler(FaultManager* manager);
92
93  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
94
95 private:
96  DISALLOW_COPY_AND_ASSIGN(NullPointerHandler);
97};
98
99class SuspensionHandler FINAL : public FaultHandler {
100 public:
101  explicit SuspensionHandler(FaultManager* manager);
102
103  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
104
105 private:
106  DISALLOW_COPY_AND_ASSIGN(SuspensionHandler);
107};
108
109class StackOverflowHandler FINAL : public FaultHandler {
110 public:
111  explicit StackOverflowHandler(FaultManager* manager);
112
113  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE;
114
115 private:
116  DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler);
117};
118
119class JavaStackTraceHandler FINAL : public FaultHandler {
120 public:
121  explicit JavaStackTraceHandler(FaultManager* manager);
122
123  bool Action(int sig, siginfo_t* siginfo, void* context) OVERRIDE NO_THREAD_SAFETY_ANALYSIS;
124
125 private:
126  DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler);
127};
128
129// Statically allocated so the the signal handler can Get access to it.
130extern FaultManager fault_manager;
131
132}       // namespace art
133#endif  // ART_RUNTIME_FAULT_HANDLER_H_
134
135