blob: 986f31ff6807e1e9c6ab2fad637f80f2d2dba613 [file] [log] [blame]
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +00001//===-- 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//===----------------------------------------------------------------------===//
14
15#ifdef __APPLE__
16
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000017#include "sanitizer_common.h"
Alexey Samsonov5bbf8292012-06-05 14:25:27 +000018#include "sanitizer_internal_defs.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000019#include "sanitizer_libc.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000020#include "sanitizer_procmaps.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000021
Alexey Samsonov0c53a382012-06-14 14:07:21 +000022#include <crt_externs.h> // for _NSGetEnviron
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000023#include <fcntl.h>
Alexey Samsonov28a98952012-06-07 06:15:12 +000024#include <mach-o/dyld.h>
25#include <mach-o/loader.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000026#include <pthread.h>
Alexey Samsonov58a3c582012-06-18 08:44:30 +000027#include <sched.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000028#include <sys/mman.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000029#include <sys/resource.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000030#include <sys/stat.h>
31#include <sys/types.h>
Alexey Samsonov03c8b842012-06-05 08:32:53 +000032#include <unistd.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000033
34namespace __sanitizer {
35
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000036// ---------------------- sanitizer_libc.h
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000037void *internal_mmap(void *addr, size_t length, int prot, int flags,
38 int fd, u64 offset) {
39 return mmap(addr, length, prot, flags, fd, offset);
40}
41
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000042int internal_munmap(void *addr, uptr length) {
43 return munmap(addr, length);
44}
45
Alexey Samsonov03c8b842012-06-05 08:32:53 +000046int internal_close(fd_t fd) {
47 return close(fd);
48}
49
Alexey Samsonovdde1f112012-06-05 07:05:10 +000050fd_t internal_open(const char *filename, bool write) {
51 return open(filename,
Alexey Samsonov3768b582012-06-05 14:07:11 +000052 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000053}
54
Alexey Samsonov03c8b842012-06-05 08:32:53 +000055uptr internal_read(fd_t fd, void *buf, uptr count) {
56 return read(fd, buf, count);
57}
58
59uptr internal_write(fd_t fd, const void *buf, uptr count) {
60 return write(fd, buf, count);
61}
62
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000063uptr internal_filesize(fd_t fd) {
Alexey Samsonovce8d4972012-08-02 10:09:31 +000064 struct stat st;
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000065 if (fstat(fd, &st))
66 return -1;
67 return (uptr)st.st_size;
68}
69
70int internal_dup2(int oldfd, int newfd) {
71 return dup2(oldfd, newfd);
72}
73
Alexey Samsonov58a3c582012-06-18 08:44:30 +000074int internal_sched_yield() {
75 return sched_yield();
76}
77
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000078// ----------------- sanitizer_common.h
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000079void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000080 uptr *stack_bottom) {
81 CHECK(stack_top);
82 CHECK(stack_bottom);
83 uptr stacksize = pthread_get_stacksize_np(pthread_self());
84 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
85 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000086 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000087}
88
Alexey Samsonov0c53a382012-06-14 14:07:21 +000089const char *GetEnv(const char *name) {
90 char ***env_ptr = _NSGetEnviron();
91 CHECK(env_ptr);
92 char **environ = *env_ptr;
93 CHECK(environ);
94 uptr name_len = internal_strlen(name);
95 while (*environ != 0) {
96 uptr len = internal_strlen(*environ);
97 if (len > name_len) {
98 const char *p = *environ;
99 if (!internal_memcmp(p, name, name_len) &&
100 p[name_len] == '=') { // Match.
101 return *environ + name_len + 1; // String starting after =.
102 }
103 }
104 environ++;
105 }
106 return 0;
107}
Alexey Samsonov4b1f1032012-06-07 07:13:46 +0000108
109// ----------------- sanitizer_procmaps.h
Alexey Samsonov28a98952012-06-07 06:15:12 +0000110
Alexey Samsonovcc622112012-08-27 13:48:48 +0000111MemoryMappingLayout::MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000112 Reset();
113}
114
Alexey Samsonovcc622112012-08-27 13:48:48 +0000115MemoryMappingLayout::~MemoryMappingLayout() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000116}
117
118// More information about Mach-O headers can be found in mach-o/loader.h
119// Each Mach-O image has a header (mach_header or mach_header_64) starting with
120// a magic number, and a list of linker load commands directly following the
121// header.
122// A load command is at least two 32-bit words: the command type and the
123// command size in bytes. We're interested only in segment load commands
124// (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
125// into the task's address space.
126// The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
127// segment_command_64 correspond to the memory address, memory size and the
128// file offset of the current memory segment.
129// Because these fields are taken from the images as is, one needs to add
130// _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
131
Alexey Samsonovcc622112012-08-27 13:48:48 +0000132void MemoryMappingLayout::Reset() {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000133 // Count down from the top.
134 // TODO(glider): as per man 3 dyld, iterating over the headers with
135 // _dyld_image_count is thread-unsafe. We need to register callbacks for
Alexey Samsonovcc622112012-08-27 13:48:48 +0000136 // adding and removing images which will invalidate the MemoryMappingLayout
137 // state.
Alexey Samsonov28a98952012-06-07 06:15:12 +0000138 current_image_ = _dyld_image_count();
139 current_load_cmd_count_ = -1;
140 current_load_cmd_addr_ = 0;
141 current_magic_ = 0;
142}
143
144// Next and NextSegmentLoad were inspired by base/sysinfo.cc in
145// Google Perftools, http://code.google.com/p/google-perftools.
146
147// NextSegmentLoad scans the current image for the next segment load command
148// and returns the start and end addresses and file offset of the corresponding
149// segment.
150// Note that the segment addresses are not necessarily sorted.
151template<u32 kLCSegment, typename SegmentCommand>
Alexey Samsonovcc622112012-08-27 13:48:48 +0000152bool MemoryMappingLayout::NextSegmentLoad(
Alexey Samsonov28a98952012-06-07 06:15:12 +0000153 uptr *start, uptr *end, uptr *offset,
154 char filename[], uptr filename_size) {
155 const char* lc = current_load_cmd_addr_;
156 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
157 if (((const load_command *)lc)->cmd == kLCSegment) {
158 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
159 const SegmentCommand* sc = (const SegmentCommand *)lc;
160 if (start) *start = sc->vmaddr + dlloff;
161 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
162 if (offset) *offset = sc->fileoff;
163 if (filename) {
164 internal_strncpy(filename, _dyld_get_image_name(current_image_),
165 filename_size);
166 }
167 return true;
168 }
169 return false;
170}
171
Alexey Samsonovcc622112012-08-27 13:48:48 +0000172bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
173 char filename[], uptr filename_size) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000174 for (; current_image_ >= 0; current_image_--) {
175 const mach_header* hdr = _dyld_get_image_header(current_image_);
176 if (!hdr) continue;
177 if (current_load_cmd_count_ < 0) {
178 // Set up for this image;
179 current_load_cmd_count_ = hdr->ncmds;
180 current_magic_ = hdr->magic;
181 switch (current_magic_) {
182#ifdef MH_MAGIC_64
183 case MH_MAGIC_64: {
184 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
185 break;
186 }
187#endif
188 case MH_MAGIC: {
189 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
190 break;
191 }
192 default: {
193 continue;
194 }
195 }
196 }
197
198 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
199 switch (current_magic_) {
200 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
201#ifdef MH_MAGIC_64
202 case MH_MAGIC_64: {
203 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
204 start, end, offset, filename, filename_size))
205 return true;
206 break;
207 }
208#endif
209 case MH_MAGIC: {
210 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
211 start, end, offset, filename, filename_size))
212 return true;
213 break;
214 }
215 }
216 }
217 // If we get here, no more load_cmd's in this image talk about
218 // segments. Go on to the next image.
219 }
220 return false;
221}
222
Alexey Samsonovcc622112012-08-27 13:48:48 +0000223bool MemoryMappingLayout::GetObjectNameAndOffset(uptr addr, uptr *offset,
224 char filename[],
225 uptr filename_size) {
Alexey Samsonov28a98952012-06-07 06:15:12 +0000226 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
227}
228
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000229} // namespace __sanitizer
230
231#endif // __APPLE__