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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * Copyright (c) 2017 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@
 */


#ifndef DyldSharedCache_h
#define DyldSharedCache_h

#include <TargetConditionals.h>
#include <uuid/uuid.h>

#if !(BUILDING_LIBDYLD || BUILDING_DYLD)
#include <set>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#endif

#include "dyld_cache_format.h"
#include "Diagnostics.h"
#include "MachOAnalyzer.h"
#include "Closure.h"
#include "JSON.h"

namespace objc_opt {
struct objc_opt_t;
}

class VIS_HIDDEN DyldSharedCache
{
public:

#if BUILDING_CACHE_BUILDER
    enum CodeSigningDigestMode
    {
        SHA256only = 0,
        SHA1only   = 1,
        Agile      = 2
    };

    enum class LocalSymbolsMode {
        keep,
        unmap,
        strip
    };

    struct CreateOptions
    {
        std::string                                 outputFilePath;
        std::string                                 outputMapFilePath;
        const dyld3::GradedArchs*                   archs;
        dyld3::Platform                             platform;
        LocalSymbolsMode                            localSymbolMode;
        bool                                        optimizeStubs;
        bool                                        optimizeDyldDlopens;
        bool                                        optimizeDyldLaunches;
        CodeSigningDigestMode                       codeSigningDigestMode;
        bool                                        dylibsRemovedDuringMastering;
        bool                                        inodesAreSameAsRuntime;
        bool                                        cacheSupportsASLR;
        bool                                        forSimulator;
        bool                                        isLocallyBuiltCache;
        bool                                        verbose;
        bool                                        evictLeafDylibsOnOverflow;
        std::unordered_map<std::string, unsigned>   dylibOrdering;
        std::unordered_map<std::string, unsigned>   dirtyDataSegmentOrdering;
        dyld3::json::Node                           objcOptimizations;
        std::string                                 loggingPrefix;
    };

    struct MappedMachO
    {
                                    MappedMachO()
                                            : mh(nullptr), length(0), isSetUID(false), protectedBySIP(false), sliceFileOffset(0), modTime(0), inode(0) { }
                                    MappedMachO(const std::string& path, const dyld3::MachOAnalyzer* p, size_t l, bool isu, bool sip, uint64_t o, uint64_t m, uint64_t i)
                                            : runtimePath(path), mh(p), length(l), isSetUID(isu), protectedBySIP(sip), sliceFileOffset(o), modTime(m), inode(i) { }

        std::string                 runtimePath;
        const dyld3::MachOAnalyzer* mh;
        size_t                      length;
        uint64_t                    isSetUID        :  1,
                                    protectedBySIP  :  1,
                                    sliceFileOffset : 62;
        uint64_t                    modTime;                // only recorded if inodesAreSameAsRuntime
        uint64_t                    inode;                  // only recorded if inodesAreSameAsRuntime
    };

    struct CreateResults
    {
        std::string                             errorMessage;
        std::set<std::string>                   warnings;
        std::set<const dyld3::MachOAnalyzer*>   evictions;
        bool                                    agileSignature  = false;
        std::string                             cdHashFirst;
        std::string                             cdHashSecond;
    };


    struct FileAlias
    {
        std::string             realPath;
        std::string             aliasPath;
    };


    // This function verifies the set of dylibs that will go into the cache are self contained.  That the depend on no dylibs
    // outset the set.  It will call back the loader function to try to find any mising dylibs.
    static bool verifySelfContained(std::vector<MappedMachO>& dylibsToCache,
                                    std::unordered_set<std::string>& badZippered,
                                    MappedMachO (^loader)(const std::string& runtimePath, Diagnostics& diag), std::vector<std::pair<DyldSharedCache::MappedMachO, std::set<std::string>>>& excluded);


    //
    // This function is single threaded and creates a shared cache. The cache file is created in-memory.
    //
    // Inputs:
    //      options:        various per-platform flags
    //      dylibsToCache:  a list of dylibs to include in the cache
    //      otherOsDylibs:  a list of other OS dylibs and bundle which should have load info added to the cache
    //      osExecutables:  a list of main executables which should have closures created in the cache
    //
    // Returns:
    //    On success:
    //         cacheContent: start of the allocated cache buffer which must be vm_deallocated after the caller writes out the buffer.
    //         cacheLength:  size of the allocated cache buffer
    //         cdHash:       hash of the code directory of the code blob of the created cache
    //         warnings:     all warning messsages generated during the creation of the cache
    //
    //    On failure:
    //         cacheContent: nullptr
    //         errorMessage: the string describing why the cache could not be created
    //         warnings:     all warning messsages generated before the failure
    //
    static CreateResults create(const CreateOptions&               options,
                                const dyld3::closure::FileSystem&  fileSystem,
                                const std::vector<MappedMachO>&    dylibsToCache,
                                const std::vector<MappedMachO>&    otherOsDylibs,
                                const std::vector<MappedMachO>&    osExecutables);


