blob: 6399c0d321ccb6ec473203c9046755efd0e4d473 [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
38// Signal handler called on SIGSEGV.
39static void art_fault_handler(int sig, siginfo_t* info, void* context) {
40 fault_manager.HandleFault(sig, info, context);
41}
42
43FaultManager::FaultManager() {
44 sigaction(SIGSEGV, nullptr, &oldaction_);
45}
46
47FaultManager::~FaultManager() {
48 sigaction(SIGSEGV, &oldaction_, nullptr); // Restore old handler.
49}
50
51void FaultManager::Init() {
52 struct sigaction action;
53 action.sa_sigaction = art_fault_handler;
54 sigemptyset(&action.sa_mask);
55 action.sa_flags = SA_SIGINFO | SA_ONSTACK;
56 action.sa_restorer = nullptr;
57 sigaction(SIGSEGV, &action, &oldaction_);
58}
59
60void FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
61 bool handled = false;
62 if (IsInGeneratedCode(context)) {
63 for (auto& handler : handlers_) {
64 handled = handler->Action(sig, info, context);
65 if (handled) {
66 return;
67 }
68 }
69 }
70
71 if (!handled) {
72 LOG(INFO)<< "Caught unknown SIGSEGV in ART fault handler";
73 oldaction_.sa_sigaction(sig, info, context);
74 }
75}
76
77void FaultManager::AddHandler(FaultHandler* handler) {
78 handlers_.push_back(handler);
79}
80
81void FaultManager::RemoveHandler(FaultHandler* handler) {
82 for (Handlers::iterator i = handlers_.begin(); i != handlers_.end(); ++i) {
83 FaultHandler* h = *i;
84 if (h == handler) {
85 handlers_.erase(i);
86 return;
87 }
88 }
89}
90
91
92// This function is called within the signal handler. It checks that
93// the mutator_lock is held (shared). No annotalysis is done.
94bool FaultManager::IsInGeneratedCode(void *context) {
95 // We can only be running Java code in the current thread if it
96 // is in Runnable state.
97 Thread* thread = Thread::Current();
98 if (thread == nullptr) {
99 return false;
100 }
101
102 ThreadState state = thread->GetState();
103 if (state != kRunnable) {
104 return false;
105 }
106
107 // Current thread is runnable.
108 // Make sure it has the mutator lock.
109 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
110 return false;
111 }
112
113 uintptr_t potential_method = 0;
114 uintptr_t return_pc = 0;
115
116 // Get the architecture specific method address and return address. These
117 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.cc
118 GetMethodAndReturnPC(context, /*out*/potential_method, /*out*/return_pc);
119
120 // If we don't have a potential method, we're outta here.
121 if (potential_method == 0) {
122 return false;
123 }
124
125 // Verify that the potential method is indeed a method.
126 // TODO: check the GC maps to make sure it's an object.
127
128 mirror::Object* method_obj =
129 reinterpret_cast<mirror::Object*>(potential_method);
130
131 // Check that the class pointer inside the object is not null and is aligned.
132 mirror::Class* cls = method_obj->GetClass<kVerifyNone>();
133 if (cls == nullptr) {
134 return false;
135 }
136 if (!IsAligned<kObjectAlignment>(cls)) {
137 return false;
138 }
139
140
141 if (!VerifyClassClass(cls)) {
142 return false;
143 }
144
145 // Now make sure the class is a mirror::ArtMethod.
146 if (!cls->IsArtMethodClass()) {
147 return false;
148 }
149
150 // We can be certain that this is a method now. Check if we have a GC map
151 // at the return PC address.
152 mirror::ArtMethod* method =
153 reinterpret_cast<mirror::ArtMethod*>(potential_method);
154 return method->ToDexPc(return_pc, false) != DexFile::kDexNoIndex;
155}
156
157//
158// Null pointer fault handler
159//
160
161NullPointerHandler::NullPointerHandler(FaultManager* manager) {
162 manager->AddHandler(this);
163}
164
165//
166// Suspension fault handler
167//
168
169SuspensionHandler::SuspensionHandler(FaultManager* manager) {
170 manager->AddHandler(this);
171}
172
173//
174// Stack overflow fault handler
175//
176
177StackOverflowHandler::StackOverflowHandler(FaultManager* manager) {
178 manager->AddHandler(this);
179}
180} // namespace art
181