FuzzerIO.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//
  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 interface.
  9. //===----------------------------------------------------------------------===//
  10. #ifndef LLVM_FUZZER_IO_H
  11. #define LLVM_FUZZER_IO_H
  12. #include "FuzzerDefs.h"
  13. namespace fuzzer {
  14. long GetEpoch(const std::string &Path);
  15. Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
  16. bool ExitOnError = true);
  17. std::string FileToString(const std::string &Path);
  18. void CopyFileToErr(const std::string &Path);
  19. void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path);
  20. // Write Data.c_str() to the file without terminating null character.
  21. void WriteToFile(const std::string &Data, const std::string &Path);
  22. void WriteToFile(const Unit &U, const std::string &Path);
  23. void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
  24. long *Epoch, size_t MaxSize, bool ExitOnError);
  25. // Returns "Dir/FileName" or equivalent for the current OS.
  26. std::string DirPlusFile(const std::string &DirPath,
  27. const std::string &FileName);
  28. // Returns the name of the dir, similar to the 'dirname' utility.
  29. std::string DirName(const std::string &FileName);
  30. // Returns path to a TmpDir.
  31. std::string TmpDir();
  32. std::string TempPath(const char *Extension);
  33. bool IsInterestingCoverageFile(const std::string &FileName);
  34. void DupAndCloseStderr();
  35. void CloseStdout();
  36. void Printf(const char *Fmt, ...);
  37. void VPrintf(bool Verbose, const char *Fmt, ...);
  38. // Print using raw syscalls, useful when printing at early init stages.
  39. void RawPrint(const char *Str);
  40. // Platform specific functions:
  41. bool IsFile(const std::string &Path);
  42. size_t FileSize(const std::string &Path);
  43. void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
  44. Vector<std::string> *V, bool TopDir);
  45. void RmDirRecursive(const std::string &Dir);
  46. // Iterate files and dirs inside Dir, recursively.
  47. // Call DirPreCallback/DirPostCallback on dirs before/after
  48. // calling FileCallback on files.
  49. void IterateDirRecursive(const std::string &Dir,
  50. void (*DirPreCallback)(const std::string &Dir),
  51. void (*DirPostCallback)(const std::string &Dir),
  52. void (*FileCallback)(const std::string &Dir));
  53. struct SizedFile {
  54. std::string File;
  55. size_t Size;
  56. bool operator<(const SizedFile &B) const { return Size < B.Size; }
  57. };
  58. void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
  59. char GetSeparator();
  60. // Similar to the basename utility: returns the file name w/o the dir prefix.
  61. std::string Basename(const std::string &Path);
  62. FILE* OpenFile(int Fd, const char *Mode);
  63. int CloseFile(int Fd);
  64. int DuplicateFile(int Fd);
  65. void RemoveFile(const std::string &Path);
  66. void RenameFile(const std::string &OldPath, const std::string &NewPath);
  67. void DiscardOutput(int Fd);
  68. intptr_t GetHandleFromFd(int fd);
  69. void MkDir(const std::string &Path);
  70. void RmDir(const std::string &Path);
  71. const std::string &getDevNull();
  72. } // namespace fuzzer
  73. #endif // LLVM_FUZZER_IO_H