blob: a7bde0309655d2ef625daaa8a70f8e20ea2d2642 [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
15#ifdef __APPLE__
16
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000017#include "sanitizer_common.h"
Alexey Samsonov5bbf8292012-06-05 14:25:27 +000018#include "sanitizer_internal_defs.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000019#include "sanitizer_libc.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000020#include "sanitizer_procmaps.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000021
Alexey Samsonov0c53a382012-06-14 14:07:21 +000022#include <crt_externs.h> // for _NSGetEnviron
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000023#include <fcntl.h>
Alexey Samsonov28a98952012-06-07 06:15:12 +000024#include <mach-o/dyld.h>
25#include <mach-o/loader.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000026#include <pthread.h>
Alexey Samsonov58a3c582012-06-18 08:44:30 +000027#include <sched.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000028#include <sys/mman.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000029#include <sys/resource.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000030#include <sys/stat.h>
31#include <sys/types.h>
Alexey Samsonov03c8b842012-06-05 08:32:53 +000032#include <unistd.h>
Dmitry Vyukovaf4b0b02013-01-14 08:01:58 +000033#include <libkern/OSAtomic.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000034
35namespace __sanitizer {
36
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000037// ---------------------- sanitizer_libc.h
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000038void *internal_mmap(void *addr, size_t length, int prot, int flags,
39 int fd, u64 offset) {
40 return mmap(addr, length, prot, flags, fd, offset);
41}
42
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000043int internal_munmap(void *addr, uptr length) {
44 return munmap(addr, length);
45}
46
Alexey Samsonov03c8b842012-06-05 08:32:53 +000047int internal_close(fd_t fd) {
48 return close(fd);
49}
50
Alexey Samsonov39313b72013-02-01 15:58:46 +000051fd_t internal_open(const char *filename, int flags) {
52 return open(filename, flags);
53}
54
Alexey Samsonov4985b872013-02-01 16:32:18 +000055fd_t internal_open(const char *filename, int flags, u32 mode) {
Alexey Samsonov39313b72013-02-01 15:58:46 +000056 return open(filename, flags, mode);
57}
58
59fd_t OpenFile(const char *filename, bool write) {
60 return internal_open(filename,
61 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000062}
63
Alexey Samsonov03c8b842012-06-05 08:32:53 +000064uptr internal_read(fd_t fd, void *buf, uptr count) {
65 return read(fd, buf, count);
66}
67
68uptr internal_write(fd_t fd, const void *buf, uptr count) {
69 return write(fd, buf, count);
70}
71
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000072uptr internal_filesize(fd_t fd) {
Alexey Samsonovce8d4972012-08-02 10:09:31 +000073 struct stat st;
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000074 if (fstat(fd, &st))
75 return -1;
76 return (uptr)st.st_size;
77}
78
79int internal_dup2(int oldfd, int newfd) {
80 return dup2(oldfd, newfd);
81}
82
Alexey Samsonovf6d21252012-09-05 14:48:24 +000083uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
84 return readlink(path, buf, bufsize);
85}
86
Alexey Samsonov58a3c582012-06-18 08:44:30 +000087int internal_sched_yield() {
88 return sched_yield();
89}
90
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000091// ----------------- sanitizer_common.h
Alexey Samsonovae9b18b2012-11-09 14:45:30 +000092bool FileExists(const char *filename) {
93 struct stat st;
94 if (stat(filename, &st))
95 return false;
96 // Sanity check: filename is a regular file.
97 return S_ISREG(st.st_mode);
98}
99
Dmitry Vyukov56faa552012-10-02 12:58:14 +0000100uptr GetTid() {
101 return reinterpret_cast<uptr>(pthread_self());
102}
103
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000104void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000105 uptr *stack_bottom) {
106 CHECK(stack_top);
107 CHECK(stack_bottom);
108 uptr stacksize = pthread_get_stacksize_np(pthread_self());
109 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
110 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +0000111 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000112}
113
Alexey Samsonov0c53a382012-06-14 14:07:21 +0000114const char *GetEnv(const char *name) {
115 char ***env_ptr = _NSGetEnviron();
116 CHECK(env_ptr);
117 char **environ = *env_ptr;
118 CHECK(environ);
119 uptr name_len = internal_strlen(name);
120 while (*environ != 0) {
121 uptr len = internal_strlen(*environ);
122 if (len > name_len) {
123 const char *p = *environ;
124 if (!internal_memcmp(p, name, name_len) &&
125 p[name_len] == '=') { // Match.
126 return *environ + name_len + 1; // String starting after =.
127 }
128 }
129 environ++;
130 }
131 return 0;
132}
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000133
Alexey Samsonov97ca3062012-09-17 09:12:39 +0000134void ReExec() {
135 UNIMPLEMENTED();
136}
137
Alexander Potapenko1746f552012-12-10 13:10:40 +0000138void PrepareForSandboxing() {
139 // Nothing here for now.
140}
141
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000142// ----------------- sanitizer_procmaps.h
Alexey Samsonov28a98952012-06-07 06:15:12 +0000143
Alexey Samsonovcc622112012-08-27 13:48:48 +0000144MemoryMappingLayout::MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000145 Reset();
146}
147
Alexey Samsonovcc622112012-08-27 13:48:48 +0000148MemoryMappingLayout::~MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000149}
150
151// More information about Mach-O headers can be found in mach-o/loader.h
152// Each Mach-O image has a header (mach_header or mach_header_64) starting with
153// a magic number, and a list of linker load commands directly following the
154// header.
155// A load command is at least two 32-bit words: the command type and the
156// command size in bytes. We're interested only in segment load commands
157// (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
158// into the task's address space.
159// The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
160// segment_command_64 correspond to the memory address, memory size and the
161// file offset of the current memory segment.
162// Because these fields are taken from the images as is, one needs to add
163// _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
164
Alexey Samsonovcc622112012-08-27 13:48:48 +0000165void MemoryMappingLayout::Reset() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000166 // Count down from the top.
167 // TODO(glider): as per man 3 dyld, iterating over the headers with
168 // _dyld_image_count is thread-unsafe. We need to register callbacks for
Alexey Samsonovcc622112012-08-27 13:48:48 +0000169 // adding and removing images which will invalidate the MemoryMappingLayout
170 // state.
Alexey Samsonov28a98952012-06-07 06:15:12 +0000171 current_image_ = _dyld_image_count();
172 current_load_cmd_count_ = -1;
173 current_load_cmd_addr_ = 0;
174 current_magic_ = 0;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000175 current_filetype_ = 0;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000176}
177
Alexander Potapenko78114252012-12-01 02:39:45 +0000178// static
179void MemoryMappingLayout::CacheMemoryMappings() {
180 // No-op on Mac for now.
181}
182
183void MemoryMappingLayout::LoadFromCache() {
184 // No-op on Mac for now.
185}
186
Alexey Samsonov28a98952012-06-07 06:15:12 +0000187// Next and NextSegmentLoad were inspired by base/sysinfo.cc in
188// Google Perftools, http://code.google.com/p/google-perftools.
189
190// NextSegmentLoad scans the current image for the next segment load command
191// and returns the start and end addresses and file offset of the corresponding
192// segment.
193// Note that the segment addresses are not necessarily sorted.
194template<u32 kLCSegment, typename SegmentCommand>
Alexey Samsonovcc622112012-08-27 13:48:48 +0000195bool MemoryMappingLayout::NextSegmentLoad(
Alexey Samsonov28a98952012-06-07 06:15:12 +0000196 uptr *start, uptr *end, uptr *offset,
197 char filename[], uptr filename_size) {
198 const char* lc = current_load_cmd_addr_;
199 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
200 if (((const load_command *)lc)->cmd == kLCSegment) {
201 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
202 const SegmentCommand* sc = (const SegmentCommand *)lc;
203 if (start) *start = sc->vmaddr + dlloff;
204 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000205 if (offset) {
206 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
207 *offset = sc->vmaddr;
208 } else {
209 *offset = sc->fileoff;
210 }
211 }
Alexey Samsonov28a98952012-06-07 06:15:12 +0000212 if (filename) {
213 internal_strncpy(filename, _dyld_get_image_name(current_image_),
214 filename_size);
215 }
216 return true;
217 }
218 return false;
219}
220
Alexey Samsonovcc622112012-08-27 13:48:48 +0000221bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
222 char filename[], uptr filename_size) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000223 for (; current_image_ >= 0; current_image_--) {
224 const mach_header* hdr = _dyld_get_image_header(current_image_);
225 if (!hdr) continue;
226 if (current_load_cmd_count_ < 0) {
227 // Set up for this image;
228 current_load_cmd_count_ = hdr->ncmds;
229 current_magic_ = hdr->magic;
Alexander Potapenko77c0ac22012-10-02 15:42:24 +0000230 current_filetype_ = hdr->filetype;
Alexey Samsonov28a98952012-06-07 06:15:12 +0000231 switch (current_magic_) {
232#ifdef MH_MAGIC_64
233 case MH_MAGIC_64: {
234 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
235 break;
236 }
237#endif
238 case MH_MAGIC: {
239 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
240 break;
241 }
242 default: {
243 continue;
244 }
245 }
246 }
247
248 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
249 switch (current_magic_) {
250 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
251#ifdef MH_MAGIC_64
252 case MH_MAGIC_64: {
253 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
254 start, end, offset, filename, filename_size))
255 return true;
256 break;
257 }
258#endif
259 case MH_MAGIC: {
260 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
261 start, end, offset, filename, filename_size))
262 return true;
263 break;
264 }
265 }
266 }
267 // If we get here, no more load_cmd's in this image talk about
268 // segments. Go on to the next image.
269 }
270 return false;
271}
272
Alexey Samsonovcc622112012-08-27 13:48:48 +0000273bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
274 char filename[],
275 uptr filename_size) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000276 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
277}
278
Dmitry Vyukovf22982b2013-01-14 07:51:39 +0000279BlockingMutex::BlockingMutex(LinkerInitialized) {
280 // We assume that OS_SPINLOCK_INIT is zero
281}
282
283void BlockingMutex::Lock() {
284 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
285 CHECK(OS_SPINLOCK_INIT == 0);
286 CHECK(owner_ != (uptr)pthread_self());
287 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
288 CHECK(!owner_);
289 owner_ = (uptr)pthread_self();
290}
291
292void BlockingMutex::Unlock() {
293 CHECK(owner_ == (uptr)pthread_self());
294 owner_ = 0;
295 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
296}
297
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000298} // namespace __sanitizer
299
300#endif // __APPLE__