blob: f9f3e2576e1e7c6ba35c2faa1288446915af7ca0 [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#include "fault_handler.h"
18#include <sys/mman.h>
19#include <sys/ucontext.h>
20#include "base/macros.h"
21#include "globals.h"
22#include "base/logging.h"
23#include "base/hex_dump.h"
24#include "thread.h"
25#include "mirror/art_method-inl.h"
26#include "mirror/class-inl.h"
27#include "mirror/dex_cache.h"
28#include "mirror/object_array-inl.h"
29#include "mirror/object-inl.h"
30#include "object_utils.h"
31#include "scoped_thread_state_change.h"
32#include "verify_object-inl.h"
33
34namespace art {
35// Static fault manger object accessed by signal handler.
36FaultManager fault_manager;
37
38// Signal handler called on SIGSEGV.
39static void art_fault_handler(int sig, siginfo_t* info, void* context) {
40 fault_manager.HandleFault(sig, info, context);
41}
42
43FaultManager::FaultManager() {
44 sigaction(SIGSEGV, nullptr, &oldaction_);
45}
46
47FaultManager::~FaultManager() {
48 sigaction(SIGSEGV, &oldaction_, nullptr); // Restore old handler.
49}
50
51void FaultManager::Init() {
52 struct sigaction action;
53 action.sa_sigaction = art_fault_handler;
54 sigemptyset(&action.sa_mask);
55 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
Narayan Kamath15245bc2014-03-14 12:53:43 +000056#if !defined(__mips__)
Dave Allisonb373e092014-02-20 16:06:36 -080057 action.sa_restorer = nullptr;
Narayan Kamath15245bc2014-03-14 12:53:43 +000058#endif
Dave Allisonb373e092014-02-20 16:06:36 -080059 sigaction(SIGSEGV, &action, &oldaction_);
60}
61
62void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
63 bool handled = false;
64 if (IsInGeneratedCode(context)) {
65 for (auto& handler : handlers_) {
66 handled = handler->Action(sig, info, context);
67 if (handled) {
68 return;
69 }
70 }
71 }
72
73 if (!handled) {
74 LOG(INFO)<< "Caught unknown SIGSEGV in ART fault handler";
75 oldaction_.sa_sigaction(sig, info, context);
76 }
77}
78
79void FaultManager::AddHandler(FaultHandler* handler) {
80 handlers_.push_back(handler);
81}
82
83void FaultManager::RemoveHandler(FaultHandler* handler) {
84 for (Handlers::iterator i = handlers_.begin(); i != handlers_.end(); ++i) {
85 FaultHandler* h = *i;
86 if (h == handler) {
87 handlers_.erase(i);
88 return;
89 }
90 }
91}
92
93
94// This function is called within the signal handler. It checks that
95// the mutator_lock is held (shared). No annotalysis is done.
96bool FaultManager::IsInGeneratedCode(void *context) {
97 // We can only be running Java code in the current thread if it
98 // is in Runnable state.
99 Thread* thread = Thread::Current();
100 if (thread == nullptr) {
101 return false;
102 }
103
104 ThreadState state = thread->GetState();
105 if (state != kRunnable) {
106 return false;
107 }
108
109 // Current thread is runnable.
110 // Make sure it has the mutator lock.
111 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
112 return false;
113 }
114
115 uintptr_t potential_method = 0;
116 uintptr_t return_pc = 0;
117
118 // Get the architecture specific method address and return address. These
119 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.cc
120 GetMethodAndReturnPC(context, /*out*/potential_method, /*out*/return_pc);
121
122 // If we don't have a potential method, we're outta here.
123 if (potential_method == 0) {
124 return false;
125 }
126
127 // Verify that the potential method is indeed a method.
128 // TODO: check the GC maps to make sure it's an object.
129
130 mirror::Object* method_obj =
131 reinterpret_cast<mirror::Object*>(potential_method);
132
133 // Check that the class pointer inside the object is not null and is aligned.
134 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
135 if (cls == nullptr) {
136 return false;
137 }
138 if (!IsAligned<kObjectAlignment>(cls)) {
139 return false;
140 }
141
142
143 if (!VerifyClassClass(cls)) {
144 return false;
145 }
146
147 // Now make sure the class is a mirror::ArtMethod.
148 if (!cls->IsArtMethodClass()) {
149 return false;
150 }
151
152 // We can be certain that this is a method now. Check if we have a GC map
153 // at the return PC address.
154 mirror::ArtMethod* method =
155 reinterpret_cast<mirror::ArtMethod*>(potential_method);
156 return method->ToDexPc(return_pc, false) != DexFile::kDexNoIndex;
157}
158
159//
160// Null pointer fault handler
161//
162
163NullPointerHandler::NullPointerHandler(FaultManager* manager) {
164 manager->AddHandler(this);
165}
166
167//
168// Suspension fault handler
169//
170
171SuspensionHandler::SuspensionHandler(FaultManager* manager) {
172 manager->AddHandler(this);
173}
174
175//
176// Stack overflow fault handler
177//
178
179StackOverflowHandler::StackOverflowHandler(FaultManager* manager) {
180 manager->AddHandler(this);
181}
182} // namespace art
183