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