Fix spacing.
[gcc.git] / libsanitizer / sanitizer_common / sanitizer_mac.cc
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 //===----------------------------------------------------------------------===//
12
13 #include "sanitizer_platform.h"
14 #if SANITIZER_MAC
15
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
20 #endif
21 #include <stdio.h>
22
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"
28
29 #include <crt_externs.h> // for _NSGetEnviron
30 #include <fcntl.h>
31 #include <mach-o/dyld.h>
32 #include <mach-o/loader.h>
33 #include <pthread.h>
34 #include <sched.h>
35 #include <sys/mman.h>
36 #include <sys/resource.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <libkern/OSAtomic.h>
41 #include <errno.h>
42
43 namespace __sanitizer {
44
45 #include "sanitizer_syscall_generic.inc"
46
47 // ---------------------- sanitizer_libc.h
48 uptr internal_mmap(void *addr, size_t length, int prot, int flags,
49 int fd, u64 offset) {
50 return (uptr)mmap(addr, length, prot, flags, fd, offset);
51 }
52
53 uptr internal_munmap(void *addr, uptr length) {
54 return munmap(addr, length);
55 }
56
57 uptr internal_close(fd_t fd) {
58 return close(fd);
59 }
60
61 uptr internal_open(const char *filename, int flags) {
62 return open(filename, flags);
63 }
64
65 uptr internal_open(const char *filename, int flags, u32 mode) {
66 return open(filename, flags, mode);
67 }
68
69 uptr OpenFile(const char *filename, bool write) {
70 return internal_open(filename,
71 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
72 }
73
74 uptr internal_read(fd_t fd, void *buf, uptr count) {
75 return read(fd, buf, count);
76 }
77
78 uptr internal_write(fd_t fd, const void *buf, uptr count) {
79 return write(fd, buf, count);
80 }
81
82 uptr internal_stat(const char *path, void *buf) {
83 return stat(path, (struct stat *)buf);
84 }
85
86 uptr internal_lstat(const char *path, void *buf) {
87 return lstat(path, (struct stat *)buf);
88 }
89
90 uptr internal_fstat(fd_t fd, void *buf) {
91 return fstat(fd, (struct stat *)buf);
92 }
93
94 uptr internal_filesize(fd_t fd) {
95 struct stat st;
96 if (internal_fstat(fd, &st))
97 return -1;
98 return (uptr)st.st_size;
99 }
100
101 uptr internal_dup2(int oldfd, int newfd) {
102 return dup2(oldfd, newfd);
103 }
104
105 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
106 return readlink(path, buf, bufsize);
107 }
108
109 uptr internal_sched_yield() {
110 return sched_yield();
111 }
112
113 void internal__exit(int exitcode) {
114 _exit(exitcode);
115 }
116
117 uptr internal_getpid() {
118 return getpid();
119 }
120
121 // ----------------- sanitizer_common.h
122 bool FileExists(const char *filename) {
123 struct stat st;
124 if (stat(filename, &st))
125 return false;
126 // Sanity check: filename is a regular file.
127 return S_ISREG(st.st_mode);
128 }
129
130 uptr GetTid() {
131 return reinterpret_cast<uptr>(pthread_self());
132 }
133
134 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
135 uptr *stack_bottom) {
136 CHECK(stack_top);
137 CHECK(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;
142 }
143
144 const char *GetEnv(const char *name) {
145 char ***env_ptr = _NSGetEnviron();
146 CHECK(env_ptr);
147 char **environ = *env_ptr;
148 CHECK(environ);
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 =.
157 }
158 }
159 environ++;
160 }
161 return 0;
162 }
163
164 void ReExec() {
165 UNIMPLEMENTED();
166 }
167
168 void PrepareForSandboxing() {
169 // Nothing here for now.
170 }
171
172 uptr GetPageSize() {
173 return sysconf(_SC_PAGESIZE);
174 }
175
176 // ----------------- sanitizer_procmaps.h
177
178 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
179 Reset();
180 }
181
182 MemoryMappingLayout::~MemoryMappingLayout() {
183 }
184
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
188 // header.
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.
198
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
204 // state.
205 current_image_ = _dyld_image_count();
206 current_load_cmd_count_ = -1;
207 current_load_cmd_addr_ = 0;
208 current_magic_ = 0;
209 current_filetype_ = 0;
210 }
211
212 // static
213 void MemoryMappingLayout::CacheMemoryMappings() {
214 // No-op on Mac for now.
215 }
216
217 void MemoryMappingLayout::LoadFromCache() {
218 // No-op on Mac for now.
219 }
220
221 // Next and NextSegmentLoad were inspired by base/sysinfo.cc in
222 // Google Perftools, http://code.google.com/p/google-perftools.
223
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
226 // segment.
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) {
232 if (protection)
233 UNIMPLEMENTED();
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;
241 if (offset) {
242 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
243 *offset = sc->vmaddr;
244 } else {
245 *offset = sc->fileoff;
246 }
247 }
248 if (filename) {
249 internal_strncpy(filename, _dyld_get_image_name(current_image_),
250 filename_size);
251 }
252 return true;
253 }
254 return false;
255 }
256
257 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
258 char filename[], uptr filename_size,
259 uptr *protection) {
260 for (; current_image_ >= 0; current_image_--) {
261 const mach_header* hdr = _dyld_get_image_header(current_image_);
262 if (!hdr) continue;
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_) {
269 #ifdef MH_MAGIC_64
270 case MH_MAGIC_64: {
271 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
272 break;
273 }
274 #endif
275 case MH_MAGIC: {
276 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
277 break;
278 }
279 default: {
280 continue;
281 }
282 }
283 }
284
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.
288 #ifdef MH_MAGIC_64
289 case MH_MAGIC_64: {
290 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
291 start, end, offset, filename, filename_size, protection))
292 return true;
293 break;
294 }
295 #endif
296 case MH_MAGIC: {
297 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
298 start, end, offset, filename, filename_size, protection))
299 return true;
300 break;
301 }
302 }
303 }
304 // If we get here, no more load_cmd's in this image talk about
305 // segments. Go on to the next image.
306 }
307 return false;
308 }
309
310 bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
311 char filename[],
312 uptr filename_size,
313 uptr *protection) {
314 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size,
315 protection);
316 }
317
318 BlockingMutex::BlockingMutex(LinkerInitialized) {
319 // We assume that OS_SPINLOCK_INIT is zero
320 }
321
322 BlockingMutex::BlockingMutex() {
323 internal_memset(this, 0, sizeof(*this));
324 }
325
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_);
331 CHECK(!owner_);
332 owner_ = (uptr)pthread_self();
333 }
334
335 void BlockingMutex::Unlock() {
336 CHECK(owner_ == (uptr)pthread_self());
337 owner_ = 0;
338 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
339 }
340
341 void BlockingMutex::CheckLocked() {
342 CHECK_EQ((uptr)pthread_self(), owner_);
343 }
344
345 u64 NanoTime() {
346 return 0;
347 }
348
349 uptr GetTlsSize() {
350 return 0;
351 }
352
353 void InitTlsSize() {
354 }
355
356 void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
357 uptr *tls_addr, uptr *tls_size) {
358 #ifndef SANITIZER_GO
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;
363 *tls_addr = 0;
364 *tls_size = 0;
365 #else
366 *stk_addr = 0;
367 *stk_size = 0;
368 *tls_addr = 0;
369 *tls_size = 0;
370 #endif
371 }
372
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);
379 uptr n_modules = 0;
380 for (uptr i = 0;
381 n_modules < max_modules &&
382 memory_mapping.Next(&cur_beg, &cur_end, &cur_offset,
383 module_name.data(), module_name.size(), 0);
384 i++) {
385 const char *cur_name = module_name.data();
386 if (cur_name[0] == '\0')
387 continue;
388 if (filter && !filter(cur_name))
389 continue;
390 LoadedModule *cur_module = 0;
391 if (n_modules > 0 &&
392 0 == internal_strcmp(cur_name, modules[n_modules - 1].full_name())) {
393 cur_module = &modules[n_modules - 1];
394 } else {
395 void *mem = &modules[n_modules];
396 cur_module = new(mem) LoadedModule(cur_name, cur_beg);
397 n_modules++;
398 }
399 cur_module->addAddressRange(cur_beg, cur_end);
400 }
401 return n_modules;
402 }
403
404 } // namespace __sanitizer
405
406 #endif // SANITIZER_MAC