Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 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 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 12 | // sanitizer_posix.h. |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | |
| 15 | #include "sanitizer_platform.h" |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 16 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 17 | #if SANITIZER_POSIX |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 18 | |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 19 | #include "sanitizer_common.h" |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 20 | #include "sanitizer_libc.h" |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 21 | #include "sanitizer_posix.h" |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 22 | #include "sanitizer_procmaps.h" |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 23 | #include "sanitizer_stacktrace.h" |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 24 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <signal.h> |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 27 | #include <sys/mman.h> |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 28 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 29 | #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 Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 37 | #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 Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 44 | namespace __sanitizer { |
| 45 | |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 46 | // ------------- sanitizer_common.h |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 47 | uptr GetMmapGranularity() { |
| 48 | return GetPageSize(); |
| 49 | } |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 50 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 51 | #if SANITIZER_WORDSIZE == 32 |
| 52 | // Take care of unusable kernel area in top gigabyte. |
| 53 | static uptr GetKernelAreaSize() { |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 54 | #if SANITIZER_LINUX && !SANITIZER_X32 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 55 | 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 Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 61 | while (proc_maps.Next(/*start*/nullptr, &end, |
| 62 | /*offset*/nullptr, /*filename*/nullptr, |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 63 | /*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 Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 86 | #endif // SANITIZER_LINUX && !SANITIZER_X32 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 87 | } |
| 88 | #endif // SANITIZER_WORDSIZE == 32 |
| 89 | |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 90 | uptr GetMaxVirtualAddress() { |
| 91 | #if SANITIZER_WORDSIZE == 64 |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 92 | # if defined(__powerpc64__) || defined(__aarch64__) |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 93 | // On PowerPC64 we have two different address space layouts: 44- and 46-bit. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 94 | // We somehow need to figure out which one we are using now and choose |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 95 | // 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 98 | // This should (does) work for both PowerPC64 Endian modes. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 99 | // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 100 | return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 101 | # elif defined(__mips64) |
| 102 | return (1ULL << 40) - 1; // 0x000000ffffffffffUL; |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 103 | # else |
| 104 | return (1ULL << 47) - 1; // 0x00007fffffffffffUL; |
| 105 | # endif |
| 106 | #else // SANITIZER_WORDSIZE == 32 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 107 | 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 Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 112 | #endif // SANITIZER_WORDSIZE |
| 113 | } |
| 114 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 115 | void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) { |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 116 | size = RoundUpTo(size, GetPageSizeCached()); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 117 | uptr res = internal_mmap(nullptr, size, |
| 118 | PROT_READ | PROT_WRITE, |
| 119 | MAP_PRIVATE | MAP_ANON, -1, 0); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 120 | int reserrno; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 121 | if (internal_iserror(res, &reserrno)) |
| 122 | ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 123 | IncreaseTotalMmap(size); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 124 | return (void *)res; |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void UnmapOrDie(void *addr, uptr size) { |
| 128 | if (!addr || !size) return; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 129 | uptr res = internal_munmap(addr, size); |
| 130 | if (internal_iserror(res)) { |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 131 | Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n", |
| 132 | SanitizerToolName, size, size, addr); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 133 | CHECK("unable to unmap" && 0); |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 134 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 135 | DecreaseTotalMmap(size); |
| 136 | } |
| 137 | |
| 138 | void *MmapNoReserveOrDie(uptr size, const char *mem_type) { |
| 139 | uptr PageSize = GetPageSizeCached(); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 140 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 145 | int reserrno; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 146 | if (internal_iserror(p, &reserrno)) |
| 147 | ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 148 | IncreaseTotalMmap(size); |
| 149 | return (void *)p; |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 152 | void *MmapFixedOrDie(uptr fixed_addr, uptr size) { |
| 153 | uptr PageSize = GetPageSizeCached(); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 154 | uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 155 | RoundUpTo(size, PageSize), |
| 156 | PROT_READ | PROT_WRITE, |
| 157 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 158 | -1, 0); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 159 | int reserrno; |
| 160 | if (internal_iserror(p, &reserrno)) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 161 | 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 Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 165 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 166 | IncreaseTotalMmap(size); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 167 | return (void *)p; |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 170 | bool MprotectNoAccess(uptr addr, uptr size) { |
| 171 | return 0 == internal_mprotect((void*)addr, size, PROT_NONE); |
| 172 | } |
| 173 | |
| 174 | fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) { |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 175 | 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 Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 181 | fd_t res = internal_open(filename, flags, 0660); |
| 182 | if (internal_iserror(res, errno_p)) |
| 183 | return kInvalidFd; |
| 184 | return res; |
| 185 | } |
| 186 | |
| 187 | void CloseFile(fd_t fd) { |
| 188 | internal_close(fd); |
| 189 | } |
| 190 | |
| 191 | bool 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 | |
| 201 | bool 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 | |
| 211 | bool 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 Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 216 | void *MapFileToMemory(const char *file_name, uptr *buff_size) { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 217 | fd_t fd = OpenFile(file_name, RdOnly); |
| 218 | CHECK(fd != kInvalidFd); |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 219 | uptr fsize = internal_filesize(fd); |
| 220 | CHECK_NE(fsize, (uptr)-1); |
| 221 | CHECK_GT(fsize, 0); |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 222 | *buff_size = RoundUpTo(fsize, GetPageSizeCached()); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 223 | uptr map = internal_mmap(nullptr, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 224 | return internal_iserror(map) ? nullptr : (void *)map; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 227 | void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 228 | 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 Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 231 | int mmap_errno = 0; |
| 232 | if (internal_iserror(p, &mmap_errno)) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 233 | 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 Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 236 | } |
| 237 | return (void *)p; |
| 238 | } |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 239 | |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 240 | static 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). |
| 251 | bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 252 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 253 | uptr start, end; |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 254 | while (proc_maps.Next(&start, &end, |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 255 | /*offset*/nullptr, /*filename*/nullptr, |
| 256 | /*filename_size*/0, /*protection*/nullptr)) { |
| 257 | if (start == end) continue; // Empty range. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 258 | CHECK_NE(0, end); |
| 259 | if (!IntervalsAreSeparate(start, end - 1, range_start, range_end)) |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 265 | void DumpProcessMap() { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 266 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 267 | uptr start, end; |
| 268 | const sptr kBufSize = 4095; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 269 | char *filename = (char*)MmapOrDie(kBufSize, __func__); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 270 | Report("Process memory map follows:\n"); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 271 | while (proc_maps.Next(&start, &end, /* file_offset */nullptr, |
| 272 | filename, kBufSize, /* protection */nullptr)) { |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 273 | Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename); |
| 274 | } |
| 275 | Report("End of process memory map.\n"); |
Alexey Samsonov | b84ee02 | 2012-06-15 07:41:23 +0000 | [diff] [blame] | 276 | UnmapOrDie(filename, kBufSize); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 279 | const char *GetPwd() { |
| 280 | return GetEnv("PWD"); |
| 281 | } |
| 282 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 283 | bool IsPathSeparator(const char c) { |
| 284 | return c == '/'; |
| 285 | } |
| 286 | |
| 287 | bool IsAbsolutePath(const char *path) { |
| 288 | return path != nullptr && IsPathSeparator(path[0]); |
| 289 | } |
| 290 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 291 | void 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 Kleckner | 923bac7 | 2013-09-05 03:19:57 +0000 | [diff] [blame] | 298 | Die(); |
| 299 | } |
| 300 | } |
| 301 | |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 302 | bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) { |
| 303 | uptr s, e, off, prot; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 304 | InternalScopedString buff(kMaxPathLength); |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 305 | MemoryMappingLayout proc_maps(/*cache_enabled*/false); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 306 | while (proc_maps.Next(&s, &e, &off, buff.data(), buff.size(), &prot)) { |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 307 | if ((prot & MemoryMappingLayout::kProtectionExecute) != 0 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 308 | && internal_strcmp(module, buff.data()) == 0) { |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 309 | *start = s; |
| 310 | *end = e; |
| 311 | return true; |
| 312 | } |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 317 | SignalContext 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 Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 324 | } // namespace __sanitizer |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 325 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 326 | #endif // SANITIZER_POSIX |