blob: 9d74a48f5356c6a214d0466764f0031fa2a96278 [file] [log] [blame]
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +00001//===-- 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 Stepanov0af67232013-03-19 14:33:38 +000015#include "sanitizer_platform.h"
16#if SANITIZER_MAC
17
Alexander Potapenkod895ae92013-02-06 14:41:15 +000018// 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 Samsonov2c5fc3b2012-06-04 14:27:50 +000024
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000025#include "sanitizer_common.h"
Alexey Samsonov5bbf8292012-06-05 14:25:27 +000026#include "sanitizer_internal_defs.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000027#include "sanitizer_libc.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000028#include "sanitizer_procmaps.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000029
Alexey Samsonov0c53a382012-06-14 14:07:21 +000030#include <crt_externs.h> // for _NSGetEnviron
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000031#include <fcntl.h>
Alexey Samsonov28a98952012-06-07 06:15:12 +000032#include <mach-o/dyld.h>
33#include <mach-o/loader.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000034#include <pthread.h>
Alexey Samsonov58a3c582012-06-18 08:44:30 +000035#include <sched.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000036#include <sys/mman.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000037#include <sys/resource.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000038#include <sys/stat.h>
39#include <sys/types.h>
Alexey Samsonov03c8b842012-06-05 08:32:53 +000040#include <unistd.h>
Dmitry Vyukovaf4b0b02013-01-14 08:01:58 +000041#include <libkern/OSAtomic.h>
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000042#include <errno.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000043
44namespace __sanitizer {
45
Peter Collingbourne6f4be192013-05-08 14:43:49 +000046#include "sanitizer_syscall_generic.inc"
47
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000048// ---------------------- sanitizer_libc.h
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000049uptr internal_mmap(void *addr, size_t length, int prot, int flags,
50 int fd, u64 offset) {
51 return (uptr)mmap(addr, length, prot, flags, fd, offset);
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000052}
53
Peter Collingbourne8e110ce2013-05-08 15:07:12 +000054uptr internal_munmap(void *addr, uptr length) {
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000055 return munmap(addr, length);
56}
57
Peter Collingbourne6f4be192013-05-08 14:43:49 +000058uptr internal_close(fd_t fd) {
Alexey Samsonov03c8b842012-06-05 08:32:53 +000059 return close(fd);
60}
61
Peter Collingbourne6f4be192013-05-08 14:43:49 +000062uptr internal_open(const char *filename, int flags) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000063 return open(filename, flags);
64}
65
Peter Collingbourne6f4be192013-05-08 14:43:49 +000066uptr internal_open(const char *filename, int flags, u32 mode) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000067 return open(filename, flags, mode);
68}
69
Peter Collingbourne6f4be192013-05-08 14:43:49 +000070uptr OpenFile(const char *filename, bool write) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000071 return internal_open(filename,
72 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000073}
74
Alexey Samsonov03c8b842012-06-05 08:32:53 +000075uptr internal_read(fd_t fd, void *buf, uptr count) {
76 return read(fd, buf, count);
77}
78
79uptr internal_write(fd_t fd, const void *buf, uptr count) {
80 return write(fd, buf, count);
81}
82
Peter Collingbourne6f4be192013-05-08 14:43:49 +000083uptr internal_stat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000084 return stat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000085}
86
Peter Collingbourne6f4be192013-05-08 14:43:49 +000087uptr internal_lstat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000088 return lstat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000089}
90
Peter Collingbourne6f4be192013-05-08 14:43:49 +000091uptr internal_fstat(fd_t fd, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000092 return fstat(fd, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000093}
94
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000095uptr internal_filesize(fd_t fd) {
Alexey Samsonovce8d4972012-08-02 10:09:31 +000096 struct stat st;
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000097 if (internal_fstat(fd, &st))
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000098 return -1;
99 return (uptr)st.st_size;
100}
101
Peter Collingbourne6f4be192013-05-08 14:43:49 +0000102uptr internal_dup2(int oldfd, int newfd) {
Alexey Samsonovca2b5d72012-06-06 07:30:33 +0000103 return dup2(oldfd, newfd);
104}
105
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000106uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
107 return readlink(path, buf, bufsize);
108}
109
Peter Collingbourne6f4be192013-05-08 14:43:49 +0000110uptr internal_sched_yield() {
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000111 return sched_yield();
112}
113
Alexey Samsonovaadd1f22013-02-20 13:54:32 +0000114void internal__exit(int exitcode) {
115 _exit(exitcode);
116}
117
Peter Collingbourneffaf2ea2013-05-17 16:56:53 +0000118uptr internal_getpid() {
119 return getpid();
120}
121
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000122// ----------------- sanitizer_common.h
Alexey Samsonovae9b18b2012-11-09 14:45:30 +0000123bool FileExists(const char *filename) {
124 struct stat st;
125 if (stat(filename, &st))
126 return false;
127 // Sanity check: filename is a regular file.
128 return S_ISREG(st.st_mode);
129}
130
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000131uptr GetTid() {
132 return reinterpret_cast<uptr>(pthread_self());
133}
134
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000135void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000136 uptr *stack_bottom) {
137 CHECK(stack_top);
138 CHECK(stack_bottom);
139 uptr stacksize = pthread_get_stacksize_np(pthread_self());
140 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
141 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000142 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000143}
144
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000145const char *GetEnv(const char *name) {
146 char ***env_ptr = _NSGetEnviron();
147 CHECK(env_ptr);
148 char **environ = *env_ptr;
149 CHECK(environ);
150 uptr name_len = internal_strlen(name);
151 while (*environ != 0) {
152 uptr len = internal_strlen(*environ);
153 if (len > name_len) {
154 const char *p = *environ;
155 if (!internal_memcmp(p, name, name_len) &&
156 p[name_len] == '=') { // Match.
157 return *environ + name_len + 1; // String starting after =.
158 }
159 }
160 environ++;
161 }
162 return 0;
163}
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000164
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000165void ReExec() {
166 UNIMPLEMENTED();
167}
168
Alexander Potapenko1746f552012-12-10 13:10:40 +0000169void PrepareForSandboxing() {
170 // Nothing here for now.
171}
172
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000173// ----------------- sanitizer_procmaps.h
Alexey Samsonov28a98952012-06-07 06:15:12 +0000174
Alexander Potapenkof8109dd2013-03-26 10:34:37 +0000175MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000176 Reset();
177}
178
Alexey Samsonovcc622112012-08-27 13:48:48 +0000179MemoryMappingLayout::~MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000180}
181
182// More information about Mach-O headers can be found in mach-o/loader.h
183// Each Mach-O image has a header (mach_header or mach_header_64) starting with
184// a magic number, and a list of linker load commands directly following the
185// header.
186// A load command is at least two 32-bit words: the command type and the
187// command size in bytes. We're interested only in segment load commands
188// (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
189// into the task's address space.
190// The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
191// segment_command_64 correspond to the memory address, memory size and the
192// file offset of the current memory segment.
193// Because these fields are taken from the images as is, one needs to add
194// _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
195
Alexey Samsonovcc622112012-08-27 13:48:48 +0000196void MemoryMappingLayout::Reset() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000197 // Count down from the top.
198 // TODO(glider): as per man 3 dyld, iterating over the headers with
199 // _dyld_image_count is thread-unsafe. We need to register callbacks for
Alexey Samsonovcc622112012-08-27 13:48:48 +0000200 // adding and removing images which will invalidate the MemoryMappingLayout
201 // state.
Alexey Samsonov28a98952012-06-07 06:15:12 +0000202 current_image_ = _dyld_image_count();
203 current_load_cmd_count_ = -1;
204 current_load_cmd_addr_ = 0;
205 current_magic_ = 0;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000206 current_filetype_ = 0;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000207}
208
Alexander Potapenko78114252012-12-01 02:39:45 +0000209// static
210void MemoryMappingLayout::CacheMemoryMappings() {
211 // No-op on Mac for now.
212}
213
214void MemoryMappingLayout::LoadFromCache() {
215 // No-op on Mac for now.
216}
217
Alexey Samsonov28a98952012-06-07 06:15:12 +0000218// Next and NextSegmentLoad were inspired by base/sysinfo.cc in
219// Google Perftools, http://code.google.com/p/google-perftools.
220
221// NextSegmentLoad scans the current image for the next segment load command
222// and returns the start and end addresses and file offset of the corresponding
223// segment.
224// Note that the segment addresses are not necessarily sorted.
225template<u32 kLCSegment, typename SegmentCommand>
Alexey Samsonovcc622112012-08-27 13:48:48 +0000226bool MemoryMappingLayout::NextSegmentLoad(
Alexey Samsonov28a98952012-06-07 06:15:12 +0000227 uptr *start, uptr *end, uptr *offset,
Alexey Samsonov06d3aa42013-03-13 06:51:02 +0000228 char filename[], uptr filename_size, uptr *protection) {
229 if (protection)
230 UNIMPLEMENTED();
Alexey Samsonov28a98952012-06-07 06:15:12 +0000231 const char* lc = current_load_cmd_addr_;
232 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
233 if (((const load_command *)lc)->cmd == kLCSegment) {
234 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
235 const SegmentCommand* sc = (const SegmentCommand *)lc;
236 if (start) *start = sc->vmaddr + dlloff;
237 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000238 if (offset) {
239 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
240 *offset = sc->vmaddr;
241 } else {
242 *offset = sc->fileoff;
243 }
244 }
Alexey Samsonov28a98952012-06-07 06:15:12 +0000245 if (filename) {
246 internal_strncpy(filename, _dyld_get_image_name(current_image_),
247 filename_size);
248 }
249 return true;
250 }
251 return false;
252}
253
Alexey Samsonovcc622112012-08-27 13:48:48 +0000254bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000255 char filename[], uptr filename_size,
256 uptr *protection) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000257 for (; current_image_ >= 0; current_image_--) {
258 const mach_header* hdr = _dyld_get_image_header(current_image_);
259 if (!hdr) continue;
260 if (current_load_cmd_count_ < 0) {
261 // Set up for this image;
262 current_load_cmd_count_ = hdr->ncmds;
263 current_magic_ = hdr->magic;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000264 current_filetype_ = hdr->filetype;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000265 switch (current_magic_) {
266#ifdef MH_MAGIC_64
267 case MH_MAGIC_64: {
268 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
269 break;
270 }
271#endif
272 case MH_MAGIC: {
273 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
274 break;
275 }
276 default: {
277 continue;
278 }
279 }
280 }
281
282 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
283 switch (current_magic_) {
284 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
285#ifdef MH_MAGIC_64
286 case MH_MAGIC_64: {
287 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000288 start, end, offset, filename, filename_size, protection))
Alexey Samsonov28a98952012-06-07 06:15:12 +0000289 return true;
290 break;
291 }
292#endif
293 case MH_MAGIC: {
294 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000295 start, end, offset, filename, filename_size, protection))
Alexey Samsonov28a98952012-06-07 06:15:12 +0000296 return true;
297 break;
298 }
299 }
300 }
301 // If we get here, no more load_cmd's in this image talk about
302 // segments. Go on to the next image.
303 }
304 return false;
305}
306
Alexey Samsonovcc622112012-08-27 13:48:48 +0000307bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
308 char filename[],
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000309 uptr filename_size,
Alexey Samsonov06d3aa42013-03-13 06:51:02 +0000310 uptr *protection) {
311 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size,
312 protection);
Alexey Samsonov28a98952012-06-07 06:15:12 +0000313}
314
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000315BlockingMutex::BlockingMutex(LinkerInitialized) {
316 // We assume that OS_SPINLOCK_INIT is zero
317}
318
Alexey Samsonova097f7b2013-03-14 13:30:56 +0000319BlockingMutex::BlockingMutex() {
320 internal_memset(this, 0, sizeof(*this));
321}
322
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000323void BlockingMutex::Lock() {
324 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
Kostya Serebryany459df6f2013-02-26 12:59:06 +0000325 CHECK_EQ(OS_SPINLOCK_INIT, 0);
326 CHECK_NE(owner_, (uptr)pthread_self());
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000327 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
328 CHECK(!owner_);
329 owner_ = (uptr)pthread_self();
330}
331
332void BlockingMutex::Unlock() {
333 CHECK(owner_ == (uptr)pthread_self());
334 owner_ = 0;
335 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
336}
337
Alexey Samsonovdb7d9652013-03-11 15:45:20 +0000338void BlockingMutex::CheckLocked() {
339 CHECK_EQ((uptr)pthread_self(), owner_);
340}
341
Evgeniy Stepanov5697b582013-03-13 08:19:53 +0000342uptr GetTlsSize() {
343 return 0;
344}
345
346void InitTlsSize() {
347}
348
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000349void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
350 uptr *tls_addr, uptr *tls_size) {
351 uptr stack_top, stack_bottom;
352 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
353 *stk_addr = stack_bottom;
354 *stk_size = stack_top - stack_bottom;
355 *tls_addr = 0;
356 *tls_size = 0;
357}
358
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000359} // namespace __sanitizer
360
Alexey Samsonova0e28a72013-04-03 07:24:35 +0000361#endif // SANITIZER_MAC