    //
    // Returns a text "map" file as a big string
    //
    std::string         mapFile() const;

#endif // BUILDING_CACHE_BUILDER


    //
    // Returns the architecture name of the shared cache, e.g. "arm64"
    //
    const char*         archName() const;


    //
    // Returns the platform the cache is for
    //
    dyld3::Platform    platform() const;


    //
    // Iterates over each dylib in the cache
    //
    void                forEachImage(void (^handler)(const mach_header* mh, const char* installName)) const;


    //
    // Searches cache for dylib with specified path
    //
    bool                hasImagePath(const char* dylibPath, uint32_t& imageIndex) const;


    //
    // Is this path (which we know is in the shared cache), overridable
    //
    bool                isOverridablePath(const char* dylibPath) const;


    //
    // Path is to a dylib in the cache and this is an optimized cache so that path cannot be overridden
    //
    bool                hasNonOverridablePath(const char* dylibPath) const;


    //
    // Check if shared cache contains local symbols info
    //
    const bool          hasLocalSymbolsInfo() const;


    //
    // Get code signature mapped address
    //
    uint64_t             getCodeSignAddress() const;


    //
    // Searches cache for dylib with specified mach_header
    //
    bool                findMachHeaderImageIndex(const mach_header* mh, uint32_t& imageIndex) const;

   //
    // Iterates over each dylib in the cache
    //
    void                forEachImageEntry(void (^handler)(const char* path, uint64_t mTime, uint64_t inode)) const;


    //
    // Get image entry from index
    //
    const mach_header*  getIndexedImageEntry(uint32_t index, uint64_t& mTime, uint64_t& node) const;


    // iterates over all dylibs and aliases
    void forEachDylibPath(void (^handler)(const char* dylibPath, uint32_t index)) const;


    //
    // Get image path from index
    //
    const char*         getIndexedImagePath(uint32_t index) const;

#if BUILDING_LIBDYLD
    //
    // Get the canonical (dylib) path for a given path, which may be a symlink to something in the cache
    //
    const char*         getCanonicalPath(const char* path) const;
#endif

    //
    // Iterates over each text segment in the cache
    //
    void                forEachImageTextSegment(void (^handler)(uint64_t loadAddressUnslid, uint64_t textSegmentSize, const uuid_t dylibUUID, const char* installName, bool& stop)) const;


    //
    // Iterates over each of the three regions in the cache
    //
    void                forEachRegion(void (^handler)(const void* content, uint64_t vmAddr, uint64_t size, uint32_t permissions,
                                                      uint64_t flags)) const;


    //
    // Get local symbols nlist entries
    //
    const void*         getLocalNlistEntries() const;


    //
    // Get local symbols nlist count
    //
    const uint32_t      getLocalNlistCount() const;


    //
    // Get local symbols strings
    //
    const char*  getLocalStrings() const;


    //
    // Get local symbols strings size
    //
    const uint32_t       getLocalStringsSize() const;


     //
     // Iterates over each local symbol entry in the cache
     //
     void                forEachLocalSymbolEntry(void (^handler)(uint32_t dylibOffset, uint32_t nlistStartIndex, uint32_t nlistCount, bool& stop)) const;

    //
    // Returns if an address range is in this cache, and if so if in a read-only area
    //
    bool                inCache(const void* addr, size_t length, bool& readOnly) const;

    //
    // Returns true if a path is an alternate path (symlink)
    //
    bool isAlias(const char* path) const;

    //
    // returns address the cache would load at if unslid
    //
    uint64_t            unslidLoadAddress() const;


    //
    // returns UUID of cache
    //
    void                getUUID(uuid_t uuid) const;


    //
    // returns the vm size required to map cache
    //
    uint64_t            mappedSize() const;


    //
    // searches cache for pre-built closure for program
    //
    const dyld3::closure::LaunchClosure* findClosure(const char* executablePath) const;


    //
    // iterates all pre-built closures for program
    //
    void forEachLaunchClosure(void (^handler)(const char* executableRuntimePath, const dyld3::closure::LaunchClosure* closure)) const;


    //
    // iterates all pre-built Image* for OS dylibs/bundles not in dyld cache
    //
    void forEachDlopenImage(void (^handler)(const char* runtimePath, const dyld3::closure::Image* image)) const;


    //
    // returns the ImageArray pointer to Images in dyld shared cache
    //
    const dyld3::closure::ImageArray*  cachedDylibsImageArray() const;


    //
    // returns the ImageArray pointer to Images in OS with pre-build dlopen closure
    //
    const dyld3::closure::ImageArray*  otherOSImageArray() const;


    //
    // searches cache for pre-built dlopen closure for OS dylib/bundle
    //
    const dyld3::closure::Image* findDlopenOtherImage(const char* path) const;

    //
    // Returns the pointer to the slide info for this cache
    //
    const dyld_cache_slide_info* legacyCacheSlideInfo() const;

    //
    // Returns a pointer to the __DATA region mapping in the cache
    //
    const dyld_cache_mapping_info* legacyCacheDataRegionMapping() const;

