Fix spacing.
[gcc.git] / libsanitizer / sanitizer_common / sanitizer_posix.cc
1 //===-- sanitizer_posix.cc ------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements POSIX-specific functions from
10 // sanitizer_libc.h.
11 //===----------------------------------------------------------------------===//
12
13 #include "sanitizer_platform.h"
14 #if SANITIZER_LINUX || SANITIZER_MAC
15
16 #include "sanitizer_common.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_procmaps.h"
19 #include "sanitizer_stacktrace.h"
20
21 #include <sys/mman.h>
22
23 namespace __sanitizer {
24
25 // ------------- sanitizer_common.h
26 uptr GetMmapGranularity() {
27 return GetPageSize();
28 }
29
30 uptr GetMaxVirtualAddress() {
31 #if SANITIZER_WORDSIZE == 64
32 # if defined(__powerpc64__)
33 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
34 // We somehow need to figure our which one we are using now and choose
35 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
36 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
37 // of the address space, so simply checking the stack address is not enough.
38 return (1ULL << 44) - 1; // 0x00000fffffffffffUL
39 # else
40 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
41 # endif
42 #else // SANITIZER_WORDSIZE == 32
43 // FIXME: We can probably lower this on Android?
44 return (1ULL << 32) - 1; // 0xffffffff;
45 #endif // SANITIZER_WORDSIZE
46 }
47
48 void *MmapOrDie(uptr size, const char *mem_type) {
49 size = RoundUpTo(size, GetPageSizeCached());
50 uptr res = internal_mmap(0, size,
51 PROT_READ | PROT_WRITE,
52 MAP_PRIVATE | MAP_ANON, -1, 0);
53 int reserrno;
54 if (internal_iserror(res, &reserrno)) {
55 static int recursion_count;
56 if (recursion_count) {
57 // The Report() and CHECK calls below may call mmap recursively and fail.
58 // If we went into recursion, just die.
59 RawWrite("ERROR: Failed to mmap\n");
60 Die();
61 }
62 recursion_count++;
63 Report("ERROR: %s failed to allocate 0x%zx (%zd) bytes of %s: %d\n",
64 SanitizerToolName, size, size, mem_type, reserrno);
65 DumpProcessMap();
66 CHECK("unable to mmap" && 0);
67 }
68 return (void *)res;
69 }
70
71 void UnmapOrDie(void *addr, uptr size) {
72 if (!addr || !size) return;
73 uptr res = internal_munmap(addr, size);
74 if (internal_iserror(res)) {
75 Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
76 SanitizerToolName, size, size, addr);
77 CHECK("unable to unmap" && 0);
78 }
79 }
80
81 void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
82 uptr PageSize = GetPageSizeCached();
83 uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
84 RoundUpTo(size, PageSize),
85 PROT_READ | PROT_WRITE,
86 MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
87 -1, 0);
88 int reserrno;
89 if (internal_iserror(p, &reserrno))
90 Report("ERROR: "
91 "%s failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n",
92 SanitizerToolName, size, size, fixed_addr, reserrno);
93 return (void *)p;
94 }
95
96 void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
97 uptr PageSize = GetPageSizeCached();
98 uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
99 RoundUpTo(size, PageSize),
100 PROT_READ | PROT_WRITE,
101 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
102 -1, 0);
103 int reserrno;
104 if (internal_iserror(p, &reserrno)) {
105 Report("ERROR:"
106 " %s failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n",
107 SanitizerToolName, size, size, fixed_addr, reserrno);
108 CHECK("unable to mmap" && 0);
109 }
110 return (void *)p;
111 }
112
113 void *Mprotect(uptr fixed_addr, uptr size) {
114 return (void *)internal_mmap((void*)fixed_addr, size,
115 PROT_NONE,
116 MAP_PRIVATE | MAP_ANON | MAP_FIXED |
117 MAP_NORESERVE, -1, 0);
118 }
119
120 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
121 uptr openrv = OpenFile(file_name, false);
122 CHECK(!internal_iserror(openrv));
123 fd_t fd = openrv;
124 uptr fsize = internal_filesize(fd);
125 CHECK_NE(fsize, (uptr)-1);
126 CHECK_GT(fsize, 0);
127 *buff_size = RoundUpTo(fsize, GetPageSizeCached());
128 uptr map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
129 return internal_iserror(map) ? 0 : (void *)map;
130 }
131
132
133 static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
134 uptr start2, uptr end2) {
135 CHECK(start1 <= end1);
136 CHECK(start2 <= end2);
137 return (end1 < start2) || (end2 < start1);
138 }
139
140 // FIXME: this is thread-unsafe, but should not cause problems most of the time.
141 // When the shadow is mapped only a single thread usually exists (plus maybe
142 // several worker threads on Mac, which aren't expected to map big chunks of
143 // memory).
144 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
145 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
146 uptr start, end;
147 while (proc_maps.Next(&start, &end,
148 /*offset*/0, /*filename*/0, /*filename_size*/0,
149 /*protection*/0)) {
150 if (!IntervalsAreSeparate(start, end, range_start, range_end))
151 return false;
152 }
153 return true;
154 }
155
156 void DumpProcessMap() {
157 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
158 uptr start, end;
159 const sptr kBufSize = 4095;
160 char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__);
161 Report("Process memory map follows:\n");
162 while (proc_maps.Next(&start, &end, /* file_offset */0,
163 filename, kBufSize, /* protection */0)) {
164 Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
165 }
166 Report("End of process memory map.\n");
167 UnmapOrDie(filename, kBufSize);
168 }
169
170 const char *GetPwd() {
171 return GetEnv("PWD");
172 }
173
174 char *FindPathToBinary(const char *name) {
175 const char *path = GetEnv("PATH");
176 if (!path)
177 return 0;
178 uptr name_len = internal_strlen(name);
179 InternalScopedBuffer<char> buffer(kMaxPathLength);
180 const char *beg = path;
181 while (true) {
182 const char *end = internal_strchrnul(beg, ':');
183 uptr prefix_len = end - beg;
184 if (prefix_len + name_len + 2 <= kMaxPathLength) {
185 internal_memcpy(buffer.data(), beg, prefix_len);
186 buffer[prefix_len] = '/';
187 internal_memcpy(&buffer[prefix_len + 1], name, name_len);
188 buffer[prefix_len + 1 + name_len] = '\0';
189 if (FileExists(buffer.data()))
190 return internal_strdup(buffer.data());
191 }
192 if (*end == '\0') break;
193 beg = end + 1;
194 }
195 return 0;
196 }
197
198 void MaybeOpenReportFile() {
199 if (!log_to_file || (report_fd_pid == internal_getpid())) return;
200 InternalScopedBuffer<char> report_path_full(4096);
201 internal_snprintf(report_path_full.data(), report_path_full.size(),
202 "%s.%d", report_path_prefix, internal_getpid());
203 uptr openrv = OpenFile(report_path_full.data(), true);
204 if (internal_iserror(openrv)) {
205 report_fd = kStderrFd;
206 log_to_file = false;
207 Report("ERROR: Can't open file: %s\n", report_path_full.data());
208 Die();
209 }
210 if (report_fd != kInvalidFd) {
211 // We're in the child. Close the parent's log.
212 internal_close(report_fd);
213 }
214 report_fd = openrv;
215 report_fd_pid = internal_getpid();
216 }
217
218 void RawWrite(const char *buffer) {
219 static const char *kRawWriteError =
220 "RawWrite can't output requested buffer!\n";
221 uptr length = (uptr)internal_strlen(buffer);
222 MaybeOpenReportFile();
223 if (length != internal_write(report_fd, buffer, length)) {
224 internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));
225 Die();
226 }
227 }
228
229 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) {
230 uptr s, e, off, prot;
231 InternalMmapVector<char> fn(4096);
232 fn.push_back(0);
233 MemoryMappingLayout proc_maps(/*cache_enabled*/false);
234 while (proc_maps.Next(&s, &e, &off, &fn[0], fn.capacity(), &prot)) {
235 if ((prot & MemoryMappingLayout::kProtectionExecute) != 0
236 && internal_strcmp(module, &fn[0]) == 0) {
237 *start = s;
238 *end = e;
239 return true;
240 }
241 }
242 return false;
243 }
244
245 } // namespace __sanitizer
246
247 #endif // SANITIZER_LINUX || SANITIZER_MAC