sanitizer_posix.cc revision f882247088952deed954a19d745c2dd8871e2035
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#if defined(__linux__) || defined(__APPLE__)
15
16#include "sanitizer_common.h"
17#include "sanitizer_libc.h"
18#include "sanitizer_procmaps.h"
19
20#include <errno.h>
21#include <pthread.h>
22#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/resource.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <unistd.h>
31
32namespace __sanitizer {
33
34// ------------- sanitizer_common.h
35uptr GetPageSize() {
36  return sysconf(_SC_PAGESIZE);
37}
38
39uptr GetMmapGranularity() {
40  return GetPageSize();
41}
42
43int GetPid() {
44  return getpid();
45}
46
47u32 GetUid() {
48  return getuid();
49}
50
51uptr GetThreadSelf() {
52  return (uptr)pthread_self();
53}
54
55void *MmapOrDie(uptr size, const char *mem_type) {
56  size = RoundUpTo(size, GetPageSizeCached());
57  void *res = internal_mmap(0, size,
58                            PROT_READ | PROT_WRITE,
59                            MAP_PRIVATE | MAP_ANON, -1, 0);
60  if (res == (void*)-1) {
61    static int recursion_count;
62    if (recursion_count) {
63      // The Report() and CHECK calls below may call mmap recursively and fail.
64      // If we went into recursion, just die.
65      RawWrite("ERROR: Failed to mmap\n");
66      Die();
67    }
68    recursion_count++;
69    Report("ERROR: %s failed to allocate 0x%zx (%zd) bytes of %s: %s\n",
70           SanitizerToolName, size, size, mem_type, strerror(errno));
71    DumpProcessMap();
72    CHECK("unable to mmap" && 0);
73  }
74  return res;
75}
76
77void UnmapOrDie(void *addr, uptr size) {
78  if (!addr || !size) return;
79  int res = internal_munmap(addr, size);
80  if (res != 0) {
81    Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
82           SanitizerToolName, size, size, addr);
83    CHECK("unable to unmap" && 0);
84  }
85}
86
87void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
88  uptr PageSize = GetPageSizeCached();
89  void *p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
90      RoundUpTo(size, PageSize),
91      PROT_READ | PROT_WRITE,
92      MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
93      -1, 0);
94  if (p == (void*)-1)
95    Report("ERROR: "
96           "%s failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n",
97           SanitizerToolName, size, size, fixed_addr, errno);
98  return p;
99}
100
101void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
102  uptr PageSize = GetPageSizeCached();
103  void *p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
104      RoundUpTo(size, PageSize),
105      PROT_READ | PROT_WRITE,
106      MAP_PRIVATE | MAP_ANON | MAP_FIXED,
107      -1, 0);
108  if (p == (void*)-1) {
109    Report("ERROR:"
110           " %s failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n",
111           SanitizerToolName, size, size, fixed_addr, errno);
112    CHECK("unable to mmap" && 0);
113  }
114  return p;
115}
116
117void *Mprotect(uptr fixed_addr, uptr size) {
118  return internal_mmap((void*)fixed_addr, size,
119                       PROT_NONE,
120                       MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
121                       -1, 0);
122}
123
124void FlushUnneededShadowMemory(uptr addr, uptr size) {
125  madvise((void*)addr, size, MADV_DONTNEED);
126}
127
128void *MapFileToMemory(const char *file_name, uptr *buff_size) {
129  fd_t fd = OpenFile(file_name, false);
130  CHECK_NE(fd, kInvalidFd);
131  uptr fsize = internal_filesize(fd);
132  CHECK_NE(fsize, (uptr)-1);
133  CHECK_GT(fsize, 0);
134  *buff_size = RoundUpTo(fsize, GetPageSizeCached());
135  void *map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
136  return (map == MAP_FAILED) ? 0 : map;
137}
138
139
140static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
141                                        uptr start2, uptr end2) {
142  CHECK(start1 <= end1);
143  CHECK(start2 <= end2);
144  return (end1 < start2) || (end2 < start1);
145}
146
147// FIXME: this is thread-unsafe, but should not cause problems most of the time.
148// When the shadow is mapped only a single thread usually exists (plus maybe
149// several worker threads on Mac, which aren't expected to map big chunks of
150// memory).
151bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
152  MemoryMappingLayout procmaps;
153  uptr start, end;
154  while (procmaps.Next(&start, &end,
155                       /*offset*/0, /*filename*/0, /*filename_size*/0)) {
156    if (!IntervalsAreSeparate(start, end, range_start, range_end))
157      return false;
158  }
159  return true;
160}
161
162void DumpProcessMap() {
163  MemoryMappingLayout proc_maps;
164  uptr start, end;
165  const sptr kBufSize = 4095;
166  char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__);
167  Report("Process memory map follows:\n");
168  while (proc_maps.Next(&start, &end, /* file_offset */0,
169                        filename, kBufSize)) {
170    Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
171  }
172  Report("End of process memory map.\n");
173  UnmapOrDie(filename, kBufSize);
174}
175
176const char *GetPwd() {
177  return GetEnv("PWD");
178}
179
180void DisableCoreDumper() {
181  struct rlimit nocore;
182  nocore.rlim_cur = 0;
183  nocore.rlim_max = 0;
184  setrlimit(RLIMIT_CORE, &nocore);
185}
186
187bool StackSizeIsUnlimited() {
188  struct rlimit rlim;
189  CHECK_EQ(0, getrlimit(RLIMIT_STACK, &rlim));
190  return (rlim.rlim_cur == (uptr)-1);
191}
192
193void SetStackSizeLimitInBytes(uptr limit) {
194  struct rlimit rlim;
195  rlim.rlim_cur = limit;
196  rlim.rlim_max = limit;
197  if (setrlimit(RLIMIT_STACK, &rlim)) {
198    Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
199    Die();
200  }
201  CHECK(!StackSizeIsUnlimited());
202}
203
204void SleepForSeconds(int seconds) {
205  sleep(seconds);
206}
207
208void SleepForMillis(int millis) {
209  usleep(millis * 1000);
210}
211
212void Abort() {
213  abort();
214}
215
216int Atexit(void (*function)(void)) {
217#ifndef SANITIZER_GO
218  return atexit(function);
219#else
220  return 0;
221#endif
222}
223
224int internal_isatty(fd_t fd) {
225  return isatty(fd);
226}
227
228}  // namespace __sanitizer
229
230#endif  // __linux__ || __APPLE_
231