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" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 16 | #if SANITIZER_POSIX |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 17 | |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 18 | #include "sanitizer_common.h" |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 19 | #include "sanitizer_libc.h" |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 20 | #include "sanitizer_posix.h" |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 21 | #include "sanitizer_procmaps.h" |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 22 | #include "sanitizer_stacktrace.h" |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 23 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <signal.h> |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 26 | #include <sys/mman.h> |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 27 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 28 | #if SANITIZER_LINUX |
| 29 | #include <sys/utsname.h> |
| 30 | #endif |
| 31 | |
| 32 | #if SANITIZER_LINUX && !SANITIZER_ANDROID |
| 33 | #include <sys/personality.h> |
| 34 | #endif |
| 35 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 36 | #if SANITIZER_FREEBSD |
| 37 | // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before |
| 38 | // that, it was never implemented. So just define it to zero. |
| 39 | #undef MAP_NORESERVE |
| 40 | #define MAP_NORESERVE 0 |
| 41 | #endif |
| 42 | |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 43 | namespace __sanitizer { |
| 44 | |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 45 | // ------------- sanitizer_common.h |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 46 | uptr GetMmapGranularity() { |
| 47 | return GetPageSize(); |
| 48 | } |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 49 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 50 | #if SANITIZER_WORDSIZE == 32 |
| 51 | // Take care of unusable kernel area in top gigabyte. |
| 52 | static uptr GetKernelAreaSize() { |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 53 | #if SANITIZER_LINUX && !SANITIZER_X32 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 54 | const uptr gbyte = 1UL << 30; |
| 55 | |
| 56 | // Firstly check if there are writable segments |
| 57 | // mapped to top gigabyte (e.g. stack). |
| 58 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
| 59 | uptr end, prot; |
| 60 | while (proc_maps.Next(/*start*/0, &end, |
| 61 | /*offset*/0, /*filename*/0, |
| 62 | /*filename_size*/0, &prot)) { |
| 63 | if ((end >= 3 * gbyte) |
| 64 | && (prot & MemoryMappingLayout::kProtectionWrite) != 0) |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | #if !SANITIZER_ANDROID |
| 69 | // Even if nothing is mapped, top Gb may still be accessible |
| 70 | // if we are running on 64-bit kernel. |
| 71 | // Uname may report misleading results if personality type |
| 72 | // is modified (e.g. under schroot) so check this as well. |
| 73 | struct utsname uname_info; |
| 74 | int pers = personality(0xffffffffUL); |
| 75 | if (!(pers & PER_MASK) |
| 76 | && uname(&uname_info) == 0 |
| 77 | && internal_strstr(uname_info.machine, "64")) |
| 78 | return 0; |
| 79 | #endif // SANITIZER_ANDROID |
| 80 | |
| 81 | // Top gigabyte is reserved for kernel. |
| 82 | return gbyte; |
| 83 | #else |
| 84 | return 0; |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 85 | #endif // SANITIZER_LINUX && !SANITIZER_X32 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 86 | } |
| 87 | #endif // SANITIZER_WORDSIZE == 32 |
| 88 | |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 89 | uptr GetMaxVirtualAddress() { |
| 90 | #if SANITIZER_WORDSIZE == 64 |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 91 | # if defined(__powerpc64__) || defined(__aarch64__) |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 92 | // 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] | 93 | // 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] | 94 | // one of 0x00000fffffffffffUL and 0x00003fffffffffffUL. |
| 95 | // Note that with 'ulimit -s unlimited' the stack is moved away from the top |
| 96 | // 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] | 97 | // This should (does) work for both PowerPC64 Endian modes. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 98 | // Similarly, aarch64 has multiple address space layouts: 39, 42 and 47-bit. |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 99 | return (1ULL << (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1)) - 1; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 100 | # elif defined(__mips64) |
| 101 | return (1ULL << 40) - 1; // 0x000000ffffffffffUL; |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 102 | # else |
| 103 | return (1ULL << 47) - 1; // 0x00007fffffffffffUL; |
| 104 | # endif |
| 105 | #else // SANITIZER_WORDSIZE == 32 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 106 | uptr res = (1ULL << 32) - 1; // 0xffffffff; |
| 107 | if (!common_flags()->full_address_space) |
| 108 | res -= GetKernelAreaSize(); |
| 109 | CHECK_LT(reinterpret_cast<uptr>(&res), res); |
| 110 | return res; |
Timur Iskhodzhanov | bb7f2d8 | 2013-07-16 09:47:39 +0000 | [diff] [blame] | 111 | #endif // SANITIZER_WORDSIZE |
| 112 | } |
| 113 | |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 114 | void *MmapOrDie(uptr size, const char *mem_type) { |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 115 | size = RoundUpTo(size, GetPageSizeCached()); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 116 | uptr res = internal_mmap(0, size, |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 117 | PROT_READ | PROT_WRITE, |
| 118 | MAP_PRIVATE | MAP_ANON, -1, 0); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 119 | int reserrno; |
| 120 | if (internal_iserror(res, &reserrno)) { |
Kostya Serebryany | e8216fd | 2012-10-04 07:21:09 +0000 | [diff] [blame] | 121 | static int recursion_count; |
| 122 | if (recursion_count) { |
| 123 | // The Report() and CHECK calls below may call mmap recursively and fail. |
| 124 | // If we went into recursion, just die. |
Dmitry Vyukov | c8490e2 | 2013-01-29 09:39:58 +0000 | [diff] [blame] | 125 | RawWrite("ERROR: Failed to mmap\n"); |
Kostya Serebryany | e8216fd | 2012-10-04 07:21:09 +0000 | [diff] [blame] | 126 | Die(); |
| 127 | } |
| 128 | recursion_count++; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 129 | Report("ERROR: %s failed to " |
| 130 | "allocate 0x%zx (%zd) bytes of %s (errno: %d)\n", |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 131 | SanitizerToolName, size, size, mem_type, reserrno); |
Kostya Serebryany | 0aa04b3 | 2012-08-14 15:18:40 +0000 | [diff] [blame] | 132 | DumpProcessMap(); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 133 | CHECK("unable to mmap" && 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 | IncreaseTotalMmap(size); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 136 | return (void *)res; |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void UnmapOrDie(void *addr, uptr size) { |
| 140 | if (!addr || !size) return; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 141 | uptr res = internal_munmap(addr, size); |
| 142 | if (internal_iserror(res)) { |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 143 | Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n", |
| 144 | SanitizerToolName, size, size, addr); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 145 | CHECK("unable to unmap" && 0); |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 146 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 147 | DecreaseTotalMmap(size); |
| 148 | } |
| 149 | |
| 150 | void *MmapNoReserveOrDie(uptr size, const char *mem_type) { |
| 151 | uptr PageSize = GetPageSizeCached(); |
| 152 | uptr p = internal_mmap(0, |
| 153 | RoundUpTo(size, PageSize), |
| 154 | PROT_READ | PROT_WRITE, |
| 155 | MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
| 156 | -1, 0); |
| 157 | int reserrno; |
| 158 | if (internal_iserror(p, &reserrno)) { |
| 159 | Report("ERROR: %s failed to " |
| 160 | "allocate noreserve 0x%zx (%zd) bytes for '%s' (errno: %d)\n", |
| 161 | SanitizerToolName, size, size, mem_type, reserrno); |
| 162 | CHECK("unable to mmap" && 0); |
| 163 | } |
| 164 | IncreaseTotalMmap(size); |
| 165 | return (void *)p; |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 168 | void *MmapFixedOrDie(uptr fixed_addr, uptr size) { |
| 169 | uptr PageSize = GetPageSizeCached(); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 170 | uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 171 | RoundUpTo(size, PageSize), |
| 172 | PROT_READ | PROT_WRITE, |
| 173 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 174 | -1, 0); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 175 | int reserrno; |
| 176 | if (internal_iserror(p, &reserrno)) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 177 | Report("ERROR: %s failed to " |
| 178 | "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n", |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 179 | SanitizerToolName, size, size, fixed_addr, reserrno); |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 180 | CHECK("unable to mmap" && 0); |
| 181 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 182 | IncreaseTotalMmap(size); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 183 | return (void *)p; |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 186 | bool MprotectNoAccess(uptr addr, uptr size) { |
| 187 | return 0 == internal_mprotect((void*)addr, size, PROT_NONE); |
| 188 | } |
| 189 | |
| 190 | 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] | 191 | int flags; |
| 192 | switch (mode) { |
| 193 | case RdOnly: flags = O_RDONLY; break; |
| 194 | case WrOnly: flags = O_WRONLY | O_CREAT; break; |
| 195 | case RdWr: flags = O_RDWR | O_CREAT; break; |
| 196 | } |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 197 | fd_t res = internal_open(filename, flags, 0660); |
| 198 | if (internal_iserror(res, errno_p)) |
| 199 | return kInvalidFd; |
| 200 | return res; |
| 201 | } |
| 202 | |
| 203 | void CloseFile(fd_t fd) { |
| 204 | internal_close(fd); |
| 205 | } |
| 206 | |
| 207 | bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read, |
| 208 | error_t *error_p) { |
| 209 | uptr res = internal_read(fd, buff, buff_size); |
| 210 | if (internal_iserror(res, error_p)) |
| 211 | return false; |
| 212 | if (bytes_read) |
| 213 | *bytes_read = res; |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written, |
| 218 | error_t *error_p) { |
| 219 | uptr res = internal_write(fd, buff, buff_size); |
| 220 | if (internal_iserror(res, error_p)) |
| 221 | return false; |
| 222 | if (bytes_written) |
| 223 | *bytes_written = res; |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | bool RenameFile(const char *oldpath, const char *newpath, error_t *error_p) { |
| 228 | uptr res = internal_rename(oldpath, newpath); |
| 229 | return !internal_iserror(res, error_p); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 232 | void *MapFileToMemory(const char *file_name, uptr *buff_size) { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 233 | fd_t fd = OpenFile(file_name, RdOnly); |
| 234 | CHECK(fd != kInvalidFd); |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 235 | uptr fsize = internal_filesize(fd); |
| 236 | CHECK_NE(fsize, (uptr)-1); |
| 237 | CHECK_GT(fsize, 0); |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 238 | *buff_size = RoundUpTo(fsize, GetPageSizeCached()); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 239 | uptr map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 240 | return internal_iserror(map) ? 0 : (void *)map; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 243 | void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, uptr offset) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 244 | uptr flags = MAP_SHARED; |
| 245 | if (addr) flags |= MAP_FIXED; |
| 246 | 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] | 247 | int mmap_errno = 0; |
| 248 | if (internal_iserror(p, &mmap_errno)) { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 249 | Printf("could not map writable file (%d, %zu, %zu): %zd, errno: %d\n", |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 250 | fd, offset, size, p, mmap_errno); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | return (void *)p; |
| 254 | } |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 255 | |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 256 | static inline bool IntervalsAreSeparate(uptr start1, uptr end1, |
| 257 | uptr start2, uptr end2) { |
| 258 | CHECK(start1 <= end1); |
| 259 | CHECK(start2 <= end2); |
| 260 | return (end1 < start2) || (end2 < start1); |
| 261 | } |
| 262 | |
| 263 | // FIXME: this is thread-unsafe, but should not cause problems most of the time. |
| 264 | // When the shadow is mapped only a single thread usually exists (plus maybe |
| 265 | // several worker threads on Mac, which aren't expected to map big chunks of |
| 266 | // memory). |
| 267 | bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 268 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 269 | uptr start, end; |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 270 | while (proc_maps.Next(&start, &end, |
| 271 | /*offset*/0, /*filename*/0, /*filename_size*/0, |
| 272 | /*protection*/0)) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 273 | CHECK_NE(0, end); |
| 274 | if (!IntervalsAreSeparate(start, end - 1, range_start, range_end)) |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | return true; |
| 278 | } |
| 279 | |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 280 | void DumpProcessMap() { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 281 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 282 | uptr start, end; |
| 283 | const sptr kBufSize = 4095; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 284 | char *filename = (char*)MmapOrDie(kBufSize, __func__); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 285 | Report("Process memory map follows:\n"); |
| 286 | while (proc_maps.Next(&start, &end, /* file_offset */0, |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 287 | filename, kBufSize, /* protection */0)) { |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 288 | Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename); |
| 289 | } |
| 290 | Report("End of process memory map.\n"); |
Alexey Samsonov | b84ee02 | 2012-06-15 07:41:23 +0000 | [diff] [blame] | 291 | UnmapOrDie(filename, kBufSize); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 294 | const char *GetPwd() { |
| 295 | return GetEnv("PWD"); |
| 296 | } |
| 297 | |
Alexey Samsonov | 1dcd1d9 | 2013-09-03 13:20:48 +0000 | [diff] [blame] | 298 | char *FindPathToBinary(const char *name) { |
| 299 | const char *path = GetEnv("PATH"); |
| 300 | if (!path) |
| 301 | return 0; |
| 302 | uptr name_len = internal_strlen(name); |
| 303 | InternalScopedBuffer<char> buffer(kMaxPathLength); |
| 304 | const char *beg = path; |
| 305 | while (true) { |
| 306 | const char *end = internal_strchrnul(beg, ':'); |
| 307 | uptr prefix_len = end - beg; |
| 308 | if (prefix_len + name_len + 2 <= kMaxPathLength) { |
| 309 | internal_memcpy(buffer.data(), beg, prefix_len); |
| 310 | buffer[prefix_len] = '/'; |
| 311 | internal_memcpy(&buffer[prefix_len + 1], name, name_len); |
| 312 | buffer[prefix_len + 1 + name_len] = '\0'; |
| 313 | if (FileExists(buffer.data())) |
| 314 | return internal_strdup(buffer.data()); |
| 315 | } |
| 316 | if (*end == '\0') break; |
| 317 | beg = end + 1; |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 322 | bool IsPathSeparator(const char c) { |
| 323 | return c == '/'; |
| 324 | } |
| 325 | |
| 326 | bool IsAbsolutePath(const char *path) { |
| 327 | return path != nullptr && IsPathSeparator(path[0]); |
| 328 | } |
| 329 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 330 | void ReportFile::Write(const char *buffer, uptr length) { |
| 331 | SpinMutexLock l(mu); |
| 332 | static const char *kWriteError = |
| 333 | "ReportFile::Write() can't output requested buffer!\n"; |
| 334 | ReopenIfNecessary(); |
| 335 | if (length != internal_write(fd, buffer, length)) { |
| 336 | internal_write(fd, kWriteError, internal_strlen(kWriteError)); |
Reid Kleckner | 923bac7 | 2013-09-05 03:19:57 +0000 | [diff] [blame] | 337 | Die(); |
| 338 | } |
| 339 | } |
| 340 | |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 341 | bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) { |
| 342 | uptr s, e, off, prot; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 343 | InternalScopedString buff(kMaxPathLength); |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 344 | MemoryMappingLayout proc_maps(/*cache_enabled*/false); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 345 | while (proc_maps.Next(&s, &e, &off, buff.data(), buff.size(), &prot)) { |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 346 | if ((prot & MemoryMappingLayout::kProtectionExecute) != 0 |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 347 | && internal_strcmp(module, buff.data()) == 0) { |
Dmitry Vyukov | 821acfa | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 348 | *start = s; |
| 349 | *end = e; |
| 350 | return true; |
| 351 | } |
| 352 | } |
| 353 | return false; |
| 354 | } |
| 355 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 356 | SignalContext SignalContext::Create(void *siginfo, void *context) { |
| 357 | uptr addr = (uptr)((siginfo_t*)siginfo)->si_addr; |
| 358 | uptr pc, sp, bp; |
| 359 | GetPcSpBp(context, &pc, &sp, &bp); |
| 360 | return SignalContext(context, addr, pc, sp, bp); |
| 361 | } |
| 362 | |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 363 | } // namespace __sanitizer |
| 364 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 365 | #endif // SANITIZER_POSIX |