blob: 3015b1010399d904dfaaa60c1e7f435b6a56ac08 [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>
Alex Light21f498f2018-02-02 11:12:49 -080020#include <string.h>
Dave Allisonb373e092014-02-20 16:06:36 -080021#include <sys/mman.h>
22#include <sys/ucontext.h>
Mathieu Chartiere401d142015-04-22 13:56:20 -070023
24#include "art_method-inl.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080025#include "base/logging.h" // For VLOG
Josh Gao143f61c2017-04-17 20:10:29 -070026#include "base/safe_copy.h"
Andreas Gampe928f72b2014-09-09 19:53:48 -070027#include "base/stl_util.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file_types.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070029#include "mirror/class.h"
Josh Gao143f61c2017-04-17 20:10:29 -070030#include "mirror/object_reference.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010031#include "oat_quick_method_header.h"
Dave Allisonf4b80bc2014-05-14 15:41:25 -070032#include "sigchain.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070033#include "thread-current-inl.h"
Dave Allisonb373e092014-02-20 16:06:36 -080034#include "verify_object-inl.h"
35
36namespace art {
37// Static fault manger object accessed by signal handler.
38FaultManager fault_manager;
39
Narayan Kamathc37769b2015-06-17 09:49:40 +010040extern "C" __attribute__((visibility("default"))) void art_sigsegv_fault() {
Dave Allisonf4b80bc2014-05-14 15:41:25 -070041 // Set a breakpoint here to be informed when a SIGSEGV is unhandled by ART.
42 VLOG(signals)<< "Caught unknown SIGSEGV in ART fault handler - chaining to next handler.";
43}
Dave Allisonf4b80bc2014-05-14 15:41:25 -070044
Dave Allisonb373e092014-02-20 16:06:36 -080045// Signal handler called on SIGSEGV.
Josh Gao85a78cf2017-03-20 16:26:42 -070046static bool art_fault_handler(int sig, siginfo_t* info, void* context) {
47 return fault_manager.HandleFault(sig, info, context);
Dave Allisonb373e092014-02-20 16:06:36 -080048}
49
Josh Gao143f61c2017-04-17 20:10:29 -070050#if defined(__linux__)
51
52// Change to verify the safe implementations against the original ones.
53constexpr bool kVerifySafeImpls = false;
54
55// Provide implementations of ArtMethod::GetDeclaringClass and VerifyClassClass that use SafeCopy
56// to safely dereference pointers which are potentially garbage.
57// Only available on Linux due to availability of SafeCopy.
58
59static mirror::Class* SafeGetDeclaringClass(ArtMethod* method)
60 REQUIRES_SHARED(Locks::mutator_lock_) {
61 char* method_declaring_class =
62 reinterpret_cast<char*>(method) + ArtMethod::DeclaringClassOffset().SizeValue();
63
64 // ArtMethod::declaring_class_ is a GcRoot<mirror::Class>.
65 // Read it out into as a CompressedReference directly for simplicity's sake.
66 mirror::CompressedReference<mirror::Class> cls;
67 ssize_t rc = SafeCopy(&cls, method_declaring_class, sizeof(cls));
68 CHECK_NE(-1, rc);
69
70 if (kVerifySafeImpls) {
71 mirror::Class* actual_class = method->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
72 CHECK_EQ(actual_class, cls.AsMirrorPtr());
73 }
74
75 if (rc != sizeof(cls)) {
76 return nullptr;
77 }
78
79 return cls.AsMirrorPtr();
80}
81
82static mirror::Class* SafeGetClass(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
83 char* obj_cls = reinterpret_cast<char*>(obj) + mirror::Object::ClassOffset().SizeValue();
84
Mathieu Chartierad8ebb32017-08-09 20:13:18 -070085 mirror::HeapReference<mirror::Class> cls;
Josh Gao143f61c2017-04-17 20:10:29 -070086 ssize_t rc = SafeCopy(&cls, obj_cls, sizeof(cls));
87 CHECK_NE(-1, rc);
88
89 if (kVerifySafeImpls) {
90 mirror::Class* actual_class = obj->GetClass<kVerifyNone>();
91 CHECK_EQ(actual_class, cls.AsMirrorPtr());
92 }
93
94 if (rc != sizeof(cls)) {
95 return nullptr;
96 }
97
98 return cls.AsMirrorPtr();
99}
100
101static bool SafeVerifyClassClass(mirror::Class* cls) REQUIRES_SHARED(Locks::mutator_lock_) {
102 mirror::Class* c_c = SafeGetClass(cls);
103 bool result = c_c != nullptr && c_c == SafeGetClass(c_c);
104
105 if (kVerifySafeImpls) {
106 CHECK_EQ(VerifyClassClass(cls), result);
107 }
108
109 return result;
110}
111
112#else
113
Josh Gao77c14152017-04-19 13:20:19 -0700114static mirror::Class* SafeGetDeclaringClass(ArtMethod* method_obj)
115 REQUIRES_SHARED(Locks::mutator_lock_) {
Josh Gao143f61c2017-04-17 20:10:29 -0700116 return method_obj->GetDeclaringClassUnchecked<kWithoutReadBarrier>();
117}
118
Josh Gao77c14152017-04-19 13:20:19 -0700119static bool SafeVerifyClassClass(mirror::Class* cls) REQUIRES_SHARED(Locks::mutator_lock_) {
Josh Gao143f61c2017-04-17 20:10:29 -0700120 return VerifyClassClass(cls);
121}
122#endif
123
124
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700125FaultManager::FaultManager() : initialized_(false) {
Dave Allisonb373e092014-02-20 16:06:36 -0800126 sigaction(SIGSEGV, nullptr, &oldaction_);
127}
128
129FaultManager::~FaultManager() {
Dave Allisonb373e092014-02-20 16:06:36 -0800130}
131
132void FaultManager::Init() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700133 CHECK(!initialized_);
Josh Gao6b2018f2017-05-04 13:55:28 -0700134 sigset_t mask;
135 sigfillset(&mask);
136 sigdelset(&mask, SIGABRT);
137 sigdelset(&mask, SIGBUS);
138 sigdelset(&mask, SIGFPE);
139 sigdelset(&mask, SIGILL);
140 sigdelset(&mask, SIGSEGV);
141
142 SigchainAction sa = {
143 .sc_sigaction = art_fault_handler,
144 .sc_mask = mask,
145 .sc_flags = 0UL,
146 };
147
148 AddSpecialSignalHandlerFn(SIGSEGV, &sa);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700149 initialized_ = true;
150}
151
Andreas Gampe928f72b2014-09-09 19:53:48 -0700152void FaultManager::Release() {
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700153 if (initialized_) {
Josh Gao85a78cf2017-03-20 16:26:42 -0700154 RemoveSpecialSignalHandlerFn(SIGSEGV, art_fault_handler);
Dave Allison1f8ef6f2014-08-20 17:38:41 -0700155 initialized_ = false;
156 }
Dave Allisonb373e092014-02-20 16:06:36 -0800157}
158
Andreas Gampe928f72b2014-09-09 19:53:48 -0700159void FaultManager::Shutdown() {
160 if (initialized_) {
161 Release();
162
163 // Free all handlers.
164 STLDeleteElements(&generated_code_handlers_);
165 STLDeleteElements(&other_handlers_);
166 }
167}
168
jgu211376bdf2016-01-18 09:12:33 -0500169bool FaultManager::HandleFaultByOtherHandlers(int sig, siginfo_t* info, void* context) {
Andreas Gamped14b73d2016-04-18 17:15:42 -0700170 if (other_handlers_.empty()) {
171 return false;
172 }
173
Dave Allison0c2894b2014-08-29 12:06:16 -0700174 Thread* self = Thread::Current();
175
jgu211376bdf2016-01-18 09:12:33 -0500176 DCHECK(self != nullptr);
177 DCHECK(Runtime::Current() != nullptr);
178 DCHECK(Runtime::Current()->IsStarted());
Josh Gaoefd20cb2017-02-28 16:53:59 -0800179 for (const auto& handler : other_handlers_) {
180 if (handler->Action(sig, info, context)) {
181 return true;
Ian Rogersad11e7a2014-11-11 16:55:11 -0800182 }
183 }
jgu211376bdf2016-01-18 09:12:33 -0500184 return false;
185}
186
Alex Light21f498f2018-02-02 11:12:49 -0800187static const char* SignalCodeName(int sig, int code) {
188 if (sig != SIGSEGV) {
189 return "UNKNOWN";
190 } else {
191 switch (code) {
192 case SEGV_MAPERR: return "SEGV_MAPERR";
193 case SEGV_ACCERR: return "SEGV_ACCERR";
194 default: return "UNKNOWN";
195 }
196 }
197}
198static std::ostream& PrintSignalInfo(std::ostream& os, siginfo_t* info) {
199 os << " si_signo: " << info->si_signo << " (" << strsignal(info->si_signo) << ")\n"
200 << " si_code: " << info->si_code
201 << " (" << SignalCodeName(info->si_signo, info->si_code) << ")";
202 if (info->si_signo == SIGSEGV) {
203 os << "\n" << " si_addr: " << info->si_addr;
204 }
205 return os;
206}
207
Josh Gao85a78cf2017-03-20 16:26:42 -0700208bool FaultManager::HandleFault(int sig, siginfo_t* info, void* context) {
Alex Light21f498f2018-02-02 11:12:49 -0800209 if (VLOG_IS_ON(signals)) {
210 PrintSignalInfo(VLOG_STREAM(signals) << "Handling fault:" << "\n", info);
211 }
Josh Gaoefd20cb2017-02-28 16:53:59 -0800212
213#ifdef TEST_NESTED_SIGNAL
Josh Gao85a78cf2017-03-20 16:26:42 -0700214 // Simulate a crash in a handler.
215 raise(SIGSEGV);
Josh Gaoefd20cb2017-02-28 16:53:59 -0800216#endif
217
Josh Gao85a78cf2017-03-20 16:26:42 -0700218 if (IsInGeneratedCode(info, context, true)) {
219 VLOG(signals) << "in generated code, looking for handler";
220 for (const auto& handler : generated_code_handlers_) {
221 VLOG(signals) << "invoking Action on handler " << handler;
222 if (handler->Action(sig, info, context)) {
223 // We have handled a signal so it's time to return from the
224 // signal handler to the appropriate place.
225 return true;
Josh Gaoefd20cb2017-02-28 16:53:59 -0800226 }
Josh Gao85a78cf2017-03-20 16:26:42 -0700227 }
Alex Lightf4ed7e82018-01-17 11:52:36 -0800228 }
Josh Gaoefd20cb2017-02-28 16:53:59 -0800229
Alex Lightf4ed7e82018-01-17 11:52:36 -0800230 // We hit a signal we didn't handle. This might be something for which
231 // we can give more information about so call all registered handlers to
232 // see if it is.
233 if (HandleFaultByOtherHandlers(sig, info, context)) {
234 return true;
jgu211376bdf2016-01-18 09:12:33 -0500235 }
Dave Allisonf4b80bc2014-05-14 15:41:25 -0700236
Dave Allison8be44cf2014-09-04 14:33:42 -0700237 // Set a breakpoint in this function to catch unhandled signals.
238 art_sigsegv_fault();
Josh Gao85a78cf2017-03-20 16:26:42 -0700239 return false;
Dave Allisonb373e092014-02-20 16:06:36 -0800240}
241
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700242void FaultManager::AddHandler(FaultHandler* handler, bool generated_code) {
Andreas Gampe928f72b2014-09-09 19:53:48 -0700243 DCHECK(initialized_);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700244 if (generated_code) {
245 generated_code_handlers_.push_back(handler);
246 } else {
247 other_handlers_.push_back(handler);
248 }
249}
250
251void FaultManager::RemoveHandler(FaultHandler* handler) {
252 auto it = std::find(generated_code_handlers_.begin(), generated_code_handlers_.end(), handler);
253 if (it != generated_code_handlers_.end()) {
254 generated_code_handlers_.erase(it);
255 return;
256 }
257 auto it2 = std::find(other_handlers_.begin(), other_handlers_.end(), handler);
258 if (it2 != other_handlers_.end()) {
Alex Lightf4ed7e82018-01-17 11:52:36 -0800259 other_handlers_.erase(it2);
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700260 return;
261 }
262 LOG(FATAL) << "Attempted to remove non existent handler " << handler;
263}
Dave Allisonb373e092014-02-20 16:06:36 -0800264
265// This function is called within the signal handler. It checks that
266// the mutator_lock is held (shared). No annotalysis is done.
Dave Allison69dfe512014-07-11 17:11:58 +0000267bool FaultManager::IsInGeneratedCode(siginfo_t* siginfo, void* context, bool check_dex_pc) {
Dave Allisonb373e092014-02-20 16:06:36 -0800268 // We can only be running Java code in the current thread if it
269 // is in Runnable state.
Dave Allison5cd33752014-04-15 15:57:58 -0700270 VLOG(signals) << "Checking for generated code";
Dave Allisonb373e092014-02-20 16:06:36 -0800271 Thread* thread = Thread::Current();
272 if (thread == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700273 VLOG(signals) << "no current thread";
Dave Allisonb373e092014-02-20 16:06:36 -0800274 return false;
275 }
276
277 ThreadState state = thread->GetState();
278 if (state != kRunnable) {
Dave Allison5cd33752014-04-15 15:57:58 -0700279 VLOG(signals) << "not runnable";
Dave Allisonb373e092014-02-20 16:06:36 -0800280 return false;
281 }
282
283 // Current thread is runnable.
284 // Make sure it has the mutator lock.
285 if (!Locks::mutator_lock_->IsSharedHeld(thread)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700286 VLOG(signals) << "no lock";
Dave Allisonb373e092014-02-20 16:06:36 -0800287 return false;
288 }
289
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000290 ArtMethod* method_obj = nullptr;
Dave Allisonb373e092014-02-20 16:06:36 -0800291 uintptr_t return_pc = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700292 uintptr_t sp = 0;
Dave Allisonb373e092014-02-20 16:06:36 -0800293
294 // Get the architecture specific method address and return address. These
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700295 // are in architecture specific files in arch/<arch>/fault_handler_<arch>.
Dave Allisondfd3b472014-07-16 16:04:32 -0700296 GetMethodAndReturnPcAndSp(siginfo, context, &method_obj, &return_pc, &sp);
Dave Allisonb373e092014-02-20 16:06:36 -0800297
298 // If we don't have a potential method, we're outta here.
Dave Allison5cd33752014-04-15 15:57:58 -0700299 VLOG(signals) << "potential method: " << method_obj;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 // TODO: Check linear alloc and image.
Andreas Gampe542451c2016-07-26 09:02:02 -0700301 DCHECK_ALIGNED(ArtMethod::Size(kRuntimePointerSize), sizeof(void*))
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +0000302 << "ArtMethod is not pointer aligned";
303 if (method_obj == nullptr || !IsAligned<sizeof(void*)>(method_obj)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700304 VLOG(signals) << "no method";
Dave Allisonb373e092014-02-20 16:06:36 -0800305 return false;
306 }
307
308 // Verify that the potential method is indeed a method.
309 // TODO: check the GC maps to make sure it's an object.
Dave Allisonb373e092014-02-20 16:06:36 -0800310 // Check that the class pointer inside the object is not null and is aligned.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800311 // No read barrier because method_obj may not be a real object.
Josh Gao143f61c2017-04-17 20:10:29 -0700312 mirror::Class* cls = SafeGetDeclaringClass(method_obj);
Dave Allisonb373e092014-02-20 16:06:36 -0800313 if (cls == nullptr) {
Dave Allison5cd33752014-04-15 15:57:58 -0700314 VLOG(signals) << "not a class";
Dave Allisonb373e092014-02-20 16:06:36 -0800315 return false;
316 }
Josh Gao143f61c2017-04-17 20:10:29 -0700317
Dave Allisonb373e092014-02-20 16:06:36 -0800318 if (!IsAligned<kObjectAlignment>(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700319 VLOG(signals) << "not aligned";
Dave Allisonb373e092014-02-20 16:06:36 -0800320 return false;
321 }
322
Josh Gao143f61c2017-04-17 20:10:29 -0700323 if (!SafeVerifyClassClass(cls)) {
Dave Allison5cd33752014-04-15 15:57:58 -0700324 VLOG(signals) << "not a class class";
Dave Allisonb373e092014-02-20 16:06:36 -0800325 return false;
326 }
327
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100328 const OatQuickMethodHeader* method_header = method_obj->GetOatQuickMethodHeader(return_pc);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100329
Dave Allisonb373e092014-02-20 16:06:36 -0800330 // We can be certain that this is a method now. Check if we have a GC map
331 // at the return PC address.
Dave Allisonf9439142014-03-27 15:10:22 -0700332 if (true || kIsDebugBuild) {
Dave Allison5cd33752014-04-15 15:57:58 -0700333 VLOG(signals) << "looking for dex pc for return pc " << std::hex << return_pc;
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100334 uint32_t sought_offset = return_pc -
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100335 reinterpret_cast<uintptr_t>(method_header->GetEntryPoint());
Dave Allison5cd33752014-04-15 15:57:58 -0700336 VLOG(signals) << "pc offset: " << std::hex << sought_offset;
Dave Allisonf9439142014-03-27 15:10:22 -0700337 }
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100338 uint32_t dexpc = method_header->ToDexPc(method_obj, return_pc, false);
Dave Allison5cd33752014-04-15 15:57:58 -0700339 VLOG(signals) << "dexpc: " << dexpc;
Andreas Gampee2abbc62017-09-15 11:59:26 -0700340 return !check_dex_pc || dexpc != dex::kDexNoIndex;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700341}
342
343FaultHandler::FaultHandler(FaultManager* manager) : manager_(manager) {
Dave Allisonb373e092014-02-20 16:06:36 -0800344}
345
346//
347// Null pointer fault handler
348//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700349NullPointerHandler::NullPointerHandler(FaultManager* manager) : FaultHandler(manager) {
350 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800351}
352
353//
354// Suspension fault handler
355//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700356SuspensionHandler::SuspensionHandler(FaultManager* manager) : FaultHandler(manager) {
357 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800358}
359
360//
361// Stack overflow fault handler
362//
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700363StackOverflowHandler::StackOverflowHandler(FaultManager* manager) : FaultHandler(manager) {
364 manager_->AddHandler(this, true);
Dave Allisonb373e092014-02-20 16:06:36 -0800365}
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700366
367//
368// Stack trace handler, used to help get a stack trace from SIGSEGV inside of compiled code.
369//
370JavaStackTraceHandler::JavaStackTraceHandler(FaultManager* manager) : FaultHandler(manager) {
371 manager_->AddHandler(this, false);
372}
373
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100374bool JavaStackTraceHandler::Action(int sig ATTRIBUTE_UNUSED, siginfo_t* siginfo, void* context) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700375 // Make sure that we are in the generated code, but we may not have a dex pc.
Dave Allison8ce6b902014-08-26 11:07:58 -0700376 bool in_generated_code = manager_->IsInGeneratedCode(siginfo, context, false);
Dave Allison8ce6b902014-08-26 11:07:58 -0700377 if (in_generated_code) {
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700378 LOG(ERROR) << "Dumping java stack trace for crash in generated code";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 ArtMethod* method = nullptr;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700380 uintptr_t return_pc = 0;
381 uintptr_t sp = 0;
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700382 Thread* self = Thread::Current();
Dave Allison8ce6b902014-08-26 11:07:58 -0700383
Dave Allison0c2894b2014-08-29 12:06:16 -0700384 manager_->GetMethodAndReturnPcAndSp(siginfo, context, &method, &return_pc, &sp);
385 // Inside of generated code, sp[0] is the method, so sp is the frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 self->SetTopOfStack(reinterpret_cast<ArtMethod**>(sp));
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700387 self->DumpJavaStack(LOG_STREAM(ERROR));
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700388 }
Dave Allison8ce6b902014-08-26 11:07:58 -0700389
Mathieu Chartierc751fdc2014-03-30 15:25:44 -0700390 return false; // Return false since we want to propagate the fault to the main signal handler.
391}
392
Dave Allisonb373e092014-02-20 16:06:36 -0800393} // namespace art