blob: e6c86c0f3e989eaee66a775c6e1cc4a6c0e5e24f [file] [log] [blame]
Alexey Samsonovae4d9ca2012-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 Stepanov24e13722013-03-19 14:33:38 +000015#include "sanitizer_platform.h"
16#if SANITIZER_MAC
17
Alexander Potapenko2bf0d032013-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 Samsonovae4d9ca2012-06-04 14:27:50 +000024
Alexey Samsonove5931fd2012-06-07 07:13:46 +000025#include "sanitizer_common.h"
Alexey Samsonov94b50362012-06-05 14:25:27 +000026#include "sanitizer_internal_defs.h"
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000027#include "sanitizer_libc.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000028#include "sanitizer_procmaps.h"
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000029
Alexey Samsonov3dbeabb2012-06-14 14:07:21 +000030#include <crt_externs.h> // for _NSGetEnviron
Alexey Samsonove5931fd2012-06-07 07:13:46 +000031#include <fcntl.h>
Alexey Samsonov6895adc2012-06-07 06:15:12 +000032#include <mach-o/dyld.h>
33#include <mach-o/loader.h>
Alexey Samsonove5931fd2012-06-07 07:13:46 +000034#include <pthread.h>
Alexey Samsonov0969bcf2012-06-18 08:44:30 +000035#include <sched.h>
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000036#include <sys/mman.h>
Alexey Samsonove5931fd2012-06-07 07:13:46 +000037#include <sys/resource.h>
Alexey Samsonovc5d46512012-06-05 07:05:10 +000038#include <sys/stat.h>
39#include <sys/types.h>
Alexey Samsonova56aefd2012-06-05 08:32:53 +000040#include <unistd.h>
Dmitry Vyukov48526012013-01-14 08:01:58 +000041#include <libkern/OSAtomic.h>
Peter Collingbourne65246dc2013-05-08 15:07:12 +000042#include <errno.h>
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +000043
44namespace __sanitizer {
45
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000046#include "sanitizer_syscall_generic.inc"
47
Alexey Samsonove5931fd2012-06-07 07:13:46 +000048// ---------------------- sanitizer_libc.h
Peter Collingbourne65246dc2013-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 Samsonovae4d9ca2012-06-04 14:27:50 +000052}
53
Peter Collingbourne65246dc2013-05-08 15:07:12 +000054uptr internal_munmap(void *addr, uptr length) {
Alexey Samsonov1f11d312012-06-05 09:49:25 +000055 return munmap(addr, length);
56}
57
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000058uptr internal_close(fd_t fd) {
Alexey Samsonova56aefd2012-06-05 08:32:53 +000059 return close(fd);
60}
61
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000062uptr internal_open(const char *filename, int flags) {
Alexey Samsonovee7cc442013-02-01 15:58:46 +000063 return open(filename, flags);
64}
65
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000066uptr internal_open(const char *filename, int flags, u32 mode) {
Alexey Samsonovee7cc442013-02-01 15:58:46 +000067 return open(filename, flags, mode);
68}
69
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000070uptr OpenFile(const char *filename, bool write) {
Alexey Samsonovee7cc442013-02-01 15:58:46 +000071 return internal_open(filename,
72 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovc5d46512012-06-05 07:05:10 +000073}
74
Alexey Samsonova56aefd2012-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 Collingbourne9578a3e2013-05-08 14:43:49 +000083uptr internal_stat(const char *path, void *buf) {
Alexey Samsonov7c50ee72013-02-04 10:31:39 +000084 return stat(path, (struct stat *)buf);
Alexey Samsonov4c9317a2013-02-04 10:16:50 +000085}
86
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000087uptr internal_lstat(const char *path, void *buf) {
Alexey Samsonov7c50ee72013-02-04 10:31:39 +000088 return lstat(path, (struct stat *)buf);
Alexey Samsonov4c9317a2013-02-04 10:16:50 +000089}
90
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000091uptr internal_fstat(fd_t fd, void *buf) {
Alexey Samsonov7c50ee72013-02-04 10:31:39 +000092 return fstat(fd, (struct stat *)buf);
Alexey Samsonov4c9317a2013-02-04 10:16:50 +000093}
94
Alexey Samsonov8e820fc2012-06-06 07:30:33 +000095uptr internal_filesize(fd_t fd) {
Alexey Samsonov668accc2012-08-02 10:09:31 +000096 struct stat st;
Alexey Samsonov4c9317a2013-02-04 10:16:50 +000097 if (internal_fstat(fd, &st))
Alexey Samsonov8e820fc2012-06-06 07:30:33 +000098 return -1;
99 return (uptr)st.st_size;
100}
101
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000102uptr internal_dup2(int oldfd, int newfd) {
Alexey Samsonov8e820fc2012-06-06 07:30:33 +0000103 return dup2(oldfd, newfd);
104}
105
Alexey Samsonovd1b8f582012-09-05 14:48:24 +0000106uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
107 return readlink(path, buf, bufsize);
108}
109
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000110uptr internal_sched_yield() {
Alexey Samsonov0969bcf2012-06-18 08:44:30 +0000111 return sched_yield();
112}
113
Alexey Samsonovf8822472013-02-20 13:54:32 +0000114void internal__exit(int exitcode) {
115 _exit(exitcode);
116}
117
Peter Collingbourne0b694fc2013-05-17 16:56:53 +0000118uptr internal_getpid() {
119 return getpid();
120}
121
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000122// ----------------- sanitizer_common.h
Alexey Samsonov93b4caf2012-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 Vyukove0023f72012-10-02 12:58:14 +0000131uptr GetTid() {
132 return reinterpret_cast<uptr>(pthread_self());
133}
134
Alexey Samsonoved996f72012-06-07 07:32:00 +0000135void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonove5931fd2012-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 Samsonoved996f72012-06-07 07:32:00 +0000142 *stack_bottom = *stack_top - stacksize;
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000143}
144
Alexey Samsonov3dbeabb2012-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 Samsonove5931fd2012-06-07 07:13:46 +0000164
Alexey Samsonovd7e5bb42012-09-17 09:12:39 +0000165void ReExec() {
166 UNIMPLEMENTED();
167}
168
Alexander Potapenko25742572012-12-10 13:10:40 +0000169void PrepareForSandboxing() {
170 // Nothing here for now.
171}
172
Peter Collingbourne4df343a2013-05-20 17:05:29 +0000173uptr GetPageSize() {
174 return sysconf(_SC_PAGESIZE);
175}
176
Alexey Samsonove5931fd2012-06-07 07:13:46 +0000177// ----------------- sanitizer_procmaps.h
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000178
Alexander Potapenko9ae28832013-03-26 10:34:37 +0000179MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000180 Reset();
181}
182
Alexey Samsonove1f5dac2012-08-27 13:48:48 +0000183MemoryMappingLayout::~MemoryMappingLayout() {
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000184}
185
186// More information about Mach-O headers can be found in mach-o/loader.h
187// Each Mach-O image has a header (mach_header or mach_header_64) starting with
188// a magic number, and a list of linker load commands directly following the
189// header.
190// A load command is at least two 32-bit words: the command type and the
191// command size in bytes. We're interested only in segment load commands
192// (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
193// into the task's address space.
194// The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
195// segment_command_64 correspond to the memory address, memory size and the
196// file offset of the current memory segment.
197// Because these fields are taken from the images as is, one needs to add
198// _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
199
Alexey Samsonove1f5dac2012-08-27 13:48:48 +0000200void MemoryMappingLayout::Reset() {
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000201 // Count down from the top.
202 // TODO(glider): as per man 3 dyld, iterating over the headers with
203 // _dyld_image_count is thread-unsafe. We need to register callbacks for
Alexey Samsonove1f5dac2012-08-27 13:48:48 +0000204 // adding and removing images which will invalidate the MemoryMappingLayout
205 // state.
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000206 current_image_ = _dyld_image_count();
207 current_load_cmd_count_ = -1;
208 current_load_cmd_addr_ = 0;
209 current_magic_ = 0;
Alexander Potapenko8bf8b792012-10-02 15:42:24 +0000210 current_filetype_ = 0;
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000211}
212
Alexander Potapenko93da8b62012-12-01 02:39:45 +0000213// static
214void MemoryMappingLayout::CacheMemoryMappings() {
215 // No-op on Mac for now.
216}
217
218void MemoryMappingLayout::LoadFromCache() {
219 // No-op on Mac for now.
220}
221
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000222// Next and NextSegmentLoad were inspired by base/sysinfo.cc in
223// Google Perftools, http://code.google.com/p/google-perftools.
224
225// NextSegmentLoad scans the current image for the next segment load command
226// and returns the start and end addresses and file offset of the corresponding
227// segment.
228// Note that the segment addresses are not necessarily sorted.
229template<u32 kLCSegment, typename SegmentCommand>
Alexey Samsonove1f5dac2012-08-27 13:48:48 +0000230bool MemoryMappingLayout::NextSegmentLoad(
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000231 uptr *start, uptr *end, uptr *offset,
Alexey Samsonov45717c92013-03-13 06:51:02 +0000232 char filename[], uptr filename_size, uptr *protection) {
233 if (protection)
234 UNIMPLEMENTED();
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000235 const char* lc = current_load_cmd_addr_;
236 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
237 if (((const load_command *)lc)->cmd == kLCSegment) {
238 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
239 const SegmentCommand* sc = (const SegmentCommand *)lc;
240 if (start) *start = sc->vmaddr + dlloff;
241 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
Alexander Potapenko8bf8b792012-10-02 15:42:24 +0000242 if (offset) {
243 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
244 *offset = sc->vmaddr;
245 } else {
246 *offset = sc->fileoff;
247 }
248 }
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000249 if (filename) {
250 internal_strncpy(filename, _dyld_get_image_name(current_image_),
251 filename_size);
252 }
253 return true;
254 }
255 return false;
256}
257
Alexey Samsonove1f5dac2012-08-27 13:48:48 +0000258bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
Alexey Samsonov4a1a4952013-03-13 07:39:25 +0000259 char filename[], uptr filename_size,
260 uptr *protection) {
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000261 for (; current_image_ >= 0; current_image_--) {
262 const mach_header* hdr = _dyld_get_image_header(current_image_);
263 if (!hdr) continue;
264 if (current_load_cmd_count_ < 0) {
265 // Set up for this image;
266 current_load_cmd_count_ = hdr->ncmds;
267 current_magic_ = hdr->magic;
Alexander Potapenko8bf8b792012-10-02 15:42:24 +0000268 current_filetype_ = hdr->filetype;
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000269 switch (current_magic_) {
270#ifdef MH_MAGIC_64
271 case MH_MAGIC_64: {
272 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
273 break;
274 }
275#endif
276 case MH_MAGIC: {
277 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
278 break;
279 }
280 default: {
281 continue;
282 }
283 }
284 }
285
286 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
287 switch (current_magic_) {
288 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
289#ifdef MH_MAGIC_64
290 case MH_MAGIC_64: {
291 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
Alexey Samsonov4a1a4952013-03-13 07:39:25 +0000292 start, end, offset, filename, filename_size, protection))
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000293 return true;
294 break;
295 }
296#endif
297 case MH_MAGIC: {
298 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
Alexey Samsonov4a1a4952013-03-13 07:39:25 +0000299 start, end, offset, filename, filename_size, protection))
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000300 return true;
301 break;
302 }
303 }
304 }
305 // If we get here, no more load_cmd's in this image talk about
306 // segments. Go on to the next image.
307 }
308 return false;
309}
310
Alexey Samsonove1f5dac2012-08-27 13:48:48 +0000311bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
312 char filename[],
Alexey Samsonov4a1a4952013-03-13 07:39:25 +0000313 uptr filename_size,
Alexey Samsonov45717c92013-03-13 06:51:02 +0000314 uptr *protection) {
315 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size,
316 protection);
Alexey Samsonov6895adc2012-06-07 06:15:12 +0000317}
318
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000319BlockingMutex::BlockingMutex(LinkerInitialized) {
320 // We assume that OS_SPINLOCK_INIT is zero
321}
322
Alexey Samsonov93af5942013-03-14 13:30:56 +0000323BlockingMutex::BlockingMutex() {
324 internal_memset(this, 0, sizeof(*this));
325}
326
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000327void BlockingMutex::Lock() {
328 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
Kostya Serebryanyf155fcc2013-02-26 12:59:06 +0000329 CHECK_EQ(OS_SPINLOCK_INIT, 0);
330 CHECK_NE(owner_, (uptr)pthread_self());
Dmitry Vyukovf4f51f22013-01-14 07:51:39 +0000331 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
332 CHECK(!owner_);
333 owner_ = (uptr)pthread_self();
334}
335
336void BlockingMutex::Unlock() {
337 CHECK(owner_ == (uptr)pthread_self());
338 owner_ = 0;
339 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
340}
341
Alexey Samsonovce700972013-03-11 15:45:20 +0000342void BlockingMutex::CheckLocked() {
343 CHECK_EQ((uptr)pthread_self(), owner_);
344}
345
Dmitry Vyukovd4bb4a62013-06-06 13:00:32 +0000346u64 NanoTime() {
347 return 0;
348}
349
Evgeniy Stepanovb114ed82013-03-13 08:19:53 +0000350uptr GetTlsSize() {
351 return 0;
352}
353
354void InitTlsSize() {
355}
356
Sergey Matveev24323de2013-05-07 14:41:43 +0000357void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
358 uptr *tls_addr, uptr *tls_size) {
359 uptr stack_top, stack_bottom;
360 GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
361 *stk_addr = stack_bottom;
362 *stk_size = stack_top - stack_bottom;
363 *tls_addr = 0;
364 *tls_size = 0;
365}
366
Alexey Samsonovae4d9ca2012-06-04 14:27:50 +0000367} // namespace __sanitizer
368
Alexey Samsonov46f93952013-04-03 07:24:35 +0000369#endif // SANITIZER_MAC