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