Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 1 | //===-- sanitizer_linux.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 linux-specific functions from |
| 12 | // sanitizer_libc.h. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifdef __linux__ |
| 15 | |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame^] | 16 | #include "sanitizer_common.h" |
Alexey Samsonov | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 17 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 18 | #include "sanitizer_libc.h" |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame^] | 19 | #include "sanitizer_procmaps.h" |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 20 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 21 | #include <fcntl.h> |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 22 | #include <sys/mman.h> |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 23 | #include <sys/stat.h> |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 24 | #include <sys/syscall.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | namespace __sanitizer { |
| 29 | |
| 30 | void *internal_mmap(void *addr, uptr length, int prot, int flags, |
| 31 | int fd, u64 offset) { |
| 32 | #if __WORDSIZE == 64 |
| 33 | return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
| 34 | #else |
| 35 | return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); |
| 36 | #endif |
| 37 | } |
| 38 | |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 39 | int internal_munmap(void *addr, uptr length) { |
| 40 | return syscall(__NR_munmap, addr, length); |
| 41 | } |
| 42 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 43 | int internal_close(fd_t fd) { |
| 44 | return syscall(__NR_close, fd); |
| 45 | } |
| 46 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 47 | fd_t internal_open(const char *filename, bool write) { |
| 48 | return syscall(__NR_open, filename, |
Kostya Serebryany | 64166ca | 2012-06-06 14:11:31 +0000 | [diff] [blame] | 49 | write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660); |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 52 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 53 | return (uptr)syscall(__NR_read, fd, buf, count); |
| 54 | } |
| 55 | |
| 56 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 57 | return (uptr)syscall(__NR_write, fd, buf, count); |
| 58 | } |
| 59 | |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 60 | uptr internal_filesize(fd_t fd) { |
| 61 | struct stat st = {}; |
| 62 | if (syscall(__NR_fstat, fd, &st)) |
| 63 | return -1; |
| 64 | return (uptr)st.st_size; |
| 65 | } |
| 66 | |
| 67 | int internal_dup2(int oldfd, int newfd) { |
| 68 | return syscall(__NR_dup2, oldfd, newfd); |
| 69 | } |
| 70 | |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame^] | 71 | // ----------------- ProcessMaps implementation. |
| 72 | ProcessMaps::ProcessMaps() { |
| 73 | proc_self_maps_buff_len_ = |
| 74 | ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_, |
| 75 | &proc_self_maps_buff_mmaped_size_, 1 << 26); |
| 76 | CHECK(proc_self_maps_buff_len_ > 0); |
| 77 | // internal_write(2, proc_self_maps_buff_, proc_self_maps_buff_len_); |
| 78 | Reset(); |
| 79 | } |
| 80 | |
| 81 | ProcessMaps::~ProcessMaps() { |
| 82 | UnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_); |
| 83 | } |
| 84 | |
| 85 | void ProcessMaps::Reset() { |
| 86 | current_ = proc_self_maps_buff_; |
| 87 | } |
| 88 | |
| 89 | bool ProcessMaps::Next(uptr *start, uptr *end, uptr *offset, |
| 90 | char filename[], uptr filename_size) { |
| 91 | char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_; |
| 92 | if (current_ >= last) return false; |
| 93 | int consumed = 0; |
| 94 | char flags[10]; |
| 95 | int major, minor; |
| 96 | uptr inode; |
| 97 | uptr dummy; |
| 98 | if (!start) start = &dummy; |
| 99 | if (!end) end = &dummy; |
| 100 | if (!offset) offset = &dummy; |
| 101 | char *next_line = (char*)internal_memchr(current_, '\n', last - current_); |
| 102 | if (next_line == 0) |
| 103 | next_line = last; |
| 104 | if (internal_sscanf(current_, "%lx-%lx %4s %lx %x:%x %ld %n", |
| 105 | start, end, flags, offset, &major, &minor, |
| 106 | &inode, &consumed) != 7) |
| 107 | return false; |
| 108 | current_ += consumed; |
| 109 | // Skip spaces. |
| 110 | while (current_ < next_line && *current_ == ' ') |
| 111 | current_++; |
| 112 | // Fill in the filename. |
| 113 | uptr i = 0; |
| 114 | while (current_ < next_line) { |
| 115 | if (filename && i < filename_size - 1) |
| 116 | filename[i++] = *current_; |
| 117 | current_++; |
| 118 | } |
| 119 | if (filename && i < filename_size) |
| 120 | filename[i] = 0; |
| 121 | current_ = next_line + 1; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // Gets the object name and the offset by walking ProcessMaps. |
| 126 | bool ProcessMaps::GetObjectNameAndOffset(uptr addr, uptr *offset, |
| 127 | char filename[], |
| 128 | uptr filename_size) { |
| 129 | return IterateForObjectNameAndOffset(addr, offset, filename, filename_size); |
| 130 | } |
| 131 | |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 132 | } // namespace __sanitizer |
| 133 | |
| 134 | #endif // __linux__ |