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