2013-05-21 18:27:07 +08:00
|
|
|
//===-- sanitizer_posix_libcdep.cc ----------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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-03-04 16:55:41 +08:00
|
|
|
#if SANITIZER_POSIX
|
2015-10-01 07:52:54 +08:00
|
|
|
|
2013-05-21 18:27:07 +08:00
|
|
|
#include "sanitizer_common.h"
|
2014-01-31 21:10:07 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2017-08-29 05:03:23 +08:00
|
|
|
#include "sanitizer_platform_limits_netbsd.h"
|
2014-01-31 21:10:07 +08:00
|
|
|
#include "sanitizer_platform_limits_posix.h"
|
[Sanitizers] Basic sanitizer Solaris support (PR 33274)
Summary:
This is the first mostly working version of the Sanitizer port to 32-bit Solaris/x86.
It is currently based on Solaris 11.4 Beta.
This part was initially developed inside libsanitizer in the GCC tree and should apply to
both. Subsequent parts will address changes to clang, the compiler-rt build system
and testsuite.
I'm not yet sure what the right patch granularity is: if it's profitable to split the patch
up, I'd like to get guidance on how to do so.
Most of the changes are probably straightforward with a few exceptions:
* The Solaris syscall interface isn't stable, undocumented and can change within an
OS release. The stable interface is the libc interface, which I'm using here, if possible
using the internal _-prefixed names.
* While the patch primarily target 32-bit x86, I've left a few sparc changes in. They
cannot currently be used with clang due to a backend limitation, but have worked
fine inside the gcc tree.
* Some functions (e.g. largefile versions of functions like open64) only exist in 32-bit
Solaris, so I've introduced a separate SANITIZER_SOLARIS32 to check for that.
The patch (with the subsequent ones to be submitted shortly) was tested
on i386-pc-solaris2.11. Only a few failures remain, some of them analyzed, some
still TBD:
AddressSanitizer-i386-sunos :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos :: TestCases/malloc-no-intercept.c
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/start-deactivated.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/default_options.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/malloc-no-intercept.c
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/MemoryMappingLayout.DumpListOfModules
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/SanitizerCommon.PthreadDestructorIterations
Maybe this is good enough the get the ball rolling.
Reviewers: kcc, alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, jyknight, kubamracek, krytarowski, fedor.sergeev, llvm-commits, #sanitizers
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D40898
llvm-svn: 320740
2017-12-15 04:14:29 +08:00
|
|
|
#include "sanitizer_platform_limits_solaris.h"
|
2015-04-09 01:08:24 +08:00
|
|
|
#include "sanitizer_posix.h"
|
2015-04-02 01:56:29 +08:00
|
|
|
#include "sanitizer_procmaps.h"
|
2013-05-21 18:27:07 +08:00
|
|
|
#include "sanitizer_stacktrace.h"
|
2015-04-02 01:56:29 +08:00
|
|
|
#include "sanitizer_symbolizer.h"
|
2013-05-21 18:27:07 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
2015-05-30 06:31:28 +08:00
|
|
|
#include <fcntl.h>
|
2013-05-21 18:27:07 +08:00
|
|
|
#include <pthread.h>
|
2014-01-28 19:12:29 +08:00
|
|
|
#include <signal.h>
|
2013-05-21 18:27:07 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/resource.h>
|
2015-07-24 06:05:20 +08:00
|
|
|
#include <sys/stat.h>
|
2013-05-21 18:27:07 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2016-01-27 04:10:01 +08:00
|
|
|
#include <sys/wait.h>
|
2013-05-21 18:27:07 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-06-04 08:42:15 +08: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.
|
|
|
|
#undef MAP_NORESERVE
|
|
|
|
#define MAP_NORESERVE 0
|
|
|
|
#endif
|
|
|
|
|
2016-09-08 07:40:53 +08:00
|
|
|
typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
|
|
|
|
|
2013-05-21 18:27:07 +08:00
|
|
|
namespace __sanitizer {
|
|
|
|
|
|
|
|
u32 GetUid() {
|
|
|
|
return getuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr GetThreadSelf() {
|
|
|
|
return (uptr)pthread_self();
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:41:59 +08: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)
|
[Sanitizers] Basic sanitizer Solaris support (PR 33274)
Summary:
This is the first mostly working version of the Sanitizer port to 32-bit Solaris/x86.
It is currently based on Solaris 11.4 Beta.
This part was initially developed inside libsanitizer in the GCC tree and should apply to
both. Subsequent parts will address changes to clang, the compiler-rt build system
and testsuite.
I'm not yet sure what the right patch granularity is: if it's profitable to split the patch
up, I'd like to get guidance on how to do so.
Most of the changes are probably straightforward with a few exceptions:
* The Solaris syscall interface isn't stable, undocumented and can change within an
OS release. The stable interface is the libc interface, which I'm using here, if possible
using the internal _-prefixed names.
* While the patch primarily target 32-bit x86, I've left a few sparc changes in. They
cannot currently be used with clang due to a backend limitation, but have worked
fine inside the gcc tree.
* Some functions (e.g. largefile versions of functions like open64) only exist in 32-bit
Solaris, so I've introduced a separate SANITIZER_SOLARIS32 to check for that.
The patch (with the subsequent ones to be submitted shortly) was tested
on i386-pc-solaris2.11. Only a few failures remain, some of them analyzed, some
still TBD:
AddressSanitizer-i386-sunos :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos :: TestCases/malloc-no-intercept.c
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/start-deactivated.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/default_options.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/malloc-no-intercept.c
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/MemoryMappingLayout.DumpListOfModules
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/SanitizerCommon.PthreadDestructorIterations
Maybe this is good enough the get the ball rolling.
Reviewers: kcc, alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, jyknight, kubamracek, krytarowski, fedor.sergeev, llvm-commits, #sanitizers
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D40898
llvm-svn: 320740
2017-12-15 04:14:29 +08:00
|
|
|
// In the default Solaris compilation environment, madvise() is declared
|
|
|
|
// to take a caddr_t arg; casting it to void * results in an invalid
|
|
|
|
// conversion error, so use char * instead.
|
|
|
|
madvise((char *)beg_aligned, end_aligned - beg_aligned,
|
2017-12-14 08:04:30 +08:00
|
|
|
SANITIZER_MADVISE_DONTNEED);
|
2013-05-21 18:27:07 +08:00
|
|
|
}
|
|
|
|
|
2015-01-21 10:05:31 +08:00
|
|
|
void NoHugePagesInRegion(uptr addr, uptr size) {
|
|
|
|
#ifdef MADV_NOHUGEPAGE // May not be defined on old systems.
|
|
|
|
madvise((void *)addr, size, MADV_NOHUGEPAGE);
|
|
|
|
#endif // MADV_NOHUGEPAGE
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:15:15 +08:00
|
|
|
void DontDumpShadowMemory(uptr addr, uptr length) {
|
|
|
|
#ifdef MADV_DONTDUMP
|
|
|
|
madvise((void *)addr, length, MADV_DONTDUMP);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:31:19 +08: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) {
|
|
|
|
// The following magic is to prevent clang from replacing it with memset.
|
|
|
|
volatile struct rlimit rlim;
|
|
|
|
rlim.rlim_cur = lim;
|
|
|
|
rlim.rlim_max = lim;
|
2014-11-14 06:40:59 +08:00
|
|
|
if (setrlimit(res, const_cast<struct rlimit *>(&rlim))) {
|
2014-08-13 06:31:19 +08:00
|
|
|
Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisableCoreDumperIfNecessary() {
|
|
|
|
if (common_flags()->disable_coredump) {
|
|
|
|
setlim(RLIMIT_CORE, 0);
|
|
|
|
}
|
2013-05-21 18:27:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StackSizeIsUnlimited() {
|
2014-08-13 06:31:19 +08:00
|
|
|
rlim_t stack_size = getlim(RLIMIT_STACK);
|
2014-09-06 15:52:51 +08:00
|
|
|
return (stack_size == RLIM_INFINITY);
|
2013-05-21 18:27:07 +08:00
|
|
|
}
|
|
|
|
|
2016-05-28 08:25:16 +08:00
|
|
|
uptr GetStackSizeLimitInBytes() {
|
|
|
|
return (uptr)getlim(RLIMIT_STACK);
|
|
|
|
}
|
|
|
|
|
2013-05-21 18:27:07 +08:00
|
|
|
void SetStackSizeLimitInBytes(uptr limit) {
|
2014-08-13 06:31:19 +08:00
|
|
|
setlim(RLIMIT_STACK, (rlim_t)limit);
|
2013-05-21 18:27:07 +08:00
|
|
|
CHECK(!StackSizeIsUnlimited());
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:31:19 +08:00
|
|
|
bool AddressSpaceIsUnlimited() {
|
|
|
|
rlim_t as_size = getlim(RLIMIT_AS);
|
2014-09-06 15:52:51 +08:00
|
|
|
return (as_size == RLIM_INFINITY);
|
2014-08-13 06:31:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetAddressSpaceUnlimited() {
|
2014-09-06 15:52:51 +08:00
|
|
|
setlim(RLIMIT_AS, RLIM_INFINITY);
|
2014-08-13 06:31:19 +08:00
|
|
|
CHECK(AddressSpaceIsUnlimited());
|
|
|
|
}
|
|
|
|
|
2013-05-21 18:27:07 +08:00
|
|
|
void SleepForSeconds(int seconds) {
|
|
|
|
sleep(seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SleepForMillis(int millis) {
|
|
|
|
usleep(millis * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Abort() {
|
2016-10-29 04:14:18 +08:00
|
|
|
#if !SANITIZER_GO
|
2016-09-08 07:40:53 +08:00
|
|
|
// If we are handling SIGABRT, unhandle it first.
|
2017-05-26 07:42:33 +08:00
|
|
|
// TODO(vitalybuka): Check if handler belongs to sanitizer.
|
|
|
|
if (GetHandleSignalMode(SIGABRT) != kHandleSignalNo) {
|
2016-09-08 07:40:53 +08:00
|
|
|
struct sigaction sigact;
|
|
|
|
internal_memset(&sigact, 0, sizeof(sigact));
|
|
|
|
sigact.sa_sigaction = (sa_sigaction_t)SIG_DFL;
|
|
|
|
internal_sigaction(SIGABRT, &sigact, nullptr);
|
|
|
|
}
|
2016-09-09 03:16:01 +08:00
|
|
|
#endif
|
2016-09-08 07:40:53 +08:00
|
|
|
|
2013-05-21 18:27:07 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Atexit(void (*function)(void)) {
|
2016-10-29 04:14:18 +08:00
|
|
|
#if !SANITIZER_GO
|
2013-05-21 18:27:07 +08:00
|
|
|
return atexit(function);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-09 01:42:57 +08:00
|
|
|
bool SupportsColoredOutput(fd_t fd) {
|
|
|
|
return isatty(fd) != 0;
|
2013-05-21 18:27:07 +08:00
|
|
|
}
|
|
|
|
|
2016-10-29 04:14:18 +08:00
|
|
|
#if !SANITIZER_GO
|
2014-01-28 19:12:29 +08:00
|
|
|
// TODO(glider): different tools may require different altstack size.
|
|
|
|
static const uptr kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough.
|
|
|
|
|
|
|
|
void SetAlternateSignalStack() {
|
|
|
|
stack_t altstack, oldstack;
|
2015-10-01 07:52:54 +08:00
|
|
|
CHECK_EQ(0, sigaltstack(nullptr, &oldstack));
|
2014-01-28 19:12:29 +08:00
|
|
|
// If the alternate stack is already in place, do nothing.
|
2014-02-18 19:14:30 +08:00
|
|
|
// Android always sets an alternate stack, but it's too small for us.
|
|
|
|
if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE)) return;
|
2014-01-28 19:12:29 +08:00
|
|
|
// 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()).
|
2014-02-27 04:33:22 +08:00
|
|
|
void* base = MmapOrDie(kAltStackSize, __func__);
|
2014-03-04 16:55:41 +08:00
|
|
|
altstack.ss_sp = (char*) base;
|
2014-01-28 19:12:29 +08:00
|
|
|
altstack.ss_flags = 0;
|
|
|
|
altstack.ss_size = kAltStackSize;
|
2015-10-01 07:52:54 +08:00
|
|
|
CHECK_EQ(0, sigaltstack(&altstack, nullptr));
|
2014-01-28 19:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnsetAlternateSignalStack() {
|
|
|
|
stack_t altstack, oldstack;
|
2015-10-01 07:52:54 +08:00
|
|
|
altstack.ss_sp = nullptr;
|
2014-01-28 19:12:29 +08:00
|
|
|
altstack.ss_flags = SS_DISABLE;
|
2014-04-25 18:39:41 +08:00
|
|
|
altstack.ss_size = kAltStackSize; // Some sane value required on Darwin.
|
2014-01-28 19:12:29 +08:00
|
|
|
CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
|
|
|
|
UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
|
|
|
|
}
|
|
|
|
|
2014-01-31 21:10:07 +08:00
|
|
|
static void MaybeInstallSigaction(int signum,
|
|
|
|
SignalHandlerType handler) {
|
2017-06-21 09:10:23 +08:00
|
|
|
if (GetHandleSignalMode(signum) == kHandleSignalNo) return;
|
2017-05-26 07:42:33 +08:00
|
|
|
|
2014-01-31 21:10:07 +08:00
|
|
|
struct sigaction sigact;
|
|
|
|
internal_memset(&sigact, 0, sizeof(sigact));
|
|
|
|
sigact.sa_sigaction = (sa_sigaction_t)handler;
|
2014-09-04 17:34:22 +08: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-01-31 21:10:07 +08:00
|
|
|
if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
|
2015-10-01 07:52:54 +08:00
|
|
|
CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr));
|
2014-01-31 21:10:07 +08: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-05-05 09:37:33 +08:00
|
|
|
MaybeInstallSigaction(SIGABRT, handler);
|
2015-08-07 01:52:54 +08:00
|
|
|
MaybeInstallSigaction(SIGFPE, handler);
|
2015-12-15 08:33:45 +08:00
|
|
|
MaybeInstallSigaction(SIGILL, handler);
|
2014-01-31 21:10:07 +08:00
|
|
|
}
|
2017-09-13 14:25:09 +08:00
|
|
|
|
2017-09-14 11:23:02 +08:00
|
|
|
bool SignalContext::IsStackOverflow() const {
|
2017-09-13 08:24:44 +08:00
|
|
|
// 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.
|
2017-09-17 17:38:55 +08:00
|
|
|
bool IsStackAccess = addr >= (sp & ~0xFFF) && addr < sp + 0xFFFF;
|
2017-09-13 08:24:44 +08:00
|
|
|
#else
|
2017-09-14 11:23:02 +08:00
|
|
|
bool IsStackAccess = addr + 512 > sp && addr < sp + 0xFFFF;
|
2017-09-13 08:24:44 +08: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.
|
2017-09-14 11:23:02 +08:00
|
|
|
if (!IsStackAccess && IsAccessibleMemoryRange(pc, 4)) {
|
|
|
|
u32 inst = *(unsigned *)pc;
|
2017-09-13 08:24:44 +08:00
|
|
|
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.
|
2017-09-14 11:23:02 +08:00
|
|
|
auto si = static_cast<const siginfo_t *>(siginfo);
|
2017-09-14 02:30:06 +08:00
|
|
|
return IsStackAccess &&
|
|
|
|
(si->si_code == si_SEGV_MAPERR || si->si_code == si_SEGV_ACCERR);
|
2017-09-13 08:24:44 +08:00
|
|
|
}
|
|
|
|
|
2014-01-31 22:30:12 +08:00
|
|
|
#endif // SANITIZER_GO
|
2014-01-31 21:10:07 +08:00
|
|
|
|
2014-09-18 01:56:15 +08: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;
|
|
|
|
}
|
|
|
|
|
2015-04-02 01:56:29 +08:00
|
|
|
void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
|
|
|
|
// 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();
|
|
|
|
// Same for /proc/self/exe in the symbolizer.
|
|
|
|
#if !SANITIZER_GO
|
|
|
|
Symbolizer::GetOrInit()->PrepareForSandboxing();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:54:25 +08:00
|
|
|
#if SANITIZER_ANDROID || SANITIZER_GO
|
2015-05-30 06:31:28 +08:00
|
|
|
int GetNamedMappingFd(const char *name, uptr size) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int GetNamedMappingFd(const char *name, uptr size) {
|
|
|
|
if (!common_flags()->decorate_proc_maps)
|
|
|
|
return -1;
|
|
|
|
char shmname[200];
|
|
|
|
CHECK(internal_strlen(name) < sizeof(shmname) - 10);
|
|
|
|
internal_snprintf(shmname, sizeof(shmname), "%zu [%s]", internal_getpid(),
|
|
|
|
name);
|
|
|
|
int fd = shm_open(shmname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
|
|
|
|
CHECK_GE(fd, 0);
|
|
|
|
int res = internal_ftruncate(fd, size);
|
|
|
|
CHECK_EQ(0, res);
|
|
|
|
res = shm_unlink(shmname);
|
|
|
|
CHECK_EQ(0, res);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
|
|
|
|
int fd = name ? GetNamedMappingFd(name, size) : -1;
|
|
|
|
unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE;
|
|
|
|
if (fd == -1) flags |= MAP_ANON;
|
|
|
|
|
|
|
|
uptr PageSize = GetPageSizeCached();
|
|
|
|
uptr p = internal_mmap((void *)(fixed_addr & ~(PageSize - 1)),
|
|
|
|
RoundUpTo(size, PageSize), PROT_READ | PROT_WRITE,
|
|
|
|
flags, fd, 0);
|
|
|
|
int reserrno;
|
|
|
|
if (internal_iserror(p, &reserrno))
|
|
|
|
Report("ERROR: %s failed to "
|
|
|
|
"allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n",
|
|
|
|
SanitizerToolName, size, size, fixed_addr, reserrno);
|
|
|
|
IncreaseTotalMmap(size);
|
|
|
|
return (void *)p;
|
|
|
|
}
|
|
|
|
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
uptr ReservedAddressRange::Init(uptr size, const char *name, uptr fixed_addr) {
|
2017-11-08 00:19:24 +08:00
|
|
|
// We don't pass `name` along because, when you enable `decorate_proc_maps`
|
|
|
|
// AND actually use a named mapping AND are using a sanitizer intercepting
|
|
|
|
// `open` (e.g. TSAN, ESAN), then you'll get a failure during initialization.
|
|
|
|
// TODO(flowerhack): Fix the implementation of GetNamedMappingFd to solve
|
|
|
|
// this problem.
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
if (fixed_addr) {
|
2017-11-08 00:19:24 +08:00
|
|
|
base_ = MmapFixedNoAccess(fixed_addr, size);
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
} else {
|
|
|
|
base_ = MmapNoAccess(size);
|
|
|
|
}
|
|
|
|
size_ = size;
|
|
|
|
name_ = name;
|
2017-11-28 03:53:53 +08:00
|
|
|
(void)os_handle_; // unsupported
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
return reinterpret_cast<uptr>(base_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uses fixed_addr for now.
|
|
|
|
// Will use offset instead once we've implemented this function for real.
|
2017-11-08 00:19:24 +08:00
|
|
|
uptr ReservedAddressRange::Map(uptr fixed_addr, uptr size) {
|
|
|
|
return reinterpret_cast<uptr>(MmapFixedOrDieOnFatalError(fixed_addr, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr size) {
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
return reinterpret_cast<uptr>(MmapFixedOrDie(fixed_addr, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReservedAddressRange::Unmap(uptr addr, uptr size) {
|
|
|
|
void* addr_as_void = reinterpret_cast<void*>(addr);
|
|
|
|
uptr base_as_uptr = reinterpret_cast<uptr>(base_);
|
|
|
|
// Only unmap at the beginning or end of the range.
|
|
|
|
CHECK((addr_as_void == base_) || (addr + size == base_as_uptr + size_));
|
|
|
|
CHECK_LE(size, size_);
|
|
|
|
UnmapOrDie(reinterpret_cast<void*>(addr), size);
|
2017-11-14 04:38:22 +08:00
|
|
|
if (addr_as_void == base_) {
|
|
|
|
base_ = reinterpret_cast<void*>(addr + size);
|
|
|
|
}
|
|
|
|
size_ = size_ - size;
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 07:46:53 +08:00
|
|
|
void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
|
2015-05-30 06:31:28 +08:00
|
|
|
int fd = name ? GetNamedMappingFd(name, size) : -1;
|
2016-04-23 07:27:15 +08:00
|
|
|
unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE;
|
2015-05-30 06:31:28 +08:00
|
|
|
if (fd == -1) flags |= MAP_ANON;
|
|
|
|
|
|
|
|
return (void *)internal_mmap((void *)fixed_addr, size, PROT_NONE, flags, fd,
|
|
|
|
0);
|
|
|
|
}
|
2015-11-03 22:33:39 +08:00
|
|
|
|
2016-04-23 08:05:24 +08: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-03 22:33:39 +08: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-01-27 04:10:01 +08:00
|
|
|
pid_t StartSubprocess(const char *program, const char *const argv[],
|
|
|
|
fd_t stdin_fd, fd_t stdout_fd, fd_t stderr_fd) {
|
|
|
|
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);
|
|
|
|
|
2016-01-28 04:35:18 +08:00
|
|
|
execv(program, const_cast<char **>(&argv[0]));
|
2016-01-27 04:10:01 +08: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;
|
|
|
|
}
|
|
|
|
|
2016-01-28 07:51:36 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-14 01:28:52 +08:00
|
|
|
bool IsStateDetached(int state) {
|
|
|
|
return state == PTHREAD_CREATE_DETACHED;
|
|
|
|
}
|
|
|
|
|
2015-07-24 06:05:20 +08:00
|
|
|
} // namespace __sanitizer
|
2013-05-21 18:27:07 +08:00
|
|
|
|
2015-10-01 07:52:54 +08:00
|
|
|
#endif // SANITIZER_POSIX
|