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 | |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 98 | static void SetUpArtAction(struct sigaction* action) { |
| 99 | action->sa_sigaction = art_fault_handler; |
| 100 | sigemptyset(&action->sa_mask); |
| 101 | action->sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 102 | #if !defined(__APPLE__) && !defined(__mips__) |
| 103 | action->sa_restorer = nullptr; |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | void FaultManager::EnsureArtActionInFrontOfSignalChain() { |
| 108 | if (initialized_) { |
| 109 | struct sigaction action; |
| 110 | SetUpArtAction(&action); |
| 111 | EnsureFrontOfChain(SIGSEGV, &action); |
| 112 | } else { |
| 113 | LOG(WARNING) << "Can't call " << __FUNCTION__ << " due to unitialized fault manager"; |
| 114 | } |
| 115 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 116 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 117 | void FaultManager::Init() { |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 118 | CHECK(!initialized_); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 119 | struct sigaction action; |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 120 | SetUpArtAction(&action); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 121 | |
| 122 | // Set our signal handler now. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 123 | int e = sigaction(SIGSEGV, &action, &oldaction_); |
| 124 | if (e != 0) { |
| 125 | VLOG(signals) << "Failed to claim SEGV: " << strerror(errno); |
| 126 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 127 | // Make sure our signal handler is called before any user handlers. |
| 128 | ClaimSignalChain(SIGSEGV, &oldaction_); |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 129 | initialized_ = true; |
| 130 | } |
| 131 | |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 132 | void FaultManager::Release() { |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 133 | if (initialized_) { |
| 134 | UnclaimSignalChain(SIGSEGV); |
| 135 | initialized_ = false; |
| 136 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 139 | void FaultManager::Shutdown() { |
| 140 | if (initialized_) { |
| 141 | Release(); |
| 142 | |
| 143 | // Free all handlers. |
| 144 | STLDeleteElements(&generated_code_handlers_); |
| 145 | STLDeleteElements(&other_handlers_); |
| 146 | } |
| 147 | } |
| 148 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 149 | void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 150 | // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...) |
| 151 | // |
| 152 | // If malloc calls abort, it will be holding its lock. |
| 153 | // If the handler tries to call malloc, it will deadlock. |
| 154 | VLOG(signals) << "Handling fault"; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 155 | if (IsInGeneratedCode(info, context, true)) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 156 | VLOG(signals) << "in generated code, looking for handler"; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 157 | for (const auto& handler : generated_code_handlers_) { |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 158 | VLOG(signals) << "invoking Action on handler " << handler; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 159 | if (handler->Action(sig, info, context)) { |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 160 | #ifdef TEST_NESTED_SIGNAL |
| 161 | // In test mode we want to fall through to stack trace handler |
| 162 | // on every signal (in reality this will cause a crash on the first |
| 163 | // signal). |
| 164 | break; |
| 165 | #else |
| 166 | // We have handled a signal so it's time to return from the |
| 167 | // signal handler to the appropriate place. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 168 | return; |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 169 | #endif |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 173 | |
| 174 | // We hit a signal we didn't handle. This might be something for which |
| 175 | // we can give more information about so call all registered handlers to see |
| 176 | // if it is. |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 177 | |
| 178 | Thread* self = Thread::Current(); |
| 179 | |
Christopher Ferris | 5e2b874 | 2014-11-18 15:50:47 -0800 | [diff] [blame] | 180 | // If ART is not running, or the thread is not attached to ART pass the |
| 181 | // signal on to the next handler in the chain. |
| 182 | if (self == nullptr || Runtime::Current() == nullptr || !Runtime::Current()->IsStarted()) { |
| 183 | InvokeUserSignalHandler(sig, info, context); |
| 184 | return; |
| 185 | } |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 186 | // Now set up the nested signal handler. |
| 187 | |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 188 | // TODO: add SIGSEGV back to the nested signals when we can handle running out stack gracefully. |
| 189 | static const int handled_nested_signals[] = {SIGABRT}; |
| 190 | constexpr size_t num_handled_nested_signals = arraysize(handled_nested_signals); |
| 191 | |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 192 | // 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] | 193 | // SIGSEGV and we call the real sigaction. |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 194 | fault_manager.Release(); |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 195 | |
| 196 | // The action for SIGSEGV should be the default handler now. |
| 197 | |
| 198 | // Unblock the signals we allow so that they can be delivered in the signal handler. |
| 199 | sigset_t sigset; |
| 200 | sigemptyset(&sigset); |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 201 | for (int signal : handled_nested_signals) { |
| 202 | sigaddset(&sigset, signal); |
| 203 | } |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 204 | pthread_sigmask(SIG_UNBLOCK, &sigset, nullptr); |
| 205 | |
| 206 | // If we get a signal in this code we want to invoke our nested signal |
| 207 | // handler. |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 208 | struct sigaction action; |
| 209 | struct sigaction oldactions[num_handled_nested_signals]; |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 210 | action.sa_sigaction = art_nested_signal_handler; |
| 211 | |
| 212 | // Explicitly mask out SIGSEGV and SIGABRT from the nested signal handler. This |
| 213 | // should be the default but we definitely don't want these happening in our |
| 214 | // nested signal handler. |
| 215 | sigemptyset(&action.sa_mask); |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 216 | for (int signal : handled_nested_signals) { |
| 217 | sigaddset(&action.sa_mask, signal); |
| 218 | } |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 219 | |
| 220 | action.sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 221 | #if !defined(__APPLE__) && !defined(__mips__) |
| 222 | action.sa_restorer = nullptr; |
| 223 | #endif |
| 224 | |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 225 | // Catch handled signals to invoke our nested handler. |
| 226 | bool success = true; |
| 227 | for (size_t i = 0; i < num_handled_nested_signals; ++i) { |
| 228 | success = sigaction(handled_nested_signals[i], &action, &oldactions[i]) == 0; |
| 229 | if (!success) { |
| 230 | PLOG(ERROR) << "Unable to set up nested signal handler"; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | if (success) { |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 235 | // Save the current state and call the handlers. If anything causes a signal |
| 236 | // our nested signal handler will be invoked and this will longjmp to the saved |
| 237 | // state. |
| 238 | if (setjmp(*self->GetNestedSignalState()) == 0) { |
| 239 | for (const auto& handler : other_handlers_) { |
| 240 | if (handler->Action(sig, info, context)) { |
Dave Allison | 8be44cf | 2014-09-04 14:33:42 -0700 | [diff] [blame] | 241 | // Restore the signal handlers, reinit the fault manager and return. Signal was |
| 242 | // handled. |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 243 | for (size_t i = 0; i < num_handled_nested_signals; ++i) { |
| 244 | success = sigaction(handled_nested_signals[i], &oldactions[i], nullptr) == 0; |
| 245 | if (!success) { |
| 246 | PLOG(ERROR) << "Unable to restore signal handler"; |
| 247 | } |
| 248 | } |
Dave Allison | 8be44cf | 2014-09-04 14:33:42 -0700 | [diff] [blame] | 249 | fault_manager.Init(); |
| 250 | return; |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } else { |
| 254 | LOG(ERROR) << "Nested signal detected - original signal being reported"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 255 | } |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 256 | |
| 257 | // Restore the signal handlers. |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 258 | for (size_t i = 0; i < num_handled_nested_signals; ++i) { |
| 259 | success = sigaction(handled_nested_signals[i], &oldactions[i], nullptr) == 0; |
| 260 | if (!success) { |
| 261 | PLOG(ERROR) << "Unable to restore signal handler"; |
| 262 | } |
| 263 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 264 | } |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 265 | |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 266 | // Now put the fault manager back in place. |
| 267 | fault_manager.Init(); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 268 | |
Dave Allison | 8be44cf | 2014-09-04 14:33:42 -0700 | [diff] [blame] | 269 | // Set a breakpoint in this function to catch unhandled signals. |
| 270 | art_sigsegv_fault(); |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 271 | |
Dave Allison | 8be44cf | 2014-09-04 14:33:42 -0700 | [diff] [blame] | 272 | // Pass this on to the next handler in the chain, or the default if none. |
| 273 | InvokeUserSignalHandler(sig, info, context); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 276 | void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) { |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 277 | DCHECK(initialized_); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 278 | if (generated_code) { |
| 279 | generated_code_handlers_.push_back(handler); |
| 280 | } else { |
| 281 | other_handlers_.push_back(handler); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void FaultManager::RemoveHandler(FaultHandler* handler) { |
| 286 | auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler); |
| 287 | if (it != generated_code_handlers_.end()) { |
| 288 | generated_code_handlers_.erase(it); |
| 289 | return; |
| 290 | } |
| 291 | auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler); |
| 292 | if (it2 != other_handlers_.end()) { |
| 293 | other_handlers_.erase(it); |
| 294 | return; |
| 295 | } |
| 296 | LOG(FATAL) << "Attempted to remove non existent handler " << handler; |
| 297 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 298 | |
| 299 | // This function is called within the signal handler. It checks that |
| 300 | // the mutator_lock is held (shared). No annotalysis is done. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 301 | bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 302 | // We can only be running Java code in the current thread if it |
| 303 | // is in Runnable state. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 304 | VLOG(signals) << "Checking for generated code"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 305 | Thread* thread = Thread::Current(); |
| 306 | if (thread == nullptr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 307 | VLOG(signals) << "no current thread"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | |
| 311 | ThreadState state = thread->GetState(); |
| 312 | if (state != kRunnable) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 313 | VLOG(signals) << "not runnable"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 314 | return false; |
| 315 | } |
| 316 | |
| 317 | // Current thread is runnable. |
| 318 | // Make sure it has the mutator lock. |
| 319 | if (!Locks::mutator_lock_->IsSharedHeld(thread)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 320 | VLOG(signals) << "no lock"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 321 | return false; |
| 322 | } |
| 323 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 324 | mirror::ArtMethod* method_obj = 0; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 325 | uintptr_t return_pc = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 326 | uintptr_t sp = 0; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 327 | |
| 328 | // Get the architecture specific method address and return address. These |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 329 | // are in architecture specific files in arch/<arch>/fault_handler_<arch>. |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 330 | GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 331 | |
| 332 | // If we don't have a potential method, we're outta here. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 333 | VLOG(signals) << "potential method: " << method_obj; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 334 | if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 335 | VLOG(signals) << "no method"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 336 | return false; |
| 337 | } |
| 338 | |
| 339 | // Verify that the potential method is indeed a method. |
| 340 | // TODO: check the GC maps to make sure it's an object. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 341 | // 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] | 342 | // TODO: Method might be not a heap address, and GetClass could fault. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 343 | // No read barrier because method_obj may not be a real object. |
| 344 | mirror::Class* cls = method_obj->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 345 | if (cls == nullptr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 346 | VLOG(signals) << "not a class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | if (!IsAligned<kObjectAlignment>(cls)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 350 | VLOG(signals) << "not aligned"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | if (!VerifyClassClass(cls)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 356 | VLOG(signals) << "not a class class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
| 360 | // Now make sure the class is a mirror::ArtMethod. |
| 361 | if (!cls->IsArtMethodClass()) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 362 | VLOG(signals) << "not a method"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | |
| 366 | // We can be certain that this is a method now. Check if we have a GC map |
| 367 | // at the return PC address. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 368 | if (true || kIsDebugBuild) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 369 | VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc; |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 370 | const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj, |
| 371 | sizeof(void*)); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 372 | uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 373 | VLOG(signals) << "pc offset: " << std::hex << sought_offset; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 374 | } |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 375 | uint32_t dexpc = method_obj->ToDexPc(return_pc, false); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 376 | VLOG(signals) << "dexpc: " << dexpc; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 377 | return !check_dex_pc || dexpc != DexFile::kDexNoIndex; |
| 378 | } |
| 379 | |
| 380 | FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // |
| 384 | // Null pointer fault handler |
| 385 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 386 | NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) { |
| 387 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // |
| 391 | // Suspension fault handler |
| 392 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 393 | SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) { |
| 394 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // |
| 398 | // Stack overflow fault handler |
| 399 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 400 | StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) { |
| 401 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 402 | } |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 403 | |
| 404 | // |
| 405 | // Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code. |
| 406 | // |
| 407 | JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) { |
| 408 | manager_->AddHandler(this, false); |
| 409 | } |
| 410 | |
| 411 | bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) { |
| 412 | // Make sure that we are in the generated code, but we may not have a dex pc. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 413 | UNUSED(sig); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 414 | #ifdef TEST_NESTED_SIGNAL |
| 415 | bool in_generated_code = true; |
| 416 | #else |
| 417 | bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false); |
| 418 | #endif |
| 419 | if (in_generated_code) { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 420 | LOG(ERROR) << "Dumping java stack trace for crash in generated code"; |
| 421 | mirror::ArtMethod* method = nullptr; |
| 422 | uintptr_t return_pc = 0; |
| 423 | uintptr_t sp = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 424 | Thread* self = Thread::Current(); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 425 | |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 426 | manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp); |
| 427 | // Inside of generated code, sp[0] is the method, so sp is the frame. |
| 428 | StackReference<mirror::ArtMethod>* frame = |
| 429 | reinterpret_cast<StackReference<mirror::ArtMethod>*>(sp); |
Ian Rogers | 1d8cdbc | 2014-09-22 22:51:09 -0700 | [diff] [blame] | 430 | self->SetTopOfStack(frame); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 431 | #ifdef TEST_NESTED_SIGNAL |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 432 | // To test the nested signal handler we raise a signal here. This will cause the |
| 433 | // nested signal handler to be called and perform a longjmp back to the setjmp |
| 434 | // above. |
| 435 | abort(); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 436 | #endif |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 437 | self->DumpJavaStack(LOG(ERROR)); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 438 | } |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 439 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 440 | return false; // Return false since we want to propagate the fault to the main signal handler. |
| 441 | } |
| 442 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 443 | } // namespace art |