1 //===- FuzzerUtil.cpp - Misc utils ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #include "FuzzerInternal.h"
24 void Print(const Unit &v, const char *PrintAfter) {
26 Printf("0x%x,", (unsigned) x);
27 Printf("%s", PrintAfter);
30 void PrintASCII(const Unit &U, const char *PrintAfter) {
35 Printf("\\x%x", (unsigned)X);
37 Printf("%s", PrintAfter);
40 std::string Hash(const Unit &U) {
41 uint8_t Hash[kSHA1NumBytes];
42 ComputeSHA1(U.data(), U.size(), Hash);
44 for (int i = 0; i < kSHA1NumBytes; i++)
45 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
49 static void AlarmHandler(int, siginfo_t *, void *) {
50 Fuzzer::StaticAlarmCallback();
53 void SetTimer(int Seconds) {
54 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
55 Printf("SetTimer %d\n", Seconds);
56 int Res = setitimer(ITIMER_REAL, &T, nullptr);
58 struct sigaction sigact;
59 memset(&sigact, 0, sizeof(sigact));
60 sigact.sa_sigaction = AlarmHandler;
61 Res = sigaction(SIGALRM, &sigact, 0);
65 int NumberOfCpuCores() {
66 FILE *F = popen("nproc", "r");
73 void ExecuteCommand(const std::string &Command) {
74 system(Command.c_str());
77 bool ToASCII(Unit &U) {
82 if (!isspace(NewX) && !isprint(NewX))
90 bool IsASCII(const Unit &U) {
92 if (!(isprint(X) || isspace(X))) return false;
96 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
98 if (Str.empty()) return false;
99 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
100 // Skip spaces from both sides.
101 while (L < R && isspace(Str[L])) L++;
102 while (R > L && isspace(Str[R])) R--;
103 if (R - L < 2) return false;
104 // Check the closing "
105 if (Str[R] != '"') return false;
107 // Find the opening "
108 while (L < R && Str[L] != '"') L++;
109 if (L >= R) return false;
110 assert(Str[L] == '\"');
113 for (size_t Pos = L; Pos <= R; Pos++) {
114 uint8_t V = (uint8_t)Str[Pos];
115 if (!isprint(V) && !isspace(V)) return false;
118 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
119 U->push_back(Str[Pos + 1]);
124 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
125 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
127 Hex[2] = Str[Pos + 2];
128 Hex[3] = Str[Pos + 3];
129 U->push_back(strtol(Hex, nullptr, 16));
133 return false; // Invalid escape.
135 // Any other character.
142 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
144 Printf("ParseDictionaryFile: file does not exist or is empty\n");
147 std::istringstream ISS(Text);
152 while (std::getline(ISS, S, '\n')) {
155 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
156 if (Pos == S.size()) continue; // Empty line.
157 if (S[Pos] == '#') continue; // Comment line.
158 if (ParseOneDictionaryEntry(S, &U)) {
161 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
169 int GetPid() { return getpid(); }
171 } // namespace fuzzer