FuzzerIOPosix.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // IO functions implementation using Posix API.
  9. //===----------------------------------------------------------------------===//
  10. #include "FuzzerPlatform.h"
  11. #if LIBFUZZER_POSIX || LIBFUZZER_FUCHSIA
  12. #include "FuzzerExtFunctions.h"
  13. #include "FuzzerIO.h"
  14. #include <cstdarg>
  15. #include <cstdio>
  16. #include <dirent.h>
  17. #include <fstream>
  18. #include <iterator>
  19. #include <libgen.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. namespace fuzzer {
  24. bool IsFile(const std::string &Path) {
  25. struct stat St;
  26. if (stat(Path.c_str(), &St))
  27. return false;
  28. return S_ISREG(St.st_mode);
  29. }
  30. bool IsDirectory(const std::string &Path) {
  31. struct stat St;
  32. if (stat(Path.c_str(), &St))
  33. return false;
  34. return S_ISDIR(St.st_mode);
  35. }
  36. size_t FileSize(const std::string &Path) {
  37. struct stat St;
  38. if (stat(Path.c_str(), &St))
  39. return 0;
  40. return St.st_size;
  41. }
  42. std::string Basename(const std::string &Path) {
  43. size_t Pos = Path.rfind(GetSeparator());
  44. if (Pos == std::string::npos) return Path;
  45. assert(Pos < Path.size());
  46. return Path.substr(Pos + 1);
  47. }
  48. void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
  49. std::vector<std::string> *V, bool TopDir) {
  50. auto E = GetEpoch(Dir);
  51. if (Epoch)
  52. if (E && *Epoch >= E) return;
  53. DIR *D = opendir(Dir.c_str());
  54. if (!D) {
  55. Printf("%s: %s; exiting\n", strerror(errno), Dir.c_str());
  56. exit(1);
  57. }
  58. while (auto E = readdir(D)) {
  59. std::string Path = DirPlusFile(Dir, E->d_name);
  60. if (E->d_type == DT_REG || E->d_type == DT_LNK ||
  61. (E->d_type == DT_UNKNOWN && IsFile(Path)))
  62. V->push_back(Path);
  63. else if ((E->d_type == DT_DIR ||
  64. (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
  65. *E->d_name != '.')
  66. ListFilesInDirRecursive(Path, Epoch, V, false);
  67. }
  68. closedir(D);
  69. if (Epoch && TopDir)
  70. *Epoch = E;
  71. }
  72. void IterateDirRecursive(const std::string &Dir,
  73. void (*DirPreCallback)(const std::string &Dir),
  74. void (*DirPostCallback)(const std::string &Dir),
  75. void (*FileCallback)(const std::string &Dir)) {
  76. DirPreCallback(Dir);
  77. DIR *D = opendir(Dir.c_str());
  78. if (!D) return;
  79. while (auto E = readdir(D)) {
  80. std::string Path = DirPlusFile(Dir, E->d_name);
  81. if (E->d_type == DT_REG || E->d_type == DT_LNK ||
  82. (E->d_type == DT_UNKNOWN && IsFile(Path)))
  83. FileCallback(Path);
  84. else if ((E->d_type == DT_DIR ||
  85. (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
  86. *E->d_name != '.')
  87. IterateDirRecursive(Path, DirPreCallback, DirPostCallback, FileCallback);
  88. }
  89. closedir(D);
  90. DirPostCallback(Dir);
  91. }
  92. char GetSeparator() {
  93. return '/';
  94. }
  95. bool IsSeparator(char C) {
  96. return C == '/';
  97. }
  98. FILE* OpenFile(int Fd, const char* Mode) {
  99. return fdopen(Fd, Mode);
  100. }
  101. int CloseFile(int fd) {
  102. return close(fd);
  103. }
  104. int DuplicateFile(int Fd) {
  105. return dup(Fd);
  106. }
  107. void RemoveFile(const std::string &Path) {
  108. unlink(Path.c_str());
  109. }
  110. void RenameFile(const std::string &OldPath, const std::string &NewPath) {
  111. rename(OldPath.c_str(), NewPath.c_str());
  112. }
  113. intptr_t GetHandleFromFd(int fd) {
  114. return static_cast<intptr_t>(fd);
  115. }
  116. std::string DirName(const std::string &FileName) {
  117. char *Tmp = new char[FileName.size() + 1];
  118. memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
  119. std::string Res = dirname(Tmp);
  120. delete [] Tmp;
  121. return Res;
  122. }
  123. std::string TmpDir() {
  124. if (auto Env = getenv("TMPDIR"))
  125. return Env;
  126. return "/tmp";
  127. }
  128. bool IsInterestingCoverageFile(const std::string &FileName) {
  129. if (FileName.find("compiler-rt/lib/") != std::string::npos)
  130. return false; // sanitizer internal.
  131. if (FileName.find("/usr/lib/") != std::string::npos)
  132. return false;
  133. if (FileName.find("/usr/include/") != std::string::npos)
  134. return false;
  135. if (FileName == "<null>")
  136. return false;
  137. return true;
  138. }
  139. void RawPrint(const char *Str) {
  140. (void)write(2, Str, strlen(Str));
  141. }
  142. void MkDir(const std::string &Path) {
  143. mkdir(Path.c_str(), 0700);
  144. }
  145. void RmDir(const std::string &Path) {
  146. rmdir(Path.c_str());
  147. }
  148. const std::string &getDevNull() {
  149. static const std::string devNull = "/dev/null";
  150. return devNull;
  151. }
  152. } // namespace fuzzer
  153. #endif // LIBFUZZER_POSIX