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 | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 19 | #include <setjmp.h> |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 20 | #include <sys/mman.h> |
| 21 | #include <sys/ucontext.h> |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 22 | #include "mirror/art_method.h" |
| 23 | #include "mirror/class.h" |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 24 | #include "sigchain.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 25 | #include "thread-inl.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 26 | #include "verify_object-inl.h" |
| 27 | |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 28 | // Note on nested signal support |
| 29 | // ----------------------------- |
| 30 | // |
| 31 | // Typically a signal handler should not need to deal with signals that occur within it. |
| 32 | // However, when a SIGSEGV occurs that is in generated code and is not one of the |
| 33 | // handled signals (implicit checks), we call a function to try to dump the stack |
| 34 | // to the log. This enhances the debugging experience but may have the side effect |
| 35 | // that it may not work. If the cause of the original SIGSEGV is a corrupted stack or other |
| 36 | // memory region, the stack backtrace code may run into trouble and may either crash |
| 37 | // or fail with an abort (SIGABRT). In either case we don't want that (new) signal to |
| 38 | // mask the original signal and thus prevent useful debug output from being presented. |
| 39 | // |
| 40 | // In order to handle this situation, before we call the stack tracer we do the following: |
| 41 | // |
| 42 | // 1. shutdown the fault manager so that we are talking to the real signal management |
| 43 | // functions rather than those in sigchain. |
| 44 | // 2. use pthread_sigmask to allow SIGSEGV and SIGABRT signals to be delivered to the |
| 45 | // thread running the signal handler. |
| 46 | // 3. set the handler for SIGSEGV and SIGABRT to a secondary signal handler. |
| 47 | // 4. save the thread's state to the TLS of the current thread using 'setjmp' |
| 48 | // |
| 49 | // We then call the stack tracer and one of two things may happen: |
| 50 | // a. it completes successfully |
| 51 | // b. it crashes and a signal is raised. |
| 52 | // |
| 53 | // In the former case, we fall through and everything is fine. In the latter case |
| 54 | // our secondary signal handler gets called in a signal context. This results in |
| 55 | // a call to FaultManager::HandledNestedSignal(), an archirecture specific function |
| 56 | // whose purpose is to call 'longjmp' on the jmp_buf saved in the TLS of the current |
| 57 | // thread. This results in a return with a non-zero value from 'setjmp'. We detect this |
| 58 | // and write something to the log to tell the user that it happened. |
| 59 | // |
| 60 | // Regardless of how we got there, we reach the code after the stack tracer and we |
| 61 | // restore the signal states to their original values, reinstate the fault manager (thus |
| 62 | // reestablishing the signal chain) and continue. |
| 63 | |
| 64 | // This is difficult to test with a runtime test. To invoke the nested signal code |
| 65 | // on any signal, uncomment the following line and run something that throws a |
| 66 | // NullPointerException. |
| 67 | // #define TEST_NESTED_SIGNAL |
| 68 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 69 | namespace art { |
| 70 | // Static fault manger object accessed by signal handler. |
| 71 | FaultManager fault_manager; |
| 72 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 73 | extern "C" { |
| 74 | void art_sigsegv_fault() { |
| 75 | // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART. |
| 76 | VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler."; |
| 77 | } |
| 78 | } |
| 79 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 80 | // Signal handler called on SIGSEGV. |
| 81 | static void art_fault_handler(int sig, siginfo_t* info, void* context) { |
| 82 | fault_manager.HandleFault(sig, info, context); |
| 83 | } |
| 84 | |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 85 | // Signal handler for dealing with a nested signal. |
| 86 | static void art_nested_signal_handler(int sig, siginfo_t* info, void* context) { |
| 87 | fault_manager.HandleNestedSignal(sig, info, context); |
| 88 | } |
| 89 | |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 90 | FaultManager::FaultManager() : initialized_(false) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 91 | sigaction(SIGSEGV, nullptr, &oldaction_); |
| 92 | } |
| 93 | |
| 94 | FaultManager::~FaultManager() { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 97 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 98 | void FaultManager::Init() { |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 99 | CHECK(!initialized_); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 100 | struct sigaction action; |
| 101 | action.sa_sigaction = art_fault_handler; |
| 102 | sigemptyset(&action.sa_mask); |
| 103 | action.sa_flags = SA_SIGINFO | SA_ONSTACK; |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 104 | #if !defined(__APPLE__) && !defined(__mips__) |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 105 | action.sa_restorer = nullptr; |
Narayan Kamath | 15245bc | 2014-03-14 12:53:43 +0000 | [diff] [blame] | 106 | #endif |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 107 | |
| 108 | // Set our signal handler now. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 109 | int e = sigaction(SIGSEGV, &action, &oldaction_); |
| 110 | if (e != 0) { |
| 111 | VLOG(signals) << "Failed to claim SEGV: " << strerror(errno); |
| 112 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 113 | // Make sure our signal handler is called before any user handlers. |
| 114 | ClaimSignalChain(SIGSEGV, &oldaction_); |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 115 | initialized_ = true; |
| 116 | } |
| 117 | |
| 118 | void FaultManager::Shutdown() { |
| 119 | if (initialized_) { |
| 120 | UnclaimSignalChain(SIGSEGV); |
| 121 | initialized_ = false; |
| 122 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 126 | // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...) |
| 127 | // |
| 128 | // If malloc calls abort, it will be holding its lock. |
| 129 | // If the handler tries to call malloc, it will deadlock. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 130 | |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 131 | VLOG(signals) << "Handling fault"; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 132 | if (IsInGeneratedCode(info, context, true)) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 133 | VLOG(signals) << "in generated code, looking for handler"; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 134 | for (const auto& handler : generated_code_handlers_) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 135 | VLOG(signals) << "invoking Action on handler " << handler; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 136 | if (handler->Action(sig, info, context)) { |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 137 | #ifdef TEST_NESTED_SIGNAL |
| 138 | // In test mode we want to fall through to stack trace handler |
| 139 | // on every signal (in reality this will cause a crash on the first |
| 140 | // signal). |
| 141 | break; |
| 142 | #else |
| 143 | // We have handled a signal so it's time to return from the |
| 144 | // signal handler to the appropriate place. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 145 | return; |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 146 | #endif |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 150 | |
| 151 | // We hit a signal we didn't handle. This might be something for which |
| 152 | // we can give more information about so call all registered handlers to see |
| 153 | // if it is. |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 154 | for (const auto& handler : other_handlers_) { |
| 155 | if (handler->Action(sig, info, context)) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | } |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 159 | |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 160 | // Set a breakpoint in this function to catch unhandled signals. |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 161 | art_sigsegv_fault(); |
| 162 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 163 | // 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] | 164 | InvokeUserSignalHandler(sig, info, context); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 167 | void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) { |
| 168 | if (generated_code) { |
| 169 | generated_code_handlers_.push_back(handler); |
| 170 | } else { |
| 171 | other_handlers_.push_back(handler); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void FaultManager::RemoveHandler(FaultHandler* handler) { |
| 176 | auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler); |
| 177 | if (it != generated_code_handlers_.end()) { |
| 178 | generated_code_handlers_.erase(it); |
| 179 | return; |
| 180 | } |
| 181 | auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler); |
| 182 | if (it2 != other_handlers_.end()) { |
| 183 | other_handlers_.erase(it); |
| 184 | return; |
| 185 | } |
| 186 | LOG(FATAL) << "Attempted to remove non existent handler " << handler; |
| 187 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 188 | |
| 189 | // This function is called within the signal handler. It checks that |
| 190 | // the mutator_lock is held (shared). No annotalysis is done. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 191 | bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 192 | // We can only be running Java code in the current thread if it |
| 193 | // is in Runnable state. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 194 | VLOG(signals) << "Checking for generated code"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 195 | Thread* thread = Thread::Current(); |
| 196 | if (thread == nullptr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 197 | VLOG(signals) << "no current thread"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | |
| 201 | ThreadState state = thread->GetState(); |
| 202 | if (state != kRunnable) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 203 | VLOG(signals) << "not runnable"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
| 207 | // Current thread is runnable. |
| 208 | // Make sure it has the mutator lock. |
| 209 | if (!Locks::mutator_lock_->IsSharedHeld(thread)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 210 | VLOG(signals) << "no lock"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 214 | mirror::ArtMethod* method_obj = 0; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 215 | uintptr_t return_pc = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 216 | uintptr_t sp = 0; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 217 | |
| 218 | // Get the architecture specific method address and return address. These |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 219 | // are in architecture specific files in arch/<arch>/fault_handler_<arch>. |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 220 | GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 221 | |
| 222 | // If we don't have a potential method, we're outta here. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 223 | VLOG(signals) << "potential method: " << method_obj; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 224 | if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 225 | VLOG(signals) << "no method"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | // Verify that the potential method is indeed a method. |
| 230 | // TODO: check the GC maps to make sure it's an object. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 231 | // 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] | 232 | // TODO: Method might be not a heap address, and GetClass could fault. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 233 | mirror::Class* cls = method_obj->GetClass<kVerifyNone>(); |
| 234 | if (cls == nullptr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 235 | VLOG(signals) << "not a class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | if (!IsAligned<kObjectAlignment>(cls)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 239 | VLOG(signals) << "not aligned"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | if (!VerifyClassClass(cls)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 245 | VLOG(signals) << "not a class class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
| 249 | // Now make sure the class is a mirror::ArtMethod. |
| 250 | if (!cls->IsArtMethodClass()) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 251 | VLOG(signals) << "not a method"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
| 255 | // We can be certain that this is a method now. Check if we have a GC map |
| 256 | // at the return PC address. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 257 | if (true || kIsDebugBuild) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 258 | 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] | 259 | const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 260 | uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 261 | VLOG(signals) << "pc offset: " << std::hex << sought_offset; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 262 | } |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 263 | uint32_t dexpc = method_obj->ToDexPc(return_pc, false); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 264 | VLOG(signals) << "dexpc: " << dexpc; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 265 | return !check_dex_pc || dexpc != DexFile::kDexNoIndex; |
| 266 | } |
| 267 | |
| 268 | FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // |
| 272 | // Null pointer fault handler |
| 273 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 274 | NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) { |
| 275 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // |
| 279 | // Suspension fault handler |
| 280 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 281 | SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) { |
| 282 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // |
| 286 | // Stack overflow fault handler |
| 287 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 288 | StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) { |
| 289 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 290 | } |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 291 | |
| 292 | // |
| 293 | // Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code. |
| 294 | // |
| 295 | JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) { |
| 296 | manager_->AddHandler(this, false); |
| 297 | } |
| 298 | |
| 299 | bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) { |
| 300 | // Make sure that we are in the generated code, but we may not have a dex pc. |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 301 | |
| 302 | #ifdef TEST_NESTED_SIGNAL |
| 303 | bool in_generated_code = true; |
| 304 | #else |
| 305 | bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false); |
| 306 | #endif |
| 307 | if (in_generated_code) { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 308 | LOG(ERROR) << "Dumping java stack trace for crash in generated code"; |
| 309 | mirror::ArtMethod* method = nullptr; |
| 310 | uintptr_t return_pc = 0; |
| 311 | uintptr_t sp = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 312 | Thread* self = Thread::Current(); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 313 | |
| 314 | // Shutdown the fault manager so that it will remove the signal chain for |
| 315 | // SIGSEGV and we call the real sigaction. |
| 316 | fault_manager.Shutdown(); |
| 317 | |
| 318 | // The action for SIGSEGV should be the default handler now. |
| 319 | |
| 320 | // Unblock the signals we allow so that they can be delivered in the signal handler. |
| 321 | sigset_t sigset; |
| 322 | sigemptyset(&sigset); |
| 323 | sigaddset(&sigset, SIGSEGV); |
| 324 | sigaddset(&sigset, SIGABRT); |
| 325 | pthread_sigmask(SIG_UNBLOCK, &sigset, nullptr); |
| 326 | |
| 327 | // If we get a signal in this code we want to invoke our nested signal |
| 328 | // handler. |
| 329 | struct sigaction action, oldsegvaction, oldabortaction; |
| 330 | action.sa_sigaction = art_nested_signal_handler; |
| 331 | |
| 332 | // Explictly mask out SIGSEGV and SIGABRT from the nested signal handler. This |
| 333 | // should be the default but we definitely don't want these happening in our |
| 334 | // nested signal handler. |
| 335 | sigemptyset(&action.sa_mask); |
| 336 | sigaddset(&action.sa_mask, SIGSEGV); |
| 337 | sigaddset(&action.sa_mask, SIGABRT); |
| 338 | |
| 339 | action.sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 340 | #if !defined(__APPLE__) && !defined(__mips__) |
| 341 | action.sa_restorer = nullptr; |
| 342 | #endif |
| 343 | |
| 344 | // Catch SIGSEGV and SIGABRT to invoke our nested handler |
| 345 | int e1 = sigaction(SIGSEGV, &action, &oldsegvaction); |
| 346 | int e2 = sigaction(SIGABRT, &action, &oldabortaction); |
| 347 | if (e1 != 0 || e2 != 0) { |
| 348 | LOG(ERROR) << "Unable to register nested signal handler - no stack trace possible"; |
| 349 | // If sigaction failed we have a serious problem. We cannot catch |
| 350 | // any failures in the stack tracer and it's likely to occur since |
| 351 | // the program state is bad. Therefore we don't even try to give |
| 352 | // a stack trace. |
| 353 | } else { |
| 354 | // Save the current state and try to dump the stack. If this causes a signal |
| 355 | // our nested signal handler will be invoked and this will longjmp to the saved |
| 356 | // state. |
| 357 | if (setjmp(*self->GetNestedSignalState()) == 0) { |
| 358 | manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp); |
| 359 | // Inside of generated code, sp[0] is the method, so sp is the frame. |
| 360 | StackReference<mirror::ArtMethod>* frame = |
| 361 | reinterpret_cast<StackReference<mirror::ArtMethod>*>(sp); |
| 362 | self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0. |
| 363 | #ifdef TEST_NESTED_SIGNAL |
| 364 | // To test the nested signal handler we raise a signal here. This will cause the |
| 365 | // nested signal handler to be called and perform a longjmp back to the setjmp |
| 366 | // above. |
| 367 | abort(); |
| 368 | #endif |
| 369 | self->DumpJavaStack(LOG(ERROR)); |
| 370 | } else { |
| 371 | LOG(ERROR) << "Stack trace aborted due to nested signal - original signal being reported"; |
| 372 | } |
| 373 | |
| 374 | // Restore the signal handlers. |
| 375 | sigaction(SIGSEGV, &oldsegvaction, nullptr); |
| 376 | sigaction(SIGABRT, &oldabortaction, nullptr); |
| 377 | } |
| 378 | |
| 379 | // Now put the fault manager back in place. |
| 380 | fault_manager.Init(); |
| 381 | |
| 382 | // And we're done. |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 383 | } |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame^] | 384 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 385 | return false; // Return false since we want to propagate the fault to the main signal handler. |
| 386 | } |
| 387 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 388 | } // namespace art |
| 389 | |