blob: baac4a932f5c8f806901edf6d162fe0ccfec8b7d [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>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000042
43namespace __sanitizer {
44
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000045// ---------------------- sanitizer_libc.h
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000046void *internal_mmap(void *addr, size_t length, int prot, int flags,
47 int fd, u64 offset) {
48 return mmap(addr, length, prot, flags, fd, offset);
49}
50
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000051int internal_munmap(void *addr, uptr length) {
52 return munmap(addr, length);
53}
54
Alexey Samsonov03c8b842012-06-05 08:32:53 +000055int internal_close(fd_t fd) {
56 return close(fd);
57}
58
Alexey Samsonov39313b72013-02-01 15:58:46 +000059fd_t internal_open(const char *filename, int flags) {
60 return open(filename, flags);
61}
62
Alexey Samsonov4985b872013-02-01 16:32:18 +000063fd_t internal_open(const char *filename, int flags, u32 mode) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000064 return open(filename, flags, mode);
65}
66
67fd_t OpenFile(const char *filename, bool write) {
68 return internal_open(filename,
69 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000070}
71
Alexey Samsonov03c8b842012-06-05 08:32:53 +000072uptr internal_read(fd_t fd, void *buf, uptr count) {
73 return read(fd, buf, count);
74}
75
76uptr internal_write(fd_t fd, const void *buf, uptr count) {
77 return write(fd, buf, count);
78}
79
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000080int internal_stat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000081 return stat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000082}
83
84int internal_lstat(const char *path, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000085 return lstat(path, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000086}
87
88int internal_fstat(fd_t fd, void *buf) {
Alexey Samsonov576e2702013-02-04 10:31:39 +000089 return fstat(fd, (struct stat *)buf);
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000090}
91
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000092uptr internal_filesize(fd_t fd) {
Alexey Samsonovce8d4972012-08-02 10:09:31 +000093 struct stat st;
Alexey Samsonov2c5cbd22013-02-04 10:16:50 +000094 if (internal_fstat(fd, &st))
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000095 return -1;
96 return (uptr)st.st_size;
97}
98
99int internal_dup2(int oldfd, int newfd) {
100 return dup2(oldfd, newfd);
101}
102
Alexey Samsonovf6d21252012-09-05 14:48:24 +0000103uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
104 return readlink(path, buf, bufsize);
105}
106
Alexey Samsonov58a3c582012-06-18 08:44:30 +0000107int internal_sched_yield() {
108 return sched_yield();
109}
110
Alexey Samsonovaadd1f22013-02-20 13:54:32 +0000111void internal__exit(int exitcode) {
112 _exit(exitcode);
113}
114
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000115// ----------------- sanitizer_common.h
Alexey Samsonovae9b18b2012-11-09 14:45:30 +0000116bool FileExists(const char *filename) {
117 struct stat st;
118 if (stat(filename, &st))
119 return false;
120 // Sanity check: filename is a regular file.
121 return S_ISREG(st.st_mode);
122}
123
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000124uptr GetTid() {
125 return reinterpret_cast<uptr>(pthread_self());
126}
127
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000128void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000129 uptr *stack_bottom) {
130 CHECK(stack_top);
131 CHECK(stack_bottom);
132 uptr stacksize = pthread_get_stacksize_np(pthread_self());
133 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
134 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000135 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000136}
137
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000138const char *GetEnv(const char *name) {
139 char ***env_ptr = _NSGetEnviron();
140 CHECK(env_ptr);
141 char **environ = *env_ptr;
142 CHECK(environ);
143 uptr name_len = internal_strlen(name);
144 while (*environ != 0) {
145 uptr len = internal_strlen(*environ);
146 if (len > name_len) {
147 const char *p = *environ;
148 if (!internal_memcmp(p, name, name_len) &&
149 p[name_len] == '=') { // Match.
150 return *environ + name_len + 1; // String starting after =.
151 }
152 }
153 environ++;
154 }
155 return 0;
156}
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000157
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000158void ReExec() {
159 UNIMPLEMENTED();
160}
161
Alexander Potapenko1746f552012-12-10 13:10:40 +0000162void PrepareForSandboxing() {
163 // Nothing here for now.
164}
165
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000166// ----------------- sanitizer_procmaps.h
Alexey Samsonov28a98952012-06-07 06:15:12 +0000167
Alexander Potapenkof8109dd2013-03-26 10:34:37 +0000168MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000169 Reset();
170}
171
Alexey Samsonovcc622112012-08-27 13:48:48 +0000172MemoryMappingLayout::~MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000173}
174
175// More information about Mach-O headers can be found in mach-o/loader.h
176// Each Mach-O image has a header (mach_header or mach_header_64) starting with
177// a magic number, and a list of linker load commands directly following the
178// header.
179// A load command is at least two 32-bit words: the command type and the
180// command size in bytes. We're interested only in segment load commands
181// (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
182// into the task's address space.
183// The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
184// segment_command_64 correspond to the memory address, memory size and the
185// file offset of the current memory segment.
186// Because these fields are taken from the images as is, one needs to add
187// _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
188
Alexey Samsonovcc622112012-08-27 13:48:48 +0000189void MemoryMappingLayout::Reset() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000190 // Count down from the top.
191 // TODO(glider): as per man 3 dyld, iterating over the headers with
192 // _dyld_image_count is thread-unsafe. We need to register callbacks for
Alexey Samsonovcc622112012-08-27 13:48:48 +0000193 // adding and removing images which will invalidate the MemoryMappingLayout
194 // state.
Alexey Samsonov28a98952012-06-07 06:15:12 +0000195 current_image_ = _dyld_image_count();
196 current_load_cmd_count_ = -1;
197 current_load_cmd_addr_ = 0;
198 current_magic_ = 0;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000199 current_filetype_ = 0;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000200}
201
Alexander Potapenko78114252012-12-01 02:39:45 +0000202// static
203void MemoryMappingLayout::CacheMemoryMappings() {
204 // No-op on Mac for now.
205}
206
207void MemoryMappingLayout::LoadFromCache() {
208 // No-op on Mac for now.
209}
210
Alexey Samsonov28a98952012-06-07 06:15:12 +0000211// Next and NextSegmentLoad were inspired by base/sysinfo.cc in
212// Google Perftools, http://code.google.com/p/google-perftools.
213
214// NextSegmentLoad scans the current image for the next segment load command
215// and returns the start and end addresses and file offset of the corresponding
216// segment.
217// Note that the segment addresses are not necessarily sorted.
218template<u32 kLCSegment, typename SegmentCommand>
Alexey Samsonovcc622112012-08-27 13:48:48 +0000219bool MemoryMappingLayout::NextSegmentLoad(
Alexey Samsonov28a98952012-06-07 06:15:12 +0000220 uptr *start, uptr *end, uptr *offset,
Alexey Samsonov06d3aa42013-03-13 06:51:02 +0000221 char filename[], uptr filename_size, uptr *protection) {
222 if (protection)
223 UNIMPLEMENTED();
Alexey Samsonov28a98952012-06-07 06:15:12 +0000224 const char* lc = current_load_cmd_addr_;
225 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
226 if (((const load_command *)lc)->cmd == kLCSegment) {
227 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
228 const SegmentCommand* sc = (const SegmentCommand *)lc;
229 if (start) *start = sc->vmaddr + dlloff;
230 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000231 if (offset) {
232 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
233 *offset = sc->vmaddr;
234 } else {
235 *offset = sc->fileoff;
236 }
237 }
Alexey Samsonov28a98952012-06-07 06:15:12 +0000238 if (filename) {
239 internal_strncpy(filename, _dyld_get_image_name(current_image_),
240 filename_size);
241 }
242 return true;
243 }
244 return false;
245}
246
Alexey Samsonovcc622112012-08-27 13:48:48 +0000247bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000248 char filename[], uptr filename_size,
249 uptr *protection) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000250 for (; current_image_ >= 0; current_image_--) {
251 const mach_header* hdr = _dyld_get_image_header(current_image_);
252 if (!hdr) continue;
253 if (current_load_cmd_count_ < 0) {
254 // Set up for this image;
255 current_load_cmd_count_ = hdr->ncmds;
256 current_magic_ = hdr->magic;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000257 current_filetype_ = hdr->filetype;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000258 switch (current_magic_) {
259#ifdef MH_MAGIC_64
260 case MH_MAGIC_64: {
261 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
262 break;
263 }
264#endif
265 case MH_MAGIC: {
266 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
267 break;
268 }
269 default: {
270 continue;
271 }
272 }
273 }
274
275 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
276 switch (current_magic_) {
277 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
278#ifdef MH_MAGIC_64
279 case MH_MAGIC_64: {
280 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000281 start, end, offset, filename, filename_size, protection))
Alexey Samsonov28a98952012-06-07 06:15:12 +0000282 return true;
283 break;
284 }
285#endif
286 case MH_MAGIC: {
287 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
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 }
293 }
294 // If we get here, no more load_cmd's in this image talk about
295 // segments. Go on to the next image.
296 }
297 return false;
298}
299
Alexey Samsonovcc622112012-08-27 13:48:48 +0000300bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
301 char filename[],
Alexey Samsonov91f833a2013-03-13 07:39:25 +0000302 uptr filename_size,
Alexey Samsonov06d3aa42013-03-13 06:51:02 +0000303 uptr *protection) {
304 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size,
305 protection);
Alexey Samsonov28a98952012-06-07 06:15:12 +0000306}
307
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000308BlockingMutex::BlockingMutex(LinkerInitialized) {
309 // We assume that OS_SPINLOCK_INIT is zero
310}
311
Alexey Samsonova097f7b2013-03-14 13:30:56 +0000312BlockingMutex::BlockingMutex() {
313 internal_memset(this, 0, sizeof(*this));
314}
315
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000316void BlockingMutex::Lock() {
317 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
Kostya Serebryany459df6f2013-02-26 12:59:06 +0000318 CHECK_EQ(OS_SPINLOCK_INIT, 0);
319 CHECK_NE(owner_, (uptr)pthread_self());
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000320 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
321 CHECK(!owner_);
322 owner_ = (uptr)pthread_self();
323}
324
325void BlockingMutex::Unlock() {
326 CHECK(owner_ == (uptr)pthread_self());
327 owner_ = 0;
328 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
329}
330
Alexey Samsonovdb7d9652013-03-11 15:45:20 +0000331void BlockingMutex::CheckLocked() {
332 CHECK_EQ((uptr)pthread_self(), owner_);
333}
334
Evgeniy Stepanov5697b582013-03-13 08:19:53 +0000335uptr GetTlsSize() {
336 return 0;
337}
338
339void InitTlsSize() {
340}
341
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000342} // namespace __sanitizer
343
Alexey Samsonova0e28a72013-04-03 07:24:35 +0000344#endif // SANITIZER_MAC