blob: f6cf2d77d7035089ba0064b8d0e01de6a48bf890 [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <signal.h>
Dave Allisonb373e092014-02-20 16:06:36 -080022#include <stdint.h>
23
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include <vector>
25
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080026#include "base/locks.h" // For annotalysis.
Andreas Gampe5a0430d2019-01-04 14:33:57 -080027#include "runtime_globals.h" // For CanDoImplicitNullCheckOn.
Dave Allisonb373e092014-02-20 16:06:36 -080028
29namespace art {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070030
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070031class ArtMethod;
Dave Allisonb373e092014-02-20 16:06:36 -080032class FaultHandler;
33
34class FaultManager {
35 public:
36 FaultManager();
37 ~FaultManager();
38
39 void Init();
Andreas Gampe928f72b2014-09-09 19:53:48 -070040
41 // Unclaim signals.
42 void Release();
43
44 // Unclaim signals and delete registered handlers.
Dave Allison1f8ef6f2014-08-20 17:38:41 -070045 void Shutdown();
Dave Allisonb373e092014-02-20 16:06:36 -080046
Josh Gao85a78cf2017-03-20 16:26:42 -070047 // Try to handle a fault, returns true if successful.
48 bool HandleFault(int sig, siginfo_t* info, void* context);
Andreas Gampe928f72b2014-09-09 19:53:48 -070049
50 // Added handlers are owned by the fault handler and will be freed on Shutdown().
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070051 void AddHandler(FaultHandler* handler, bool generated_code);
Dave Allisonb373e092014-02-20 16:06:36 -080052 void RemoveHandler(FaultHandler* handler);
Dave Allisondfd3b472014-07-16 16:04:32 -070053
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 Chartiere401d142015-04-22 13:56:20 -070058 void GetMethodAndReturnPcAndSp(siginfo_t* siginfo, void* context, ArtMethod** out_method,
Dave Allisondfd3b472014-07-16 16:04:32 -070059 uintptr_t* out_return_pc, uintptr_t* out_sp)
60 NO_THREAD_SAFETY_ANALYSIS;
Dave Allison69dfe512014-07-11 17:11:58 +000061 bool IsInGeneratedCode(siginfo_t* siginfo, void *context, bool check_dex_pc)
62 NO_THREAD_SAFETY_ANALYSIS;
Dave Allisonb373e092014-02-20 16:06:36 -080063
64 private:
jgu211376bdf2016-01-18 09:12:33 -050065 // 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 Chartierc751fdc2014-03-30 15:25:44 -070069 std::vector<FaultHandler*> generated_code_handlers_;
70 std::vector<FaultHandler*> other_handlers_;
Dave Allisonb373e092014-02-20 16:06:36 -080071 struct sigaction oldaction_;
Dave Allison1f8ef6f2014-08-20 17:38:41 -070072 bool initialized_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070073 DISALLOW_COPY_AND_ASSIGN(FaultManager);
Dave Allisonb373e092014-02-20 16:06:36 -080074};
75
76class FaultHandler {
77 public:
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070078 explicit FaultHandler(FaultManager* manager);
Dave Allisonb373e092014-02-20 16:06:36 -080079 virtual ~FaultHandler() {}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070080 FaultManager* GetFaultManager() {
81 return manager_;
82 }
Dave Allisonb373e092014-02-20 16:06:36 -080083
84 virtual bool Action(int sig, siginfo_t* siginfo, void* context) = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070085
Dave Allisonb373e092014-02-20 16:06:36 -080086 protected:
87 FaultManager* const manager_;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070088
89 private:
90 DISALLOW_COPY_AND_ASSIGN(FaultHandler);
Dave Allisonb373e092014-02-20 16:06:36 -080091};
92
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010093class NullPointerHandler final : public FaultHandler {
Dave Allisonb373e092014-02-20 16:06:36 -080094 public:
Dave Allisonb373e092014-02-20 16:06:36 -080095 explicit NullPointerHandler(FaultManager* manager);
96
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010097 bool Action(int sig, siginfo_t* siginfo, void* context) override;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070098
Nicolas Geoffraye8e11272016-06-28 18:08:46 +010099 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 Chartierc751fdc2014-03-30 15:25:44 -0700107 private:
108 DISALLOW_COPY_AND_ASSIGN(NullPointerHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800109};
110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100111class SuspensionHandler final : public FaultHandler {
Dave Allisonb373e092014-02-20 16:06:36 -0800112 public:
Dave Allisonb373e092014-02-20 16:06:36 -0800113 explicit SuspensionHandler(FaultManager* manager);
114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100115 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(SuspensionHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800119};
120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100121class StackOverflowHandler final : public FaultHandler {
Dave Allisonb373e092014-02-20 16:06:36 -0800122 public:
Dave Allisonb373e092014-02-20 16:06:36 -0800123 explicit StackOverflowHandler(FaultManager* manager);
124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100125 bool Action(int sig, siginfo_t* siginfo, void* context) override;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700126
127 private:
128 DISALLOW_COPY_AND_ASSIGN(StackOverflowHandler);
Dave Allisonb373e092014-02-20 16:06:36 -0800129};
130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100131class JavaStackTraceHandler final : public FaultHandler {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700132 public:
133 explicit JavaStackTraceHandler(FaultManager* manager);
134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100135 bool Action(int sig, siginfo_t* siginfo, void* context) override NO_THREAD_SAFETY_ANALYSIS;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(JavaStackTraceHandler);
139};
140
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700141// Statically allocated so the the signal handler can Get access to it.
Dave Allisonb373e092014-02-20 16:06:36 -0800142extern FaultManager fault_manager;
143
144} // namespace art
145#endif // ART_RUNTIME_FAULT_HANDLER_H_
146