[libFuzzer] print a stack trace on timeout
[oota-llvm.git] / lib / Fuzzer / FuzzerUtil.cpp
1 //===- FuzzerUtil.cpp - Misc utils ----------------------------------------===//
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 // Misc utils.
10 //===----------------------------------------------------------------------===//
11
12 #include "FuzzerInternal.h"
13 #include <sstream>
14 #include <iomanip>
15 #include <sys/time.h>
16 #include <cassert>
17 #include <cstring>
18 #include <signal.h>
19 #include <sstream>
20 #include <unistd.h>
21
22 namespace fuzzer {
23
24 void Print(const Unit &v, const char *PrintAfter) {
25   for (auto x : v)
26     Printf("0x%x,", (unsigned) x);
27   Printf("%s", PrintAfter);
28 }
29
30 void PrintASCII(const Unit &U, const char *PrintAfter) {
31   for (auto X : U) {
32     if (isprint(X))
33       Printf("%c", X);
34     else
35       Printf("\\x%x", (unsigned)X);
36   }
37   Printf("%s", PrintAfter);
38 }
39
40 std::string Hash(const Unit &U) {
41   uint8_t Hash[kSHA1NumBytes];
42   ComputeSHA1(U.data(), U.size(), Hash);
43   std::stringstream SS;
44   for (int i = 0; i < kSHA1NumBytes; i++)
45     SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
46   return SS.str();
47 }
48
49 static void AlarmHandler(int, siginfo_t *, void *) {
50   Fuzzer::StaticAlarmCallback();
51 }
52
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);
57   assert(Res == 0);
58   struct sigaction sigact;
59   memset(&sigact, 0, sizeof(sigact));
60   sigact.sa_sigaction = AlarmHandler;
61   Res = sigaction(SIGALRM, &sigact, 0);
62   assert(Res == 0);
63 }
64
65 int NumberOfCpuCores() {
66   FILE *F = popen("nproc", "r");
67   int N = 0;
68   fscanf(F, "%d", &N);
69   fclose(F);
70   return N;
71 }
72
73 void ExecuteCommand(const std::string &Command) {
74   system(Command.c_str());
75 }
76
77 bool ToASCII(Unit &U) {
78   bool Changed = false;
79   for (auto &X : U) {
80     auto NewX = X;
81     NewX &= 127;
82     if (!isspace(NewX) && !isprint(NewX))
83       NewX = ' ';
84     Changed |= NewX != X;
85     X = NewX;
86   }
87   return Changed;
88 }
89
90 bool IsASCII(const Unit &U) {
91   for (auto X : U)
92     if (!(isprint(X) || isspace(X))) return false;
93   return true;
94 }
95
96 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
97   U->clear();
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;
106   R--;
107   // Find the opening "
108   while (L < R && Str[L] != '"') L++;
109   if (L >= R) return false;
110   assert(Str[L] == '\"');
111   L++;
112   assert(L <= R);
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;
116     if (V =='\\') {
117       // Handle '\\'
118       if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
119         U->push_back(Str[Pos + 1]);
120         Pos++;
121         continue;
122       }
123       // Handle '\xAB'
124       if (Pos + 3 <= R && Str[Pos + 1] == 'x'
125            && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
126         char Hex[] = "0xAA";
127         Hex[2] = Str[Pos + 2];
128         Hex[3] = Str[Pos + 3];
129         U->push_back(strtol(Hex, nullptr, 16));
130         Pos += 3;
131         continue;
132       }
133       return false;  // Invalid escape.
134     } else {
135       // Any other character.
136       U->push_back(V);
137     }
138   }
139   return true;
140 }
141
142 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
143   if (Text.empty()) {
144     Printf("ParseDictionaryFile: file does not exist or is empty\n");
145     return false;
146   }
147   std::istringstream ISS(Text);
148   Units->clear();
149   Unit U;
150   int LineNo = 0;
151   std::string S;
152   while (std::getline(ISS, S, '\n')) {
153     LineNo++;
154     size_t Pos = 0;
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)) {
159       Units->push_back(U);
160     } else {
161       Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
162              S.c_str());
163       return false;
164     }
165   }
166   return true;
167 }
168
169 int GetPid() { return getpid(); }
170
171 }  // namespace fuzzer