[Sanitizer] extend internal libc with stat/fstat/lstat functions
[blocksruntime.git] / lib / sanitizer_common / sanitizer_mac.cc
blob1bd50ca1d0601414334afe2e8e7d148eff4dd25f
1 //===-- 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 //===----------------------------------------------------------------------===//
15 #ifdef __APPLE__
17 #include "sanitizer_common.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_libc.h"
20 #include "sanitizer_procmaps.h"
22 #include <crt_externs.h> // for _NSGetEnviron
23 #include <fcntl.h>
24 #include <mach-o/dyld.h>
25 #include <mach-o/loader.h>
26 #include <pthread.h>
27 #include <sched.h>
28 #include <sys/mman.h>
29 #include <sys/resource.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <libkern/OSAtomic.h>
35 namespace __sanitizer {
37 // ---------------------- sanitizer_libc.h
38 void *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);
43 int internal_munmap(void *addr, uptr length) {
44 return munmap(addr, length);
47 int internal_close(fd_t fd) {
48 return close(fd);
51 fd_t internal_open(const char *filename, int flags) {
52 return open(filename, flags);
55 fd_t internal_open(const char *filename, int flags, u32 mode) {
56 return open(filename, flags, mode);
59 fd_t OpenFile(const char *filename, bool write) {
60 return internal_open(filename,
61 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
64 uptr internal_read(fd_t fd, void *buf, uptr count) {
65 return read(fd, buf, count);
68 uptr internal_write(fd_t fd, const void *buf, uptr count) {
69 return write(fd, buf, count);
72 int internal_stat(const char *path, void *buf) {
73 return stat(path, buf);
76 int internal_lstat(const char *path, void *buf) {
77 return lstat(path, buf);
80 int internal_fstat(fd_t fd, void *buf) {
81 return fstat(fd, buf);
84 uptr internal_filesize(fd_t fd) {
85 struct stat st;
86 if (internal_fstat(fd, &st))
87 return -1;
88 return (uptr)st.st_size;
91 int internal_dup2(int oldfd, int newfd) {
92 return dup2(oldfd, newfd);
95 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
96 return readlink(path, buf, bufsize);
99 int internal_sched_yield() {
100 return sched_yield();
103 // ----------------- sanitizer_common.h
104 bool FileExists(const char *filename) {
105 struct stat st;
106 if (stat(filename, &st))
107 return false;
108 // Sanity check: filename is a regular file.
109 return S_ISREG(st.st_mode);
112 uptr GetTid() {
113 return reinterpret_cast<uptr>(pthread_self());
116 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
117 uptr *stack_bottom) {
118 CHECK(stack_top);
119 CHECK(stack_bottom);
120 uptr stacksize = pthread_get_stacksize_np(pthread_self());
121 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
122 *stack_top = (uptr)stackaddr;
123 *stack_bottom = *stack_top - stacksize;
126 const char *GetEnv(const char *name) {
127 char ***env_ptr = _NSGetEnviron();
128 CHECK(env_ptr);
129 char **environ = *env_ptr;
130 CHECK(environ);
131 uptr name_len = internal_strlen(name);
132 while (*environ != 0) {
133 uptr len = internal_strlen(*environ);
134 if (len > name_len) {
135 const char *p = *environ;
136 if (!internal_memcmp(p, name, name_len) &&
137 p[name_len] == '=') { // Match.
138 return *environ + name_len + 1; // String starting after =.
141 environ++;
143 return 0;
146 void ReExec() {
147 UNIMPLEMENTED();
150 void PrepareForSandboxing() {
151 // Nothing here for now.
154 // ----------------- sanitizer_procmaps.h
156 MemoryMappingLayout::MemoryMappingLayout() {
157 Reset();
160 MemoryMappingLayout::~MemoryMappingLayout() {
163 // More information about Mach-O headers can be found in mach-o/loader.h
164 // Each Mach-O image has a header (mach_header or mach_header_64) starting with
165 // a magic number, and a list of linker load commands directly following the
166 // header.
167 // A load command is at least two 32-bit words: the command type and the
168 // command size in bytes. We're interested only in segment load commands
169 // (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
170 // into the task's address space.
171 // The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
172 // segment_command_64 correspond to the memory address, memory size and the
173 // file offset of the current memory segment.
174 // Because these fields are taken from the images as is, one needs to add
175 // _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
177 void MemoryMappingLayout::Reset() {
178 // Count down from the top.
179 // TODO(glider): as per man 3 dyld, iterating over the headers with
180 // _dyld_image_count is thread-unsafe. We need to register callbacks for
181 // adding and removing images which will invalidate the MemoryMappingLayout
182 // state.
183 current_image_ = _dyld_image_count();
184 current_load_cmd_count_ = -1;
185 current_load_cmd_addr_ = 0;
186 current_magic_ = 0;
187 current_filetype_ = 0;
190 // static
191 void MemoryMappingLayout::CacheMemoryMappings() {
192 // No-op on Mac for now.
195 void MemoryMappingLayout::LoadFromCache() {
196 // No-op on Mac for now.
199 // Next and NextSegmentLoad were inspired by base/sysinfo.cc in
200 // Google Perftools, http://code.google.com/p/google-perftools.
202 // NextSegmentLoad scans the current image for the next segment load command
203 // and returns the start and end addresses and file offset of the corresponding
204 // segment.
205 // Note that the segment addresses are not necessarily sorted.
206 template<u32 kLCSegment, typename SegmentCommand>
207 bool MemoryMappingLayout::NextSegmentLoad(
208 uptr *start, uptr *end, uptr *offset,
209 char filename[], uptr filename_size) {
210 const char* lc = current_load_cmd_addr_;
211 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
212 if (((const load_command *)lc)->cmd == kLCSegment) {
213 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
214 const SegmentCommand* sc = (const SegmentCommand *)lc;
215 if (start) *start = sc->vmaddr + dlloff;
216 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
217 if (offset) {
218 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
219 *offset = sc->vmaddr;
220 } else {
221 *offset = sc->fileoff;
224 if (filename) {
225 internal_strncpy(filename, _dyld_get_image_name(current_image_),
226 filename_size);
228 return true;
230 return false;
233 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
234 char filename[], uptr filename_size) {
235 for (; current_image_ >= 0; current_image_--) {
236 const mach_header* hdr = _dyld_get_image_header(current_image_);
237 if (!hdr) continue;
238 if (current_load_cmd_count_ < 0) {
239 // Set up for this image;
240 current_load_cmd_count_ = hdr->ncmds;
241 current_magic_ = hdr->magic;
242 current_filetype_ = hdr->filetype;
243 switch (current_magic_) {
244 #ifdef MH_MAGIC_64
245 case MH_MAGIC_64: {
246 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
247 break;
249 #endif
250 case MH_MAGIC: {
251 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
252 break;
254 default: {
255 continue;
260 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
261 switch (current_magic_) {
262 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
263 #ifdef MH_MAGIC_64
264 case MH_MAGIC_64: {
265 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
266 start, end, offset, filename, filename_size))
267 return true;
268 break;
270 #endif
271 case MH_MAGIC: {
272 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
273 start, end, offset, filename, filename_size))
274 return true;
275 break;
279 // If we get here, no more load_cmd's in this image talk about
280 // segments. Go on to the next image.
282 return false;
285 bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
286 char filename[],
287 uptr filename_size) {
288 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
291 BlockingMutex::BlockingMutex(LinkerInitialized) {
292 // We assume that OS_SPINLOCK_INIT is zero
295 void BlockingMutex::Lock() {
296 CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
297 CHECK(OS_SPINLOCK_INIT == 0);
298 CHECK(owner_ != (uptr)pthread_self());
299 OSSpinLockLock((OSSpinLock*)&opaque_storage_);
300 CHECK(!owner_);
301 owner_ = (uptr)pthread_self();
304 void BlockingMutex::Unlock() {
305 CHECK(owner_ == (uptr)pthread_self());
306 owner_ = 0;
307 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
310 } // namespace __sanitizer
312 #endif // __APPLE__