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