sanitizer_posix.cc revision c8490e2a09cd415cc58fc8f3b63bebddce61c82d
1//===-- sanitizer_posix.cc ------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is shared between AddressSanitizer and ThreadSanitizer 11// run-time libraries and implements POSIX-specific functions from 12// sanitizer_libc.h. 13//===----------------------------------------------------------------------===// 14#if defined(__linux__) || defined(__APPLE__) 15 16#include "sanitizer_common.h" 17#include "sanitizer_libc.h" 18#include "sanitizer_procmaps.h" 19 20#include <errno.h> 21#include <pthread.h> 22#include <stdarg.h> 23#include <stdio.h> 24#include <stdlib.h> 25#include <string.h> 26#include <sys/mman.h> 27#include <sys/resource.h> 28#include <sys/time.h> 29#include <sys/types.h> 30#include <unistd.h> 31 32namespace __sanitizer { 33 34// ------------- sanitizer_common.h 35uptr GetPageSize() { 36 return sysconf(_SC_PAGESIZE); 37} 38 39uptr GetMmapGranularity() { 40 return GetPageSize(); 41} 42 43int GetPid() { 44 return getpid(); 45} 46 47uptr GetThreadSelf() { 48 return (uptr)pthread_self(); 49} 50 51void *MmapOrDie(uptr size, const char *mem_type) { 52 size = RoundUpTo(size, GetPageSizeCached()); 53 void *res = internal_mmap(0, size, 54 PROT_READ | PROT_WRITE, 55 MAP_PRIVATE | MAP_ANON, -1, 0); 56 if (res == (void*)-1) { 57 static int recursion_count; 58 if (recursion_count) { 59 // The Report() and CHECK calls below may call mmap recursively and fail. 60 // If we went into recursion, just die. 61 RawWrite("ERROR: Failed to mmap\n"); 62 Die(); 63 } 64 recursion_count++; 65 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s: %s\n", 66 size, size, mem_type, strerror(errno)); 67 DumpProcessMap(); 68 CHECK("unable to mmap" && 0); 69 } 70 return res; 71} 72 73void UnmapOrDie(void *addr, uptr size) { 74 if (!addr || !size) return; 75 int res = internal_munmap(addr, size); 76 if (res != 0) { 77 Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n", 78 size, size, addr); 79 CHECK("unable to unmap" && 0); 80 } 81} 82 83void *MmapFixedNoReserve(uptr fixed_addr, uptr size) { 84 uptr PageSize = GetPageSizeCached(); 85 void *p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), 86 RoundUpTo(size, PageSize), 87 PROT_READ | PROT_WRITE, 88 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 89 -1, 0); 90 if (p == (void*)-1) 91 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n", 92 size, size, fixed_addr, errno); 93 return p; 94} 95 96void *MmapFixedOrDie(uptr fixed_addr, uptr size) { 97 uptr PageSize = GetPageSizeCached(); 98 void *p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), 99 RoundUpTo(size, PageSize), 100 PROT_READ | PROT_WRITE, 101 MAP_PRIVATE | MAP_ANON | MAP_FIXED, 102 -1, 0); 103 if (p == (void*)-1) { 104 Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n", 105 size, size, fixed_addr, errno); 106 CHECK("unable to mmap" && 0); 107 } 108 return p; 109} 110 111void *Mprotect(uptr fixed_addr, uptr size) { 112 return internal_mmap((void*)fixed_addr, size, 113 PROT_NONE, 114 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 115 -1, 0); 116} 117 118void FlushUnneededShadowMemory(uptr addr, uptr size) { 119 madvise((void*)addr, size, MADV_DONTNEED); 120} 121 122void *MapFileToMemory(const char *file_name, uptr *buff_size) { 123 fd_t fd = internal_open(file_name, false); 124 CHECK_NE(fd, kInvalidFd); 125 uptr fsize = internal_filesize(fd); 126 CHECK_NE(fsize, (uptr)-1); 127 CHECK_GT(fsize, 0); 128 *buff_size = RoundUpTo(fsize, GetPageSizeCached()); 129 void *map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0); 130 return (map == MAP_FAILED) ? 0 : map; 131} 132 133 134static inline bool IntervalsAreSeparate(uptr start1, uptr end1, 135 uptr start2, uptr end2) { 136 CHECK(start1 <= end1); 137 CHECK(start2 <= end2); 138 return (end1 < start2) || (end2 < start1); 139} 140 141// FIXME: this is thread-unsafe, but should not cause problems most of the time. 142// When the shadow is mapped only a single thread usually exists (plus maybe 143// several worker threads on Mac, which aren't expected to map big chunks of 144// memory). 145bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { 146 MemoryMappingLayout procmaps; 147 uptr start, end; 148 while (procmaps.Next(&start, &end, 149 /*offset*/0, /*filename*/0, /*filename_size*/0)) { 150 if (!IntervalsAreSeparate(start, end, range_start, range_end)) 151 return false; 152 } 153 return true; 154} 155 156void DumpProcessMap() { 157 MemoryMappingLayout proc_maps; 158 uptr start, end; 159 const sptr kBufSize = 4095; 160 char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__); 161 Report("Process memory map follows:\n"); 162 while (proc_maps.Next(&start, &end, /* file_offset */0, 163 filename, kBufSize)) { 164 Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename); 165 } 166 Report("End of process memory map.\n"); 167 UnmapOrDie(filename, kBufSize); 168} 169 170const char *GetPwd() { 171 return GetEnv("PWD"); 172} 173 174void DisableCoreDumper() { 175 struct rlimit nocore; 176 nocore.rlim_cur = 0; 177 nocore.rlim_max = 0; 178 setrlimit(RLIMIT_CORE, &nocore); 179} 180 181bool StackSizeIsUnlimited() { 182 struct rlimit rlim; 183 CHECK_EQ(0, getrlimit(RLIMIT_STACK, &rlim)); 184 return (rlim.rlim_cur == (uptr)-1); 185} 186 187void SetStackSizeLimitInBytes(uptr limit) { 188 struct rlimit rlim; 189 rlim.rlim_cur = limit; 190 rlim.rlim_max = limit; 191 if (setrlimit(RLIMIT_STACK, &rlim)) { 192 Report("setrlimit() failed %d\n", errno); 193 Die(); 194 } 195 CHECK(!StackSizeIsUnlimited()); 196} 197 198void SleepForSeconds(int seconds) { 199 sleep(seconds); 200} 201 202void SleepForMillis(int millis) { 203 usleep(millis * 1000); 204} 205 206void Exit(int exitcode) { 207 _exit(exitcode); 208} 209 210void Abort() { 211 abort(); 212} 213 214int Atexit(void (*function)(void)) { 215#ifndef SANITIZER_GO 216 return atexit(function); 217#else 218 return 0; 219#endif 220} 221 222int internal_isatty(fd_t fd) { 223 return isatty(fd); 224} 225 226} // namespace __sanitizer 227 228#endif // __linux__ || __APPLE_ 229