blob: b4355cad5c4be9bf53b6a59e5b68ff2aa66c0107 [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"
Ian Rogers22d5e732014-07-15 22:23:51 -070018
Dave Allisonb373e092014-02-20 16:06:36 -080019#include <sys/mman.h>
20#include <sys/ucontext.h>
Ian Rogers22d5e732014-07-15 22:23:51 -070021
22#include "mirror/art_method.h"
23#include "mirror/class.h"
Nicolas Geoffray0025a862014-07-11 08:26:40 +000024#ifdef HAVE_ANDROID_OS
Dave Allisonf4b80bc2014-05-14 15:41:25 -070025#include "sigchain.h"
Nicolas Geoffray0025a862014-07-11 08:26:40 +000026#endif
Ian Rogers22d5e732014-07-15 22:23:51 -070027#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080028#include "verify_object-inl.h"
29
30namespace art {
31// Static fault manger object accessed by signal handler.
32FaultManager fault_manager;
33
Dave Allisonf4b80bc2014-05-14 15:41:25 -070034extern "C" {
35void art_sigsegv_fault() {
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}
39}
40
Dave Allisonb373e092014-02-20 16:06:36 -080041// Signal handler called on SIGSEGV.
42static void art_fault_handler(int sig, siginfo_t* info, void* context) {
43 fault_manager.HandleFault(sig, info, context);
44}
45
46FaultManager::FaultManager() {
47 sigaction(SIGSEGV, nullptr, &oldaction_);
48}
49
50FaultManager::~FaultManager() {
Nicolas Geoffray0025a862014-07-11 08:26:40 +000051#ifdef HAVE_ANDROID_OS
Dave Allisonf4b80bc2014-05-14 15:41:25 -070052 UnclaimSignalChain(SIGSEGV);
Nicolas Geoffray0025a862014-07-11 08:26:40 +000053#endif
Dave Allisonb373e092014-02-20 16:06:36 -080054 sigaction(SIGSEGV, &oldaction_, nullptr); // Restore old handler.
55}
56
Dave Allisonf4b80bc2014-05-14 15:41:25 -070057
Dave Allisonb373e092014-02-20 16:06:36 -080058void FaultManager::Init() {
59 struct sigaction action;
60 action.sa_sigaction = art_fault_handler;
61 sigemptyset(&action.sa_mask);
62 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
Ian Rogersc5f17732014-06-05 20:48:42 -070063#if !defined(__APPLE__) && !defined(__mips__)
Dave Allisonb373e092014-02-20 16:06:36 -080064 action.sa_restorer = nullptr;
Narayan Kamath15245bc2014-03-14 12:53:43 +000065#endif
Dave Allisonf4b80bc2014-05-14 15:41:25 -070066
67 // Set our signal handler now.
Nicolas Geoffray0025a862014-07-11 08:26:40 +000068 sigaction(SIGSEGV, &action, &oldaction_);
69#ifdef HAVE_ANDROID_OS
Dave Allisonf4b80bc2014-05-14 15:41:25 -070070 // Make sure our signal handler is called before any user handlers.
71 ClaimSignalChain(SIGSEGV, &oldaction_);
Nicolas Geoffray0025a862014-07-11 08:26:40 +000072#endif
Dave Allisonb373e092014-02-20 16:06:36 -080073}
74
75void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070076 // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...)
77 //
78 // If malloc calls abort, it will be holding its lock.
79 // If the handler tries to call malloc, it will deadlock.
80 VLOG(signals) << "Handling fault";
Nicolas Geoffray0025a862014-07-11 08:26:40 +000081 if (IsInGeneratedCode(context, true)) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070082 VLOG(signals) << "in generated code, looking for handler";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070083 for (const auto& handler : generated_code_handlers_) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070084 VLOG(signals) << "invoking Action on handler " << handler;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070085 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080086 return;
87 }
88 }
89 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070090 for (const auto& handler : other_handlers_) {
91 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080092 return;
93 }
94 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -070095 art_sigsegv_fault();
96
Nicolas Geoffray0025a862014-07-11 08:26:40 +000097#ifdef HAVE_ANDROID_OS
Dave Allisonf4b80bc2014-05-14 15:41:25 -070098 InvokeUserSignalHandler(sig, info, context);
Nicolas Geoffray0025a862014-07-11 08:26:40 +000099#else
100 oldaction_.sa_sigaction(sig, info, context);
101#endif
Dave Allisonb373e092014-02-20 16:06:36 -0800102}
103
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700104void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
105 if (generated_code) {
106 generated_code_handlers_.push_back(handler);
107 } else {
108 other_handlers_.push_back(handler);
109 }
110}
111
112void FaultManager::RemoveHandler(FaultHandler* handler) {
113 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
114 if (it != generated_code_handlers_.end()) {
115 generated_code_handlers_.erase(it);
116 return;
117 }
118 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
119 if (it2 != other_handlers_.end()) {
120 other_handlers_.erase(it);
121 return;
122 }
123 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
124}
Dave Allisonb373e092014-02-20 16:06:36 -0800125
126// This function is called within the signal handler. It checks that
127// the mutator_lock is held (shared). No annotalysis is done.
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000128bool FaultManager::IsInGeneratedCode(void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800129 // We can only be running Java code in the current thread if it
130 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700131 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800132 Thread* thread = Thread::Current();
133 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700134 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800135 return false;
136 }
137
138 ThreadState state = thread->GetState();
139 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700140 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800141 return false;
142 }
143
144 // Current thread is runnable.
145 // Make sure it has the mutator lock.
146 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700147 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800148 return false;
149 }
150
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700151 mirror::ArtMethod* method_obj = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800152 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700153 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800154
155 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700156 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000157 GetMethodAndReturnPCAndSP(context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800158
159 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700160 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700161 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700162 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800163 return false;
164 }
165
166 // Verify that the potential method is indeed a method.
167 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800168 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700169 // TODO: Method might be not a heap address, and GetClass could fault.
Dave Allisonb373e092014-02-20 16:06:36 -0800170 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
171 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700172 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800173 return false;
174 }
175 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700176 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800177 return false;
178 }
179
180
181 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700182 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800183 return false;
184 }
185
186 // Now make sure the class is a mirror::ArtMethod.
187 if (!cls->IsArtMethodClass()) {
Dave Allison5cd33752014-04-15 15:57:58 -0700188 VLOG(signals) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800189 return false;
190 }
191
192 // We can be certain that this is a method now. Check if we have a GC map
193 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700194 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700195 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700196 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj);
Dave Allisonf9439142014-03-27 15:10:22 -0700197 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
Dave Allison5cd33752014-04-15 15:57:58 -0700198 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700199 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700200 uint32_t dexpc = method_obj->ToDexPc(return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700201 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700202 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
203}
204
205FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800206}
207
208//
209// Null pointer fault handler
210//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700211NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
212 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800213}
214
215//
216// Suspension fault handler
217//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700218SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
219 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800220}
221
222//
223// Stack overflow fault handler
224//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700225StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
226 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800227}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700228
229//
230// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
231//
232JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
233 manager_->AddHandler(this, false);
234}
235
236bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) {
237 // Make sure that we are in the generated code, but we may not have a dex pc.
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000238 if (manager_->IsInGeneratedCode(context, false)) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700239 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
240 mirror::ArtMethod* method = nullptr;
241 uintptr_t return_pc = 0;
242 uintptr_t sp = 0;
Nicolas Geoffray0025a862014-07-11 08:26:40 +0000243 manager_->GetMethodAndReturnPCAndSP(context, &method, &return_pc, &sp);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700244 Thread* self = Thread::Current();
245 // Inside of generated code, sp[0] is the method, so sp is the frame.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700246 StackReference<mirror::ArtMethod>* frame =
247 reinterpret_cast<StackReference<mirror::ArtMethod>*>(sp);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700248 self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0.
249 self->DumpJavaStack(LOG(ERROR));
250 }
251 return false; // Return false since we want to propagate the fault to the main signal handler.
252}
253
Dave Allisonb373e092014-02-20 16:06:36 -0800254} // namespace art
255