blob: c4736847b9819de83f782186782bff529e688af6 [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>
Andreas Gampe928f72b2014-09-09 19:53:48 -070022#include "base/stl_util.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070023#include "mirror/art_method.h"
24#include "mirror/class.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070025#include "sigchain.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070026#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080027#include "verify_object-inl.h"
28
Dave Allison8ce6b902014-08-26 11:07:58 -070029// Note on nested signal support
30// -----------------------------
31//
32// Typically a signal handler should not need to deal with signals that occur within it.
33// However, when a SIGSEGV occurs that is in generated code and is not one of the
34// handled signals (implicit checks), we call a function to try to dump the stack
35// to the log. This enhances the debugging experience but may have the side effect
36// that it may not work. If the cause of the original SIGSEGV is a corrupted stack or other
37// memory region, the stack backtrace code may run into trouble and may either crash
38// or fail with an abort (SIGABRT). In either case we don't want that (new) signal to
39// mask the original signal and thus prevent useful debug output from being presented.
40//
41// In order to handle this situation, before we call the stack tracer we do the following:
42//
43// 1. shutdown the fault manager so that we are talking to the real signal management
44// functions rather than those in sigchain.
45// 2. use pthread_sigmask to allow SIGSEGV and SIGABRT signals to be delivered to the
46// thread running the signal handler.
47// 3. set the handler for SIGSEGV and SIGABRT to a secondary signal handler.
48// 4. save the thread's state to the TLS of the current thread using 'setjmp'
49//
50// We then call the stack tracer and one of two things may happen:
51// a. it completes successfully
52// b. it crashes and a signal is raised.
53//
54// In the former case, we fall through and everything is fine. In the latter case
55// our secondary signal handler gets called in a signal context. This results in
56// a call to FaultManager::HandledNestedSignal(), an archirecture specific function
57// whose purpose is to call 'longjmp' on the jmp_buf saved in the TLS of the current
58// thread. This results in a return with a non-zero value from 'setjmp'. We detect this
59// and write something to the log to tell the user that it happened.
60//
61// Regardless of how we got there, we reach the code after the stack tracer and we
62// restore the signal states to their original values, reinstate the fault manager (thus
63// reestablishing the signal chain) and continue.
64
65// This is difficult to test with a runtime test. To invoke the nested signal code
66// on any signal, uncomment the following line and run something that throws a
67// NullPointerException.
68// #define TEST_NESTED_SIGNAL
69
Dave Allisonb373e092014-02-20 16:06:36 -080070namespace art {
71// Static fault manger object accessed by signal handler.
72FaultManager fault_manager;
73
Dave Allisonf4b80bc2014-05-14 15:41:25 -070074extern "C" {
75void art_sigsegv_fault() {
76 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART.
77 VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler.";
78}
79}
80
Dave Allisonb373e092014-02-20 16:06:36 -080081// Signal handler called on SIGSEGV.
82static void art_fault_handler(int sig, siginfo_t* info, void* context) {
83 fault_manager.HandleFault(sig, info, context);
84}
85
Dave Allison8ce6b902014-08-26 11:07:58 -070086// Signal handler for dealing with a nested signal.
87static void art_nested_signal_handler(int sig, siginfo_t* info, void* context) {
88 fault_manager.HandleNestedSignal(sig, info, context);
89}
90
Dave Allison1f8ef6f2014-08-20 17:38:41 -070091FaultManager::FaultManager() : initialized_(false) {
Dave Allisonb373e092014-02-20 16:06:36 -080092 sigaction(SIGSEGV, nullptr, &oldaction_);
93}
94
95FaultManager::~FaultManager() {
Dave Allisonb373e092014-02-20 16:06:36 -080096}
97
Mathieu Chartierd0004802014-10-15 16:59:47 -070098static void SetUpArtAction(struct sigaction* action) {
99 action->sa_sigaction = art_fault_handler;
100 sigemptyset(&action->sa_mask);
101 action->sa_flags = SA_SIGINFO | SA_ONSTACK;
102#if !defined(__APPLE__) && !defined(__mips__)
103 action->sa_restorer = nullptr;
104#endif
105}
106
107void FaultManager::EnsureArtActionInFrontOfSignalChain() {
108 if (initialized_) {
109 struct sigaction action;
110 SetUpArtAction(&action);
111 EnsureFrontOfChain(SIGSEGV, &action);
112 } else {
113 LOG(WARNING) << "Can't call " << __FUNCTION__ << " due to unitialized fault manager";
114 }
115}
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700116
Dave Allisonb373e092014-02-20 16:06:36 -0800117void FaultManager::Init() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700118 CHECK(!initialized_);
Dave Allisonb373e092014-02-20 16:06:36 -0800119 struct sigaction action;
Mathieu Chartierd0004802014-10-15 16:59:47 -0700120 SetUpArtAction(&action);
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700121
122 // Set our signal handler now.
Dave Allison69dfe512014-07-11 17:11:58 +0000123 int e = sigaction(SIGSEGV, &action, &oldaction_);
124 if (e != 0) {
125 VLOG(signals) << "Failed to claim SEGV: " << strerror(errno);
126 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700127 // Make sure our signal handler is called before any user handlers.
128 ClaimSignalChain(SIGSEGV, &oldaction_);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700129 initialized_ = true;
130}
131
Andreas Gampe928f72b2014-09-09 19:53:48 -0700132void FaultManager::Release() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700133 if (initialized_) {
134 UnclaimSignalChain(SIGSEGV);
135 initialized_ = false;
136 }
Dave Allisonb373e092014-02-20 16:06:36 -0800137}
138
Andreas Gampe928f72b2014-09-09 19:53:48 -0700139void FaultManager::Shutdown() {
140 if (initialized_) {
141 Release();
142
143 // Free all handlers.
144 STLDeleteElements(&generated_code_handlers_);
145 STLDeleteElements(&other_handlers_);
146 }
147}
148
Dave Allisonb373e092014-02-20 16:06:36 -0800149void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700150 // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...)
151 //
152 // If malloc calls abort, it will be holding its lock.
153 // If the handler tries to call malloc, it will deadlock.
154 VLOG(signals) << "Handling fault";
Dave Allison69dfe512014-07-11 17:11:58 +0000155 if (IsInGeneratedCode(info, context, true)) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700156 VLOG(signals) << "in generated code, looking for handler";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700157 for (const auto& handler : generated_code_handlers_) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700158 VLOG(signals) << "invoking Action on handler " << handler;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700159 if (handler->Action(sig, info, context)) {
Dave Allison8ce6b902014-08-26 11:07:58 -0700160#ifdef TEST_NESTED_SIGNAL
161 // In test mode we want to fall through to stack trace handler
162 // on every signal (in reality this will cause a crash on the first
163 // signal).
164 break;
165#else
166 // We have handled a signal so it's time to return from the
167 // signal handler to the appropriate place.
Dave Allisonb373e092014-02-20 16:06:36 -0800168 return;
Dave Allison8ce6b902014-08-26 11:07:58 -0700169#endif
Dave Allisonb373e092014-02-20 16:06:36 -0800170 }
171 }
172 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700173
174 // We hit a signal we didn't handle. This might be something for which
175 // we can give more information about so call all registered handlers to see
176 // if it is.
Dave Allison0c2894b2014-08-29 12:06:16 -0700177
178 Thread* self = Thread::Current();
179
180 // Now set up the nested signal handler.
181
Andreas Gampe928f72b2014-09-09 19:53:48 -0700182 // Release the fault manager so that it will remove the signal chain for
Dave Allison0c2894b2014-08-29 12:06:16 -0700183 // SIGSEGV and we call the real sigaction.
Andreas Gampe928f72b2014-09-09 19:53:48 -0700184 fault_manager.Release();
Dave Allison0c2894b2014-08-29 12:06:16 -0700185
186 // The action for SIGSEGV should be the default handler now.
187
188 // Unblock the signals we allow so that they can be delivered in the signal handler.
189 sigset_t sigset;
190 sigemptyset(&sigset);
191 sigaddset(&sigset, SIGSEGV);
192 sigaddset(&sigset, SIGABRT);
193 pthread_sigmask(SIG_UNBLOCK, &sigset, nullptr);
194
195 // If we get a signal in this code we want to invoke our nested signal
196 // handler.
197 struct sigaction action, oldsegvaction, oldabortaction;
198 action.sa_sigaction = art_nested_signal_handler;
199
200 // Explicitly mask out SIGSEGV and SIGABRT from the nested signal handler. This
201 // should be the default but we definitely don't want these happening in our
202 // nested signal handler.
203 sigemptyset(&action.sa_mask);
204 sigaddset(&action.sa_mask, SIGSEGV);
205 sigaddset(&action.sa_mask, SIGABRT);
206
207 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
208#if !defined(__APPLE__) && !defined(__mips__)
209 action.sa_restorer = nullptr;
210#endif
211
Dave Allison0c2894b2014-08-29 12:06:16 -0700212 // Catch SIGSEGV and SIGABRT to invoke our nested handler
213 int e1 = sigaction(SIGSEGV, &action, &oldsegvaction);
214 int e2 = sigaction(SIGABRT, &action, &oldabortaction);
215 if (e1 != 0 || e2 != 0) {
216 LOG(ERROR) << "Unable to set up nested signal handler";
217 } else {
218 // Save the current state and call the handlers. If anything causes a signal
219 // our nested signal handler will be invoked and this will longjmp to the saved
220 // state.
221 if (setjmp(*self->GetNestedSignalState()) == 0) {
222 for (const auto& handler : other_handlers_) {
223 if (handler->Action(sig, info, context)) {
Dave Allison8be44cf2014-09-04 14:33:42 -0700224 // Restore the signal handlers, reinit the fault manager and return. Signal was
225 // handled.
226 sigaction(SIGSEGV, &oldsegvaction, nullptr);
227 sigaction(SIGABRT, &oldabortaction, nullptr);
228 fault_manager.Init();
229 return;
Dave Allison0c2894b2014-08-29 12:06:16 -0700230 }
231 }
232 } else {
233 LOG(ERROR) << "Nested signal detected - original signal being reported";
Dave Allisonb373e092014-02-20 16:06:36 -0800234 }
Dave Allison0c2894b2014-08-29 12:06:16 -0700235
236 // Restore the signal handlers.
237 sigaction(SIGSEGV, &oldsegvaction, nullptr);
238 sigaction(SIGABRT, &oldabortaction, nullptr);
Dave Allisonb373e092014-02-20 16:06:36 -0800239 }
Dave Allisondfd3b472014-07-16 16:04:32 -0700240
Dave Allison0c2894b2014-08-29 12:06:16 -0700241 // Now put the fault manager back in place.
242 fault_manager.Init();
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700243
Dave Allison8be44cf2014-09-04 14:33:42 -0700244 // Set a breakpoint in this function to catch unhandled signals.
245 art_sigsegv_fault();
Dave Allison0c2894b2014-08-29 12:06:16 -0700246
Dave Allison8be44cf2014-09-04 14:33:42 -0700247 // Pass this on to the next handler in the chain, or the default if none.
248 InvokeUserSignalHandler(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -0800249}
250
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700251void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
Andreas Gampe928f72b2014-09-09 19:53:48 -0700252 DCHECK(initialized_);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700253 if (generated_code) {
254 generated_code_handlers_.push_back(handler);
255 } else {
256 other_handlers_.push_back(handler);
257 }
258}
259
260void FaultManager::RemoveHandler(FaultHandler* handler) {
261 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
262 if (it != generated_code_handlers_.end()) {
263 generated_code_handlers_.erase(it);
264 return;
265 }
266 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
267 if (it2 != other_handlers_.end()) {
268 other_handlers_.erase(it);
269 return;
270 }
271 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
272}
Dave Allisonb373e092014-02-20 16:06:36 -0800273
274// This function is called within the signal handler. It checks that
275// the mutator_lock is held (shared). No annotalysis is done.
Dave Allison69dfe512014-07-11 17:11:58 +0000276bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800277 // We can only be running Java code in the current thread if it
278 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700279 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800280 Thread* thread = Thread::Current();
281 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700282 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800283 return false;
284 }
285
286 ThreadState state = thread->GetState();
287 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700288 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800289 return false;
290 }
291
292 // Current thread is runnable.
293 // Make sure it has the mutator lock.
294 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700295 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800296 return false;
297 }
298
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700299 mirror::ArtMethod* method_obj = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800300 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700301 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800302
303 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700304 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Dave Allisondfd3b472014-07-16 16:04:32 -0700305 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800306
307 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700308 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700309 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700310 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800311 return false;
312 }
313
314 // Verify that the potential method is indeed a method.
315 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800316 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700317 // TODO: Method might be not a heap address, and GetClass could fault.
Dave Allisonb373e092014-02-20 16:06:36 -0800318 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
319 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700320 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800321 return false;
322 }
323 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700324 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800325 return false;
326 }
327
328
329 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700330 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800331 return false;
332 }
333
334 // Now make sure the class is a mirror::ArtMethod.
335 if (!cls->IsArtMethodClass()) {
Dave Allison5cd33752014-04-15 15:57:58 -0700336 VLOG(signals) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800337 return false;
338 }
339
340 // We can be certain that this is a method now. Check if we have a GC map
341 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700342 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700343 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700344 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj);
Dave Allisonf9439142014-03-27 15:10:22 -0700345 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
Dave Allison5cd33752014-04-15 15:57:58 -0700346 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700347 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700348 uint32_t dexpc = method_obj->ToDexPc(return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700349 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700350 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
351}
352
353FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800354}
355
356//
357// Null pointer fault handler
358//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700359NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
360 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800361}
362
363//
364// Suspension fault handler
365//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700366SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
367 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800368}
369
370//
371// Stack overflow fault handler
372//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700373StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
374 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800375}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700376
377//
378// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
379//
380JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
381 manager_->AddHandler(this, false);
382}
383
384bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) {
385 // Make sure that we are in the generated code, but we may not have a dex pc.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700386 UNUSED(sig);
Dave Allison8ce6b902014-08-26 11:07:58 -0700387#ifdef TEST_NESTED_SIGNAL
388 bool in_generated_code = true;
389#else
390 bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false);
391#endif
392 if (in_generated_code) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700393 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
394 mirror::ArtMethod* method = nullptr;
395 uintptr_t return_pc = 0;
396 uintptr_t sp = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700397 Thread* self = Thread::Current();
Dave Allison8ce6b902014-08-26 11:07:58 -0700398
Dave Allison0c2894b2014-08-29 12:06:16 -0700399 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp);
400 // Inside of generated code, sp[0] is the method, so sp is the frame.
401 StackReference<mirror::ArtMethod>* frame =
402 reinterpret_cast<StackReference<mirror::ArtMethod>*>(sp);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700403 self->SetTopOfStack(frame);
Dave Allison8ce6b902014-08-26 11:07:58 -0700404#ifdef TEST_NESTED_SIGNAL
Dave Allison0c2894b2014-08-29 12:06:16 -0700405 // To test the nested signal handler we raise a signal here. This will cause the
406 // nested signal handler to be called and perform a longjmp back to the setjmp
407 // above.
408 abort();
Dave Allison8ce6b902014-08-26 11:07:58 -0700409#endif
Dave Allison0c2894b2014-08-29 12:06:16 -0700410 self->DumpJavaStack(LOG(ERROR));
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700411 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700412
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700413 return false; // Return false since we want to propagate the fault to the main signal handler.
414}
415
Dave Allisonb373e092014-02-20 16:06:36 -0800416} // namespace art
417