blob: 49c2a15e862d0e50566facea505b8eb0f685cad6 [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 Gampe170331f2017-12-07 18:41:03 -080024#include "base/logging.h" // For VLOG
Josh Gao143f61c2017-04-17 20:10:29 -070025#include "base/safe_copy.h"
Andreas Gampe928f72b2014-09-09 19:53:48 -070026#include "base/stl_util.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file_types.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "mirror/class.h"
Josh Gao143f61c2017-04-17 20:10:29 -070029#include "mirror/object_reference.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010030#include "oat_quick_method_header.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070031#include "sigchain.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070032#include "thread-current-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080033#include "verify_object-inl.h"
34
35namespace art {
36// Static fault manger object accessed by signal handler.
37FaultManager fault_manager;
38
Narayan Kamathc37769b2015-06-17 09:49:40 +010039extern "C" __attribute__((visibility("default"))) void art_sigsegv_fault() {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070040 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART.
41 VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler.";
42}
Dave Allisonf4b80bc2014-05-14 15:41:25 -070043
Dave Allisonb373e092014-02-20 16:06:36 -080044// Signal handler called on SIGSEGV.
Josh Gao85a78cf2017-03-20 16:26:42 -070045static bool art_fault_handler(int sig, siginfo_t* info, void* context) {
46 return fault_manager.HandleFault(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -080047}
48
Josh Gao143f61c2017-04-17 20:10:29 -070049#if defined(__linux__)
50
51// Change to verify the safe implementations against the original ones.
52constexpr bool kVerifySafeImpls = false;
53
54// Provide implementations of ArtMethod::GetDeclaringClass and VerifyClassClass that use SafeCopy
55// to safely dereference pointers which are potentially garbage.
56// Only available on Linux due to availability of SafeCopy.
57
58static mirror::Class* SafeGetDeclaringClass(ArtMethod* method)
59 REQUIRES_SHARED(Locks::mutator_lock_) {
60 char* method_declaring_class =
61 reinterpret_cast<char*>(method) + ArtMethod::DeclaringClassOffset().SizeValue();
62
63 // ArtMethod::declaring_class_ is a GcRoot<mirror::Class>.
64 // Read it out into as a CompressedReference directly for simplicity's sake.
65 mirror::CompressedReference<mirror::Class> cls;
66 ssize_t rc = SafeCopy(&cls, method_declaring_class, sizeof(cls));
67 CHECK_NE(-1, rc);
68
69 if (kVerifySafeImpls) {
70 mirror::Class* actual_class = method->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
71 CHECK_EQ(actual_class, cls.AsMirrorPtr());
72 }
73
74 if (rc != sizeof(cls)) {
75 return nullptr;
76 }
77
78 return cls.AsMirrorPtr();
79}
80
81static mirror::Class* SafeGetClass(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
82 char* obj_cls = reinterpret_cast<char*>(obj) + mirror::Object::ClassOffset().SizeValue();
83
Mathieu Chartierad8ebb32017-08-09 20:13:18 -070084 mirror::HeapReference<mirror::Class> cls;
Josh Gao143f61c2017-04-17 20:10:29 -070085 ssize_t rc = SafeCopy(&cls, obj_cls, sizeof(cls));
86 CHECK_NE(-1, rc);
87
88 if (kVerifySafeImpls) {
89 mirror::Class* actual_class = obj->GetClass<kVerifyNone>();
90 CHECK_EQ(actual_class, cls.AsMirrorPtr());
91 }
92
93 if (rc != sizeof(cls)) {
94 return nullptr;
95 }
96
97 return cls.AsMirrorPtr();
98}
99
100static bool SafeVerifyClassClass(mirror::Class* cls) REQUIRES_SHARED(Locks::mutator_lock_) {
101 mirror::Class* c_c = SafeGetClass(cls);
102 bool result = c_c != nullptr && c_c == SafeGetClass(c_c);
103
104 if (kVerifySafeImpls) {
105 CHECK_EQ(VerifyClassClass(cls), result);
106 }
107
108 return result;
109}
110
111#else
112
Josh Gao77c14152017-04-19 13:20:19 -0700113static mirror::Class* SafeGetDeclaringClass(ArtMethod* method_obj)
114 REQUIRES_SHARED(Locks::mutator_lock_) {
Josh Gao143f61c2017-04-17 20:10:29 -0700115 return method_obj->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
116}
117
Josh Gao77c14152017-04-19 13:20:19 -0700118static bool SafeVerifyClassClass(mirror::Class* cls) REQUIRES_SHARED(Locks::mutator_lock_) {
Josh Gao143f61c2017-04-17 20:10:29 -0700119 return VerifyClassClass(cls);
120}
121#endif
122
123
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700124FaultManager::FaultManager() : initialized_(false) {
Dave Allisonb373e092014-02-20 16:06:36 -0800125 sigaction(SIGSEGV, nullptr, &oldaction_);
126}
127
128FaultManager::~FaultManager() {
Dave Allisonb373e092014-02-20 16:06:36 -0800129}
130
131void FaultManager::Init() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700132 CHECK(!initialized_);
Josh Gao6b2018f2017-05-04 13:55:28 -0700133 sigset_t mask;
134 sigfillset(&mask);
135 sigdelset(&mask, SIGABRT);
136 sigdelset(&mask, SIGBUS);
137 sigdelset(&mask, SIGFPE);
138 sigdelset(&mask, SIGILL);
139 sigdelset(&mask, SIGSEGV);
140
141 SigchainAction sa = {
142 .sc_sigaction = art_fault_handler,
143 .sc_mask = mask,
144 .sc_flags = 0UL,
145 };
146
147 AddSpecialSignalHandlerFn(SIGSEGV, &sa);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700148 initialized_ = true;
149}
150
Andreas Gampe928f72b2014-09-09 19:53:48 -0700151void FaultManager::Release() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700152 if (initialized_) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700153 RemoveSpecialSignalHandlerFn(SIGSEGV, art_fault_handler);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700154 initialized_ = false;
155 }
Dave Allisonb373e092014-02-20 16:06:36 -0800156}
157
Andreas Gampe928f72b2014-09-09 19:53:48 -0700158void FaultManager::Shutdown() {
159 if (initialized_) {
160 Release();
161
162 // Free all handlers.
163 STLDeleteElements(&generated_code_handlers_);
164 STLDeleteElements(&other_handlers_);
165 }
166}
167
jgu211376bdf2016-01-18 09:12:33 -0500168bool FaultManager::HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context) {
Andreas Gamped14b73d2016-04-18 17:15:42 -0700169 if (other_handlers_.empty()) {
170 return false;
171 }
172
Dave Allison0c2894b2014-08-29 12:06:16 -0700173 Thread* self = Thread::Current();
174
jgu211376bdf2016-01-18 09:12:33 -0500175 DCHECK(self != nullptr);
176 DCHECK(Runtime::Current() != nullptr);
177 DCHECK(Runtime::Current()->IsStarted());
Josh Gaoefd20cb2017-02-28 16:53:59 -0800178 for (const auto& handler : other_handlers_) {
179 if (handler->Action(sig, info, context)) {
180 return true;
Ian Rogersad11e7a2014-11-11 16:55:11 -0800181 }
182 }
jgu211376bdf2016-01-18 09:12:33 -0500183 return false;
184}
185
Josh Gao85a78cf2017-03-20 16:26:42 -0700186bool FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
187 VLOG(signals) << "Handling fault";
Josh Gaoefd20cb2017-02-28 16:53:59 -0800188
189#ifdef TEST_NESTED_SIGNAL
Josh Gao85a78cf2017-03-20 16:26:42 -0700190 // Simulate a crash in a handler.
191 raise(SIGSEGV);
Josh Gaoefd20cb2017-02-28 16:53:59 -0800192#endif
193
Josh Gao85a78cf2017-03-20 16:26:42 -0700194 if (IsInGeneratedCode(info, context, true)) {
195 VLOG(signals) << "in generated code, looking for handler";
196 for (const auto& handler : generated_code_handlers_) {
197 VLOG(signals) << "invoking Action on handler " << handler;
198 if (handler->Action(sig, info, context)) {
199 // We have handled a signal so it's time to return from the
200 // signal handler to the appropriate place.
201 return true;
Josh Gaoefd20cb2017-02-28 16:53:59 -0800202 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700203 }
Josh Gaoefd20cb2017-02-28 16:53:59 -0800204
Josh Gao85a78cf2017-03-20 16:26:42 -0700205 // We hit a signal we didn't handle. This might be something for which
206 // we can give more information about so call all registered handlers to
207 // see if it is.
208 if (HandleFaultByOtherHandlers(sig, info, context)) {
209 return true;
jgu211376bdf2016-01-18 09:12:33 -0500210 }
211 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700212
Dave Allison8be44cf2014-09-04 14:33:42 -0700213 // Set a breakpoint in this function to catch unhandled signals.
214 art_sigsegv_fault();
Josh Gao85a78cf2017-03-20 16:26:42 -0700215 return false;
Dave Allisonb373e092014-02-20 16:06:36 -0800216}
217
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700218void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
Andreas Gampe928f72b2014-09-09 19:53:48 -0700219 DCHECK(initialized_);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700220 if (generated_code) {
221 generated_code_handlers_.push_back(handler);
222 } else {
223 other_handlers_.push_back(handler);
224 }
225}
226
227void FaultManager::RemoveHandler(FaultHandler* handler) {
228 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
229 if (it != generated_code_handlers_.end()) {
230 generated_code_handlers_.erase(it);
231 return;
232 }
233 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
234 if (it2 != other_handlers_.end()) {
235 other_handlers_.erase(it);
236 return;
237 }
238 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
239}
Dave Allisonb373e092014-02-20 16:06:36 -0800240
241// This function is called within the signal handler. It checks that
242// the mutator_lock is held (shared). No annotalysis is done.
Dave Allison69dfe512014-07-11 17:11:58 +0000243bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800244 // We can only be running Java code in the current thread if it
245 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700246 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800247 Thread* thread = Thread::Current();
248 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700249 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800250 return false;
251 }
252
253 ThreadState state = thread->GetState();
254 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700255 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800256 return false;
257 }
258
259 // Current thread is runnable.
260 // Make sure it has the mutator lock.
261 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700262 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800263 return false;
264 }
265
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000266 ArtMethod* method_obj = nullptr;
Dave Allisonb373e092014-02-20 16:06:36 -0800267 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700268 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800269
270 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700271 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Dave Allisondfd3b472014-07-16 16:04:32 -0700272 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800273
274 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700275 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 // TODO: Check linear alloc and image.
Andreas Gampe542451c2016-07-26 09:02:02 -0700277 DCHECK_ALIGNED(ArtMethod::Size(kRuntimePointerSize), sizeof(void*))
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000278 << "ArtMethod is not pointer aligned";
279 if (method_obj == nullptr || !IsAligned<sizeof(void*)>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700280 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800281 return false;
282 }
283
284 // Verify that the potential method is indeed a method.
285 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800286 // Check that the class pointer inside the object is not null and is aligned.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800287 // No read barrier because method_obj may not be a real object.
Josh Gao143f61c2017-04-17 20:10:29 -0700288 mirror::Class* cls = SafeGetDeclaringClass(method_obj);
Dave Allisonb373e092014-02-20 16:06:36 -0800289 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700290 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800291 return false;
292 }
Josh Gao143f61c2017-04-17 20:10:29 -0700293
Dave Allisonb373e092014-02-20 16:06:36 -0800294 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700295 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800296 return false;
297 }
298
Josh Gao143f61c2017-04-17 20:10:29 -0700299 if (!SafeVerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700300 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800301 return false;
302 }
303
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100304 const OatQuickMethodHeader* method_header = method_obj->GetOatQuickMethodHeader(return_pc);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100305
Dave Allisonb373e092014-02-20 16:06:36 -0800306 // We can be certain that this is a method now. Check if we have a GC map
307 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700308 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700309 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100310 uint32_t sought_offset = return_pc -
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100311 reinterpret_cast<uintptr_t>(method_header->GetEntryPoint());
Dave Allison5cd33752014-04-15 15:57:58 -0700312 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700313 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100314 uint32_t dexpc = method_header->ToDexPc(method_obj, return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700315 VLOG(signals) << "dexpc: " << dexpc;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700316 return !check_dex_pc || dexpc != dex::kDexNoIndex;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700317}
318
319FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800320}
321
322//
323// Null pointer fault handler
324//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700325NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
326 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800327}
328
329//
330// Suspension fault handler
331//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700332SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
333 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800334}
335
336//
337// Stack overflow fault handler
338//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700339StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
340 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800341}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700342
343//
344// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
345//
346JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
347 manager_->AddHandler(this, false);
348}
349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100350bool JavaStackTraceHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* siginfo, void* context) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700351 // Make sure that we are in the generated code, but we may not have a dex pc.
Dave Allison8ce6b902014-08-26 11:07:58 -0700352 bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false);
Dave Allison8ce6b902014-08-26 11:07:58 -0700353 if (in_generated_code) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700354 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 ArtMethod* method = nullptr;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700356 uintptr_t return_pc = 0;
357 uintptr_t sp = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700358 Thread* self = Thread::Current();
Dave Allison8ce6b902014-08-26 11:07:58 -0700359
Dave Allison0c2894b2014-08-29 12:06:16 -0700360 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp);
361 // Inside of generated code, sp[0] is the method, so sp is the frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700362 self->SetTopOfStack(reinterpret_cast<ArtMethod**>(sp));
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700363 self->DumpJavaStack(LOG_STREAM(ERROR));
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700364 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700365
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700366 return false; // Return false since we want to propagate the fault to the main signal handler.
367}
368
Dave Allisonb373e092014-02-20 16:06:36 -0800369} // namespace art