    //
    // Returns a pointer to the start of the __DATA region in the cache
    //
    const uint8_t* legacyCacheDataRegionBuffer() const;

    //
    // Returns a pointer to the shared cache optimized Objective-C data structures
    //
    const objc_opt::objc_opt_t* objcOpt() const;

    //
    // Returns a pointer to the shared cache optimized Objective-C pointer structures
    //
    const void* objcOptPtrs() const;

    // Returns true if the cache has any slide info, either old style on a single data region
    // or on each individual data mapping
    bool                hasSlideInfo() const;

    void                forEachSlideInfo(void (^handler)(uint64_t mappingStartAddress, uint64_t mappingSize,
                                                         const uint8_t* mappingPagesStart,
                                                         uint64_t slideInfoOffset, uint64_t slideInfoSize,
                                                         const dyld_cache_slide_info* slideInfoHeader)) const;


    //
    // returns true if the offset is in the TEXT of some cached dylib and sets *index to the dylib index
    //
    bool              addressInText(uint32_t cacheOffset, uint32_t* index) const;

    uint32_t          patchableExportCount(uint32_t imageIndex) const;
    void              forEachPatchableExport(uint32_t imageIndex, void (^handler)(uint32_t cacheOffsetOfImpl, const char* exportName)) const;
    void              forEachPatchableUseOfExport(uint32_t imageIndex, uint32_t cacheOffsetOfImpl,
                                                  void (^handler)(dyld_cache_patchable_location patchLocation)) const;

    // Helper to get the addend for a patch location since we don't want to put C++ in the shared cache format header
    static uint64_t getAddend(const dyld_cache_patchable_location& loc) {
        uint64_t unsingedAddend = loc.addend;
        int64_t signedAddend = (int64_t)unsingedAddend;
        signedAddend = (signedAddend << 52) >> 52;
        return (uint64_t)signedAddend;
    }
    // Helper to get the key nam for a patch location since we don't want to put C++ in the shared cache format header
    static const char* keyName(const dyld_cache_patchable_location& patchLocation) {
        dyld3::MachOLoaded::ChainedFixupPointerOnDisk dummy;
        dummy.arm64e.authRebase.auth = 1;
        dummy.arm64e.authRebase.bind = 0;
        dummy.arm64e.authRebase.key  = patchLocation.key;
        return dummy.arm64e.keyName();
    }

#if !(BUILDING_LIBDYLD || BUILDING_DYLD)
    // MRM map file generator
    std::string generateJSONMap(const char* disposition) const;

    // This generates a JSON representation of deep reverse dependency information in the cache.
    // For each dylib, the output will contain the list of all the other dylibs transitively
    // dependening on that library. (For example, the entry for libsystem will contain almost
    // all of the dylibs in the cache ; a very high-level framework such as ARKit will have way
    // fewer dependents).
    // This is used by the shared cache ordering script to put "deep" dylibs used by everybody
    // closer to the center of the cache.
    std::string generateJSONDependents() const;
#endif

    // Note these enum entries are only valid for 64-bit archs.
    enum class ConstantClasses {
        cfStringAtomSize = 32
    };

    // Returns the start and size of the range in the shared cache of the ObjC constants, such as
    // all of the CFString's which have been moved in to a contiguous range
    std::pair<const void*, uint64_t> getObjCConstantRange() const;

#if !(BUILDING_LIBDYLD || BUILDING_DYLD)
    dyld3::MachOAnalyzer::VMAddrConverter makeVMAddrConverter(bool contentRebased) const;
#endif

    dyld_cache_header header;

    // The most mappings we could generate.
    // For now its __TEXT, __DATA_CONST, __DATA_DIRTY, __DATA, __LINKEDIT,
    // and optionally also __AUTH, __AUTH_CONST, __AUTH_DIRTY
    static const uint32_t MaxMappings = 8;

private:
    // Returns a variable of type "const T" which corresponds to the header field with the given unslid address
    template<typename T>
    const T getAddrField(uint64_t addr) const;

#if !(BUILDING_LIBDYLD || BUILDING_DYLD)
    void fillMachOAnalyzersMap(std::unordered_map<std::string,dyld3::MachOAnalyzer*> & dylibAnalyzers) const;
    void computeReverseDependencyMapForDylib(std::unordered_map<std::string, std::set<std::string>> &reverseDependencyMap, const std::unordered_map<std::string,dyld3::MachOAnalyzer*> & dylibAnalyzers, const std::string &loadPath) const;
    void computeReverseDependencyMap(std::unordered_map<std::string, std::set<std::string>> &reverseDependencyMap) const;
    void findDependentsRecursively(std::unordered_map<std::string, std::set<std::string>> &transitiveDependents, const std::unordered_map<std::string, std::set<std::string>> &reverseDependencyMap, std::set<std::string> & visited, const std::string &loadPath) const;
    void computeTransitiveDependents(std::unordered_map<std::string, std::set<std::string>> & transitiveDependents) const;
#endif
};








#endif /* DyldSharedCache_h */