1//===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8// fuzzer::Dictionary
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_FUZZER_DICTIONARY_H
12#define LLVM_FUZZER_DICTIONARY_H
13
14#include "FuzzerDefs.h"
15#include "FuzzerIO.h"
16#include "FuzzerUtil.h"
17#include <algorithm>
18#include <limits>
19
20namespace fuzzer {
21// A simple POD sized array of bytes.
22template <size_t kMaxSizeT> class FixedWord {
23public:
24 static const size_t kMaxSize = kMaxSizeT;
25 FixedWord() {}
26 FixedWord(const uint8_t *B, size_t S) { Set(B, S); }
27
28 void Set(const uint8_t *B, size_t S) {
29 static_assert(kMaxSizeT <= std::numeric_limits<uint8_t>::max(),
30 "FixedWord::kMaxSizeT cannot fit in a uint8_t.");
31 assert(S <= kMaxSize);
32 // memcpy cannot take null pointer arguments even if Size is 0.
33 if (S)
34 memcpy(Data, B, S);
35 Size = static_cast<uint8_t>(S);
36 }
37
38 bool operator==(const FixedWord<kMaxSize> &w) const {
39 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
40 }
41
42 static size_t GetMaxSize() { return kMaxSize; }
43 const uint8_t *data() const { return Data; }
44 uint8_t size() const { return Size; }
45
46private:
47 uint8_t Size = 0;
48 uint8_t Data[kMaxSize];
49};
50
51typedef FixedWord<64> Word;
52
53class DictionaryEntry {
54 public:
55 DictionaryEntry() {}
56 DictionaryEntry(Word W) : W(W) {}
57 DictionaryEntry(Word W, size_t PositionHint)
58 : W(W), PositionHint(PositionHint) {}
59 const Word &GetW() const { return W; }
60
61 bool HasPositionHint() const {
62 return PositionHint != std::numeric_limits<size_t>::max();
63 }
64 size_t GetPositionHint() const {
65 assert(HasPositionHint());
66 return PositionHint;
67 }
68 void IncUseCount() { UseCount++; }
69 void IncSuccessCount() { SuccessCount++; }
70 size_t GetUseCount() const { return UseCount; }
71 size_t GetSuccessCount() const {return SuccessCount; }
72
73 void Print(const char *PrintAfter = "\n") {
74 PrintASCII(Data: W.data(), Size: W.size());
75 if (HasPositionHint())
76 Printf(Fmt: "@%zd", GetPositionHint());
77 Printf(Fmt: "%s", PrintAfter);
78 }
79
80private:
81 Word W;
82 size_t PositionHint = std::numeric_limits<size_t>::max();
83 size_t UseCount = 0;
84 size_t SuccessCount = 0;
85};
86
87class Dictionary {
88 public:
89 static const size_t kMaxDictSize = 1 << 14;
90
91 bool ContainsWord(const Word &W) const {
92 return std::any_of(first: begin(), last: end(), pred: [&](const DictionaryEntry &DE) {
93 return DE.GetW() == W;
94 });
95 }
96 const DictionaryEntry *begin() const { return &DE[0]; }
97 const DictionaryEntry *end() const { return begin() + Size; }
98 DictionaryEntry & operator[] (size_t Idx) {
99 assert(Idx < Size);
100 return DE[Idx];
101 }
102 void push_back(DictionaryEntry DE) {
103 if (Size < kMaxDictSize)
104 this->DE[Size++] = DE;
105 }
106 void clear() { Size = 0; }
107 bool empty() const { return Size == 0; }
108 size_t size() const { return Size; }
109
110private:
111 DictionaryEntry DE[kMaxDictSize];
112 size_t Size = 0;
113};
114
115// Parses one dictionary entry.
116// If successful, writes the entry to Unit and returns true,
117// otherwise returns false.
118bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
119// Parses the dictionary file, fills Units, returns true iff all lines
120// were parsed successfully.
121bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
122
123} // namespace fuzzer
124
125#endif // LLVM_FUZZER_DICTIONARY_H
126

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of compiler-rt/lib/fuzzer/FuzzerDictionary.h