| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 1 | //===-- sanitizer_mac.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 mac-specific functions from |
| 12 | // sanitizer_libc.h. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifdef __APPLE__ |
| 16 | |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 17 | #include "sanitizer_common.h" |
| Alexey Samsonov | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 18 | #include "sanitizer_internal_defs.h" |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 19 | #include "sanitizer_libc.h" |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 20 | #include "sanitizer_procmaps.h" |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 21 | |
| Alexey Samsonov | 0c53a38 | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 22 | #include <crt_externs.h> // for _NSGetEnviron |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 24 | #include <mach-o/dyld.h> |
| 25 | #include <mach-o/loader.h> |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 26 | #include <pthread.h> |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 27 | #include <sys/mman.h> |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 28 | #include <sys/resource.h> |
| Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 32 | |
| 33 | namespace __sanitizer { |
| 34 | |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 35 | // ---------------------- sanitizer_libc.h |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 36 | void *internal_mmap(void *addr, size_t length, int prot, int flags, |
| 37 | int fd, u64 offset) { |
| 38 | return mmap(addr, length, prot, flags, fd, offset); |
| 39 | } |
| 40 | |
| Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 41 | int internal_munmap(void *addr, uptr length) { |
| 42 | return munmap(addr, length); |
| 43 | } |
| 44 | |
| Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 45 | int internal_close(fd_t fd) { |
| 46 | return close(fd); |
| 47 | } |
| 48 | |
| Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 49 | fd_t internal_open(const char *filename, bool write) { |
| 50 | return open(filename, |
| Alexey Samsonov | 3768b58 | 2012-06-05 14:07:11 +0000 | [diff] [blame] | 51 | write ? O_WRONLY | O_CREAT : O_RDONLY, 0660); |
| Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 54 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 55 | return read(fd, buf, count); |
| 56 | } |
| 57 | |
| 58 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 59 | return write(fd, buf, count); |
| 60 | } |
| 61 | |
| Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 62 | uptr internal_filesize(fd_t fd) { |
| 63 | struct stat st = {}; |
| 64 | if (fstat(fd, &st)) |
| 65 | return -1; |
| 66 | return (uptr)st.st_size; |
| 67 | } |
| 68 | |
| 69 | int internal_dup2(int oldfd, int newfd) { |
| 70 | return dup2(oldfd, newfd); |
| 71 | } |
| 72 | |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 73 | // ----------------- sanitizer_common.h |
| Alexey Samsonov | cf4d3a0 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 74 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 75 | uptr *stack_bottom) { |
| 76 | CHECK(stack_top); |
| 77 | CHECK(stack_bottom); |
| 78 | uptr stacksize = pthread_get_stacksize_np(pthread_self()); |
| 79 | void *stackaddr = pthread_get_stackaddr_np(pthread_self()); |
| 80 | *stack_top = (uptr)stackaddr; |
| Alexey Samsonov | cf4d3a0 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 81 | *stack_bottom = *stack_top - stacksize; |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| Alexey Samsonov | 0c53a38 | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 84 | const char *GetEnv(const char *name) { |
| 85 | char ***env_ptr = _NSGetEnviron(); |
| 86 | CHECK(env_ptr); |
| 87 | char **environ = *env_ptr; |
| 88 | CHECK(environ); |
| 89 | uptr name_len = internal_strlen(name); |
| 90 | while (*environ != 0) { |
| 91 | uptr len = internal_strlen(*environ); |
| 92 | if (len > name_len) { |
| 93 | const char *p = *environ; |
| 94 | if (!internal_memcmp(p, name, name_len) && |
| 95 | p[name_len] == '=') { // Match. |
| 96 | return *environ + name_len + 1; // String starting after =. |
| 97 | } |
| 98 | } |
| 99 | environ++; |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| Alexey Samsonov | 4b1f103 | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 103 | |
| 104 | // ----------------- sanitizer_procmaps.h |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 105 | |
| 106 | ProcessMaps::ProcessMaps() { |
| 107 | Reset(); |
| 108 | } |
| 109 | |
| 110 | ProcessMaps::~ProcessMaps() { |
| 111 | } |
| 112 | |
| 113 | // More information about Mach-O headers can be found in mach-o/loader.h |
| 114 | // Each Mach-O image has a header (mach_header or mach_header_64) starting with |
| 115 | // a magic number, and a list of linker load commands directly following the |
| 116 | // header. |
| 117 | // A load command is at least two 32-bit words: the command type and the |
| 118 | // command size in bytes. We're interested only in segment load commands |
| 119 | // (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped |
| 120 | // into the task's address space. |
| 121 | // The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or |
| 122 | // segment_command_64 correspond to the memory address, memory size and the |
| 123 | // file offset of the current memory segment. |
| 124 | // Because these fields are taken from the images as is, one needs to add |
| 125 | // _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime. |
| 126 | |
| 127 | void ProcessMaps::Reset() { |
| 128 | // Count down from the top. |
| 129 | // TODO(glider): as per man 3 dyld, iterating over the headers with |
| 130 | // _dyld_image_count is thread-unsafe. We need to register callbacks for |
| 131 | // adding and removing images which will invalidate the ProcessMaps state. |
| 132 | current_image_ = _dyld_image_count(); |
| 133 | current_load_cmd_count_ = -1; |
| 134 | current_load_cmd_addr_ = 0; |
| 135 | current_magic_ = 0; |
| 136 | } |
| 137 | |
| 138 | // Next and NextSegmentLoad were inspired by base/sysinfo.cc in |
| 139 | // Google Perftools, http://code.google.com/p/google-perftools. |
| 140 | |
| 141 | // NextSegmentLoad scans the current image for the next segment load command |
| 142 | // and returns the start and end addresses and file offset of the corresponding |
| 143 | // segment. |
| 144 | // Note that the segment addresses are not necessarily sorted. |
| 145 | template<u32 kLCSegment, typename SegmentCommand> |
| 146 | bool ProcessMaps::NextSegmentLoad( |
| 147 | uptr *start, uptr *end, uptr *offset, |
| 148 | char filename[], uptr filename_size) { |
| 149 | const char* lc = current_load_cmd_addr_; |
| 150 | current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize; |
| 151 | if (((const load_command *)lc)->cmd == kLCSegment) { |
| 152 | const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_); |
| 153 | const SegmentCommand* sc = (const SegmentCommand *)lc; |
| 154 | if (start) *start = sc->vmaddr + dlloff; |
| 155 | if (end) *end = sc->vmaddr + sc->vmsize + dlloff; |
| 156 | if (offset) *offset = sc->fileoff; |
| 157 | if (filename) { |
| 158 | internal_strncpy(filename, _dyld_get_image_name(current_image_), |
| 159 | filename_size); |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool ProcessMaps::Next(uptr *start, uptr *end, uptr *offset, |
| 167 | char filename[], uptr filename_size) { |
| 168 | for (; current_image_ >= 0; current_image_--) { |
| 169 | const mach_header* hdr = _dyld_get_image_header(current_image_); |
| 170 | if (!hdr) continue; |
| 171 | if (current_load_cmd_count_ < 0) { |
| 172 | // Set up for this image; |
| 173 | current_load_cmd_count_ = hdr->ncmds; |
| 174 | current_magic_ = hdr->magic; |
| 175 | switch (current_magic_) { |
| 176 | #ifdef MH_MAGIC_64 |
| 177 | case MH_MAGIC_64: { |
| 178 | current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64); |
| 179 | break; |
| 180 | } |
| 181 | #endif |
| 182 | case MH_MAGIC: { |
| 183 | current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header); |
| 184 | break; |
| 185 | } |
| 186 | default: { |
| 187 | continue; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) { |
| 193 | switch (current_magic_) { |
| 194 | // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64. |
| 195 | #ifdef MH_MAGIC_64 |
| 196 | case MH_MAGIC_64: { |
| 197 | if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>( |
| 198 | start, end, offset, filename, filename_size)) |
| 199 | return true; |
| 200 | break; |
| 201 | } |
| 202 | #endif |
| 203 | case MH_MAGIC: { |
| 204 | if (NextSegmentLoad<LC_SEGMENT, struct segment_command>( |
| 205 | start, end, offset, filename, filename_size)) |
| 206 | return true; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | // If we get here, no more load_cmd's in this image talk about |
| 212 | // segments. Go on to the next image. |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | bool ProcessMaps::GetObjectNameAndOffset(uptr addr, uptr *offset, |
| 218 | char filename[], |
| 219 | uptr filename_size) { |
| 220 | return IterateForObjectNameAndOffset(addr, offset, filename, filename_size); |
| 221 | } |
| 222 | |
| Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 223 | } // namespace __sanitizer |
| 224 | |
| 225 | #endif // __APPLE__ |