sanitizer_posix.cc revision 84d57b4ce545d6c19effac01124749a9df0fd0a5
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
35
36int GetPid() {
37  return getpid();
38}
39
40uptr GetThreadSelf() {
41  return (uptr)pthread_self();
42}
43
44void *MmapOrDie(uptr size, const char *mem_type) {
45  size = RoundUpTo(size, kPageSize);
46  void *res = internal_mmap(0, size,
47                            PROT_READ | PROT_WRITE,
48                            MAP_PRIVATE | MAP_ANON, -1, 0);
49  if (res == (void*)-1) {
50    static int recursion_count;
51    if (recursion_count) {
52      // The Report() and CHECK calls below may call mmap recursively and fail.
53      // If we went into recursion, just die.
54      RawWrite("AddressSanitizer is unable to mmap\n");
55      Die();
56    }
57    recursion_count++;
58    Report("ERROR: Failed to allocate 0x%zx (%zd) bytes of %s: %s\n",
59           size, size, mem_type, strerror(errno));
60    DumpProcessMap();
61    CHECK("unable to mmap" && 0);
62  }
63  return res;
64}
65
66void UnmapOrDie(void *addr, uptr size) {
67  if (!addr || !size) return;
68  int res = internal_munmap(addr, size);
69  if (res != 0) {
70    Report("ERROR: Failed to deallocate 0x%zx (%zd) bytes at address %p\n",
71           size, size, addr);
72    CHECK("unable to unmap" && 0);
73  }
74}
75
76void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
77  return internal_mmap((void*)fixed_addr, size,
78                      PROT_READ | PROT_WRITE,
79                      MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
80                      -1, 0);
81}
82
83void *Mprotect(uptr fixed_addr, uptr size) {
84  return internal_mmap((void*)fixed_addr, size,
85                       PROT_NONE,
86                       MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
87                       -1, 0);
88}
89
90void *MapFileToMemory(const char *file_name, uptr *buff_size) {
91  fd_t fd = internal_open(file_name, false);
92  CHECK_NE(fd, kInvalidFd);
93  uptr fsize = internal_filesize(fd);
94  CHECK_NE(fsize, (uptr)-1);
95  CHECK_GT(fsize, 0);
96  *buff_size = RoundUpTo(fsize, kPageSize);
97  void *map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
98  return (map == MAP_FAILED) ? 0 : map;
99}
100
101
102static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
103                                        uptr start2, uptr end2) {
104  CHECK(start1 <= end1);
105  CHECK(start2 <= end2);
106  return (end1 < start2) || (end2 < start1);
107}
108
109// FIXME: this is thread-unsafe, but should not cause problems most of the time.
110// When the shadow is mapped only a single thread usually exists (plus maybe
111// several worker threads on Mac, which aren't expected to map big chunks of
112// memory).
113bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
114  MemoryMappingLayout procmaps;
115  uptr start, end;
116  while (procmaps.Next(&start, &end,
117                       /*offset*/0, /*filename*/0, /*filename_size*/0)) {
118    if (!IntervalsAreSeparate(start, end, range_start, range_end))
119      return false;
120  }
121  return true;
122}
123
124void DumpProcessMap() {
125  MemoryMappingLayout proc_maps;
126  uptr start, end;
127  const sptr kBufSize = 4095;
128  char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__);
129  Report("Process memory map follows:\n");
130  while (proc_maps.Next(&start, &end, /* file_offset */0,
131                        filename, kBufSize)) {
132    Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
133  }
134  Report("End of process memory map.\n");
135  UnmapOrDie(filename, kBufSize);
136}
137
138const char *GetPwd() {
139  return GetEnv("PWD");
140}
141
142void DisableCoreDumper() {
143  struct rlimit nocore;
144  nocore.rlim_cur = 0;
145  nocore.rlim_max = 0;
146  setrlimit(RLIMIT_CORE, &nocore);
147}
148
149bool StackSizeIsUnlimited() {
150  struct rlimit rlim;
151  CHECK_EQ(0, getrlimit(RLIMIT_STACK, &rlim));
152  return (rlim.rlim_cur == (uptr)-1);
153}
154
155void SetStackSizeLimitInBytes(uptr limit) {
156  struct rlimit rlim;
157  rlim.rlim_cur = limit;
158  rlim.rlim_max = limit;
159  CHECK_EQ(0, setrlimit(RLIMIT_STACK, &rlim));
160  CHECK(!StackSizeIsUnlimited());
161}
162
163void SleepForSeconds(int seconds) {
164  sleep(seconds);
165}
166
167void SleepForMillis(int millis) {
168  usleep(millis * 1000);
169}
170
171void Exit(int exitcode) {
172  _exit(exitcode);
173}
174
175void Abort() {
176  abort();
177}
178
179int Atexit(void (*function)(void)) {
180#ifndef SANITIZER_GO
181  return atexit(function);
182#else
183  return 0;
184#endif
185}
186
187int internal_isatty(fd_t fd) {
188  return isatty(fd);
189}
190
191}  // namespace __sanitizer
192
193#endif  // __linux__ || __APPLE_
194