Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/tools/fuzzing/libfuzzer/FuzzerIOPosix.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
// IO functions implementation using Posix API.
10
//===----------------------------------------------------------------------===//
11
#include "FuzzerDefs.h"
12
#if LIBFUZZER_POSIX || LIBFUZZER_FUCHSIA
13
14
#include "FuzzerExtFunctions.h"
15
#include "FuzzerIO.h"
16
#include <cstdarg>
17
#include <cstdio>
18
#include <dirent.h>
19
#include <fstream>
20
#include <iterator>
21
#include <libgen.h>
22
#include <sys/stat.h>
23
#include <sys/types.h>
24
#include <unistd.h>
25
26
namespace fuzzer {
27
28
3
bool IsFile(const std::string &Path) {
29
3
  struct stat St;
30
3
  if (stat(Path.c_str(), &St))
31
0
    return false;
32
3
  return S_ISREG(St.st_mode);
33
3
}
34
35
0
static bool IsDirectory(const std::string &Path) {
36
0
  struct stat St;
37
0
  if (stat(Path.c_str(), &St))
38
0
    return false;
39
0
  return S_ISDIR(St.st_mode);
40
0
}
41
42
16.2k
size_t FileSize(const std::string &Path) {
43
16.2k
  struct stat St;
44
16.2k
  if (stat(Path.c_str(), &St))
45
0
    return 0;
46
16.2k
  return St.st_size;
47
16.2k
}
48
49
0
std::string Basename(const std::string &Path) {
50
0
  size_t Pos = Path.rfind(GetSeparator());
51
0
  if (Pos == std::string::npos) return Path;
52
0
  assert(Pos < Path.size());
53
0
  return Path.substr(Pos + 1);
54
0
}
55
56
void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
57
3
                             Vector<std::string> *V, bool TopDir) {
58
3
  auto E = GetEpoch(Dir);
59
3
  if (Epoch)
60
0
    if (E && *Epoch >= E) return;
61
3
62
3
  DIR *D = opendir(Dir.c_str());
63
3
  if (!D) {
64
0
    Printf("%s: %s; exiting\n", strerror(errno), Dir.c_str());
65
0
    exit(1);
66
0
  }
67
16.2k
  while (auto E = readdir(D)) {
68
16.2k
    std::string Path = DirPlusFile(Dir, E->d_name);
69
16.2k
    if (E->d_type == DT_REG || E->d_type == DT_LNK ||
70
16.2k
        (E->d_type == DT_UNKNOWN && IsFile(Path)))
71
16.2k
      V->push_back(Path);
72
6
    else if ((E->d_type == DT_DIR ||
73
6
             (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
74
6
             *E->d_name != '.')
75
0
      ListFilesInDirRecursive(Path, Epoch, V, false);
76
16.2k
  }
77
3
  closedir(D);
78
3
  if (Epoch && TopDir)
79
0
    *Epoch = E;
80
3
}
81
82
16.2k
char GetSeparator() {
83
16.2k
  return '/';
84
16.2k
}
85
86
3
FILE* OpenFile(int Fd, const char* Mode) {
87
3
  return fdopen(Fd, Mode);
88
3
}
89
90
0
int CloseFile(int fd) {
91
0
  return close(fd);
92
0
}
93
94
3
int DuplicateFile(int Fd) {
95
3
  return dup(Fd);
96
3
}
97
98
0
void RemoveFile(const std::string &Path) {
99
0
  unlink(Path.c_str());
100
0
}
101
102
6
void DiscardOutput(int Fd) {
103
6
  FILE* Temp = fopen("/dev/null", "w");
104
6
  if (!Temp)
105
0
    return;
106
6
  dup2(fileno(Temp), Fd);
107
6
  fclose(Temp);
108
6
}
109
110
3
intptr_t GetHandleFromFd(int fd) {
111
3
  return static_cast<intptr_t>(fd);
112
3
}
113
114
0
std::string DirName(const std::string &FileName) {
115
0
  char *Tmp = new char[FileName.size() + 1];
116
0
  memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
117
0
  std::string Res = dirname(Tmp);
118
0
  delete [] Tmp;
119
0
  return Res;
120
0
}
121
122
0
std::string TmpDir() {
123
0
  if (auto Env = getenv("TMPDIR"))
124
0
    return Env;
125
0
  return "/tmp";
126
0
}
127
128
0
bool IsInterestingCoverageFile(const std::string &FileName) {
129
0
  if (FileName.find("compiler-rt/lib/") != std::string::npos)
130
0
    return false; // sanitizer internal.
131
0
  if (FileName.find("/usr/lib/") != std::string::npos)
132
0
    return false;
133
0
  if (FileName.find("/usr/include/") != std::string::npos)
134
0
    return false;
135
0
  if (FileName == "<null>")
136
0
    return false;
137
0
  return true;
138
0
}
139
140
141
0
void RawPrint(const char *Str) {
142
0
  write(2, Str, strlen(Str));
143
0
}
144
145
}  // namespace fuzzer
146
147
#endif // LIBFUZZER_POSIX