1c5f01b2fSopenharmony_ci// This file is distributed under the University of Illinois Open Source 2c5f01b2fSopenharmony_ci// License. See LICENSE.TXT for details. 3c5f01b2fSopenharmony_ci 4c5f01b2fSopenharmony_ci// Avoid ODR violations (LibFuzzer is built without ASan and this test is built 5c5f01b2fSopenharmony_ci// with ASan) involving C++ standard library types when using libcxx. 6c5f01b2fSopenharmony_ci#define _LIBCPP_HAS_NO_ASAN 7c5f01b2fSopenharmony_ci 8c5f01b2fSopenharmony_ci#include "FuzzerCorpus.h" 9c5f01b2fSopenharmony_ci#include "FuzzerInternal.h" 10c5f01b2fSopenharmony_ci#include "FuzzerDictionary.h" 11c5f01b2fSopenharmony_ci#include "FuzzerMerge.h" 12c5f01b2fSopenharmony_ci#include "FuzzerMutate.h" 13c5f01b2fSopenharmony_ci#include "FuzzerRandom.h" 14c5f01b2fSopenharmony_ci#include "gtest/gtest.h" 15c5f01b2fSopenharmony_ci#include <memory> 16c5f01b2fSopenharmony_ci#include <set> 17c5f01b2fSopenharmony_ci 18c5f01b2fSopenharmony_ciusing namespace fuzzer; 19c5f01b2fSopenharmony_ci 20c5f01b2fSopenharmony_ci// For now, have LLVMFuzzerTestOneInput just to make it link. 21c5f01b2fSopenharmony_ci// Later we may want to make unittests that actually call LLVMFuzzerTestOneInput. 22c5f01b2fSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { 23c5f01b2fSopenharmony_ci abort(); 24c5f01b2fSopenharmony_ci} 25c5f01b2fSopenharmony_ci 26c5f01b2fSopenharmony_ciTEST(Fuzzer, CrossOver) { 27c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 28c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 29c5f01b2fSopenharmony_ci Random Rand(0); 30c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 31c5f01b2fSopenharmony_ci Unit A({0, 1, 2}), B({5, 6, 7}); 32c5f01b2fSopenharmony_ci Unit C; 33c5f01b2fSopenharmony_ci Unit Expected[] = { 34c5f01b2fSopenharmony_ci { 0 }, 35c5f01b2fSopenharmony_ci { 0, 1 }, 36c5f01b2fSopenharmony_ci { 0, 5 }, 37c5f01b2fSopenharmony_ci { 0, 1, 2 }, 38c5f01b2fSopenharmony_ci { 0, 1, 5 }, 39c5f01b2fSopenharmony_ci { 0, 5, 1 }, 40c5f01b2fSopenharmony_ci { 0, 5, 6 }, 41c5f01b2fSopenharmony_ci { 0, 1, 2, 5 }, 42c5f01b2fSopenharmony_ci { 0, 1, 5, 2 }, 43c5f01b2fSopenharmony_ci { 0, 1, 5, 6 }, 44c5f01b2fSopenharmony_ci { 0, 5, 1, 2 }, 45c5f01b2fSopenharmony_ci { 0, 5, 1, 6 }, 46c5f01b2fSopenharmony_ci { 0, 5, 6, 1 }, 47c5f01b2fSopenharmony_ci { 0, 5, 6, 7 }, 48c5f01b2fSopenharmony_ci { 0, 1, 2, 5, 6 }, 49c5f01b2fSopenharmony_ci { 0, 1, 5, 2, 6 }, 50c5f01b2fSopenharmony_ci { 0, 1, 5, 6, 2 }, 51c5f01b2fSopenharmony_ci { 0, 1, 5, 6, 7 }, 52c5f01b2fSopenharmony_ci { 0, 5, 1, 2, 6 }, 53c5f01b2fSopenharmony_ci { 0, 5, 1, 6, 2 }, 54c5f01b2fSopenharmony_ci { 0, 5, 1, 6, 7 }, 55c5f01b2fSopenharmony_ci { 0, 5, 6, 1, 2 }, 56c5f01b2fSopenharmony_ci { 0, 5, 6, 1, 7 }, 57c5f01b2fSopenharmony_ci { 0, 5, 6, 7, 1 }, 58c5f01b2fSopenharmony_ci { 0, 1, 2, 5, 6, 7 }, 59c5f01b2fSopenharmony_ci { 0, 1, 5, 2, 6, 7 }, 60c5f01b2fSopenharmony_ci { 0, 1, 5, 6, 2, 7 }, 61c5f01b2fSopenharmony_ci { 0, 1, 5, 6, 7, 2 }, 62c5f01b2fSopenharmony_ci { 0, 5, 1, 2, 6, 7 }, 63c5f01b2fSopenharmony_ci { 0, 5, 1, 6, 2, 7 }, 64c5f01b2fSopenharmony_ci { 0, 5, 1, 6, 7, 2 }, 65c5f01b2fSopenharmony_ci { 0, 5, 6, 1, 2, 7 }, 66c5f01b2fSopenharmony_ci { 0, 5, 6, 1, 7, 2 }, 67c5f01b2fSopenharmony_ci { 0, 5, 6, 7, 1, 2 } 68c5f01b2fSopenharmony_ci }; 69c5f01b2fSopenharmony_ci for (size_t Len = 1; Len < 8; Len++) { 70c5f01b2fSopenharmony_ci std::set<Unit> FoundUnits, ExpectedUnitsWitThisLength; 71c5f01b2fSopenharmony_ci for (int Iter = 0; Iter < 3000; Iter++) { 72c5f01b2fSopenharmony_ci C.resize(Len); 73c5f01b2fSopenharmony_ci size_t NewSize = MD.CrossOver(A.data(), A.size(), B.data(), B.size(), 74c5f01b2fSopenharmony_ci C.data(), C.size()); 75c5f01b2fSopenharmony_ci C.resize(NewSize); 76c5f01b2fSopenharmony_ci FoundUnits.insert(C); 77c5f01b2fSopenharmony_ci } 78c5f01b2fSopenharmony_ci for (const Unit &U : Expected) 79c5f01b2fSopenharmony_ci if (U.size() <= Len) 80c5f01b2fSopenharmony_ci ExpectedUnitsWitThisLength.insert(U); 81c5f01b2fSopenharmony_ci EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits); 82c5f01b2fSopenharmony_ci } 83c5f01b2fSopenharmony_ci} 84c5f01b2fSopenharmony_ci 85c5f01b2fSopenharmony_ciTEST(Fuzzer, Hash) { 86c5f01b2fSopenharmony_ci uint8_t A[] = {'a', 'b', 'c'}; 87c5f01b2fSopenharmony_ci fuzzer::Unit U(A, A + sizeof(A)); 88c5f01b2fSopenharmony_ci EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", fuzzer::Hash(U)); 89c5f01b2fSopenharmony_ci U.push_back('d'); 90c5f01b2fSopenharmony_ci EXPECT_EQ("81fe8bfe87576c3ecb22426f8e57847382917acf", fuzzer::Hash(U)); 91c5f01b2fSopenharmony_ci} 92c5f01b2fSopenharmony_ci 93c5f01b2fSopenharmony_citypedef size_t (MutationDispatcher::*Mutator)(uint8_t *Data, size_t Size, 94c5f01b2fSopenharmony_ci size_t MaxSize); 95c5f01b2fSopenharmony_ci 96c5f01b2fSopenharmony_civoid TestEraseBytes(Mutator M, int NumIter) { 97c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 98c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 99c5f01b2fSopenharmony_ci uint8_t REM0[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 100c5f01b2fSopenharmony_ci uint8_t REM1[8] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 101c5f01b2fSopenharmony_ci uint8_t REM2[8] = {0x00, 0x11, 0x33, 0x44, 0x55, 0x66, 0x77}; 102c5f01b2fSopenharmony_ci uint8_t REM3[8] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; 103c5f01b2fSopenharmony_ci uint8_t REM4[8] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x66, 0x77}; 104c5f01b2fSopenharmony_ci uint8_t REM5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x66, 0x77}; 105c5f01b2fSopenharmony_ci uint8_t REM6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x77}; 106c5f01b2fSopenharmony_ci uint8_t REM7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 107c5f01b2fSopenharmony_ci 108c5f01b2fSopenharmony_ci uint8_t REM8[6] = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 109c5f01b2fSopenharmony_ci uint8_t REM9[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; 110c5f01b2fSopenharmony_ci uint8_t REM10[6] = {0x00, 0x11, 0x22, 0x55, 0x66, 0x77}; 111c5f01b2fSopenharmony_ci 112c5f01b2fSopenharmony_ci uint8_t REM11[5] = {0x33, 0x44, 0x55, 0x66, 0x77}; 113c5f01b2fSopenharmony_ci uint8_t REM12[5] = {0x00, 0x11, 0x22, 0x33, 0x44}; 114c5f01b2fSopenharmony_ci uint8_t REM13[5] = {0x00, 0x44, 0x55, 0x66, 0x77}; 115c5f01b2fSopenharmony_ci 116c5f01b2fSopenharmony_ci 117c5f01b2fSopenharmony_ci Random Rand(0); 118c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 119c5f01b2fSopenharmony_ci int FoundMask = 0; 120c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 121c5f01b2fSopenharmony_ci uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 122c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, sizeof(T), sizeof(T)); 123c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM0, T, 7)) FoundMask |= 1 << 0; 124c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM1, T, 7)) FoundMask |= 1 << 1; 125c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM2, T, 7)) FoundMask |= 1 << 2; 126c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM3, T, 7)) FoundMask |= 1 << 3; 127c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM4, T, 7)) FoundMask |= 1 << 4; 128c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM5, T, 7)) FoundMask |= 1 << 5; 129c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM6, T, 7)) FoundMask |= 1 << 6; 130c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(REM7, T, 7)) FoundMask |= 1 << 7; 131c5f01b2fSopenharmony_ci 132c5f01b2fSopenharmony_ci if (NewSize == 6 && !memcmp(REM8, T, 6)) FoundMask |= 1 << 8; 133c5f01b2fSopenharmony_ci if (NewSize == 6 && !memcmp(REM9, T, 6)) FoundMask |= 1 << 9; 134c5f01b2fSopenharmony_ci if (NewSize == 6 && !memcmp(REM10, T, 6)) FoundMask |= 1 << 10; 135c5f01b2fSopenharmony_ci 136c5f01b2fSopenharmony_ci if (NewSize == 5 && !memcmp(REM11, T, 5)) FoundMask |= 1 << 11; 137c5f01b2fSopenharmony_ci if (NewSize == 5 && !memcmp(REM12, T, 5)) FoundMask |= 1 << 12; 138c5f01b2fSopenharmony_ci if (NewSize == 5 && !memcmp(REM13, T, 5)) FoundMask |= 1 << 13; 139c5f01b2fSopenharmony_ci } 140c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, (1 << 14) - 1); 141c5f01b2fSopenharmony_ci} 142c5f01b2fSopenharmony_ci 143c5f01b2fSopenharmony_ciTEST(FuzzerMutate, EraseBytes1) { 144c5f01b2fSopenharmony_ci TestEraseBytes(&MutationDispatcher::Mutate_EraseBytes, 200); 145c5f01b2fSopenharmony_ci} 146c5f01b2fSopenharmony_ciTEST(FuzzerMutate, EraseBytes2) { 147c5f01b2fSopenharmony_ci TestEraseBytes(&MutationDispatcher::Mutate, 2000); 148c5f01b2fSopenharmony_ci} 149c5f01b2fSopenharmony_ci 150c5f01b2fSopenharmony_civoid TestInsertByte(Mutator M, int NumIter) { 151c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 152c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 153c5f01b2fSopenharmony_ci Random Rand(0); 154c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 155c5f01b2fSopenharmony_ci int FoundMask = 0; 156c5f01b2fSopenharmony_ci uint8_t INS0[8] = {0xF1, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 157c5f01b2fSopenharmony_ci uint8_t INS1[8] = {0x00, 0xF2, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 158c5f01b2fSopenharmony_ci uint8_t INS2[8] = {0x00, 0x11, 0xF3, 0x22, 0x33, 0x44, 0x55, 0x66}; 159c5f01b2fSopenharmony_ci uint8_t INS3[8] = {0x00, 0x11, 0x22, 0xF4, 0x33, 0x44, 0x55, 0x66}; 160c5f01b2fSopenharmony_ci uint8_t INS4[8] = {0x00, 0x11, 0x22, 0x33, 0xF5, 0x44, 0x55, 0x66}; 161c5f01b2fSopenharmony_ci uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF6, 0x55, 0x66}; 162c5f01b2fSopenharmony_ci uint8_t INS6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF7, 0x66}; 163c5f01b2fSopenharmony_ci uint8_t INS7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF8}; 164c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 165c5f01b2fSopenharmony_ci uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 166c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 7, 8); 167c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS0, T, 8)) FoundMask |= 1 << 0; 168c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS1, T, 8)) FoundMask |= 1 << 1; 169c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS2, T, 8)) FoundMask |= 1 << 2; 170c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS3, T, 8)) FoundMask |= 1 << 3; 171c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS4, T, 8)) FoundMask |= 1 << 4; 172c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5; 173c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6; 174c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7; 175c5f01b2fSopenharmony_ci } 176c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 255); 177c5f01b2fSopenharmony_ci} 178c5f01b2fSopenharmony_ci 179c5f01b2fSopenharmony_ciTEST(FuzzerMutate, InsertByte1) { 180c5f01b2fSopenharmony_ci TestInsertByte(&MutationDispatcher::Mutate_InsertByte, 1 << 15); 181c5f01b2fSopenharmony_ci} 182c5f01b2fSopenharmony_ciTEST(FuzzerMutate, InsertByte2) { 183c5f01b2fSopenharmony_ci TestInsertByte(&MutationDispatcher::Mutate, 1 << 17); 184c5f01b2fSopenharmony_ci} 185c5f01b2fSopenharmony_ci 186c5f01b2fSopenharmony_civoid TestInsertRepeatedBytes(Mutator M, int NumIter) { 187c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 188c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 189c5f01b2fSopenharmony_ci Random Rand(0); 190c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 191c5f01b2fSopenharmony_ci int FoundMask = 0; 192c5f01b2fSopenharmony_ci uint8_t INS0[7] = {0x00, 0x11, 0x22, 0x33, 'a', 'a', 'a'}; 193c5f01b2fSopenharmony_ci uint8_t INS1[7] = {0x00, 0x11, 0x22, 'a', 'a', 'a', 0x33}; 194c5f01b2fSopenharmony_ci uint8_t INS2[7] = {0x00, 0x11, 'a', 'a', 'a', 0x22, 0x33}; 195c5f01b2fSopenharmony_ci uint8_t INS3[7] = {0x00, 'a', 'a', 'a', 0x11, 0x22, 0x33}; 196c5f01b2fSopenharmony_ci uint8_t INS4[7] = {'a', 'a', 'a', 0x00, 0x11, 0x22, 0x33}; 197c5f01b2fSopenharmony_ci 198c5f01b2fSopenharmony_ci uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33, 'b', 'b', 'b', 'b'}; 199c5f01b2fSopenharmony_ci uint8_t INS6[8] = {0x00, 0x11, 0x22, 'b', 'b', 'b', 'b', 0x33}; 200c5f01b2fSopenharmony_ci uint8_t INS7[8] = {0x00, 0x11, 'b', 'b', 'b', 'b', 0x22, 0x33}; 201c5f01b2fSopenharmony_ci uint8_t INS8[8] = {0x00, 'b', 'b', 'b', 'b', 0x11, 0x22, 0x33}; 202c5f01b2fSopenharmony_ci uint8_t INS9[8] = {'b', 'b', 'b', 'b', 0x00, 0x11, 0x22, 0x33}; 203c5f01b2fSopenharmony_ci 204c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 205c5f01b2fSopenharmony_ci uint8_t T[8] = {0x00, 0x11, 0x22, 0x33}; 206c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 4, 8); 207c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(INS0, T, 7)) FoundMask |= 1 << 0; 208c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(INS1, T, 7)) FoundMask |= 1 << 1; 209c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(INS2, T, 7)) FoundMask |= 1 << 2; 210c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(INS3, T, 7)) FoundMask |= 1 << 3; 211c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(INS4, T, 7)) FoundMask |= 1 << 4; 212c5f01b2fSopenharmony_ci 213c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5; 214c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6; 215c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7; 216c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS8, T, 8)) FoundMask |= 1 << 8; 217c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(INS9, T, 8)) FoundMask |= 1 << 9; 218c5f01b2fSopenharmony_ci 219c5f01b2fSopenharmony_ci } 220c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, (1 << 10) - 1); 221c5f01b2fSopenharmony_ci} 222c5f01b2fSopenharmony_ci 223c5f01b2fSopenharmony_ciTEST(FuzzerMutate, InsertRepeatedBytes1) { 224c5f01b2fSopenharmony_ci TestInsertRepeatedBytes(&MutationDispatcher::Mutate_InsertRepeatedBytes, 10000); 225c5f01b2fSopenharmony_ci} 226c5f01b2fSopenharmony_ciTEST(FuzzerMutate, InsertRepeatedBytes2) { 227c5f01b2fSopenharmony_ci TestInsertRepeatedBytes(&MutationDispatcher::Mutate, 300000); 228c5f01b2fSopenharmony_ci} 229c5f01b2fSopenharmony_ci 230c5f01b2fSopenharmony_civoid TestChangeByte(Mutator M, int NumIter) { 231c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 232c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 233c5f01b2fSopenharmony_ci Random Rand(0); 234c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 235c5f01b2fSopenharmony_ci int FoundMask = 0; 236c5f01b2fSopenharmony_ci uint8_t CH0[8] = {0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 237c5f01b2fSopenharmony_ci uint8_t CH1[8] = {0x00, 0xF1, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 238c5f01b2fSopenharmony_ci uint8_t CH2[8] = {0x00, 0x11, 0xF2, 0x33, 0x44, 0x55, 0x66, 0x77}; 239c5f01b2fSopenharmony_ci uint8_t CH3[8] = {0x00, 0x11, 0x22, 0xF3, 0x44, 0x55, 0x66, 0x77}; 240c5f01b2fSopenharmony_ci uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0xF4, 0x55, 0x66, 0x77}; 241c5f01b2fSopenharmony_ci uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF5, 0x66, 0x77}; 242c5f01b2fSopenharmony_ci uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF5, 0x77}; 243c5f01b2fSopenharmony_ci uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7}; 244c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 245c5f01b2fSopenharmony_ci uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 246c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 8, 9); 247c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 248c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 249c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 250c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 251c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4; 252c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 253c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 254c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 255c5f01b2fSopenharmony_ci } 256c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 255); 257c5f01b2fSopenharmony_ci} 258c5f01b2fSopenharmony_ci 259c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeByte1) { 260c5f01b2fSopenharmony_ci TestChangeByte(&MutationDispatcher::Mutate_ChangeByte, 1 << 15); 261c5f01b2fSopenharmony_ci} 262c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeByte2) { 263c5f01b2fSopenharmony_ci TestChangeByte(&MutationDispatcher::Mutate, 1 << 17); 264c5f01b2fSopenharmony_ci} 265c5f01b2fSopenharmony_ci 266c5f01b2fSopenharmony_civoid TestChangeBit(Mutator M, int NumIter) { 267c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 268c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 269c5f01b2fSopenharmony_ci Random Rand(0); 270c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 271c5f01b2fSopenharmony_ci int FoundMask = 0; 272c5f01b2fSopenharmony_ci uint8_t CH0[8] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 273c5f01b2fSopenharmony_ci uint8_t CH1[8] = {0x00, 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 274c5f01b2fSopenharmony_ci uint8_t CH2[8] = {0x00, 0x11, 0x02, 0x33, 0x44, 0x55, 0x66, 0x77}; 275c5f01b2fSopenharmony_ci uint8_t CH3[8] = {0x00, 0x11, 0x22, 0x37, 0x44, 0x55, 0x66, 0x77}; 276c5f01b2fSopenharmony_ci uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x54, 0x55, 0x66, 0x77}; 277c5f01b2fSopenharmony_ci uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x54, 0x66, 0x77}; 278c5f01b2fSopenharmony_ci uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x76, 0x77}; 279c5f01b2fSopenharmony_ci uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7}; 280c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 281c5f01b2fSopenharmony_ci uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 282c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 8, 9); 283c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 284c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 285c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 286c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 287c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4; 288c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 289c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 290c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 291c5f01b2fSopenharmony_ci } 292c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 255); 293c5f01b2fSopenharmony_ci} 294c5f01b2fSopenharmony_ci 295c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeBit1) { 296c5f01b2fSopenharmony_ci TestChangeBit(&MutationDispatcher::Mutate_ChangeBit, 1 << 16); 297c5f01b2fSopenharmony_ci} 298c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeBit2) { 299c5f01b2fSopenharmony_ci TestChangeBit(&MutationDispatcher::Mutate, 1 << 18); 300c5f01b2fSopenharmony_ci} 301c5f01b2fSopenharmony_ci 302c5f01b2fSopenharmony_civoid TestShuffleBytes(Mutator M, int NumIter) { 303c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 304c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 305c5f01b2fSopenharmony_ci Random Rand(0); 306c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 307c5f01b2fSopenharmony_ci int FoundMask = 0; 308c5f01b2fSopenharmony_ci uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66}; 309c5f01b2fSopenharmony_ci uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66}; 310c5f01b2fSopenharmony_ci uint8_t CH2[7] = {0x00, 0x33, 0x11, 0x22, 0x44, 0x55, 0x66}; 311c5f01b2fSopenharmony_ci uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x33}; 312c5f01b2fSopenharmony_ci uint8_t CH4[7] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x44, 0x66}; 313c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 314c5f01b2fSopenharmony_ci uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 315c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 7, 7); 316c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; 317c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; 318c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; 319c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3; 320c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4; 321c5f01b2fSopenharmony_ci } 322c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 31); 323c5f01b2fSopenharmony_ci} 324c5f01b2fSopenharmony_ci 325c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ShuffleBytes1) { 326c5f01b2fSopenharmony_ci TestShuffleBytes(&MutationDispatcher::Mutate_ShuffleBytes, 1 << 16); 327c5f01b2fSopenharmony_ci} 328c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ShuffleBytes2) { 329c5f01b2fSopenharmony_ci TestShuffleBytes(&MutationDispatcher::Mutate, 1 << 20); 330c5f01b2fSopenharmony_ci} 331c5f01b2fSopenharmony_ci 332c5f01b2fSopenharmony_civoid TestCopyPart(Mutator M, int NumIter) { 333c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 334c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 335c5f01b2fSopenharmony_ci Random Rand(0); 336c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 337c5f01b2fSopenharmony_ci int FoundMask = 0; 338c5f01b2fSopenharmony_ci uint8_t CH0[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11}; 339c5f01b2fSopenharmony_ci uint8_t CH1[7] = {0x55, 0x66, 0x22, 0x33, 0x44, 0x55, 0x66}; 340c5f01b2fSopenharmony_ci uint8_t CH2[7] = {0x00, 0x55, 0x66, 0x33, 0x44, 0x55, 0x66}; 341c5f01b2fSopenharmony_ci uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x66}; 342c5f01b2fSopenharmony_ci uint8_t CH4[7] = {0x00, 0x11, 0x11, 0x22, 0x33, 0x55, 0x66}; 343c5f01b2fSopenharmony_ci 344c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 345c5f01b2fSopenharmony_ci uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 346c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 7, 7); 347c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; 348c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; 349c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; 350c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3; 351c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4; 352c5f01b2fSopenharmony_ci } 353c5f01b2fSopenharmony_ci 354c5f01b2fSopenharmony_ci uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11, 0x22}; 355c5f01b2fSopenharmony_ci uint8_t CH6[8] = {0x22, 0x33, 0x44, 0x00, 0x11, 0x22, 0x33, 0x44}; 356c5f01b2fSopenharmony_ci uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x33, 0x44}; 357c5f01b2fSopenharmony_ci uint8_t CH8[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x22, 0x33, 0x44}; 358c5f01b2fSopenharmony_ci uint8_t CH9[8] = {0x00, 0x11, 0x22, 0x22, 0x33, 0x44, 0x33, 0x44}; 359c5f01b2fSopenharmony_ci 360c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 361c5f01b2fSopenharmony_ci uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 362c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 5, 8); 363c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 364c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 365c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 366c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH8, T, 8)) FoundMask |= 1 << 8; 367c5f01b2fSopenharmony_ci if (NewSize == 8 && !memcmp(CH9, T, 8)) FoundMask |= 1 << 9; 368c5f01b2fSopenharmony_ci } 369c5f01b2fSopenharmony_ci 370c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 1023); 371c5f01b2fSopenharmony_ci} 372c5f01b2fSopenharmony_ci 373c5f01b2fSopenharmony_ciTEST(FuzzerMutate, CopyPart1) { 374c5f01b2fSopenharmony_ci TestCopyPart(&MutationDispatcher::Mutate_CopyPart, 1 << 10); 375c5f01b2fSopenharmony_ci} 376c5f01b2fSopenharmony_ciTEST(FuzzerMutate, CopyPart2) { 377c5f01b2fSopenharmony_ci TestCopyPart(&MutationDispatcher::Mutate, 1 << 13); 378c5f01b2fSopenharmony_ci} 379c5f01b2fSopenharmony_ci 380c5f01b2fSopenharmony_civoid TestAddWordFromDictionary(Mutator M, int NumIter) { 381c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 382c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 383c5f01b2fSopenharmony_ci Random Rand(0); 384c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 385c5f01b2fSopenharmony_ci uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD}; 386c5f01b2fSopenharmony_ci uint8_t Word2[3] = {0xFF, 0xEE, 0xEF}; 387c5f01b2fSopenharmony_ci MD.AddWordToManualDictionary(Word(Word1, sizeof(Word1))); 388c5f01b2fSopenharmony_ci MD.AddWordToManualDictionary(Word(Word2, sizeof(Word2))); 389c5f01b2fSopenharmony_ci int FoundMask = 0; 390c5f01b2fSopenharmony_ci uint8_t CH0[7] = {0x00, 0x11, 0x22, 0xAA, 0xBB, 0xCC, 0xDD}; 391c5f01b2fSopenharmony_ci uint8_t CH1[7] = {0x00, 0x11, 0xAA, 0xBB, 0xCC, 0xDD, 0x22}; 392c5f01b2fSopenharmony_ci uint8_t CH2[7] = {0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0x11, 0x22}; 393c5f01b2fSopenharmony_ci uint8_t CH3[7] = {0xAA, 0xBB, 0xCC, 0xDD, 0x00, 0x11, 0x22}; 394c5f01b2fSopenharmony_ci uint8_t CH4[6] = {0x00, 0x11, 0x22, 0xFF, 0xEE, 0xEF}; 395c5f01b2fSopenharmony_ci uint8_t CH5[6] = {0x00, 0x11, 0xFF, 0xEE, 0xEF, 0x22}; 396c5f01b2fSopenharmony_ci uint8_t CH6[6] = {0x00, 0xFF, 0xEE, 0xEF, 0x11, 0x22}; 397c5f01b2fSopenharmony_ci uint8_t CH7[6] = {0xFF, 0xEE, 0xEF, 0x00, 0x11, 0x22}; 398c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 399c5f01b2fSopenharmony_ci uint8_t T[7] = {0x00, 0x11, 0x22}; 400c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 3, 7); 401c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; 402c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; 403c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; 404c5f01b2fSopenharmony_ci if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3; 405c5f01b2fSopenharmony_ci if (NewSize == 6 && !memcmp(CH4, T, 6)) FoundMask |= 1 << 4; 406c5f01b2fSopenharmony_ci if (NewSize == 6 && !memcmp(CH5, T, 6)) FoundMask |= 1 << 5; 407c5f01b2fSopenharmony_ci if (NewSize == 6 && !memcmp(CH6, T, 6)) FoundMask |= 1 << 6; 408c5f01b2fSopenharmony_ci if (NewSize == 6 && !memcmp(CH7, T, 6)) FoundMask |= 1 << 7; 409c5f01b2fSopenharmony_ci } 410c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 255); 411c5f01b2fSopenharmony_ci} 412c5f01b2fSopenharmony_ci 413c5f01b2fSopenharmony_ciTEST(FuzzerMutate, AddWordFromDictionary1) { 414c5f01b2fSopenharmony_ci TestAddWordFromDictionary( 415c5f01b2fSopenharmony_ci &MutationDispatcher::Mutate_AddWordFromManualDictionary, 1 << 15); 416c5f01b2fSopenharmony_ci} 417c5f01b2fSopenharmony_ci 418c5f01b2fSopenharmony_ciTEST(FuzzerMutate, AddWordFromDictionary2) { 419c5f01b2fSopenharmony_ci TestAddWordFromDictionary(&MutationDispatcher::Mutate, 1 << 15); 420c5f01b2fSopenharmony_ci} 421c5f01b2fSopenharmony_ci 422c5f01b2fSopenharmony_civoid TestAddWordFromDictionaryWithHint(Mutator M, int NumIter) { 423c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 424c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 425c5f01b2fSopenharmony_ci Random Rand(0); 426c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 427c5f01b2fSopenharmony_ci uint8_t W[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xFF, 0xEE, 0xEF}; 428c5f01b2fSopenharmony_ci size_t PosHint = 7777; 429c5f01b2fSopenharmony_ci MD.AddWordToAutoDictionary({Word(W, sizeof(W)), PosHint}); 430c5f01b2fSopenharmony_ci int FoundMask = 0; 431c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 432c5f01b2fSopenharmony_ci uint8_t T[10000]; 433c5f01b2fSopenharmony_ci memset(T, 0, sizeof(T)); 434c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 9000, 10000); 435c5f01b2fSopenharmony_ci if (NewSize >= PosHint + sizeof(W) && 436c5f01b2fSopenharmony_ci !memcmp(W, T + PosHint, sizeof(W))) 437c5f01b2fSopenharmony_ci FoundMask = 1; 438c5f01b2fSopenharmony_ci } 439c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 1); 440c5f01b2fSopenharmony_ci} 441c5f01b2fSopenharmony_ci 442c5f01b2fSopenharmony_ciTEST(FuzzerMutate, AddWordFromDictionaryWithHint1) { 443c5f01b2fSopenharmony_ci TestAddWordFromDictionaryWithHint( 444c5f01b2fSopenharmony_ci &MutationDispatcher::Mutate_AddWordFromTemporaryAutoDictionary, 1 << 5); 445c5f01b2fSopenharmony_ci} 446c5f01b2fSopenharmony_ci 447c5f01b2fSopenharmony_ciTEST(FuzzerMutate, AddWordFromDictionaryWithHint2) { 448c5f01b2fSopenharmony_ci TestAddWordFromDictionaryWithHint(&MutationDispatcher::Mutate, 1 << 10); 449c5f01b2fSopenharmony_ci} 450c5f01b2fSopenharmony_ci 451c5f01b2fSopenharmony_civoid TestChangeASCIIInteger(Mutator M, int NumIter) { 452c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 453c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 454c5f01b2fSopenharmony_ci Random Rand(0); 455c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 456c5f01b2fSopenharmony_ci 457c5f01b2fSopenharmony_ci uint8_t CH0[8] = {'1', '2', '3', '4', '5', '6', '7', '7'}; 458c5f01b2fSopenharmony_ci uint8_t CH1[8] = {'1', '2', '3', '4', '5', '6', '7', '9'}; 459c5f01b2fSopenharmony_ci uint8_t CH2[8] = {'2', '4', '6', '9', '1', '3', '5', '6'}; 460c5f01b2fSopenharmony_ci uint8_t CH3[8] = {'0', '6', '1', '7', '2', '8', '3', '9'}; 461c5f01b2fSopenharmony_ci int FoundMask = 0; 462c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 463c5f01b2fSopenharmony_ci uint8_t T[8] = {'1', '2', '3', '4', '5', '6', '7', '8'}; 464c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 8, 8); 465c5f01b2fSopenharmony_ci /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 466c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 467c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 468c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 469c5f01b2fSopenharmony_ci else if (NewSize == 8) FoundMask |= 1 << 4; 470c5f01b2fSopenharmony_ci } 471c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 31); 472c5f01b2fSopenharmony_ci} 473c5f01b2fSopenharmony_ci 474c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeASCIIInteger1) { 475c5f01b2fSopenharmony_ci TestChangeASCIIInteger(&MutationDispatcher::Mutate_ChangeASCIIInteger, 476c5f01b2fSopenharmony_ci 1 << 15); 477c5f01b2fSopenharmony_ci} 478c5f01b2fSopenharmony_ci 479c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeASCIIInteger2) { 480c5f01b2fSopenharmony_ci TestChangeASCIIInteger(&MutationDispatcher::Mutate, 1 << 15); 481c5f01b2fSopenharmony_ci} 482c5f01b2fSopenharmony_ci 483c5f01b2fSopenharmony_civoid TestChangeBinaryInteger(Mutator M, int NumIter) { 484c5f01b2fSopenharmony_ci std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 485c5f01b2fSopenharmony_ci fuzzer::EF = t.get(); 486c5f01b2fSopenharmony_ci Random Rand(0); 487c5f01b2fSopenharmony_ci MutationDispatcher MD(Rand, {}); 488c5f01b2fSopenharmony_ci 489c5f01b2fSopenharmony_ci uint8_t CH0[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x79}; 490c5f01b2fSopenharmony_ci uint8_t CH1[8] = {0x00, 0x11, 0x22, 0x31, 0x44, 0x55, 0x66, 0x77}; 491c5f01b2fSopenharmony_ci uint8_t CH2[8] = {0xff, 0x10, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 492c5f01b2fSopenharmony_ci uint8_t CH3[8] = {0x00, 0x11, 0x2a, 0x33, 0x44, 0x55, 0x66, 0x77}; 493c5f01b2fSopenharmony_ci uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x4f, 0x66, 0x77}; 494c5f01b2fSopenharmony_ci uint8_t CH5[8] = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88}; 495c5f01b2fSopenharmony_ci uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x00, 0x00, 0x00, 0x08, 0x77}; // Size 496c5f01b2fSopenharmony_ci uint8_t CH7[8] = {0x00, 0x08, 0x00, 0x33, 0x44, 0x55, 0x66, 0x77}; // Sw(Size) 497c5f01b2fSopenharmony_ci 498c5f01b2fSopenharmony_ci int FoundMask = 0; 499c5f01b2fSopenharmony_ci for (int i = 0; i < NumIter; i++) { 500c5f01b2fSopenharmony_ci uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 501c5f01b2fSopenharmony_ci size_t NewSize = (MD.*M)(T, 8, 8); 502c5f01b2fSopenharmony_ci /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 503c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 504c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 505c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 506c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4; 507c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 508c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 509c5f01b2fSopenharmony_ci else if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 510c5f01b2fSopenharmony_ci } 511c5f01b2fSopenharmony_ci EXPECT_EQ(FoundMask, 255); 512c5f01b2fSopenharmony_ci} 513c5f01b2fSopenharmony_ci 514c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeBinaryInteger1) { 515c5f01b2fSopenharmony_ci TestChangeBinaryInteger(&MutationDispatcher::Mutate_ChangeBinaryInteger, 516c5f01b2fSopenharmony_ci 1 << 12); 517c5f01b2fSopenharmony_ci} 518c5f01b2fSopenharmony_ci 519c5f01b2fSopenharmony_ciTEST(FuzzerMutate, ChangeBinaryInteger2) { 520c5f01b2fSopenharmony_ci TestChangeBinaryInteger(&MutationDispatcher::Mutate, 1 << 15); 521c5f01b2fSopenharmony_ci} 522c5f01b2fSopenharmony_ci 523c5f01b2fSopenharmony_ci 524c5f01b2fSopenharmony_ciTEST(FuzzerDictionary, ParseOneDictionaryEntry) { 525c5f01b2fSopenharmony_ci Unit U; 526c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry("", &U)); 527c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry(" ", &U)); 528c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry("\t ", &U)); 529c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry(" \" ", &U)); 530c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry(" zz\" ", &U)); 531c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry(" \"zz ", &U)); 532c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry(" \"\" ", &U)); 533c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("\"a\"", &U)); 534c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({'a'})); 535c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("\"abc\"", &U)); 536c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({'a', 'b', 'c'})); 537c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("abc=\"abc\"", &U)); 538c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({'a', 'b', 'c'})); 539c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseOneDictionaryEntry("\"\\\"", &U)); 540c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\\\"", &U)); 541c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({'\\'})); 542c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xAB\"", &U)); 543c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({0xAB})); 544c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xABz\\xDE\"", &U)); 545c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({0xAB, 'z', 0xDE})); 546c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("\"#\"", &U)); 547c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({'#'})); 548c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\"\"", &U)); 549c5f01b2fSopenharmony_ci EXPECT_EQ(U, Unit({'"'})); 550c5f01b2fSopenharmony_ci} 551c5f01b2fSopenharmony_ci 552c5f01b2fSopenharmony_ciTEST(FuzzerDictionary, ParseDictionaryFile) { 553c5f01b2fSopenharmony_ci std::vector<Unit> Units; 554c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseDictionaryFile("zzz\n", &Units)); 555c5f01b2fSopenharmony_ci EXPECT_FALSE(ParseDictionaryFile("", &Units)); 556c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseDictionaryFile("\n", &Units)); 557c5f01b2fSopenharmony_ci EXPECT_EQ(Units.size(), 0U); 558c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseDictionaryFile("#zzzz a b c d\n", &Units)); 559c5f01b2fSopenharmony_ci EXPECT_EQ(Units.size(), 0U); 560c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseDictionaryFile(" #zzzz\n", &Units)); 561c5f01b2fSopenharmony_ci EXPECT_EQ(Units.size(), 0U); 562c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseDictionaryFile(" #zzzz\n", &Units)); 563c5f01b2fSopenharmony_ci EXPECT_EQ(Units.size(), 0U); 564c5f01b2fSopenharmony_ci EXPECT_TRUE(ParseDictionaryFile(" #zzzz\naaa=\"aa\"", &Units)); 565c5f01b2fSopenharmony_ci EXPECT_EQ(Units, std::vector<Unit>({Unit({'a', 'a'})})); 566c5f01b2fSopenharmony_ci EXPECT_TRUE( 567c5f01b2fSopenharmony_ci ParseDictionaryFile(" #zzzz\naaa=\"aa\"\n\nabc=\"abc\"", &Units)); 568c5f01b2fSopenharmony_ci EXPECT_EQ(Units, 569c5f01b2fSopenharmony_ci std::vector<Unit>({Unit({'a', 'a'}), Unit({'a', 'b', 'c'})})); 570c5f01b2fSopenharmony_ci} 571c5f01b2fSopenharmony_ci 572c5f01b2fSopenharmony_ciTEST(FuzzerUtil, Base64) { 573c5f01b2fSopenharmony_ci EXPECT_EQ("", Base64({})); 574c5f01b2fSopenharmony_ci EXPECT_EQ("YQ==", Base64({'a'})); 575c5f01b2fSopenharmony_ci EXPECT_EQ("eA==", Base64({'x'})); 576c5f01b2fSopenharmony_ci EXPECT_EQ("YWI=", Base64({'a', 'b'})); 577c5f01b2fSopenharmony_ci EXPECT_EQ("eHk=", Base64({'x', 'y'})); 578c5f01b2fSopenharmony_ci EXPECT_EQ("YWJj", Base64({'a', 'b', 'c'})); 579c5f01b2fSopenharmony_ci EXPECT_EQ("eHl6", Base64({'x', 'y', 'z'})); 580c5f01b2fSopenharmony_ci EXPECT_EQ("YWJjeA==", Base64({'a', 'b', 'c', 'x'})); 581c5f01b2fSopenharmony_ci EXPECT_EQ("YWJjeHk=", Base64({'a', 'b', 'c', 'x', 'y'})); 582c5f01b2fSopenharmony_ci EXPECT_EQ("YWJjeHl6", Base64({'a', 'b', 'c', 'x', 'y', 'z'})); 583c5f01b2fSopenharmony_ci} 584c5f01b2fSopenharmony_ci 585c5f01b2fSopenharmony_ciTEST(Corpus, Distribution) { 586c5f01b2fSopenharmony_ci Random Rand(0); 587c5f01b2fSopenharmony_ci InputCorpus C(""); 588c5f01b2fSopenharmony_ci size_t N = 10; 589c5f01b2fSopenharmony_ci size_t TriesPerUnit = 1<<16; 590c5f01b2fSopenharmony_ci for (size_t i = 0; i < N; i++) 591c5f01b2fSopenharmony_ci C.AddToCorpus(Unit{ static_cast<uint8_t>(i) }, 0); 592c5f01b2fSopenharmony_ci 593c5f01b2fSopenharmony_ci std::vector<size_t> Hist(N); 594c5f01b2fSopenharmony_ci for (size_t i = 0; i < N * TriesPerUnit; i++) { 595c5f01b2fSopenharmony_ci Hist[C.ChooseUnitIdxToMutate(Rand)]++; 596c5f01b2fSopenharmony_ci } 597c5f01b2fSopenharmony_ci for (size_t i = 0; i < N; i++) { 598c5f01b2fSopenharmony_ci // A weak sanity check that every unit gets invoked. 599c5f01b2fSopenharmony_ci EXPECT_GT(Hist[i], TriesPerUnit / N / 3); 600c5f01b2fSopenharmony_ci } 601c5f01b2fSopenharmony_ci} 602c5f01b2fSopenharmony_ci 603c5f01b2fSopenharmony_ciTEST(Merge, Bad) { 604c5f01b2fSopenharmony_ci const char *kInvalidInputs[] = { 605c5f01b2fSopenharmony_ci "", 606c5f01b2fSopenharmony_ci "x", 607c5f01b2fSopenharmony_ci "3\nx", 608c5f01b2fSopenharmony_ci "2\n3", 609c5f01b2fSopenharmony_ci "2\n2", 610c5f01b2fSopenharmony_ci "2\n2\nA\n", 611c5f01b2fSopenharmony_ci "2\n2\nA\nB\nC\n", 612c5f01b2fSopenharmony_ci "0\n0\n", 613c5f01b2fSopenharmony_ci "1\n1\nA\nDONE 0", 614c5f01b2fSopenharmony_ci "1\n1\nA\nSTARTED 1", 615c5f01b2fSopenharmony_ci }; 616c5f01b2fSopenharmony_ci Merger M; 617c5f01b2fSopenharmony_ci for (auto S : kInvalidInputs) { 618c5f01b2fSopenharmony_ci // fprintf(stderr, "TESTING:\n%s\n", S); 619c5f01b2fSopenharmony_ci EXPECT_FALSE(M.Parse(S, false)); 620c5f01b2fSopenharmony_ci } 621c5f01b2fSopenharmony_ci} 622c5f01b2fSopenharmony_ci 623c5f01b2fSopenharmony_civoid EQ(const std::vector<uint32_t> &A, const std::vector<uint32_t> &B) { 624c5f01b2fSopenharmony_ci EXPECT_EQ(A, B); 625c5f01b2fSopenharmony_ci} 626c5f01b2fSopenharmony_ci 627c5f01b2fSopenharmony_civoid EQ(const std::vector<std::string> &A, const std::vector<std::string> &B) { 628c5f01b2fSopenharmony_ci std::set<std::string> a(A.begin(), A.end()); 629c5f01b2fSopenharmony_ci std::set<std::string> b(B.begin(), B.end()); 630c5f01b2fSopenharmony_ci EXPECT_EQ(a, b); 631c5f01b2fSopenharmony_ci} 632c5f01b2fSopenharmony_ci 633c5f01b2fSopenharmony_cistatic void Merge(const std::string &Input, 634c5f01b2fSopenharmony_ci const std::vector<std::string> Result, 635c5f01b2fSopenharmony_ci size_t NumNewFeatures) { 636c5f01b2fSopenharmony_ci Merger M; 637c5f01b2fSopenharmony_ci std::vector<std::string> NewFiles; 638c5f01b2fSopenharmony_ci EXPECT_TRUE(M.Parse(Input, true)); 639c5f01b2fSopenharmony_ci EXPECT_EQ(NumNewFeatures, M.Merge(&NewFiles)); 640c5f01b2fSopenharmony_ci EQ(NewFiles, Result); 641c5f01b2fSopenharmony_ci} 642c5f01b2fSopenharmony_ci 643c5f01b2fSopenharmony_ciTEST(Merge, Good) { 644c5f01b2fSopenharmony_ci Merger M; 645c5f01b2fSopenharmony_ci 646c5f01b2fSopenharmony_ci EXPECT_TRUE(M.Parse("1\n0\nAA\n", false)); 647c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files.size(), 1U); 648c5f01b2fSopenharmony_ci EXPECT_EQ(M.NumFilesInFirstCorpus, 0U); 649c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[0].Name, "AA"); 650c5f01b2fSopenharmony_ci EXPECT_TRUE(M.LastFailure.empty()); 651c5f01b2fSopenharmony_ci EXPECT_EQ(M.FirstNotProcessedFile, 0U); 652c5f01b2fSopenharmony_ci 653c5f01b2fSopenharmony_ci EXPECT_TRUE(M.Parse("2\n1\nAA\nBB\nSTARTED 0 42\n", false)); 654c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files.size(), 2U); 655c5f01b2fSopenharmony_ci EXPECT_EQ(M.NumFilesInFirstCorpus, 1U); 656c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[0].Name, "AA"); 657c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[1].Name, "BB"); 658c5f01b2fSopenharmony_ci EXPECT_EQ(M.LastFailure, "AA"); 659c5f01b2fSopenharmony_ci EXPECT_EQ(M.FirstNotProcessedFile, 1U); 660c5f01b2fSopenharmony_ci 661c5f01b2fSopenharmony_ci EXPECT_TRUE(M.Parse("3\n1\nAA\nBB\nC\n" 662c5f01b2fSopenharmony_ci "STARTED 0 1000\n" 663c5f01b2fSopenharmony_ci "DONE 0 1 2 3\n" 664c5f01b2fSopenharmony_ci "STARTED 1 1001\n" 665c5f01b2fSopenharmony_ci "DONE 1 4 5 6 \n" 666c5f01b2fSopenharmony_ci "STARTED 2 1002\n" 667c5f01b2fSopenharmony_ci "", true)); 668c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files.size(), 3U); 669c5f01b2fSopenharmony_ci EXPECT_EQ(M.NumFilesInFirstCorpus, 1U); 670c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[0].Name, "AA"); 671c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[0].Size, 1000U); 672c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[1].Name, "BB"); 673c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[1].Size, 1001U); 674c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[2].Name, "C"); 675c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files[2].Size, 1002U); 676c5f01b2fSopenharmony_ci EXPECT_EQ(M.LastFailure, "C"); 677c5f01b2fSopenharmony_ci EXPECT_EQ(M.FirstNotProcessedFile, 3U); 678c5f01b2fSopenharmony_ci EQ(M.Files[0].Features, {1, 2, 3}); 679c5f01b2fSopenharmony_ci EQ(M.Files[1].Features, {4, 5, 6}); 680c5f01b2fSopenharmony_ci 681c5f01b2fSopenharmony_ci 682c5f01b2fSopenharmony_ci std::vector<std::string> NewFiles; 683c5f01b2fSopenharmony_ci 684c5f01b2fSopenharmony_ci EXPECT_TRUE(M.Parse("3\n2\nAA\nBB\nC\n" 685c5f01b2fSopenharmony_ci "STARTED 0 1000\nDONE 0 1 2 3\n" 686c5f01b2fSopenharmony_ci "STARTED 1 1001\nDONE 1 4 5 6 \n" 687c5f01b2fSopenharmony_ci "STARTED 2 1002\nDONE 2 6 1 3 \n" 688c5f01b2fSopenharmony_ci "", true)); 689c5f01b2fSopenharmony_ci EXPECT_EQ(M.Files.size(), 3U); 690c5f01b2fSopenharmony_ci EXPECT_EQ(M.NumFilesInFirstCorpus, 2U); 691c5f01b2fSopenharmony_ci EXPECT_TRUE(M.LastFailure.empty()); 692c5f01b2fSopenharmony_ci EXPECT_EQ(M.FirstNotProcessedFile, 3U); 693c5f01b2fSopenharmony_ci EQ(M.Files[0].Features, {1, 2, 3}); 694c5f01b2fSopenharmony_ci EQ(M.Files[1].Features, {4, 5, 6}); 695c5f01b2fSopenharmony_ci EQ(M.Files[2].Features, {1, 3, 6}); 696c5f01b2fSopenharmony_ci EXPECT_EQ(0U, M.Merge(&NewFiles)); 697c5f01b2fSopenharmony_ci EQ(NewFiles, {}); 698c5f01b2fSopenharmony_ci 699c5f01b2fSopenharmony_ci EXPECT_TRUE(M.Parse("3\n1\nA\nB\nC\n" 700c5f01b2fSopenharmony_ci "STARTED 0 1000\nDONE 0 1 2 3\n" 701c5f01b2fSopenharmony_ci "STARTED 1 1001\nDONE 1 4 5 6 \n" 702c5f01b2fSopenharmony_ci "STARTED 2 1002\nDONE 2 6 1 3\n" 703c5f01b2fSopenharmony_ci "", true)); 704c5f01b2fSopenharmony_ci EQ(M.Files[0].Features, {1, 2, 3}); 705c5f01b2fSopenharmony_ci EQ(M.Files[1].Features, {4, 5, 6}); 706c5f01b2fSopenharmony_ci EQ(M.Files[2].Features, {1, 3, 6}); 707c5f01b2fSopenharmony_ci EXPECT_EQ(3U, M.Merge(&NewFiles)); 708c5f01b2fSopenharmony_ci EQ(NewFiles, {"B"}); 709c5f01b2fSopenharmony_ci} 710c5f01b2fSopenharmony_ci 711c5f01b2fSopenharmony_ciTEST(Merge, Merge) { 712c5f01b2fSopenharmony_ci 713c5f01b2fSopenharmony_ci Merge("3\n1\nA\nB\nC\n" 714c5f01b2fSopenharmony_ci "STARTED 0 1000\nDONE 0 1 2 3\n" 715c5f01b2fSopenharmony_ci "STARTED 1 1001\nDONE 1 4 5 6 \n" 716c5f01b2fSopenharmony_ci "STARTED 2 1002\nDONE 2 6 1 3 \n", 717c5f01b2fSopenharmony_ci {"B"}, 3); 718c5f01b2fSopenharmony_ci 719c5f01b2fSopenharmony_ci Merge("3\n0\nA\nB\nC\n" 720c5f01b2fSopenharmony_ci "STARTED 0 2000\nDONE 0 1 2 3\n" 721c5f01b2fSopenharmony_ci "STARTED 1 1001\nDONE 1 4 5 6 \n" 722c5f01b2fSopenharmony_ci "STARTED 2 1002\nDONE 2 6 1 3 \n", 723c5f01b2fSopenharmony_ci {"A", "B", "C"}, 6); 724c5f01b2fSopenharmony_ci 725c5f01b2fSopenharmony_ci Merge("4\n0\nA\nB\nC\nD\n" 726c5f01b2fSopenharmony_ci "STARTED 0 2000\nDONE 0 1 2 3\n" 727c5f01b2fSopenharmony_ci "STARTED 1 1101\nDONE 1 4 5 6 \n" 728c5f01b2fSopenharmony_ci "STARTED 2 1102\nDONE 2 6 1 3 100 \n" 729c5f01b2fSopenharmony_ci "STARTED 3 1000\nDONE 3 1 \n", 730c5f01b2fSopenharmony_ci {"A", "B", "C", "D"}, 7); 731c5f01b2fSopenharmony_ci 732c5f01b2fSopenharmony_ci Merge("4\n1\nA\nB\nC\nD\n" 733c5f01b2fSopenharmony_ci "STARTED 0 2000\nDONE 0 4 5 6 7 8\n" 734c5f01b2fSopenharmony_ci "STARTED 1 1100\nDONE 1 1 2 3 \n" 735c5f01b2fSopenharmony_ci "STARTED 2 1100\nDONE 2 2 3 \n" 736c5f01b2fSopenharmony_ci "STARTED 3 1000\nDONE 3 1 \n", 737c5f01b2fSopenharmony_ci {"B", "D"}, 3); 738c5f01b2fSopenharmony_ci} 739