fault_handler.cc revision b0f05b9654eb005bc8c8e15f615a7f5a312f640c
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#include "fault_handler.h" 18 19#include <sys/mman.h> 20#include <sys/ucontext.h> 21#include "mirror/art_method.h" 22#include "mirror/class.h" 23#include "sigchain.h" 24#include "thread-inl.h" 25#include "verify_object-inl.h" 26 27namespace art { 28// Static fault manger object accessed by signal handler. 29FaultManager fault_manager; 30 31extern "C" { 32void art_sigsegv_fault() { 33 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART. 34 VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler."; 35} 36} 37 38// Signal handler called on SIGSEGV. 39static void art_fault_handler(int sig, siginfo_t* info, void* context) { 40 // std::cout << "handling fault in ART handler\n"; 41 fault_manager.HandleFault(sig, info, context); 42} 43 44FaultManager::FaultManager() { 45 sigaction(SIGSEGV, nullptr, &oldaction_); 46} 47 48FaultManager::~FaultManager() { 49} 50 51 52void FaultManager::Init() { 53 struct sigaction action; 54 action.sa_sigaction = art_fault_handler; 55 sigemptyset(&action.sa_mask); 56 action.sa_flags = SA_SIGINFO | SA_ONSTACK; 57#if !defined(__APPLE__) && !defined(__mips__) 58 action.sa_restorer = nullptr; 59#endif 60 61 // Set our signal handler now. 62 int e = sigaction(SIGSEGV, &action, &oldaction_); 63 if (e != 0) { 64 VLOG(signals) << "Failed to claim SEGV: " << strerror(errno); 65 } 66 // Make sure our signal handler is called before any user handlers. 67 ClaimSignalChain(SIGSEGV, &oldaction_); 68} 69 70void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) { 71 // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...) 72 // 73 // If malloc calls abort, it will be holding its lock. 74 // If the handler tries to call malloc, it will deadlock. 75 76 // Also, there is only an 8K stack available here to logging can cause memory 77 // overwrite issues if you are unlucky. If you want to enable logging and 78 // are getting crashes, allocate more space for the alternate signal stack. 79 80 VLOG(signals) << "Handling fault"; 81 if (IsInGeneratedCode(info, context, true)) { 82 VLOG(signals) << "in generated code, looking for handler"; 83 for (const auto& handler : generated_code_handlers_) { 84 VLOG(signals) << "invoking Action on handler " << handler; 85 if (handler->Action(sig, info, context)) { 86 return; 87 } 88 } 89 } 90 for (const auto& handler : other_handlers_) { 91 if (handler->Action(sig, info, context)) { 92 return; 93 } 94 } 95 96 art_sigsegv_fault(); 97 98 // Pass this on to the next handler in the chain, or the default if none. 99 InvokeUserSignalHandler(sig, info, context); 100} 101 102void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) { 103 if (generated_code) { 104 generated_code_handlers_.push_back(handler); 105 } else { 106 other_handlers_.push_back(handler); 107 } 108} 109 110void FaultManager::RemoveHandler(FaultHandler* handler) { 111 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler); 112 if (it != generated_code_handlers_.end()) { 113 generated_code_handlers_.erase(it); 114 return; 115 } 116 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler); 117 if (it2 != other_handlers_.end()) { 118 other_handlers_.erase(it); 119 return; 120 } 121 LOG(FATAL) << "Attempted to remove non existent handler " << handler; 122} 123 124// This function is called within the signal handler. It checks that 125// the mutator_lock is held (shared). No annotalysis is done. 126bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) { 127 // We can only be running Java code in the current thread if it 128 // is in Runnable state. 129 VLOG(signals) << "Checking for generated code"; 130 Thread* thread = Thread::Current(); 131 if (thread == nullptr) { 132 VLOG(signals) << "no current thread"; 133 return false; 134 } 135 136 ThreadState state = thread->GetState(); 137 if (state != kRunnable) { 138 VLOG(signals) << "not runnable"; 139 return false; 140 } 141 142 // Current thread is runnable. 143 // Make sure it has the mutator lock. 144 if (!Locks::mutator_lock_->IsSharedHeld(thread)) { 145 VLOG(signals) << "no lock"; 146 return false; 147 } 148 149 mirror::ArtMethod* method_obj = 0; 150 uintptr_t return_pc = 0; 151 uintptr_t sp = 0; 152 153 // Get the architecture specific method address and return address. These 154 // are in architecture specific files in arch/<arch>/fault_handler_<arch>. 155 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp); 156 157 // If we don't have a potential method, we're outta here. 158 VLOG(signals) << "potential method: " << method_obj; 159 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) { 160 VLOG(signals) << "no method"; 161 return false; 162 } 163 164 // Verify that the potential method is indeed a method. 165 // TODO: check the GC maps to make sure it's an object. 166 // Check that the class pointer inside the object is not null and is aligned. 167 // TODO: Method might be not a heap address, and GetClass could fault. 168 mirror::Class* cls = method_obj->GetClass<kVerifyNone>(); 169 if (cls == nullptr) { 170 VLOG(signals) << "not a class"; 171 return false; 172 } 173 if (!IsAligned<kObjectAlignment>(cls)) { 174 VLOG(signals) << "not aligned"; 175 return false; 176 } 177 178 179 if (!VerifyClassClass(cls)) { 180 VLOG(signals) << "not a class class"; 181 return false; 182 } 183 184 // Now make sure the class is a mirror::ArtMethod. 185 if (!cls->IsArtMethodClass()) { 186 VLOG(signals) << "not a method"; 187 return false; 188 } 189 190 // We can be certain that this is a method now. Check if we have a GC map 191 // at the return PC address. 192 if (true || kIsDebugBuild) { 193 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc; 194 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj); 195 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code); 196 VLOG(signals) << "pc offset: " << std::hex << sought_offset; 197 } 198 uint32_t dexpc = method_obj->ToDexPc(return_pc, false); 199 VLOG(signals) << "dexpc: " << dexpc; 200 return !check_dex_pc || dexpc != DexFile::kDexNoIndex; 201} 202 203FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) { 204} 205 206// 207// Null pointer fault handler 208// 209NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) { 210 manager_->AddHandler(this, true); 211} 212 213// 214// Suspension fault handler 215// 216SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) { 217 manager_->AddHandler(this, true); 218} 219 220// 221// Stack overflow fault handler 222// 223StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) { 224 manager_->AddHandler(this, true); 225} 226 227// 228// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code. 229// 230JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) { 231 manager_->AddHandler(this, false); 232} 233 234bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) { 235 // Make sure that we are in the generated code, but we may not have a dex pc. 236 if (manager_->IsInGeneratedCode(siginfo, context, false)) { 237 LOG(ERROR) << "Dumping java stack trace for crash in generated code"; 238 mirror::ArtMethod* method = nullptr; 239 uintptr_t return_pc = 0; 240 uintptr_t sp = 0; 241 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp); 242 Thread* self = Thread::Current(); 243 // Inside of generated code, sp[0] is the method, so sp is the frame. 244 StackReference<mirror::ArtMethod>* frame = 245 reinterpret_cast<StackReference<mirror::ArtMethod>*>(sp); 246 self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0. 247 self->DumpJavaStack(LOG(ERROR)); 248 } 249 return false; // Return false since we want to propagate the fault to the main signal handler. 250} 251 252} // namespace art 253 254