Debian Codegraph @

tests/framework/shim/unix_shim.cpp

raw
1 /*
2 * Copyright (c) 2021-2022 The Khronos Group Inc.
3 * Copyright (c) 2021-2022 Valve Corporation
4 * Copyright (c) 2021-2022 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
16 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 * Author: Charles Giessen <[email protected]>
26 */
27
28 #include "shim.h"
29
30 #include <cstring>
31
32 #include <algorithm>
33 #include <iostream>
34
35 #if defined(__APPLE__)
36 #include <CoreFoundation/CoreFoundation.h>
37 #endif
38
39 #include <dlfcn.h>
40
41 #include "util/folder_manager.h"
42
43 PlatformShim platform_shim;
44 extern "C" {
45 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__) || defined(__QNX__)
46 PlatformShim* get_platform_shim() {
47 platform_shim = PlatformShim();
48 return &platform_shim;
49 }
50 #elif defined(__APPLE__)
51 FRAMEWORK_EXPORT PlatformShim* get_platform_shim() {
52 platform_shim = PlatformShim();
53 return &platform_shim;
54 }
55 #endif
56
57 // Necessary for MacOS function shimming
58 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__) || defined(__QNX__)
59 #define OPENDIR_FUNC_NAME opendir
60 #define READDIR_FUNC_NAME readdir
61 #define CLOSEDIR_FUNC_NAME closedir
62 #define ACCESS_FUNC_NAME access
63 #define FOPEN_FUNC_NAME fopen
64 #define DLOPEN_FUNC_NAME dlopen
65 #define GETEUID_FUNC_NAME geteuid
66 #define GETEGID_FUNC_NAME getegid
67 #if defined(HAVE_SECURE_GETENV)
68 #define SECURE_GETENV_FUNC_NAME secure_getenv
69 #endif
70 #if defined(HAVE___SECURE_GETENV)
71 #define __SECURE_GETENV_FUNC_NAME __secure_getenv
72 #endif
73 #define PRINTF_FUNC_NAME printf
74 #define FPUTS_FUNC_NAME fputs
75 #define FPUTC_FUNC_NAME fputc
76 #elif defined(__APPLE__)
77 #define OPENDIR_FUNC_NAME my_opendir
78 #define READDIR_FUNC_NAME my_readdir
79 #define CLOSEDIR_FUNC_NAME my_closedir
80 #define ACCESS_FUNC_NAME my_access
81 #define FOPEN_FUNC_NAME my_fopen
82 #define DLOPEN_FUNC_NAME my_dlopen
83 #define GETEUID_FUNC_NAME my_geteuid
84 #define GETEGID_FUNC_NAME my_getegid
85 #if !defined(TARGET_OS_IPHONE)
86 #if defined(HAVE_SECURE_GETENV)
87 #define SECURE_GETENV_FUNC_NAME my_secure_getenv
88 #endif
89 #if defined(HAVE___SECURE_GETENV)
90 #define __SECURE_GETENV_FUNC_NAME my__secure_getenv
91 #endif
92 #endif
93 #define FPUTS_FUNC_NAME my_fputs
94 #define FPUTC_FUNC_NAME my_fputc
95 #endif
96
97 using PFN_OPENDIR = DIR* (*)(const char* path_name);
98 using PFN_READDIR = struct dirent* (*)(DIR* dir_stream);
99 using PFN_CLOSEDIR = int (*)(DIR* dir_stream);
100 using PFN_ACCESS = int (*)(const char* pathname, int mode);
101 using PFN_FOPEN = FILE* (*)(const char* filename, const char* mode);
102 using PFN_DLOPEN = void* (*)(const char* in_filename, int flags);
103 using PFN_GETEUID = uid_t (*)(void);
104 using PFN_GETEGID = gid_t (*)(void);
105 #if defined(HAVE_SECURE_GETENV) || defined(HAVE___SECURE_GETENV)
106 using PFN_SEC_GETENV = char* (*)(const char* name);
107 #endif
108 using PFN_FPUTS = int (*)(const char* str, FILE* stream);
109 using PFN_FPUTC = int (*)(int c, FILE* stream);
110
111 #if defined(__APPLE__)
112 #define real_opendir opendir
113 #define real_readdir readdir
114 #define real_closedir closedir
115 #define real_access access
116 #define real_fopen fopen
117 #define real_dlopen dlopen
118 #define real_geteuid geteuid
119 #define real_getegid getegid
120 #if defined(HAVE_SECURE_GETENV)
121 #define real_secure_getenv secure_getenv
122 #endif
123 #if defined(HAVE___SECURE_GETENV)
124 #define real__secure_getenv __secure_getenv
125 #endif
126 #define real_fputs fputs
127 #define real_fputc fputc
128 #else
129 PFN_OPENDIR real_opendir = nullptr;
130 PFN_READDIR real_readdir = nullptr;
131 PFN_CLOSEDIR real_closedir = nullptr;
132 PFN_ACCESS real_access = nullptr;
133 PFN_FOPEN real_fopen = nullptr;
134 PFN_DLOPEN real_dlopen = nullptr;
135 PFN_GETEUID real_geteuid = nullptr;
136 PFN_GETEGID real_getegid = nullptr;
137 #if defined(HAVE_SECURE_GETENV)
138 PFN_SEC_GETENV real_secure_getenv = nullptr;
139 #endif
140 #if defined(HAVE___SECURE_GETENV)
141 PFN_SEC_GETENV real__secure_getenv = nullptr;
142 #endif
143 PFN_FPUTS real_fputs = nullptr;
144 PFN_FPUTC real_fputc = nullptr;
145 #endif
146
147 FRAMEWORK_EXPORT DIR* OPENDIR_FUNC_NAME(const char* path_name) {
148 #if !defined(__APPLE__)
149 if (!real_opendir) real_opendir = (PFN_OPENDIR)dlsym(RTLD_NEXT, "opendir");
150 #endif
151 if (platform_shim.is_during_destruction || !platform_shim.is_finished_setup) {
152 return real_opendir(path_name);
153 }
154 DIR* dir = nullptr;
155
156 if (auto* folder = platform_shim.file_system_manager->get_folder_for_given_path(path_name); folder != nullptr) {
157 dir = real_opendir(folder->location().c_str());
158 platform_shim.dir_entries.push_back(DirEntry{dir, folder, {}, 0});
159 }
160 // Dont pass through as this allows non-test paths to escape containment
161
162 return dir;
163 }
164
165 FRAMEWORK_EXPORT struct dirent* READDIR_FUNC_NAME(DIR* dir_stream) {
166 #if !defined(__APPLE__)
167 if (!real_readdir) {
168 if (sizeof(void*) == 8) {
169 real_readdir = (PFN_READDIR)dlsym(RTLD_NEXT, "readdir");
170 } else {
171 // Necessary to specify the 64 bit readdir version since that is what is linked in when using _FILE_OFFSET_BITS
172 real_readdir = (PFN_READDIR)dlsym(RTLD_NEXT, "readdir64");
173 }
174 }
175
176 #endif
177 if (platform_shim.is_during_destruction || !platform_shim.is_finished_setup) {
178 return real_readdir(dir_stream);
179 }
180 auto it = std::find_if(platform_shim.dir_entries.begin(), platform_shim.dir_entries.end(),
181 [dir_stream](DirEntry const& entry) { return entry.directory == dir_stream; });
182
183 // Folder has not been opened with opendir previously, probably not a loader call so we should ignore it
184 if (it == platform_shim.dir_entries.end()) {
185 return real_readdir(dir_stream);
186 }
187 // Folder was found but this is the first file to be read from it
188 if (it->current_index == 0) {
189 std::vector<struct dirent*> readdir_contents;
190 std::vector<std::string> dirent_filenames;
191 while (true) {
192 errno = 0;
193 struct dirent* dir_entry = real_readdir(dir_stream);
194 if (errno != 0) {
195 std::cerr << "Readdir failed: errno has value of " << std::to_string(errno) << "\n";
196 }
197 if (NULL == dir_entry) {
198 break;
199 }
200 // skip . and .. entries
201 if ((dir_entry->d_name[0] == '.' && dir_entry->d_name[1] == '.' && dir_entry->d_name[2] == '\0') ||
202 (dir_entry->d_name[0] == '.' && dir_entry->d_name[1] == '\0')) {
203 continue;
204 }
205 readdir_contents.push_back(dir_entry);
206 dirent_filenames.push_back(&dir_entry->d_name[0]);
207 }
208
209 // Add the dirent structures in the order they appear in the list of files provided by the Folder
210 // Ignore anything which wasn't in the Folder
211 auto folder_contents = it->folder_represented->get_files();
212 for (auto const& file : folder_contents) {
213 for (size_t i = 0; i < dirent_filenames.size(); i++) {
214 if (file == dirent_filenames.at(i)) {
215 it->contents.push_back(readdir_contents.at(i));
216 break;
217 }
218 }
219 }
220 }
221 if (it->current_index >= it->contents.size()) return nullptr;
222 return it->contents.at(it->current_index++);
223 }
224
225 FRAMEWORK_EXPORT int CLOSEDIR_FUNC_NAME(DIR* dir_stream) {
226 #if !defined(__APPLE__)
227 if (!real_closedir) real_closedir = (PFN_CLOSEDIR)dlsym(RTLD_NEXT, "closedir");
228 #endif
229 if (platform_shim.is_during_destruction || !platform_shim.is_finished_setup) {
230 return real_closedir(dir_stream);
231 }
232 auto it = std::find_if(platform_shim.dir_entries.begin(), platform_shim.dir_entries.end(),
233 [dir_stream](DirEntry const& entry) { return entry.directory == dir_stream; });
234
235 if (it != platform_shim.dir_entries.end()) {
236 platform_shim.dir_entries.erase(it);
237 }
238
239 return real_closedir(dir_stream);
240 }
241
242 FRAMEWORK_EXPORT int ACCESS_FUNC_NAME(const char* in_pathname, int mode) {
243 #if !defined(__APPLE__)
244 if (!real_access) real_access = (PFN_ACCESS)dlsym(RTLD_NEXT, "access");
245 #endif
246 if (platform_shim.is_during_destruction || !platform_shim.is_finished_setup) {
247 return real_access(in_pathname, mode);
248 }
249
250 std::filesystem::path path{in_pathname};
251 if (!path.has_parent_path()) {
252 return real_access(in_pathname, mode);
253 } else if (auto real_path = platform_shim.file_system_manager->get_real_path_of_redirected_path(path.parent_path());
254 !real_path.empty()) {
255 real_path /= path.filename();
256 return real_access(real_path.c_str(), mode);
257 } else if (platform_shim.file_system_manager->is_folder_path(path.parent_path())) {
258 return real_access(in_pathname, mode);
259 } else {
260 return -1;
261 }
262 }
263
264 FRAMEWORK_EXPORT FILE* FOPEN_FUNC_NAME(const char* in_filename, const char* mode) {
265 #if !defined(__APPLE__)
266 if (!real_fopen) real_fopen = (PFN_FOPEN)dlsym(RTLD_NEXT, "fopen");
267 #endif
268 if (platform_shim.is_during_destruction || !platform_shim.is_finished_setup) {
269 return real_fopen(in_filename, mode);
270 }
271 FILE* out_file = nullptr;
272 std::filesystem::path path{in_filename};
273 if (!path.has_parent_path()) {
274 out_file = real_fopen(in_filename, mode);
275 } else if (auto real_path = platform_shim.file_system_manager->get_real_path_of_redirected_path(path.parent_path());
276 !real_path.empty()) {
277 real_path /= path.filename();
278 out_file = real_fopen(real_path.c_str(), mode);
279 } else {
280 out_file = real_fopen(in_filename, mode);
281 }
282
283 // Fuzz tests have sub files embedded in the input data file. This
284 if (!platform_shim.fuzz_data.empty() && out_file == NULL) {
285 FILE* fp = fopen(path.c_str(), "wb");
286 if (nullptr == fp) {
287 path.replace_filename("callback_file_" + std::to_string(platform_shim.temp_fuzz_files.size()));
288 fp = fopen(path.c_str(), "wb");
289 if (nullptr == fp) {
290 abort();
291 }
292 }
293 fwrite(platform_shim.fuzz_data.data(), platform_shim.fuzz_data.size(), 1, fp);
294 fclose(fp);
295 platform_shim.temp_fuzz_files.emplace_back(path.c_str());
296
297 out_file = fopen(path.c_str(), "rb");
298 }
299 return out_file;
300 }
301
302 FRAMEWORK_EXPORT void* DLOPEN_FUNC_NAME(const char* in_filename, int flags) {
303 #if !defined(__APPLE__)
304 if (!real_dlopen) real_dlopen = (PFN_DLOPEN)dlsym(RTLD_NEXT, "dlopen");
305 #endif
306 if (platform_shim.is_during_destruction || !platform_shim.is_finished_setup) {
307 return real_dlopen(in_filename, flags);
308 }
309
310 std::filesystem::path path{in_filename};
311 if (auto real_path = platform_shim.get_system_library(path); !real_path.empty()) {
312 return real_dlopen(real_path.c_str(), flags);
313 } else if (auto real_path = platform_shim.file_system_manager->get_real_path_of_redirected_path(path.parent_path());
314 !real_path.empty()) {
315 real_path /= path.filename();
316 return real_dlopen(real_path.c_str(), flags);
317 } else {
318 return real_dlopen(in_filename, flags);
319 }
320 }
321
322 FRAMEWORK_EXPORT uid_t GETEUID_FUNC_NAME(void) {
323 #if !defined(__APPLE__)
324 if (!real_geteuid) real_geteuid = (PFN_GETEUID)dlsym(RTLD_NEXT, "geteuid");
325 #endif
326 if (platform_shim.use_fake_elevation) {
327 // Root on linux is 0, so force pretending like we're root
328 return 0;
329 } else {
330 return real_geteuid();
331 }
332 }
333
334 FRAMEWORK_EXPORT gid_t GETEGID_FUNC_NAME(void) {
335 #if !defined(__APPLE__)
336 if (!real_getegid) real_getegid = (PFN_GETEGID)dlsym(RTLD_NEXT, "getegid");
337 #endif
338 if (platform_shim.use_fake_elevation) {
339 // Root on linux is 0, so force pretending like we're root
340 return 0;
341 } else {
342 return real_getegid();
343 }
344 }
345
346 #if !defined(TARGET_OS_IPHONE)
347 #if defined(HAVE_SECURE_GETENV)
348 FRAMEWORK_EXPORT char* SECURE_GETENV_FUNC_NAME(const char* name) {
349 #if !defined(__APPLE__)
350 if (!real_secure_getenv) real_secure_getenv = (PFN_SEC_GETENV)dlsym(RTLD_NEXT, "secure_getenv");
351 #endif
352 if (platform_shim.use_fake_elevation) {
353 return NULL;
354 } else {
355 return real_secure_getenv(name);
356 }
357 }
358 #endif
359 #if defined(HAVE___SECURE_GETENV)
360 FRAMEWORK_EXPORT char* __SECURE_GETENV_FUNC_NAME(const char* name) {
361 #if !defined(__APPLE__)
362 if (!real__secure_getenv) real__secure_getenv = (PFN_SEC_GETENV)dlsym(RTLD_NEXT, "__secure_getenv");
363 #endif
364
365 if (platform_shim.use_fake_elevation) {
366 return NULL;
367 } else {
368 return real__secure_getenv(name);
369 }
370 }
371 #endif
372 #endif
373
374 FRAMEWORK_EXPORT int FPUTS_FUNC_NAME(const char* str, FILE* stream) {
375 #if !defined(__APPLE__)
376 if (!real_fputs) real_fputs = (PFN_FPUTS)dlsym(RTLD_NEXT, "fputs");
377 #endif
378 if (stream == stderr) {
379 platform_shim.fputs_stderr_log += str;
380 }
381 return real_fputs(str, stream);
382 }
383
384 FRAMEWORK_EXPORT int FPUTC_FUNC_NAME(int ch, FILE* stream) {
385 #if !defined(__APPLE__)
386 if (!real_fputc) real_fputc = (PFN_FPUTC)dlsym(RTLD_NEXT, "fputc");
387 #endif
388 if (stream == stderr) {
389 platform_shim.fputs_stderr_log += ch;
390 }
391 return real_fputc(ch, stream);
392 }
393
394 #if defined(__APPLE__)
395 FRAMEWORK_EXPORT CFBundleRef my_CFBundleGetMainBundle() {
396 static CFBundleRef global_bundle{};
397 return reinterpret_cast<CFBundleRef>(&global_bundle);
398 }
399 FRAMEWORK_EXPORT CFURLRef my_CFBundleCopyResourcesDirectoryURL([[maybe_unused]] CFBundleRef bundle) {
400 static CFURLRef global_url{};
401 return reinterpret_cast<CFURLRef>(&global_url);
402 }
403 FRAMEWORK_EXPORT Boolean my_CFURLGetFileSystemRepresentation([[maybe_unused]] CFURLRef url,
404 [[maybe_unused]] Boolean resolveAgainstBase, UInt8* buffer,
405 CFIndex maxBufLen) {
406 if (!platform_shim.bundle_contents.empty()) {
407 CFIndex copy_len = (CFIndex)platform_shim.bundle_contents.size();
408 if (copy_len > maxBufLen) {
409 copy_len = maxBufLen;
410 }
411 strncpy(reinterpret_cast<char*>(buffer), platform_shim.bundle_contents.c_str(), copy_len);
412 return TRUE;
413 }
414 return FALSE;
415 }
416 #endif
417
418 /* Shiming functions on apple is limited by the linker prefering to not use functions in the
419 * executable in loaded dylibs. By adding an interposer, we redirect the linker to use our
420 * version of the function over the real one, thus shimming the system function.
421 */
422 #if defined(__APPLE__)
423 #define MACOS_ATTRIB __attribute__((section("__DATA,__interpose")))
424 #define VOIDP_CAST(_func) reinterpret_cast<const void*>(&_func)
425
426 struct Interposer {
427 const void* shim_function;
428 const void* underlying_function;
429 };
430
431 __attribute__((used)) static Interposer _interpose_opendir MACOS_ATTRIB = {VOIDP_CAST(my_opendir), VOIDP_CAST(opendir)};
432 __attribute__((used)) static Interposer _interpose_readdir MACOS_ATTRIB = {VOIDP_CAST(my_readdir), VOIDP_CAST(readdir)};
433 __attribute__((used)) static Interposer _interpose_closedir MACOS_ATTRIB = {VOIDP_CAST(my_closedir), VOIDP_CAST(closedir)};
434 __attribute__((used)) static Interposer _interpose_access MACOS_ATTRIB = {VOIDP_CAST(my_access), VOIDP_CAST(access)};
435 __attribute__((used)) static Interposer _interpose_fopen MACOS_ATTRIB = {VOIDP_CAST(my_fopen), VOIDP_CAST(fopen)};
436 __attribute__((used)) static Interposer _interpose_dlopen MACOS_ATTRIB = {VOIDP_CAST(my_dlopen), VOIDP_CAST(dlopen)};
437 __attribute__((used)) static Interposer _interpose_euid MACOS_ATTRIB = {VOIDP_CAST(my_geteuid), VOIDP_CAST(geteuid)};
438 __attribute__((used)) static Interposer _interpose_egid MACOS_ATTRIB = {VOIDP_CAST(my_getegid), VOIDP_CAST(getegid)};
439 #if !defined(TARGET_OS_IPHONE)
440 #if defined(HAVE_SECURE_GETENV)
441 __attribute__((used)) static Interposer _interpose_secure_getenv MACOS_ATTRIB = {VOIDP_CAST(my_secure_getenv),
442 VOIDP_CAST(secure_getenv)};
443 #endif
444 #if defined(HAVE___SECURE_GETENV)
445 __attribute__((used)) static Interposer _interpose__secure_getenv MACOS_ATTRIB = {VOIDP_CAST(my__secure_getenv),
446 VOIDP_CAST(__secure_getenv)};
447 #endif
448 #endif
449 __attribute__((used)) static Interposer _interpose_fputs MACOS_ATTRIB = {VOIDP_CAST(my_fputs), VOIDP_CAST(fputs)};
450 __attribute__((used)) static Interposer _interpose_fputc MACOS_ATTRIB = {VOIDP_CAST(my_fputc), VOIDP_CAST(fputc)};
451 __attribute__((used)) static Interposer _interpose_CFBundleGetMainBundle MACOS_ATTRIB = {VOIDP_CAST(my_CFBundleGetMainBundle),
452 VOIDP_CAST(CFBundleGetMainBundle)};
453 __attribute__((used)) static Interposer _interpose_CFBundleCopyResourcesDirectoryURL MACOS_ATTRIB = {
454 VOIDP_CAST(my_CFBundleCopyResourcesDirectoryURL), VOIDP_CAST(CFBundleCopyResourcesDirectoryURL)};
455 __attribute__((used)) static Interposer _interpose_CFURLGetFileSystemRepresentation MACOS_ATTRIB = {
456 VOIDP_CAST(my_CFURLGetFileSystemRepresentation), VOIDP_CAST(CFURLGetFileSystemRepresentation)};
457
458 #endif
459 } // extern "C"