10b57cec5SDimitry Andric //===- FuzzerIOWindows.cpp - IO utils for Windows. ------------------------===//
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 functions implementation for Windows.
90b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
105ffd83dbSDimitry Andric #include "FuzzerPlatform.h"
110b57cec5SDimitry Andric #if LIBFUZZER_WINDOWS
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "FuzzerExtFunctions.h"
140b57cec5SDimitry Andric #include "FuzzerIO.h"
150b57cec5SDimitry Andric #include <cstdarg>
160b57cec5SDimitry Andric #include <cstdio>
170b57cec5SDimitry Andric #include <fstream>
180b57cec5SDimitry Andric #include <io.h>
190b57cec5SDimitry Andric #include <iterator>
200b57cec5SDimitry Andric #include <sys/stat.h>
210b57cec5SDimitry Andric #include <sys/types.h>
220b57cec5SDimitry Andric #include <windows.h>
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric namespace fuzzer {
250b57cec5SDimitry Andric
IsFile(const std::string & Path,const DWORD & FileAttributes)260b57cec5SDimitry Andric static bool IsFile(const std::string &Path, const DWORD &FileAttributes) {
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric if (FileAttributes & FILE_ATTRIBUTE_NORMAL)
290b57cec5SDimitry Andric return true;
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric if (FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
320b57cec5SDimitry Andric return false;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric HANDLE FileHandle(
350b57cec5SDimitry Andric CreateFileA(Path.c_str(), 0, FILE_SHARE_READ, NULL, OPEN_EXISTING,
360b57cec5SDimitry Andric FILE_FLAG_BACKUP_SEMANTICS, 0));
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric if (FileHandle == INVALID_HANDLE_VALUE) {
390b57cec5SDimitry Andric Printf("CreateFileA() failed for \"%s\" (Error code: %lu).\n", Path.c_str(),
400b57cec5SDimitry Andric GetLastError());
410b57cec5SDimitry Andric return false;
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric DWORD FileType = GetFileType(FileHandle);
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric if (FileType == FILE_TYPE_UNKNOWN) {
470b57cec5SDimitry Andric Printf("GetFileType() failed for \"%s\" (Error code: %lu).\n", Path.c_str(),
480b57cec5SDimitry Andric GetLastError());
490b57cec5SDimitry Andric CloseHandle(FileHandle);
500b57cec5SDimitry Andric return false;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric if (FileType != FILE_TYPE_DISK) {
540b57cec5SDimitry Andric CloseHandle(FileHandle);
550b57cec5SDimitry Andric return false;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric CloseHandle(FileHandle);
590b57cec5SDimitry Andric return true;
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric
IsFile(const std::string & Path)620b57cec5SDimitry Andric bool IsFile(const std::string &Path) {
630b57cec5SDimitry Andric DWORD Att = GetFileAttributesA(Path.c_str());
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric if (Att == INVALID_FILE_ATTRIBUTES) {
660b57cec5SDimitry Andric Printf("GetFileAttributesA() failed for \"%s\" (Error code: %lu).\n",
670b57cec5SDimitry Andric Path.c_str(), GetLastError());
680b57cec5SDimitry Andric return false;
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric return IsFile(Path, Att);
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
IsDir(DWORD FileAttrs)740b57cec5SDimitry Andric static bool IsDir(DWORD FileAttrs) {
750b57cec5SDimitry Andric if (FileAttrs == INVALID_FILE_ATTRIBUTES) return false;
760b57cec5SDimitry Andric return FileAttrs & FILE_ATTRIBUTE_DIRECTORY;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
IsDirectory(const std::string & Path)79e8d8bef9SDimitry Andric bool IsDirectory(const std::string &Path) {
80e8d8bef9SDimitry Andric DWORD Att = GetFileAttributesA(Path.c_str());
81e8d8bef9SDimitry Andric
82e8d8bef9SDimitry Andric if (Att == INVALID_FILE_ATTRIBUTES) {
83e8d8bef9SDimitry Andric Printf("GetFileAttributesA() failed for \"%s\" (Error code: %lu).\n",
84e8d8bef9SDimitry Andric Path.c_str(), GetLastError());
85e8d8bef9SDimitry Andric return false;
86e8d8bef9SDimitry Andric }
87e8d8bef9SDimitry Andric
88e8d8bef9SDimitry Andric return IsDir(Att);
89e8d8bef9SDimitry Andric }
90e8d8bef9SDimitry Andric
Basename(const std::string & Path)910b57cec5SDimitry Andric std::string Basename(const std::string &Path) {
920b57cec5SDimitry Andric size_t Pos = Path.find_last_of("/\\");
930b57cec5SDimitry Andric if (Pos == std::string::npos) return Path;
940b57cec5SDimitry Andric assert(Pos < Path.size());
950b57cec5SDimitry Andric return Path.substr(Pos + 1);
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric
FileSize(const std::string & Path)980b57cec5SDimitry Andric size_t FileSize(const std::string &Path) {
990b57cec5SDimitry Andric WIN32_FILE_ATTRIBUTE_DATA attr;
1000b57cec5SDimitry Andric if (!GetFileAttributesExA(Path.c_str(), GetFileExInfoStandard, &attr)) {
1010b57cec5SDimitry Andric DWORD LastError = GetLastError();
1020b57cec5SDimitry Andric if (LastError != ERROR_FILE_NOT_FOUND)
1030b57cec5SDimitry Andric Printf("GetFileAttributesExA() failed for \"%s\" (Error code: %lu).\n",
1040b57cec5SDimitry Andric Path.c_str(), LastError);
1050b57cec5SDimitry Andric return 0;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric ULARGE_INTEGER size;
1080b57cec5SDimitry Andric size.HighPart = attr.nFileSizeHigh;
1090b57cec5SDimitry Andric size.LowPart = attr.nFileSizeLow;
1100b57cec5SDimitry Andric return size.QuadPart;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric
ListFilesInDirRecursive(const std::string & Dir,long * Epoch,std::vector<std::string> * V,bool TopDir)1130b57cec5SDimitry Andric void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
114349cc55cSDimitry Andric std::vector<std::string> *V, bool TopDir) {
1150b57cec5SDimitry Andric auto E = GetEpoch(Dir);
1160b57cec5SDimitry Andric if (Epoch)
1170b57cec5SDimitry Andric if (E && *Epoch >= E) return;
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric std::string Path(Dir);
1200b57cec5SDimitry Andric assert(!Path.empty());
1210b57cec5SDimitry Andric if (Path.back() != '\\')
1220b57cec5SDimitry Andric Path.push_back('\\');
1230b57cec5SDimitry Andric Path.push_back('*');
1240b57cec5SDimitry Andric
1250b57cec5SDimitry Andric // Get the first directory entry.
1260b57cec5SDimitry Andric WIN32_FIND_DATAA FindInfo;
1270b57cec5SDimitry Andric HANDLE FindHandle(FindFirstFileA(Path.c_str(), &FindInfo));
1280b57cec5SDimitry Andric if (FindHandle == INVALID_HANDLE_VALUE)
1290b57cec5SDimitry Andric {
1300b57cec5SDimitry Andric if (GetLastError() == ERROR_FILE_NOT_FOUND)
1310b57cec5SDimitry Andric return;
1320b57cec5SDimitry Andric Printf("No such file or directory: %s; exiting\n", Dir.c_str());
1330b57cec5SDimitry Andric exit(1);
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric do {
1370b57cec5SDimitry Andric std::string FileName = DirPlusFile(Dir, FindInfo.cFileName);
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric if (FindInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1400b57cec5SDimitry Andric size_t FilenameLen = strlen(FindInfo.cFileName);
1410b57cec5SDimitry Andric if ((FilenameLen == 1 && FindInfo.cFileName[0] == '.') ||
1420b57cec5SDimitry Andric (FilenameLen == 2 && FindInfo.cFileName[0] == '.' &&
1430b57cec5SDimitry Andric FindInfo.cFileName[1] == '.'))
1440b57cec5SDimitry Andric continue;
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric ListFilesInDirRecursive(FileName, Epoch, V, false);
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric else if (IsFile(FileName, FindInfo.dwFileAttributes))
1490b57cec5SDimitry Andric V->push_back(FileName);
1500b57cec5SDimitry Andric } while (FindNextFileA(FindHandle, &FindInfo));
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric DWORD LastError = GetLastError();
1530b57cec5SDimitry Andric if (LastError != ERROR_NO_MORE_FILES)
1540b57cec5SDimitry Andric Printf("FindNextFileA failed (Error code: %lu).\n", LastError);
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric FindClose(FindHandle);
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric if (Epoch && TopDir)
1590b57cec5SDimitry Andric *Epoch = E;
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric
IterateDirRecursive(const std::string & Dir,void (* DirPreCallback)(const std::string & Dir),void (* DirPostCallback)(const std::string & Dir),void (* FileCallback)(const std::string & Dir))1620b57cec5SDimitry Andric void IterateDirRecursive(const std::string &Dir,
1630b57cec5SDimitry Andric void (*DirPreCallback)(const std::string &Dir),
1640b57cec5SDimitry Andric void (*DirPostCallback)(const std::string &Dir),
1650b57cec5SDimitry Andric void (*FileCallback)(const std::string &Dir)) {
1660b57cec5SDimitry Andric // TODO(metzman): Implement ListFilesInDirRecursive via this function.
1670b57cec5SDimitry Andric DirPreCallback(Dir);
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric DWORD DirAttrs = GetFileAttributesA(Dir.c_str());
1700b57cec5SDimitry Andric if (!IsDir(DirAttrs)) return;
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric std::string TargetDir(Dir);
1730b57cec5SDimitry Andric assert(!TargetDir.empty());
1740b57cec5SDimitry Andric if (TargetDir.back() != '\\') TargetDir.push_back('\\');
1750b57cec5SDimitry Andric TargetDir.push_back('*');
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric WIN32_FIND_DATAA FindInfo;
1780b57cec5SDimitry Andric // Find the directory's first file.
1790b57cec5SDimitry Andric HANDLE FindHandle = FindFirstFileA(TargetDir.c_str(), &FindInfo);
1800b57cec5SDimitry Andric if (FindHandle == INVALID_HANDLE_VALUE) {
1810b57cec5SDimitry Andric DWORD LastError = GetLastError();
1820b57cec5SDimitry Andric if (LastError != ERROR_FILE_NOT_FOUND) {
1830b57cec5SDimitry Andric // If the directory isn't empty, then something abnormal is going on.
1840b57cec5SDimitry Andric Printf("FindFirstFileA failed for %s (Error code: %lu).\n", Dir.c_str(),
1850b57cec5SDimitry Andric LastError);
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric return;
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric do {
1910b57cec5SDimitry Andric std::string Path = DirPlusFile(Dir, FindInfo.cFileName);
1920b57cec5SDimitry Andric DWORD PathAttrs = FindInfo.dwFileAttributes;
1930b57cec5SDimitry Andric if (IsDir(PathAttrs)) {
1940b57cec5SDimitry Andric // Is Path the current directory (".") or the parent ("..")?
1950b57cec5SDimitry Andric if (strcmp(FindInfo.cFileName, ".") == 0 ||
1960b57cec5SDimitry Andric strcmp(FindInfo.cFileName, "..") == 0)
1970b57cec5SDimitry Andric continue;
1980b57cec5SDimitry Andric IterateDirRecursive(Path, DirPreCallback, DirPostCallback, FileCallback);
1990b57cec5SDimitry Andric } else if (PathAttrs != INVALID_FILE_ATTRIBUTES) {
2000b57cec5SDimitry Andric FileCallback(Path);
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric } while (FindNextFileA(FindHandle, &FindInfo));
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric DWORD LastError = GetLastError();
2050b57cec5SDimitry Andric if (LastError != ERROR_NO_MORE_FILES)
2060b57cec5SDimitry Andric Printf("FindNextFileA failed for %s (Error code: %lu).\n", Dir.c_str(),
2070b57cec5SDimitry Andric LastError);
2080b57cec5SDimitry Andric
2090b57cec5SDimitry Andric FindClose(FindHandle);
2100b57cec5SDimitry Andric DirPostCallback(Dir);
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric
GetSeparator()2130b57cec5SDimitry Andric char GetSeparator() {
2140b57cec5SDimitry Andric return '\\';
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
OpenFile(int Fd,const char * Mode)2170b57cec5SDimitry Andric FILE* OpenFile(int Fd, const char* Mode) {
2180b57cec5SDimitry Andric return _fdopen(Fd, Mode);
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric
CloseFile(int Fd)2210b57cec5SDimitry Andric int CloseFile(int Fd) {
2220b57cec5SDimitry Andric return _close(Fd);
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric
DuplicateFile(int Fd)2250b57cec5SDimitry Andric int DuplicateFile(int Fd) {
2260b57cec5SDimitry Andric return _dup(Fd);
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric
RemoveFile(const std::string & Path)2290b57cec5SDimitry Andric void RemoveFile(const std::string &Path) {
2300b57cec5SDimitry Andric _unlink(Path.c_str());
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric
RenameFile(const std::string & OldPath,const std::string & NewPath)2330b57cec5SDimitry Andric void RenameFile(const std::string &OldPath, const std::string &NewPath) {
2340b57cec5SDimitry Andric rename(OldPath.c_str(), NewPath.c_str());
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
GetHandleFromFd(int fd)2370b57cec5SDimitry Andric intptr_t GetHandleFromFd(int fd) {
2380b57cec5SDimitry Andric return _get_osfhandle(fd);
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric
IsSeparator(char C)241e8d8bef9SDimitry Andric bool IsSeparator(char C) {
2420b57cec5SDimitry Andric return C == '\\' || C == '/';
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric // Parse disk designators, like "C:\". If Relative == true, also accepts: "C:".
2460b57cec5SDimitry Andric // Returns number of characters considered if successful.
ParseDrive(const std::string & FileName,const size_t Offset,bool Relative=true)2470b57cec5SDimitry Andric static size_t ParseDrive(const std::string &FileName, const size_t Offset,
2480b57cec5SDimitry Andric bool Relative = true) {
2490b57cec5SDimitry Andric if (Offset + 1 >= FileName.size() || FileName[Offset + 1] != ':')
2500b57cec5SDimitry Andric return 0;
2510b57cec5SDimitry Andric if (Offset + 2 >= FileName.size() || !IsSeparator(FileName[Offset + 2])) {
2520b57cec5SDimitry Andric if (!Relative) // Accept relative path?
2530b57cec5SDimitry Andric return 0;
2540b57cec5SDimitry Andric else
2550b57cec5SDimitry Andric return 2;
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric return 3;
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric // Parse a file name, like: SomeFile.txt
2610b57cec5SDimitry Andric // Returns number of characters considered if successful.
ParseFileName(const std::string & FileName,const size_t Offset)2620b57cec5SDimitry Andric static size_t ParseFileName(const std::string &FileName, const size_t Offset) {
2630b57cec5SDimitry Andric size_t Pos = Offset;
2640b57cec5SDimitry Andric const size_t End = FileName.size();
2650b57cec5SDimitry Andric for(; Pos < End && !IsSeparator(FileName[Pos]); ++Pos)
2660b57cec5SDimitry Andric ;
2670b57cec5SDimitry Andric return Pos - Offset;
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric // Parse a directory ending in separator, like: `SomeDir\`
2710b57cec5SDimitry Andric // Returns number of characters considered if successful.
ParseDir(const std::string & FileName,const size_t Offset)2720b57cec5SDimitry Andric static size_t ParseDir(const std::string &FileName, const size_t Offset) {
2730b57cec5SDimitry Andric size_t Pos = Offset;
2740b57cec5SDimitry Andric const size_t End = FileName.size();
2750b57cec5SDimitry Andric if (Pos >= End || IsSeparator(FileName[Pos]))
2760b57cec5SDimitry Andric return 0;
2770b57cec5SDimitry Andric for(; Pos < End && !IsSeparator(FileName[Pos]); ++Pos)
2780b57cec5SDimitry Andric ;
2790b57cec5SDimitry Andric if (Pos >= End)
2800b57cec5SDimitry Andric return 0;
2810b57cec5SDimitry Andric ++Pos; // Include separator.
2820b57cec5SDimitry Andric return Pos - Offset;
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric
2850b57cec5SDimitry Andric // Parse a servername and share, like: `SomeServer\SomeShare\`
2860b57cec5SDimitry Andric // Returns number of characters considered if successful.
ParseServerAndShare(const std::string & FileName,const size_t Offset)2870b57cec5SDimitry Andric static size_t ParseServerAndShare(const std::string &FileName,
2880b57cec5SDimitry Andric const size_t Offset) {
2890b57cec5SDimitry Andric size_t Pos = Offset, Res;
2900b57cec5SDimitry Andric if (!(Res = ParseDir(FileName, Pos)))
2910b57cec5SDimitry Andric return 0;
2920b57cec5SDimitry Andric Pos += Res;
2930b57cec5SDimitry Andric if (!(Res = ParseDir(FileName, Pos)))
2940b57cec5SDimitry Andric return 0;
2950b57cec5SDimitry Andric Pos += Res;
2960b57cec5SDimitry Andric return Pos - Offset;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric
299349cc55cSDimitry Andric // Parse the given Ref string from the position Offset, to exactly match the
300349cc55cSDimitry Andric // given string Patt. Returns number of characters considered if successful.
ParseCustomString(const std::string & Ref,size_t Offset,const char * Patt)3010b57cec5SDimitry Andric static size_t ParseCustomString(const std::string &Ref, size_t Offset,
3020b57cec5SDimitry Andric const char *Patt) {
3030b57cec5SDimitry Andric size_t Len = strlen(Patt);
3040b57cec5SDimitry Andric if (Offset + Len > Ref.size())
3050b57cec5SDimitry Andric return 0;
3060b57cec5SDimitry Andric return Ref.compare(Offset, Len, Patt) == 0 ? Len : 0;
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andric // Parse a location, like:
3100b57cec5SDimitry Andric // \\?\UNC\Server\Share\ \\?\C:\ \\Server\Share\ \ C:\ C:
3110b57cec5SDimitry Andric // Returns number of characters considered if successful.
ParseLocation(const std::string & FileName)3120b57cec5SDimitry Andric static size_t ParseLocation(const std::string &FileName) {
3130b57cec5SDimitry Andric size_t Pos = 0, Res;
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andric if ((Res = ParseCustomString(FileName, Pos, R"(\\?\)"))) {
3160b57cec5SDimitry Andric Pos += Res;
3170b57cec5SDimitry Andric if ((Res = ParseCustomString(FileName, Pos, R"(UNC\)"))) {
3180b57cec5SDimitry Andric Pos += Res;
3190b57cec5SDimitry Andric if ((Res = ParseServerAndShare(FileName, Pos)))
3200b57cec5SDimitry Andric return Pos + Res;
3210b57cec5SDimitry Andric return 0;
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric if ((Res = ParseDrive(FileName, Pos, false)))
3240b57cec5SDimitry Andric return Pos + Res;
3250b57cec5SDimitry Andric return 0;
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric
3280b57cec5SDimitry Andric if (Pos < FileName.size() && IsSeparator(FileName[Pos])) {
3290b57cec5SDimitry Andric ++Pos;
3300b57cec5SDimitry Andric if (Pos < FileName.size() && IsSeparator(FileName[Pos])) {
3310b57cec5SDimitry Andric ++Pos;
3320b57cec5SDimitry Andric if ((Res = ParseServerAndShare(FileName, Pos)))
3330b57cec5SDimitry Andric return Pos + Res;
3340b57cec5SDimitry Andric return 0;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric return Pos;
3370b57cec5SDimitry Andric }
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andric if ((Res = ParseDrive(FileName, Pos)))
3400b57cec5SDimitry Andric return Pos + Res;
3410b57cec5SDimitry Andric
3420b57cec5SDimitry Andric return Pos;
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric
DirName(const std::string & FileName)3450b57cec5SDimitry Andric std::string DirName(const std::string &FileName) {
3460b57cec5SDimitry Andric size_t LocationLen = ParseLocation(FileName);
3470b57cec5SDimitry Andric size_t DirLen = 0, Res;
3480b57cec5SDimitry Andric while ((Res = ParseDir(FileName, LocationLen + DirLen)))
3490b57cec5SDimitry Andric DirLen += Res;
3500b57cec5SDimitry Andric size_t FileLen = ParseFileName(FileName, LocationLen + DirLen);
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andric if (LocationLen + DirLen + FileLen != FileName.size()) {
3530b57cec5SDimitry Andric Printf("DirName() failed for \"%s\", invalid path.\n", FileName.c_str());
3540b57cec5SDimitry Andric exit(1);
3550b57cec5SDimitry Andric }
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric if (DirLen) {
3580b57cec5SDimitry Andric --DirLen; // Remove trailing separator.
3590b57cec5SDimitry Andric if (!FileLen) { // Path ended in separator.
3600b57cec5SDimitry Andric assert(DirLen);
3610b57cec5SDimitry Andric // Remove file name from Dir.
3620b57cec5SDimitry Andric while (DirLen && !IsSeparator(FileName[LocationLen + DirLen - 1]))
3630b57cec5SDimitry Andric --DirLen;
3640b57cec5SDimitry Andric if (DirLen) // Remove trailing separator.
3650b57cec5SDimitry Andric --DirLen;
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric }
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric if (!LocationLen) { // Relative path.
3700b57cec5SDimitry Andric if (!DirLen)
3710b57cec5SDimitry Andric return ".";
3720b57cec5SDimitry Andric return std::string(".\\").append(FileName, 0, DirLen);
3730b57cec5SDimitry Andric }
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andric return FileName.substr(0, LocationLen + DirLen);
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric
TmpDir()3780b57cec5SDimitry Andric std::string TmpDir() {
3790b57cec5SDimitry Andric std::string Tmp;
3800b57cec5SDimitry Andric Tmp.resize(MAX_PATH + 1);
3810b57cec5SDimitry Andric DWORD Size = GetTempPathA(Tmp.size(), &Tmp[0]);
3820b57cec5SDimitry Andric if (Size == 0) {
3830b57cec5SDimitry Andric Printf("Couldn't get Tmp path.\n");
3840b57cec5SDimitry Andric exit(1);
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric Tmp.resize(Size);
3870b57cec5SDimitry Andric return Tmp;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric
IsInterestingCoverageFile(const std::string & FileName)3900b57cec5SDimitry Andric bool IsInterestingCoverageFile(const std::string &FileName) {
3910b57cec5SDimitry Andric if (FileName.find("Program Files") != std::string::npos)
3920b57cec5SDimitry Andric return false;
3930b57cec5SDimitry Andric if (FileName.find("compiler-rt\\lib\\") != std::string::npos)
3940b57cec5SDimitry Andric return false; // sanitizer internal.
3950b57cec5SDimitry Andric if (FileName == "<null>")
3960b57cec5SDimitry Andric return false;
3970b57cec5SDimitry Andric return true;
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric
RawPrint(const char * Str)4000b57cec5SDimitry Andric void RawPrint(const char *Str) {
4010b57cec5SDimitry Andric _write(2, Str, strlen(Str));
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric
MkDir(const std::string & Path)4040b57cec5SDimitry Andric void MkDir(const std::string &Path) {
4050b57cec5SDimitry Andric if (CreateDirectoryA(Path.c_str(), nullptr)) return;
4060b57cec5SDimitry Andric Printf("CreateDirectoryA failed for %s (Error code: %lu).\n", Path.c_str(),
4070b57cec5SDimitry Andric GetLastError());
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric
RmDir(const std::string & Path)4100b57cec5SDimitry Andric void RmDir(const std::string &Path) {
4110b57cec5SDimitry Andric if (RemoveDirectoryA(Path.c_str())) return;
4120b57cec5SDimitry Andric Printf("RemoveDirectoryA failed for %s (Error code: %lu).\n", Path.c_str(),
4130b57cec5SDimitry Andric GetLastError());
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric
getDevNull()4160b57cec5SDimitry Andric const std::string &getDevNull() {
4170b57cec5SDimitry Andric static const std::string devNull = "NUL";
4180b57cec5SDimitry Andric return devNull;
4190b57cec5SDimitry Andric }
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andric } // namespace fuzzer
4220b57cec5SDimitry Andric
4230b57cec5SDimitry Andric #endif // LIBFUZZER_WINDOWS
424