]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc
Merge ^/head r320042 through r320397.
[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 *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);
173   if (!map_res)
174     return nullptr;
175   uptr map_end = map_res + map_size;
176   uptr res = map_res;
177   if (res & (alignment - 1))  // Not aligned.
178     res = (map_res + alignment) & ~(alignment - 1);
179   uptr end = res + size;
180   if (res != map_res)
181     UnmapOrDie((void*)map_res, res - map_res);
182   if (end != map_end)
183     UnmapOrDie((void*)end, map_end - end);
184   return (void*)res;
185 }
186
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,
193                          -1, 0);
194   int reserrno;
195   if (internal_iserror(p, &reserrno))
196     ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
197   IncreaseTotalMmap(size);
198   return (void *)p;
199 }
200
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,
207       -1, 0);
208   int reserrno;
209   if (internal_iserror(p, &reserrno)) {
210     char mem_type[30];
211     internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
212                       fixed_addr);
213     ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
214   }
215   IncreaseTotalMmap(size);
216   return (void *)p;
217 }
218
219 bool MprotectNoAccess(uptr addr, uptr size) {
220   return 0 == internal_mprotect((void*)addr, size, PROT_NONE);
221 }
222
223 bool MprotectReadOnly(uptr addr, uptr size) {
224   return 0 == internal_mprotect((void *)addr, size, PROT_READ);
225 }
226
227 fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
228   int flags;
229   switch (mode) {
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;
233   }
234   fd_t res = internal_open(filename, flags, 0660);
235   if (internal_iserror(res, errno_p))
236     return kInvalidFd;
237   return res;
238 }
239
240 void CloseFile(fd_t fd) {
241   internal_close(fd);
242 }
243
244 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
245                   error_t *error_p) {
246   uptr res = internal_read(fd, buff, buff_size);
247   if (internal_iserror(res, error_p))
248     return false;
249   if (bytes_read)
250     *bytes_read = res;
251   return true;
252 }
253
254 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
255                  error_t *error_p) {
256   uptr res = internal_write(fd, buff, buff_size);
257   if (internal_iserror(res, error_p))
258     return false;
259   if (bytes_written)
260     *bytes_written = res;
261   return true;
262 }
263
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);
267 }
268
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);
274   CHECK_GT(fsize, 0);
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;
278 }
279
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);
284   int mmap_errno = 0;
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);
288     return nullptr;
289   }
290   return (void *)p;
291 }
292
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);
298 }
299
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
303 // memory).
304 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
305   MemoryMappingLayout proc_maps(/*cache_enabled*/true);
306   uptr start, end;
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.
311     CHECK_NE(0, end);
312     if (!IntervalsAreSeparate(start, end - 1, range_start, range_end))
313       return false;
314   }
315   return true;
316 }
317
318 void DumpProcessMap() {
319   MemoryMappingLayout proc_maps(/*cache_enabled*/true);
320   uptr start, end;
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);
327   }
328   Report("End of process memory map.\n");
329   UnmapOrDie(filename, kBufSize);
330 }
331
332 const char *GetPwd() {
333   return GetEnv("PWD");
334 }
335
336 bool IsPathSeparator(const char c) {
337   return c == '/';
338 }
339
340 bool IsAbsolutePath(const char *path) {
341   return path != nullptr && IsPathSeparator(path[0]);
342 }
343
344 void ReportFile::Write(const char *buffer, uptr length) {
345   SpinMutexLock l(mu);
346   static const char *kWriteError =
347       "ReportFile::Write() can't output requested buffer!\n";
348   ReopenIfNecessary();
349   if (length != internal_write(fd, buffer, length)) {
350     internal_write(fd, kWriteError, internal_strlen(kWriteError));
351     Die();
352   }
353 }
354
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) {
362       *start = s;
363       *end = e;
364       return true;
365     }
366   }
367   return false;
368 }
369
370 SignalContext SignalContext::Create(void *siginfo, void *context) {
371   auto si = (siginfo_t *)siginfo;
372   uptr addr = (uptr)si->si_addr;
373   uptr pc, sp, bp;
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);
378 }
379
380 const char *DescribeSignalOrException(int signo) {
381   switch (signo) {
382     case SIGFPE:
383       return "FPE";
384     case SIGILL:
385       return "ILL";
386     case SIGABRT:
387       return "ABRT";
388     case SIGSEGV:
389       return "SEGV";
390     case SIGBUS:
391       return "BUS";
392   }
393   return "UNKNOWN SIGNAL";
394 }
395
396 } // namespace __sanitizer
397
398 #endif // SANITIZER_POSIX