summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Fuzzer/FuzzerInternal.h
diff options
context:
space:
mode:
authorKostya Serebryany <kcc@google.com>2016-01-16 03:53:32 +0000
committerKostya Serebryany <kcc@google.com>2016-01-16 03:53:32 +0000
commit476f0ce31a442f5fadc3e0978c2e3190faf96447 (patch)
tree5dadf11893d75cf52ee4d2445408b3046ecc469d /llvm/lib/Fuzzer/FuzzerInternal.h
parent33ff1dda6a2638f4e1040c282ddc75e5e4483871 (diff)
downloadbcm5719-llvm-476f0ce31a442f5fadc3e0978c2e3190faf96447.tar.gz
bcm5719-llvm-476f0ce31a442f5fadc3e0978c2e3190faf96447.zip
[libFuzzer] replace vector with a simpler data structure in the Dictionaries to avoid memory allocations on hot path
llvm-svn: 257985
Diffstat (limited to 'llvm/lib/Fuzzer/FuzzerInternal.h')
-rw-r--r--llvm/lib/Fuzzer/FuzzerInternal.h40
1 files changed, 38 insertions, 2 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerInternal.h b/llvm/lib/Fuzzer/FuzzerInternal.h
index 44af57d5312..bd513d019cb 100644
--- a/llvm/lib/Fuzzer/FuzzerInternal.h
+++ b/llvm/lib/Fuzzer/FuzzerInternal.h
@@ -18,6 +18,7 @@
#include <cstddef>
#include <cstdlib>
#include <string>
+#include <string.h>
#include <vector>
#include <unordered_set>
@@ -27,6 +28,40 @@ namespace fuzzer {
using namespace std::chrono;
typedef std::vector<uint8_t> Unit;
+// A simple POD sized array of bytes.
+template<size_t kMaxSize>
+class FixedWord {
+ public:
+
+ FixedWord() : Size(0) {}
+ FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
+
+ void Set(const uint8_t *B, uint8_t S) {
+ assert(S <= kMaxSize);
+ memcpy(Data, B, S);
+ Size = S;
+ }
+
+ bool operator == (const FixedWord<kMaxSize> &w) const {
+ return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
+ }
+
+ bool operator < (const FixedWord<kMaxSize> &w) const {
+ if (Size != w.Size) return Size < w.Size;
+ return memcmp(Data, w.Data, Size) < 0;
+ }
+
+ static size_t GetMaxSize() { return kMaxSize; }
+ const uint8_t *data() const { return Data; }
+ uint8_t size() const { return Size; }
+
+ private:
+ uint8_t Size;
+ uint8_t Data[kMaxSize];
+};
+
+typedef FixedWord<27> Word; // 28 bytes.
+
std::string FileToString(const std::string &Path);
Unit FileToVector(const std::string &Path);
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
@@ -43,6 +78,7 @@ void PrintHexArray(const uint8_t *Data, size_t Size,
const char *PrintAfter = "");
void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
void PrintASCII(const Unit &U, const char *PrintAfter = "");
+void PrintASCII(const Word &W, const char *PrintAfter = "");
std::string Hash(const Unit &U);
void SetTimer(int Seconds);
std::string Base64(const Unit &U);
@@ -118,9 +154,9 @@ class MutationDispatcher {
size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
size_t Size2, uint8_t *Out, size_t MaxOutSize);
- void AddWordToManualDictionary(const Unit &Word);
+ void AddWordToManualDictionary(const Word &W);
- void AddWordToAutoDictionary(const Unit &Word, size_t PositionHint);
+ void AddWordToAutoDictionary(const Word &W, size_t PositionHint);
void ClearAutoDictionary();
void PrintRecommendedDictionary();
OpenPOWER on IntegriCloud