5d3805fca3
* 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
194 lines
5.4 KiB
C++
194 lines
5.4 KiB
C++
//===-- asan_linux.cc -----------------------------------------------------===//
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
//
|
|
// Linux-specific details.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
|
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
|
|
|
|
#include "asan_interceptors.h"
|
|
#include "asan_internal.h"
|
|
#include "asan_thread.h"
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
|
#include "sanitizer_common/sanitizer_freebsd.h"
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
#include "sanitizer_common/sanitizer_procmaps.h"
|
|
|
|
#include <sys/time.h>
|
|
#include <sys/resource.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/syscall.h>
|
|
#include <sys/types.h>
|
|
#include <dlfcn.h>
|
|
#include <fcntl.h>
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <unwind.h>
|
|
|
|
#if SANITIZER_FREEBSD
|
|
#include <sys/link_elf.h>
|
|
#endif
|
|
|
|
#if SANITIZER_ANDROID || SANITIZER_FREEBSD
|
|
#include <ucontext.h>
|
|
extern "C" void* _DYNAMIC;
|
|
#elif SANITIZER_NETBSD
|
|
#include <link_elf.h>
|
|
#include <ucontext.h>
|
|
extern Elf_Dyn _DYNAMIC;
|
|
#else
|
|
#include <sys/ucontext.h>
|
|
#include <link.h>
|
|
#endif
|
|
|
|
// x86-64 FreeBSD 9.2 and older define 'ucontext_t' incorrectly in
|
|
// 32-bit mode.
|
|
#if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32) && \
|
|
__FreeBSD_version <= 902001 // v9.2
|
|
#define ucontext_t xucontext_t
|
|
#endif
|
|
|
|
typedef enum {
|
|
ASAN_RT_VERSION_UNDEFINED = 0,
|
|
ASAN_RT_VERSION_DYNAMIC,
|
|
ASAN_RT_VERSION_STATIC,
|
|
} asan_rt_version_t;
|
|
|
|
// FIXME: perhaps also store abi version here?
|
|
extern "C" {
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
asan_rt_version_t __asan_rt_version;
|
|
}
|
|
|
|
namespace __asan {
|
|
|
|
void InitializePlatformInterceptors() {}
|
|
void InitializePlatformExceptionHandlers() {}
|
|
bool IsSystemHeapAddress (uptr addr) { return false; }
|
|
|
|
void *AsanDoesNotSupportStaticLinkage() {
|
|
// This will fail to link with -static.
|
|
return &_DYNAMIC; // defined in link.h
|
|
}
|
|
|
|
uptr FindDynamicShadowStart() {
|
|
UNREACHABLE("FindDynamicShadowStart is not available");
|
|
return 0;
|
|
}
|
|
|
|
void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
|
|
UNIMPLEMENTED();
|
|
}
|
|
|
|
#if SANITIZER_ANDROID
|
|
// FIXME: should we do anything for Android?
|
|
void AsanCheckDynamicRTPrereqs() {}
|
|
void AsanCheckIncompatibleRT() {}
|
|
#else
|
|
static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
|
|
void *data) {
|
|
// Continue until the first dynamic library is found
|
|
if (!info->dlpi_name || info->dlpi_name[0] == 0)
|
|
return 0;
|
|
|
|
// Ignore vDSO
|
|
if (internal_strncmp(info->dlpi_name, "linux-", sizeof("linux-") - 1) == 0)
|
|
return 0;
|
|
|
|
#if SANITIZER_NETBSD
|
|
// Ignore first entry (the main program)
|
|
char **p = (char **)data;
|
|
if (!(*p)) {
|
|
*p = (char *)-1;
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
*(const char **)data = info->dlpi_name;
|
|
return 1;
|
|
}
|
|
|
|
static bool IsDynamicRTName(const char *libname) {
|
|
return internal_strstr(libname, "libclang_rt.asan") ||
|
|
internal_strstr(libname, "libasan.so");
|
|
}
|
|
|
|
static void ReportIncompatibleRT() {
|
|
Report("Your application is linked against incompatible ASan runtimes.\n");
|
|
Die();
|
|
}
|
|
|
|
void AsanCheckDynamicRTPrereqs() {
|
|
if (!ASAN_DYNAMIC || !flags()->verify_asan_link_order)
|
|
return;
|
|
|
|
// Ensure that dynamic RT is the first DSO in the list
|
|
const char *first_dso_name = nullptr;
|
|
dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name);
|
|
if (first_dso_name && !IsDynamicRTName(first_dso_name)) {
|
|
Report("ASan runtime does not come first in initial library list; "
|
|
"you should either link runtime to your application or "
|
|
"manually preload it with LD_PRELOAD.\n");
|
|
Die();
|
|
}
|
|
}
|
|
|
|
void AsanCheckIncompatibleRT() {
|
|
if (ASAN_DYNAMIC) {
|
|
if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
|
|
__asan_rt_version = ASAN_RT_VERSION_DYNAMIC;
|
|
} else if (__asan_rt_version != ASAN_RT_VERSION_DYNAMIC) {
|
|
ReportIncompatibleRT();
|
|
}
|
|
} else {
|
|
if (__asan_rt_version == ASAN_RT_VERSION_UNDEFINED) {
|
|
// Ensure that dynamic runtime is not present. We should detect it
|
|
// as early as possible, otherwise ASan interceptors could bind to
|
|
// the functions in dynamic ASan runtime instead of the functions in
|
|
// system libraries, causing crashes later in ASan initialization.
|
|
MemoryMappingLayout proc_maps(/*cache_enabled*/true);
|
|
char filename[128];
|
|
MemoryMappedSegment segment(filename, sizeof(filename));
|
|
while (proc_maps.Next(&segment)) {
|
|
if (IsDynamicRTName(segment.filename)) {
|
|
Report("Your application is linked against "
|
|
"incompatible ASan runtimes.\n");
|
|
Die();
|
|
}
|
|
}
|
|
__asan_rt_version = ASAN_RT_VERSION_STATIC;
|
|
} else if (__asan_rt_version != ASAN_RT_VERSION_STATIC) {
|
|
ReportIncompatibleRT();
|
|
}
|
|
}
|
|
}
|
|
#endif // SANITIZER_ANDROID
|
|
|
|
#if !SANITIZER_ANDROID
|
|
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
|
|
ucontext_t *ucp = (ucontext_t*)context;
|
|
*stack = (uptr)ucp->uc_stack.ss_sp;
|
|
*ssize = ucp->uc_stack.ss_size;
|
|
}
|
|
#else
|
|
void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
|
|
UNIMPLEMENTED();
|
|
}
|
|
#endif
|
|
|
|
void *AsanDlSymNext(const char *sym) {
|
|
return dlsym(RTLD_NEXT, sym);
|
|
}
|
|
|
|
} // namespace __asan
|
|
|
|
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
|