Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- * * Copyright (c) 2014 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <assert.h> #include "MachOFileAbstraction.hpp" #include "DyldSharedCache.h" #include "CacheBuilder.h" #include "Diagnostics.h" CacheBuilder::CacheBuilder(const DyldSharedCache::CreateOptions& options, const dyld3::closure::FileSystem& fileSystem) : _options(options) , _fileSystem(fileSystem) , _fullAllocatedBuffer(0) , _diagnostics(options.loggingPrefix, options.verbose) , _allocatedBufferSize(0) { } std::string CacheBuilder::errorMessage() { return _diagnostics.errorMessage(); } void CacheBuilder::copyRawSegments() { const bool log = false; dispatch_apply(_sortedDylibs.size(), DISPATCH_APPLY_AUTO, ^(size_t index) { const DylibInfo& dylib = _sortedDylibs[index]; for (const SegmentMappingInfo& info : dylib.cacheLocation) { if (log) fprintf(stderr, "copy %s segment %s (0x%08X bytes) from %p to %p (logical addr 0x%llX) for %s\n", _options.archs->name(), info.segName, info.copySegmentSize, info.srcSegment, info.dstSegment, info.dstCacheUnslidAddress, dylib.input->mappedFile.runtimePath.c_str()); ::memcpy(info.dstSegment, info.srcSegment, info.copySegmentSize); } }); // Copy the coalesced sections const uint64_t numCoalescedSections = sizeof(CacheCoalescedText::SupportedSections) / sizeof(*CacheCoalescedText::SupportedSections); dispatch_apply(numCoalescedSections, DISPATCH_APPLY_AUTO, ^(size_t index) { const CacheCoalescedText::StringSection& cacheStringSection = _coalescedText.getSectionData(CacheCoalescedText::SupportedSections[index]); if (log) fprintf(stderr, "copy %s __TEXT_COAL section %s (0x%08X bytes) to %p (logical addr 0x%llX)\n", _options.archs->name(), CacheCoalescedText::SupportedSections[index], cacheStringSection.bufferSize, cacheStringSection.bufferAddr, cacheStringSection.bufferVMAddr); for (const auto& stringAndOffset : cacheStringSection.stringsToOffsets) ::memcpy(cacheStringSection.bufferAddr + stringAndOffset.second, stringAndOffset.first.data(), stringAndOffset.first.size() + 1); }); } void CacheBuilder::adjustAllImagesForNewSegmentLocations() { __block std::vector<Diagnostics> diags; diags.resize(_sortedDylibs.size()); // Note this cannot to be done in parallel because the LOH Tracker and aslr tracker are not thread safe for (size_t index = 0; index != _sortedDylibs.size(); ++index) { const DylibInfo& dylib = _sortedDylibs[index]; adjustDylibSegments(dylib, diags[index]); } for (const Diagnostics& diag : diags) { if ( diag.hasError() ) { _diagnostics.error("%s", diag.errorMessage().c_str()); break; } } } CacheBuilder::ASLR_Tracker::~ASLR_Tracker() { if ( _bitmap != nullptr ) ::free(_bitmap); } void CacheBuilder::ASLR_Tracker::setDataRegion(const void* rwRegionStart, size_t rwRegionSize) { _pageCount = (unsigned)(rwRegionSize+_pageSize-1)/_pageSize; _regionStart = (uint8_t*)rwRegionStart; _regionEnd = (uint8_t*)rwRegionStart + rwRegionSize; _bitmap = (bool*)calloc(_pageCount*(_pageSize/4)*sizeof(bool), 1); } void CacheBuilder::ASLR_Tracker::add(void* loc) { if (!_enabled) return; uint8_t* p = (uint8_t*)loc; assert(p >= _regionStart); assert(p < _regionEnd); _bitmap[(p-_regionStart)/4] = true; } void CacheBuilder::ASLR_Tracker::remove(void* loc) { if (!_enabled) return; uint8_t* p = (uint8_t*)loc; assert(p >= _regionStart); assert(p < _regionEnd); _bitmap[(p-_regionStart)/4] = false; } bool CacheBuilder::ASLR_Tracker::has(void* loc) { if (!_enabled) return true; uint8_t* p = (uint8_t*)loc; assert(p >= _regionStart); assert(p < _regionEnd); return _bitmap[(p-_regionStart)/4]; } void CacheBuilder::ASLR_Tracker::setHigh8(void* p, uint8_t high8) { _high8Map[p] = high8; } void CacheBuilder::ASLR_Tracker::setAuthData(void* p, uint16_t diversity, bool hasAddrDiv, uint8_t key) { _authDataMap[p] = {diversity, hasAddrDiv, key}; } void CacheBuilder::ASLR_Tracker::setRebaseTarget32(void*p, uint32_t targetVMAddr) { _rebaseTarget32[p] = targetVMAddr; } void CacheBuilder::ASLR_Tracker::setRebaseTarget64(void*p, uint64_t targetVMAddr) { _rebaseTarget64[p] = targetVMAddr; } bool CacheBuilder::ASLR_Tracker::hasHigh8(void* p, uint8_t* highByte) { auto pos = _high8Map.find(p); if ( pos == _high8Map.end() ) return false; *highByte = pos->second; return true; } bool CacheBuilder::ASLR_Tracker::hasAuthData(void* p, uint16_t* diversity, bool* hasAddrDiv, uint8_t* key) { auto pos = _authDataMap.find(p); if ( pos == _authDataMap.end() ) return false; *diversity = pos->second.diversity; *hasAddrDiv = pos->second.addrDiv; *key = pos->second.key; return true; } bool CacheBuilder::ASLR_Tracker::hasRebaseTarget32(void* p, uint32_t* vmAddr) { auto pos = _rebaseTarget32.find(p); if ( pos == _rebaseTarget32.end() ) return false; *vmAddr = pos->second; return true; } bool CacheBuilder::ASLR_Tracker::hasRebaseTarget64(void* p, uint64_t* vmAddr) { auto pos = _rebaseTarget64.find(p); if ( pos == _rebaseTarget64.end() ) return false; *vmAddr = pos->second; return true; } //////////////////////////// DylibTextCoalescer //////////////////////////////////// bool CacheBuilder::DylibTextCoalescer::sectionWasCoalesced(std::string_view sectionName) const { if (sectionName.size() > 16) sectionName = sectionName.substr(0, 16); std::map<std::string_view, const DylibSectionOffsetToCacheSectionOffset*> supportedSections = { { "__objc_classname", &objcClassNames }, { "__objc_methname", &objcMethNames }, { "__objc_methtype", &objcMethTypes } }; auto it = supportedSections.find(sectionName); if (it == supportedSections.end()) return false; return !it->second->empty(); } CacheBuilder::DylibTextCoalescer::DylibSectionOffsetToCacheSectionOffset& CacheBuilder::DylibTextCoalescer::getSectionCoalescer(std::string_view sectionName) { if (sectionName.size() > 16) sectionName = sectionName.substr(0, 16); std::map<std::string_view, DylibSectionOffsetToCacheSectionOffset*> supportedSections = { { "__objc_classname", &objcClassNames }, { "__objc_methname", &objcMethNames }, { "__objc_methtype", &objcMethTypes } }; auto it = supportedSections.find(sectionName); assert(it != supportedSections.end()); return *it->second; } const CacheBuilder::DylibTextCoalescer::DylibSectionOffsetToCacheSectionOffset& CacheBuilder::DylibTextCoalescer::getSectionCoalescer(std::string_view sectionName) const { if (sectionName.size() > 16) sectionName = sectionName.substr(0, 16); std::map<std::string_view, const DylibSectionOffsetToCacheSectionOffset*> supportedSections = { { "__objc_classname", &objcClassNames }, { "__objc_methname", &objcMethNames }, { "__objc_methtype", &objcMethTypes } }; auto it = supportedSections.find(sectionName); assert(it != supportedSections.end()); return *it->second; } //////////////////////////// CacheCoalescedText //////////////////////////////////// const char* CacheBuilder::CacheCoalescedText::SupportedSections[] = { "__objc_classname", "__objc_methname", "__objc_methtype", }; void CacheBuilder::CacheCoalescedText::parseCoalescableText(const dyld3::MachOAnalyzer *ma, DylibTextCoalescer& textCoalescer) { static const bool log = false; // We can only remove sections if we know we have split seg v2 to point to it // Otherwise, a PC relative load in the __TEXT segment wouldn't know how to point to the new strings // which are no longer in the same segment uint32_t splitSegSize = 0; const void* splitSegStart = ma->getSplitSeg(splitSegSize); if (!splitSegStart) return; if ((*(const uint8_t*)splitSegStart) != DYLD_CACHE_ADJ_V2_FORMAT) return; // We can only remove sections from the end of a segment, so cache them all and walk backwards. __block std::vector<std::pair<std::string, dyld3::MachOAnalyzer::SectionInfo>> textSectionInfos; ma->forEachSection(^(const dyld3::MachOAnalyzer::SectionInfo §Info, bool malformedSectionRange, bool &stop) { if (strcmp(sectInfo.segInfo.segName, "__TEXT") != 0) return; assert(!malformedSectionRange); textSectionInfos.push_back({ sectInfo.sectName, sectInfo }); }); const std::set<std::string_view> supportedSections(std::begin(SupportedSections), std::end(SupportedSections)); int64_t slide = ma->getSlide(); for (auto sectionInfoIt = textSectionInfos.rbegin(); sectionInfoIt != textSectionInfos.rend(); ++sectionInfoIt) { const std::string& sectionName = sectionInfoIt->first; const dyld3::MachOAnalyzer::SectionInfo& sectInfo = sectionInfoIt->second; // If we find a section we can't handle then stop here. Hopefully we coalesced some from the end. if (supportedSections.find(sectionName) == supportedSections.end()) break; StringSection& cacheStringSection = getSectionData(sectionName); DylibTextCoalescer::DylibSectionOffsetToCacheSectionOffset& sectionStringData = textCoalescer.getSectionCoalescer(sectionName); // Walk the strings in this section const uint8_t* content = (uint8_t*)(sectInfo.sectAddr + slide); const char* s = (char*)content; const char* end = s + sectInfo.sectSize; while ( s < end ) { std::string_view str = s; auto itAndInserted = cacheStringSection.stringsToOffsets.insert({ str, cacheStringSection.bufferSize }); if (itAndInserted.second) { // If we inserted the string then we need to include it in the total cacheStringSection.bufferSize += str.size() + 1; if (log) printf("Selector: %s -> %s\n", ma->installName(), s); } else { // Debugging only. If we didn't include the string then we saved that many bytes cacheStringSection.savedSpace += str.size() + 1; } // Now keep track of this offset in our source dylib as pointing to this offset uint32_t sourceSectionOffset = (uint32_t)((uint64_t)s - (uint64_t)content); uint32_t cacheSectionOffset = itAndInserted.first->second; sectionStringData[sourceSectionOffset] = cacheSectionOffset; s += str.size() + 1; } } } void CacheBuilder::CacheCoalescedText::clear() { *this = CacheBuilder::CacheCoalescedText(); } CacheBuilder::CacheCoalescedText::StringSection& CacheBuilder::CacheCoalescedText::getSectionData(std::string_view sectionName) { if (sectionName.size() > 16) sectionName = sectionName.substr(0, 16); std::map<std::string_view, StringSection*> supportedSections = { { "__objc_classname", &objcClassNames }, { "__objc_methname", &objcMethNames }, { "__objc_methtype", &objcMethTypes } }; auto it = supportedSections.find(sectionName); assert(it != supportedSections.end()); return *it->second; } const CacheBuilder::CacheCoalescedText::StringSection& CacheBuilder::CacheCoalescedText::getSectionData(std::string_view sectionName) const { if (sectionName.size() > 16) sectionName = sectionName.substr(0, 16); std::map<std::string_view, const StringSection*> supportedSections = { { "__objc_classname", &objcClassNames }, { "__objc_methname", &objcMethNames }, { "__objc_methtype", &objcMethTypes } }; auto it = supportedSections.find(sectionName); assert(it != supportedSections.end()); return *it->second; } |