blob: cc1038cb6224c9b0dfd3e039b391236a93d3d42a [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 Allison8ce6b902014-08-26 11:07:58 -070019#include <setjmp.h>
Dave Allisonb373e092014-02-20 16:06:36 -080020#include <sys/mman.h>
21#include <sys/ucontext.h>
Ian Rogers22d5e732014-07-15 22:23:51 -070022#include "mirror/art_method.h"
23#include "mirror/class.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070024#include "sigchain.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070025#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080026#include "verify_object-inl.h"
27
Dave Allison8ce6b902014-08-26 11:07:58 -070028// Note on nested signal support
29// -----------------------------
30//
31// Typically a signal handler should not need to deal with signals that occur within it.
32// However, when a SIGSEGV occurs that is in generated code and is not one of the
33// handled signals (implicit checks), we call a function to try to dump the stack
34// to the log. This enhances the debugging experience but may have the side effect
35// that it may not work. If the cause of the original SIGSEGV is a corrupted stack or other
36// memory region, the stack backtrace code may run into trouble and may either crash
37// or fail with an abort (SIGABRT). In either case we don't want that (new) signal to
38// mask the original signal and thus prevent useful debug output from being presented.
39//
40// In order to handle this situation, before we call the stack tracer we do the following:
41//
42// 1. shutdown the fault manager so that we are talking to the real signal management
43// functions rather than those in sigchain.
44// 2. use pthread_sigmask to allow SIGSEGV and SIGABRT signals to be delivered to the
45// thread running the signal handler.
46// 3. set the handler for SIGSEGV and SIGABRT to a secondary signal handler.
47// 4. save the thread's state to the TLS of the current thread using 'setjmp'
48//
49// We then call the stack tracer and one of two things may happen:
50// a. it completes successfully
51// b. it crashes and a signal is raised.
52//
53// In the former case, we fall through and everything is fine. In the latter case
54// our secondary signal handler gets called in a signal context. This results in
55// a call to FaultManager::HandledNestedSignal(), an archirecture specific function
56// whose purpose is to call 'longjmp' on the jmp_buf saved in the TLS of the current
57// thread. This results in a return with a non-zero value from 'setjmp'. We detect this
58// and write something to the log to tell the user that it happened.
59//
60// Regardless of how we got there, we reach the code after the stack tracer and we
61// restore the signal states to their original values, reinstate the fault manager (thus
62// reestablishing the signal chain) and continue.
63
64// This is difficult to test with a runtime test. To invoke the nested signal code
65// on any signal, uncomment the following line and run something that throws a
66// NullPointerException.
67// #define TEST_NESTED_SIGNAL
68
Dave Allisonb373e092014-02-20 16:06:36 -080069namespace art {
70// Static fault manger object accessed by signal handler.
71FaultManager fault_manager;
72
Dave Allisonf4b80bc2014-05-14 15:41:25 -070073extern "C" {
74void art_sigsegv_fault() {
75 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART.
76 VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler.";
77}
78}
79
Dave Allisonb373e092014-02-20 16:06:36 -080080// Signal handler called on SIGSEGV.
81static void art_fault_handler(int sig, siginfo_t* info, void* context) {
82 fault_manager.HandleFault(sig, info, context);
83}
84
Dave Allison8ce6b902014-08-26 11:07:58 -070085// Signal handler for dealing with a nested signal.
86static void art_nested_signal_handler(int sig, siginfo_t* info, void* context) {
87 fault_manager.HandleNestedSignal(sig, info, context);
88}
89
Dave Allison1f8ef6f2014-08-20 17:38:41 -070090FaultManager::FaultManager() : initialized_(false) {
Dave Allisonb373e092014-02-20 16:06:36 -080091 sigaction(SIGSEGV, nullptr, &oldaction_);
92}
93
94FaultManager::~FaultManager() {
Dave Allisonb373e092014-02-20 16:06:36 -080095}
96
Dave Allisonf4b80bc2014-05-14 15:41:25 -070097
Dave Allisonb373e092014-02-20 16:06:36 -080098void FaultManager::Init() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -070099 CHECK(!initialized_);
Dave Allisonb373e092014-02-20 16:06:36 -0800100 struct sigaction action;
101 action.sa_sigaction = art_fault_handler;
102 sigemptyset(&action.sa_mask);
103 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
Ian Rogersc5f17732014-06-05 20:48:42 -0700104#if !defined(__APPLE__) && !defined(__mips__)
Dave Allisonb373e092014-02-20 16:06:36 -0800105 action.sa_restorer = nullptr;
Narayan Kamath15245bc2014-03-14 12:53:43 +0000106#endif
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700107
108 // Set our signal handler now.
Dave Allison69dfe512014-07-11 17:11:58 +0000109 int e = sigaction(SIGSEGV, &action, &oldaction_);
110 if (e != 0) {
111 VLOG(signals) << "Failed to claim SEGV: " << strerror(errno);
112 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700113 // Make sure our signal handler is called before any user handlers.
114 ClaimSignalChain(SIGSEGV, &oldaction_);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700115 initialized_ = true;
116}
117
118void FaultManager::Shutdown() {
119 if (initialized_) {
120 UnclaimSignalChain(SIGSEGV);
121 initialized_ = false;
122 }
Dave Allisonb373e092014-02-20 16:06:36 -0800123}
124
125void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700126 // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...)
127 //
128 // If malloc calls abort, it will be holding its lock.
129 // If the handler tries to call malloc, it will deadlock.
Dave Allison69dfe512014-07-11 17:11:58 +0000130
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700131 VLOG(signals) << "Handling fault";
Dave Allison69dfe512014-07-11 17:11:58 +0000132 if (IsInGeneratedCode(info, context, true)) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700133 VLOG(signals) << "in generated code, looking for handler";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700134 for (const auto& handler : generated_code_handlers_) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700135 VLOG(signals) << "invoking Action on handler " << handler;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700136 if (handler->Action(sig, info, context)) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700137#ifdef TEST_NESTED_SIGNAL
138 // In test mode we want to fall through to stack trace handler
139 // on every signal (in reality this will cause a crash on the first
140 // signal).
141 break;
142#else
143 // We have handled a signal so it's time to return from the
144 // signal handler to the appropriate place.
Dave Allisonb373e092014-02-20 16:06:36 -0800145 return;
Dave Allison8ce6b902014-08-26 11:07:58 -0700146#endif
Dave Allisonb373e092014-02-20 16:06:36 -0800147 }
148 }
149 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700150
151 // We hit a signal we didn't handle. This might be something for which
152 // we can give more information about so call all registered handlers to see
153 // if it is.
Dave Allison0c2894b2014-08-29 12:06:16 -0700154
155 Thread* self = Thread::Current();
156
157 // Now set up the nested signal handler.
158
159 // Shutdown the fault manager so that it will remove the signal chain for
160 // SIGSEGV and we call the real sigaction.
161 fault_manager.Shutdown();
162
163 // The action for SIGSEGV should be the default handler now.
164
165 // Unblock the signals we allow so that they can be delivered in the signal handler.
166 sigset_t sigset;
167 sigemptyset(&sigset);
168 sigaddset(&sigset, SIGSEGV);
169 sigaddset(&sigset, SIGABRT);
170 pthread_sigmask(SIG_UNBLOCK, &sigset, nullptr);
171
172 // If we get a signal in this code we want to invoke our nested signal
173 // handler.
174 struct sigaction action, oldsegvaction, oldabortaction;
175 action.sa_sigaction = art_nested_signal_handler;
176
177 // Explicitly mask out SIGSEGV and SIGABRT from the nested signal handler. This
178 // should be the default but we definitely don't want these happening in our
179 // nested signal handler.
180 sigemptyset(&action.sa_mask);
181 sigaddset(&action.sa_mask, SIGSEGV);
182 sigaddset(&action.sa_mask, SIGABRT);
183
184 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
185#if !defined(__APPLE__) && !defined(__mips__)
186 action.sa_restorer = nullptr;
187#endif
188
189 bool signal_handled = false;
190
191 // Catch SIGSEGV and SIGABRT to invoke our nested handler
192 int e1 = sigaction(SIGSEGV, &action, &oldsegvaction);
193 int e2 = sigaction(SIGABRT, &action, &oldabortaction);
194 if (e1 != 0 || e2 != 0) {
195 LOG(ERROR) << "Unable to set up nested signal handler";
196 } else {
197 // Save the current state and call the handlers. If anything causes a signal
198 // our nested signal handler will be invoked and this will longjmp to the saved
199 // state.
200 if (setjmp(*self->GetNestedSignalState()) == 0) {
201 for (const auto& handler : other_handlers_) {
202 if (handler->Action(sig, info, context)) {
203 signal_handled = true;
204 break;
205 }
206 }
207 } else {
208 LOG(ERROR) << "Nested signal detected - original signal being reported";
Dave Allisonb373e092014-02-20 16:06:36 -0800209 }
Dave Allison0c2894b2014-08-29 12:06:16 -0700210
211 // Restore the signal handlers.
212 sigaction(SIGSEGV, &oldsegvaction, nullptr);
213 sigaction(SIGABRT, &oldabortaction, nullptr);
Dave Allisonb373e092014-02-20 16:06:36 -0800214 }
Dave Allisondfd3b472014-07-16 16:04:32 -0700215
Dave Allison0c2894b2014-08-29 12:06:16 -0700216 // Now put the fault manager back in place.
217 fault_manager.Init();
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700218
Dave Allison0c2894b2014-08-29 12:06:16 -0700219 if (!signal_handled) {
220 // Set a breakpoint in this function to catch unhandled signals.
221 art_sigsegv_fault();
222
223 // Pass this on to the next handler in the chain, or the default if none.
224 InvokeUserSignalHandler(sig, info, context);
225 }
Dave Allisonb373e092014-02-20 16:06:36 -0800226}
227
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700228void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
229 if (generated_code) {
230 generated_code_handlers_.push_back(handler);
231 } else {
232 other_handlers_.push_back(handler);
233 }
234}
235
236void FaultManager::RemoveHandler(FaultHandler* handler) {
237 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
238 if (it != generated_code_handlers_.end()) {
239 generated_code_handlers_.erase(it);
240 return;
241 }
242 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
243 if (it2 != other_handlers_.end()) {
244 other_handlers_.erase(it);
245 return;
246 }
247 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
248}
Dave Allisonb373e092014-02-20 16:06:36 -0800249
250// This function is called within the signal handler. It checks that
251// the mutator_lock is held (shared). No annotalysis is done.
Dave Allison69dfe512014-07-11 17:11:58 +0000252bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800253 // We can only be running Java code in the current thread if it
254 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700255 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800256 Thread* thread = Thread::Current();
257 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700258 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800259 return false;
260 }
261
262 ThreadState state = thread->GetState();
263 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700264 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800265 return false;
266 }
267
268 // Current thread is runnable.
269 // Make sure it has the mutator lock.
270 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700271 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800272 return false;
273 }
274
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700275 mirror::ArtMethod* method_obj = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800276 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700277 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800278
279 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700280 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Dave Allisondfd3b472014-07-16 16:04:32 -0700281 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800282
283 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700284 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700285 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700286 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800287 return false;
288 }
289
290 // Verify that the potential method is indeed a method.
291 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800292 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700293 // TODO: Method might be not a heap address, and GetClass could fault.
Dave Allisonb373e092014-02-20 16:06:36 -0800294 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
295 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700296 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800297 return false;
298 }
299 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700300 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800301 return false;
302 }
303
304
305 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700306 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800307 return false;
308 }
309
310 // Now make sure the class is a mirror::ArtMethod.
311 if (!cls->IsArtMethodClass()) {
Dave Allison5cd33752014-04-15 15:57:58 -0700312 VLOG(signals) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800313 return false;
314 }
315
316 // We can be certain that this is a method now. Check if we have a GC map
317 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700318 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700319 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700320 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj);
Dave Allisonf9439142014-03-27 15:10:22 -0700321 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
Dave Allison5cd33752014-04-15 15:57:58 -0700322 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700323 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700324 uint32_t dexpc = method_obj->ToDexPc(return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700325 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700326 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
327}
328
329FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800330}
331
332//
333// Null pointer fault handler
334//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700335NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
336 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800337}
338
339//
340// Suspension fault handler
341//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700342SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
343 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800344}
345
346//
347// Stack overflow fault handler
348//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700349StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
350 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800351}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700352
353//
354// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
355//
356JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
357 manager_->AddHandler(this, false);
358}
359
360bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) {
361 // Make sure that we are in the generated code, but we may not have a dex pc.
Dave Allison8ce6b902014-08-26 11:07:58 -0700362
363#ifdef TEST_NESTED_SIGNAL
364 bool in_generated_code = true;
365#else
366 bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false);
367#endif
368 if (in_generated_code) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700369 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
370 mirror::ArtMethod* method = nullptr;
371 uintptr_t return_pc = 0;
372 uintptr_t sp = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700373 Thread* self = Thread::Current();
Dave Allison8ce6b902014-08-26 11:07:58 -0700374
Dave Allison0c2894b2014-08-29 12:06:16 -0700375 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp);
376 // Inside of generated code, sp[0] is the method, so sp is the frame.
377 StackReference<mirror::ArtMethod>* frame =
378 reinterpret_cast<StackReference<mirror::ArtMethod>*>(sp);
379 self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0.
Dave Allison8ce6b902014-08-26 11:07:58 -0700380#ifdef TEST_NESTED_SIGNAL
Dave Allison0c2894b2014-08-29 12:06:16 -0700381 // To test the nested signal handler we raise a signal here. This will cause the
382 // nested signal handler to be called and perform a longjmp back to the setjmp
383 // above.
384 abort();
Dave Allison8ce6b902014-08-26 11:07:58 -0700385#endif
Dave Allison0c2894b2014-08-29 12:06:16 -0700386 self->DumpJavaStack(LOG(ERROR));
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700387 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700388
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700389 return false; // Return false since we want to propagate the fault to the main signal handler.
390}
391
Dave Allisonb373e092014-02-20 16:06:36 -0800392} // namespace art
393