From 3c704eefe1dfb7f35e80b8af9bd80c6eeaf7d5de Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Wed, 6 Dec 2017 23:35:02 +0000 Subject: [libFuzzer] Decrease stack usage in unit tests Summary: With 3 Dictionary objects, each containing space of ~16k DictionaryEntry objects, the MutationDispatcher object is fairly memory heavy. On platforms with a lower default stack size, this can cause panics in FuzzerUnittest as those tests stack-allocate the MutationDispatcher. This may be especially problematic for platforms that do not (yet) have a way to programmatically change their stack size, aside from link-time flags. In general, it seems more prudent to use the heap for an object of this size. Reviewers: kcc, morehouse Reviewed By: kcc Differential Revision: https://reviews.llvm.org/D40926 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@319988 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/fuzzer/tests/FuzzerUnittest.cpp | 52 ++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'lib') diff --git a/lib/fuzzer/tests/FuzzerUnittest.cpp b/lib/fuzzer/tests/FuzzerUnittest.cpp index c11299954..97ec3b4bb 100644 --- a/lib/fuzzer/tests/FuzzerUnittest.cpp +++ b/lib/fuzzer/tests/FuzzerUnittest.cpp @@ -32,7 +32,7 @@ TEST(Fuzzer, CrossOver) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); Unit A({0, 1, 2}), B({5, 6, 7}); Unit C; Unit Expected[] = { @@ -75,8 +75,8 @@ TEST(Fuzzer, CrossOver) { Set FoundUnits, ExpectedUnitsWitThisLength; for (int Iter = 0; Iter < 3000; Iter++) { C.resize(Len); - size_t NewSize = MD.CrossOver(A.data(), A.size(), B.data(), B.size(), - C.data(), C.size()); + size_t NewSize = MD->CrossOver(A.data(), A.size(), B.data(), B.size(), + C.data(), C.size()); C.resize(NewSize); FoundUnits.insert(C); } @@ -120,11 +120,11 @@ void TestEraseBytes(Mutator M, int NumIter) { Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; for (int i = 0; i < NumIter; i++) { uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; - size_t NewSize = (MD.*M)(T, sizeof(T), sizeof(T)); + size_t NewSize = (*MD.*M)(T, sizeof(T), sizeof(T)); if (NewSize == 7 && !memcmp(REM0, T, 7)) FoundMask |= 1 << 0; if (NewSize == 7 && !memcmp(REM1, T, 7)) FoundMask |= 1 << 1; if (NewSize == 7 && !memcmp(REM2, T, 7)) FoundMask |= 1 << 2; @@ -156,7 +156,7 @@ void TestInsertByte(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; uint8_t INS0[8] = {0xF1, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; uint8_t INS1[8] = {0x00, 0xF2, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; @@ -168,7 +168,7 @@ void TestInsertByte(Mutator M, int NumIter) { uint8_t INS7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF8}; for (int i = 0; i < NumIter; i++) { uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; - size_t NewSize = (MD.*M)(T, 7, 8); + size_t NewSize = (*MD.*M)(T, 7, 8); if (NewSize == 8 && !memcmp(INS0, T, 8)) FoundMask |= 1 << 0; if (NewSize == 8 && !memcmp(INS1, T, 8)) FoundMask |= 1 << 1; if (NewSize == 8 && !memcmp(INS2, T, 8)) FoundMask |= 1 << 2; @@ -192,7 +192,7 @@ void TestInsertRepeatedBytes(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; uint8_t INS0[7] = {0x00, 0x11, 0x22, 0x33, 'a', 'a', 'a'}; uint8_t INS1[7] = {0x00, 0x11, 0x22, 'a', 'a', 'a', 0x33}; @@ -208,7 +208,7 @@ void TestInsertRepeatedBytes(Mutator M, int NumIter) { for (int i = 0; i < NumIter; i++) { uint8_t T[8] = {0x00, 0x11, 0x22, 0x33}; - size_t NewSize = (MD.*M)(T, 4, 8); + size_t NewSize = (*MD.*M)(T, 4, 8); if (NewSize == 7 && !memcmp(INS0, T, 7)) FoundMask |= 1 << 0; if (NewSize == 7 && !memcmp(INS1, T, 7)) FoundMask |= 1 << 1; if (NewSize == 7 && !memcmp(INS2, T, 7)) FoundMask |= 1 << 2; @@ -236,7 +236,7 @@ void TestChangeByte(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; uint8_t CH0[8] = {0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; uint8_t CH1[8] = {0x00, 0xF1, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; @@ -248,7 +248,7 @@ void TestChangeByte(Mutator M, int NumIter) { uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7}; for (int i = 0; i < NumIter; i++) { uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; - size_t NewSize = (MD.*M)(T, 8, 9); + size_t NewSize = (*MD.*M)(T, 8, 9); if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; @@ -272,7 +272,7 @@ void TestChangeBit(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; uint8_t CH0[8] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; uint8_t CH1[8] = {0x00, 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; @@ -284,7 +284,7 @@ void TestChangeBit(Mutator M, int NumIter) { uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7}; for (int i = 0; i < NumIter; i++) { uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; - size_t NewSize = (MD.*M)(T, 8, 9); + size_t NewSize = (*MD.*M)(T, 8, 9); if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; @@ -308,7 +308,7 @@ void TestShuffleBytes(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66}; uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66}; @@ -317,7 +317,7 @@ void TestShuffleBytes(Mutator M, int NumIter) { uint8_t CH4[7] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x44, 0x66}; for (int i = 0; i < NumIter; i++) { uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; - size_t NewSize = (MD.*M)(T, 7, 7); + size_t NewSize = (*MD.*M)(T, 7, 7); if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; @@ -338,7 +338,7 @@ void TestCopyPart(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); int FoundMask = 0; uint8_t CH0[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11}; uint8_t CH1[7] = {0x55, 0x66, 0x22, 0x33, 0x44, 0x55, 0x66}; @@ -348,7 +348,7 @@ void TestCopyPart(Mutator M, int NumIter) { for (int i = 0; i < NumIter; i++) { uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; - size_t NewSize = (MD.*M)(T, 7, 7); + size_t NewSize = (*MD.*M)(T, 7, 7); if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; @@ -364,7 +364,7 @@ void TestCopyPart(Mutator M, int NumIter) { for (int i = 0; i < NumIter; i++) { uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; - size_t NewSize = (MD.*M)(T, 5, 8); + size_t NewSize = (*MD.*M)(T, 5, 8); if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; @@ -386,11 +386,11 @@ void TestAddWordFromDictionary(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD}; uint8_t Word2[3] = {0xFF, 0xEE, 0xEF}; - MD.AddWordToManualDictionary(Word(Word1, sizeof(Word1))); - MD.AddWordToManualDictionary(Word(Word2, sizeof(Word2))); + MD->AddWordToManualDictionary(Word(Word1, sizeof(Word1))); + MD->AddWordToManualDictionary(Word(Word2, sizeof(Word2))); int FoundMask = 0; uint8_t CH0[7] = {0x00, 0x11, 0x22, 0xAA, 0xBB, 0xCC, 0xDD}; uint8_t CH1[7] = {0x00, 0x11, 0xAA, 0xBB, 0xCC, 0xDD, 0x22}; @@ -402,7 +402,7 @@ void TestAddWordFromDictionary(Mutator M, int NumIter) { uint8_t CH7[6] = {0xFF, 0xEE, 0xEF, 0x00, 0x11, 0x22}; for (int i = 0; i < NumIter; i++) { uint8_t T[7] = {0x00, 0x11, 0x22}; - size_t NewSize = (MD.*M)(T, 3, 7); + size_t NewSize = (*MD.*M)(T, 3, 7); if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; @@ -428,7 +428,7 @@ void TestChangeASCIIInteger(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); uint8_t CH0[8] = {'1', '2', '3', '4', '5', '6', '7', '7'}; uint8_t CH1[8] = {'1', '2', '3', '4', '5', '6', '7', '9'}; @@ -437,7 +437,7 @@ void TestChangeASCIIInteger(Mutator M, int NumIter) { int FoundMask = 0; for (int i = 0; i < NumIter; i++) { uint8_t T[8] = {'1', '2', '3', '4', '5', '6', '7', '8'}; - size_t NewSize = (MD.*M)(T, 8, 8); + size_t NewSize = (*MD.*M)(T, 8, 8); /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; @@ -460,7 +460,7 @@ void TestChangeBinaryInteger(Mutator M, int NumIter) { std::unique_ptr t(new ExternalFunctions()); fuzzer::EF = t.get(); Random Rand(0); - MutationDispatcher MD(Rand, {}); + std::unique_ptr MD(new MutationDispatcher(Rand, {})); uint8_t CH0[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x79}; uint8_t CH1[8] = {0x00, 0x11, 0x22, 0x31, 0x44, 0x55, 0x66, 0x77}; @@ -474,7 +474,7 @@ void TestChangeBinaryInteger(Mutator M, int NumIter) { int FoundMask = 0; for (int i = 0; i < NumIter; i++) { uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; - size_t NewSize = (MD.*M)(T, 8, 8); + size_t NewSize = (*MD.*M)(T, 8, 8); /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; -- cgit v1.2.3