6 #define _LIBCPP_HAS_NO_ASAN
14 #include "gtest/gtest.h"
18 using namespace fuzzer;
31 Unit A({0, 1, 2}),
B({5, 6, 7});
69 for (
size_t Len = 1; Len < 8; Len++) {
70 std::set<Unit> FoundUnits, ExpectedUnitsWitThisLength;
71 for (
int Iter = 0; Iter < 3000; Iter++) {
73 size_t NewSize = MD.CrossOver(
A.data(),
A.size(),
B.data(),
B.size(),
78 for (
const Unit &U : Expected)
80 ExpectedUnitsWitThisLength.insert(U);
81 EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits);
86 uint8_t
A[] = {
'a',
'b',
'c'};
88 EXPECT_EQ(
"a9993e364706816aba3e25717850c26c9cd0d89d",
fuzzer::Hash(U));
90 EXPECT_EQ(
"81fe8bfe87576c3ecb22426f8e57847382917acf",
fuzzer::Hash(U));
99 uint8_t REM0[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
100 uint8_t REM1[8] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
101 uint8_t REM2[8] = {0x00, 0x11, 0x33, 0x44, 0x55, 0x66, 0x77};
102 uint8_t REM3[8] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
103 uint8_t REM4[8] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x66, 0x77};
104 uint8_t REM5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x66, 0x77};
105 uint8_t REM6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x77};
106 uint8_t REM7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
108 uint8_t REM8[6] = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
109 uint8_t REM9[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
110 uint8_t REM10[6] = {0x00, 0x11, 0x22, 0x55, 0x66, 0x77};
112 uint8_t REM11[5] = {0x33, 0x44, 0x55, 0x66, 0x77};
113 uint8_t REM12[5] = {0x00, 0x11, 0x22, 0x33, 0x44};
114 uint8_t REM13[5] = {0x00, 0x44, 0x55, 0x66, 0x77};
120 for (
int i = 0;
i < NumIter;
i++) {
121 uint8_t
T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
122 size_t NewSize = (MD.*M)(T,
sizeof(T),
sizeof(
T));
123 if (NewSize == 7 && !memcmp(REM0, T, 7)) FoundMask |= 1 << 0;
124 if (NewSize == 7 && !memcmp(REM1, T, 7)) FoundMask |= 1 << 1;
125 if (NewSize == 7 && !memcmp(REM2, T, 7)) FoundMask |= 1 << 2;
126 if (NewSize == 7 && !memcmp(REM3, T, 7)) FoundMask |= 1 << 3;
127 if (NewSize == 7 && !memcmp(REM4, T, 7)) FoundMask |= 1 << 4;
128 if (NewSize == 7 && !memcmp(REM5, T, 7)) FoundMask |= 1 << 5;
129 if (NewSize == 7 && !memcmp(REM6, T, 7)) FoundMask |= 1 << 6;
130 if (NewSize == 7 && !memcmp(REM7, T, 7)) FoundMask |= 1 << 7;
132 if (NewSize == 6 && !memcmp(REM8, T, 6)) FoundMask |= 1 << 8;
133 if (NewSize == 6 && !memcmp(REM9, T, 6)) FoundMask |= 1 << 9;
134 if (NewSize == 6 && !memcmp(REM10, T, 6)) FoundMask |= 1 << 10;
136 if (NewSize == 5 && !memcmp(REM11, T, 5)) FoundMask |= 1 << 11;
137 if (NewSize == 5 && !memcmp(REM12, T, 5)) FoundMask |= 1 << 12;
138 if (NewSize == 5 && !memcmp(REM13, T, 5)) FoundMask |= 1 << 13;
140 EXPECT_EQ(FoundMask, (1 << 14) - 1);
143 TEST(FuzzerMutate, EraseBytes1) {
146 TEST(FuzzerMutate, EraseBytes2) {
156 uint8_t INS0[8] = {0xF1, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
157 uint8_t INS1[8] = {0x00, 0xF2, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
158 uint8_t INS2[8] = {0x00, 0x11, 0xF3, 0x22, 0x33, 0x44, 0x55, 0x66};
159 uint8_t INS3[8] = {0x00, 0x11, 0x22, 0xF4, 0x33, 0x44, 0x55, 0x66};
160 uint8_t INS4[8] = {0x00, 0x11, 0x22, 0x33, 0xF5, 0x44, 0x55, 0x66};
161 uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF6, 0x55, 0x66};
162 uint8_t INS6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF7, 0x66};
163 uint8_t INS7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF8};
164 for (
int i = 0;
i < NumIter;
i++) {
165 uint8_t
T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
166 size_t NewSize = (MD.*M)(T, 7, 8);
167 if (NewSize == 8 && !memcmp(INS0, T, 8)) FoundMask |= 1 << 0;
168 if (NewSize == 8 && !memcmp(INS1, T, 8)) FoundMask |= 1 << 1;
169 if (NewSize == 8 && !memcmp(INS2, T, 8)) FoundMask |= 1 << 2;
170 if (NewSize == 8 && !memcmp(INS3, T, 8)) FoundMask |= 1 << 3;
171 if (NewSize == 8 && !memcmp(INS4, T, 8)) FoundMask |= 1 << 4;
172 if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5;
173 if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6;
174 if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7;
176 EXPECT_EQ(FoundMask, 255);
179 TEST(FuzzerMutate, InsertByte1) {
182 TEST(FuzzerMutate, InsertByte2) {
192 uint8_t INS0[7] = {0x00, 0x11, 0x22, 0x33,
'a',
'a',
'a'};
193 uint8_t INS1[7] = {0x00, 0x11, 0x22,
'a',
'a',
'a', 0x33};
194 uint8_t INS2[7] = {0x00, 0x11,
'a',
'a',
'a', 0x22, 0x33};
195 uint8_t INS3[7] = {0x00,
'a',
'a',
'a', 0x11, 0x22, 0x33};
196 uint8_t INS4[7] = {
'a',
'a',
'a', 0x00, 0x11, 0x22, 0x33};
198 uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33,
'b',
'b',
'b',
'b'};
199 uint8_t INS6[8] = {0x00, 0x11, 0x22,
'b',
'b',
'b',
'b', 0x33};
200 uint8_t INS7[8] = {0x00, 0x11,
'b',
'b',
'b',
'b', 0x22, 0x33};
201 uint8_t INS8[8] = {0x00,
'b',
'b',
'b',
'b', 0x11, 0x22, 0x33};
202 uint8_t INS9[8] = {
'b',
'b',
'b',
'b', 0x00, 0x11, 0x22, 0x33};
204 for (
int i = 0;
i < NumIter;
i++) {
205 uint8_t
T[8] = {0x00, 0x11, 0x22, 0x33};
206 size_t NewSize = (MD.*M)(T, 4, 8);
207 if (NewSize == 7 && !memcmp(INS0, T, 7)) FoundMask |= 1 << 0;
208 if (NewSize == 7 && !memcmp(INS1, T, 7)) FoundMask |= 1 << 1;
209 if (NewSize == 7 && !memcmp(INS2, T, 7)) FoundMask |= 1 << 2;
210 if (NewSize == 7 && !memcmp(INS3, T, 7)) FoundMask |= 1 << 3;
211 if (NewSize == 7 && !memcmp(INS4, T, 7)) FoundMask |= 1 << 4;
213 if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5;
214 if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6;
215 if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7;
216 if (NewSize == 8 && !memcmp(INS8, T, 8)) FoundMask |= 1 << 8;
217 if (NewSize == 8 && !memcmp(INS9, T, 8)) FoundMask |= 1 << 9;
220 EXPECT_EQ(FoundMask, (1 << 10) - 1);
223 TEST(FuzzerMutate, InsertRepeatedBytes1) {
226 TEST(FuzzerMutate, InsertRepeatedBytes2) {
236 uint8_t CH0[8] = {0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
237 uint8_t CH1[8] = {0x00, 0xF1, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
238 uint8_t CH2[8] = {0x00, 0x11, 0xF2, 0x33, 0x44, 0x55, 0x66, 0x77};
239 uint8_t CH3[8] = {0x00, 0x11, 0x22, 0xF3, 0x44, 0x55, 0x66, 0x77};
240 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0xF4, 0x55, 0x66, 0x77};
241 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF5, 0x66, 0x77};
242 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF5, 0x77};
243 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7};
244 for (
int i = 0;
i < NumIter;
i++) {
245 uint8_t
T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
246 size_t NewSize = (MD.*M)(T, 8, 9);
247 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
248 if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
249 if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
250 if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
251 if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4;
252 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
253 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
254 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
256 EXPECT_EQ(FoundMask, 255);
259 TEST(FuzzerMutate, ChangeByte1) {
262 TEST(FuzzerMutate, ChangeByte2) {
272 uint8_t CH0[8] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
273 uint8_t CH1[8] = {0x00, 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
274 uint8_t CH2[8] = {0x00, 0x11, 0x02, 0x33, 0x44, 0x55, 0x66, 0x77};
275 uint8_t CH3[8] = {0x00, 0x11, 0x22, 0x37, 0x44, 0x55, 0x66, 0x77};
276 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x54, 0x55, 0x66, 0x77};
277 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x54, 0x66, 0x77};
278 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x76, 0x77};
279 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7};
280 for (
int i = 0;
i < NumIter;
i++) {
281 uint8_t
T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
282 size_t NewSize = (MD.*M)(T, 8, 9);
283 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
284 if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
285 if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
286 if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
287 if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4;
288 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
289 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
290 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
292 EXPECT_EQ(FoundMask, 255);
295 TEST(FuzzerMutate, ChangeBit1) {
298 TEST(FuzzerMutate, ChangeBit2) {
308 uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66};
309 uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66};
310 uint8_t CH2[7] = {0x00, 0x33, 0x11, 0x22, 0x44, 0x55, 0x66};
311 uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x33};
312 uint8_t CH4[7] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x44, 0x66};
313 for (
int i = 0;
i < NumIter;
i++) {
314 uint8_t
T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
315 size_t NewSize = (MD.*M)(T, 7, 7);
316 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
317 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
318 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
319 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
320 if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4;
322 EXPECT_EQ(FoundMask, 31);
325 TEST(FuzzerMutate, ShuffleBytes1) {
328 TEST(FuzzerMutate, ShuffleBytes2) {
338 uint8_t CH0[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11};
339 uint8_t CH1[7] = {0x55, 0x66, 0x22, 0x33, 0x44, 0x55, 0x66};
340 uint8_t CH2[7] = {0x00, 0x55, 0x66, 0x33, 0x44, 0x55, 0x66};
341 uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x66};
342 uint8_t CH4[7] = {0x00, 0x11, 0x11, 0x22, 0x33, 0x55, 0x66};
344 for (
int i = 0;
i < NumIter;
i++) {
345 uint8_t
T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
346 size_t NewSize = (MD.*M)(T, 7, 7);
347 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
348 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
349 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
350 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
351 if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4;
354 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11, 0x22};
355 uint8_t CH6[8] = {0x22, 0x33, 0x44, 0x00, 0x11, 0x22, 0x33, 0x44};
356 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x33, 0x44};
357 uint8_t CH8[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x22, 0x33, 0x44};
358 uint8_t CH9[8] = {0x00, 0x11, 0x22, 0x22, 0x33, 0x44, 0x33, 0x44};
360 for (
int i = 0;
i < NumIter;
i++) {
361 uint8_t
T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
362 size_t NewSize = (MD.*M)(T, 5, 8);
363 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
364 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
365 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
366 if (NewSize == 8 && !memcmp(CH8, T, 8)) FoundMask |= 1 << 8;
367 if (NewSize == 8 && !memcmp(CH9, T, 8)) FoundMask |= 1 << 9;
370 EXPECT_EQ(FoundMask, 1023);
373 TEST(FuzzerMutate, CopyPart1) {
376 TEST(FuzzerMutate, CopyPart2) {
385 uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD};
386 uint8_t Word2[3] = {0xFF, 0xEE, 0xEF};
388 MD.AddWordToManualDictionary(
Word(Word2,
sizeof(Word2)));
390 uint8_t CH0[7] = {0x00, 0x11, 0x22, 0xAA, 0xBB, 0xCC, 0xDD};
391 uint8_t CH1[7] = {0x00, 0x11, 0xAA, 0xBB, 0xCC, 0xDD, 0x22};
392 uint8_t CH2[7] = {0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0x11, 0x22};
393 uint8_t CH3[7] = {0xAA, 0xBB, 0xCC, 0xDD, 0x00, 0x11, 0x22};
394 uint8_t CH4[6] = {0x00, 0x11, 0x22, 0xFF, 0xEE, 0xEF};
395 uint8_t CH5[6] = {0x00, 0x11, 0xFF, 0xEE, 0xEF, 0x22};
396 uint8_t CH6[6] = {0x00, 0xFF, 0xEE, 0xEF, 0x11, 0x22};
397 uint8_t CH7[6] = {0xFF, 0xEE, 0xEF, 0x00, 0x11, 0x22};
398 for (
int i = 0;
i < NumIter;
i++) {
399 uint8_t
T[7] = {0x00, 0x11, 0x22};
400 size_t NewSize = (MD.*M)(T, 3, 7);
401 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
402 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
403 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
404 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
405 if (NewSize == 6 && !memcmp(CH4, T, 6)) FoundMask |= 1 << 4;
406 if (NewSize == 6 && !memcmp(CH5, T, 6)) FoundMask |= 1 << 5;
407 if (NewSize == 6 && !memcmp(CH6, T, 6)) FoundMask |= 1 << 6;
408 if (NewSize == 6 && !memcmp(CH7, T, 6)) FoundMask |= 1 << 7;
410 EXPECT_EQ(FoundMask, 255);
413 TEST(FuzzerMutate, AddWordFromDictionary1) {
418 TEST(FuzzerMutate, AddWordFromDictionary2) {
427 uint8_t W[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xFF, 0xEE, 0xEF};
428 size_t PosHint = 7777;
431 for (
int i = 0;
i < NumIter;
i++) {
433 memset(T, 0,
sizeof(T));
434 size_t NewSize = (MD.*M)(T, 9000, 10000);
435 if (NewSize >= PosHint +
sizeof(W) &&
436 !memcmp(W, T + PosHint,
sizeof(W)))
439 EXPECT_EQ(FoundMask, 1);
442 TEST(FuzzerMutate, AddWordFromDictionaryWithHint1) {
447 TEST(FuzzerMutate, AddWordFromDictionaryWithHint2) {
457 uint8_t CH0[8] = {
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'7'};
458 uint8_t CH1[8] = {
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'9'};
459 uint8_t CH2[8] = {
'2',
'4',
'6',
'9',
'1',
'3',
'5',
'6'};
460 uint8_t CH3[8] = {
'0',
'6',
'1',
'7',
'2',
'8',
'3',
'9'};
462 for (
int i = 0;
i < NumIter;
i++) {
463 uint8_t
T[8] = {
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8'};
464 size_t NewSize = (MD.*M)(T, 8, 8);
465 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
466 else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
467 else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
468 else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
469 else if (NewSize == 8) FoundMask |= 1 << 4;
471 EXPECT_EQ(FoundMask, 31);
474 TEST(FuzzerMutate, ChangeASCIIInteger1) {
479 TEST(FuzzerMutate, ChangeASCIIInteger2) {
489 uint8_t CH0[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x79};
490 uint8_t CH1[8] = {0x00, 0x11, 0x22, 0x31, 0x44, 0x55, 0x66, 0x77};
491 uint8_t CH2[8] = {0xff, 0x10, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
492 uint8_t CH3[8] = {0x00, 0x11, 0x2a, 0x33, 0x44, 0x55, 0x66, 0x77};
493 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x4f, 0x66, 0x77};
494 uint8_t CH5[8] = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88};
495 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x00, 0x00, 0x00, 0x08, 0x77};
496 uint8_t CH7[8] = {0x00, 0x08, 0x00, 0x33, 0x44, 0x55, 0x66, 0x77};
499 for (
int i = 0;
i < NumIter;
i++) {
500 uint8_t
T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
501 size_t NewSize = (MD.*M)(T, 8, 8);
502 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
503 else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
504 else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
505 else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
506 else if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4;
507 else if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
508 else if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
509 else if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
511 EXPECT_EQ(FoundMask, 255);
514 TEST(FuzzerMutate, ChangeBinaryInteger1) {
519 TEST(FuzzerMutate, ChangeBinaryInteger2) {
534 EXPECT_EQ(U,
Unit({
'a'}));
536 EXPECT_EQ(U,
Unit({
'a',
'b',
'c'}));
538 EXPECT_EQ(U,
Unit({
'a',
'b',
'c'}));
541 EXPECT_EQ(U,
Unit({
'\\'}));
543 EXPECT_EQ(U,
Unit({0xAB}));
545 EXPECT_EQ(U,
Unit({0xAB,
'z', 0xDE}));
547 EXPECT_EQ(U,
Unit({
'#'}));
549 EXPECT_EQ(U,
Unit({
'"'}));
553 std::vector<Unit> Units;
557 EXPECT_EQ(Units.size(), 0U);
559 EXPECT_EQ(Units.size(), 0U);
561 EXPECT_EQ(Units.size(), 0U);
563 EXPECT_EQ(Units.size(), 0U);
565 EXPECT_EQ(Units, std::vector<Unit>({
Unit({
'a',
'a'})}));
569 std::vector<Unit>({
Unit({
'a',
'a'}),
Unit({
'a',
'b',
'c'})}));
573 EXPECT_EQ(
"",
Base64({}));
574 EXPECT_EQ(
"YQ==",
Base64({
'a'}));
575 EXPECT_EQ(
"eA==",
Base64({
'x'}));
576 EXPECT_EQ(
"YWI=",
Base64({
'a',
'b'}));
577 EXPECT_EQ(
"eHk=",
Base64({
'x',
'y'}));
578 EXPECT_EQ(
"YWJj",
Base64({
'a',
'b',
'c'}));
579 EXPECT_EQ(
"eHl6",
Base64({
'x',
'y',
'z'}));
580 EXPECT_EQ(
"YWJjeA==",
Base64({
'a',
'b',
'c',
'x'}));
581 EXPECT_EQ(
"YWJjeHk=",
Base64({
'a',
'b',
'c',
'x',
'y'}));
582 EXPECT_EQ(
"YWJjeHl6",
Base64({
'a',
'b',
'c',
'x',
'y',
'z'}));
589 size_t TriesPerUnit = 1<<16;
590 for (
size_t i = 0;
i <
N;
i++)
593 std::vector<size_t> Hist(
N);
594 for (
size_t i = 0;
i <
N * TriesPerUnit;
i++) {
595 Hist[
C.ChooseUnitIdxToMutate(Rand)]++;
597 for (
size_t i = 0;
i <
N;
i++) {
599 EXPECT_GT(Hist[
i], TriesPerUnit / N / 3);
604 const char *kInvalidInputs[] = {
614 "1\n1\nA\nSTARTED 1",
617 for (
auto S : kInvalidInputs) {
619 EXPECT_FALSE(M.
Parse(S,
false));
623 void EQ(
const std::vector<uint32_t> &
A,
const std::vector<uint32_t> &
B) {
627 void EQ(
const std::vector<std::string> &
A,
const std::vector<std::string> &
B) {
628 std::set<std::string> a(A.begin(), A.end());
629 std::set<std::string> b(B.begin(), B.end());
633 static void Merge(
const std::string &Input,
634 const std::vector<std::string> Result,
635 size_t NumNewFeatures) {
637 std::vector<std::string> NewFiles;
638 EXPECT_TRUE(M.
Parse(Input,
true));
639 EXPECT_EQ(NumNewFeatures, M.
Merge(&NewFiles));
640 EQ(NewFiles, Result);
646 EXPECT_TRUE(M.
Parse(
"1\n0\nAA\n",
false));
647 EXPECT_EQ(M.
Files.size(), 1U);
649 EXPECT_EQ(M.
Files[0].Name,
"AA");
653 EXPECT_TRUE(M.
Parse(
"2\n1\nAA\nBB\nSTARTED 0 42\n",
false));
654 EXPECT_EQ(M.
Files.size(), 2U);
656 EXPECT_EQ(M.
Files[0].Name,
"AA");
657 EXPECT_EQ(M.
Files[1].Name,
"BB");
661 EXPECT_TRUE(M.
Parse(
"3\n1\nAA\nBB\nC\n"
668 EXPECT_EQ(M.
Files.size(), 3U);
670 EXPECT_EQ(M.
Files[0].Name,
"AA");
671 EXPECT_EQ(M.
Files[0].Size, 1000U);
672 EXPECT_EQ(M.
Files[1].Name,
"BB");
673 EXPECT_EQ(M.
Files[1].Size, 1001U);
674 EXPECT_EQ(M.
Files[2].Name,
"C");
675 EXPECT_EQ(M.
Files[2].Size, 1002U);
678 EQ(M.
Files[0].Features, {1, 2, 3});
679 EQ(M.Files[1].Features, {4, 5, 6});
682 std::vector<std::string> NewFiles;
684 EXPECT_TRUE(M.Parse(
"3\n2\nAA\nBB\nC\n"
685 "STARTED 0 1000\nDONE 0 1 2 3\n"
686 "STARTED 1 1001\nDONE 1 4 5 6 \n"
687 "STARTED 2 1002\nDONE 2 6 1 3 \n"
689 EXPECT_EQ(M.Files.size(), 3U);
690 EXPECT_EQ(M.NumFilesInFirstCorpus, 2U);
691 EXPECT_TRUE(M.LastFailure.empty());
692 EXPECT_EQ(M.FirstNotProcessedFile, 3U);
693 EQ(M.Files[0].Features, {1, 2, 3});
694 EQ(M.Files[1].Features, {4, 5, 6});
695 EQ(M.Files[2].Features, {1, 3, 6});
696 EXPECT_EQ(0U, M.Merge(&NewFiles));
699 EXPECT_TRUE(M.Parse(
"3\n1\nA\nB\nC\n"
700 "STARTED 0 1000\nDONE 0 1 2 3\n"
701 "STARTED 1 1001\nDONE 1 4 5 6 \n"
702 "STARTED 2 1002\nDONE 2 6 1 3\n"
704 EQ(M.Files[0].Features, {1, 2, 3});
705 EQ(M.Files[1].Features, {4, 5, 6});
706 EQ(M.Files[2].Features, {1, 3, 6});
707 EXPECT_EQ(3U, M.Merge(&NewFiles));
713 Merge(
"3\n1\nA\nB\nC\n"
714 "STARTED 0 1000\nDONE 0 1 2 3\n"
715 "STARTED 1 1001\nDONE 1 4 5 6 \n"
716 "STARTED 2 1002\nDONE 2 6 1 3 \n",
719 Merge(
"3\n0\nA\nB\nC\n"
720 "STARTED 0 2000\nDONE 0 1 2 3\n"
721 "STARTED 1 1001\nDONE 1 4 5 6 \n"
722 "STARTED 2 1002\nDONE 2 6 1 3 \n",
725 Merge(
"4\n0\nA\nB\nC\nD\n"
726 "STARTED 0 2000\nDONE 0 1 2 3\n"
727 "STARTED 1 1101\nDONE 1 4 5 6 \n"
728 "STARTED 2 1102\nDONE 2 6 1 3 100 \n"
729 "STARTED 3 1000\nDONE 3 1 \n",
730 {
"A",
"B",
"C",
"D"}, 7);
732 Merge(
"4\n1\nA\nB\nC\nD\n"
733 "STARTED 0 2000\nDONE 0 4 5 6 7 8\n"
734 "STARTED 1 1100\nDONE 1 1 2 3 \n"
735 "STARTED 2 1100\nDONE 2 2 3 \n"
736 "STARTED 3 1000\nDONE 3 1 \n",
size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by inserting a byte.
void EQ(const std::vector< uint32_t > &A, const std::vector< uint32_t > &B)
size_t Mutate_ChangeBinaryInteger(uint8_t *Data, size_t Size, size_t MaxSize)
Change a 1-, 2-, 4-, or 8-byte integer in interesting ways.
size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize)
Applies one of the configured mutations.
bool ParseOneDictionaryEntry(const std::string &Str, Unit *U)
size_t Merge(std::vector< std::string > *NewFiles)
size_t(MutationDispatcher::* Mutator)(uint8_t *Data, size_t Size, size_t MaxSize)
size_t NumFilesInFirstCorpus
bool Parse(std::istream &IS, bool ParseCoverage)
void TestChangeBit(Mutator M, int NumIter)
size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by adding a word from the manual dictionary.
void TestInsertRepeatedBytes(Mutator M, int NumIter)
void TestInsertByte(Mutator M, int NumIter)
void AddWordToAutoDictionary(DictionaryEntry DE)
size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by chanding one bit.
void TestEraseBytes(Mutator M, int NumIter)
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
void TestAddWordFromDictionary(Mutator M, int NumIter)
std::string Base64(const Unit &U)
size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by adding a word from the temporary automatic dictionary.
void TestChangeASCIIInteger(Mutator M, int NumIter)
bool ParseDictionaryFile(const std::string &Text, std::vector< Unit > *Units)
size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by inserting several repeated bytes.
void AddWordToManualDictionary(const Word &W)
void TestCopyPart(Mutator M, int NumIter)
size_t Mutate_CopyPart(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by copying/inserting a part of data into a different place.
size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize)
Tries to find an ASCII integer in Data, changes it to another ASCII int.
void TestShuffleBytes(Mutator M, int NumIter)
void TestAddWordFromDictionaryWithHint(Mutator M, int NumIter)
static void Merge(const std::string &Input, const std::vector< std::string > Result, size_t NumNewFeatures)
size_t FirstNotProcessedFile
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by chanding one byte.
std::vector< uint8_t > Unit
std::vector< MergeFileInfo > Files
std::string Hash(const Unit &U)
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
void TestChangeByte(Mutator M, int NumIter)
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by erasing bytes.
size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize)
Mutates data by shuffling bytes.
void TestChangeBinaryInteger(Mutator M, int NumIter)