2019-08-14 10:47:11 +02:00
|
|
|
//===-- sanitizer_posix_libcdep.cpp ---------------------------------------===//
|
2013-11-04 22:33:31 +01: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
|
2013-11-04 22:33:31 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries and implements libc-dependent POSIX-specific functions
|
|
|
|
// from sanitizer_libc.h.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_platform.h"
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
#if SANITIZER_POSIX
|
2015-10-21 09:32:45 +02:00
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
#include "sanitizer_common.h"
|
2014-05-22 09:09:21 +02:00
|
|
|
#include "sanitizer_flags.h"
|
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
|
|
|
#include "sanitizer_platform_limits_netbsd.h"
|
2014-05-22 09:09:21 +02:00
|
|
|
#include "sanitizer_platform_limits_posix.h"
|
2018-10-31 12:14:23 +01:00
|
|
|
#include "sanitizer_platform_limits_solaris.h"
|
2015-10-21 09:32:45 +02:00
|
|
|
#include "sanitizer_posix.h"
|
|
|
|
#include "sanitizer_procmaps.h"
|
2013-11-04 22:33:31 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-10-21 09:32:45 +02:00
|
|
|
#include <fcntl.h>
|
2013-11-04 22:33:31 +01:00
|
|
|
#include <pthread.h>
|
2014-05-22 09:09:21 +02:00
|
|
|
#include <signal.h>
|
2013-11-04 22:33:31 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/resource.h>
|
2015-10-21 09:32:45 +02:00
|
|
|
#include <sys/stat.h>
|
2013-11-04 22:33:31 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2016-11-08 23:04:09 +01:00
|
|
|
#include <sys/wait.h>
|
2013-11-04 22:33:31 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
#if SANITIZER_FREEBSD
|
|
|
|
// The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
|
|
|
|
// that, it was never implemented. So just define it to zero.
|
2018-10-31 12:14:23 +01:00
|
|
|
#undef MAP_NORESERVE
|
2015-10-21 09:32:45 +02:00
|
|
|
#define MAP_NORESERVE 0
|
|
|
|
#endif
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
u32 GetUid() {
|
|
|
|
return getuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr GetThreadSelf() {
|
|
|
|
return (uptr)pthread_self();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void ReleaseMemoryPagesToOS(uptr beg, uptr end) {
|
|
|
|
uptr page_size = GetPageSizeCached();
|
|
|
|
uptr beg_aligned = RoundUpTo(beg, page_size);
|
|
|
|
uptr end_aligned = RoundDownTo(end, page_size);
|
|
|
|
if (beg_aligned < end_aligned)
|
2020-10-16 10:03:04 +02:00
|
|
|
internal_madvise(beg_aligned, end_aligned - beg_aligned,
|
|
|
|
SANITIZER_MADVISE_DONTNEED);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:47:11 +02:00
|
|
|
void SetShadowRegionHugePageMode(uptr addr, uptr size) {
|
2015-10-21 09:32:45 +02:00
|
|
|
#ifdef MADV_NOHUGEPAGE // May not be defined on old systems.
|
2019-08-14 10:47:11 +02:00
|
|
|
if (common_flags()->no_huge_pages_for_shadow)
|
2020-10-16 10:03:04 +02:00
|
|
|
internal_madvise(addr, size, MADV_NOHUGEPAGE);
|
2019-08-14 10:47:11 +02:00
|
|
|
else
|
2020-10-16 10:03:04 +02:00
|
|
|
internal_madvise(addr, size, MADV_HUGEPAGE);
|
2015-10-21 09:32:45 +02:00
|
|
|
#endif // MADV_NOHUGEPAGE
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
bool DontDumpShadowMemory(uptr addr, uptr length) {
|
|
|
|
#if defined(MADV_DONTDUMP)
|
2020-10-16 10:03:04 +02:00
|
|
|
return internal_madvise(addr, length, MADV_DONTDUMP) == 0;
|
2018-10-31 12:14:23 +01:00
|
|
|
#elif defined(MADV_NOCORE)
|
2020-10-16 10:03:04 +02:00
|
|
|
return internal_madvise(addr, length, MADV_NOCORE) == 0;
|
2018-10-31 12:14:23 +01:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif // MADV_DONTDUMP
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
static rlim_t getlim(int res) {
|
|
|
|
rlimit rlim;
|
|
|
|
CHECK_EQ(0, getrlimit(res, &rlim));
|
|
|
|
return rlim.rlim_cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setlim(int res, rlim_t lim) {
|
2019-08-14 10:47:11 +02:00
|
|
|
struct rlimit rlim;
|
|
|
|
if (getrlimit(res, const_cast<struct rlimit *>(&rlim))) {
|
|
|
|
Report("ERROR: %s getrlimit() failed %d\n", SanitizerToolName, errno);
|
|
|
|
Die();
|
|
|
|
}
|
2014-09-23 19:59:53 +02:00
|
|
|
rlim.rlim_cur = lim;
|
2015-10-21 09:32:45 +02:00
|
|
|
if (setrlimit(res, const_cast<struct rlimit *>(&rlim))) {
|
2014-09-23 19:59:53 +02:00
|
|
|
Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisableCoreDumperIfNecessary() {
|
|
|
|
if (common_flags()->disable_coredump) {
|
|
|
|
setlim(RLIMIT_CORE, 0);
|
|
|
|
}
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StackSizeIsUnlimited() {
|
2014-09-23 19:59:53 +02:00
|
|
|
rlim_t stack_size = getlim(RLIMIT_STACK);
|
|
|
|
return (stack_size == RLIM_INFINITY);
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetStackSizeLimitInBytes(uptr limit) {
|
2014-09-23 19:59:53 +02:00
|
|
|
setlim(RLIMIT_STACK, (rlim_t)limit);
|
2013-11-04 22:33:31 +01:00
|
|
|
CHECK(!StackSizeIsUnlimited());
|
|
|
|
}
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
bool AddressSpaceIsUnlimited() {
|
|
|
|
rlim_t as_size = getlim(RLIMIT_AS);
|
|
|
|
return (as_size == RLIM_INFINITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAddressSpaceUnlimited() {
|
|
|
|
setlim(RLIMIT_AS, RLIM_INFINITY);
|
|
|
|
CHECK(AddressSpaceIsUnlimited());
|
|
|
|
}
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
void Abort() {
|
2016-11-08 23:04:09 +01:00
|
|
|
#if !SANITIZER_GO
|
|
|
|
// If we are handling SIGABRT, unhandle it first.
|
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
|
|
|
// TODO(vitalybuka): Check if handler belongs to sanitizer.
|
|
|
|
if (GetHandleSignalMode(SIGABRT) != kHandleSignalNo) {
|
2016-11-08 23:04:09 +01:00
|
|
|
struct sigaction sigact;
|
|
|
|
internal_memset(&sigact, 0, sizeof(sigact));
|
2021-05-12 14:37:22 +02:00
|
|
|
sigact.sa_handler = SIG_DFL;
|
2016-11-08 23:04:09 +01:00
|
|
|
internal_sigaction(SIGABRT, &sigact, nullptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-04 22:33:31 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Atexit(void (*function)(void)) {
|
2016-11-08 23:04:09 +01:00
|
|
|
#if !SANITIZER_GO
|
2013-11-04 22:33:31 +01:00
|
|
|
return atexit(function);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-27 19:43:33 +02:00
|
|
|
bool CreateDir(const char *pathname) { return mkdir(pathname, 0755) == 0; }
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
bool SupportsColoredOutput(fd_t fd) {
|
|
|
|
return isatty(fd) != 0;
|
2013-11-04 22:33:31 +01:00
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
#if !SANITIZER_GO
|
2014-05-22 09:09:21 +02:00
|
|
|
// TODO(glider): different tools may require different altstack size.
|
2021-04-17 11:27:14 +02:00
|
|
|
static uptr GetAltStackSize() {
|
2021-07-20 19:44:37 +02:00
|
|
|
// Note: since GLIBC_2.31, SIGSTKSZ may be a function call, so this may be
|
|
|
|
// more costly that you think. However GetAltStackSize is only call 2-3 times
|
|
|
|
// per thread so don't cache the evaluation.
|
|
|
|
return SIGSTKSZ * 4;
|
2021-04-17 11:27:14 +02:00
|
|
|
}
|
2014-05-22 09:09:21 +02:00
|
|
|
|
|
|
|
void SetAlternateSignalStack() {
|
|
|
|
stack_t altstack, oldstack;
|
2015-10-21 09:32:45 +02:00
|
|
|
CHECK_EQ(0, sigaltstack(nullptr, &oldstack));
|
2014-05-22 09:09:21 +02:00
|
|
|
// If the alternate stack is already in place, do nothing.
|
|
|
|
// Android always sets an alternate stack, but it's too small for us.
|
|
|
|
if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE)) return;
|
|
|
|
// TODO(glider): the mapped stack should have the MAP_STACK flag in the
|
|
|
|
// future. It is not required by man 2 sigaltstack now (they're using
|
|
|
|
// malloc()).
|
2021-04-17 11:27:14 +02:00
|
|
|
altstack.ss_size = GetAltStackSize();
|
|
|
|
altstack.ss_sp = (char *)MmapOrDie(altstack.ss_size, __func__);
|
2014-05-22 09:09:21 +02:00
|
|
|
altstack.ss_flags = 0;
|
2015-10-21 09:32:45 +02:00
|
|
|
CHECK_EQ(0, sigaltstack(&altstack, nullptr));
|
2014-05-22 09:09:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnsetAlternateSignalStack() {
|
|
|
|
stack_t altstack, oldstack;
|
2015-10-21 09:32:45 +02:00
|
|
|
altstack.ss_sp = nullptr;
|
2014-05-22 09:09:21 +02:00
|
|
|
altstack.ss_flags = SS_DISABLE;
|
2021-04-17 11:27:14 +02:00
|
|
|
altstack.ss_size = GetAltStackSize(); // Some sane value required on Darwin.
|
2014-05-22 09:09:21 +02:00
|
|
|
CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
|
|
|
|
UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MaybeInstallSigaction(int signum,
|
|
|
|
SignalHandlerType handler) {
|
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 (GetHandleSignalMode(signum) == kHandleSignalNo) return;
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
struct sigaction sigact;
|
|
|
|
internal_memset(&sigact, 0, sizeof(sigact));
|
|
|
|
sigact.sa_sigaction = (sa_sigaction_t)handler;
|
2014-09-23 19:59:53 +02:00
|
|
|
// Do not block the signal from being received in that signal's handler.
|
|
|
|
// Clients are responsible for handling this correctly.
|
|
|
|
sigact.sa_flags = SA_SIGINFO | SA_NODEFER;
|
2014-05-22 09:09:21 +02:00
|
|
|
if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
|
2015-10-21 09:32:45 +02:00
|
|
|
CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr));
|
2014-05-22 09:09:21 +02:00
|
|
|
VReport(1, "Installed the sigaction for signal %d\n", signum);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallDeadlySignalHandlers(SignalHandlerType handler) {
|
|
|
|
// Set the alternate signal stack for the main thread.
|
|
|
|
// This will cause SetAlternateSignalStack to be called twice, but the stack
|
|
|
|
// will be actually set only once.
|
|
|
|
if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
|
|
|
|
MaybeInstallSigaction(SIGSEGV, handler);
|
|
|
|
MaybeInstallSigaction(SIGBUS, handler);
|
2015-10-21 09:32:45 +02:00
|
|
|
MaybeInstallSigaction(SIGABRT, handler);
|
|
|
|
MaybeInstallSigaction(SIGFPE, handler);
|
2016-11-08 23:04:09 +01:00
|
|
|
MaybeInstallSigaction(SIGILL, handler);
|
2018-10-31 12:14:23 +01:00
|
|
|
MaybeInstallSigaction(SIGTRAP, handler);
|
2014-05-22 09:09:21 +02:00
|
|
|
}
|
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
|
|
|
|
|
|
|
bool SignalContext::IsStackOverflow() const {
|
|
|
|
// Access at a reasonable offset above SP, or slightly below it (to account
|
|
|
|
// for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
|
|
|
|
// probably a stack overflow.
|
|
|
|
#ifdef __s390__
|
|
|
|
// On s390, the fault address in siginfo points to start of the page, not
|
|
|
|
// to the precise word that was accessed. Mask off the low bits of sp to
|
|
|
|
// take it into account.
|
|
|
|
bool IsStackAccess = addr >= (sp & ~0xFFF) && addr < sp + 0xFFFF;
|
|
|
|
#else
|
2018-10-31 12:14:23 +01:00
|
|
|
// Let's accept up to a page size away from top of stack. Things like stack
|
|
|
|
// probing can trigger accesses with such large offsets.
|
|
|
|
bool IsStackAccess = addr + GetPageSizeCached() > sp && addr < sp + 0xFFFF;
|
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
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __powerpc__
|
|
|
|
// Large stack frames can be allocated with e.g.
|
|
|
|
// lis r0,-10000
|
|
|
|
// stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
|
|
|
|
// If the store faults then sp will not have been updated, so test above
|
|
|
|
// will not work, because the fault address will be more than just "slightly"
|
|
|
|
// below sp.
|
|
|
|
if (!IsStackAccess && IsAccessibleMemoryRange(pc, 4)) {
|
|
|
|
u32 inst = *(unsigned *)pc;
|
|
|
|
u32 ra = (inst >> 16) & 0x1F;
|
|
|
|
u32 opcd = inst >> 26;
|
|
|
|
u32 xo = (inst >> 1) & 0x3FF;
|
|
|
|
// Check for store-with-update to sp. The instructions we accept are:
|
|
|
|
// stbu rs,d(ra) stbux rs,ra,rb
|
|
|
|
// sthu rs,d(ra) sthux rs,ra,rb
|
|
|
|
// stwu rs,d(ra) stwux rs,ra,rb
|
|
|
|
// stdu rs,ds(ra) stdux rs,ra,rb
|
|
|
|
// where ra is r1 (the stack pointer).
|
|
|
|
if (ra == 1 &&
|
|
|
|
(opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
|
|
|
|
(opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
|
|
|
|
IsStackAccess = true;
|
|
|
|
}
|
|
|
|
#endif // __powerpc__
|
|
|
|
|
|
|
|
// We also check si_code to filter out SEGV caused by something else other
|
|
|
|
// then hitting the guard page or unmapped memory, like, for example,
|
|
|
|
// unaligned memory access.
|
|
|
|
auto si = static_cast<const siginfo_t *>(siginfo);
|
|
|
|
return IsStackAccess &&
|
|
|
|
(si->si_code == si_SEGV_MAPERR || si->si_code == si_SEGV_ACCERR);
|
|
|
|
}
|
|
|
|
|
2014-05-22 09:09:21 +02:00
|
|
|
#endif // SANITIZER_GO
|
|
|
|
|
2014-09-23 19:59:53 +02:00
|
|
|
bool IsAccessibleMemoryRange(uptr beg, uptr size) {
|
|
|
|
uptr page_size = GetPageSizeCached();
|
|
|
|
// Checking too large memory ranges is slow.
|
|
|
|
CHECK_LT(size, page_size * 10);
|
|
|
|
int sock_pair[2];
|
|
|
|
if (pipe(sock_pair))
|
|
|
|
return false;
|
|
|
|
uptr bytes_written =
|
|
|
|
internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size);
|
|
|
|
int write_errno;
|
|
|
|
bool result;
|
|
|
|
if (internal_iserror(bytes_written, &write_errno)) {
|
|
|
|
CHECK_EQ(EFAULT, write_errno);
|
|
|
|
result = false;
|
|
|
|
} else {
|
|
|
|
result = (bytes_written == size);
|
|
|
|
}
|
|
|
|
internal_close(sock_pair[0]);
|
|
|
|
internal_close(sock_pair[1]);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
|
2015-10-21 09:32:45 +02:00
|
|
|
// Some kinds of sandboxes may forbid filesystem access, so we won't be able
|
|
|
|
// to read the file mappings from /proc/self/maps. Luckily, neither the
|
|
|
|
// process will be able to load additional libraries, so it's fine to use the
|
|
|
|
// cached mappings.
|
|
|
|
MemoryMappingLayout::CacheMemoryMappings();
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:54:57 +01:00
|
|
|
static bool MmapFixed(uptr fixed_addr, uptr size, int additional_flags,
|
|
|
|
const char *name) {
|
2019-08-14 10:47:11 +02:00
|
|
|
size = RoundUpTo(size, GetPageSizeCached());
|
|
|
|
fixed_addr = RoundDownTo(fixed_addr, GetPageSizeCached());
|
2019-11-05 14:54:57 +01:00
|
|
|
uptr p =
|
|
|
|
MmapNamed((void *)fixed_addr, size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_FIXED | additional_flags | MAP_ANON, name);
|
2015-10-21 09:32:45 +02:00
|
|
|
int reserrno;
|
2018-10-31 12:14:23 +01:00
|
|
|
if (internal_iserror(p, &reserrno)) {
|
2015-10-21 09:32:45 +02:00
|
|
|
Report("ERROR: %s failed to "
|
|
|
|
"allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n",
|
|
|
|
SanitizerToolName, size, size, fixed_addr, reserrno);
|
2018-10-31 12:14:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-21 09:32:45 +02:00
|
|
|
IncreaseTotalMmap(size);
|
2018-10-31 12:14:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:54:57 +01:00
|
|
|
bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
|
|
|
|
return MmapFixed(fixed_addr, size, MAP_NORESERVE, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MmapFixedSuperNoReserve(uptr fixed_addr, uptr size, const char *name) {
|
|
|
|
#if SANITIZER_FREEBSD
|
|
|
|
if (common_flags()->no_huge_pages_for_shadow)
|
|
|
|
return MmapFixedNoReserve(fixed_addr, size, name);
|
|
|
|
// MAP_NORESERVE is implicit with FreeBSD
|
|
|
|
return MmapFixed(fixed_addr, size, MAP_ALIGNED_SUPER, name);
|
|
|
|
#else
|
|
|
|
bool r = MmapFixedNoReserve(fixed_addr, size, name);
|
|
|
|
if (r)
|
|
|
|
SetShadowRegionHugePageMode(fixed_addr, size);
|
|
|
|
return r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:14:23 +01:00
|
|
|
uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) {
|
2019-08-14 10:47:11 +02:00
|
|
|
base_ = fixed_addr ? MmapFixedNoAccess(fixed_addr, size, name)
|
|
|
|
: MmapNoAccess(size);
|
2018-10-31 12:14:23 +01:00
|
|
|
size_ = size;
|
|
|
|
name_ = name;
|
|
|
|
(void)os_handle_; // unsupported
|
|
|
|
return reinterpret_cast<uptr>(base_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uses fixed_addr for now.
|
|
|
|
// Will use offset instead once we've implemented this function for real.
|
2019-08-14 10:47:11 +02:00
|
|
|
uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size, const char *name) {
|
|
|
|
return reinterpret_cast<uptr>(
|
|
|
|
MmapFixedOrDieOnFatalError(fixed_addr, size, name));
|
2018-10-31 12:14:23 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:47:11 +02:00
|
|
|
uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr size,
|
|
|
|
const char *name) {
|
|
|
|
return reinterpret_cast<uptr>(MmapFixedOrDie(fixed_addr, size, name));
|
2018-10-31 12:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReservedAddressRange::Unmap(uptr addr, uptr size) {
|
|
|
|
CHECK_LE(size, size_);
|
|
|
|
if (addr == reinterpret_cast<uptr>(base_))
|
|
|
|
// If we unmap the whole range, just null out the base.
|
|
|
|
base_ = (size == size_) ? nullptr : reinterpret_cast<void*>(addr + size);
|
|
|
|
else
|
|
|
|
CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);
|
|
|
|
size_ -= size;
|
|
|
|
UnmapOrDie(reinterpret_cast<void*>(addr), size);
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
|
2019-08-14 10:47:11 +02:00
|
|
|
return (void *)MmapNamed((void *)fixed_addr, size, PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE | MAP_ANON,
|
|
|
|
name);
|
2015-10-21 09:32:45 +02:00
|
|
|
}
|
2015-11-23 10:07:18 +01:00
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
void *MmapNoAccess(uptr size) {
|
|
|
|
unsigned flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE;
|
|
|
|
return (void *)internal_mmap(nullptr, size, PROT_NONE, flags, -1, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-23 10:07:18 +01:00
|
|
|
// This function is defined elsewhere if we intercepted pthread_attr_getstack.
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_WEAK_ATTRIBUTE int
|
|
|
|
real_pthread_attr_getstack(void *attr, void **addr, size_t *size);
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
int my_pthread_attr_getstack(void *attr, void **addr, uptr *size) {
|
|
|
|
#if !SANITIZER_GO && !SANITIZER_MAC
|
|
|
|
if (&real_pthread_attr_getstack)
|
|
|
|
return real_pthread_attr_getstack((pthread_attr_t *)attr, addr,
|
|
|
|
(size_t *)size);
|
|
|
|
#endif
|
|
|
|
return pthread_attr_getstack((pthread_attr_t *)attr, addr, (size_t *)size);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !SANITIZER_GO
|
|
|
|
void AdjustStackSize(void *attr_) {
|
|
|
|
pthread_attr_t *attr = (pthread_attr_t *)attr_;
|
|
|
|
uptr stackaddr = 0;
|
|
|
|
uptr stacksize = 0;
|
|
|
|
my_pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize);
|
|
|
|
// GLibC will return (0 - stacksize) as the stack address in the case when
|
|
|
|
// stacksize is set, but stackaddr is not.
|
|
|
|
bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0);
|
|
|
|
// We place a lot of tool data into TLS, account for that.
|
|
|
|
const uptr minstacksize = GetTlsSize() + 128*1024;
|
|
|
|
if (stacksize < minstacksize) {
|
|
|
|
if (!stack_set) {
|
|
|
|
if (stacksize != 0) {
|
|
|
|
VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize,
|
|
|
|
minstacksize);
|
|
|
|
pthread_attr_setstacksize(attr, minstacksize);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Printf("Sanitizer: pre-allocated stack size is insufficient: "
|
|
|
|
"%zu < %zu\n", stacksize, minstacksize);
|
|
|
|
Printf("Sanitizer: pthread_create is likely to fail.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // !SANITIZER_GO
|
|
|
|
|
2016-11-08 23:04:09 +01:00
|
|
|
pid_t StartSubprocess(const char *program, const char *const argv[],
|
2020-06-01 21:15:18 +02:00
|
|
|
const char *const envp[], fd_t stdin_fd, fd_t stdout_fd,
|
|
|
|
fd_t stderr_fd) {
|
2016-11-08 23:04:09 +01:00
|
|
|
auto file_closer = at_scope_exit([&] {
|
|
|
|
if (stdin_fd != kInvalidFd) {
|
|
|
|
internal_close(stdin_fd);
|
|
|
|
}
|
|
|
|
if (stdout_fd != kInvalidFd) {
|
|
|
|
internal_close(stdout_fd);
|
|
|
|
}
|
|
|
|
if (stderr_fd != kInvalidFd) {
|
|
|
|
internal_close(stderr_fd);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
int pid = internal_fork();
|
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
int rverrno;
|
|
|
|
if (internal_iserror(pid, &rverrno)) {
|
|
|
|
Report("WARNING: failed to fork (errno %d)\n", rverrno);
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
// Child subprocess
|
|
|
|
if (stdin_fd != kInvalidFd) {
|
|
|
|
internal_close(STDIN_FILENO);
|
|
|
|
internal_dup2(stdin_fd, STDIN_FILENO);
|
|
|
|
internal_close(stdin_fd);
|
|
|
|
}
|
|
|
|
if (stdout_fd != kInvalidFd) {
|
|
|
|
internal_close(STDOUT_FILENO);
|
|
|
|
internal_dup2(stdout_fd, STDOUT_FILENO);
|
|
|
|
internal_close(stdout_fd);
|
|
|
|
}
|
|
|
|
if (stderr_fd != kInvalidFd) {
|
|
|
|
internal_close(STDERR_FILENO);
|
|
|
|
internal_dup2(stderr_fd, STDERR_FILENO);
|
|
|
|
internal_close(stderr_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd);
|
|
|
|
|
2020-06-01 21:15:18 +02:00
|
|
|
internal_execve(program, const_cast<char **>(&argv[0]),
|
|
|
|
const_cast<char *const *>(envp));
|
2016-11-08 23:04:09 +01:00
|
|
|
internal__exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsProcessRunning(pid_t pid) {
|
|
|
|
int process_status;
|
|
|
|
uptr waitpid_status = internal_waitpid(pid, &process_status, WNOHANG);
|
|
|
|
int local_errno;
|
|
|
|
if (internal_iserror(waitpid_status, &local_errno)) {
|
|
|
|
VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return waitpid_status == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WaitForProcess(pid_t pid) {
|
|
|
|
int process_status;
|
|
|
|
uptr waitpid_status = internal_waitpid(pid, &process_status, 0);
|
|
|
|
int local_errno;
|
|
|
|
if (internal_iserror(waitpid_status, &local_errno)) {
|
|
|
|
VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return process_status;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
bool IsStateDetached(int state) {
|
|
|
|
return state == PTHREAD_CREATE_DETACHED;
|
|
|
|
}
|
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
} // namespace __sanitizer
|
2013-11-04 22:33:31 +01:00
|
|
|
|
2015-10-21 09:32:45 +02:00
|
|
|
#endif // SANITIZER_POSIX
|