blob: 1304b04b9043253f5595215ce47a30436968f557 [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) {
Dave Allisonf9439142014-03-27 15:10:22 -070063 LOG(DEBUG) << "Handling fault";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070064 if (IsInGeneratedCode(context, true)) {
Dave Allisonf9439142014-03-27 15:10:22 -070065 LOG(DEBUG) << "in generated code, looking for handler";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070066 for (const auto& handler : generated_code_handlers_) {
Dave Allisonf9439142014-03-27 15:10:22 -070067 LOG(DEBUG) << "invoking Action on handler " << handler;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070068 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080069 return;
70 }
71 }
72 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070073 for (const auto& handler : other_handlers_) {
74 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080075 return;
76 }
77 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070078 LOG(ERROR)<< "Caught unknown SIGSEGV in ART fault handler";
79 oldaction_.sa_sigaction(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -080080}
81
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070082void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
83 if (generated_code) {
84 generated_code_handlers_.push_back(handler);
85 } else {
86 other_handlers_.push_back(handler);
87 }
88}
89
90void FaultManager::RemoveHandler(FaultHandler* handler) {
91 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
92 if (it != generated_code_handlers_.end()) {
93 generated_code_handlers_.erase(it);
94 return;
95 }
96 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
97 if (it2 != other_handlers_.end()) {
98 other_handlers_.erase(it);
99 return;
100 }
101 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
102}
Dave Allisonb373e092014-02-20 16:06:36 -0800103
104// This function is called within the signal handler. It checks that
105// the mutator_lock is held (shared). No annotalysis is done.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700106bool FaultManager::IsInGeneratedCode(void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800107 // We can only be running Java code in the current thread if it
108 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700109 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800110 Thread* thread = Thread::Current();
111 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700112 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800113 return false;
114 }
115
116 ThreadState state = thread->GetState();
117 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700118 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800119 return false;
120 }
121
122 // Current thread is runnable.
123 // Make sure it has the mutator lock.
124 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700125 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800126 return false;
127 }
128
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700129 mirror::ArtMethod* method_obj = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800130 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700131 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800132
133 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700134 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
135 GetMethodAndReturnPCAndSP(context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800136
137 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700138 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700139 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700140 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800141 return false;
142 }
143
144 // Verify that the potential method is indeed a method.
145 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800146 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700147 // TODO: Method might be not a heap address, and GetClass could fault.
Dave Allisonb373e092014-02-20 16:06:36 -0800148 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
149 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700150 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800151 return false;
152 }
153 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700154 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800155 return false;
156 }
157
158
159 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700160 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800161 return false;
162 }
163
164 // Now make sure the class is a mirror::ArtMethod.
165 if (!cls->IsArtMethodClass()) {
Dave Allison5cd33752014-04-15 15:57:58 -0700166 VLOG(signals) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800167 return false;
168 }
169
170 // We can be certain that this is a method now. Check if we have a GC map
171 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700172 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700173 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700174 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj);
Dave Allisonf9439142014-03-27 15:10:22 -0700175 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
Dave Allison5cd33752014-04-15 15:57:58 -0700176 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700177 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700178 uint32_t dexpc = method_obj->ToDexPc(return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700179 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700180 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
181}
182
183FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800184}
185
186//
187// Null pointer fault handler
188//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700189NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
190 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800191}
192
193//
194// Suspension fault handler
195//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700196SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
197 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800198}
199
200//
201// Stack overflow fault handler
202//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700203StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
204 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800205}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700206
207//
208// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
209//
210JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
211 manager_->AddHandler(this, false);
212}
213
214bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) {
215 // Make sure that we are in the generated code, but we may not have a dex pc.
216 if (manager_->IsInGeneratedCode(context, false)) {
217 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
218 mirror::ArtMethod* method = nullptr;
219 uintptr_t return_pc = 0;
220 uintptr_t sp = 0;
221 manager_->GetMethodAndReturnPCAndSP(context, &method, &return_pc, &sp);
222 Thread* self = Thread::Current();
223 // Inside of generated code, sp[0] is the method, so sp is the frame.
224 mirror::ArtMethod** frame = reinterpret_cast<mirror::ArtMethod**>(sp);
225 self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0.
226 self->DumpJavaStack(LOG(ERROR));
227 }
228 return false; // Return false since we want to propagate the fault to the main signal handler.
229}
230
Dave Allisonb373e092014-02-20 16:06:36 -0800231} // namespace art
232