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