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 *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
168 const char *mem_type) {
169 CHECK(IsPowerOfTwo(size));
170 CHECK(IsPowerOfTwo(alignment));
171 uptr map_size = size + alignment;
172 uptr map_res = (uptr)MmapOrDieOnFatalError(map_size, mem_type);
175 uptr map_end = map_res + map_size;
177 if (res & (alignment - 1)) // Not aligned.
178 res = (map_res + alignment) & ~(alignment - 1);
179 uptr end = res + size;
181 UnmapOrDie((void*)map_res, res - map_res);
183 UnmapOrDie((void*)end, map_end - end);
187 void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
188 uptr PageSize = GetPageSizeCached();
189 uptr p = internal_mmap(nullptr,
190 RoundUpTo(size, PageSize),
191 PROT_READ | PROT_WRITE,
192 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
195 if (internal_iserror(p, &reserrno))
196 ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
197 IncreaseTotalMmap(size);
201 void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
202 uptr PageSize = GetPageSizeCached();
203 uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
204 RoundUpTo(size, PageSize),
205 PROT_READ | PROT_WRITE,
206 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
209 if (internal_iserror(p, &reserrno)) {
211 internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
213 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
215 IncreaseTotalMmap(size);
219 bool MprotectNoAccess(uptr addr, uptr size) {
220 return 0 == internal_mprotect((void*)addr, size, PROT_NONE);
223 bool MprotectReadOnly(uptr addr, uptr size) {
224 return 0 == internal_mprotect((void *)addr, size, PROT_READ);
227 fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
230 case RdOnly: flags = O_RDONLY; break;
231 case WrOnly: flags = O_WRONLY | O_CREAT; break;
232 case RdWr: flags = O_RDWR | O_CREAT; break;
234 fd_t res = internal_open(filename, flags, 0660);
235 if (internal_iserror(res, errno_p))
240 void CloseFile(fd_t fd) {
244 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
246 uptr res = internal_read(fd, buff, buff_size);
247 if (internal_iserror(res, error_p))
254 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
256 uptr res = internal_write(fd, buff, buff_size);
257 if (internal_iserror(res, error_p))
260 *bytes_written = res;
264 bool RenameFile(const char *oldpath, const char *newpath, error_t *error_p) {
265 uptr res = internal_rename(oldpath, newpath);
266 return !internal_iserror(res, error_p);
269 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
270 fd_t fd = OpenFile(file_name, RdOnly);
271 CHECK(fd != kInvalidFd);
272 uptr fsize = internal_filesize(fd);
273 CHECK_NE(fsize, (uptr)-1);
275 *buff_size = RoundUpTo(fsize, GetPageSizeCached());
276 uptr map = internal_mmap(nullptr, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
277 return internal_iserror(map) ? nullptr : (void *)map;
280 void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
281 uptr flags = MAP_SHARED;
282 if (addr) flags |= MAP_FIXED;
283 uptr p = internal_mmap(addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
285 if (internal_iserror(p, &mmap_errno)) {
286 Printf("could not map writable file (%d, %lld, %zu): %zd, errno: %d\n",
287 fd, (long long)offset, size, p, mmap_errno);
293 static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
294 uptr start2, uptr end2) {
295 CHECK(start1 <= end1);
296 CHECK(start2 <= end2);
297 return (end1 < start2) || (end2 < start1);
300 // FIXME: this is thread-unsafe, but should not cause problems most of the time.
301 // When the shadow is mapped only a single thread usually exists (plus maybe
302 // several worker threads on Mac, which aren't expected to map big chunks of
304 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
305 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
307 while (proc_maps.Next(&start, &end,
308 /*offset*/nullptr, /*filename*/nullptr,
309 /*filename_size*/0, /*protection*/nullptr)) {
310 if (start == end) continue; // Empty range.
312 if (!IntervalsAreSeparate(start, end - 1, range_start, range_end))
318 void DumpProcessMap() {
319 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
321 const sptr kBufSize = 4095;
322 char *filename = (char*)MmapOrDie(kBufSize, __func__);
323 Report("Process memory map follows:\n");
324 while (proc_maps.Next(&start, &end, /* file_offset */nullptr,
325 filename, kBufSize, /* protection */nullptr)) {
326 Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
328 Report("End of process memory map.\n");
329 UnmapOrDie(filename, kBufSize);
332 const char *GetPwd() {
333 return GetEnv("PWD");
336 bool IsPathSeparator(const char c) {
340 bool IsAbsolutePath(const char *path) {
341 return path != nullptr && IsPathSeparator(path[0]);
344 void ReportFile::Write(const char *buffer, uptr length) {
346 static const char *kWriteError =
347 "ReportFile::Write() can't output requested buffer!\n";
349 if (length != internal_write(fd, buffer, length)) {
350 internal_write(fd, kWriteError, internal_strlen(kWriteError));
355 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) {
356 uptr s, e, off, prot;
357 InternalScopedString buff(kMaxPathLength);
358 MemoryMappingLayout proc_maps(/*cache_enabled*/false);
359 while (proc_maps.Next(&s, &e, &off, buff.data(), buff.size(), &prot)) {
360 if ((prot & MemoryMappingLayout::kProtectionExecute) != 0
361 && internal_strcmp(module, buff.data()) == 0) {
370 SignalContext SignalContext::Create(void *siginfo, void *context) {
371 auto si = (siginfo_t *)siginfo;
372 uptr addr = (uptr)si->si_addr;
374 GetPcSpBp(context, &pc, &sp, &bp);
375 WriteFlag write_flag = GetWriteFlag(context);
376 bool is_memory_access = si->si_signo == SIGSEGV;
377 return SignalContext(context, addr, pc, sp, bp, is_memory_access, write_flag);
380 const char *DescribeSignalOrException(int signo) {
393 return "UNKNOWN SIGNAL";
396 } // namespace __sanitizer
398 #endif // SANITIZER_POSIX