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" |
| 18 | #include <sys/mman.h> |
| 19 | #include <sys/ucontext.h> |
| 20 | #include "base/macros.h" |
| 21 | #include "globals.h" |
| 22 | #include "base/logging.h" |
| 23 | #include "base/hex_dump.h" |
| 24 | #include "thread.h" |
| 25 | #include "mirror/art_method-inl.h" |
| 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/dex_cache.h" |
| 28 | #include "mirror/object_array-inl.h" |
| 29 | #include "mirror/object-inl.h" |
| 30 | #include "object_utils.h" |
| 31 | #include "scoped_thread_state_change.h" |
| 32 | #include "verify_object-inl.h" |
| 33 | |
| 34 | namespace art { |
| 35 | // Static fault manger object accessed by signal handler. |
| 36 | FaultManager fault_manager; |
| 37 | |
| 38 | // Signal handler called on SIGSEGV. |
| 39 | static void art_fault_handler(int sig, siginfo_t* info, void* context) { |
| 40 | fault_manager.HandleFault(sig, info, context); |
| 41 | } |
| 42 | |
| 43 | FaultManager::FaultManager() { |
| 44 | sigaction(SIGSEGV, nullptr, &oldaction_); |
| 45 | } |
| 46 | |
| 47 | FaultManager::~FaultManager() { |
| 48 | sigaction(SIGSEGV, &oldaction_, nullptr); // Restore old handler. |
| 49 | } |
| 50 | |
| 51 | void FaultManager::Init() { |
| 52 | struct sigaction action; |
| 53 | action.sa_sigaction = art_fault_handler; |
| 54 | sigemptyset(&action.sa_mask); |
| 55 | action.sa_flags = SA_SIGINFO | SA_ONSTACK; |
Narayan Kamath | 15245bc | 2014-03-14 12:53:43 +0000 | [diff] [blame] | 56 | #if !defined(__mips__) |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 57 | action.sa_restorer = nullptr; |
Narayan Kamath | 15245bc | 2014-03-14 12:53:43 +0000 | [diff] [blame] | 58 | #endif |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 59 | sigaction(SIGSEGV, &action, &oldaction_); |
| 60 | } |
| 61 | |
| 62 | void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) { |
| 63 | bool handled = false; |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 64 | LOG(DEBUG) << "Handling fault"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 65 | if (IsInGeneratedCode(context)) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 66 | LOG(DEBUG) << "in generated code, looking for handler"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 67 | for (auto& handler : handlers_) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 68 | LOG(DEBUG) << "invoking Action on handler " << handler; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 69 | handled = handler->Action(sig, info, context); |
| 70 | if (handled) { |
| 71 | return; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (!handled) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 77 | LOG(ERROR)<< "Caught unknown SIGSEGV in ART fault handler"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 78 | oldaction_.sa_sigaction(sig, info, context); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void FaultManager::AddHandler(FaultHandler* handler) { |
| 83 | handlers_.push_back(handler); |
| 84 | } |
| 85 | |
| 86 | void FaultManager::RemoveHandler(FaultHandler* handler) { |
| 87 | for (Handlers::iterator i = handlers_.begin(); i != handlers_.end(); ++i) { |
| 88 | FaultHandler* h = *i; |
| 89 | if (h == handler) { |
| 90 | handlers_.erase(i); |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | // This function is called within the signal handler. It checks that |
| 98 | // the mutator_lock is held (shared). No annotalysis is done. |
| 99 | bool FaultManager::IsInGeneratedCode(void *context) { |
| 100 | // We can only be running Java code in the current thread if it |
| 101 | // is in Runnable state. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 102 | LOG(DEBUG) << "Checking for generated code"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 103 | Thread* thread = Thread::Current(); |
| 104 | if (thread == nullptr) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 105 | LOG(DEBUG) << "no current thread"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | |
| 109 | ThreadState state = thread->GetState(); |
| 110 | if (state != kRunnable) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 111 | LOG(DEBUG) << "not runnable"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
| 115 | // Current thread is runnable. |
| 116 | // Make sure it has the mutator lock. |
| 117 | if (!Locks::mutator_lock_->IsSharedHeld(thread)) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 118 | LOG(DEBUG) << "no lock"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
| 122 | uintptr_t potential_method = 0; |
| 123 | uintptr_t return_pc = 0; |
| 124 | |
| 125 | // Get the architecture specific method address and return address. These |
| 126 | // are in architecture specific files in arch/<arch>/fault_handler_<arch>.cc |
| 127 | GetMethodAndReturnPC(context, /*out*/potential_method, /*out*/return_pc); |
| 128 | |
| 129 | // If we don't have a potential method, we're outta here. |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 130 | LOG(DEBUG) << "potential method: " << potential_method; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 131 | if (potential_method == 0) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 132 | LOG(DEBUG) << "no method"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // Verify that the potential method is indeed a method. |
| 137 | // TODO: check the GC maps to make sure it's an object. |
| 138 | |
| 139 | mirror::Object* method_obj = |
| 140 | reinterpret_cast<mirror::Object*>(potential_method); |
| 141 | |
| 142 | // Check that the class pointer inside the object is not null and is aligned. |
| 143 | mirror::Class* cls = method_obj->GetClass<kVerifyNone>(); |
| 144 | if (cls == nullptr) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 145 | LOG(DEBUG) << "not a class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | if (!IsAligned<kObjectAlignment>(cls)) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 149 | LOG(DEBUG) << "not aligned"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | if (!VerifyClassClass(cls)) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 155 | LOG(DEBUG) << "not a class class"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // Now make sure the class is a mirror::ArtMethod. |
| 160 | if (!cls->IsArtMethodClass()) { |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 161 | LOG(DEBUG) << "not a method"; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
| 165 | // We can be certain that this is a method now. Check if we have a GC map |
| 166 | // at the return PC address. |
| 167 | mirror::ArtMethod* method = |
| 168 | reinterpret_cast<mirror::ArtMethod*>(potential_method); |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 169 | if (true || kIsDebugBuild) { |
| 170 | LOG(DEBUG) << "looking for dex pc for return pc " << std::hex << return_pc; |
| 171 | const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method); |
| 172 | uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code); |
| 173 | LOG(DEBUG) << "pc offset: " << std::hex << sought_offset; |
| 174 | } |
| 175 | uint32_t dexpc = method->ToDexPc(return_pc, false); |
| 176 | LOG(DEBUG) << "dexpc: " << dexpc; |
| 177 | return dexpc != DexFile::kDexNoIndex; |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // |
| 181 | // Null pointer fault handler |
| 182 | // |
| 183 | |
| 184 | NullPointerHandler::NullPointerHandler(FaultManager* manager) { |
| 185 | manager->AddHandler(this); |
| 186 | } |
| 187 | |
| 188 | // |
| 189 | // Suspension fault handler |
| 190 | // |
| 191 | |
| 192 | SuspensionHandler::SuspensionHandler(FaultManager* manager) { |
| 193 | manager->AddHandler(this); |
| 194 | } |
| 195 | |
| 196 | // |
| 197 | // Stack overflow fault handler |
| 198 | // |
| 199 | |
| 200 | StackOverflowHandler::StackOverflowHandler(FaultManager* manager) { |
| 201 | manager->AddHandler(this); |
| 202 | } |
| 203 | } // namespace art |
| 204 | |