sanitizer_posix.cc revision 7c9150579ed0278492f51cc8434b1d63a44b9bd1
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 15#include "sanitizer_platform.h" 16#if SANITIZER_POSIX 17 18#include "sanitizer_common.h" 19#include "sanitizer_libc.h" 20#include "sanitizer_procmaps.h" 21#include "sanitizer_stacktrace.h" 22 23#include <fcntl.h> 24#include <signal.h> 25#include <sys/mman.h> 26 27#if SANITIZER_LINUX 28#include <sys/utsname.h> 29#endif 30 31#if SANITIZER_LINUX && !SANITIZER_ANDROID 32#include <sys/personality.h> 33#endif 34 35#if SANITIZER_FREEBSD 36// The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before 37// that, it was never implemented. So just define it to zero. 38#undef MAP_NORESERVE 39#define MAP_NORESERVE 0 40#endif 41 42namespace __sanitizer { 43 44// ------------- sanitizer_common.h 45uptr GetMmapGranularity() { 46 return GetPageSize(); 47} 48 49#if SANITIZER_WORDSIZE == 32 50// Take care of unusable kernel area in top gigabyte. 51static uptr GetKernelAreaSize() { 52#if SANITIZER_LINUX && !SANITIZER_X32 53 const uptr gbyte = 1UL << 30; 54 55 // Firstly check if there are writable segments 56 // mapped to top gigabyte (e.g. stack). 57 MemoryMappingLayout proc_maps(/*cache_enabled*/true); 58 uptr end, prot; 59 while (proc_maps.Next(/*start*/0, &end, 60 /*offset*/0, /*filename*/0, 61 /*filename_size*/0, &prot)) { 62 if ((end >= 3 * gbyte) 63 && (prot & MemoryMappingLayout::kProtectionWrite) != 0) 64 return 0; 65 } 66 67#if !SANITIZER_ANDROID 68 // Even if nothing is mapped, top Gb may still be accessible 69 // if we are running on 64-bit kernel. 70 // Uname may report misleading results if personality type 71 // is modified (e.g. under schroot) so check this as well. 72 struct utsname uname_info; 73 int pers = personality(0xffffffffUL); 74 if (!(pers & PER_MASK) 75 && uname(&uname_info) == 0 76 && internal_strstr(uname_info.machine, "64")) 77 return 0; 78#endif // SANITIZER_ANDROID 79 80 // Top gigabyte is reserved for kernel. 81 return gbyte; 82#else 83 return 0; 84#endif // SANITIZER_LINUX && !SANITIZER_X32 85} 86#endif // SANITIZER_WORDSIZE == 32 87 88uptr GetMaxVirtualAddress() { 89#if SANITIZER_WORDSIZE == 64 90# if defined(__powerpc64__) || defined(__aarch64__) 91 // On PowerPC64 we have two different address space layouts: 44- and 46-bit. 92 // We somehow need to figure out which one we are using now and choose 93 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL. 94 // Note that with 'ulimit -s unlimited' the stack is moved away from the top 95 // of the address space, so simply checking the stack address is not enough. 96 // This should (does) work for both PowerPC64 Endian modes. 97 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit. 98 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1; 99# elif defined(__mips64) 100 return (1ULL << 40) - 1; // 0x000000ffffffffffUL; 101# else 102 return (1ULL << 47) - 1; // 0x00007fffffffffffUL; 103# endif 104#else // SANITIZER_WORDSIZE == 32 105 uptr res = (1ULL << 32) - 1; // 0xffffffff; 106 if (!common_flags()->full_address_space) 107 res -= GetKernelAreaSize(); 108 CHECK_LT(reinterpret_cast<uptr>(&res), res); 109 return res; 110#endif // SANITIZER_WORDSIZE 111} 112 113void *MmapOrDie(uptr size, const char *mem_type) { 114 size = RoundUpTo(size, GetPageSizeCached()); 115 uptr res = internal_mmap(0, size, 116 PROT_READ | PROT_WRITE, 117 MAP_PRIVATE | MAP_ANON, -1, 0); 118 int reserrno; 119 if (internal_iserror(res, &reserrno)) { 120 static int recursion_count; 121 if (recursion_count) { 122 // The Report() and CHECK calls below may call mmap recursively and fail. 123 // If we went into recursion, just die. 124 RawWrite("ERROR: Failed to mmap\n"); 125 Die(); 126 } 127 recursion_count++; 128 Report("ERROR: %s failed to " 129 "allocate 0x%zx (%zd) bytes of %s (errno: %d)\n", 130 SanitizerToolName, size, size, mem_type, reserrno); 131 DumpProcessMap(); 132 CHECK("unable to mmap" && 0); 133 } 134 IncreaseTotalMmap(size); 135 return (void *)res; 136} 137 138void UnmapOrDie(void *addr, uptr size) { 139 if (!addr || !size) return; 140 uptr res = internal_munmap(addr, size); 141 if (internal_iserror(res)) { 142 Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n", 143 SanitizerToolName, size, size, addr); 144 CHECK("unable to unmap" && 0); 145 } 146 DecreaseTotalMmap(size); 147} 148 149void *MmapNoReserveOrDie(uptr size, const char *mem_type) { 150 uptr PageSize = GetPageSizeCached(); 151 uptr p = internal_mmap(0, 152 RoundUpTo(size, PageSize), 153 PROT_READ | PROT_WRITE, 154 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, 155 -1, 0); 156 int reserrno; 157 if (internal_iserror(p, &reserrno)) { 158 Report("ERROR: %s failed to " 159 "allocate noreserve 0x%zx (%zd) bytes for '%s' (errno: %d)\n", 160 SanitizerToolName, size, size, mem_type, reserrno); 161 CHECK("unable to mmap" && 0); 162 } 163 IncreaseTotalMmap(size); 164 return (void *)p; 165} 166 167void *MmapFixedNoReserve(uptr fixed_addr, uptr size) { 168 uptr PageSize = GetPageSizeCached(); 169 uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), 170 RoundUpTo(size, PageSize), 171 PROT_READ | PROT_WRITE, 172 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, 173 -1, 0); 174 int reserrno; 175 if (internal_iserror(p, &reserrno)) 176 Report("ERROR: %s failed to " 177 "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n", 178 SanitizerToolName, size, size, fixed_addr, reserrno); 179 IncreaseTotalMmap(size); 180 return (void *)p; 181} 182 183void *MmapFixedOrDie(uptr fixed_addr, uptr size) { 184 uptr PageSize = GetPageSizeCached(); 185 uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), 186 RoundUpTo(size, PageSize), 187 PROT_READ | PROT_WRITE, 188 MAP_PRIVATE | MAP_ANON | MAP_FIXED, 189 -1, 0); 190 int reserrno; 191 if (internal_iserror(p, &reserrno)) { 192 Report("ERROR: %s failed to " 193 "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n", 194 SanitizerToolName, size, size, fixed_addr, reserrno); 195 CHECK("unable to mmap" && 0); 196 } 197 IncreaseTotalMmap(size); 198 return (void *)p; 199} 200 201void *Mprotect(uptr fixed_addr, uptr size) { 202 return (void *)internal_mmap((void*)fixed_addr, size, 203 PROT_NONE, 204 MAP_PRIVATE | MAP_ANON | MAP_FIXED | 205 MAP_NORESERVE, -1, 0); 206} 207 208uptr OpenFile(const char *filename, FileAccessMode mode) { 209 int flags; 210 switch (mode) { 211 case RdOnly: flags = O_RDONLY; break; 212 case WrOnly: flags = O_WRONLY | O_CREAT; break; 213 case RdWr: flags = O_RDWR | O_CREAT; break; 214 } 215 return internal_open(filename, flags, 0660); 216} 217 218void *MapFileToMemory(const char *file_name, uptr *buff_size) { 219 uptr openrv = OpenFile(file_name, RdOnly); 220 CHECK(!internal_iserror(openrv)); 221 fd_t fd = openrv; 222 uptr fsize = internal_filesize(fd); 223 CHECK_NE(fsize, (uptr)-1); 224 CHECK_GT(fsize, 0); 225 *buff_size = RoundUpTo(fsize, GetPageSizeCached()); 226 uptr map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0); 227 return internal_iserror(map) ? 0 : (void *)map; 228} 229 230void *MapWritableFileToMemory(void *addr, uptr size, uptr fd, uptr offset) { 231 uptr flags = MAP_SHARED; 232 if (addr) flags |= MAP_FIXED; 233 uptr p = internal_mmap(addr, size, PROT_READ | PROT_WRITE, flags, fd, offset); 234 int mmap_errno = 0; 235 if (internal_iserror(p, &mmap_errno)) { 236 Printf("could not map writable file (%zd, %zu, %zu): %zd, errno: %d\n", 237 fd, offset, size, p, mmap_errno); 238 return 0; 239 } 240 return (void *)p; 241} 242 243static inline bool IntervalsAreSeparate(uptr start1, uptr end1, 244 uptr start2, uptr end2) { 245 CHECK(start1 <= end1); 246 CHECK(start2 <= end2); 247 return (end1 < start2) || (end2 < start1); 248} 249 250// FIXME: this is thread-unsafe, but should not cause problems most of the time. 251// When the shadow is mapped only a single thread usually exists (plus maybe 252// several worker threads on Mac, which aren't expected to map big chunks of 253// memory). 254bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { 255 MemoryMappingLayout proc_maps(/*cache_enabled*/true); 256 uptr start, end; 257 while (proc_maps.Next(&start, &end, 258 /*offset*/0, /*filename*/0, /*filename_size*/0, 259 /*protection*/0)) { 260 CHECK_NE(0, end); 261 if (!IntervalsAreSeparate(start, end - 1, range_start, range_end)) 262 return false; 263 } 264 return true; 265} 266 267void DumpProcessMap() { 268 MemoryMappingLayout proc_maps(/*cache_enabled*/true); 269 uptr start, end; 270 const sptr kBufSize = 4095; 271 char *filename = (char*)MmapOrDie(kBufSize, __func__); 272 Report("Process memory map follows:\n"); 273 while (proc_maps.Next(&start, &end, /* file_offset */0, 274 filename, kBufSize, /* protection */0)) { 275 Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename); 276 } 277 Report("End of process memory map.\n"); 278 UnmapOrDie(filename, kBufSize); 279} 280 281const char *GetPwd() { 282 return GetEnv("PWD"); 283} 284 285char *FindPathToBinary(const char *name) { 286 const char *path = GetEnv("PATH"); 287 if (!path) 288 return 0; 289 uptr name_len = internal_strlen(name); 290 InternalScopedBuffer<char> buffer(kMaxPathLength); 291 const char *beg = path; 292 while (true) { 293 const char *end = internal_strchrnul(beg, ':'); 294 uptr prefix_len = end - beg; 295 if (prefix_len + name_len + 2 <= kMaxPathLength) { 296 internal_memcpy(buffer.data(), beg, prefix_len); 297 buffer[prefix_len] = '/'; 298 internal_memcpy(&buffer[prefix_len + 1], name, name_len); 299 buffer[prefix_len + 1 + name_len] = '\0'; 300 if (FileExists(buffer.data())) 301 return internal_strdup(buffer.data()); 302 } 303 if (*end == '\0') break; 304 beg = end + 1; 305 } 306 return 0; 307} 308 309bool IsPathSeparator(const char c) { 310 return c == '/'; 311} 312 313bool IsAbsolutePath(const char *path) { 314 return path != nullptr && IsPathSeparator(path[0]); 315} 316 317void ReportFile::Write(const char *buffer, uptr length) { 318 SpinMutexLock l(mu); 319 static const char *kWriteError = 320 "ReportFile::Write() can't output requested buffer!\n"; 321 ReopenIfNecessary(); 322 if (length != internal_write(fd, buffer, length)) { 323 internal_write(fd, kWriteError, internal_strlen(kWriteError)); 324 Die(); 325 } 326} 327 328bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) { 329 uptr s, e, off, prot; 330 InternalScopedString buff(kMaxPathLength); 331 MemoryMappingLayout proc_maps(/*cache_enabled*/false); 332 while (proc_maps.Next(&s, &e, &off, buff.data(), buff.size(), &prot)) { 333 if ((prot & MemoryMappingLayout::kProtectionExecute) != 0 334 && internal_strcmp(module, buff.data()) == 0) { 335 *start = s; 336 *end = e; 337 return true; 338 } 339 } 340 return false; 341} 342 343SignalContext SignalContext::Create(void *siginfo, void *context) { 344 uptr addr = (uptr)((siginfo_t*)siginfo)->si_addr; 345 uptr pc, sp, bp; 346 GetPcSpBp(context, &pc, &sp, &bp); 347 return SignalContext(context, addr, pc, sp, bp); 348} 349 350} // namespace __sanitizer 351 352#endif // SANITIZER_POSIX 353