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" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 26 | #include "oat_quick_method_header.h" |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 27 | #include "sigchain.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 28 | #include "thread-inl.h" |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 29 | #include "verify_object-inl.h" |
| 30 | |
| 31 | namespace art { |
| 32 | // Static fault manger object accessed by signal handler. |
| 33 | FaultManager fault_manager; |
| 34 | |
Narayan Kamath | c37769b | 2015-06-17 09:49:40 +0100 | [diff] [blame] | 35 | extern "C" __attribute__((visibility("default"))) void art_sigsegv_fault() { |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 36 | // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART. |
| 37 | VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler."; |
| 38 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 39 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 40 | // Signal handler called on SIGSEGV. |
| 41 | static void art_fault_handler(int sig, siginfo_t* info, void* context) { |
| 42 | fault_manager.HandleFault(sig, info, context); |
| 43 | } |
| 44 | |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 45 | FaultManager::FaultManager() : initialized_(false) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 46 | sigaction(SIGSEGV, nullptr, &oldaction_); |
| 47 | } |
| 48 | |
| 49 | FaultManager::~FaultManager() { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 52 | static void SetUpArtAction(struct sigaction* action) { |
| 53 | action->sa_sigaction = art_fault_handler; |
| 54 | sigemptyset(&action->sa_mask); |
| 55 | action->sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 56 | #if !defined(__APPLE__) && !defined(__mips__) |
| 57 | action->sa_restorer = nullptr; |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | void FaultManager::EnsureArtActionInFrontOfSignalChain() { |
| 62 | if (initialized_) { |
| 63 | struct sigaction action; |
| 64 | SetUpArtAction(&action); |
| 65 | EnsureFrontOfChain(SIGSEGV, &action); |
| 66 | } else { |
| 67 | LOG(WARNING) << "Can't call " << __FUNCTION__ << " due to unitialized fault manager"; |
| 68 | } |
| 69 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 70 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 71 | void FaultManager::Init() { |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 72 | CHECK(!initialized_); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 73 | struct sigaction action; |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 74 | SetUpArtAction(&action); |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 75 | |
| 76 | // Set our signal handler now. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 77 | int e = sigaction(SIGSEGV, &action, &oldaction_); |
| 78 | if (e != 0) { |
| 79 | VLOG(signals) << "Failed to claim SEGV: " << strerror(errno); |
| 80 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 81 | // Make sure our signal handler is called before any user handlers. |
| 82 | ClaimSignalChain(SIGSEGV, &oldaction_); |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 83 | initialized_ = true; |
| 84 | } |
| 85 | |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 86 | void FaultManager::Release() { |
Dave Allison | 1f8ef6f | 2014-08-20 17:38:41 -0700 | [diff] [blame] | 87 | if (initialized_) { |
| 88 | UnclaimSignalChain(SIGSEGV); |
| 89 | initialized_ = false; |
| 90 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 93 | void FaultManager::Shutdown() { |
| 94 | if (initialized_) { |
| 95 | Release(); |
| 96 | |
| 97 | // Free all handlers. |
| 98 | STLDeleteElements(&generated_code_handlers_); |
| 99 | STLDeleteElements(&other_handlers_); |
| 100 | } |
| 101 | } |
| 102 | |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 103 | bool FaultManager::HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context) { |
Andreas Gampe | d14b73d | 2016-04-18 17:15:42 -0700 | [diff] [blame] | 104 | if (other_handlers_.empty()) { |
| 105 | return false; |
| 106 | } |
| 107 | |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 108 | Thread* self = Thread::Current(); |
| 109 | |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 110 | DCHECK(self != nullptr); |
| 111 | DCHECK(Runtime::Current() != nullptr); |
| 112 | DCHECK(Runtime::Current()->IsStarted()); |
Josh Gao | efd20cb | 2017-02-28 16:53:59 -0800 | [diff] [blame] | 113 | for (const auto& handler : other_handlers_) { |
| 114 | if (handler->Action(sig, info, context)) { |
| 115 | return true; |
Ian Rogers | ad11e7a | 2014-11-11 16:55:11 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
Josh Gao | efd20cb | 2017-02-28 16:53:59 -0800 | [diff] [blame] | 121 | class ScopedSignalUnblocker { |
| 122 | public: |
| 123 | explicit ScopedSignalUnblocker(const std::initializer_list<int>& signals) { |
| 124 | sigset_t new_mask; |
| 125 | sigemptyset(&new_mask); |
| 126 | for (int signal : signals) { |
| 127 | sigaddset(&new_mask, signal); |
| 128 | } |
| 129 | if (sigprocmask(SIG_UNBLOCK, &new_mask, &previous_mask_) != 0) { |
| 130 | PLOG(FATAL) << "failed to unblock signals"; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | ~ScopedSignalUnblocker() { |
| 135 | if (sigprocmask(SIG_SETMASK, &previous_mask_, nullptr) != 0) { |
| 136 | PLOG(FATAL) << "failed to unblock signals"; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | sigset_t previous_mask_; |
| 142 | }; |
| 143 | |
| 144 | class ScopedHandlingSignalSetter { |
| 145 | public: |
| 146 | explicit ScopedHandlingSignalSetter(Thread* thread) : thread_(thread) { |
| 147 | CHECK(!thread->HandlingSignal()); |
| 148 | thread_->SetHandlingSignal(true); |
| 149 | } |
| 150 | |
| 151 | ~ScopedHandlingSignalSetter() { |
| 152 | CHECK(thread_->HandlingSignal()); |
| 153 | thread_->SetHandlingSignal(false); |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | Thread* thread_; |
| 158 | }; |
| 159 | |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 160 | void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) { |
| 161 | // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...) |
| 162 | // |
| 163 | // If malloc calls abort, it will be holding its lock. |
| 164 | // If the handler tries to call malloc, it will deadlock. |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 165 | |
Josh Gao | efd20cb | 2017-02-28 16:53:59 -0800 | [diff] [blame] | 166 | // Use a thread local field to track whether we're recursing, and fall back. |
| 167 | // (e.g.. if one of our handlers crashed) |
| 168 | Thread* thread = Thread::Current(); |
| 169 | |
| 170 | if (thread != nullptr && !thread->HandlingSignal()) { |
| 171 | // Unblock some signals and set thread->handling_signal_ to true, |
| 172 | // so that we can catch crashes in our signal handler. |
| 173 | ScopedHandlingSignalSetter setter(thread); |
| 174 | ScopedSignalUnblocker unblocker { SIGABRT, SIGBUS, SIGSEGV }; // NOLINT |
| 175 | |
| 176 | VLOG(signals) << "Handling fault"; |
| 177 | |
| 178 | #ifdef TEST_NESTED_SIGNAL |
| 179 | // Simulate a crash in a handler. |
| 180 | raise(SIGSEGV); |
| 181 | #endif |
| 182 | |
| 183 | if (IsInGeneratedCode(info, context, true)) { |
| 184 | VLOG(signals) << "in generated code, looking for handler"; |
| 185 | for (const auto& handler : generated_code_handlers_) { |
| 186 | VLOG(signals) << "invoking Action on handler " << handler; |
| 187 | if (handler->Action(sig, info, context)) { |
| 188 | // We have handled a signal so it's time to return from the |
| 189 | // signal handler to the appropriate place. |
| 190 | return; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // We hit a signal we didn't handle. This might be something for which |
| 195 | // we can give more information about so call all registered handlers to |
| 196 | // see if it is. |
| 197 | if (HandleFaultByOtherHandlers(sig, info, context)) { |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 198 | return; |
Josh Gao | efd20cb | 2017-02-28 16:53:59 -0800 | [diff] [blame] | 199 | } |
jgu21 | 1376bdf | 2016-01-18 09:12:33 -0500 | [diff] [blame] | 200 | } |
| 201 | } |
Dave Allison | f4b80bc | 2014-05-14 15:41:25 -0700 | [diff] [blame] | 202 | |
Dave Allison | 8be44cf | 2014-09-04 14:33:42 -0700 | [diff] [blame] | 203 | // Set a breakpoint in this function to catch unhandled signals. |
| 204 | art_sigsegv_fault(); |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 205 | |
Dave Allison | 8be44cf | 2014-09-04 14:33:42 -0700 | [diff] [blame] | 206 | // Pass this on to the next handler in the chain, or the default if none. |
| 207 | InvokeUserSignalHandler(sig, info, context); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 210 | void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) { |
Andreas Gampe | 928f72b | 2014-09-09 19:53:48 -0700 | [diff] [blame] | 211 | DCHECK(initialized_); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 212 | if (generated_code) { |
| 213 | generated_code_handlers_.push_back(handler); |
| 214 | } else { |
| 215 | other_handlers_.push_back(handler); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void FaultManager::RemoveHandler(FaultHandler* handler) { |
| 220 | auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler); |
| 221 | if (it != generated_code_handlers_.end()) { |
| 222 | generated_code_handlers_.erase(it); |
| 223 | return; |
| 224 | } |
| 225 | auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler); |
| 226 | if (it2 != other_handlers_.end()) { |
| 227 | other_handlers_.erase(it); |
| 228 | return; |
| 229 | } |
| 230 | LOG(FATAL) << "Attempted to remove non existent handler " << handler; |
| 231 | } |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 232 | |
| 233 | // This function is called within the signal handler. It checks that |
| 234 | // the mutator_lock is held (shared). No annotalysis is done. |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 235 | bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 236 | // We can only be running Java code in the current thread if it |
| 237 | // is in Runnable state. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 238 | VLOG(signals) << "Checking for generated code"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 239 | Thread* thread = Thread::Current(); |
| 240 | if (thread == nullptr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 241 | VLOG(signals) << "no current thread"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
| 245 | ThreadState state = thread->GetState(); |
| 246 | if (state != kRunnable) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 247 | VLOG(signals) << "not runnable"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | |
| 251 | // Current thread is runnable. |
| 252 | // Make sure it has the mutator lock. |
| 253 | if (!Locks::mutator_lock_->IsSharedHeld(thread)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 254 | VLOG(signals) << "no lock"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 258 | ArtMethod* method_obj = nullptr; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 259 | uintptr_t return_pc = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 260 | uintptr_t sp = 0; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 261 | |
| 262 | // Get the architecture specific method address and return address. These |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 263 | // are in architecture specific files in arch/<arch>/fault_handler_<arch>. |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 264 | GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 265 | |
| 266 | // If we don't have a potential method, we're outta here. |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 267 | VLOG(signals) << "potential method: " << method_obj; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 268 | // TODO: Check linear alloc and image. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 269 | DCHECK_ALIGNED(ArtMethod::Size(kRuntimePointerSize), sizeof(void*)) |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 270 | << "ArtMethod is not pointer aligned"; |
| 271 | if (method_obj == nullptr || !IsAligned<sizeof(void*)>(method_obj)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 272 | VLOG(signals) << "no method"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | |
| 276 | // Verify that the potential method is indeed a method. |
| 277 | // TODO: check the GC maps to make sure it's an object. |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 278 | // 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] | 279 | // TODO: Method might be not a heap address, and GetClass could fault. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 280 | // No read barrier because method_obj may not be a real object. |
Mathieu Chartier | e7f75f3 | 2016-02-01 16:08:15 -0800 | [diff] [blame] | 281 | mirror::Class* cls = method_obj->GetDeclaringClassUnchecked<kWithoutReadBarrier>(); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 282 | if (cls == nullptr) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 283 | VLOG(signals) << "not a class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | if (!IsAligned<kObjectAlignment>(cls)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 287 | VLOG(signals) << "not aligned"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | if (!VerifyClassClass(cls)) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 293 | VLOG(signals) << "not a class class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 297 | const OatQuickMethodHeader* method_header = method_obj->GetOatQuickMethodHeader(return_pc); |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 298 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 299 | // We can be certain that this is a method now. Check if we have a GC map |
| 300 | // at the return PC address. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 301 | if (true || kIsDebugBuild) { |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 302 | VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc; |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 303 | uint32_t sought_offset = return_pc - |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 304 | reinterpret_cast<uintptr_t>(method_header->GetEntryPoint()); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 305 | VLOG(signals) << "pc offset: " << std::hex << sought_offset; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 306 | } |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 307 | uint32_t dexpc = method_header->ToDexPc(method_obj, return_pc, false); |
Dave Allison | 5cd3375 | 2014-04-15 15:57:58 -0700 | [diff] [blame] | 308 | VLOG(signals) << "dexpc: " << dexpc; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 309 | return !check_dex_pc || dexpc != DexFile::kDexNoIndex; |
| 310 | } |
| 311 | |
| 312 | FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) { |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // |
| 316 | // Null pointer fault handler |
| 317 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 318 | NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) { |
| 319 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // |
| 323 | // Suspension fault handler |
| 324 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 325 | SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) { |
| 326 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // |
| 330 | // Stack overflow fault handler |
| 331 | // |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 332 | StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) { |
| 333 | manager_->AddHandler(this, true); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 334 | } |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 335 | |
| 336 | // |
| 337 | // Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code. |
| 338 | // |
| 339 | JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) { |
| 340 | manager_->AddHandler(this, false); |
| 341 | } |
| 342 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 343 | bool JavaStackTraceHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* siginfo, void* context) { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 344 | // 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] | 345 | bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 346 | if (in_generated_code) { |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 347 | LOG(ERROR) << "Dumping java stack trace for crash in generated code"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 348 | ArtMethod* method = nullptr; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 349 | uintptr_t return_pc = 0; |
| 350 | uintptr_t sp = 0; |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 351 | Thread* self = Thread::Current(); |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 352 | |
Dave Allison | 0c2894b | 2014-08-29 12:06:16 -0700 | [diff] [blame] | 353 | manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp); |
| 354 | // 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] | 355 | self->SetTopOfStack(reinterpret_cast<ArtMethod**>(sp)); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 356 | self->DumpJavaStack(LOG_STREAM(ERROR)); |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 357 | } |
Dave Allison | 8ce6b90 | 2014-08-26 11:07:58 -0700 | [diff] [blame] | 358 | |
Mathieu Chartier | c751fdc | 2014-03-30 15:25:44 -0700 | [diff] [blame] | 359 | return false; // Return false since we want to propagate the fault to the main signal handler. |
| 360 | } |
| 361 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 362 | } // namespace art |