]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc
Merge ^/head r319801 through r320041.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_posix.cc
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_posix.h.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_platform.h"
16
17 #if SANITIZER_POSIX
18
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"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <sys/mman.h>
29
30 #if SANITIZER_LINUX
31 #include <sys/utsname.h>
32 #endif
33
34 #if SANITIZER_LINUX && !SANITIZER_ANDROID
35 #include <sys/personality.h>
36 #endif
37
38 #if SANITIZER_FREEBSD
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.
41 #undef  MAP_NORESERVE
42 #define MAP_NORESERVE 0
43 #endif
44
45 namespace __sanitizer {
46
47 // ------------- sanitizer_common.h
48 uptr GetMmapGranularity() {
49   return GetPageSize();
50 }
51
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;
57
58   // Firstly check if there are writable segments
59   // mapped to top gigabyte (e.g. stack).
60   MemoryMappingLayout proc_maps(/*cache_enabled*/true);
61   uptr end, prot;
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)
67       return 0;
68   }
69
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"))
80     return 0;
81 #endif  // SANITIZER_ANDROID
82
83   // Top gigabyte is reserved for kernel.
84   return gbyte;
85 #else
86   return 0;
87 #endif  // SANITIZER_LINUX && !SANITIZER_X32
88 }
89 #endif  // SANITIZER_WORDSIZE == 32
90
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;
110 # else
111   return (1ULL << 47) - 1;  // 0x00007fffffffffffUL;
112 # endif
113 #else  // SANITIZER_WORDSIZE == 32
114 # if defined(__s390__)
115   return (1ULL << 31) - 1;  // 0x7fffffff;
116 # else
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);
121   return res;
122 # endif
123 #endif  // SANITIZER_WORDSIZE
124 }
125
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);
131   int reserrno;
132   if (internal_iserror(res, &reserrno))
133     ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report);
134   IncreaseTotalMmap(size);
135   return (void *)res;
136 }
137
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);
145   }
146   DecreaseTotalMmap(size);
147 }
148
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);
154   int reserrno;
155   if (internal_iserror(res, &reserrno)) {
156     if (reserrno == ENOMEM)
157       return nullptr;
158     ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
159   }
160   IncreaseTotalMmap(size);
161   return (void *)res;
162 }
163
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;
173   uptr res = map_res;
174   if (res & (alignment - 1))  // Not aligned.
175     res = (map_res + alignment) & ~(alignment - 1);
176   uptr end = res + size;
177   if (res != map_res)
178     UnmapOrDie((void*)map_res, res - map_res);
179   if (end != map_end)
180     UnmapOrDie((void*)end, map_end - end);
181   return (void*)res;
182 }
183
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,
190                          -1, 0);
191   int reserrno;
192   if (internal_iserror(p, &reserrno))
193     ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
194   IncreaseTotalMmap(size);
195   return (void *)p;
196 }
197
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,
204       -1, 0);
205   int reserrno;
206   if (internal_iserror(p, &reserrno)) {
207     char mem_type[30];
208     internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
209                       fixed_addr);
210     ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
211   }
212   IncreaseTotalMmap(size);
213   return (void *)p;
214 }
215
216 bool MprotectNoAccess(uptr addr, uptr size) {
217   return 0 == internal_mprotect((void*)addr, size, PROT_NONE);
218 }
219
220 bool MprotectReadOnly(uptr addr, uptr size) {
221   return 0 == internal_mprotect((void *)addr, size, PROT_READ);
222 }
223
224 fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
225   int flags;
226   switch (mode) {
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;
230   }
231   fd_t res = internal_open(filename, flags, 0660);
232   if (internal_iserror(res, errno_p))
233     return kInvalidFd;
234   return res;
235 }
236
237 void CloseFile(fd_t fd) {
238   internal_close(fd);
239 }
240
241 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
242                   error_t *error_p) {
243   uptr res = internal_read(fd, buff, buff_size);
244   if (internal_iserror(res, error_p))
245     return false;
246   if (bytes_read)
247     *bytes_read = res;
248   return true;
249 }
250
251 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
252                  error_t *error_p) {
253   uptr res = internal_write(fd, buff, buff_size);
254   if (internal_iserror(res, error_p))
255     return false;
256   if (bytes_written)
257     *bytes_written = res;
258   return true;
259 }
260
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);
264 }
265
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);
271   CHECK_GT(fsize, 0);
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;
275 }
276
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);
281   int mmap_errno = 0;
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);
285     return nullptr;
286   }
287   return (void *)p;
288 }
289
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);
295 }
296
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
300 // memory).
301 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
302   MemoryMappingLayout proc_maps(/*cache_enabled*/true);
303   uptr start, end;
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.
308     CHECK_NE(0, end);
309     if (!IntervalsAreSeparate(start, end - 1, range_start, range_end))
310       return false;
311   }
312   return true;
313 }
314
315 void DumpProcessMap() {
316   MemoryMappingLayout proc_maps(/*cache_enabled*/true);
317   uptr start, end;
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);
324   }
325   Report("End of process memory map.\n");
326   UnmapOrDie(filename, kBufSize);
327 }
328
329 const char *GetPwd() {
330   return GetEnv("PWD");
331 }
332
333 bool IsPathSeparator(const char c) {
334   return c == '/';
335 }
336
337 bool IsAbsolutePath(const char *path) {
338   return path != nullptr && IsPathSeparator(path[0]);
339 }
340
341 void ReportFile::Write(const char *buffer, uptr length) {
342   SpinMutexLock l(mu);
343   static const char *kWriteError =
344       "ReportFile::Write() can't output requested buffer!\n";
345   ReopenIfNecessary();
346   if (length != internal_write(fd, buffer, length)) {
347     internal_write(fd, kWriteError, internal_strlen(kWriteError));
348     Die();
349   }
350 }
351
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) {
359       *start = s;
360       *end = e;
361       return true;
362     }
363   }
364   return false;
365 }
366
367 SignalContext SignalContext::Create(void *siginfo, void *context) {
368   auto si = (siginfo_t *)siginfo;
369   uptr addr = (uptr)si->si_addr;
370   uptr pc, sp, bp;
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);
375 }
376
377 const char *DescribeSignalOrException(int signo) {
378   switch (signo) {
379     case SIGFPE:
380       return "FPE";
381     case SIGILL:
382       return "ILL";
383     case SIGABRT:
384       return "ABRT";
385     case SIGSEGV:
386       return "SEGV";
387     case SIGBUS:
388       return "BUS";
389   }
390   return "UNKNOWN SIGNAL";
391 }
392
393 } // namespace __sanitizer
394
395 #endif // SANITIZER_POSIX