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