1 //===-- sanitizer_mac.cc --------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries and implements mac-specific functions from
11 //===----------------------------------------------------------------------===//
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
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
28 #include <mach-o/dyld.h>
29 #include <mach-o/loader.h>
33 #include <sys/resource.h>
35 #include <sys/types.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
,
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
) {
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
) {
90 if (internal_fstat(fd
, &st
))
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
) {
111 // ----------------- sanitizer_common.h
112 bool FileExists(const char *filename
) {
114 if (stat(filename
, &st
))
116 // Sanity check: filename is a regular file.
117 return S_ISREG(st
.st_mode
);
121 return reinterpret_cast<uptr
>(pthread_self());
124 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
125 uptr
*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();
137 char **environ
= *env_ptr
;
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 =.
158 void PrepareForSandboxing() {
159 // Nothing here for now.
162 // ----------------- sanitizer_procmaps.h
164 MemoryMappingLayout::MemoryMappingLayout() {
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
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
191 current_image_
= _dyld_image_count();
192 current_load_cmd_count_
= -1;
193 current_load_cmd_addr_
= 0;
195 current_filetype_
= 0;
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
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
;
226 if (current_filetype_
== /*MH_EXECUTE*/ 0x2) {
227 *offset
= sc
->vmaddr
;
229 *offset
= sc
->fileoff
;
233 internal_strncpy(filename
, _dyld_get_image_name(current_image_
),
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_
);
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_
) {
254 current_load_cmd_addr_
= (char*)hdr
+ sizeof(mach_header_64
);
259 current_load_cmd_addr_
= (char*)hdr
+ sizeof(mach_header
);
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.
273 if (NextSegmentLoad
<LC_SEGMENT_64
, struct segment_command_64
>(
274 start
, end
, offset
, filename
, filename_size
))
280 if (NextSegmentLoad
<LC_SEGMENT
, struct segment_command
>(
281 start
, end
, offset
, filename
, filename_size
))
287 // If we get here, no more load_cmd's in this image talk about
288 // segments. Go on to the next image.
293 bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr
, uptr
*offset
,
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_
);
309 owner_
= (uptr
)pthread_self();
312 void BlockingMutex::Unlock() {
313 CHECK(owner_
== (uptr
)pthread_self());
315 OSSpinLockUnlock((OSSpinLock
*)&opaque_storage_
);
318 } // namespace __sanitizer