1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/mozalloc_abort.h"
11 # include <android/log.h>
13 #ifdef MOZ_WIDGET_ANDROID
20 #include "mozilla/Assertions.h"
21 #include "mozilla/Sprintf.h"
23 void mozalloc_abort(const char* const msg
) {
28 __android_log_print(ANDROID_LOG_ERROR
, "Gecko", "mozalloc_abort: %s", msg
);
31 #ifdef MOZ_WIDGET_ANDROID
32 abortThroughJava(msg
);
35 MOZ_CRASH_UNSAFE(msg
);
38 #ifdef MOZ_WIDGET_ANDROID
40 void fillAbortMessage(char (&msg
)[N
], uintptr_t retAddress
) {
42 * On Android, we often don't have reliable backtrace when crashing inside
43 * abort(). Therefore, we try to find out who is calling abort() and add
44 * that to the message.
47 dladdr(reinterpret_cast<void*>(retAddress
), &info
);
49 const char* const module
= info
.dli_fname
? info
.dli_fname
: "";
50 const char* const base_module
= strrchr(module
, '/');
51 const void* const module_offset
=
52 reinterpret_cast<void*>(retAddress
- uintptr_t(info
.dli_fbase
));
53 const char* const sym
= info
.dli_sname
? info
.dli_sname
: "";
55 SprintfLiteral(msg
, "abort() called from %s:%p (%s)",
56 base_module
? base_module
+ 1 : module
, module_offset
, sym
);
60 #if defined(XP_UNIX) && !defined(MOZ_ASAN) && !defined(MOZ_TSAN) && \
62 // Define abort() here, so that it is used instead of the system abort(). This
63 // lets us control the behavior when aborting, in order to get better results
64 // on *NIX platforms. See mozalloc_abort for details.
66 // For AddressSanitizer, we must not redefine system abort because the ASan
67 // option "abort_on_error=1" calls abort() and therefore causes the following
68 // call chain with our redefined abort:
70 // ASan -> abort() -> moz_abort() -> MOZ_CRASH() -> Segmentation fault
72 // That segmentation fault will be interpreted as another bug by ASan and as a
73 // result, ASan will just exit(1) instead of aborting.
75 // The same applies to ThreadSanitizer when run with "halt_on_error=1" in
76 // combination with "abort_on_error=1".
78 // When building with libFuzzer, it pulls in the UndefinedBehaviorSanitizer
79 // runtime which also requires the same workaround as with ASan or TSan.
80 extern "C" void abort(void) {
81 # ifdef MOZ_WIDGET_ANDROID
83 fillAbortMessage(msg
, uintptr_t(__builtin_return_address(0)));
85 const char* const msg
= "Redirecting call to abort() to mozalloc_abort\n";
90 // We won't reach here because mozalloc_abort() is MOZ_NORETURN. But that
91 // annotation isn't used on ARM (see mozalloc_abort.h for why) so we add a
92 // unreachable marker here to avoid a "'noreturn' function does return"
94 MOZ_ASSUME_UNREACHABLE_MARKER();