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 | |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 32 | void *MmapOrDie(uptr size, const char *mem_type) { |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 33 | size = RoundUpTo(size, GetPageSizeCached()); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 34 | uptr res = internal_mmap(0, size, |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 35 | PROT_READ | PROT_WRITE, |
| 36 | MAP_PRIVATE | MAP_ANON, -1, 0); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 37 | int reserrno; |
| 38 | if (internal_iserror(res, &reserrno)) { |
Kostya Serebryany | e8216fd | 2012-10-04 07:21:09 +0000 | [diff] [blame] | 39 | static int recursion_count; |
| 40 | if (recursion_count) { |
| 41 | // The Report() and CHECK calls below may call mmap recursively and fail. |
| 42 | // If we went into recursion, just die. |
Dmitry Vyukov | c8490e2 | 2013-01-29 09:39:58 +0000 | [diff] [blame] | 43 | RawWrite("ERROR: Failed to mmap\n"); |
Kostya Serebryany | e8216fd | 2012-10-04 07:21:09 +0000 | [diff] [blame] | 44 | Die(); |
| 45 | } |
| 46 | recursion_count++; |
Alexander Potapenko | a8d37a0 | 2013-03-11 10:21:28 +0000 | [diff] [blame] | 47 | 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] | 48 | SanitizerToolName, size, size, mem_type, reserrno); |
Kostya Serebryany | 0aa04b3 | 2012-08-14 15:18:40 +0000 | [diff] [blame] | 49 | DumpProcessMap(); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 50 | CHECK("unable to mmap" && 0); |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 51 | } |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 52 | return (void *)res; |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void UnmapOrDie(void *addr, uptr size) { |
| 56 | if (!addr || !size) return; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 57 | uptr res = internal_munmap(addr, size); |
| 58 | if (internal_iserror(res)) { |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 59 | Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n", |
| 60 | SanitizerToolName, size, size, addr); |
Alexey Samsonov | a25b346 | 2012-06-06 16:15:07 +0000 | [diff] [blame] | 61 | CHECK("unable to unmap" && 0); |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 65 | void *MmapFixedNoReserve(uptr fixed_addr, uptr size) { |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 66 | uptr PageSize = GetPageSizeCached(); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 67 | uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 68 | RoundUpTo(size, PageSize), |
Dmitry Vyukov | 6b641c5 | 2012-11-06 16:48:46 +0000 | [diff] [blame] | 69 | PROT_READ | PROT_WRITE, |
| 70 | MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, |
| 71 | -1, 0); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 72 | int reserrno; |
| 73 | if (internal_iserror(p, &reserrno)) |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 74 | Report("ERROR: " |
| 75 | "%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] | 76 | SanitizerToolName, size, size, fixed_addr, reserrno); |
| 77 | return (void *)p; |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 80 | void *MmapFixedOrDie(uptr fixed_addr, uptr size) { |
| 81 | uptr PageSize = GetPageSizeCached(); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 82 | uptr p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)), |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 83 | RoundUpTo(size, PageSize), |
| 84 | PROT_READ | PROT_WRITE, |
| 85 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 86 | -1, 0); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 87 | int reserrno; |
| 88 | if (internal_iserror(p, &reserrno)) { |
Kostya Serebryany | 859778a | 2013-01-31 14:11:21 +0000 | [diff] [blame] | 89 | Report("ERROR:" |
| 90 | " %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] | 91 | SanitizerToolName, size, size, fixed_addr, reserrno); |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 92 | CHECK("unable to mmap" && 0); |
| 93 | } |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 94 | return (void *)p; |
Kostya Serebryany | 9bfe78f | 2012-12-13 05:36:00 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 97 | void *Mprotect(uptr fixed_addr, uptr size) { |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 98 | return (void *)internal_mmap((void*)fixed_addr, size, |
| 99 | PROT_NONE, |
| 100 | MAP_PRIVATE | MAP_ANON | MAP_FIXED | |
| 101 | MAP_NORESERVE, -1, 0); |
Alexey Samsonov | f607fc1 | 2012-06-14 14:42:58 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 104 | void *MapFileToMemory(const char *file_name, uptr *buff_size) { |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 105 | uptr openrv = OpenFile(file_name, false); |
| 106 | CHECK(!internal_iserror(openrv)); |
| 107 | fd_t fd = openrv; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 108 | uptr fsize = internal_filesize(fd); |
| 109 | CHECK_NE(fsize, (uptr)-1); |
| 110 | CHECK_GT(fsize, 0); |
Kostya Serebryany | f67ec2b | 2012-11-23 15:38:49 +0000 | [diff] [blame] | 111 | *buff_size = RoundUpTo(fsize, GetPageSizeCached()); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 112 | uptr map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 113 | return internal_iserror(map) ? 0 : (void *)map; |
Alexey Samsonov | a68633f | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 117 | static inline bool IntervalsAreSeparate(uptr start1, uptr end1, |
| 118 | uptr start2, uptr end2) { |
| 119 | CHECK(start1 <= end1); |
| 120 | CHECK(start2 <= end2); |
| 121 | return (end1 < start2) || (end2 < start1); |
| 122 | } |
| 123 | |
| 124 | // FIXME: this is thread-unsafe, but should not cause problems most of the time. |
| 125 | // When the shadow is mapped only a single thread usually exists (plus maybe |
| 126 | // several worker threads on Mac, which aren't expected to map big chunks of |
| 127 | // memory). |
| 128 | bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 129 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 130 | uptr start, end; |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 131 | while (proc_maps.Next(&start, &end, |
| 132 | /*offset*/0, /*filename*/0, /*filename_size*/0, |
| 133 | /*protection*/0)) { |
Alexey Samsonov | dd3a911 | 2012-06-15 07:29:14 +0000 | [diff] [blame] | 134 | if (!IntervalsAreSeparate(start, end, range_start, range_end)) |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 140 | void DumpProcessMap() { |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 141 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 142 | uptr start, end; |
| 143 | const sptr kBufSize = 4095; |
Alexey Samsonov | b84ee02 | 2012-06-15 07:41:23 +0000 | [diff] [blame] | 144 | char *filename = (char*)MmapOrDie(kBufSize, __FUNCTION__); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 145 | Report("Process memory map follows:\n"); |
| 146 | while (proc_maps.Next(&start, &end, /* file_offset */0, |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 147 | filename, kBufSize, /* protection */0)) { |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 148 | Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename); |
| 149 | } |
| 150 | Report("End of process memory map.\n"); |
Alexey Samsonov | b84ee02 | 2012-06-15 07:41:23 +0000 | [diff] [blame] | 151 | UnmapOrDie(filename, kBufSize); |
Alexey Samsonov | be7420c | 2012-06-15 06:08:19 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 154 | const char *GetPwd() { |
| 155 | return GetEnv("PWD"); |
| 156 | } |
| 157 | |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 158 | } // namespace __sanitizer |
| 159 | |
Sergey Matveev | 736cf49 | 2013-05-08 12:45:55 +0000 | [diff] [blame] | 160 | #endif // SANITIZER_LINUX || SANITIZER_MAC |