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