[compiler-rt] r319988 - [libFuzzer] Decrease stack usage in unit tests

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 15:35:02 PST 2017


Author: kcc
Date: Wed Dec  6 15:35:02 2017
New Revision: 319988

URL: http://llvm.org/viewvc/llvm-project?rev=319988&view=rev
Log:
[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

Modified:
    compiler-rt/trunk/lib/fuzzer/tests/FuzzerUnittest.cpp

Modified: compiler-rt/trunk/lib/fuzzer/tests/FuzzerUnittest.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/fuzzer/tests/FuzzerUnittest.cpp?rev=319988&r1=319987&r2=319988&view=diff
==============================================================================
--- compiler-rt/trunk/lib/fuzzer/tests/FuzzerUnittest.cpp (original)
+++ compiler-rt/trunk/lib/fuzzer/tests/FuzzerUnittest.cpp Wed Dec  6 15:35:02 2017
@@ -32,7 +32,7 @@ TEST(Fuzzer, CrossOver) {
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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<Unit> 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 NumIt
 
 
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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 NumIt
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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 NumIt
   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,
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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,
 
   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 NumIt
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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 NumIt
   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 NumIte
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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 NumIte
   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 Num
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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 Num
   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<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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
   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, i
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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, i
   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,
   std::unique_ptr<ExternalFunctions> t(new ExternalFunctions());
   fuzzer::EF = t.get();
   Random Rand(0);
-  MutationDispatcher MD(Rand, {});
+  std::unique_ptr<MutationDispatcher> 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 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;




More information about the llvm-commits mailing list