blob: 5ae68663df0eb68ee940a79e2d2d8d45086ab449 [file] [log] [blame]
Alexey Samsonov1f11d312012-06-05 09:49:25 +00001//===-- 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
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070012// sanitizer_posix.h.
Alexey Samsonov1f11d312012-06-05 09:49:25 +000013//===----------------------------------------------------------------------===//
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000014
15#include "sanitizer_platform.h"
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080016
Stephen Hines2d1fdb22014-05-28 23:58:16 -070017#if SANITIZER_POSIX
Alexey Samsonov1f11d312012-06-05 09:49:25 +000018
Alexey Samsonov230c3be2012-06-06 09:26:25 +000019#include "sanitizer_common.h"
Alexey Samsonov1f11d312012-06-05 09:49:25 +000020#include "sanitizer_libc.h"
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070021#include "sanitizer_posix.h"
Alexey Samsonovbe7420c2012-06-15 06:08:19 +000022#include "sanitizer_procmaps.h"
Sergey Matveev736cf492013-05-08 12:45:55 +000023#include "sanitizer_stacktrace.h"
Alexey Samsonov1f11d312012-06-05 09:49:25 +000024
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070025#include <fcntl.h>
26#include <signal.h>
Alexey Samsonov230c3be2012-06-06 09:26:25 +000027#include <sys/mman.h>
Alexey Samsonov1f11d312012-06-05 09:49:25 +000028
Stephen Hines2d1fdb22014-05-28 23:58:16 -070029#if SANITIZER_LINUX
30#include <sys/utsname.h>
31#endif
32
33#if SANITIZER_LINUX && !SANITIZER_ANDROID
34#include <sys/personality.h>
35#endif
36
Stephen Hines86277eb2015-03-23 12:06:32 -070037#if SANITIZER_FREEBSD
38// The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
39// that, it was never implemented. So just define it to zero.
40#undef MAP_NORESERVE
41#define MAP_NORESERVE 0
42#endif
43
Alexey Samsonov1f11d312012-06-05 09:49:25 +000044namespace __sanitizer {
45
Alexey Samsonovbe7420c2012-06-15 06:08:19 +000046// ------------- sanitizer_common.h
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +000047uptr GetMmapGranularity() {
48 return GetPageSize();
49}
Alexey Samsonovbe7420c2012-06-15 06:08:19 +000050
Stephen Hines2d1fdb22014-05-28 23:58:16 -070051#if SANITIZER_WORDSIZE == 32
52// Take care of unusable kernel area in top gigabyte.
53static uptr GetKernelAreaSize() {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070054#if SANITIZER_LINUX && !SANITIZER_X32
Stephen Hines2d1fdb22014-05-28 23:58:16 -070055 const uptr gbyte = 1UL << 30;
56
57 // Firstly check if there are writable segments
58 // mapped to top gigabyte (e.g. stack).
59 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
60 uptr end, prot;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080061 while (proc_maps.Next(/*start*/nullptr, &end,
62 /*offset*/nullptr, /*filename*/nullptr,
Stephen Hines2d1fdb22014-05-28 23:58:16 -070063 /*filename_size*/0, &prot)) {
64 if ((end >= 3 * gbyte)
65 && (prot & MemoryMappingLayout::kProtectionWrite) != 0)
66 return 0;
67 }
68
69#if !SANITIZER_ANDROID
70 // Even if nothing is mapped, top Gb may still be accessible
71 // if we are running on 64-bit kernel.
72 // Uname may report misleading results if personality type
73 // is modified (e.g. under schroot) so check this as well.
74 struct utsname uname_info;
75 int pers = personality(0xffffffffUL);
76 if (!(pers & PER_MASK)
77 && uname(&uname_info) == 0
78 && internal_strstr(uname_info.machine, "64"))
79 return 0;
80#endif // SANITIZER_ANDROID
81
82 // Top gigabyte is reserved for kernel.
83 return gbyte;
84#else
85 return 0;
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070086#endif // SANITIZER_LINUX && !SANITIZER_X32
Stephen Hines2d1fdb22014-05-28 23:58:16 -070087}
88#endif // SANITIZER_WORDSIZE == 32
89
Timur Iskhodzhanovbb7f2d82013-07-16 09:47:39 +000090uptr GetMaxVirtualAddress() {
91#if SANITIZER_WORDSIZE == 64
Stephen Hines86277eb2015-03-23 12:06:32 -070092# if defined(__powerpc64__) || defined(__aarch64__)
Timur Iskhodzhanovbb7f2d82013-07-16 09:47:39 +000093 // On PowerPC64 we have two different address space layouts: 44- and 46-bit.
Stephen Hines2d1fdb22014-05-28 23:58:16 -070094 // We somehow need to figure out which one we are using now and choose
Timur Iskhodzhanovbb7f2d82013-07-16 09:47:39 +000095 // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL.
96 // Note that with 'ulimit -s unlimited' the stack is moved away from the top
97 // of the address space, so simply checking the stack address is not enough.
Stephen Hines6d186232014-11-26 17:56:19 -080098 // This should (does) work for both PowerPC64 Endian modes.
Stephen Hines86277eb2015-03-23 12:06:32 -070099 // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit.
Stephen Hines6d186232014-11-26 17:56:19 -0800100 return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1;
Stephen Hines6d186232014-11-26 17:56:19 -0800101# elif defined(__mips64)
102 return (1ULL << 40) - 1; // 0x000000ffffffffffUL;
Timur Iskhodzhanovbb7f2d82013-07-16 09:47:39 +0000103# else
104 return (1ULL << 47) - 1; // 0x00007fffffffffffUL;
105# endif
106#else // SANITIZER_WORDSIZE == 32
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700107 uptr res = (1ULL << 32) - 1; // 0xffffffff;
108 if (!common_flags()->full_address_space)
109 res -= GetKernelAreaSize();
110 CHECK_LT(reinterpret_cast<uptr>(&res), res);
111 return res;
Timur Iskhodzhanovbb7f2d82013-07-16 09:47:39 +0000112#endif // SANITIZER_WORDSIZE
113}
114
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800115void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000116 size = RoundUpTo(size, GetPageSizeCached());
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800117 uptr res = internal_mmap(nullptr, size,
118 PROT_READ | PROT_WRITE,
119 MAP_PRIVATE | MAP_ANON, -1, 0);
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000120 int reserrno;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800121 if (internal_iserror(res, &reserrno))
122 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700123 IncreaseTotalMmap(size);
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000124 return (void *)res;
Alexey Samsonov230c3be2012-06-06 09:26:25 +0000125}
126
127void UnmapOrDie(void *addr, uptr size) {
128 if (!addr || !size) return;
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000129 uptr res = internal_munmap(addr, size);
130 if (internal_iserror(res)) {
Kostya Serebryany859778a2013-01-31 14:11:21 +0000131 Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
132 SanitizerToolName, size, size, addr);
Alexey Samsonova25b3462012-06-06 16:15:07 +0000133 CHECK("unable to unmap" && 0);
Alexey Samsonov230c3be2012-06-06 09:26:25 +0000134 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700135 DecreaseTotalMmap(size);
136}
137
138void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
139 uptr PageSize = GetPageSizeCached();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800140 uptr p = internal_mmap(nullptr,
141 RoundUpTo(size, PageSize),
142 PROT_READ | PROT_WRITE,
143 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
144 -1, 0);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700145 int reserrno;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800146 if (internal_iserror(p, &reserrno))
147 ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700148 IncreaseTotalMmap(size);
149 return (void *)p;
Alexey Samsonov230c3be2012-06-06 09:26:25 +0000150}
151
Kostya Serebryany9bfe78f2012-12-13 05:36:00 +0000152void *MmapFixedOrDie(uptr fixed_addr, uptr size) {
153 uptr PageSize = GetPageSizeCached();
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000154 uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
Kostya Serebryany9bfe78f2012-12-13 05:36:00 +0000155 RoundUpTo(size, PageSize),
156 PROT_READ | PROT_WRITE,
157 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
158 -1, 0);
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000159 int reserrno;
160 if (internal_iserror(p, &reserrno)) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800161 char mem_type[30];
162 internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
163 fixed_addr);
164 ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
Kostya Serebryany9bfe78f2012-12-13 05:36:00 +0000165 }
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700166 IncreaseTotalMmap(size);
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000167 return (void *)p;
Kostya Serebryany9bfe78f2012-12-13 05:36:00 +0000168}
169
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700170bool MprotectNoAccess(uptr addr, uptr size) {
171 return 0 == internal_mprotect((void*)addr, size, PROT_NONE);
172}
173
174fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700175 int flags;
176 switch (mode) {
177 case RdOnly: flags = O_RDONLY; break;
178 case WrOnly: flags = O_WRONLY | O_CREAT; break;
179 case RdWr: flags = O_RDWR | O_CREAT; break;
180 }
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700181 fd_t res = internal_open(filename, flags, 0660);
182 if (internal_iserror(res, errno_p))
183 return kInvalidFd;
184 return res;
185}
186
187void CloseFile(fd_t fd) {
188 internal_close(fd);
189}
190
191bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
192 error_t *error_p) {
193 uptr res = internal_read(fd, buff, buff_size);
194 if (internal_iserror(res, error_p))
195 return false;
196 if (bytes_read)
197 *bytes_read = res;
198 return true;
199}
200
201bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
202 error_t *error_p) {
203 uptr res = internal_write(fd, buff, buff_size);
204 if (internal_iserror(res, error_p))
205 return false;
206 if (bytes_written)
207 *bytes_written = res;
208 return true;
209}
210
211bool RenameFile(const char *oldpath, const char *newpath, error_t *error_p) {
212 uptr res = internal_rename(oldpath, newpath);
213 return !internal_iserror(res, error_p);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700214}
215
Alexey Samsonova68633f2012-07-03 08:24:14 +0000216void *MapFileToMemory(const char *file_name, uptr *buff_size) {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700217 fd_t fd = OpenFile(file_name, RdOnly);
218 CHECK(fd != kInvalidFd);
Alexey Samsonova68633f2012-07-03 08:24:14 +0000219 uptr fsize = internal_filesize(fd);
220 CHECK_NE(fsize, (uptr)-1);
221 CHECK_GT(fsize, 0);
Kostya Serebryanyf67ec2b2012-11-23 15:38:49 +0000222 *buff_size = RoundUpTo(fsize, GetPageSizeCached());
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800223 uptr map = internal_mmap(nullptr, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
224 return internal_iserror(map) ? nullptr : (void *)map;
Alexey Samsonova68633f2012-07-03 08:24:14 +0000225}
226
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800227void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700228 uptr flags = MAP_SHARED;
229 if (addr) flags |= MAP_FIXED;
230 uptr p = internal_mmap(addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700231 int mmap_errno = 0;
232 if (internal_iserror(p, &mmap_errno)) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800233 Printf("could not map writable file (%d, %lld, %zu): %zd, errno: %d\n",
234 fd, (long long)offset, size, p, mmap_errno);
235 return nullptr;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700236 }
237 return (void *)p;
238}
Alexey Samsonova68633f2012-07-03 08:24:14 +0000239
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000240static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
241 uptr start2, uptr end2) {
242 CHECK(start1 <= end1);
243 CHECK(start2 <= end2);
244 return (end1 < start2) || (end2 < start1);
245}
246
247// FIXME: this is thread-unsafe, but should not cause problems most of the time.
248// When the shadow is mapped only a single thread usually exists (plus maybe
249// several worker threads on Mac, which aren't expected to map big chunks of
250// memory).
251bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
Alexander Potapenko9ae28832013-03-26 10:34:37 +0000252 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000253 uptr start, end;
Alexander Potapenko9ae28832013-03-26 10:34:37 +0000254 while (proc_maps.Next(&start, &end,
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800255 /*offset*/nullptr, /*filename*/nullptr,
256 /*filename_size*/0, /*protection*/nullptr)) {
257 if (start == end) continue; // Empty range.
Stephen Hines86277eb2015-03-23 12:06:32 -0700258 CHECK_NE(0, end);
259 if (!IntervalsAreSeparate(start, end - 1, range_start, range_end))
Alexey Samsonovdd3a9112012-06-15 07:29:14 +0000260 return false;
261 }
262 return true;
263}
264
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000265void DumpProcessMap() {
Alexander Potapenko9ae28832013-03-26 10:34:37 +0000266 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000267 uptr start, end;
268 const sptr kBufSize = 4095;
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700269 char *filename = (char*)MmapOrDie(kBufSize, __func__);
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000270 Report("Process memory map follows:\n");
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800271 while (proc_maps.Next(&start, &end, /* file_offset */nullptr,
272 filename, kBufSize, /* protection */nullptr)) {
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000273 Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
274 }
275 Report("End of process memory map.\n");
Alexey Samsonovb84ee022012-06-15 07:41:23 +0000276 UnmapOrDie(filename, kBufSize);
Alexey Samsonovbe7420c2012-06-15 06:08:19 +0000277}
278
Alexey Samsonov0969bcf2012-06-18 08:44:30 +0000279const char *GetPwd() {
280 return GetEnv("PWD");
281}
282
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700283bool IsPathSeparator(const char c) {
284 return c == '/';
285}
286
287bool IsAbsolutePath(const char *path) {
288 return path != nullptr && IsPathSeparator(path[0]);
289}
290
Stephen Hines86277eb2015-03-23 12:06:32 -0700291void ReportFile::Write(const char *buffer, uptr length) {
292 SpinMutexLock l(mu);
293 static const char *kWriteError =
294 "ReportFile::Write() can't output requested buffer!\n";
295 ReopenIfNecessary();
296 if (length != internal_write(fd, buffer, length)) {
297 internal_write(fd, kWriteError, internal_strlen(kWriteError));
Reid Kleckner923bac72013-09-05 03:19:57 +0000298 Die();
299 }
300}
301
Dmitry Vyukov821acfa2013-09-21 21:41:08 +0000302bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) {
303 uptr s, e, off, prot;
Stephen Hines86277eb2015-03-23 12:06:32 -0700304 InternalScopedString buff(kMaxPathLength);
Dmitry Vyukov821acfa2013-09-21 21:41:08 +0000305 MemoryMappingLayout proc_maps(/*cache_enabled*/false);
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700306 while (proc_maps.Next(&s, &e, &off, buff.data(), buff.size(), &prot)) {
Dmitry Vyukov821acfa2013-09-21 21:41:08 +0000307 if ((prot & MemoryMappingLayout::kProtectionExecute) != 0
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700308 && internal_strcmp(module, buff.data()) == 0) {
Dmitry Vyukov821acfa2013-09-21 21:41:08 +0000309 *start = s;
310 *end = e;
311 return true;
312 }
313 }
314 return false;
315}
316
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700317SignalContext SignalContext::Create(void *siginfo, void *context) {
318 uptr addr = (uptr)((siginfo_t*)siginfo)->si_addr;
319 uptr pc, sp, bp;
320 GetPcSpBp(context, &pc, &sp, &bp);
321 return SignalContext(context, addr, pc, sp, bp);
322}
323
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800324} // namespace __sanitizer
Alexey Samsonov1f11d312012-06-05 09:49:25 +0000325
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800326#endif // SANITIZER_POSIX