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 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- * * Copyright (c) 2016 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 <dispatch/dispatch.h> #include <Security/Security.h> #include <Security/SecCodeSigner.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> #include <sys/resource.h> #include <mach/mach.h> #include <mach/mach_time.h> #include <limits.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <math.h> #include <fcntl.h> #include <dlfcn.h> #include <signal.h> #include <errno.h> #include <sys/uio.h> #include <unistd.h> #include <sys/param.h> #include <sys/sysctl.h> #include <sys/resource.h> #include <dirent.h> #include <libgen.h> #include <pthread.h> #include <fts.h> #include <vector> #include <array> #include <set> #include <map> #include <algorithm> #include <spawn.h> #include <Bom/Bom.h> #include "mega-dylib-utils.h" #include "MultiCacheBuilder.h" #include "MachOProxy.h" #include "Manifest.h" #include "Logging.h" #if !__has_feature(objc_arc) #error The use of libdispatch in this files requires it to be compiled with ARC in order to avoid leaks #endif extern char** environ; static dispatch_queue_t build_queue; inline bool has_suffix(std::string const & value, std::string const & ending) { if (ending.size() > value.size()) return false; return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); } static const char *tempRootDirTemplate = "/tmp/dyld_shared_cache_builder.XXXXXX"; static char *tempRootDir = nullptr; void processRoot( const std::string &root, std::set<std::string> &roots ) { struct stat sb; int res = 0; pid_t pid; int status; const char* args[8]; res = stat(root.c_str(), &sb); if (res == 0 && S_ISDIR(sb.st_mode)) { roots.insert( root ); return; } else if ( has_suffix( root, ".cpio" ) || has_suffix( root, ".cpio.gz" ) || has_suffix( root, ".cpgz" ) || has_suffix( root, ".cpio.bz2" ) || has_suffix( root, ".cpbz2" ) || has_suffix( root, ".pax" ) || has_suffix( root, ".pax.gz" ) || has_suffix( root, ".pgz" ) || has_suffix( root, ".pax.bz2" ) || has_suffix( root, ".pbz2" ) ) { args[0] = (char *)"/usr/bin/ditto"; args[1] = (char *)"-x"; args[2] = (char *)root.c_str(); args[3] = tempRootDir; args[4] = nullptr; } else if (has_suffix(root, ".tar")) { args[0] = (char *)"/usr/bin/tar"; args[1] = (char *)"xf"; args[2] = (char *)root.c_str(); args[3] = (char *)"-C"; args[4] = tempRootDir; args[5] = nullptr; } else if (has_suffix(root, ".tar.gz") || has_suffix(root, ".tgz")) { args[0] = (char *)"/usr/bin/tar"; args[1] = (char *)"xzf"; args[2] = (char *)root.c_str(); args[3] = (char *)"-C"; args[4] = tempRootDir; args[5] = nullptr; } else if (has_suffix(root, ".tar.bz2") || has_suffix(root, ".tbz2") || has_suffix(root, ".tbz")) { args[0] = (char *)"/usr/bin/tar"; args[1] = (char *)"xjf"; args[2] = (char *)root.c_str(); args[3] = (char *)"-C"; args[4] = tempRootDir; args[5] = nullptr; } else if (has_suffix(root, ".xar")) { args[0] = (char *)"/usr/bin/xar"; args[1] = (char *)"-xf"; args[2] = (char *)root.c_str(); args[3] = (char *)"-C"; args[4] = tempRootDir; args[5] = nullptr; } else if (has_suffix(root, ".zip")) { args[0] = (char *)"/usr/bin/ditto"; args[1] = (char *)"-xk"; args[2] = (char *)root.c_str(); args[3] = tempRootDir; args[4] = nullptr; } else { terminate("unknown archive type: %s", root.c_str()); } res = posix_spawn(&pid, args[0], nullptr, nullptr, (char**)args, environ); if (res != 0) terminate("Failed to spawn %s: %s (%d)", args[0], strerror(res), res); do { res = waitpid(pid, &status, 0); } while (res == -1 && errno == EINTR); if (res != -1) { if (WIFEXITED(status)) { res = WEXITSTATUS(status); } else { res = -1; } } if (res != 0) terminate("Could not expand archive %s: %s (%d)", root.c_str(), strerror(res), res); for (auto &existingRoot : roots) { if (existingRoot == tempRootDir) return; } roots.insert( tempRootDir ); } bool writeRootList( const std::string &dstRoot, const std::set<std::string> &roots ) { if ( roots.size() == 0 ) return false; std::string rootFile = dstRoot + "/roots.txt"; FILE* froots = ::fopen(rootFile.c_str(), "w"); if ( froots == NULL ) return false; for (auto &root : roots) { fprintf(froots, "%s\n", root.c_str()); } ::fclose(froots); return true; } std::string realPath(const std::string& path) { char resolvedPath[PATH_MAX]; if (realpath( path.c_str(), &resolvedPath[0]) != nullptr) { return resolvedPath; } else { return ""; } } std::set<std::string> cachePaths; BOMCopierCopyOperation filteredCopy(BOMCopier copier, const char *path, BOMFSObjType type, off_t size) { std::string absolutePath = &path[1]; if (cachePaths.count(absolutePath)) { return BOMCopierSkipFile; } return BOMCopierContinue; } int main (int argc, const char * argv[]) { @autoreleasepool { std::set<std::string> roots; std::string dylibCacheDir; std::string release; bool emitDevCaches = true; bool emitElidedDylibs = true; bool listConfigs = false; bool copyRoots = false; std::string dstRoot; std::string configuration; std::string resultPath; time_t mytime = time(0); log("Started: %s", asctime(localtime(&mytime))); tempRootDir = strdup(tempRootDirTemplate); mkdtemp(tempRootDir); for (int i = 1; i < argc; ++i) { const char* arg = argv[i]; if (arg[0] == '-') { if (strcmp(arg, "-debug") == 0) { setVerbose(true); } else if (strcmp(arg, "-list_configs") == 0) { listConfigs = true; } else if (strcmp(arg, "-root") == 0) { std::string root = realPath(argv[++i]); processRoot(root, roots); } else if (strcmp(arg, "-copy_roots") == 0) { copyRoots = true; } else if (strcmp(arg, "-dylib_cache") == 0) { dylibCacheDir = realPath(argv[++i]); } else if (strcmp(arg, "-no_development_cache") == 0) { emitDevCaches = false; } else if (strcmp(arg, "-no_overflow_dylibs") == 0) { emitElidedDylibs = false; } else if (strcmp(arg, "-development_cache") == 0) { emitDevCaches = true; } else if (strcmp(arg, "-overflow_dylibs") == 0) { emitElidedDylibs = true; } else if (strcmp(arg, "-dst_root") == 0) { dstRoot = realPath(argv[++i]); } else if (strcmp(arg, "-release") == 0) { release = argv[++i]; } else if (strcmp(arg, "-results") == 0) { resultPath = realPath(argv[++i]); } else { //usage(); terminate("unknown option: %s\n", arg); } } else { if (!configuration.empty()) { terminate("You may only specify one configruation"); } configuration = argv[i]; } } struct rlimit rl = {OPEN_MAX, OPEN_MAX}; (void)setrlimit(RLIMIT_NOFILE, &rl); if (dylibCacheDir.empty() && release.empty()) { terminate("you must specify either -dylib_cache or -release"); } else if (!dylibCacheDir.empty() && !release.empty()) { terminate("you may not use -dylib_cache and -release at the same time"); } if ((configuration.empty() || dstRoot.empty()) && !listConfigs) { terminate("Must specify a configuration and a -dst_root OR -list_configs\n"); } if (dylibCacheDir.empty()) { dylibCacheDir = std::string("/AppleInternal/Developer/DylibCaches/") + release + ".dlc"; } //Move into the dir so we can use relative path manifests chdir(dylibCacheDir.c_str()); auto manifest = Manifest(dylibCacheDir + "/Manifest.plist", roots); if (manifest.build().empty()) { terminate("No manifest found at '%s/Manifest.plist'\n", dylibCacheDir.c_str()); } log("Building Caches for %s", manifest.build().c_str()); if (listConfigs) { manifest.forEachConfiguration([](const std::string& configName) { printf("%s\n", configName.c_str()); }); exit(0); } if (!manifest.filterForConfig(configuration)) { terminate("No config %s. Please run with -list_configs to see configurations available for this %s.\n", configuration.c_str(), manifest.build().c_str()); } manifest.calculateClosure(false); manifest.checkLinks(); // FIXME: Plumb through no_development std::shared_ptr<MultiCacheBuilder> builder = std::make_shared<MultiCacheBuilder>(manifest, false, false, true); builder->buildCaches(dstRoot); writeRootList(dstRoot, roots); if (copyRoots) { manifest.forEachConfiguration([&manifest](const std::string& configName) { for (auto& arch : manifest.configuration(configName).architectures) { for (auto& dylib : arch.second.results.dylibs) { if (dylib.second.included) { MachOProxy* proxy = MachOProxy::forIdentifier(dylib.first, arch.first); cachePaths.insert(proxy->installName); for (auto& alias : proxy->installNameAliases) { cachePaths.insert(alias); } } } } }); BOMCopier copier = BOMCopierNewWithSys(BomSys_default()); BOMCopierSetCopyFileStartedHandler(copier, filteredCopy); for (auto& root : roots) { BOMCopierCopy(copier, root.c_str(), dstRoot.c_str()); } BOMCopierFree(copier); } // Create an empty FIPS data in the root (void)mkpath_np((dstRoot + "/private/var/db/FIPS/").c_str(), 0755); int fd = open((dstRoot + "/private/var/db/FIPS/fips_data").c_str(), O_CREAT | O_TRUNC, 0644); close(fd); // Now that all the build commands have been issued lets put a barrier in after then which can tear down the app after // everything is written. builder->logStats(); if (!resultPath.empty()) { manifest.write(resultPath); } pid_t pid; int res = 0; int status; const char* args[8]; args[0] = (char*)"/bin/rm"; args[1] = (char*)"-rf"; args[2] = (char*)tempRootDir; args[3] = nullptr; res = posix_spawn(&pid, args[0], nullptr, nullptr, (char**)args, environ); if (res != 0) terminate("Failed to spawn %s: %s (%d)", args[0], strerror(res), res); do { res = waitpid(pid, &status, 0); } while (res == -1 && errno == EINTR); if (res != -1) { if (WIFEXITED(status)) { res = WEXITSTATUS(status); } else { res = -1; } } dumpLogAndExit(); } dispatch_main(); return 0; } |