blob: 15c38c1f4bb316ec72827dbd7b72fcbdb0d7b8ce [file] [log] [blame]
Dave Allisonb373e092014-02-20 16:06:36 -08001/*
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"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070032#ifdef HAVE_ANDROID_OS
33#include "sigchain.h"
34#endif
Dave Allisonb373e092014-02-20 16:06:36 -080035#include "verify_object-inl.h"
36
37namespace art {
38// Static fault manger object accessed by signal handler.
39FaultManager fault_manager;
40
Dave Allisonf4b80bc2014-05-14 15:41:25 -070041extern "C" {
42void art_sigsegv_fault() {
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}
46}
47
Dave Allisonb373e092014-02-20 16:06:36 -080048// Signal handler called on SIGSEGV.
49static void art_fault_handler(int sig, siginfo_t* info, void* context) {
50 fault_manager.HandleFault(sig, info, context);
51}
52
53FaultManager::FaultManager() {
54 sigaction(SIGSEGV, nullptr, &oldaction_);
55}
56
57FaultManager::~FaultManager() {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070058#ifdef HAVE_ANDROID_OS
59 UnclaimSignalChain(SIGSEGV);
60#endif
Dave Allisonb373e092014-02-20 16:06:36 -080061 sigaction(SIGSEGV, &oldaction_, nullptr); // Restore old handler.
62}
63
Dave Allisonf4b80bc2014-05-14 15:41:25 -070064
Dave Allisonb373e092014-02-20 16:06:36 -080065void FaultManager::Init() {
66 struct sigaction action;
67 action.sa_sigaction = art_fault_handler;
68 sigemptyset(&action.sa_mask);
69 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
Narayan Kamath15245bc2014-03-14 12:53:43 +000070#if !defined(__mips__)
Dave Allisonb373e092014-02-20 16:06:36 -080071 action.sa_restorer = nullptr;
Narayan Kamath15245bc2014-03-14 12:53:43 +000072#endif
Dave Allisonf4b80bc2014-05-14 15:41:25 -070073
74 // Set our signal handler now.
Dave Allisonb373e092014-02-20 16:06:36 -080075 sigaction(SIGSEGV, &action, &oldaction_);
Dave Allisonf4b80bc2014-05-14 15:41:25 -070076#ifdef HAVE_ANDROID_OS
77 // Make sure our signal handler is called before any user handlers.
78 ClaimSignalChain(SIGSEGV, &oldaction_);
79#endif
Dave Allisonb373e092014-02-20 16:06:36 -080080}
81
82void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070083 // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...)
84 //
85 // If malloc calls abort, it will be holding its lock.
86 // If the handler tries to call malloc, it will deadlock.
87 VLOG(signals) << "Handling fault";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070088 if (IsInGeneratedCode(context, true)) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070089 VLOG(signals) << "in generated code, looking for handler";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070090 for (const auto& handler : generated_code_handlers_) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070091 VLOG(signals) << "invoking Action on handler " << handler;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070092 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080093 return;
94 }
95 }
96 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070097 for (const auto& handler : other_handlers_) {
98 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080099 return;
100 }
101 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700102 art_sigsegv_fault();
103
104#ifdef HAVE_ANDROID_OS
105 InvokeUserSignalHandler(sig, info, context);
106#else
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700107 oldaction_.sa_sigaction(sig, info, context);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700108#endif
Dave Allisonb373e092014-02-20 16:06:36 -0800109}
110
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700111void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
112 if (generated_code) {
113 generated_code_handlers_.push_back(handler);
114 } else {
115 other_handlers_.push_back(handler);
116 }
117}
118
119void FaultManager::RemoveHandler(FaultHandler* handler) {
120 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
121 if (it != generated_code_handlers_.end()) {
122 generated_code_handlers_.erase(it);
123 return;
124 }
125 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
126 if (it2 != other_handlers_.end()) {
127 other_handlers_.erase(it);
128 return;
129 }
130 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
131}
Dave Allisonb373e092014-02-20 16:06:36 -0800132
133// This function is called within the signal handler. It checks that
134// the mutator_lock is held (shared). No annotalysis is done.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700135bool FaultManager::IsInGeneratedCode(void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800136 // We can only be running Java code in the current thread if it
137 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700138 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800139 Thread* thread = Thread::Current();
140 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700141 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800142 return false;
143 }
144
145 ThreadState state = thread->GetState();
146 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700147 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800148 return false;
149 }
150
151 // Current thread is runnable.
152 // Make sure it has the mutator lock.
153 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700154 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800155 return false;
156 }
157
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700158 mirror::ArtMethod* method_obj = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800159 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700160 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800161
162 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700163 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
164 GetMethodAndReturnPCAndSP(context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800165
166 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700167 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700168 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700169 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800170 return false;
171 }
172
173 // Verify that the potential method is indeed a method.
174 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800175 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700176 // TODO: Method might be not a heap address, and GetClass could fault.
Dave Allisonb373e092014-02-20 16:06:36 -0800177 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
178 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700179 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800180 return false;
181 }
182 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700183 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800184 return false;
185 }
186
187
188 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700189 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800190 return false;
191 }
192
193 // Now make sure the class is a mirror::ArtMethod.
194 if (!cls->IsArtMethodClass()) {
Dave Allison5cd33752014-04-15 15:57:58 -0700195 VLOG(signals) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800196 return false;
197 }
198
199 // We can be certain that this is a method now. Check if we have a GC map
200 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700201 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700202 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700203 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj);
Dave Allisonf9439142014-03-27 15:10:22 -0700204 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
Dave Allison5cd33752014-04-15 15:57:58 -0700205 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700206 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700207 uint32_t dexpc = method_obj->ToDexPc(return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700208 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700209 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
210}
211
212FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800213}
214
215//
216// Null pointer fault handler
217//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700218NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
219 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800220}
221
222//
223// Suspension fault handler
224//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700225SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
226 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800227}
228
229//
230// Stack overflow fault handler
231//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700232StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
233 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800234}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700235
236//
237// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
238//
239JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
240 manager_->AddHandler(this, false);
241}
242
243bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) {
244 // Make sure that we are in the generated code, but we may not have a dex pc.
245 if (manager_->IsInGeneratedCode(context, false)) {
246 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
247 mirror::ArtMethod* method = nullptr;
248 uintptr_t return_pc = 0;
249 uintptr_t sp = 0;
250 manager_->GetMethodAndReturnPCAndSP(context, &method, &return_pc, &sp);
251 Thread* self = Thread::Current();
252 // Inside of generated code, sp[0] is the method, so sp is the frame.
253 mirror::ArtMethod** frame = reinterpret_cast<mirror::ArtMethod**>(sp);
254 self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0.
255 self->DumpJavaStack(LOG(ERROR));
256 }
257 return false; // Return false since we want to propagate the fault to the main signal handler.
258}
259
Dave Allisonb373e092014-02-20 16:06:36 -0800260} // namespace art
261