2019-08-14 10:47:11 +02:00
|
|
|
//===-- sanitizer_unwind_linux_libcdep.cpp --------------------------------===//
|
2014-09-23 17:59:53 +00:00
|
|
|
//
|
2019-08-14 10:47:11 +02:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-09-23 17:59:53 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the unwind.h-based (aka "slow") stack unwinding routines
|
2018-10-31 12:14:23 +01:00
|
|
|
// available to the tools on Linux, Android, NetBSD, FreeBSD, and Solaris.
|
2014-09-23 17:59:53 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_platform.h"
|
2018-10-31 12:14:23 +01:00
|
|
|
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
|
|
|
|
SANITIZER_SOLARIS
|
2014-09-23 17:59:53 +00:00
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_stacktrace.h"
|
|
|
|
|
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
#include <dlfcn.h> // for dlopen()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SANITIZER_FREEBSD
|
|
|
|
#define _GNU_SOURCE // to declare _Unwind_Backtrace() from <unwind.h>
|
|
|
|
#endif
|
|
|
|
#include <unwind.h>
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2019-11-05 14:54:57 +01:00
|
|
|
namespace {
|
|
|
|
|
2019-08-14 10:47:11 +02:00
|
|
|
//---------------------------- UnwindSlow --------------------------------------
|
2014-09-23 17:59:53 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uptr absolute_pc;
|
|
|
|
uptr stack_top;
|
|
|
|
uptr stack_size;
|
|
|
|
} backtrace_frame_t;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
typedef void *(*acquire_my_map_info_list_func)();
|
|
|
|
typedef void (*release_my_map_info_list_func)(void *map);
|
|
|
|
typedef sptr (*unwind_backtrace_signal_arch_func)(
|
|
|
|
void *siginfo, void *sigcontext, void *map_info_list,
|
|
|
|
backtrace_frame_t *backtrace, uptr ignore_depth, uptr max_depth);
|
|
|
|
acquire_my_map_info_list_func acquire_my_map_info_list;
|
|
|
|
release_my_map_info_list_func release_my_map_info_list;
|
|
|
|
unwind_backtrace_signal_arch_func unwind_backtrace_signal_arch;
|
|
|
|
} // extern "C"
|
|
|
|
|
ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch builtins...
* ubsan.c (ubsan_expand_null_ifn): Use _v1 suffixed type mismatch
builtins, store max (log2 (align), 0) into uchar field instead of
align into uptr field.
(ubsan_expand_objsize_ifn): Use _v1 suffixed type mismatch builtins,
store uchar 0 field instead of uptr 0 field.
(instrument_nonnull_return): Use _v1 suffixed nonnull return builtin,
instead of passing one address of struct with 2 locations pass
two addresses of structs with 1 location each.
* sanitizer.def (BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_ABORT): Removed.
(BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1,
BUILT_IN_UBSAN_HANDLE_TYPE_MISMATCH_V1_ABORT,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1,
BUILT_IN_UBSAN_HANDLE_NONNULL_RETURN_V1_ABORT): New builtins.
* c-c++-common/ubsan/float-cast-overflow-1.c: Drop value keyword
from expected output regexps.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-3.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-5.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-6.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-8.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-9.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-10.c: Likewise.
* g++.dg/ubsan/float-cast-overflow-bf.C: Likewise.
* gcc.dg/ubsan/float-cast-overflow-bf.c: Likewise.
* g++.dg/asan/default-options-1.C (__asan_default_options): Add
used attribute.
* g++.dg/asan/asan_test.C: Run with ASAN_OPTIONS=handle_segv=2
in the environment.
* All source files: Merge from upstream 315899.
* asan/Makefile.am (nodist_saninclude_HEADERS): Add
include/sanitizer/tsan_interface.h.
* asan/libtool-version: Bump the libasan SONAME.
* lsan/Makefile.am (sanitizer_lsan_files): Add lsan_common_mac.cc.
(lsan_files): Add lsan_linux.cc, lsan_mac.cc and lsan_malloc_mac.cc.
* sanitizer_common/Makefile.am (sanitizer_common_files): Add
sancov_flags.cc, sanitizer_allocator_checks.cc,
sanitizer_coverage_libcdep_new.cc, sanitizer_errno.cc,
sanitizer_file.cc, sanitizer_mac_libcdep.cc and
sanitizer_stoptheworld_mac.cc. Remove sanitizer_coverage_libcdep.cc
and sanitizer_coverage_mapping_libcdep.cc.
* tsan/Makefile.am (tsan_files): Add tsan_external.cc.
* ubsan/Makefile.am (DEFS): Add -DUBSAN_CAN_USE_CXXABI=1.
(ubsan_files): Add ubsan_init_standalone.cc and
ubsan_signals_standalone.cc.
* ubsan/libtool-version: Bump the libubsan SONAME.
* asan/Makefile.in: Regenerate.
* lsan/Makefile.in: Regenerate.
* sanitizer_common/Makefile.in: Regenerate.
* tsan/Makefile.in: Regenerate.
* ubsan/Makefile.in: Regenerate.
From-SVN: r253887
2017-10-19 13:23:59 +02:00
|
|
|
#if defined(__arm__) && !SANITIZER_NETBSD
|
|
|
|
// NetBSD uses dwarf EH
|
2014-09-23 17:59:53 +00:00
|
|
|
#define UNWIND_STOP _URC_END_OF_STACK
|
|
|
|
#define UNWIND_CONTINUE _URC_NO_REASON
|
|
|
|
#else
|
|
|
|
#define UNWIND_STOP _URC_NORMAL_STOP
|
|
|
|
#define UNWIND_CONTINUE _URC_NO_REASON
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uptr Unwind_GetIP(struct _Unwind_Context *ctx) {
|
2022-08-30 11:45:34 +02:00
|
|
|
#if defined(__arm__) && !SANITIZER_APPLE
|
2014-09-23 17:59:53 +00:00
|
|
|
uptr val;
|
|
|
|
_Unwind_VRS_Result res = _Unwind_VRS_Get(ctx, _UVRSC_CORE,
|
|
|
|
15 /* r15 = PC */, _UVRSD_UINT32, &val);
|
|
|
|
CHECK(res == _UVRSR_OK && "_Unwind_VRS_Get failed");
|
|
|
|
// Clear the Thumb bit.
|
|
|
|
return val & ~(uptr)1;
|
|
|
|
#else
|
2019-08-14 10:47:11 +02:00
|
|
|
return (uptr)_Unwind_GetIP(ctx);
|
2014-09-23 17:59:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UnwindTraceArg {
|
2014-11-13 20:41:38 +00:00
|
|
|
BufferedStackTrace *stack;
|
2015-10-21 10:32:45 +03:00
|
|
|
u32 max_depth;
|
2014-09-23 17:59:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_Unwind_Reason_Code Unwind_Trace(struct _Unwind_Context *ctx, void *param) {
|
|
|
|
UnwindTraceArg *arg = (UnwindTraceArg*)param;
|
|
|
|
CHECK_LT(arg->stack->size, arg->max_depth);
|
|
|
|
uptr pc = Unwind_GetIP(ctx);
|
2016-11-08 22:04:09 +00:00
|
|
|
const uptr kPageSize = GetPageSizeCached();
|
|
|
|
// Let's assume that any pointer in the 0th page (i.e. <0x1000 on i386 and
|
|
|
|
// x86_64) is invalid and stop unwinding here. If we're adding support for
|
|
|
|
// a platform where this isn't true, we need to reconsider this check.
|
|
|
|
if (pc < kPageSize) return UNWIND_STOP;
|
2014-11-13 20:41:38 +00:00
|
|
|
arg->stack->trace_buffer[arg->stack->size++] = pc;
|
2014-09-23 17:59:53 +00:00
|
|
|
if (arg->stack->size == arg->max_depth) return UNWIND_STOP;
|
|
|
|
return UNWIND_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:54:57 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
void SanitizerInitializeUnwinder() {
|
|
|
|
if (AndroidGetApiLevel() >= ANDROID_LOLLIPOP_MR1) return;
|
|
|
|
|
|
|
|
// Pre-lollipop Android can not unwind through signal handler frames with
|
|
|
|
// libgcc unwinder, but it has a libcorkscrew.so library with the necessary
|
|
|
|
// workarounds.
|
|
|
|
void *p = dlopen("libcorkscrew.so", RTLD_LAZY);
|
|
|
|
if (!p) {
|
|
|
|
VReport(1,
|
|
|
|
"Failed to open libcorkscrew.so. You may see broken stack traces "
|
|
|
|
"in SEGV reports.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
acquire_my_map_info_list =
|
|
|
|
(acquire_my_map_info_list_func)(uptr)dlsym(p, "acquire_my_map_info_list");
|
|
|
|
release_my_map_info_list =
|
|
|
|
(release_my_map_info_list_func)(uptr)dlsym(p, "release_my_map_info_list");
|
|
|
|
unwind_backtrace_signal_arch = (unwind_backtrace_signal_arch_func)(uptr)dlsym(
|
|
|
|
p, "unwind_backtrace_signal_arch");
|
|
|
|
if (!acquire_my_map_info_list || !release_my_map_info_list ||
|
|
|
|
!unwind_backtrace_signal_arch) {
|
|
|
|
VReport(1,
|
|
|
|
"Failed to find one of the required symbols in libcorkscrew.so. "
|
|
|
|
"You may see broken stack traces in SEGV reports.");
|
|
|
|
acquire_my_map_info_list = 0;
|
|
|
|
unwind_backtrace_signal_arch = 0;
|
|
|
|
release_my_map_info_list = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-14 10:47:11 +02:00
|
|
|
void BufferedStackTrace::UnwindSlow(uptr pc, u32 max_depth) {
|
2014-09-23 17:59:53 +00:00
|
|
|
CHECK_GE(max_depth, 2);
|
|
|
|
size = 0;
|
|
|
|
UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)};
|
|
|
|
_Unwind_Backtrace(Unwind_Trace, &arg);
|
|
|
|
// We need to pop a few frames so that pc is on top.
|
|
|
|
uptr to_pop = LocatePcInTrace(pc);
|
2015-10-21 10:32:45 +03:00
|
|
|
// trace_buffer[0] belongs to the current function so we always pop it,
|
|
|
|
// unless there is only 1 frame in the stack trace (1 frame is always better
|
|
|
|
// than 0!).
|
|
|
|
// 1-frame stacks don't normally happen, but this depends on the actual
|
|
|
|
// unwinder implementation (libgcc, libunwind, etc) which is outside of our
|
|
|
|
// control.
|
2014-11-13 20:41:38 +00:00
|
|
|
if (to_pop == 0 && size > 1)
|
2014-09-23 17:59:53 +00:00
|
|
|
to_pop = 1;
|
|
|
|
PopStackFrames(to_pop);
|
2014-11-13 20:41:38 +00:00
|
|
|
trace_buffer[0] = pc;
|
2014-09-23 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:47:11 +02:00
|
|
|
void BufferedStackTrace::UnwindSlow(uptr pc, void *context, u32 max_depth) {
|
|
|
|
CHECK(context);
|
2014-09-23 17:59:53 +00:00
|
|
|
CHECK_GE(max_depth, 2);
|
|
|
|
if (!unwind_backtrace_signal_arch) {
|
2019-08-14 10:47:11 +02:00
|
|
|
UnwindSlow(pc, max_depth);
|
2014-09-23 17:59:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *map = acquire_my_map_info_list();
|
|
|
|
CHECK(map);
|
2018-10-31 12:14:23 +01:00
|
|
|
InternalMmapVector<backtrace_frame_t> frames(kStackTraceMax);
|
2014-09-23 17:59:53 +00:00
|
|
|
// siginfo argument appears to be unused.
|
|
|
|
sptr res = unwind_backtrace_signal_arch(/* siginfo */ 0, context, map,
|
|
|
|
frames.data(),
|
|
|
|
/* ignore_depth */ 0, max_depth);
|
|
|
|
release_my_map_info_list(map);
|
|
|
|
if (res < 0) return;
|
|
|
|
CHECK_LE((uptr)res, kStackTraceMax);
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
// +2 compensate for libcorkscrew unwinder returning addresses of call
|
|
|
|
// instructions instead of raw return addresses.
|
|
|
|
for (sptr i = 0; i < res; ++i)
|
2014-11-13 20:41:38 +00:00
|
|
|
trace_buffer[size++] = frames[i].absolute_pc + 2;
|
2014-09-23 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||
|
|
|
|
// SANITIZER_SOLARIS
|