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