blob: c457f120a803a42e260bb19afc66496839548345 [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"
18#include <sys/mman.h>
19#include <sys/ucontext.h>
20#include "base/macros.h"
21#include "globals.h"
22#include "base/logging.h"
23#include "base/hex_dump.h"
24#include "thread.h"
25#include "mirror/art_method-inl.h"
26#include "mirror/class-inl.h"
27#include "mirror/dex_cache.h"
28#include "mirror/object_array-inl.h"
29#include "mirror/object-inl.h"
30#include "object_utils.h"
31#include "scoped_thread_state_change.h"
32#include "verify_object-inl.h"
33
34namespace art {
35// Static fault manger object accessed by signal handler.
36FaultManager fault_manager;
37
Dave Allison05266432014-05-05 13:17:37 -070038extern "C" {
39void art_sigsegv_fault() {
40 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART.
41 LOG(ERROR)<< "Caught unknown SIGSEGV in ART fault handler";
42}
43}
44
Dave Allisonb373e092014-02-20 16:06:36 -080045// Signal handler called on SIGSEGV.
46static void art_fault_handler(int sig, siginfo_t* info, void* context) {
47 fault_manager.HandleFault(sig, info, context);
48}
49
50FaultManager::FaultManager() {
51 sigaction(SIGSEGV, nullptr, &oldaction_);
52}
53
54FaultManager::~FaultManager() {
55 sigaction(SIGSEGV, &oldaction_, nullptr); // Restore old handler.
56}
57
58void 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;
Narayan Kamath15245bc2014-03-14 12:53:43 +000063#if !defined(__mips__)
Dave Allisonb373e092014-02-20 16:06:36 -080064 action.sa_restorer = nullptr;
Narayan Kamath15245bc2014-03-14 12:53:43 +000065#endif
Dave Allisonb373e092014-02-20 16:06:36 -080066 sigaction(SIGSEGV, &action, &oldaction_);
67}
68
69void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070070 // BE CAREFUL ALLOCATING HERE INCLUDING USING LOG(...)
71 //
72 // If malloc calls abort, it will be holding its lock.
73 // If the handler tries to call malloc, it will deadlock.
74 VLOG(signals) << "Handling fault";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070075 if (IsInGeneratedCode(context, true)) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070076 VLOG(signals) << "in generated code, looking for handler";
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070077 for (const auto& handler : generated_code_handlers_) {
Brian Carlstrom4d466a82014-05-08 19:05:29 -070078 VLOG(signals) << "invoking Action on handler " << handler;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070079 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080080 return;
81 }
82 }
83 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070084 for (const auto& handler : other_handlers_) {
85 if (handler->Action(sig, info, context)) {
Dave Allisonb373e092014-02-20 16:06:36 -080086 return;
87 }
88 }
Dave Allison05266432014-05-05 13:17:37 -070089
90 // Allow the user to catch this problem with a simple breakpoint in art_sigsegv_fault.
91 art_sigsegv_fault();
92
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070093 oldaction_.sa_sigaction(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -080094}
95
Mathieu Chartierc751fdc2014-03-30 15:25:44 -070096void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
97 if (generated_code) {
98 generated_code_handlers_.push_back(handler);
99 } else {
100 other_handlers_.push_back(handler);
101 }
102}
103
104void FaultManager::RemoveHandler(FaultHandler* handler) {
105 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
106 if (it != generated_code_handlers_.end()) {
107 generated_code_handlers_.erase(it);
108 return;
109 }
110 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
111 if (it2 != other_handlers_.end()) {
112 other_handlers_.erase(it);
113 return;
114 }
115 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
116}
Dave Allisonb373e092014-02-20 16:06:36 -0800117
118// This function is called within the signal handler. It checks that
119// the mutator_lock is held (shared). No annotalysis is done.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700120bool FaultManager::IsInGeneratedCode(void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800121 // We can only be running Java code in the current thread if it
122 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700123 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800124 Thread* thread = Thread::Current();
125 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700126 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800127 return false;
128 }
129
130 ThreadState state = thread->GetState();
131 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700132 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800133 return false;
134 }
135
136 // Current thread is runnable.
137 // Make sure it has the mutator lock.
138 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700139 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800140 return false;
141 }
142
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700143 mirror::ArtMethod* method_obj = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800144 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700145 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800146
147 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700148 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
149 GetMethodAndReturnPCAndSP(context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800150
151 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700152 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700153 if (method_obj == 0 || !IsAligned<kObjectAlignment>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700154 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800155 return false;
156 }
157
158 // Verify that the potential method is indeed a method.
159 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800160 // Check that the class pointer inside the object is not null and is aligned.
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700161 // TODO: Method might be not a heap address, and GetClass could fault.
Dave Allisonb373e092014-02-20 16:06:36 -0800162 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
163 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700164 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800165 return false;
166 }
167 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700168 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800169 return false;
170 }
171
172
173 if (!VerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700174 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800175 return false;
176 }
177
178 // Now make sure the class is a mirror::ArtMethod.
179 if (!cls->IsArtMethodClass()) {
Dave Allison5cd33752014-04-15 15:57:58 -0700180 VLOG(signals) << "not a method";
Dave Allisonb373e092014-02-20 16:06:36 -0800181 return false;
182 }
183
184 // We can be certain that this is a method now. Check if we have a GC map
185 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700186 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700187 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700188 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(method_obj);
Dave Allisonf9439142014-03-27 15:10:22 -0700189 uint32_t sought_offset = return_pc - reinterpret_cast<uintptr_t>(code);
Dave Allison5cd33752014-04-15 15:57:58 -0700190 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700191 }
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700192 uint32_t dexpc = method_obj->ToDexPc(return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700193 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700194 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
195}
196
197FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800198}
199
200//
201// Null pointer fault handler
202//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700203NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
204 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800205}
206
207//
208// Suspension fault handler
209//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700210SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
211 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800212}
213
214//
215// Stack overflow fault handler
216//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700217StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
218 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800219}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700220
221//
222// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
223//
224JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
225 manager_->AddHandler(this, false);
226}
227
228bool JavaStackTraceHandler::Action(int sig, siginfo_t* siginfo, void* context) {
229 // Make sure that we are in the generated code, but we may not have a dex pc.
230 if (manager_->IsInGeneratedCode(context, false)) {
231 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
232 mirror::ArtMethod* method = nullptr;
233 uintptr_t return_pc = 0;
234 uintptr_t sp = 0;
235 manager_->GetMethodAndReturnPCAndSP(context, &method, &return_pc, &sp);
236 Thread* self = Thread::Current();
237 // Inside of generated code, sp[0] is the method, so sp is the frame.
238 mirror::ArtMethod** frame = reinterpret_cast<mirror::ArtMethod**>(sp);
239 self->SetTopOfStack(frame, 0); // Since we don't necessarily have a dex pc, pass in 0.
240 self->DumpJavaStack(LOG(ERROR));
241 }
242 return false; // Return false since we want to propagate the fault to the main signal handler.
243}
244
Dave Allisonb373e092014-02-20 16:06:36 -0800245} // namespace art
246