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 int Res = setitimer(ITIMER_REAL, &T, nullptr);
57 struct sigaction sigact;
58 memset(&sigact, 0, sizeof(sigact));
59 sigact.sa_sigaction = AlarmHandler;
60 Res = sigaction(SIGALRM, &sigact, 0);
64 int NumberOfCpuCores() {
65 FILE *F = popen("nproc", "r");
72 int ExecuteCommand(const std::string &Command) {
73 return system(Command.c_str());
76 bool ToASCII(Unit &U) {
81 if (!isspace(NewX) && !isprint(NewX))
89 bool IsASCII(const Unit &U) {
91 if (!(isprint(X) || isspace(X))) return false;
95 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
97 if (Str.empty()) return false;
98 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
99 // Skip spaces from both sides.
100 while (L < R && isspace(Str[L])) L++;
101 while (R > L && isspace(Str[R])) R--;
102 if (R - L < 2) return false;
103 // Check the closing "
104 if (Str[R] != '"') return false;
106 // Find the opening "
107 while (L < R && Str[L] != '"') L++;
108 if (L >= R) return false;
109 assert(Str[L] == '\"');
112 for (size_t Pos = L; Pos <= R; Pos++) {
113 uint8_t V = (uint8_t)Str[Pos];
114 if (!isprint(V) && !isspace(V)) return false;
117 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
118 U->push_back(Str[Pos + 1]);
123 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
124 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
126 Hex[2] = Str[Pos + 2];
127 Hex[3] = Str[Pos + 3];
128 U->push_back(strtol(Hex, nullptr, 16));
132 return false; // Invalid escape.
134 // Any other character.
141 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
143 Printf("ParseDictionaryFile: file does not exist or is empty\n");
146 std::istringstream ISS(Text);
151 while (std::getline(ISS, S, '\n')) {
154 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
155 if (Pos == S.size()) continue; // Empty line.
156 if (S[Pos] == '#') continue; // Comment line.
157 if (ParseOneDictionaryEntry(S, &U)) {
160 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
168 int GetPid() { return getpid(); }
170 } // namespace fuzzer