dyld-421.1.tar.gz
[apple/dyld.git] / interlinked-dylibs / FileCache.cpp
1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 *
3 * Copyright (c) 2014 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 #include "mega-dylib-utils.h"
27 #include "MachOFileAbstraction.hpp"
28 #include "Trie.hpp"
29
30 #include <dirent.h>
31 #include <sys/errno.h>
32 #include <sys/fcntl.h>
33 #include <sys/param.h>
34 #include <mach-o/loader.h>
35 #include <mach-o/fat.h>
36 #include <mach-o/dyld.h>
37 #include <assert.h>
38 #include <Availability.h>
39
40 #include <fstream>
41 #include <iostream>
42 #include <sstream>
43 #include <string>
44 #include <algorithm>
45 #include <unordered_map>
46 #include <unordered_set>
47
48 #include <System/sys/csr.h>
49
50 #include "dyld_cache_config.h"
51
52 #include "OptimizerBranches.h"
53
54 #include "CacheFileAbstraction.hpp"
55
56 #include "mega-dylib-utils.h"
57 #include "Logging.h"
58
59
60 //#include <rootless.h>
61 extern "C" int rootless_check_trusted(const char *path);
62 extern "C" int rootless_check_trusted_fd(int fd) __attribute__((weak_import));
63
64 static bool rootlessEnabled;
65 static dispatch_once_t onceToken;
66
67 bool isProtectedBySIP(const std::string& path, int fd)
68 {
69 bool isProtected = false;
70 // Check to make sure file system protections are on at all
71 dispatch_once(&onceToken, ^{
72 rootlessEnabled = csr_check(CSR_ALLOW_UNRESTRICTED_FS);
73 });
74 if (!rootlessEnabled)
75 return false;
76 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
77 if ( (fd != -1) && (&rootless_check_trusted_fd != NULL) )
78 isProtected = (rootless_check_trusted_fd(fd) == 0);
79 else
80 #endif
81 if ( &rootless_check_trusted != NULL )
82 isProtected = (rootless_check_trusted(path.c_str()) == 0);
83 return isProtected;
84 }
85
86
87 std::string toolDir()
88 {
89 char buffer[PATH_MAX];
90 uint32_t bufsize = PATH_MAX;
91 int result = _NSGetExecutablePath(buffer, &bufsize);
92 if ( result == 0 ) {
93 std::string path = buffer;
94 size_t pos = path.rfind('/');
95 if ( pos != std::string::npos )
96 return path.substr(0,pos+1);
97 }
98 warning("tool directory not found");
99 return "/tmp/";
100 }
101
102 std::string baspath(const std::string& path)
103 {
104 std::string::size_type slash_pos = path.rfind("/");
105 if (slash_pos != std::string::npos) {
106 slash_pos++;
107 return path.substr(slash_pos);
108 } else {
109 return path;
110 }
111 }
112
113 std::string dirpath(const std::string& path)
114 {
115 std::string::size_type slash_pos = path.rfind("/");
116 if (slash_pos != std::string::npos) {
117 slash_pos++;
118 return path.substr(0, slash_pos);
119 } else {
120 char cwd[MAXPATHLEN];
121 (void)getcwd(cwd, MAXPATHLEN);
122 return cwd;
123 }
124 }
125
126 std::string normalize_absolute_file_path(const std::string &path) {
127 std::vector<std::string> components;
128 std::vector<std::string> processed_components;
129 std::stringstream ss(path);
130 std::string retval;
131 std::string item;
132
133 while (std::getline(ss, item, '/')) {
134 components.push_back(item);
135 }
136
137 if (components[0] == ".") {
138 retval = ".";
139 }
140
141 for (auto& component : components) {
142 if (component.empty() || component == ".")
143 continue;
144 else if (component == ".." && processed_components.size())
145 processed_components.pop_back();
146 else
147 processed_components.push_back(component);
148 }
149
150 for (auto & component : processed_components) {
151 retval = retval + "/" + component;
152 }
153
154 return retval;
155 }
156
157 FileCache fileCache;
158
159 FileCache::FileCache(void) {
160 cache_queue = dispatch_queue_create("com.apple.dyld.cache.cache", DISPATCH_QUEUE_SERIAL);
161 }
162
163
164 void FileCache::preflightCache(const std::unordered_set<std::string>& paths) {
165 for (auto &path : paths) {
166 preflightCache(path);
167 }
168 }
169
170 void FileCache::preflightCache(const std::string& path)
171 {
172 cacheBuilderDispatchAsync(cache_queue, [=] {
173 std::string normalizedPath = normalize_absolute_file_path(path);
174 if (entries.count(normalizedPath) == 0) {
175 fill(normalizedPath);
176 }
177 });
178 }
179
180 std::tuple<uint8_t *, struct stat, bool> FileCache::cacheLoad(const std::string path) {
181 std::string normalizedPath = normalize_absolute_file_path(path);
182 cacheBuilderDispatchSync(cache_queue, [=] {
183 if ( entries.count(normalizedPath) == 0 )
184 fill(normalizedPath);
185 });
186
187 return entries[normalizedPath];
188 }
189
190
191 //FIXME error handling
192 void FileCache::fill(const std::string& path) {
193 struct stat stat_buf;
194
195 int fd = ::open(path.c_str(), O_RDONLY, 0);
196 if ( fd == -1 ) {
197 verboseLog("can't open file '%s', errno=%d", path.c_str(), errno);
198 entries[path] = std::make_tuple((uint8_t*)(-1), stat_buf, false);
199 return;
200 }
201
202 if ( fstat(fd, &stat_buf) == -1) {
203 verboseLog("can't stat open file '%s', errno=%d", path.c_str(), errno);
204 entries[path] = std::make_tuple((uint8_t*)(-1), stat_buf, false);
205 ::close(fd);
206 return;
207 }
208
209 if ( stat_buf.st_size < 4096 ) {
210 verboseLog("file too small '%s'", path.c_str());
211 entries[path] = std::make_tuple((uint8_t*)(-1), stat_buf, false);
212 ::close(fd);
213 return;
214 }
215
216 bool rootlessProtected = isProtectedBySIP(path, fd);
217
218 void* buffer_ptr = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
219 if (buffer_ptr == MAP_FAILED) {
220 verboseLog("mmap() for shared cache at %s failed, errno=%d", path.c_str(), errno);
221 entries[path] = std::make_tuple((uint8_t*)(-1), stat_buf, false);
222 ::close(fd);
223 return;
224 }
225
226 entries[path] = std::make_tuple((uint8_t*)buffer_ptr, stat_buf, rootlessProtected);
227 ::close(fd);
228 }
229
230
231
232
233
234