Index: compiler-rt/lib/fuzzer/tests/FuzzerTestUtil.h =================================================================== --- compiler-rt/lib/fuzzer/tests/FuzzerTestUtil.h +++ compiler-rt/lib/fuzzer/tests/FuzzerTestUtil.h @@ -11,21 +11,31 @@ #ifndef LLVM_FUZZER_TEST_UTIL_H #define LLVM_FUZZER_TEST_UTIL_H +#include "FuzzerExtFunctions.h" #include "FuzzerTracePC.h" #include "gtest/gtest.h" #include #include #include #include -#include namespace fuzzer { // Helper environment to ensure that: // * The PRNG is seeded deterministically. +// * The ExternalFunctions are allocated. class TestEnvironment : public testing::Environment { public: - void SetUp() override { srand(0); } + void SetUp() override { + srand(0); + ExtFuncs.reset(new ExternalFunctions()); + EF = ExtFuncs.get(); + } + + void TearDown() override { EF = nullptr; } + +private: + std::unique_ptr ExtFuncs; }; // Test utilities for picking repeatable psuedorandom numbers. Index: compiler-rt/lib/fuzzer/tests/FuzzerUnittest.cpp =================================================================== --- compiler-rt/lib/fuzzer/tests/FuzzerUnittest.cpp +++ compiler-rt/lib/fuzzer/tests/FuzzerUnittest.cpp @@ -48,8 +48,6 @@ } TEST(Fuzzer, CrossOver) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); Unit A({0, 1, 2}), B({5, 6, 7}); @@ -118,8 +116,6 @@ size_t MaxSize); void TestEraseBytes(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); uint8_t REM0[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; uint8_t REM1[8] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; uint8_t REM2[8] = {0x00, 0x11, 0x33, 0x44, 0x55, 0x66, 0x77}; @@ -172,8 +168,6 @@ } void TestInsertByte(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; @@ -208,8 +202,6 @@ } void TestInsertRepeatedBytes(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; @@ -252,8 +244,6 @@ } void TestChangeByte(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; @@ -288,8 +278,6 @@ } void TestChangeBit(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; @@ -324,8 +312,6 @@ } void TestShuffleBytes(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; @@ -354,8 +340,6 @@ } void TestCopyPart(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; @@ -404,8 +388,6 @@ // This (non exhaustively) tests if `Mutate_CopyPart` tries to perform an // insert on an input of size `MaxSize`. Performing an insert in this case // will lead to the mutation failing. - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); uint8_t Data[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11, 0x22}; @@ -417,8 +399,6 @@ } void TestAddWordFromDictionary(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD}; @@ -459,8 +439,6 @@ } void TestChangeASCIIInteger(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {})); @@ -491,8 +469,6 @@ } void TestChangeBinaryInteger(Mutator M, int NumIter) { - std::unique_ptr t(new ExternalFunctions()); - fuzzer::EF = t.get(); Random Rand(0); std::unique_ptr MD(new MutationDispatcher(Rand, {}));