blob: 7f738bf5a95e351c8f1632fabb8beabfcfeda713 [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"
Josh Gao143f61c2017-04-17 20:10:29 -070024#include "base/safe_copy.h"
Andreas Gampe928f72b2014-09-09 19:53:48 -070025#include "base/stl_util.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070026#include "mirror/class.h"
Josh Gao143f61c2017-04-17 20:10:29 -070027#include "mirror/object_reference.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010028#include "oat_quick_method_header.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070029#include "sigchain.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070030#include "thread-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080031#include "verify_object-inl.h"
32
33namespace art {
34// Static fault manger object accessed by signal handler.
35FaultManager fault_manager;
36
Narayan Kamathc37769b2015-06-17 09:49:40 +010037extern "C" __attribute__((visibility("default"))) void art_sigsegv_fault() {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070038 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART.
39 VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler.";
40}
Dave Allisonf4b80bc2014-05-14 15:41:25 -070041
Dave Allisonb373e092014-02-20 16:06:36 -080042// Signal handler called on SIGSEGV.
Josh Gao85a78cf2017-03-20 16:26:42 -070043static bool art_fault_handler(int sig, siginfo_t* info, void* context) {
44 return fault_manager.HandleFault(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -080045}
46
Josh Gao143f61c2017-04-17 20:10:29 -070047#if defined(__linux__)
48
49// Change to verify the safe implementations against the original ones.
50constexpr bool kVerifySafeImpls = false;
51
52// Provide implementations of ArtMethod::GetDeclaringClass and VerifyClassClass that use SafeCopy
53// to safely dereference pointers which are potentially garbage.
54// Only available on Linux due to availability of SafeCopy.
55
56static mirror::Class* SafeGetDeclaringClass(ArtMethod* method)
57 REQUIRES_SHARED(Locks::mutator_lock_) {
58 char* method_declaring_class =
59 reinterpret_cast<char*>(method) + ArtMethod::DeclaringClassOffset().SizeValue();
60
61 // ArtMethod::declaring_class_ is a GcRoot<mirror::Class>.
62 // Read it out into as a CompressedReference directly for simplicity's sake.
63 mirror::CompressedReference<mirror::Class> cls;
64 ssize_t rc = SafeCopy(&cls, method_declaring_class, sizeof(cls));
65 CHECK_NE(-1, rc);
66
67 if (kVerifySafeImpls) {
68 mirror::Class* actual_class = method->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
69 CHECK_EQ(actual_class, cls.AsMirrorPtr());
70 }
71
72 if (rc != sizeof(cls)) {
73 return nullptr;
74 }
75
76 return cls.AsMirrorPtr();
77}
78
79static mirror::Class* SafeGetClass(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
80 char* obj_cls = reinterpret_cast<char*>(obj) + mirror::Object::ClassOffset().SizeValue();
81
Nicolas Geoffrayd4ac7672017-04-20 12:50:13 +010082 mirror::HeapReference<mirror::Class> cls =
83 mirror::HeapReference<mirror::Class>::FromMirrorPtr(nullptr);
Josh Gao143f61c2017-04-17 20:10:29 -070084 ssize_t rc = SafeCopy(&cls, obj_cls, sizeof(cls));
85 CHECK_NE(-1, rc);
86
87 if (kVerifySafeImpls) {
88 mirror::Class* actual_class = obj->GetClass<kVerifyNone>();
89 CHECK_EQ(actual_class, cls.AsMirrorPtr());
90 }
91
92 if (rc != sizeof(cls)) {
93 return nullptr;
94 }
95
96 return cls.AsMirrorPtr();
97}
98
99static bool SafeVerifyClassClass(mirror::Class* cls) REQUIRES_SHARED(Locks::mutator_lock_) {
100 mirror::Class* c_c = SafeGetClass(cls);
101 bool result = c_c != nullptr && c_c == SafeGetClass(c_c);
102
103 if (kVerifySafeImpls) {
104 CHECK_EQ(VerifyClassClass(cls), result);
105 }
106
107 return result;
108}
109
110#else
111
Josh Gao77c14152017-04-19 13:20:19 -0700112static mirror::Class* SafeGetDeclaringClass(ArtMethod* method_obj)
113 REQUIRES_SHARED(Locks::mutator_lock_) {
Josh Gao143f61c2017-04-17 20:10:29 -0700114 return method_obj->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
115}
116
Josh Gao77c14152017-04-19 13:20:19 -0700117static bool SafeVerifyClassClass(mirror::Class* cls) REQUIRES_SHARED(Locks::mutator_lock_) {
Josh Gao143f61c2017-04-17 20:10:29 -0700118 return VerifyClassClass(cls);
119}
120#endif
121
122
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700123FaultManager::FaultManager() : initialized_(false) {
Dave Allisonb373e092014-02-20 16:06:36 -0800124 sigaction(SIGSEGV, nullptr, &oldaction_);
125}
126
127FaultManager::~FaultManager() {
Dave Allisonb373e092014-02-20 16:06:36 -0800128}
129
130void FaultManager::Init() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700131 CHECK(!initialized_);
Josh Gao85a78cf2017-03-20 16:26:42 -0700132 AddSpecialSignalHandlerFn(SIGSEGV, art_fault_handler);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700133 initialized_ = true;
134}
135
Andreas Gampe928f72b2014-09-09 19:53:48 -0700136void FaultManager::Release() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700137 if (initialized_) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700138 RemoveSpecialSignalHandlerFn(SIGSEGV, art_fault_handler);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700139 initialized_ = false;
140 }
Dave Allisonb373e092014-02-20 16:06:36 -0800141}
142
Andreas Gampe928f72b2014-09-09 19:53:48 -0700143void FaultManager::Shutdown() {
144 if (initialized_) {
145 Release();
146
147 // Free all handlers.
148 STLDeleteElements(&generated_code_handlers_);
149 STLDeleteElements(&other_handlers_);
150 }
151}
152
jgu211376bdf2016-01-18 09:12:33 -0500153bool FaultManager::HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context) {
Andreas Gamped14b73d2016-04-18 17:15:42 -0700154 if (other_handlers_.empty()) {
155 return false;
156 }
157
Dave Allison0c2894b2014-08-29 12:06:16 -0700158 Thread* self = Thread::Current();
159
jgu211376bdf2016-01-18 09:12:33 -0500160 DCHECK(self != nullptr);
161 DCHECK(Runtime::Current() != nullptr);
162 DCHECK(Runtime::Current()->IsStarted());
Josh Gaoefd20cb2017-02-28 16:53:59 -0800163 for (const auto& handler : other_handlers_) {
164 if (handler->Action(sig, info, context)) {
165 return true;
Ian Rogersad11e7a2014-11-11 16:55:11 -0800166 }
167 }
jgu211376bdf2016-01-18 09:12:33 -0500168 return false;
169}
170
Josh Gao85a78cf2017-03-20 16:26:42 -0700171bool FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
172 VLOG(signals) << "Handling fault";
Josh Gaoefd20cb2017-02-28 16:53:59 -0800173
174#ifdef TEST_NESTED_SIGNAL
Josh Gao85a78cf2017-03-20 16:26:42 -0700175 // Simulate a crash in a handler.
176 raise(SIGSEGV);
Josh Gaoefd20cb2017-02-28 16:53:59 -0800177#endif
178
Josh Gao85a78cf2017-03-20 16:26:42 -0700179 if (IsInGeneratedCode(info, context, true)) {
180 VLOG(signals) << "in generated code, looking for handler";
181 for (const auto& handler : generated_code_handlers_) {
182 VLOG(signals) << "invoking Action on handler " << handler;
183 if (handler->Action(sig, info, context)) {
184 // We have handled a signal so it's time to return from the
185 // signal handler to the appropriate place.
186 return true;
Josh Gaoefd20cb2017-02-28 16:53:59 -0800187 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700188 }
Josh Gaoefd20cb2017-02-28 16:53:59 -0800189
Josh Gao85a78cf2017-03-20 16:26:42 -0700190 // We hit a signal we didn't handle. This might be something for which
191 // we can give more information about so call all registered handlers to
192 // see if it is.
193 if (HandleFaultByOtherHandlers(sig, info, context)) {
194 return true;
jgu211376bdf2016-01-18 09:12:33 -0500195 }
196 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700197
Dave Allison8be44cf2014-09-04 14:33:42 -0700198 // Set a breakpoint in this function to catch unhandled signals.
199 art_sigsegv_fault();
Josh Gao85a78cf2017-03-20 16:26:42 -0700200 return false;
Dave Allisonb373e092014-02-20 16:06:36 -0800201}
202
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700203void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
Andreas Gampe928f72b2014-09-09 19:53:48 -0700204 DCHECK(initialized_);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700205 if (generated_code) {
206 generated_code_handlers_.push_back(handler);
207 } else {
208 other_handlers_.push_back(handler);
209 }
210}
211
212void FaultManager::RemoveHandler(FaultHandler* handler) {
213 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
214 if (it != generated_code_handlers_.end()) {
215 generated_code_handlers_.erase(it);
216 return;
217 }
218 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
219 if (it2 != other_handlers_.end()) {
220 other_handlers_.erase(it);
221 return;
222 }
223 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
224}
Dave Allisonb373e092014-02-20 16:06:36 -0800225
226// This function is called within the signal handler. It checks that
227// the mutator_lock is held (shared). No annotalysis is done.
Dave Allison69dfe512014-07-11 17:11:58 +0000228bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800229 // We can only be running Java code in the current thread if it
230 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700231 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800232 Thread* thread = Thread::Current();
233 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700234 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800235 return false;
236 }
237
238 ThreadState state = thread->GetState();
239 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700240 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800241 return false;
242 }
243
244 // Current thread is runnable.
245 // Make sure it has the mutator lock.
246 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700247 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800248 return false;
249 }
250
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000251 ArtMethod* method_obj = nullptr;
Dave Allisonb373e092014-02-20 16:06:36 -0800252 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700253 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800254
255 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700256 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Dave Allisondfd3b472014-07-16 16:04:32 -0700257 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800258
259 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700260 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 // TODO: Check linear alloc and image.
Andreas Gampe542451c2016-07-26 09:02:02 -0700262 DCHECK_ALIGNED(ArtMethod::Size(kRuntimePointerSize), sizeof(void*))
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000263 << "ArtMethod is not pointer aligned";
264 if (method_obj == nullptr || !IsAligned<sizeof(void*)>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700265 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800266 return false;
267 }
268
269 // Verify that the potential method is indeed a method.
270 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800271 // Check that the class pointer inside the object is not null and is aligned.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800272 // No read barrier because method_obj may not be a real object.
Josh Gao143f61c2017-04-17 20:10:29 -0700273 mirror::Class* cls = SafeGetDeclaringClass(method_obj);
Dave Allisonb373e092014-02-20 16:06:36 -0800274 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700275 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800276 return false;
277 }
Josh Gao143f61c2017-04-17 20:10:29 -0700278
Dave Allisonb373e092014-02-20 16:06:36 -0800279 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700280 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800281 return false;
282 }
283
Josh Gao143f61c2017-04-17 20:10:29 -0700284 if (!SafeVerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700285 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800286 return false;
287 }
288
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100289 const OatQuickMethodHeader* method_header = method_obj->GetOatQuickMethodHeader(return_pc);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100290
Dave Allisonb373e092014-02-20 16:06:36 -0800291 // We can be certain that this is a method now. Check if we have a GC map
292 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700293 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700294 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100295 uint32_t sought_offset = return_pc -
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100296 reinterpret_cast<uintptr_t>(method_header->GetEntryPoint());
Dave Allison5cd33752014-04-15 15:57:58 -0700297 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700298 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100299 uint32_t dexpc = method_header->ToDexPc(method_obj, return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700300 VLOG(signals) << "dexpc: " << dexpc;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700301 return !check_dex_pc || dexpc != DexFile::kDexNoIndex;
302}
303
304FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800305}
306
307//
308// Null pointer fault handler
309//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700310NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
311 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800312}
313
314//
315// Suspension fault handler
316//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700317SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
318 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800319}
320
321//
322// Stack overflow fault handler
323//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700324StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
325 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800326}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700327
328//
329// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
330//
331JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
332 manager_->AddHandler(this, false);
333}
334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100335bool JavaStackTraceHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* siginfo, void* context) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700336 // Make sure that we are in the generated code, but we may not have a dex pc.
Dave Allison8ce6b902014-08-26 11:07:58 -0700337 bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false);
Dave Allison8ce6b902014-08-26 11:07:58 -0700338 if (in_generated_code) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700339 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 ArtMethod* method = nullptr;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700341 uintptr_t return_pc = 0;
342 uintptr_t sp = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700343 Thread* self = Thread::Current();
Dave Allison8ce6b902014-08-26 11:07:58 -0700344
Dave Allison0c2894b2014-08-29 12:06:16 -0700345 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp);
346 // Inside of generated code, sp[0] is the method, so sp is the frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 self->SetTopOfStack(reinterpret_cast<ArtMethod**>(sp));
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700348 self->DumpJavaStack(LOG_STREAM(ERROR));
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700349 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700350
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700351 return false; // Return false since we want to propagate the fault to the main signal handler.
352}
353
Dave Allisonb373e092014-02-20 16:06:36 -0800354} // namespace art