[Sanitizer] move ShadowRangeIsAvailable and several defines to common runtime

llvm-svn: 158499
This commit is contained in:
Alexey Samsonov 2012-06-15 07:29:14 +00:00
parent 2a4d13c5cf
commit 40e5128412
8 changed files with 49 additions and 44 deletions

View File

@ -121,7 +121,6 @@ void ReplaceSystemMalloc();
// asan_linux.cc / asan_mac.cc / asan_win.cc
void *AsanDoesNotSupportStaticLinkage();
bool AsanShadowRangeIsAvailable();
void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp);
@ -203,22 +202,12 @@ enum LinkerInitialized { LINKER_INITIALIZED = 0 };
# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
#endif
#ifndef _WIN32
const uptr kMmapGranularity = kPageSize;
# define THREAD_CALLING_CONV
typedef void* thread_return_t;
#else
const uptr kMmapGranularity = 1UL << 16;
# define THREAD_CALLING_CONV __stdcall
typedef DWORD thread_return_t;
#ifdef _WIN32
# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER
# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize
bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size);
# endif
#endif
typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
#endif // _WIN32
// These magic values are written to shadow for better error reporting.
const int kAsanHeapLeftRedzoneMagic = 0xfa;

View File

@ -32,31 +32,6 @@ static const uptr kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough.
namespace __asan {
static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
uptr start2, uptr end2) {
CHECK(start1 <= end1);
CHECK(start2 <= end2);
return (end1 < start2) || (end2 < start1);
}
// FIXME: this is thread-unsafe, but should not cause problems most of the time.
// When the shadow is mapped only a single thread usually exists (plus maybe
// several worker threads on Mac, which aren't expected to map big chunks of
// memory).
bool AsanShadowRangeIsAvailable() {
ProcessMaps procmaps;
uptr start, end;
uptr shadow_start = kLowShadowBeg;
if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
uptr shadow_end = kHighShadowEnd;
while (procmaps.Next(&start, &end,
/*offset*/0, /*filename*/0, /*filename_size*/0)) {
if (!IntervalsAreSeparate(start, end, shadow_start, shadow_end))
return false;
}
return true;
}
static void MaybeInstallSigaction(int signum,
void (*handler)(int, siginfo_t *, void *)) {
if (!AsanInterceptsSignal(signum))

View File

@ -539,7 +539,10 @@ void __asan_init() {
DisableCoreDumper();
}
if (AsanShadowRangeIsAvailable()) {
uptr shadow_start = kLowShadowBeg;
if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
uptr shadow_end = kHighShadowEnd;
if (MemoryRangeIsAvailable(shadow_start, shadow_end)) {
if (kLowShadowBeg != kLowShadowEnd) {
// mmap the low shadow plus at least one page.
ReserveShadowMemoryRange(kLowShadowBeg - kMmapGranularity, kLowShadowEnd);

View File

@ -159,11 +159,6 @@ void *AsanDoesNotSupportStaticLinkage() {
return 0;
}
bool AsanShadowRangeIsAvailable() {
// FIXME: shall we do anything here on Windows?
return true;
}
void SetAlternateSignalStack() {
// FIXME: Decide what to do on Windows.
}

View File

@ -25,6 +25,11 @@ const uptr kWordSize = __WORDSIZE / 8;
const uptr kWordSizeInBits = 8 * kWordSize;
const uptr kPageSizeBits = 12;
const uptr kPageSize = 1UL << kPageSizeBits;
#ifndef _WIN32
const uptr kMmapGranularity = kPageSize;
#else
const uptr kMmapGranularity = 1UL << 16;
#endif
// Threads
int GetPid();
@ -37,6 +42,8 @@ void *MmapOrDie(uptr size, const char *mem_type);
void UnmapOrDie(void *addr, uptr size);
void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
void *Mprotect(uptr fixed_addr, uptr size);
// Used to check if we can map shadow memory to a fixed location.
bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
// Internal allocator
void *InternalAlloc(uptr size);

View File

@ -51,6 +51,15 @@ typedef unsigned long DWORD; // NOLINT
# define USED __attribute__((used))
#endif
#if defined(_WIN32)
typedef DWORD thread_return_t;
# define THREAD_CALLING_CONV __stdcall
#else // _WIN32
typedef void* thread_return_t;
# define THREAD_CALLING_CONV
#endif // _WIN32
typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
// If __WORDSIZE was undefined by the platform, define it in terms of the
// compiler built-ins __LP64__ and _WIN64.
#ifndef __WORDSIZE

View File

@ -80,6 +80,28 @@ void *Mprotect(uptr fixed_addr, uptr size) {
0, 0);
}
static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
uptr start2, uptr end2) {
CHECK(start1 <= end1);
CHECK(start2 <= end2);
return (end1 < start2) || (end2 < start1);
}
// FIXME: this is thread-unsafe, but should not cause problems most of the time.
// When the shadow is mapped only a single thread usually exists (plus maybe
// several worker threads on Mac, which aren't expected to map big chunks of
// memory).
bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
ProcessMaps procmaps;
uptr start, end;
while (procmaps.Next(&start, &end,
/*offset*/0, /*filename*/0, /*filename_size*/0)) {
if (!IntervalsAreSeparate(start, end, range_start, range_end))
return false;
}
return true;
}
void DumpProcessMap() {
ProcessMaps proc_maps;
uptr start, end;

View File

@ -70,6 +70,11 @@ void *Mprotect(uptr fixed_addr, uptr size) {
MEM_RESERVE | MEM_COMMIT, PAGE_NOACCESS);
}
bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
// FIXME: shall we do anything here on Windows?
return true;
}
const char *GetEnv(const char *name) {
static char env_buffer[32767] = {};