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