1 //===-- sanitizer_posix.cc ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries and implements POSIX-specific functions from
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_platform.h"
19 #include "sanitizer_common.h"
20 #include "sanitizer_libc.h"
21 #include "sanitizer_posix.h"
22 #include "sanitizer_procmaps.h"
23 #include "sanitizer_stacktrace.h"
31 #include <sys/utsname.h>
34 #if SANITIZER_LINUX && !SANITIZER_ANDROID
35 #include <sys/personality.h>
39 // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
40 // that, it was never implemented. So just define it to zero.
42 #define MAP_NORESERVE 0
45 namespace __sanitizer {
47 // ------------- sanitizer_common.h
48 uptr GetMmapGranularity() {
52 #if SANITIZER_WORDSIZE == 32
53 // Take care of unusable kernel area in top gigabyte.
54 static uptr GetKernelAreaSize() {
55 #if SANITIZER_LINUX && !SANITIZER_X32
56 const uptr gbyte = 1UL << 30;
58 // Firstly check if there are writable segments
59 // mapped to top gigabyte (e.g. stack).
60 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
62 while (proc_maps.Next(/*start*/nullptr, &end,
63 /*offset*/nullptr, /*filename*/nullptr,
64 /*filename_size*/0, &prot)) {
65 if ((end >= 3 * gbyte)
66 && (prot & MemoryMappingLayout::kProtectionWrite) != 0)
70 #if !SANITIZER_ANDROID
71 // Even if nothing is mapped, top Gb may still be accessible
72 // if we are running on 64-bit kernel.
73 // Uname may report misleading results if personality type
74 // is modified (e.g. under schroot) so check this as well.
75 struct utsname uname_info;
76 int pers = personality(0xffffffffUL);
77 if (!(pers & PER_MASK)
78 && uname(&uname_info) == 0
79 && internal_strstr(uname_info.machine, "64"))
81 #endif // SANITIZER_ANDROID
83 // Top gigabyte is reserved for kernel.
87 #endif // SANITIZER_LINUX && !SANITIZER_X32
89 #endif // SANITIZER_WORDSIZE == 32
91 uptr GetMaxVirtualAddress() {
92 #if SANITIZER_WORDSIZE == 64
93 # if defined(__aarch64__) && SANITIZER_IOS && !SANITIZER_IOSSIM
94 // Ideally, we would derive the upper bound from MACH_VM_MAX_ADDRESS. The
95 // upper bound can change depending on the device.
96 return 0x200000000 - 1;
97 # elif defined(__powerpc64__) || defined(__aarch64__)
98 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
99 // We somehow need to figure out which one we are using now and choose
100 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
101 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
102 // of the address space, so simply checking the stack address is not enough.
103 // This should (does) work for both PowerPC64 Endian modes.
104 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
105 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
106 # elif defined(__mips64)
107 return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
108 # elif defined(__s390x__)
109 return (1ULL << 53) - 1; // 0x001fffffffffffffUL;
111 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
113 #else // SANITIZER_WORDSIZE == 32
114 # if defined(__s390__)
115 return (1ULL << 31) - 1; // 0x7fffffff;
117 uptr res = (1ULL << 32) - 1; // 0xffffffff;
118 if (!common_flags()->full_address_space)
119 res -= GetKernelAreaSize();
120 CHECK_LT(reinterpret_cast<uptr>(&res), res);
123 #endif // SANITIZER_WORDSIZE
126 void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
127 size = RoundUpTo(size, GetPageSizeCached());
128 uptr res = internal_mmap(nullptr, size,
129 PROT_READ | PROT_WRITE,
130 MAP_PRIVATE | MAP_ANON, -1, 0);
132 if (internal_iserror(res, &reserrno))
133 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report);
134 IncreaseTotalMmap(size);
138 void 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);
146 DecreaseTotalMmap(size);
149 void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {
150 size = RoundUpTo(size, GetPageSizeCached());
151 uptr res = internal_mmap(nullptr, size,
152 PROT_READ | PROT_WRITE,
153 MAP_PRIVATE | MAP_ANON, -1, 0);
155 if (internal_iserror(res, &reserrno)) {
156 if (reserrno == ENOMEM)
158 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
160 IncreaseTotalMmap(size);
164 // We want to map a chunk of address space aligned to 'alignment'.
165 // We do it by maping a bit more and then unmaping redundant pieces.
166 // We probably can do it with fewer syscalls in some OS-dependent way.
167 void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type) {
168 CHECK(IsPowerOfTwo(size));
169 CHECK(IsPowerOfTwo(alignment));
170 uptr map_size = size + alignment;
171 uptr map_res = (uptr)MmapOrDie(map_size, mem_type);
172 uptr map_end = map_res + map_size;
174 if (res & (alignment - 1)) // Not aligned.
175 res = (map_res + alignment) & ~(alignment - 1);
176 uptr end = res + size;
178 UnmapOrDie((void*)map_res, res - map_res);
180 UnmapOrDie((void*)end, map_end - end);
184 void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
185 uptr PageSize = GetPageSizeCached();
186 uptr p = internal_mmap(nullptr,
187 RoundUpTo(size, PageSize),
188 PROT_READ | PROT_WRITE,
189 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
192 if (internal_iserror(p, &reserrno))
193 ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
194 IncreaseTotalMmap(size);
198 void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
199 uptr PageSize = GetPageSizeCached();
200 uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
201 RoundUpTo(size, PageSize),
202 PROT_READ | PROT_WRITE,
203 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
206 if (internal_iserror(p, &reserrno)) {
208 internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
210 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
212 IncreaseTotalMmap(size);
216 bool MprotectNoAccess(uptr addr, uptr size) {
217 return 0 == internal_mprotect((void*)addr, size, PROT_NONE);
220 bool MprotectReadOnly(uptr addr, uptr size) {
221 return 0 == internal_mprotect((void *)addr, size, PROT_READ);
224 fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
227 case RdOnly: flags = O_RDONLY; break;
228 case WrOnly: flags = O_WRONLY | O_CREAT; break;
229 case RdWr: flags = O_RDWR | O_CREAT; break;
231 fd_t res = internal_open(filename, flags, 0660);
232 if (internal_iserror(res, errno_p))
237 void CloseFile(fd_t fd) {
241 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
243 uptr res = internal_read(fd, buff, buff_size);
244 if (internal_iserror(res, error_p))
251 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
253 uptr res = internal_write(fd, buff, buff_size);
254 if (internal_iserror(res, error_p))
257 *bytes_written = res;
261 bool RenameFile(const char *oldpath, const char *newpath, error_t *error_p) {
262 uptr res = internal_rename(oldpath, newpath);
263 return !internal_iserror(res, error_p);
266 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
267 fd_t fd = OpenFile(file_name, RdOnly);
268 CHECK(fd != kInvalidFd);
269 uptr fsize = internal_filesize(fd);
270 CHECK_NE(fsize, (uptr)-1);
272 *buff_size = RoundUpTo(fsize, GetPageSizeCached());
273 uptr map = internal_mmap(nullptr, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
274 return internal_iserror(map) ? nullptr : (void *)map;
277 void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
278 uptr flags = MAP_SHARED;
279 if (addr) flags |= MAP_FIXED;
280 uptr p = internal_mmap(addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
282 if (internal_iserror(p, &mmap_errno)) {
283 Printf("could not map writable file (%d, %lld, %zu): %zd, errno: %d\n",
284 fd, (long long)offset, size, p, mmap_errno);
290 static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
291 uptr start2, uptr end2) {
292 CHECK(start1 <= end1);
293 CHECK(start2 <= end2);
294 return (end1 < start2) || (end2 < start1);
297 // FIXME: this is thread-unsafe, but should not cause problems most of the time.
298 // When the shadow is mapped only a single thread usually exists (plus maybe
299 // several worker threads on Mac, which aren't expected to map big chunks of
301 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
302 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
304 while (proc_maps.Next(&start, &end,
305 /*offset*/nullptr, /*filename*/nullptr,
306 /*filename_size*/0, /*protection*/nullptr)) {
307 if (start == end) continue; // Empty range.
309 if (!IntervalsAreSeparate(start, end - 1, range_start, range_end))
315 void DumpProcessMap() {
316 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
318 const sptr kBufSize = 4095;
319 char *filename = (char*)MmapOrDie(kBufSize, __func__);
320 Report("Process memory map follows:\n");
321 while (proc_maps.Next(&start, &end, /* file_offset */nullptr,
322 filename, kBufSize, /* protection */nullptr)) {
323 Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
325 Report("End of process memory map.\n");
326 UnmapOrDie(filename, kBufSize);
329 const char *GetPwd() {
330 return GetEnv("PWD");
333 bool IsPathSeparator(const char c) {
337 bool IsAbsolutePath(const char *path) {
338 return path != nullptr && IsPathSeparator(path[0]);
341 void ReportFile::Write(const char *buffer, uptr length) {
343 static const char *kWriteError =
344 "ReportFile::Write() can't output requested buffer!\n";
346 if (length != internal_write(fd, buffer, length)) {
347 internal_write(fd, kWriteError, internal_strlen(kWriteError));
352 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) {
353 uptr s, e, off, prot;
354 InternalScopedString buff(kMaxPathLength);
355 MemoryMappingLayout proc_maps(/*cache_enabled*/false);
356 while (proc_maps.Next(&s, &e, &off, buff.data(), buff.size(), &prot)) {
357 if ((prot & MemoryMappingLayout::kProtectionExecute) != 0
358 && internal_strcmp(module, buff.data()) == 0) {
367 SignalContext SignalContext::Create(void *siginfo, void *context) {
368 auto si = (siginfo_t *)siginfo;
369 uptr addr = (uptr)si->si_addr;
371 GetPcSpBp(context, &pc, &sp, &bp);
372 WriteFlag write_flag = GetWriteFlag(context);
373 bool is_memory_access = si->si_signo == SIGSEGV;
374 return SignalContext(context, addr, pc, sp, bp, is_memory_access, write_flag);
377 const char *DescribeSignalOrException(int signo) {
390 return "UNKNOWN SIGNAL";
393 } // namespace __sanitizer
395 #endif // SANITIZER_POSIX