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