blob: 4dc8686f7625bfef1a44948d487acfdacd9c2932 [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 Samsonov4b1f1032012-06-07 07:13:46 +000022#include <fcntl.h>
Alexey Samsonov28a98952012-06-07 06:15:12 +000023#include <mach-o/dyld.h>
24#include <mach-o/loader.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000025#include <pthread.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000026#include <sys/mman.h>
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000027#include <sys/resource.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000028#include <sys/stat.h>
29#include <sys/types.h>
Alexey Samsonov03c8b842012-06-05 08:32:53 +000030#include <unistd.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000031
32namespace __sanitizer {
33
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000034// ---------------------- sanitizer_libc.h
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000035void *internal_mmap(void *addr, size_t length, int prot, int flags,
36 int fd, u64 offset) {
37 return mmap(addr, length, prot, flags, fd, offset);
38}
39
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000040int internal_munmap(void *addr, uptr length) {
41 return munmap(addr, length);
42}
43
Alexey Samsonov03c8b842012-06-05 08:32:53 +000044int internal_close(fd_t fd) {
45 return close(fd);
46}
47
Alexey Samsonovdde1f112012-06-05 07:05:10 +000048fd_t internal_open(const char *filename, bool write) {
49 return open(filename,
Alexey Samsonov3768b582012-06-05 14:07:11 +000050 write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000051}
52
Alexey Samsonov03c8b842012-06-05 08:32:53 +000053uptr internal_read(fd_t fd, void *buf, uptr count) {
54 return read(fd, buf, count);
55}
56
57uptr internal_write(fd_t fd, const void *buf, uptr count) {
58 return write(fd, buf, count);
59}
60
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000061uptr internal_filesize(fd_t fd) {
62 struct stat st = {};
63 if (fstat(fd, &st))
64 return -1;
65 return (uptr)st.st_size;
66}
67
68int internal_dup2(int oldfd, int newfd) {
69 return dup2(oldfd, newfd);
70}
71
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000072// ----------------- sanitizer_common.h
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000073void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000074 uptr *stack_bottom) {
75 CHECK(stack_top);
76 CHECK(stack_bottom);
77 uptr stacksize = pthread_get_stacksize_np(pthread_self());
78 void *stackaddr = pthread_get_stackaddr_np(pthread_self());
79 *stack_top = (uptr)stackaddr;
Alexey Samsonovcf4d3a02012-06-07 07:32:00 +000080 *stack_bottom = *stack_top - stacksize;
Alexey Samsonov4b1f1032012-06-07 07:13:46 +000081}
82
83
84// ----------------- sanitizer_procmaps.h
Alexey Samsonov28a98952012-06-07 06:15:12 +000085
86ProcessMaps::ProcessMaps() {
87 Reset();
88}
89
90ProcessMaps::~ProcessMaps() {
91}
92
93// More information about Mach-O headers can be found in mach-o/loader.h
94// Each Mach-O image has a header (mach_header or mach_header_64) starting with
95// a magic number, and a list of linker load commands directly following the
96// header.
97// A load command is at least two 32-bit words: the command type and the
98// command size in bytes. We're interested only in segment load commands
99// (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
100// into the task's address space.
101// The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
102// segment_command_64 correspond to the memory address, memory size and the
103// file offset of the current memory segment.
104// Because these fields are taken from the images as is, one needs to add
105// _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
106
107void ProcessMaps::Reset() {
108 // Count down from the top.
109 // TODO(glider): as per man 3 dyld, iterating over the headers with
110 // _dyld_image_count is thread-unsafe. We need to register callbacks for
111 // adding and removing images which will invalidate the ProcessMaps state.
112 current_image_ = _dyld_image_count();
113 current_load_cmd_count_ = -1;
114 current_load_cmd_addr_ = 0;
115 current_magic_ = 0;
116}
117
118// Next and NextSegmentLoad were inspired by base/sysinfo.cc in
119// Google Perftools, http://code.google.com/p/google-perftools.
120
121// NextSegmentLoad scans the current image for the next segment load command
122// and returns the start and end addresses and file offset of the corresponding
123// segment.
124// Note that the segment addresses are not necessarily sorted.
125template<u32 kLCSegment, typename SegmentCommand>
126bool ProcessMaps::NextSegmentLoad(
127 uptr *start, uptr *end, uptr *offset,
128 char filename[], uptr filename_size) {
129 const char* lc = current_load_cmd_addr_;
130 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
131 if (((const load_command *)lc)->cmd == kLCSegment) {
132 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
133 const SegmentCommand* sc = (const SegmentCommand *)lc;
134 if (start) *start = sc->vmaddr + dlloff;
135 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
136 if (offset) *offset = sc->fileoff;
137 if (filename) {
138 internal_strncpy(filename, _dyld_get_image_name(current_image_),
139 filename_size);
140 }
141 return true;
142 }
143 return false;
144}
145
146bool ProcessMaps::Next(uptr *start, uptr *end, uptr *offset,
147 char filename[], uptr filename_size) {
148 for (; current_image_ >= 0; current_image_--) {
149 const mach_header* hdr = _dyld_get_image_header(current_image_);
150 if (!hdr) continue;
151 if (current_load_cmd_count_ < 0) {
152 // Set up for this image;
153 current_load_cmd_count_ = hdr->ncmds;
154 current_magic_ = hdr->magic;
155 switch (current_magic_) {
156#ifdef MH_MAGIC_64
157 case MH_MAGIC_64: {
158 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
159 break;
160 }
161#endif
162 case MH_MAGIC: {
163 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
164 break;
165 }
166 default: {
167 continue;
168 }
169 }
170 }
171
172 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
173 switch (current_magic_) {
174 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
175#ifdef MH_MAGIC_64
176 case MH_MAGIC_64: {
177 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
178 start, end, offset, filename, filename_size))
179 return true;
180 break;
181 }
182#endif
183 case MH_MAGIC: {
184 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
185 start, end, offset, filename, filename_size))
186 return true;
187 break;
188 }
189 }
190 }
191 // If we get here, no more load_cmd's in this image talk about
192 // segments. Go on to the next image.
193 }
194 return false;
195}
196
197bool ProcessMaps::GetObjectNameAndOffset(uptr addr, uptr *offset,
198 char filename[],
199 uptr filename_size) {
200 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
201}
202
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000203} // namespace __sanitizer
204
205#endif // __APPLE__