george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // IO functions implementation using Posix API. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | #include "FuzzerDefs.h" |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 11 | #if LIBFUZZER_POSIX || LIBFUZZER_FUCHSIA |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 12 | |
| 13 | #include "FuzzerExtFunctions.h" |
| 14 | #include "FuzzerIO.h" |
| 15 | #include <cstdarg> |
| 16 | #include <cstdio> |
| 17 | #include <dirent.h> |
| 18 | #include <fstream> |
| 19 | #include <iterator> |
| 20 | #include <libgen.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | namespace fuzzer { |
| 26 | |
| 27 | bool IsFile(const std::string &Path) { |
| 28 | struct stat St; |
| 29 | if (stat(Path.c_str(), &St)) |
| 30 | return false; |
| 31 | return S_ISREG(St.st_mode); |
| 32 | } |
| 33 | |
kcc | e776135 | 2017-11-15 16:45:17 +0000 | [diff] [blame] | 34 | static bool IsDirectory(const std::string &Path) { |
| 35 | struct stat St; |
| 36 | if (stat(Path.c_str(), &St)) |
| 37 | return false; |
| 38 | return S_ISDIR(St.st_mode); |
| 39 | } |
| 40 | |
kcc | dc00cd3 | 2017-08-29 20:51:24 +0000 | [diff] [blame] | 41 | size_t FileSize(const std::string &Path) { |
| 42 | struct stat St; |
| 43 | if (stat(Path.c_str(), &St)) |
| 44 | return 0; |
| 45 | return St.st_size; |
| 46 | } |
| 47 | |
morehouse | 68f4643 | 2018-08-30 15:54:44 +0000 | [diff] [blame] | 48 | std::string Basename(const std::string &Path) { |
| 49 | size_t Pos = Path.rfind(GetSeparator()); |
| 50 | if (Pos == std::string::npos) return Path; |
| 51 | assert(Pos < Path.size()); |
| 52 | return Path.substr(Pos + 1); |
| 53 | } |
| 54 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 55 | void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 56 | Vector<std::string> *V, bool TopDir) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 57 | auto E = GetEpoch(Dir); |
| 58 | if (Epoch) |
| 59 | if (E && *Epoch >= E) return; |
| 60 | |
| 61 | DIR *D = opendir(Dir.c_str()); |
| 62 | if (!D) { |
morehouse | c2b235e | 2018-04-06 18:15:24 +0000 | [diff] [blame] | 63 | Printf("%s: %s; exiting\n", strerror(errno), Dir.c_str()); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 64 | exit(1); |
| 65 | } |
| 66 | while (auto E = readdir(D)) { |
| 67 | std::string Path = DirPlusFile(Dir, E->d_name); |
kcc | e776135 | 2017-11-15 16:45:17 +0000 | [diff] [blame] | 68 | if (E->d_type == DT_REG || E->d_type == DT_LNK || |
| 69 | (E->d_type == DT_UNKNOWN && IsFile(Path))) |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 70 | V->push_back(Path); |
kcc | e776135 | 2017-11-15 16:45:17 +0000 | [diff] [blame] | 71 | else if ((E->d_type == DT_DIR || |
| 72 | (E->d_type == DT_UNKNOWN && IsDirectory(Path))) && |
| 73 | *E->d_name != '.') |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 74 | ListFilesInDirRecursive(Path, Epoch, V, false); |
| 75 | } |
| 76 | closedir(D); |
| 77 | if (Epoch && TopDir) |
| 78 | *Epoch = E; |
| 79 | } |
| 80 | |
| 81 | char GetSeparator() { |
| 82 | return '/'; |
| 83 | } |
| 84 | |
| 85 | FILE* OpenFile(int Fd, const char* Mode) { |
| 86 | return fdopen(Fd, Mode); |
| 87 | } |
| 88 | |
| 89 | int CloseFile(int fd) { |
| 90 | return close(fd); |
| 91 | } |
| 92 | |
| 93 | int DuplicateFile(int Fd) { |
| 94 | return dup(Fd); |
| 95 | } |
| 96 | |
| 97 | void RemoveFile(const std::string &Path) { |
| 98 | unlink(Path.c_str()); |
| 99 | } |
| 100 | |
| 101 | void DiscardOutput(int Fd) { |
| 102 | FILE* Temp = fopen("/dev/null", "w"); |
| 103 | if (!Temp) |
| 104 | return; |
| 105 | dup2(fileno(Temp), Fd); |
| 106 | fclose(Temp); |
| 107 | } |
| 108 | |
| 109 | intptr_t GetHandleFromFd(int fd) { |
| 110 | return static_cast<intptr_t>(fd); |
| 111 | } |
| 112 | |
| 113 | std::string DirName(const std::string &FileName) { |
| 114 | char *Tmp = new char[FileName.size() + 1]; |
| 115 | memcpy(Tmp, FileName.c_str(), FileName.size() + 1); |
| 116 | std::string Res = dirname(Tmp); |
| 117 | delete [] Tmp; |
| 118 | return Res; |
| 119 | } |
| 120 | |
| 121 | std::string TmpDir() { |
| 122 | if (auto Env = getenv("TMPDIR")) |
| 123 | return Env; |
| 124 | return "/tmp"; |
| 125 | } |
| 126 | |
| 127 | bool IsInterestingCoverageFile(const std::string &FileName) { |
| 128 | if (FileName.find("compiler-rt/lib/") != std::string::npos) |
| 129 | return false; // sanitizer internal. |
| 130 | if (FileName.find("/usr/lib/") != std::string::npos) |
| 131 | return false; |
| 132 | if (FileName.find("/usr/include/") != std::string::npos) |
| 133 | return false; |
| 134 | if (FileName == "<null>") |
| 135 | return false; |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void RawPrint(const char *Str) { |
| 141 | write(2, Str, strlen(Str)); |
| 142 | } |
| 143 | |
| 144 | } // namespace fuzzer |
| 145 | |
| 146 | #endif // LIBFUZZER_POSIX |