Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | //===-- sanitizer_procmaps_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 | // Information about the process mappings (Mac-specific parts). |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "sanitizer_platform.h" |
| 14 | #if SANITIZER_MAC |
| 15 | #include "sanitizer_common.h" |
| 16 | #include "sanitizer_placement_new.h" |
| 17 | #include "sanitizer_procmaps.h" |
| 18 | |
| 19 | #include <mach-o/dyld.h> |
| 20 | #include <mach-o/loader.h> |
| 21 | |
| 22 | namespace __sanitizer { |
| 23 | |
| 24 | MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) { |
| 25 | Reset(); |
| 26 | } |
| 27 | |
| 28 | MemoryMappingLayout::~MemoryMappingLayout() { |
| 29 | } |
| 30 | |
| 31 | // More information about Mach-O headers can be found in mach-o/loader.h |
| 32 | // Each Mach-O image has a header (mach_header or mach_header_64) starting with |
| 33 | // a magic number, and a list of linker load commands directly following the |
| 34 | // header. |
| 35 | // A load command is at least two 32-bit words: the command type and the |
| 36 | // command size in bytes. We're interested only in segment load commands |
| 37 | // (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped |
| 38 | // into the task's address space. |
| 39 | // The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or |
| 40 | // segment_command_64 correspond to the memory address, memory size and the |
| 41 | // file offset of the current memory segment. |
| 42 | // Because these fields are taken from the images as is, one needs to add |
| 43 | // _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime. |
| 44 | |
| 45 | void MemoryMappingLayout::Reset() { |
| 46 | // Count down from the top. |
| 47 | // TODO(glider): as per man 3 dyld, iterating over the headers with |
| 48 | // _dyld_image_count is thread-unsafe. We need to register callbacks for |
| 49 | // adding and removing images which will invalidate the MemoryMappingLayout |
| 50 | // state. |
| 51 | current_image_ = _dyld_image_count(); |
| 52 | current_load_cmd_count_ = -1; |
| 53 | current_load_cmd_addr_ = 0; |
| 54 | current_magic_ = 0; |
| 55 | current_filetype_ = 0; |
| 56 | } |
| 57 | |
| 58 | // static |
| 59 | void MemoryMappingLayout::CacheMemoryMappings() { |
| 60 | // No-op on Mac for now. |
| 61 | } |
| 62 | |
| 63 | void MemoryMappingLayout::LoadFromCache() { |
| 64 | // No-op on Mac for now. |
| 65 | } |
| 66 | |
| 67 | // Next and NextSegmentLoad were inspired by base/sysinfo.cc in |
| 68 | // Google Perftools, http://code.google.com/p/google-perftools. |
| 69 | |
| 70 | // NextSegmentLoad scans the current image for the next segment load command |
| 71 | // and returns the start and end addresses and file offset of the corresponding |
| 72 | // segment. |
| 73 | // Note that the segment addresses are not necessarily sorted. |
| 74 | template<u32 kLCSegment, typename SegmentCommand> |
| 75 | bool MemoryMappingLayout::NextSegmentLoad( |
| 76 | uptr *start, uptr *end, uptr *offset, |
| 77 | char filename[], uptr filename_size, uptr *protection) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 78 | const char* lc = current_load_cmd_addr_; |
| 79 | current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize; |
| 80 | if (((const load_command *)lc)->cmd == kLCSegment) { |
| 81 | const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_); |
| 82 | const SegmentCommand* sc = (const SegmentCommand *)lc; |
| 83 | if (start) *start = sc->vmaddr + dlloff; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 84 | if (protection) { |
| 85 | // Return the initial protection. |
| 86 | *protection = sc->initprot; |
| 87 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 88 | if (end) *end = sc->vmaddr + sc->vmsize + dlloff; |
| 89 | if (offset) { |
| 90 | if (current_filetype_ == /*MH_EXECUTE*/ 0x2) { |
| 91 | *offset = sc->vmaddr; |
| 92 | } else { |
| 93 | *offset = sc->fileoff; |
| 94 | } |
| 95 | } |
| 96 | if (filename) { |
| 97 | internal_strncpy(filename, _dyld_get_image_name(current_image_), |
| 98 | filename_size); |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset, |
| 106 | char filename[], uptr filename_size, |
| 107 | uptr *protection) { |
| 108 | for (; current_image_ >= 0; current_image_--) { |
| 109 | const mach_header* hdr = _dyld_get_image_header(current_image_); |
| 110 | if (!hdr) continue; |
| 111 | if (current_load_cmd_count_ < 0) { |
| 112 | // Set up for this image; |
| 113 | current_load_cmd_count_ = hdr->ncmds; |
| 114 | current_magic_ = hdr->magic; |
| 115 | current_filetype_ = hdr->filetype; |
| 116 | switch (current_magic_) { |
| 117 | #ifdef MH_MAGIC_64 |
| 118 | case MH_MAGIC_64: { |
| 119 | current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64); |
| 120 | break; |
| 121 | } |
| 122 | #endif |
| 123 | case MH_MAGIC: { |
| 124 | current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header); |
| 125 | break; |
| 126 | } |
| 127 | default: { |
| 128 | continue; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) { |
| 134 | switch (current_magic_) { |
| 135 | // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64. |
| 136 | #ifdef MH_MAGIC_64 |
| 137 | case MH_MAGIC_64: { |
| 138 | if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>( |
| 139 | start, end, offset, filename, filename_size, protection)) |
| 140 | return true; |
| 141 | break; |
| 142 | } |
| 143 | #endif |
| 144 | case MH_MAGIC: { |
| 145 | if (NextSegmentLoad<LC_SEGMENT, struct segment_command>( |
| 146 | start, end, offset, filename, filename_size, protection)) |
| 147 | return true; |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | // If we get here, no more load_cmd's in this image talk about |
| 153 | // segments. Go on to the next image. |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | uptr MemoryMappingLayout::DumpListOfModules(LoadedModule *modules, |
| 159 | uptr max_modules, |
| 160 | string_predicate_t filter) { |
| 161 | Reset(); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 162 | uptr cur_beg, cur_end, prot; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 163 | InternalScopedString module_name(kMaxPathLength); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 164 | uptr n_modules = 0; |
| 165 | for (uptr i = 0; n_modules < max_modules && |
| 166 | Next(&cur_beg, &cur_end, 0, module_name.data(), |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 167 | module_name.size(), &prot); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 168 | i++) { |
| 169 | const char *cur_name = module_name.data(); |
| 170 | if (cur_name[0] == '\0') |
| 171 | continue; |
| 172 | if (filter && !filter(cur_name)) |
| 173 | continue; |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 174 | LoadedModule *cur_module = nullptr; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 175 | if (n_modules > 0 && |
| 176 | 0 == internal_strcmp(cur_name, modules[n_modules - 1].full_name())) { |
| 177 | cur_module = &modules[n_modules - 1]; |
| 178 | } else { |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 179 | cur_module = &modules[n_modules]; |
| 180 | cur_module->set(cur_name, cur_beg); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 181 | n_modules++; |
| 182 | } |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 183 | cur_module->addAddressRange(cur_beg, cur_end, prot & kProtectionExecute); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 184 | } |
| 185 | return n_modules; |
| 186 | } |
| 187 | |
| 188 | } // namespace __sanitizer |
| 189 | |
| 190 | #endif // SANITIZER_MAC |