blob: 4220250c38b670a2d1604630161a7ba0997e8e58 [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>
Mathieu Chartiere401d142015-04-22 13:56:20 -070022
23#include "art_method-inl.h"
Andreas Gampe928f72b2014-09-09 19:53:48 -070024#include "base/stl_util.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070025#include "mirror/class.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010026#include "oat_quick_method_header.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070027#include "sigchain.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080029#include "verify_object-inl.h"
30
31namespace art {
32// Static fault manger object accessed by signal handler.
33FaultManager fault_manager;
34
Narayan Kamathc37769b2015-06-17 09:49:40 +010035extern "C" __attribute__((visibility("default"))) void art_sigsegv_fault() {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070036 // 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}
Dave Allisonf4b80bc2014-05-14 15:41:25 -070039
Dave Allisonb373e092014-02-20 16:06:36 -080040// Signal handler called on SIGSEGV.
Josh Gao85a78cf2017-03-20 16:26:42 -070041static bool art_fault_handler(int sig, siginfo_t* info, void* context) {
42 return fault_manager.HandleFault(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -080043}
44
Dave Allison1f8ef6f2014-08-20 17:38:41 -070045FaultManager::FaultManager() : initialized_(false) {
Dave Allisonb373e092014-02-20 16:06:36 -080046 sigaction(SIGSEGV, nullptr, &oldaction_);
47}
48
49FaultManager::~FaultManager() {
Dave Allisonb373e092014-02-20 16:06:36 -080050}
51
52void FaultManager::Init() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -070053 CHECK(!initialized_);
Josh Gao85a78cf2017-03-20 16:26:42 -070054 AddSpecialSignalHandlerFn(SIGSEGV, art_fault_handler);
Dave Allison1f8ef6f2014-08-20 17:38:41 -070055 initialized_ = true;
56}
57
Andreas Gampe928f72b2014-09-09 19:53:48 -070058void FaultManager::Release() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -070059 if (initialized_) {
Josh Gao85a78cf2017-03-20 16:26:42 -070060 RemoveSpecialSignalHandlerFn(SIGSEGV, art_fault_handler);
Dave Allison1f8ef6f2014-08-20 17:38:41 -070061 initialized_ = false;
62 }
Dave Allisonb373e092014-02-20 16:06:36 -080063}
64
Andreas Gampe928f72b2014-09-09 19:53:48 -070065void FaultManager::Shutdown() {
66 if (initialized_) {
67 Release();
68
69 // Free all handlers.
70 STLDeleteElements(&generated_code_handlers_);
71 STLDeleteElements(&other_handlers_);
72 }
73}
74
jgu211376bdf2016-01-18 09:12:33 -050075bool FaultManager::HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context) {
Andreas Gamped14b73d2016-04-18 17:15:42 -070076 if (other_handlers_.empty()) {
77 return false;
78 }
79
Dave Allison0c2894b2014-08-29 12:06:16 -070080 Thread* self = Thread::Current();
81
jgu211376bdf2016-01-18 09:12:33 -050082 DCHECK(self != nullptr);
83 DCHECK(Runtime::Current() != nullptr);
84 DCHECK(Runtime::Current()->IsStarted());
Josh Gaoefd20cb2017-02-28 16:53:59 -080085 for (const auto& handler : other_handlers_) {
86 if (handler->Action(sig, info, context)) {
87 return true;
Ian Rogersad11e7a2014-11-11 16:55:11 -080088 }
89 }
jgu211376bdf2016-01-18 09:12:33 -050090 return false;
91}
92
Josh Gao85a78cf2017-03-20 16:26:42 -070093bool FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
94 VLOG(signals) << "Handling fault";
Josh Gaoefd20cb2017-02-28 16:53:59 -080095
96#ifdef TEST_NESTED_SIGNAL
Josh Gao85a78cf2017-03-20 16:26:42 -070097 // Simulate a crash in a handler.
98 raise(SIGSEGV);
Josh Gaoefd20cb2017-02-28 16:53:59 -080099#endif
100
Josh Gao85a78cf2017-03-20 16:26:42 -0700101 if (IsInGeneratedCode(info, context, true)) {
102 VLOG(signals) << "in generated code, looking for handler";
103 for (const auto& handler : generated_code_handlers_) {
104 VLOG(signals) << "invoking Action on handler " << handler;
105 if (handler->Action(sig, info, context)) {
106 // We have handled a signal so it's time to return from the
107 // signal handler to the appropriate place.
108 return true;
Josh Gaoefd20cb2017-02-28 16:53:59 -0800109 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700110 }
Josh Gaoefd20cb2017-02-28 16:53:59 -0800111
Josh Gao85a78cf2017-03-20 16:26:42 -0700112 // We hit a signal we didn't handle. This might be something for which
113 // we can give more information about so call all registered handlers to
114 // see if it is.
115 if (HandleFaultByOtherHandlers(sig, info, context)) {
116 return true;
jgu211376bdf2016-01-18 09:12:33 -0500117 }
118 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700119
Dave Allison8be44cf2014-09-04 14:33:42 -0700120 // Set a breakpoint in this function to catch unhandled signals.
121 art_sigsegv_fault();
Josh Gao85a78cf2017-03-20 16:26:42 -0700122 return false;
Dave Allisonb373e092014-02-20 16:06:36 -0800123}
124
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700125void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
Andreas Gampe928f72b2014-09-09 19:53:48 -0700126 DCHECK(initialized_);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700127 if (generated_code) {
128 generated_code_handlers_.push_back(handler);
129 } else {
130 other_handlers_.push_back(handler);
131 }
132}
133
134void FaultManager::RemoveHandler(FaultHandler* handler) {
135 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
136 if (it != generated_code_handlers_.end()) {
137 generated_code_handlers_.erase(it);
138 return;
139 }
140 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
141 if (it2 != other_handlers_.end()) {
142 other_handlers_.erase(it);
143 return;
144 }
145 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
146}
Dave Allisonb373e092014-02-20 16:06:36 -0800147
148// This function is called within the signal handler. It checks that
149// the mutator_lock is held (shared). No annotalysis is done.
Dave Allison69dfe512014-07-11 17:11:58 +0000150bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800151 // We can only be running Java code in the current thread if it
152 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700153 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800154 Thread* thread = Thread::Current();
155 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700156 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800157 return false;
158 }
159
160 ThreadState state = thread->GetState();
161 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700162 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800163 return false;
164 }
165
166 // Current thread is runnable.
167 // Make sure it has the mutator lock.
168 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700169 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800170 return false;
171 }
172
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000173 ArtMethod* method_obj = nullptr;
Dave Allisonb373e092014-02-20 16:06:36 -0800174 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700175 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800176
177 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700178 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Dave Allisondfd3b472014-07-16 16:04:32 -0700179 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800180
181 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700182 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183 // TODO: Check linear alloc and image.
Andreas Gampe542451c2016-07-26 09:02:02 -0700184 DCHECK_ALIGNED(ArtMethod::Size(kRuntimePointerSize), sizeof(void*))
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000185 << "ArtMethod is not pointer aligned";
186 if (method_obj == nullptr || !IsAligned<sizeof(void*)>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700187 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800188 return false;
189 }
190
191 // Verify that the potential method is indeed a method.
192 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800193 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700194 // TODO: Method might be not a heap address, and GetClass could fault.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800195 // No read barrier because method_obj may not be a real object.
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800196 mirror::Class* cls = method_obj->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Dave Allisonb373e092014-02-20 16:06:36 -0800197 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700198 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800199 return false;
200 }
201 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700202 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800203 return false;
204 }
205
206
207 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700208 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800209 return false;
210 }
211
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100212 const OatQuickMethodHeader* method_header = method_obj->GetOatQuickMethodHeader(return_pc);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100213
Dave Allisonb373e092014-02-20 16:06:36 -0800214 // We can be certain that this is a method now. Check if we have a GC map
215 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700216 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700217 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100218 uint32_t sought_offset = return_pc -
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100219 reinterpret_cast<uintptr_t>(method_header->GetEntryPoint());
Dave Allison5cd33752014-04-15 15:57:58 -0700220 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700221 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100222 uint32_t dexpc = method_header->ToDexPc(method_obj, return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700223 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700224 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
225}
226
227FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800228}
229
230//
231// Null pointer fault handler
232//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700233NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
234 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800235}
236
237//
238// Suspension fault handler
239//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700240SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
241 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800242}
243
244//
245// Stack overflow fault handler
246//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700247StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
248 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800249}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700250
251//
252// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
253//
254JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
255 manager_->AddHandler(this, false);
256}
257
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100258bool JavaStackTraceHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* siginfo, void* context) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700259 // Make sure that we are in the generated code, but we may not have a dex pc.
Dave Allison8ce6b902014-08-26 11:07:58 -0700260 bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false);
Dave Allison8ce6b902014-08-26 11:07:58 -0700261 if (in_generated_code) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700262 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 ArtMethod* method = nullptr;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700264 uintptr_t return_pc = 0;
265 uintptr_t sp = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700266 Thread* self = Thread::Current();
Dave Allison8ce6b902014-08-26 11:07:58 -0700267
Dave Allison0c2894b2014-08-29 12:06:16 -0700268 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp);
269 // Inside of generated code, sp[0] is the method, so sp is the frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270 self->SetTopOfStack(reinterpret_cast<ArtMethod**>(sp));
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700271 self->DumpJavaStack(LOG_STREAM(ERROR));
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700272 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700273
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700274 return false; // Return false since we want to propagate the fault to the main signal handler.
275}
276
Dave Allisonb373e092014-02-20 16:06:36 -0800277} // namespace art