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