FuzzerIO.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path);
  24. void AppendToFile(const std::string &Data, const std::string &Path);
  25. void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, long *Epoch,
  26. size_t MaxSize, bool ExitOnError,
  27. std::vector<std::string> *VPaths = 0);
  28. // Returns "Dir/FileName" or equivalent for the current OS.
  29. std::string DirPlusFile(const std::string &DirPath,
  30. const std::string &FileName);
  31. // Returns the name of the dir, similar to the 'dirname' utility.
  32. std::string DirName(const std::string &FileName);
  33. // Returns path to a TmpDir.
  34. std::string TmpDir();
  35. std::string TempPath(const char *Prefix, const char *Extension);
  36. bool IsInterestingCoverageFile(const std::string &FileName);
  37. void DupAndCloseStderr();
  38. void CloseStdout();
  39. // For testing.
  40. FILE *GetOutputFile();
  41. void SetOutputFile(FILE *NewOutputFile);
  42. void Printf(const char *Fmt, ...);
  43. void VPrintf(bool Verbose, const char *Fmt, ...);
  44. // Print using raw syscalls, useful when printing at early init stages.
  45. void RawPrint(const char *Str);
  46. // Platform specific functions:
  47. bool IsFile(const std::string &Path);
  48. bool IsDirectory(const std::string &Path);
  49. size_t FileSize(const std::string &Path);
  50. void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
  51. std::vector<std::string> *V, bool TopDir);
  52. bool MkDirRecursive(const std::string &Dir);
  53. void RmDirRecursive(const std::string &Dir);
  54. // Iterate files and dirs inside Dir, recursively.
  55. // Call DirPreCallback/DirPostCallback on dirs before/after
  56. // calling FileCallback on files.
  57. void IterateDirRecursive(const std::string &Dir,
  58. void (*DirPreCallback)(const std::string &Dir),
  59. void (*DirPostCallback)(const std::string &Dir),
  60. void (*FileCallback)(const std::string &Dir));
  61. struct SizedFile {
  62. std::string File;
  63. size_t Size;
  64. bool operator<(const SizedFile &B) const { return Size < B.Size; }
  65. };
  66. void GetSizedFilesFromDir(const std::string &Dir, std::vector<SizedFile> *V);
  67. char GetSeparator();
  68. bool IsSeparator(char C);
  69. // Similar to the basename utility: returns the file name w/o the dir prefix.
  70. std::string Basename(const std::string &Path);
  71. FILE* OpenFile(int Fd, const char *Mode);
  72. int CloseFile(int Fd);
  73. int DuplicateFile(int Fd);
  74. void RemoveFile(const std::string &Path);
  75. void RenameFile(const std::string &OldPath, const std::string &NewPath);
  76. intptr_t GetHandleFromFd(int fd);
  77. void MkDir(const std::string &Path);
  78. void RmDir(const std::string &Path);
  79. const std::string &getDevNull();
  80. } // namespace fuzzer
  81. #endif // LLVM_FUZZER_IO_H