blob: 8ddaf5cf246337e4a58347254820cb92ece5faee [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#include "mirror/art_method.h"
22#include "mirror/class.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070023#include "sigchain.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070024#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080025#include "verify_object-inl.h"
26
27namespace art {
28// Static fault manger object accessed by signal handler.
29FaultManager fault_manager;
30
Dave Allisonf4b80bc2014-05-14 15:41:25 -070031extern "C" {
32void art_sigsegv_fault() {
33 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART.
34 VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler.";
35}
36}
37
Dave Allisonb373e092014-02-20 16:06:36 -080038// Signal handler called on SIGSEGV.
39static void art_fault_handler(int sig, siginfo_t* info, void* context) {
Dave Allison69dfe512014-07-11 17:11:58 +000040 // std::cout << "handling fault in ART handler\n";
Dave Allisonb373e092014-02-20 16:06:36 -080041 fault_manager.HandleFault(sig, info, context);
42}
43
44FaultManager::FaultManager() {
45 sigaction(SIGSEGV, nullptr, &oldaction_);
46}
47
48FaultManager::~FaultManager() {
Dave Allisonb373e092014-02-20 16:06:36 -080049}
50
Dave Allisonf4b80bc2014-05-14 15:41:25 -070051
Dave Allisonb373e092014-02-20 16:06:36 -080052void FaultManager::Init() {
53 struct sigaction action;
54 action.sa_sigaction = art_fault_handler;
55 sigemptyset(&action.sa_mask);
56 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
Ian Rogersc5f17732014-06-05 20:48:42 -070057#if !defined(__APPLE__) && !defined(__mips__)
Dave Allisonb373e092014-02-20 16:06:36 -080058 action.sa_restorer = nullptr;
Narayan Kamath15245bc2014-03-14 12:53:43 +000059#endif
Dave Allisonf4b80bc2014-05-14 15:41:25 -070060
61 // Set our signal handler now.
Dave Allison69dfe512014-07-11 17:11:58 +000062 int e = sigaction(SIGSEGV, &action, &oldaction_);
63 if (e != 0) {
64 VLOG(signals) << "Failed to claim SEGV: " << strerror(errno);
65 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -070066 // Make sure our signal handler is called before any user handlers.
67 ClaimSignalChain(SIGSEGV, &oldaction_);
Dave Allisonb373e092014-02-20 16:06:36 -080068}
69
70void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070071 // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...)
72 //
73 // If malloc calls abort, it will be holding its lock.
74 // If the handler tries to call malloc, it will deadlock.
Dave Allison69dfe512014-07-11 17:11:58 +000075
76 // Also, there is only an 8K stack available here to logging can cause memory
77 // overwrite issues if you are unlucky. If you want to enable logging and
78 // are getting crashes, allocate more space for the alternate signal stack.
Dave Allisondfd3b472014-07-16 16:04:32 -070079
Brian Carlstrom4d466a82014-05-08 19:05:29 -070080 VLOG(signals) << "Handling fault";
Dave Allison69dfe512014-07-11 17:11:58 +000081 if (IsInGeneratedCode(info, 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 Allisondfd3b472014-07-16 16:04:32 -070095
Dave Allisonf4b80bc2014-05-14 15:41:25 -070096 art_sigsegv_fault();
97
Dave Allison69dfe512014-07-11 17:11:58 +000098 // Pass this on to the next handler in the chain, or the default if none.
Dave Allisonf4b80bc2014-05-14 15:41:25 -070099 InvokeUserSignalHandler(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -0800100}
101
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700102void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
103 if (generated_code) {
104 generated_code_handlers_.push_back(handler);
105 } else {
106 other_handlers_.push_back(handler);
107 }
108}
109
110void FaultManager::RemoveHandler(FaultHandler* handler) {
111 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
112 if (it != generated_code_handlers_.end()) {
113 generated_code_handlers_.erase(it);
114 return;
115 }
116 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
117 if (it2 != other_handlers_.end()) {
118 other_handlers_.erase(it);
119 return;
120 }
121 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
122}
Dave Allisonb373e092014-02-20 16:06:36 -0800123
124// This function is called within the signal handler. It checks that
125// the mutator_lock is held (shared). No annotalysis is done.
Dave Allison69dfe512014-07-11 17:11:58 +0000126bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800127 // We can only be running Java code in the current thread if it
128 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700129 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800130 Thread* thread = Thread::Current();
131 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700132 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800133 return false;
134 }
135
136 ThreadState state = thread->GetState();
137 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700138 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800139 return false;
140 }
141
142 // Current thread is runnable.
143 // Make sure it has the mutator lock.
144 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700145 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800146 return false;
147 }
148
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700149 mirror::ArtMethod* method_obj = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800150 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700151 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800152
153 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700154 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Dave Allisondfd3b472014-07-16 16:04:32 -0700155 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800156
157 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700158 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700159 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700160 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800161 return false;
162 }
163
164 // Verify that the potential method is indeed a method.
165 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800166 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700167 // TODO: Method might be not a heap address, and GetClass could fault.
Dave Allisonb373e092014-02-20 16:06:36 -0800168 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
169 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700170 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800171 return false;
172 }
173 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700174 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800175 return false;
176 }
177
178
179 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700180 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800181 return false;
182 }
183
184 // Now make sure the class is a mirror::ArtMethod.
185 if (!cls->IsArtMethodClass()) {
Dave Allison5cd33752014-04-15 15:57:58 -0700186 VLOG(signals) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800187 return false;
188 }
189
190 // We can be certain that this is a method now. Check if we have a GC map
191 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700192 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700193 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700194 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj);
Dave Allisonf9439142014-03-27 15:10:22 -0700195 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
Dave Allison5cd33752014-04-15 15:57:58 -0700196 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700197 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700198 uint32_t dexpc = method_obj->ToDexPc(return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700199 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700200 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
201}
202
203FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800204}
205
206//
207// Null pointer fault handler
208//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700209NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
210 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800211}
212
213//
214// Suspension fault handler
215//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700216SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
217 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800218}
219
220//
221// Stack overflow fault handler
222//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700223StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
224 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800225}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700226
227//
228// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
229//
230JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
231 manager_->AddHandler(this, false);
232}
233
234bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) {
235 // Make sure that we are in the generated code, but we may not have a dex pc.
Dave Allison69dfe512014-07-11 17:11:58 +0000236 if (manager_->IsInGeneratedCode(siginfo, context, false)) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700237 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
238 mirror::ArtMethod* method = nullptr;
239 uintptr_t return_pc = 0;
240 uintptr_t sp = 0;
Dave Allisondfd3b472014-07-16 16:04:32 -0700241 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700242 Thread* self = Thread::Current();
243 // Inside of generated code, sp[0] is the method, so sp is the frame.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700244 StackReference<mirror::ArtMethod>* frame =
245 reinterpret_cast<StackReference<mirror::ArtMethod>*>(sp);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700246 self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0.
247 self->DumpJavaStack(LOG(ERROR));
248 }
249 return false; // Return false since we want to propagate the fault to the main signal handler.
250}
251
Dave Allisonb373e092014-02-20 16:06:36 -0800252} // namespace art
253