1de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar// This file is distributed under the University of Illinois Open Source
2de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar// License. See LICENSE.TXT for details.
3de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
4de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar// Avoid ODR violations (LibFuzzer is built without ASan and this test is built
5de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar// with ASan) involving C++ standard library types when using libcxx.
6de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#define _LIBCPP_HAS_NO_ASAN
7de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
8ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "FuzzerInternal.h"
9ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "gtest/gtest.h"
10de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#include <memory>
11ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include <set>
12ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
13f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarusing namespace fuzzer;
14f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
156948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar// For now, have LLVMFuzzerTestOneInput just to make it link.
166948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar// Later we may want to make unittests that actually call LLVMFuzzerTestOneInput.
17de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarextern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
18ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  abort();
19ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
20ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
21ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesTEST(Fuzzer, CrossOver) {
22de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
23de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
24de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
25de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
26ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Unit A({0, 1, 2}), B({5, 6, 7});
27ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Unit C;
28ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Unit Expected[] = {
29ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0 },
30ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1 },
31ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5 },
32ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 2 },
33ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5 },
34ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1 },
35ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6 },
36ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 2, 5 },
37ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 2 },
38ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 6 },
39ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 2 },
40ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 6 },
41ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 1 },
42ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 7 },
43ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 2, 5, 6 },
44ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 2, 6 },
45ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 6, 2 },
46ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 6, 7 },
47ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 2, 6 },
48ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 6, 2 },
49ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 6, 7 },
50ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 1, 2 },
51ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 1, 7 },
52ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 7, 1 },
53ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 2, 5, 6, 7 },
54ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 2, 6, 7 },
55ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 6, 2, 7 },
56ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 1, 5, 6, 7, 2 },
57ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 2, 6, 7 },
58ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 6, 2, 7 },
59ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 1, 6, 7, 2 },
60ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 1, 2, 7 },
61ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 1, 7, 2 },
62ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines       { 0, 5, 6, 7, 1, 2 }
63ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  };
64ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  for (size_t Len = 1; Len < 8; Len++) {
65ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    std::set<Unit> FoundUnits, ExpectedUnitsWitThisLength;
66ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    for (int Iter = 0; Iter < 3000; Iter++) {
676948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      C.resize(Len);
68f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      size_t NewSize = MD.CrossOver(A.data(), A.size(), B.data(), B.size(),
69f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                                    C.data(), C.size());
706948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar      C.resize(NewSize);
71ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      FoundUnits.insert(C);
72ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    }
73ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    for (const Unit &U : Expected)
74ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      if (U.size() <= Len)
75ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines        ExpectedUnitsWitThisLength.insert(U);
76ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits);
77ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
78ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
796948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
806948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga NainarTEST(Fuzzer, Hash) {
816948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  uint8_t A[] = {'a', 'b', 'c'};
826948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  fuzzer::Unit U(A, A + sizeof(A));
836948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", fuzzer::Hash(U));
846948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  U.push_back('d');
856948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ("81fe8bfe87576c3ecb22426f8e57847382917acf", fuzzer::Hash(U));
866948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar}
87f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
88f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainartypedef size_t (MutationDispatcher::*Mutator)(uint8_t *Data, size_t Size,
89f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                                              size_t MaxSize);
90f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
91f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarvoid TestEraseByte(Mutator M, int NumIter) {
92de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
93de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
94f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM0[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
95f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM1[8] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
96f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM2[8] = {0x00, 0x11, 0x33, 0x44, 0x55, 0x66, 0x77};
97f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM3[8] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
98f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM4[8] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x66, 0x77};
99f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x66, 0x77};
100f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x77};
101f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t REM7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
102de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
103de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
104f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int FoundMask = 0;
105f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
106f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
107f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, sizeof(T), sizeof(T));
108f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM0, T, 7)) FoundMask |= 1 << 0;
109f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM1, T, 7)) FoundMask |= 1 << 1;
110f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM2, T, 7)) FoundMask |= 1 << 2;
111f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM3, T, 7)) FoundMask |= 1 << 3;
112f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM4, T, 7)) FoundMask |= 1 << 4;
113f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM5, T, 7)) FoundMask |= 1 << 5;
114f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM6, T, 7)) FoundMask |= 1 << 6;
115f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(REM7, T, 7)) FoundMask |= 1 << 7;
116f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
117f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(FoundMask, 255);
118f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
119f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
120f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, EraseByte1) {
121f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestEraseByte(&MutationDispatcher::Mutate_EraseByte, 100);
122f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
123f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, EraseByte2) {
124f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestEraseByte(&MutationDispatcher::Mutate, 1000);
125f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
126f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
127f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarvoid TestInsertByte(Mutator M, int NumIter) {
128de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
129de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
130de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
131de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
132f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int FoundMask = 0;
133f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS0[8] = {0xF1, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
134f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS1[8] = {0x00, 0xF2, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
135f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS2[8] = {0x00, 0x11, 0xF3, 0x22, 0x33, 0x44, 0x55, 0x66};
136f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS3[8] = {0x00, 0x11, 0x22, 0xF4, 0x33, 0x44, 0x55, 0x66};
137f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS4[8] = {0x00, 0x11, 0x22, 0x33, 0xF5, 0x44, 0x55, 0x66};
138f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF6, 0x55, 0x66};
139f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF7, 0x66};
140f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t INS7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF8};
141f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
142f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
143f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, 7, 8);
144f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS0, T, 8)) FoundMask |= 1 << 0;
145f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS1, T, 8)) FoundMask |= 1 << 1;
146f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS2, T, 8)) FoundMask |= 1 << 2;
147f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS3, T, 8)) FoundMask |= 1 << 3;
148f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS4, T, 8)) FoundMask |= 1 << 4;
149f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5;
150f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6;
151f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7;
152f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
153f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(FoundMask, 255);
154f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
155f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
156f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, InsertByte1) {
157f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestInsertByte(&MutationDispatcher::Mutate_InsertByte, 1 << 15);
158f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
159f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, InsertByte2) {
160f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestInsertByte(&MutationDispatcher::Mutate, 1 << 17);
161f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
162f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
163f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarvoid TestChangeByte(Mutator M, int NumIter) {
164de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
165de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
166de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
167de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
168f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int FoundMask = 0;
169f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH0[8] = {0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
170f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH1[8] = {0x00, 0xF1, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
171f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH2[8] = {0x00, 0x11, 0xF2, 0x33, 0x44, 0x55, 0x66, 0x77};
172f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH3[8] = {0x00, 0x11, 0x22, 0xF3, 0x44, 0x55, 0x66, 0x77};
173f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0xF4, 0x55, 0x66, 0x77};
174f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF5, 0x66, 0x77};
175f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF5, 0x77};
176f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7};
177f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
178f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
179f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, 8, 9);
180f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
181f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
182f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
183f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
184f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4;
185f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
186f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
187f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
188f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
189f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(FoundMask, 255);
190f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
191f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
192f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ChangeByte1) {
193f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestChangeByte(&MutationDispatcher::Mutate_ChangeByte, 1 << 15);
194f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
195f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ChangeByte2) {
196f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestChangeByte(&MutationDispatcher::Mutate, 1 << 17);
197f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
198f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
199f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarvoid TestChangeBit(Mutator M, int NumIter) {
200de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
201de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
202de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
203de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
204f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int FoundMask = 0;
205f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH0[8] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
206f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH1[8] = {0x00, 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
207f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH2[8] = {0x00, 0x11, 0x02, 0x33, 0x44, 0x55, 0x66, 0x77};
208f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH3[8] = {0x00, 0x11, 0x22, 0x37, 0x44, 0x55, 0x66, 0x77};
209f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x54, 0x55, 0x66, 0x77};
210f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x54, 0x66, 0x77};
211f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x76, 0x77};
212f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7};
213f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
214f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
215f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, 8, 9);
216f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
217f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
218f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
219f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
220f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4;
221f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
222f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
223f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
224f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
225f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(FoundMask, 255);
226f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
227f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
228f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ChangeBit1) {
229f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestChangeBit(&MutationDispatcher::Mutate_ChangeBit, 1 << 16);
230f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
231f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ChangeBit2) {
232f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestChangeBit(&MutationDispatcher::Mutate, 1 << 18);
233f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
234f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
235f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarvoid TestShuffleBytes(Mutator M, int NumIter) {
236de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
237de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
238de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
239de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
240f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int FoundMask = 0;
241f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66};
242f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66};
243f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH2[7] = {0x00, 0x33, 0x11, 0x22, 0x44, 0x55, 0x66};
244f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x33};
245f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH4[7] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x44, 0x66};
246f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
247f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
248f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, 7, 7);
249f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
250f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
251f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
252f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
253f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4;
254f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
255f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(FoundMask, 31);
256f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
257f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
258f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ShuffleBytes1) {
259de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  TestShuffleBytes(&MutationDispatcher::Mutate_ShuffleBytes, 1 << 16);
260f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
261f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ShuffleBytes2) {
262de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  TestShuffleBytes(&MutationDispatcher::Mutate, 1 << 20);
263f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
264f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
265f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarvoid TestAddWordFromDictionary(Mutator M, int NumIter) {
266de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
267de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
268de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
269de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
270f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD};
271f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t Word2[3] = {0xFF, 0xEE, 0xEF};
272de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MD.AddWordToManualDictionary(Word(Word1, sizeof(Word1)));
273de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MD.AddWordToManualDictionary(Word(Word2, sizeof(Word2)));
274f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int FoundMask = 0;
275f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH0[7] = {0x00, 0x11, 0x22, 0xAA, 0xBB, 0xCC, 0xDD};
276f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH1[7] = {0x00, 0x11, 0xAA, 0xBB, 0xCC, 0xDD, 0x22};
277f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH2[7] = {0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0x11, 0x22};
278f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH3[7] = {0xAA, 0xBB, 0xCC, 0xDD, 0x00, 0x11, 0x22};
279f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH4[6] = {0x00, 0x11, 0x22, 0xFF, 0xEE, 0xEF};
280f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH5[6] = {0x00, 0x11, 0xFF, 0xEE, 0xEF, 0x22};
281f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH6[6] = {0x00, 0xFF, 0xEE, 0xEF, 0x11, 0x22};
282f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH7[6] = {0xFF, 0xEE, 0xEF, 0x00, 0x11, 0x22};
283f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
284f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint8_t T[7] = {0x00, 0x11, 0x22};
285f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, 3, 7);
286f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
287f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
288f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
289f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
290f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 6 && !memcmp(CH4, T, 6)) FoundMask |= 1 << 4;
291f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 6 && !memcmp(CH5, T, 6)) FoundMask |= 1 << 5;
292f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 6 && !memcmp(CH6, T, 6)) FoundMask |= 1 << 6;
293f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    if (NewSize == 6 && !memcmp(CH7, T, 6)) FoundMask |= 1 << 7;
294f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
295f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(FoundMask, 255);
296f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
297f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
298f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, AddWordFromDictionary1) {
299de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  TestAddWordFromDictionary(
300de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      &MutationDispatcher::Mutate_AddWordFromManualDictionary, 1 << 15);
301f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
302f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
303f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, AddWordFromDictionary2) {
304f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestAddWordFromDictionary(&MutationDispatcher::Mutate, 1 << 15);
305f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
306f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
307de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainarvoid TestAddWordFromDictionaryWithHint(Mutator M, int NumIter) {
308de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
309de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
310de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
311de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
312de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  uint8_t W[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xFF, 0xEE, 0xEF};
313de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  size_t PosHint = 7777;
314de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MD.AddWordToAutoDictionary(Word(W, sizeof(W)), PosHint);
315de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  int FoundMask = 0;
316de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
317de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    uint8_t T[10000];
318de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    memset(T, 0, sizeof(T));
319de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, 9000, 10000);
320de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    if (NewSize >= PosHint + sizeof(W) &&
321de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar        !memcmp(W, T + PosHint, sizeof(W)))
322de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      FoundMask = 1;
323de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  }
324de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  EXPECT_EQ(FoundMask, 1);
325de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
326de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
327de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga NainarTEST(FuzzerMutate, AddWordFromDictionaryWithHint1) {
328de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  TestAddWordFromDictionaryWithHint(
329de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      &MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary, 1 << 5);
330de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
331de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
332de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga NainarTEST(FuzzerMutate, AddWordFromDictionaryWithHint2) {
333de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  TestAddWordFromDictionaryWithHint(&MutationDispatcher::Mutate, 1 << 10);
334de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
335de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
336f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainarvoid TestChangeASCIIInteger(Mutator M, int NumIter) {
337de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
338de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
339de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
340de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
341f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
342f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH0[8] = {'1', '2', '3', '4', '5', '6', '7', '7'};
343f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH1[8] = {'1', '2', '3', '4', '5', '6', '7', '9'};
344f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH2[8] = {'2', '4', '6', '9', '1', '3', '5', '6'};
345f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  uint8_t CH3[8] = {'0', '6', '1', '7', '2', '8', '3', '9'};
346f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  int FoundMask = 0;
347f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  for (int i = 0; i < NumIter; i++) {
348f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    uint8_t T[8] = {'1', '2', '3', '4', '5', '6', '7', '8'};
349f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    size_t NewSize = (MD.*M)(T, 8, 8);
350f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
351f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
352f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
353f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
354f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar    else if (NewSize == 8)                       FoundMask |= 1 << 4;
355f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  }
356f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(FoundMask, 31);
357f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
358f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
359f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ChangeASCIIInteger1) {
360f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestChangeASCIIInteger(&MutationDispatcher::Mutate_ChangeASCIIInteger,
361f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar                         1 << 15);
362f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
363f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
364f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerMutate, ChangeASCIIInteger2) {
365f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  TestChangeASCIIInteger(&MutationDispatcher::Mutate, 1 << 15);
366f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
367f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
368f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
369f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerDictionary, ParseOneDictionaryEntry) {
370f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  Unit U;
371f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry("", &U));
372f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry(" ", &U));
373f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry("\t  ", &U));
374f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry("  \" ", &U));
375f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry("  zz\" ", &U));
376f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry("  \"zz ", &U));
377f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry("  \"\" ", &U));
378f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("\"a\"", &U));
379f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({'a'}));
380f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("\"abc\"", &U));
381f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({'a', 'b', 'c'}));
382f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("abc=\"abc\"", &U));
383f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({'a', 'b', 'c'}));
384f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseOneDictionaryEntry("\"\\\"", &U));
385f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\\\"", &U));
386f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({'\\'}));
387f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xAB\"", &U));
388f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({0xAB}));
389f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xABz\\xDE\"", &U));
390f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({0xAB, 'z', 0xDE}));
391f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("\"#\"", &U));
392f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({'#'}));
393f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\"\"", &U));
394f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(U, Unit({'"'}));
395f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
396f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
397f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerDictionary, ParseDictionaryFile) {
398f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  std::vector<Unit> Units;
399f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseDictionaryFile("zzz\n", &Units));
400f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_FALSE(ParseDictionaryFile("", &Units));
401f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseDictionaryFile("\n", &Units));
402f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(Units.size(), 0U);
403f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseDictionaryFile("#zzzz a b c d\n", &Units));
404f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(Units.size(), 0U);
405f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseDictionaryFile(" #zzzz\n", &Units));
406f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(Units.size(), 0U);
407f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseDictionaryFile("  #zzzz\n", &Units));
408f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(Units.size(), 0U);
409f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(ParseDictionaryFile("  #zzzz\naaa=\"aa\"", &Units));
410f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(Units, std::vector<Unit>({Unit({'a', 'a'})}));
411f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_TRUE(
412f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar      ParseDictionaryFile("  #zzzz\naaa=\"aa\"\n\nabc=\"abc\"", &Units));
413f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ(Units,
414f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar            std::vector<Unit>({Unit({'a', 'a'}), Unit({'a', 'b', 'c'})}));
415f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
416f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar
417f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga NainarTEST(FuzzerUtil, Base64) {
418f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("", Base64({}));
419f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("YQ==", Base64({'a'}));
420f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("eA==", Base64({'x'}));
421f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("YWI=", Base64({'a', 'b'}));
422f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("eHk=", Base64({'x', 'y'}));
423f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("YWJj", Base64({'a', 'b', 'c'}));
424f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("eHl6", Base64({'x', 'y', 'z'}));
425f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("YWJjeA==", Base64({'a', 'b', 'c', 'x'}));
426f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("YWJjeHk=", Base64({'a', 'b', 'c', 'x', 'y'}));
427f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar  EXPECT_EQ("YWJjeHl6", Base64({'a', 'b', 'c', 'x', 'y', 'z'}));
428f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar}
429de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar
430de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga NainarTEST(Corpus, Distribution) {
431de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
432de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  fuzzer::EF = t.get();
433de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Random Rand(0);
434de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  MutationDispatcher MD(Rand, {});
435de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  Fuzzer Fuzz(LLVMFuzzerTestOneInput, MD, {});
436de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  size_t N = 10;
437de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  size_t TriesPerUnit = 1<<20;
438de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  for (size_t i = 0; i < N; i++) {
439de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    Fuzz.AddToCorpus(Unit{ static_cast<uint8_t>(i) });
440de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  }
441de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  std::vector<size_t> Hist(N);
442de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  for (size_t i = 0; i < N * TriesPerUnit; i++) {
443de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    Hist[Fuzz.ChooseUnitIdxToMutate()]++;
444de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  }
445de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  for (size_t i = 0; i < N; i++) {
446de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    // A weak sanity check that every unit gets invoked.
447de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    EXPECT_GT(Hist[i], TriesPerUnit / N / 3);
448de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar  }
449de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar}
450