libsanitizer merge from upstream r175733
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_mac.cc
blobd7885bb350963b1911a8f46631b1ce93745d4e5f
1 //===-- sanitizer_mac.cc --------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements mac-specific functions from
10 // sanitizer_libc.h.
11 //===----------------------------------------------------------------------===//
13 #ifdef __APPLE__
14 // Use 64-bit inodes in file operations. ASan does not support OS X 10.5, so
15 // the clients will most certainly use 64-bit ones as well.
16 #ifndef _DARWIN_USE_64_BIT_INODE
17 #define _DARWIN_USE_64_BIT_INODE 1
18 #endif
19 #include <stdio.h>
21 #include "sanitizer_common.h"
22 #include "sanitizer_internal_defs.h"
23 #include "sanitizer_libc.h"
24 #include "sanitizer_procmaps.h"
26 #include <crt_externs.h> // for _NSGetEnviron
27 #include <fcntl.h>
28 #include <mach-o/dyld.h>
29 #include <mach-o/loader.h>
30 #include <pthread.h>
31 #include <sched.h>
32 #include <sys/mman.h>
33 #include <sys/resource.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <libkern/OSAtomic.h>
39 namespace __sanitizer {
41 // ---------------------- sanitizer_libc.h
42 void *internal_mmap(void *addr, size_t length, int prot, int flags,
43 int fd, u64 offset) {
44 return mmap(addr, length, prot, flags, fd, offset);
47 int internal_munmap(void *addr, uptr length) {
48 return munmap(addr, length);
51 int internal_close(fd_t fd) {
52 return close(fd);
55 fd_t internal_open(const char *filename, int flags) {
56 return open(filename, flags);
59 fd_t internal_open(const char *filename, int flags, u32 mode) {
60 return open(filename, flags, mode);
63 fd_t OpenFile(const char *filename, bool write) {
64 return internal_open(filename,
65 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
68 uptr internal_read(fd_t fd, void *buf, uptr count) {
69 return read(fd, buf, count);
72 uptr internal_write(fd_t fd, const void *buf, uptr count) {
73 return write(fd, buf, count);
76 int internal_stat(const char *path, void *buf) {
77 return stat(path, (struct stat *)buf);
80 int internal_lstat(const char *path, void *buf) {
81 return lstat(path, (struct stat *)buf);
84 int internal_fstat(fd_t fd, void *buf) {
85 return fstat(fd, (struct stat *)buf);
88 uptr internal_filesize(fd_t fd) {
89 struct stat st;
90 if (internal_fstat(fd, &st))
91 return -1;
92 return (uptr)st.st_size;
95 int internal_dup2(int oldfd, int newfd) {
96 return dup2(oldfd, newfd);
99 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
100 return readlink(path, buf, bufsize);
103 int internal_sched_yield() {
104 return sched_yield();
107 void internal__exit(int exitcode) {
108 _exit(exitcode);
111 // ----------------- sanitizer_common.h
112 bool FileExists(const char *filename) {
113 struct stat st;
114 if (stat(filename, &st))
115 return false;
116 // Sanity check: filename is a regular file.
117 return S_ISREG(st.st_mode);
120 uptr GetTid() {
121 return reinterpret_cast<uptr>(pthread_self());
124 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
125 uptr *stack_bottom) {
126 CHECK(stack_top);
127 CHECK(stack_bottom);
128 uptr stacksize = pthread_get_stacksize_np(pthread_self());
129 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
130 *stack_top = (uptr)stackaddr;
131 *stack_bottom = *stack_top - stacksize;
134 const char *GetEnv(const char *name) {
135 char ***env_ptr = _NSGetEnviron();
136 CHECK(env_ptr);
137 char **environ = *env_ptr;
138 CHECK(environ);
139 uptr name_len = internal_strlen(name);
140 while (*environ != 0) {
141 uptr len = internal_strlen(*environ);
142 if (len > name_len) {
143 const char *p = *environ;
144 if (!internal_memcmp(p, name, name_len) &&
145 p[name_len] == '=') { // Match.
146 return *environ + name_len + 1; // String starting after =.
149 environ++;
151 return 0;
154 void ReExec() {
155 UNIMPLEMENTED();
158 void PrepareForSandboxing() {
159 // Nothing here for now.
162 // ----------------- sanitizer_procmaps.h
164 MemoryMappingLayout::MemoryMappingLayout() {
165 Reset();
168 MemoryMappingLayout::~MemoryMappingLayout() {
171 // More information about Mach-O headers can be found in mach-o/loader.h
172 // Each Mach-O image has a header (mach_header or mach_header_64) starting with
173 // a magic number, and a list of linker load commands directly following the
174 // header.
175 // A load command is at least two 32-bit words: the command type and the
176 // command size in bytes. We're interested only in segment load commands
177 // (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
178 // into the task's address space.
179 // The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
180 // segment_command_64 correspond to the memory address, memory size and the
181 // file offset of the current memory segment.
182 // Because these fields are taken from the images as is, one needs to add
183 // _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
185 void MemoryMappingLayout::Reset() {
186 // Count down from the top.
187 // TODO(glider): as per man 3 dyld, iterating over the headers with
188 // _dyld_image_count is thread-unsafe. We need to register callbacks for
189 // adding and removing images which will invalidate the MemoryMappingLayout
190 // state.
191 current_image_ = _dyld_image_count();
192 current_load_cmd_count_ = -1;
193 current_load_cmd_addr_ = 0;
194 current_magic_ = 0;
195 current_filetype_ = 0;
198 // static
199 void MemoryMappingLayout::CacheMemoryMappings() {
200 // No-op on Mac for now.
203 void MemoryMappingLayout::LoadFromCache() {
204 // No-op on Mac for now.
207 // Next and NextSegmentLoad were inspired by base/sysinfo.cc in
208 // Google Perftools, http://code.google.com/p/google-perftools.
210 // NextSegmentLoad scans the current image for the next segment load command
211 // and returns the start and end addresses and file offset of the corresponding
212 // segment.
213 // Note that the segment addresses are not necessarily sorted.
214 template<u32 kLCSegment, typename SegmentCommand>
215 bool MemoryMappingLayout::NextSegmentLoad(
216 uptr *start, uptr *end, uptr *offset,
217 char filename[], uptr filename_size) {
218 const char* lc = current_load_cmd_addr_;
219 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
220 if (((const load_command *)lc)->cmd == kLCSegment) {
221 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
222 const SegmentCommand* sc = (const SegmentCommand *)lc;
223 if (start) *start = sc->vmaddr + dlloff;
224 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
225 if (offset) {
226 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
227 *offset = sc->vmaddr;
228 } else {
229 *offset = sc->fileoff;
232 if (filename) {
233 internal_strncpy(filename, _dyld_get_image_name(current_image_),
234 filename_size);
236 return true;
238 return false;
241 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
242 char filename[], uptr filename_size) {
243 for (; current_image_ >= 0; current_image_--) {
244 const mach_header* hdr = _dyld_get_image_header(current_image_);
245 if (!hdr) continue;
246 if (current_load_cmd_count_ < 0) {
247 // Set up for this image;
248 current_load_cmd_count_ = hdr->ncmds;
249 current_magic_ = hdr->magic;
250 current_filetype_ = hdr->filetype;
251 switch (current_magic_) {
252 #ifdef MH_MAGIC_64
253 case MH_MAGIC_64: {
254 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
255 break;
257 #endif
258 case MH_MAGIC: {
259 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
260 break;
262 default: {
263 continue;
268 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
269 switch (current_magic_) {
270 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
271 #ifdef MH_MAGIC_64
272 case MH_MAGIC_64: {
273 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
274 start, end, offset, filename, filename_size))
275 return true;
276 break;
278 #endif
279 case MH_MAGIC: {
280 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
281 start, end, offset, filename, filename_size))
282 return true;
283 break;
287 // If we get here, no more load_cmd's in this image talk about
288 // segments. Go on to the next image.
290 return false;
293 bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
294 char filename[],
295 uptr filename_size) {
296 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
299 BlockingMutex::BlockingMutex(LinkerInitialized) {
300 // We assume that OS_SPINLOCK_INIT is zero
303 void BlockingMutex::Lock() {
304 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
305 CHECK(OS_SPINLOCK_INIT == 0);
306 CHECK(owner_ != (uptr)pthread_self());
307 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
308 CHECK(!owner_);
309 owner_ = (uptr)pthread_self();
312 void BlockingMutex::Unlock() {
313 CHECK(owner_ == (uptr)pthread_self());
314 owner_ = 0;
315 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
318 } // namespace __sanitizer
320 #endif // __APPLE__