Alexey Samsonov | ae4d9ca | 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 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 15 | #include "sanitizer_platform.h" |
| 16 | #if SANITIZER_MAC |
| 17 | |
Alexander Potapenko | 2bf0d03 | 2013-02-06 14:41:15 +0000 | [diff] [blame] | 18 | // Use 64-bit inodes in file operations. ASan does not support OS X 10.5, so |
| 19 | // the clients will most certainly use 64-bit ones as well. |
| 20 | #ifndef _DARWIN_USE_64_BIT_INODE |
| 21 | #define _DARWIN_USE_64_BIT_INODE 1 |
| 22 | #endif |
| 23 | #include <stdio.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 24 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 25 | #include "sanitizer_common.h" |
Alexey Samsonov | 94b5036 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 26 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 27 | #include "sanitizer_libc.h" |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 28 | #include "sanitizer_procmaps.h" |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 29 | |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 30 | #include <crt_externs.h> // for _NSGetEnviron |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 32 | #include <mach-o/dyld.h> |
| 33 | #include <mach-o/loader.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 34 | #include <pthread.h> |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 35 | #include <sched.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 36 | #include <sys/mman.h> |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 37 | #include <sys/resource.h> |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 40 | #include <unistd.h> |
Dmitry Vyukov | 4852601 | 2013-01-14 08:01:58 +0000 | [diff] [blame] | 41 | #include <libkern/OSAtomic.h> |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 42 | |
| 43 | namespace __sanitizer { |
| 44 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 45 | #include "sanitizer_syscall_generic.inc" |
| 46 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 47 | // ---------------------- sanitizer_libc.h |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 48 | void *internal_mmap(void *addr, size_t length, int prot, int flags, |
| 49 | int fd, u64 offset) { |
| 50 | return mmap(addr, length, prot, flags, fd, offset); |
| 51 | } |
| 52 | |
Alexey Samsonov | 1f11d31 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 53 | int internal_munmap(void *addr, uptr length) { |
| 54 | return munmap(addr, length); |
| 55 | } |
| 56 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 57 | uptr internal_close(fd_t fd) { |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 58 | return close(fd); |
| 59 | } |
| 60 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 61 | uptr internal_open(const char *filename, int flags) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 62 | return open(filename, flags); |
| 63 | } |
| 64 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 65 | uptr internal_open(const char *filename, int flags, u32 mode) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 66 | return open(filename, flags, mode); |
| 67 | } |
| 68 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 69 | uptr OpenFile(const char *filename, bool write) { |
Alexey Samsonov | ee7cc44 | 2013-02-01 15:58:46 +0000 | [diff] [blame] | 70 | return internal_open(filename, |
| 71 | write ? O_WRONLY | O_CREAT : O_RDONLY, 0660); |
Alexey Samsonov | c5d4651 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Alexey Samsonov | a56aefd | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 74 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 75 | return read(fd, buf, count); |
| 76 | } |
| 77 | |
| 78 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 79 | return write(fd, buf, count); |
| 80 | } |
| 81 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 82 | uptr internal_stat(const char *path, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 83 | return stat(path, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 86 | uptr internal_lstat(const char *path, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 87 | return lstat(path, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 90 | uptr internal_fstat(fd_t fd, void *buf) { |
Alexey Samsonov | 7c50ee7 | 2013-02-04 10:31:39 +0000 | [diff] [blame] | 91 | return fstat(fd, (struct stat *)buf); |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 94 | uptr internal_filesize(fd_t fd) { |
Alexey Samsonov | 668accc | 2012-08-02 10:09:31 +0000 | [diff] [blame] | 95 | struct stat st; |
Alexey Samsonov | 4c9317a | 2013-02-04 10:16:50 +0000 | [diff] [blame] | 96 | if (internal_fstat(fd, &st)) |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 97 | return -1; |
| 98 | return (uptr)st.st_size; |
| 99 | } |
| 100 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 101 | uptr internal_dup2(int oldfd, int newfd) { |
Alexey Samsonov | 8e820fc | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 102 | return dup2(oldfd, newfd); |
| 103 | } |
| 104 | |
Alexey Samsonov | d1b8f58 | 2012-09-05 14:48:24 +0000 | [diff] [blame] | 105 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
| 106 | return readlink(path, buf, bufsize); |
| 107 | } |
| 108 | |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame^] | 109 | uptr internal_sched_yield() { |
Alexey Samsonov | 0969bcf | 2012-06-18 08:44:30 +0000 | [diff] [blame] | 110 | return sched_yield(); |
| 111 | } |
| 112 | |
Alexey Samsonov | f882247 | 2013-02-20 13:54:32 +0000 | [diff] [blame] | 113 | void internal__exit(int exitcode) { |
| 114 | _exit(exitcode); |
| 115 | } |
| 116 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 117 | // ----------------- sanitizer_common.h |
Alexey Samsonov | 93b4caf | 2012-11-09 14:45:30 +0000 | [diff] [blame] | 118 | bool FileExists(const char *filename) { |
| 119 | struct stat st; |
| 120 | if (stat(filename, &st)) |
| 121 | return false; |
| 122 | // Sanity check: filename is a regular file. |
| 123 | return S_ISREG(st.st_mode); |
| 124 | } |
| 125 | |
Dmitry Vyukov | e0023f7 | 2012-10-02 12:58:14 +0000 | [diff] [blame] | 126 | uptr GetTid() { |
| 127 | return reinterpret_cast<uptr>(pthread_self()); |
| 128 | } |
| 129 | |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 130 | void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 131 | uptr *stack_bottom) { |
| 132 | CHECK(stack_top); |
| 133 | CHECK(stack_bottom); |
| 134 | uptr stacksize = pthread_get_stacksize_np(pthread_self()); |
| 135 | void *stackaddr = pthread_get_stackaddr_np(pthread_self()); |
| 136 | *stack_top = (uptr)stackaddr; |
Alexey Samsonov | ed996f7 | 2012-06-07 07:32:00 +0000 | [diff] [blame] | 137 | *stack_bottom = *stack_top - stacksize; |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Alexey Samsonov | 3dbeabb | 2012-06-14 14:07:21 +0000 | [diff] [blame] | 140 | const char *GetEnv(const char *name) { |
| 141 | char ***env_ptr = _NSGetEnviron(); |
| 142 | CHECK(env_ptr); |
| 143 | char **environ = *env_ptr; |
| 144 | CHECK(environ); |
| 145 | uptr name_len = internal_strlen(name); |
| 146 | while (*environ != 0) { |
| 147 | uptr len = internal_strlen(*environ); |
| 148 | if (len > name_len) { |
| 149 | const char *p = *environ; |
| 150 | if (!internal_memcmp(p, name, name_len) && |
| 151 | p[name_len] == '=') { // Match. |
| 152 | return *environ + name_len + 1; // String starting after =. |
| 153 | } |
| 154 | } |
| 155 | environ++; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 159 | |
Alexey Samsonov | d7e5bb4 | 2012-09-17 09:12:39 +0000 | [diff] [blame] | 160 | void ReExec() { |
| 161 | UNIMPLEMENTED(); |
| 162 | } |
| 163 | |
Alexander Potapenko | 2574257 | 2012-12-10 13:10:40 +0000 | [diff] [blame] | 164 | void PrepareForSandboxing() { |
| 165 | // Nothing here for now. |
| 166 | } |
| 167 | |
Alexey Samsonov | e5931fd | 2012-06-07 07:13:46 +0000 | [diff] [blame] | 168 | // ----------------- sanitizer_procmaps.h |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 169 | |
Alexander Potapenko | 9ae2883 | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 170 | MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) { |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 171 | Reset(); |
| 172 | } |
| 173 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 174 | MemoryMappingLayout::~MemoryMappingLayout() { |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // More information about Mach-O headers can be found in mach-o/loader.h |
| 178 | // Each Mach-O image has a header (mach_header or mach_header_64) starting with |
| 179 | // a magic number, and a list of linker load commands directly following the |
| 180 | // header. |
| 181 | // A load command is at least two 32-bit words: the command type and the |
| 182 | // command size in bytes. We're interested only in segment load commands |
| 183 | // (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped |
| 184 | // into the task's address space. |
| 185 | // The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or |
| 186 | // segment_command_64 correspond to the memory address, memory size and the |
| 187 | // file offset of the current memory segment. |
| 188 | // Because these fields are taken from the images as is, one needs to add |
| 189 | // _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime. |
| 190 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 191 | void MemoryMappingLayout::Reset() { |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 192 | // Count down from the top. |
| 193 | // TODO(glider): as per man 3 dyld, iterating over the headers with |
| 194 | // _dyld_image_count is thread-unsafe. We need to register callbacks for |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 195 | // adding and removing images which will invalidate the MemoryMappingLayout |
| 196 | // state. |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 197 | current_image_ = _dyld_image_count(); |
| 198 | current_load_cmd_count_ = -1; |
| 199 | current_load_cmd_addr_ = 0; |
| 200 | current_magic_ = 0; |
Alexander Potapenko | 8bf8b79 | 2012-10-02 15:42:24 +0000 | [diff] [blame] | 201 | current_filetype_ = 0; |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Potapenko | 93da8b6 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 204 | // static |
| 205 | void MemoryMappingLayout::CacheMemoryMappings() { |
| 206 | // No-op on Mac for now. |
| 207 | } |
| 208 | |
| 209 | void MemoryMappingLayout::LoadFromCache() { |
| 210 | // No-op on Mac for now. |
| 211 | } |
| 212 | |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 213 | // Next and NextSegmentLoad were inspired by base/sysinfo.cc in |
| 214 | // Google Perftools, http://code.google.com/p/google-perftools. |
| 215 | |
| 216 | // NextSegmentLoad scans the current image for the next segment load command |
| 217 | // and returns the start and end addresses and file offset of the corresponding |
| 218 | // segment. |
| 219 | // Note that the segment addresses are not necessarily sorted. |
| 220 | template<u32 kLCSegment, typename SegmentCommand> |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 221 | bool MemoryMappingLayout::NextSegmentLoad( |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 222 | uptr *start, uptr *end, uptr *offset, |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 223 | char filename[], uptr filename_size, uptr *protection) { |
| 224 | if (protection) |
| 225 | UNIMPLEMENTED(); |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 226 | const char* lc = current_load_cmd_addr_; |
| 227 | current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize; |
| 228 | if (((const load_command *)lc)->cmd == kLCSegment) { |
| 229 | const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_); |
| 230 | const SegmentCommand* sc = (const SegmentCommand *)lc; |
| 231 | if (start) *start = sc->vmaddr + dlloff; |
| 232 | if (end) *end = sc->vmaddr + sc->vmsize + dlloff; |
Alexander Potapenko | 8bf8b79 | 2012-10-02 15:42:24 +0000 | [diff] [blame] | 233 | if (offset) { |
| 234 | if (current_filetype_ == /*MH_EXECUTE*/ 0x2) { |
| 235 | *offset = sc->vmaddr; |
| 236 | } else { |
| 237 | *offset = sc->fileoff; |
| 238 | } |
| 239 | } |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 240 | if (filename) { |
| 241 | internal_strncpy(filename, _dyld_get_image_name(current_image_), |
| 242 | filename_size); |
| 243 | } |
| 244 | return true; |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 249 | bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset, |
Alexey Samsonov | 4a1a495 | 2013-03-13 07:39:25 +0000 | [diff] [blame] | 250 | char filename[], uptr filename_size, |
| 251 | uptr *protection) { |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 252 | for (; current_image_ >= 0; current_image_--) { |
| 253 | const mach_header* hdr = _dyld_get_image_header(current_image_); |
| 254 | if (!hdr) continue; |
| 255 | if (current_load_cmd_count_ < 0) { |
| 256 | // Set up for this image; |
| 257 | current_load_cmd_count_ = hdr->ncmds; |
| 258 | current_magic_ = hdr->magic; |
Alexander Potapenko | 8bf8b79 | 2012-10-02 15:42:24 +0000 | [diff] [blame] | 259 | current_filetype_ = hdr->filetype; |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 260 | switch (current_magic_) { |
| 261 | #ifdef MH_MAGIC_64 |
| 262 | case MH_MAGIC_64: { |
| 263 | current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64); |
| 264 | break; |
| 265 | } |
| 266 | #endif |
| 267 | case MH_MAGIC: { |
| 268 | current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header); |
| 269 | break; |
| 270 | } |
| 271 | default: { |
| 272 | continue; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) { |
| 278 | switch (current_magic_) { |
| 279 | // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64. |
| 280 | #ifdef MH_MAGIC_64 |
| 281 | case MH_MAGIC_64: { |
| 282 | if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>( |
Alexey Samsonov | 4a1a495 | 2013-03-13 07:39:25 +0000 | [diff] [blame] | 283 | start, end, offset, filename, filename_size, protection)) |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 284 | return true; |
| 285 | break; |
| 286 | } |
| 287 | #endif |
| 288 | case MH_MAGIC: { |
| 289 | if (NextSegmentLoad<LC_SEGMENT, struct segment_command>( |
Alexey Samsonov | 4a1a495 | 2013-03-13 07:39:25 +0000 | [diff] [blame] | 290 | start, end, offset, filename, filename_size, protection)) |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 291 | return true; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | // If we get here, no more load_cmd's in this image talk about |
| 297 | // segments. Go on to the next image. |
| 298 | } |
| 299 | return false; |
| 300 | } |
| 301 | |
Alexey Samsonov | e1f5dac | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 302 | bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset, |
| 303 | char filename[], |
Alexey Samsonov | 4a1a495 | 2013-03-13 07:39:25 +0000 | [diff] [blame] | 304 | uptr filename_size, |
Alexey Samsonov | 45717c9 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 305 | uptr *protection) { |
| 306 | return IterateForObjectNameAndOffset(addr, offset, filename, filename_size, |
| 307 | protection); |
Alexey Samsonov | 6895adc | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 310 | BlockingMutex::BlockingMutex(LinkerInitialized) { |
| 311 | // We assume that OS_SPINLOCK_INIT is zero |
| 312 | } |
| 313 | |
Alexey Samsonov | 93af594 | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 314 | BlockingMutex::BlockingMutex() { |
| 315 | internal_memset(this, 0, sizeof(*this)); |
| 316 | } |
| 317 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 318 | void BlockingMutex::Lock() { |
| 319 | CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_)); |
Kostya Serebryany | f155fcc | 2013-02-26 12:59:06 +0000 | [diff] [blame] | 320 | CHECK_EQ(OS_SPINLOCK_INIT, 0); |
| 321 | CHECK_NE(owner_, (uptr)pthread_self()); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 322 | OSSpinLockLock((OSSpinLock*)&opaque_storage_); |
| 323 | CHECK(!owner_); |
| 324 | owner_ = (uptr)pthread_self(); |
| 325 | } |
| 326 | |
| 327 | void BlockingMutex::Unlock() { |
| 328 | CHECK(owner_ == (uptr)pthread_self()); |
| 329 | owner_ = 0; |
| 330 | OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); |
| 331 | } |
| 332 | |
Alexey Samsonov | ce70097 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 333 | void BlockingMutex::CheckLocked() { |
| 334 | CHECK_EQ((uptr)pthread_self(), owner_); |
| 335 | } |
| 336 | |
Evgeniy Stepanov | b114ed8 | 2013-03-13 08:19:53 +0000 | [diff] [blame] | 337 | uptr GetTlsSize() { |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | void InitTlsSize() { |
| 342 | } |
| 343 | |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 344 | void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, |
| 345 | uptr *tls_addr, uptr *tls_size) { |
| 346 | uptr stack_top, stack_bottom; |
| 347 | GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom); |
| 348 | *stk_addr = stack_bottom; |
| 349 | *stk_size = stack_top - stack_bottom; |
| 350 | *tls_addr = 0; |
| 351 | *tls_size = 0; |
| 352 | } |
| 353 | |
Alexey Samsonov | ae4d9ca | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 354 | } // namespace __sanitizer |
| 355 | |
Alexey Samsonov | 46f9395 | 2013-04-03 07:24:35 +0000 | [diff] [blame] | 356 | #endif // SANITIZER_MAC |