xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerIO.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric // IO interface.
90b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #ifndef LLVM_FUZZER_IO_H
120b57cec5SDimitry Andric #define LLVM_FUZZER_IO_H
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "FuzzerDefs.h"
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric namespace fuzzer {
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric long GetEpoch(const std::string &Path);
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
210b57cec5SDimitry Andric                   bool ExitOnError = true);
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric std::string FileToString(const std::string &Path);
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric void CopyFileToErr(const std::string &Path);
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path);
280b57cec5SDimitry Andric // Write Data.c_str() to the file without terminating null character.
290b57cec5SDimitry Andric void WriteToFile(const std::string &Data, const std::string &Path);
300b57cec5SDimitry Andric void WriteToFile(const Unit &U, const std::string &Path);
310b57cec5SDimitry Andric 
32e8d8bef9SDimitry Andric void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path);
33e8d8bef9SDimitry Andric void AppendToFile(const std::string &Data, const std::string &Path);
34e8d8bef9SDimitry Andric 
35*fe6060f1SDimitry Andric void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V, long *Epoch,
36*fe6060f1SDimitry Andric                             size_t MaxSize, bool ExitOnError,
37*fe6060f1SDimitry Andric                             Vector<std::string> *VPaths = 0);
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric // Returns "Dir/FileName" or equivalent for the current OS.
400b57cec5SDimitry Andric std::string DirPlusFile(const std::string &DirPath,
410b57cec5SDimitry Andric                         const std::string &FileName);
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric // Returns the name of the dir, similar to the 'dirname' utility.
440b57cec5SDimitry Andric std::string DirName(const std::string &FileName);
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric // Returns path to a TmpDir.
470b57cec5SDimitry Andric std::string TmpDir();
480b57cec5SDimitry Andric 
495ffd83dbSDimitry Andric std::string TempPath(const char *Prefix, const char *Extension);
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric bool IsInterestingCoverageFile(const std::string &FileName);
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric void DupAndCloseStderr();
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric void CloseStdout();
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric void Printf(const char *Fmt, ...);
580b57cec5SDimitry Andric void VPrintf(bool Verbose, const char *Fmt, ...);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric // Print using raw syscalls, useful when printing at early init stages.
610b57cec5SDimitry Andric void RawPrint(const char *Str);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric // Platform specific functions:
640b57cec5SDimitry Andric bool IsFile(const std::string &Path);
65e8d8bef9SDimitry Andric bool IsDirectory(const std::string &Path);
660b57cec5SDimitry Andric size_t FileSize(const std::string &Path);
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
690b57cec5SDimitry Andric                              Vector<std::string> *V, bool TopDir);
700b57cec5SDimitry Andric 
71e8d8bef9SDimitry Andric bool MkDirRecursive(const std::string &Dir);
720b57cec5SDimitry Andric void RmDirRecursive(const std::string &Dir);
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric // Iterate files and dirs inside Dir, recursively.
750b57cec5SDimitry Andric // Call DirPreCallback/DirPostCallback on dirs before/after
760b57cec5SDimitry Andric // calling FileCallback on files.
770b57cec5SDimitry Andric void IterateDirRecursive(const std::string &Dir,
780b57cec5SDimitry Andric                          void (*DirPreCallback)(const std::string &Dir),
790b57cec5SDimitry Andric                          void (*DirPostCallback)(const std::string &Dir),
800b57cec5SDimitry Andric                          void (*FileCallback)(const std::string &Dir));
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric struct SizedFile {
830b57cec5SDimitry Andric   std::string File;
840b57cec5SDimitry Andric   size_t Size;
850b57cec5SDimitry Andric   bool operator<(const SizedFile &B) const { return Size < B.Size; }
860b57cec5SDimitry Andric };
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric char GetSeparator();
91e8d8bef9SDimitry Andric bool IsSeparator(char C);
920b57cec5SDimitry Andric // Similar to the basename utility: returns the file name w/o the dir prefix.
930b57cec5SDimitry Andric std::string Basename(const std::string &Path);
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric FILE* OpenFile(int Fd, const char *Mode);
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric int CloseFile(int Fd);
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric int DuplicateFile(int Fd);
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric void RemoveFile(const std::string &Path);
1020b57cec5SDimitry Andric void RenameFile(const std::string &OldPath, const std::string &NewPath);
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric intptr_t GetHandleFromFd(int fd);
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric void MkDir(const std::string &Path);
1070b57cec5SDimitry Andric void RmDir(const std::string &Path);
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric const std::string &getDevNull();
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric }  // namespace fuzzer
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric #endif  // LLVM_FUZZER_IO_H
114