blob: 0181b69e8026da06e80b04371eadd5995ff91c80 [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
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000118// ----------------- sanitizer_common.h
Alexey Samsonovae9b18b2012-11-09 14:45:30 +0000119bool FileExists(const char *filename) {
120 struct stat st;
121 if (stat(filename, &st))
122 return false;
123 // Sanity check: filename is a regular file.
124 return S_ISREG(st.st_mode);
125}
126
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000127uptr GetTid() {
128 return reinterpret_cast<uptr>(pthread_self());
129}
130
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000131void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000132 uptr *stack_bottom) {
133 CHECK(stack_top);
134 CHECK(stack_bottom);
135 uptr stacksize = pthread_get_stacksize_np(pthread_self());
136 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
137 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000138 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000139}
140
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000141const char *GetEnv(const char *name) {
142 char ***env_ptr = _NSGetEnviron();
143 CHECK(env_ptr);
144 char **environ = *env_ptr;
145 CHECK(environ);
146 uptr name_len = internal_strlen(name);
147 while (*environ != 0) {
148 uptr len = internal_strlen(*environ);
149 if (len > name_len) {
150 const char *p = *environ;
151 if (!internal_memcmp(p, name, name_len) &&
152 p[name_len] == '=') { // Match.
153 return *environ + name_len + 1; // String starting after =.
154 }
155 }
156 environ++;
157 }
158 return 0;
159}
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000160
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000161void ReExec() {
162 UNIMPLEMENTED();
163}
164
Alexander Potapenko1746f552012-12-10 13:10:40 +0000165void PrepareForSandboxing() {
166 // Nothing here for now.
167}
168
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000169// ----------------- sanitizer_procmaps.h
Alexey Samsonov28a98952012-06-07 06:15:12 +0000170
Alexander Potapenkof8109dd2013-03-26 10:34:37 +0000171MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000172 Reset();
173}
174
Alexey Samsonovcc622112012-08-27 13:48:48 +0000175MemoryMappingLayout::~MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000176}
177
178// More information about Mach-O headers can be found in mach-o/loader.h
179// Each Mach-O image has a header (mach_header or mach_header_64) starting with
180// a magic number, and a list of linker load commands directly following the
181// header.
182// A load command is at least two 32-bit words: the command type and the
183// command size in bytes. We're interested only in segment load commands
184// (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
185// into the task's address space.
186// The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
187// segment_command_64 correspond to the memory address, memory size and the
188// file offset of the current memory segment.
189// Because these fields are taken from the images as is, one needs to add
190// _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
191
Alexey Samsonovcc622112012-08-27 13:48:48 +0000192void MemoryMappingLayout::Reset() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000193 // Count down from the top.
194 // TODO(glider): as per man 3 dyld, iterating over the headers with
195 // _dyld_image_count is thread-unsafe. We need to register callbacks for
Alexey Samsonovcc622112012-08-27 13:48:48 +0000196 // adding and removing images which will invalidate the MemoryMappingLayout
197 // state.
Alexey Samsonov28a98952012-06-07 06:15:12 +0000198 current_image_ = _dyld_image_count();
199 current_load_cmd_count_ = -1;
200 current_load_cmd_addr_ = 0;
201 current_magic_ = 0;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000202 current_filetype_ = 0;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000203}
204
Alexander Potapenko78114252012-12-01 02:39:45 +0000205// static
206void MemoryMappingLayout::CacheMemoryMappings() {
207 // No-op on Mac for now.
208}
209
210void MemoryMappingLayout::LoadFromCache() {
211 // No-op on Mac for now.
212}
213
Alexey Samsonov28a98952012-06-07 06:15:12 +0000214// Next and NextSegmentLoad were inspired by base/sysinfo.cc in
215// Google Perftools, http://code.google.com/p/google-perftools.
216
217// NextSegmentLoad scans the current image for the next segment load command
218// and returns the start and end addresses and file offset of the corresponding
219// segment.
220// Note that the segment addresses are not necessarily sorted.
221template<u32 kLCSegment, typename SegmentCommand>
Alexey Samsonovcc622112012-08-27 13:48:48 +0000222bool MemoryMappingLayout::NextSegmentLoad(
Alexey Samsonov28a98952012-06-07 06:15:12 +0000223 uptr *start, uptr *end, uptr *offset,
Alexey Samsonov06d3aa42013-03-13 06:51:02 +0000224 char filename[], uptr filename_size, uptr *protection) {
225 if (protection)
226 UNIMPLEMENTED();
Alexey Samsonov28a98952012-06-07 06:15:12 +0000227 const char* lc = current_load_cmd_addr_;
228 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
229 if (((const load_command *)lc)->cmd == kLCSegment) {
230 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
231 const SegmentCommand* sc = (const SegmentCommand *)lc;
232 if (start) *start = sc->vmaddr + dlloff;
233 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000234 if (offset) {
235 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
236 *offset = sc->vmaddr;
237 } else {
238 *offset = sc->fileoff;
239 }
240 }
Alexey Samsonov28a98952012-06-07 06:15:12 +0000241 if (filename) {
242 internal_strncpy(filename, _dyld_get_image_name(current_image_),
243 filename_size);
244 }
245 return true;
246 }
247 return false;
248}
249
Alexey Samsonovcc622112012-08-27 13:48:48 +0000250bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000251 char filename[], uptr filename_size,
252 uptr *protection) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000253 for (; current_image_ >= 0; current_image_--) {
254 const mach_header* hdr = _dyld_get_image_header(current_image_);
255 if (!hdr) continue;
256 if (current_load_cmd_count_ < 0) {
257 // Set up for this image;
258 current_load_cmd_count_ = hdr->ncmds;
259 current_magic_ = hdr->magic;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000260 current_filetype_ = hdr->filetype;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000261 switch (current_magic_) {
262#ifdef MH_MAGIC_64
263 case MH_MAGIC_64: {
264 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
265 break;
266 }
267#endif
268 case MH_MAGIC: {
269 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
270 break;
271 }
272 default: {
273 continue;
274 }
275 }
276 }
277
278 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
279 switch (current_magic_) {
280 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
281#ifdef MH_MAGIC_64
282 case MH_MAGIC_64: {
283 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000284 start, end, offset, filename, filename_size, protection))
Alexey Samsonov28a98952012-06-07 06:15:12 +0000285 return true;
286 break;
287 }
288#endif
289 case MH_MAGIC: {
290 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000291 start, end, offset, filename, filename_size, protection))
Alexey Samsonov28a98952012-06-07 06:15:12 +0000292 return true;
293 break;
294 }
295 }
296 }
297 // If we get here, no more load_cmd's in this image talk about
298 // segments. Go on to the next image.
299 }
300 return false;
301}
302
Alexey Samsonovcc622112012-08-27 13:48:48 +0000303bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
304 char filename[],
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000305 uptr filename_size,
Alexey Samsonov06d3aa42013-03-13 06:51:02 +0000306 uptr *protection) {
307 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size,
308 protection);
Alexey Samsonov28a98952012-06-07 06:15:12 +0000309}
310
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000311BlockingMutex::BlockingMutex(LinkerInitialized) {
312 // We assume that OS_SPINLOCK_INIT is zero
313}
314
Alexey Samsonova097f7b2013-03-14 13:30:56 +0000315BlockingMutex::BlockingMutex() {
316 internal_memset(this, 0, sizeof(*this));
317}
318
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000319void BlockingMutex::Lock() {
320 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
Kostya Serebryany459df6f2013-02-26 12:59:06 +0000321 CHECK_EQ(OS_SPINLOCK_INIT, 0);
322 CHECK_NE(owner_, (uptr)pthread_self());
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000323 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
324 CHECK(!owner_);
325 owner_ = (uptr)pthread_self();
326}
327
328void BlockingMutex::Unlock() {
329 CHECK(owner_ == (uptr)pthread_self());
330 owner_ = 0;
331 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
332}
333
Alexey Samsonovdb7d9652013-03-11 15:45:20 +0000334void BlockingMutex::CheckLocked() {
335 CHECK_EQ((uptr)pthread_self(), owner_);
336}
337
Evgeniy Stepanov5697b582013-03-13 08:19:53 +0000338uptr GetTlsSize() {
339 return 0;
340}
341
342void InitTlsSize() {
343}
344
Sergey Matveev954c6ef12013-05-07 14:41:43 +0000345void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
346 uptr *tls_addr, uptr *tls_size) {
347 uptr stack_top, stack_bottom;
348 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
349 *stk_addr = stack_bottom;
350 *stk_size = stack_top - stack_bottom;
351 *tls_addr = 0;
352 *tls_size = 0;
353}
354
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000355} // namespace __sanitizer
356
Alexey Samsonova0e28a72013-04-03 07:24:35 +0000357#endif // SANITIZER_MAC