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 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
16 // Use 64-bit inodes in file operations. ASan does not support OS X 10.5, so
17 // the clients will most certainly use 64-bit ones as well.
18 #ifndef _DARWIN_USE_64_BIT_INODE
19 #define _DARWIN_USE_64_BIT_INODE 1
23 #include "sanitizer_common.h"
24 #include "sanitizer_internal_defs.h"
25 #include "sanitizer_libc.h"
26 #include "sanitizer_placement_new.h"
27 #include "sanitizer_procmaps.h"
29 #include <crt_externs.h> // for _NSGetEnviron
31 #include <mach-o/dyld.h>
32 #include <mach-o/loader.h>
36 #include <sys/resource.h>
38 #include <sys/types.h>
40 #include <libkern/OSAtomic.h>
43 namespace __sanitizer
{
45 #include "sanitizer_syscall_generic.inc"
47 // ---------------------- sanitizer_libc.h
48 uptr
internal_mmap(void *addr
, size_t length
, int prot
, int flags
,
50 return (uptr
)mmap(addr
, length
, prot
, flags
, fd
, offset
);
53 uptr
internal_munmap(void *addr
, uptr length
) {
54 return munmap(addr
, length
);
57 uptr
internal_close(fd_t fd
) {
61 uptr
internal_open(const char *filename
, int flags
) {
62 return open(filename
, flags
);
65 uptr
internal_open(const char *filename
, int flags
, u32 mode
) {
66 return open(filename
, flags
, mode
);
69 uptr
OpenFile(const char *filename
, bool write
) {
70 return internal_open(filename
,
71 write
? O_WRONLY
| O_CREAT
: O_RDONLY
, 0660);
74 uptr
internal_read(fd_t fd
, void *buf
, uptr count
) {
75 return read(fd
, buf
, count
);
78 uptr
internal_write(fd_t fd
, const void *buf
, uptr count
) {
79 return write(fd
, buf
, count
);
82 uptr
internal_stat(const char *path
, void *buf
) {
83 return stat(path
, (struct stat
*)buf
);
86 uptr
internal_lstat(const char *path
, void *buf
) {
87 return lstat(path
, (struct stat
*)buf
);
90 uptr
internal_fstat(fd_t fd
, void *buf
) {
91 return fstat(fd
, (struct stat
*)buf
);
94 uptr
internal_filesize(fd_t fd
) {
96 if (internal_fstat(fd
, &st
))
98 return (uptr
)st
.st_size
;
101 uptr
internal_dup2(int oldfd
, int newfd
) {
102 return dup2(oldfd
, newfd
);
105 uptr
internal_readlink(const char *path
, char *buf
, uptr bufsize
) {
106 return readlink(path
, buf
, bufsize
);
109 uptr
internal_sched_yield() {
110 return sched_yield();
113 void internal__exit(int exitcode
) {
117 uptr
internal_getpid() {
121 // ----------------- sanitizer_common.h
122 bool FileExists(const char *filename
) {
124 if (stat(filename
, &st
))
126 // Sanity check: filename is a regular file.
127 return S_ISREG(st
.st_mode
);
131 return reinterpret_cast<uptr
>(pthread_self());
134 void GetThreadStackTopAndBottom(bool at_initialization
, uptr
*stack_top
,
135 uptr
*stack_bottom
) {
138 uptr stacksize
= pthread_get_stacksize_np(pthread_self());
139 void *stackaddr
= pthread_get_stackaddr_np(pthread_self());
140 *stack_top
= (uptr
)stackaddr
;
141 *stack_bottom
= *stack_top
- stacksize
;
144 const char *GetEnv(const char *name
) {
145 char ***env_ptr
= _NSGetEnviron();
147 char **environ
= *env_ptr
;
149 uptr name_len
= internal_strlen(name
);
150 while (*environ
!= 0) {
151 uptr len
= internal_strlen(*environ
);
152 if (len
> name_len
) {
153 const char *p
= *environ
;
154 if (!internal_memcmp(p
, name
, name_len
) &&
155 p
[name_len
] == '=') { // Match.
156 return *environ
+ name_len
+ 1; // String starting after =.
168 void PrepareForSandboxing() {
169 // Nothing here for now.
173 return sysconf(_SC_PAGESIZE
);
176 // ----------------- sanitizer_procmaps.h
178 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled
) {
182 MemoryMappingLayout::~MemoryMappingLayout() {
185 // More information about Mach-O headers can be found in mach-o/loader.h
186 // Each Mach-O image has a header (mach_header or mach_header_64) starting with
187 // a magic number, and a list of linker load commands directly following the
189 // A load command is at least two 32-bit words: the command type and the
190 // command size in bytes. We're interested only in segment load commands
191 // (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
192 // into the task's address space.
193 // The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
194 // segment_command_64 correspond to the memory address, memory size and the
195 // file offset of the current memory segment.
196 // Because these fields are taken from the images as is, one needs to add
197 // _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
199 void MemoryMappingLayout::Reset() {
200 // Count down from the top.
201 // TODO(glider): as per man 3 dyld, iterating over the headers with
202 // _dyld_image_count is thread-unsafe. We need to register callbacks for
203 // adding and removing images which will invalidate the MemoryMappingLayout
205 current_image_
= _dyld_image_count();
206 current_load_cmd_count_
= -1;
207 current_load_cmd_addr_
= 0;
209 current_filetype_
= 0;
213 void MemoryMappingLayout::CacheMemoryMappings() {
214 // No-op on Mac for now.
217 void MemoryMappingLayout::LoadFromCache() {
218 // No-op on Mac for now.
221 // Next and NextSegmentLoad were inspired by base/sysinfo.cc in
222 // Google Perftools, http://code.google.com/p/google-perftools.
224 // NextSegmentLoad scans the current image for the next segment load command
225 // and returns the start and end addresses and file offset of the corresponding
227 // Note that the segment addresses are not necessarily sorted.
228 template<u32 kLCSegment
, typename SegmentCommand
>
229 bool MemoryMappingLayout::NextSegmentLoad(
230 uptr
*start
, uptr
*end
, uptr
*offset
,
231 char filename
[], uptr filename_size
, uptr
*protection
) {
234 const char* lc
= current_load_cmd_addr_
;
235 current_load_cmd_addr_
+= ((const load_command
*)lc
)->cmdsize
;
236 if (((const load_command
*)lc
)->cmd
== kLCSegment
) {
237 const sptr dlloff
= _dyld_get_image_vmaddr_slide(current_image_
);
238 const SegmentCommand
* sc
= (const SegmentCommand
*)lc
;
239 if (start
) *start
= sc
->vmaddr
+ dlloff
;
240 if (end
) *end
= sc
->vmaddr
+ sc
->vmsize
+ dlloff
;
242 if (current_filetype_
== /*MH_EXECUTE*/ 0x2) {
243 *offset
= sc
->vmaddr
;
245 *offset
= sc
->fileoff
;
249 internal_strncpy(filename
, _dyld_get_image_name(current_image_
),
257 bool MemoryMappingLayout::Next(uptr
*start
, uptr
*end
, uptr
*offset
,
258 char filename
[], uptr filename_size
,
260 for (; current_image_
>= 0; current_image_
--) {
261 const mach_header
* hdr
= _dyld_get_image_header(current_image_
);
263 if (current_load_cmd_count_
< 0) {
264 // Set up for this image;
265 current_load_cmd_count_
= hdr
->ncmds
;
266 current_magic_
= hdr
->magic
;
267 current_filetype_
= hdr
->filetype
;
268 switch (current_magic_
) {
271 current_load_cmd_addr_
= (char*)hdr
+ sizeof(mach_header_64
);
276 current_load_cmd_addr_
= (char*)hdr
+ sizeof(mach_header
);
285 for (; current_load_cmd_count_
>= 0; current_load_cmd_count_
--) {
286 switch (current_magic_
) {
287 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
290 if (NextSegmentLoad
<LC_SEGMENT_64
, struct segment_command_64
>(
291 start
, end
, offset
, filename
, filename_size
, protection
))
297 if (NextSegmentLoad
<LC_SEGMENT
, struct segment_command
>(
298 start
, end
, offset
, filename
, filename_size
, protection
))
304 // If we get here, no more load_cmd's in this image talk about
305 // segments. Go on to the next image.
310 bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr
, uptr
*offset
,
314 return IterateForObjectNameAndOffset(addr
, offset
, filename
, filename_size
,
318 BlockingMutex::BlockingMutex(LinkerInitialized
) {
319 // We assume that OS_SPINLOCK_INIT is zero
322 BlockingMutex::BlockingMutex() {
323 internal_memset(this, 0, sizeof(*this));
326 void BlockingMutex::Lock() {
327 CHECK(sizeof(OSSpinLock
) <= sizeof(opaque_storage_
));
328 CHECK_EQ(OS_SPINLOCK_INIT
, 0);
329 CHECK_NE(owner_
, (uptr
)pthread_self());
330 OSSpinLockLock((OSSpinLock
*)&opaque_storage_
);
332 owner_
= (uptr
)pthread_self();
335 void BlockingMutex::Unlock() {
336 CHECK(owner_
== (uptr
)pthread_self());
338 OSSpinLockUnlock((OSSpinLock
*)&opaque_storage_
);
341 void BlockingMutex::CheckLocked() {
342 CHECK_EQ((uptr
)pthread_self(), owner_
);
356 void GetThreadStackAndTls(bool main
, uptr
*stk_addr
, uptr
*stk_size
,
357 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
;
373 uptr
GetListOfModules(LoadedModule
*modules
, uptr max_modules
,
374 string_predicate_t filter
) {
375 MemoryMappingLayout
memory_mapping(false);
376 memory_mapping
.Reset();
377 uptr cur_beg
, cur_end
, cur_offset
;
378 InternalScopedBuffer
<char> module_name(kMaxPathLength
);
381 n_modules
< max_modules
&&
382 memory_mapping
.Next(&cur_beg
, &cur_end
, &cur_offset
,
383 module_name
.data(), module_name
.size(), 0);
385 const char *cur_name
= module_name
.data();
386 if (cur_name
[0] == '\0')
388 if (filter
&& !filter(cur_name
))
390 LoadedModule
*cur_module
= 0;
392 0 == internal_strcmp(cur_name
, modules
[n_modules
- 1].full_name())) {
393 cur_module
= &modules
[n_modules
- 1];
395 void *mem
= &modules
[n_modules
];
396 cur_module
= new(mem
) LoadedModule(cur_name
, cur_beg
);
399 cur_module
->addAddressRange(cur_beg
, cur_end
);
404 } // namespace __sanitizer
406 #endif // SANITIZER_MAC