2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: sw=2 ts=4 et :
|
2010-04-21 00:12:02 +04:00
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-04-21 00:12:02 +04:00
|
|
|
|
2013-01-05 01:28:37 +04:00
|
|
|
#include "mozilla/mozalloc_abort.h"
|
2010-04-21 00:12:02 +04:00
|
|
|
|
2013-01-05 01:28:37 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
# include <android/log.h>
|
|
|
|
#endif
|
2014-09-24 22:12:54 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
# include "APKOpen.h"
|
2014-09-24 22:12:54 +04:00
|
|
|
# include "dlfcn.h"
|
2014-09-24 22:12:54 +04:00
|
|
|
#endif
|
2012-06-06 03:49:30 +04:00
|
|
|
#include <stdio.h>
|
2018-08-10 01:21:14 +03:00
|
|
|
#include <string.h>
|
2010-04-21 00:12:02 +04:00
|
|
|
|
2013-01-05 01:28:37 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2018-03-27 17:51:32 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2010-04-21 00:12:02 +04:00
|
|
|
|
|
|
|
void mozalloc_abort(const char* const msg) {
|
2013-01-05 01:28:37 +04:00
|
|
|
#ifndef ANDROID
|
2010-04-21 00:12:02 +04:00
|
|
|
fputs(msg, stderr);
|
|
|
|
fputs("\n", stderr);
|
2013-01-05 01:28:37 +04:00
|
|
|
#else
|
|
|
|
__android_log_print(ANDROID_LOG_ERROR, "Gecko", "mozalloc_abort: %s", msg);
|
2014-09-24 22:12:54 +04:00
|
|
|
#endif
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
abortThroughJava(msg);
|
2013-01-05 01:28:37 +04:00
|
|
|
#endif
|
2012-06-06 03:49:30 +04:00
|
|
|
MOZ_CRASH();
|
2010-04-21 00:12:02 +04:00
|
|
|
}
|
2011-07-08 01:09:52 +04:00
|
|
|
|
2014-09-24 22:12:54 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
template <size_t N>
|
|
|
|
void fillAbortMessage(char (&msg)[N], uintptr_t retAddress) {
|
|
|
|
/*
|
|
|
|
* On Android, we often don't have reliable backtrace when crashing inside
|
|
|
|
* abort(). Therefore, we try to find out who is calling abort() and add
|
|
|
|
* that to the message.
|
|
|
|
*/
|
|
|
|
Dl_info info = {};
|
|
|
|
dladdr(reinterpret_cast<void*>(retAddress), &info);
|
|
|
|
|
|
|
|
const char* const module = info.dli_fname ? info.dli_fname : "";
|
|
|
|
const char* const base_module = strrchr(module, '/');
|
|
|
|
const void* const module_offset =
|
|
|
|
reinterpret_cast<void*>(retAddress - uintptr_t(info.dli_fbase));
|
|
|
|
const char* const sym = info.dli_sname ? info.dli_sname : "";
|
|
|
|
|
2018-03-27 17:51:32 +03:00
|
|
|
SprintfLiteral(msg, "abort() called from %s:%p (%s)",
|
|
|
|
base_module ? base_module + 1 : module, module_offset, sym);
|
2014-09-24 22:12:54 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-10-09 17:27:21 +03:00
|
|
|
#if defined(XP_UNIX) && !defined(MOZ_ASAN) && !defined(MOZ_TSAN)
|
2011-07-08 01:09:52 +04:00
|
|
|
// Define abort() here, so that it is used instead of the system abort(). This
|
|
|
|
// lets us control the behavior when aborting, in order to get better results
|
2012-06-06 03:49:30 +04:00
|
|
|
// on *NIX platforms. See mozalloc_abort for details.
|
2015-06-03 00:25:01 +03:00
|
|
|
//
|
|
|
|
// For AddressSanitizer, we must not redefine system abort because the ASan
|
|
|
|
// option "abort_on_error=1" calls abort() and therefore causes the following
|
|
|
|
// call chain with our redefined abort:
|
|
|
|
//
|
|
|
|
// ASan -> abort() -> moz_abort() -> MOZ_CRASH() -> Segmentation fault
|
|
|
|
//
|
|
|
|
// That segmentation fault will be interpreted as another bug by ASan and as a
|
|
|
|
// result, ASan will just exit(1) instead of aborting.
|
2019-10-09 17:27:21 +03:00
|
|
|
//
|
|
|
|
// The same applies to ThreadSanitizer when run with "halt_on_error=1" in
|
|
|
|
// combination with "abort_on_error=1".
|
2017-06-22 15:09:05 +03:00
|
|
|
extern "C" void abort(void) {
|
2014-09-24 22:12:54 +04:00
|
|
|
# ifdef MOZ_WIDGET_ANDROID
|
|
|
|
char msg[64] = {};
|
|
|
|
fillAbortMessage(msg, uintptr_t(__builtin_return_address(0)));
|
|
|
|
# else
|
|
|
|
const char* const msg = "Redirecting call to abort() to mozalloc_abort\n";
|
|
|
|
# endif
|
|
|
|
|
|
|
|
mozalloc_abort(msg);
|
2015-09-11 02:20:19 +03:00
|
|
|
|
|
|
|
// We won't reach here because mozalloc_abort() is MOZ_NORETURN. But that
|
|
|
|
// annotation isn't used on ARM (see mozalloc_abort.h for why) so we add a
|
|
|
|
// redundant MOZ_CRASH() here to avoid a "'noreturn' function does return"
|
|
|
|
// warning.
|
|
|
|
MOZ_CRASH();
|
2011-07-08 01:09:52 +04:00
|
|
|
}
|
|
|
|
#endif
|