Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/tools/fuzzing/libfuzzer/FuzzerDictionary.h
Line
Count
Source (jump to first uncovered line)
1
//===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- C++ -* ===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
// fuzzer::Dictionary
10
//===----------------------------------------------------------------------===//
11
12
#ifndef LLVM_FUZZER_DICTIONARY_H
13
#define LLVM_FUZZER_DICTIONARY_H
14
15
#include "FuzzerDefs.h"
16
#include "FuzzerIO.h"
17
#include "FuzzerUtil.h"
18
#include <algorithm>
19
#include <limits>
20
21
namespace fuzzer {
22
// A simple POD sized array of bytes.
23
template <size_t kMaxSizeT> class FixedWord {
24
public:
25
  static const size_t kMaxSize = kMaxSizeT;
26
150k
  FixedWord() {}
27
0
  FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
28
29
0
  void Set(const uint8_t *B, uint8_t S) {
30
0
    assert(S <= kMaxSize);
31
0
    memcpy(Data, B, S);
32
0
    Size = S;
33
0
  }
34
35
0
  bool operator==(const FixedWord<kMaxSize> &w) const {
36
0
    return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
37
0
  }
38
39
0
  static size_t GetMaxSize() { return kMaxSize; }
40
0
  const uint8_t *data() const { return Data; }
41
0
  uint8_t size() const { return Size; }
42
43
private:
44
  uint8_t Size = 0;
45
  uint8_t Data[kMaxSize];
46
};
47
48
typedef FixedWord<64> Word;
49
50
class DictionaryEntry {
51
 public:
52
147k
  DictionaryEntry() {}
53
0
  DictionaryEntry(Word W) : W(W) {}
54
0
  DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
55
0
  const Word &GetW() const { return W; }
56
57
0
  bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
58
0
  size_t GetPositionHint() const {
59
0
    assert(HasPositionHint());
60
0
    return PositionHint;
61
0
  }
62
0
  void IncUseCount() { UseCount++; }
63
0
  void IncSuccessCount() { SuccessCount++; }
64
0
  size_t GetUseCount() const { return UseCount; }
65
0
  size_t GetSuccessCount() const {return SuccessCount; }
66
67
0
  void Print(const char *PrintAfter = "\n") {
68
0
    PrintASCII(W.data(), W.size());
69
0
    if (HasPositionHint())
70
0
      Printf("@%zd", GetPositionHint());
71
0
    Printf("%s", PrintAfter);
72
0
  }
73
74
private:
75
  Word W;
76
  size_t PositionHint = std::numeric_limits<size_t>::max();
77
  size_t UseCount = 0;
78
  size_t SuccessCount = 0;
79
};
80
81
class Dictionary {
82
 public:
83
  static const size_t kMaxDictSize = 1 << 14;
84
85
0
  bool ContainsWord(const Word &W) const {
86
0
    return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
87
0
      return DE.GetW() == W;
88
0
    });
89
0
  }
90
6
  const DictionaryEntry *begin() const { return &DE[0]; }
91
3
  const DictionaryEntry *end() const { return begin() + Size; }
92
0
  DictionaryEntry & operator[] (size_t Idx) {
93
0
    assert(Idx < Size);
94
0
    return DE[Idx];
95
0
  }
96
0
  void push_back(DictionaryEntry DE) {
97
0
    if (Size < kMaxDictSize)
98
0
      this->DE[Size++] = DE;
99
0
  }
100
0
  void clear() { Size = 0; }
101
0
  bool empty() const { return Size == 0; }
102
0
  size_t size() const { return Size; }
103
104
private:
105
  DictionaryEntry DE[kMaxDictSize];
106
  size_t Size = 0;
107
};
108
109
// Parses one dictionary entry.
110
// If successful, write the enty to Unit and returns true,
111
// otherwise returns false.
112
bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
113
// Parses the dictionary file, fills Units, returns true iff all lines
114
// were parsed successfully.
115
bool ParseDictionaryFile(const std::string &Text, Vector<Unit> *Units);
116
117
}  // namespace fuzzer
118
119
#endif  // LLVM_FUZZER_DICTIONARY_H