xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerIO.h (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
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 
11 #ifndef LLVM_FUZZER_IO_H
12 #define LLVM_FUZZER_IO_H
13 
14 #include "FuzzerDefs.h"
15 
16 namespace fuzzer {
17 
18 long GetEpoch(const std::string &Path);
19 
20 Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
21                   bool ExitOnError = true);
22 
23 std::string FileToString(const std::string &Path);
24 
25 void CopyFileToErr(const std::string &Path);
26 
27 void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path);
28 // Write Data.c_str() to the file without terminating null character.
29 void WriteToFile(const std::string &Data, const std::string &Path);
30 void WriteToFile(const Unit &U, const std::string &Path);
31 
32 void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path);
33 void AppendToFile(const std::string &Data, const std::string &Path);
34 
35 void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
36                             long *Epoch, size_t MaxSize, bool ExitOnError);
37 
38 // Returns "Dir/FileName" or equivalent for the current OS.
39 std::string DirPlusFile(const std::string &DirPath,
40                         const std::string &FileName);
41 
42 // Returns the name of the dir, similar to the 'dirname' utility.
43 std::string DirName(const std::string &FileName);
44 
45 // Returns path to a TmpDir.
46 std::string TmpDir();
47 
48 std::string TempPath(const char *Prefix, const char *Extension);
49 
50 bool IsInterestingCoverageFile(const std::string &FileName);
51 
52 void DupAndCloseStderr();
53 
54 void CloseStdout();
55 
56 void Printf(const char *Fmt, ...);
57 void VPrintf(bool Verbose, const char *Fmt, ...);
58 
59 // Print using raw syscalls, useful when printing at early init stages.
60 void RawPrint(const char *Str);
61 
62 // Platform specific functions:
63 bool IsFile(const std::string &Path);
64 bool IsDirectory(const std::string &Path);
65 size_t FileSize(const std::string &Path);
66 
67 void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
68                              Vector<std::string> *V, bool TopDir);
69 
70 bool MkDirRecursive(const std::string &Dir);
71 void RmDirRecursive(const std::string &Dir);
72 
73 // Iterate files and dirs inside Dir, recursively.
74 // Call DirPreCallback/DirPostCallback on dirs before/after
75 // calling FileCallback on files.
76 void IterateDirRecursive(const std::string &Dir,
77                          void (*DirPreCallback)(const std::string &Dir),
78                          void (*DirPostCallback)(const std::string &Dir),
79                          void (*FileCallback)(const std::string &Dir));
80 
81 struct SizedFile {
82   std::string File;
83   size_t Size;
84   bool operator<(const SizedFile &B) const { return Size < B.Size; }
85 };
86 
87 void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
88 
89 char GetSeparator();
90 bool IsSeparator(char C);
91 // Similar to the basename utility: returns the file name w/o the dir prefix.
92 std::string Basename(const std::string &Path);
93 
94 FILE* OpenFile(int Fd, const char *Mode);
95 
96 int CloseFile(int Fd);
97 
98 int DuplicateFile(int Fd);
99 
100 void RemoveFile(const std::string &Path);
101 void RenameFile(const std::string &OldPath, const std::string &NewPath);
102 
103 intptr_t GetHandleFromFd(int fd);
104 
105 void MkDir(const std::string &Path);
106 void RmDir(const std::string &Path);
107 
108 const std::string &getDevNull();
109 
110 }  // namespace fuzzer
111 
112 #endif  // LLVM_FUZZER_IO_H
113