/src/mozilla-central/tools/fuzzing/libfuzzer/FuzzerUtil.cpp
Line | Count | Source (jump to first uncovered line) |
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 "FuzzerUtil.h" |
13 | | #include "FuzzerIO.h" |
14 | | #include "FuzzerInternal.h" |
15 | | #include <cassert> |
16 | | #include <chrono> |
17 | | #include <cstring> |
18 | | #include <errno.h> |
19 | | #include <mutex> |
20 | | #include <signal.h> |
21 | | #include <sstream> |
22 | | #include <stdio.h> |
23 | | #include <sys/types.h> |
24 | | #include <thread> |
25 | | |
26 | | namespace fuzzer { |
27 | | |
28 | | void PrintHexArray(const uint8_t *Data, size_t Size, |
29 | 0 | const char *PrintAfter) { |
30 | 0 | for (size_t i = 0; i < Size; i++) |
31 | 0 | Printf("0x%x,", (unsigned)Data[i]); |
32 | 0 | Printf("%s", PrintAfter); |
33 | 0 | } |
34 | | |
35 | 0 | void Print(const Unit &v, const char *PrintAfter) { |
36 | 0 | PrintHexArray(v.data(), v.size(), PrintAfter); |
37 | 0 | } |
38 | | |
39 | 0 | void PrintASCIIByte(uint8_t Byte) { |
40 | 0 | if (Byte == '\\') |
41 | 0 | Printf("\\\\"); |
42 | 0 | else if (Byte == '"') |
43 | 0 | Printf("\\\""); |
44 | 0 | else if (Byte >= 32 && Byte < 127) |
45 | 0 | Printf("%c", Byte); |
46 | 0 | else |
47 | 0 | Printf("\\x%02x", Byte); |
48 | 0 | } |
49 | | |
50 | 0 | void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) { |
51 | 0 | for (size_t i = 0; i < Size; i++) |
52 | 0 | PrintASCIIByte(Data[i]); |
53 | 0 | Printf("%s", PrintAfter); |
54 | 0 | } |
55 | | |
56 | 0 | void PrintASCII(const Unit &U, const char *PrintAfter) { |
57 | 0 | PrintASCII(U.data(), U.size(), PrintAfter); |
58 | 0 | } |
59 | | |
60 | 0 | bool ToASCII(uint8_t *Data, size_t Size) { |
61 | 0 | bool Changed = false; |
62 | 0 | for (size_t i = 0; i < Size; i++) { |
63 | 0 | uint8_t &X = Data[i]; |
64 | 0 | auto NewX = X; |
65 | 0 | NewX &= 127; |
66 | 0 | if (!isspace(NewX) && !isprint(NewX)) |
67 | 0 | NewX = ' '; |
68 | 0 | Changed |= NewX != X; |
69 | 0 | X = NewX; |
70 | 0 | } |
71 | 0 | return Changed; |
72 | 0 | } |
73 | | |
74 | 0 | bool IsASCII(const Unit &U) { return IsASCII(U.data(), U.size()); } |
75 | | |
76 | 0 | bool IsASCII(const uint8_t *Data, size_t Size) { |
77 | 0 | for (size_t i = 0; i < Size; i++) |
78 | 0 | if (!(isprint(Data[i]) || isspace(Data[i]))) return false; |
79 | 0 | return true; |
80 | 0 | } |
81 | | |
82 | 0 | bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) { |
83 | 0 | U->clear(); |
84 | 0 | if (Str.empty()) return false; |
85 | 0 | size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R]. |
86 | 0 | // Skip spaces from both sides. |
87 | 0 | while (L < R && isspace(Str[L])) L++; |
88 | 0 | while (R > L && isspace(Str[R])) R--; |
89 | 0 | if (R - L < 2) return false; |
90 | 0 | // Check the closing " |
91 | 0 | if (Str[R] != '"') return false; |
92 | 0 | R--; |
93 | 0 | // Find the opening " |
94 | 0 | while (L < R && Str[L] != '"') L++; |
95 | 0 | if (L >= R) return false; |
96 | 0 | assert(Str[L] == '\"'); |
97 | 0 | L++; |
98 | 0 | assert(L <= R); |
99 | 0 | for (size_t Pos = L; Pos <= R; Pos++) { |
100 | 0 | uint8_t V = (uint8_t)Str[Pos]; |
101 | 0 | if (!isprint(V) && !isspace(V)) return false; |
102 | 0 | if (V =='\\') { |
103 | 0 | // Handle '\\' |
104 | 0 | if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) { |
105 | 0 | U->push_back(Str[Pos + 1]); |
106 | 0 | Pos++; |
107 | 0 | continue; |
108 | 0 | } |
109 | 0 | // Handle '\xAB' |
110 | 0 | if (Pos + 3 <= R && Str[Pos + 1] == 'x' |
111 | 0 | && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) { |
112 | 0 | char Hex[] = "0xAA"; |
113 | 0 | Hex[2] = Str[Pos + 2]; |
114 | 0 | Hex[3] = Str[Pos + 3]; |
115 | 0 | U->push_back(strtol(Hex, nullptr, 16)); |
116 | 0 | Pos += 3; |
117 | 0 | continue; |
118 | 0 | } |
119 | 0 | return false; // Invalid escape. |
120 | 0 | } else { |
121 | 0 | // Any other character. |
122 | 0 | U->push_back(V); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | return true; |
126 | 0 | } |
127 | | |
128 | 0 | bool ParseDictionaryFile(const std::string &Text, Vector<Unit> *Units) { |
129 | 0 | if (Text.empty()) { |
130 | 0 | Printf("ParseDictionaryFile: file does not exist or is empty\n"); |
131 | 0 | return false; |
132 | 0 | } |
133 | 0 | std::istringstream ISS(Text); |
134 | 0 | Units->clear(); |
135 | 0 | Unit U; |
136 | 0 | int LineNo = 0; |
137 | 0 | std::string S; |
138 | 0 | while (std::getline(ISS, S, '\n')) { |
139 | 0 | LineNo++; |
140 | 0 | size_t Pos = 0; |
141 | 0 | while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces. |
142 | 0 | if (Pos == S.size()) continue; // Empty line. |
143 | 0 | if (S[Pos] == '#') continue; // Comment line. |
144 | 0 | if (ParseOneDictionaryEntry(S, &U)) { |
145 | 0 | Units->push_back(U); |
146 | 0 | } else { |
147 | 0 | Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo, |
148 | 0 | S.c_str()); |
149 | 0 | return false; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | return true; |
153 | 0 | } |
154 | | |
155 | 0 | std::string Base64(const Unit &U) { |
156 | 0 | static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
157 | 0 | "abcdefghijklmnopqrstuvwxyz" |
158 | 0 | "0123456789+/"; |
159 | 0 | std::string Res; |
160 | 0 | size_t i; |
161 | 0 | for (i = 0; i + 2 < U.size(); i += 3) { |
162 | 0 | uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2]; |
163 | 0 | Res += Table[(x >> 18) & 63]; |
164 | 0 | Res += Table[(x >> 12) & 63]; |
165 | 0 | Res += Table[(x >> 6) & 63]; |
166 | 0 | Res += Table[x & 63]; |
167 | 0 | } |
168 | 0 | if (i + 1 == U.size()) { |
169 | 0 | uint32_t x = (U[i] << 16); |
170 | 0 | Res += Table[(x >> 18) & 63]; |
171 | 0 | Res += Table[(x >> 12) & 63]; |
172 | 0 | Res += "=="; |
173 | 0 | } else if (i + 2 == U.size()) { |
174 | 0 | uint32_t x = (U[i] << 16) + (U[i + 1] << 8); |
175 | 0 | Res += Table[(x >> 18) & 63]; |
176 | 0 | Res += Table[(x >> 12) & 63]; |
177 | 0 | Res += Table[(x >> 6) & 63]; |
178 | 0 | Res += "="; |
179 | 0 | } |
180 | 0 | return Res; |
181 | 0 | } |
182 | | |
183 | | static std::mutex SymbolizeMutex; |
184 | | |
185 | 0 | std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC) { |
186 | 0 | std::unique_lock<std::mutex> l(SymbolizeMutex, std::try_to_lock); |
187 | 0 | if (!EF->__sanitizer_symbolize_pc || !l.owns_lock()) |
188 | 0 | return "<can not symbolize>"; |
189 | 0 | char PcDescr[1024] = {}; |
190 | 0 | EF->__sanitizer_symbolize_pc(reinterpret_cast<void*>(PC), |
191 | 0 | SymbolizedFMT, PcDescr, sizeof(PcDescr)); |
192 | 0 | PcDescr[sizeof(PcDescr) - 1] = 0; // Just in case. |
193 | 0 | return PcDescr; |
194 | 0 | } |
195 | | |
196 | 0 | void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC) { |
197 | 0 | if (EF->__sanitizer_symbolize_pc) |
198 | 0 | Printf("%s", DescribePC(SymbolizedFMT, PC).c_str()); |
199 | 0 | else |
200 | 0 | Printf(FallbackFMT, PC); |
201 | 0 | } |
202 | | |
203 | 0 | void PrintStackTrace() { |
204 | 0 | std::unique_lock<std::mutex> l(SymbolizeMutex, std::try_to_lock); |
205 | 0 | if (EF->__sanitizer_print_stack_trace && l.owns_lock()) |
206 | 0 | EF->__sanitizer_print_stack_trace(); |
207 | 0 | } |
208 | | |
209 | 0 | void PrintMemoryProfile() { |
210 | 0 | std::unique_lock<std::mutex> l(SymbolizeMutex, std::try_to_lock); |
211 | 0 | if (EF->__sanitizer_print_memory_profile && l.owns_lock()) |
212 | 0 | EF->__sanitizer_print_memory_profile(95, 8); |
213 | 0 | } |
214 | | |
215 | 0 | unsigned NumberOfCpuCores() { |
216 | 0 | unsigned N = std::thread::hardware_concurrency(); |
217 | 0 | if (!N) { |
218 | 0 | Printf("WARNING: std::thread::hardware_concurrency not well defined for " |
219 | 0 | "your platform. Assuming CPU count of 1.\n"); |
220 | 0 | N = 1; |
221 | 0 | } |
222 | 0 | return N; |
223 | 0 | } |
224 | | |
225 | 0 | size_t SimpleFastHash(const uint8_t *Data, size_t Size) { |
226 | 0 | size_t Res = 0; |
227 | 0 | for (size_t i = 0; i < Size; i++) |
228 | 0 | Res = Res * 11 + Data[i]; |
229 | 0 | return Res; |
230 | 0 | } |
231 | | |
232 | | } // namespace fuzzer |