blob: fcb567e6fd8147cc8a6026e42e669710a980b12b [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;
Dave Allisonf9439142014-03-27 15:10:22 -070064 LOG(DEBUG) << "Handling fault";
Dave Allisonb373e092014-02-20 16:06:36 -080065 if (IsInGeneratedCode(context)) {
Dave Allisonf9439142014-03-27 15:10:22 -070066 LOG(DEBUG) << "in generated code, looking for handler";
Dave Allisonb373e092014-02-20 16:06:36 -080067 for (auto& handler : handlers_) {
Dave Allisonf9439142014-03-27 15:10:22 -070068 LOG(DEBUG) << "invoking Action on handler " << handler;
Dave Allisonb373e092014-02-20 16:06:36 -080069 handled = handler->Action(sig, info, context);
70 if (handled) {
71 return;
72 }
73 }
74 }
75
76 if (!handled) {
Dave Allisonf9439142014-03-27 15:10:22 -070077 LOG(ERROR)<< "Caught unknown SIGSEGV in ART fault handler";
Dave Allisonb373e092014-02-20 16:06:36 -080078 oldaction_.sa_sigaction(sig, info, context);
79 }
80}
81
82void FaultManager::AddHandler(FaultHandler* handler) {
83 handlers_.push_back(handler);
84}
85
86void FaultManager::RemoveHandler(FaultHandler* handler) {
87 for (Handlers::iterator i = handlers_.begin(); i != handlers_.end(); ++i) {
88 FaultHandler* h = *i;
89 if (h == handler) {
90 handlers_.erase(i);
91 return;
92 }
93 }
94}
95
96
97// This function is called within the signal handler. It checks that
98// the mutator_lock is held (shared). No annotalysis is done.
99bool FaultManager::IsInGeneratedCode(void *context) {
100 // We can only be running Java code in the current thread if it
101 // is in Runnable state.
Dave Allisonf9439142014-03-27 15:10:22 -0700102 LOG(DEBUG) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800103 Thread* thread = Thread::Current();
104 if (thread == nullptr) {
Dave Allisonf9439142014-03-27 15:10:22 -0700105 LOG(DEBUG) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800106 return false;
107 }
108
109 ThreadState state = thread->GetState();
110 if (state != kRunnable) {
Dave Allisonf9439142014-03-27 15:10:22 -0700111 LOG(DEBUG) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800112 return false;
113 }
114
115 // Current thread is runnable.
116 // Make sure it has the mutator lock.
117 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allisonf9439142014-03-27 15:10:22 -0700118 LOG(DEBUG) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800119 return false;
120 }
121
122 uintptr_t potential_method = 0;
123 uintptr_t return_pc = 0;
124
125 // Get the architecture specific method address and return address. These
126 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.cc
127 GetMethodAndReturnPC(context, /*out*/potential_method, /*out*/return_pc);
128
129 // If we don't have a potential method, we're outta here.
Dave Allisonf9439142014-03-27 15:10:22 -0700130 LOG(DEBUG) << "potential method: " << potential_method;
Dave Allisonb373e092014-02-20 16:06:36 -0800131 if (potential_method == 0) {
Dave Allisonf9439142014-03-27 15:10:22 -0700132 LOG(DEBUG) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800133 return false;
134 }
135
136 // Verify that the potential method is indeed a method.
137 // TODO: check the GC maps to make sure it's an object.
138
139 mirror::Object* method_obj =
140 reinterpret_cast<mirror::Object*>(potential_method);
141
142 // Check that the class pointer inside the object is not null and is aligned.
143 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
144 if (cls == nullptr) {
Dave Allisonf9439142014-03-27 15:10:22 -0700145 LOG(DEBUG) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800146 return false;
147 }
148 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allisonf9439142014-03-27 15:10:22 -0700149 LOG(DEBUG) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800150 return false;
151 }
152
153
154 if (!VerifyClassClass(cls)) {
Dave Allisonf9439142014-03-27 15:10:22 -0700155 LOG(DEBUG) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800156 return false;
157 }
158
159 // Now make sure the class is a mirror::ArtMethod.
160 if (!cls->IsArtMethodClass()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700161 LOG(DEBUG) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800162 return false;
163 }
164
165 // We can be certain that this is a method now. Check if we have a GC map
166 // at the return PC address.
167 mirror::ArtMethod* method =
168 reinterpret_cast<mirror::ArtMethod*>(potential_method);
Dave Allisonf9439142014-03-27 15:10:22 -0700169 if (true || kIsDebugBuild) {
170 LOG(DEBUG) << "looking for dex pc for return pc " << std::hex << return_pc;
171 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method);
172 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
173 LOG(DEBUG) << "pc offset: " << std::hex << sought_offset;
174 }
175 uint32_t dexpc = method->ToDexPc(return_pc, false);
176 LOG(DEBUG) << "dexpc: " << dexpc;
177 return dexpc != DexFile::kDexNoIndex;
Dave Allisonb373e092014-02-20 16:06:36 -0800178}
179
180//
181// Null pointer fault handler
182//
183
184NullPointerHandler::NullPointerHandler(FaultManager* manager) {
185 manager->AddHandler(this);
186}
187
188//
189// Suspension fault handler
190//
191
192SuspensionHandler::SuspensionHandler(FaultManager* manager) {
193 manager->AddHandler(this);
194}
195
196//
197// Stack overflow fault handler
198//
199
200StackOverflowHandler::StackOverflowHandler(FaultManager* manager) {
201 manager->AddHandler(this);
202}
203} // namespace art
204