LLVM  5.0.0
FuzzerTracePC.h
Go to the documentation of this file.
1 //===- FuzzerTracePC.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::TracePC
10 //===----------------------------------------------------------------------===//
11 
12 #ifndef LLVM_FUZZER_TRACE_PC
13 #define LLVM_FUZZER_TRACE_PC
14 
15 #include "FuzzerDefs.h"
16 #include "FuzzerDictionary.h"
17 #include "FuzzerValueBitMap.h"
18 
19 #include <set>
20 
21 namespace fuzzer {
22 
23 // TableOfRecentCompares (TORC) remembers the most recently performed
24 // comparisons of type T.
25 // We record the arguments of CMP instructions in this table unconditionally
26 // because it seems cheaper this way than to compute some expensive
27 // conditions inside __sanitizer_cov_trace_cmp*.
28 // After the unit has been executed we may decide to use the contents of
29 // this table to populate a Dictionary.
30 template<class T, size_t kSizeT>
32  static const size_t kSize = kSizeT;
33  struct Pair {
34  T A, B;
35  };
37  void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
38  Idx = Idx % kSize;
39  Table[Idx].A = Arg1;
40  Table[Idx].B = Arg2;
41  }
42 
43  Pair Get(size_t I) { return Table[I % kSize]; }
44 
45  Pair Table[kSize];
46 };
47 
48 template <size_t kSizeT>
49 struct MemMemTable {
50  static const size_t kSize = kSizeT;
53 
54  void Add(const uint8_t *Data, size_t Size) {
55  if (Size <= 2) return;
56  Size = std::min(Size, Word::GetMaxSize());
57  size_t Idx = SimpleFastHash(Data, Size) % kSize;
58  MemMemWords[Idx].Set(Data, Size);
59  }
60  const Word &Get(size_t Idx) {
61  for (size_t i = 0; i < kSize; i++) {
62  const Word &W = MemMemWords[(Idx + i) % kSize];
63  if (W.size()) return W;
64  }
65  EmptyWord.Set(nullptr, 0);
66  return EmptyWord;
67  }
68 };
69 
70 class TracePC {
71  public:
72  static const size_t kNumPCs = 1 << 21;
73  // How many bits of PC are used from __sanitizer_cov_trace_pc.
74  static const size_t kTracePcBits = 18;
75 
77  void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop);
78  void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
79  template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
80  size_t GetTotalPCCoverage();
81  void SetUseCounters(bool UC) { UseCounters = UC; }
82  void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
83  void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
84  template <class Callback> void CollectFeatures(Callback CB) const;
85 
86  void ResetMaps() {
87  ValueProfileMap.Reset();
88  memset(Counters(), 0, GetNumPCs());
90  }
91 
92  void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
93  void PrintFeatureSet();
94 
95  void PrintModuleInfo();
96 
97  void PrintCoverage();
98  void DumpCoverage();
99 
100  void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
101  size_t n, bool StopAtZero);
102 
107 
108  void PrintNewPCs();
109  void InitializePrintNewPCs();
110  size_t GetNumPCs() const {
111  return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
112  }
113  uintptr_t GetPC(size_t Idx) {
114  assert(Idx < GetNumPCs());
115  return PCs()[Idx];
116  }
117 
118 private:
119  bool UseCounters = false;
120  bool UseValueProfile = false;
121  bool DoPrintNewPCs = false;
122 
123  struct Module {
124  uint32_t *Start, *Stop;
125  };
126 
127  Module Modules[4096];
128  size_t NumModules; // linker-initialized.
129  size_t NumGuards; // linker-initialized.
130 
131  struct { uint8_t *Start, *Stop; } ModuleCounters[4096];
132  size_t NumModulesWithInline8bitCounters; // linker-initialized.
133  size_t NumInline8bitCounters;
134 
135  uint8_t *Counters() const;
136  uintptr_t *PCs() const;
137 
138  std::set<uintptr_t> *PrintedPCs;
139 
140  ValueBitMap ValueProfileMap;
141 };
142 
143 template <class Callback> // void Callback(size_t Idx, uint8_t Value);
145 void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
146  size_t FirstFeature, Callback Handle8bitCounter) {
147  typedef uintptr_t LargeType;
148  const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
149  const size_t StepMask = Step - 1;
150  auto P = Begin;
151  // Iterate by 1 byte until either the alignment boundary or the end.
152  for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
153  if (uint8_t V = *P)
154  Handle8bitCounter(FirstFeature + P - Begin, V);
155 
156  // Iterate by Step bytes at a time.
157  for (; P < End; P += Step)
158  if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P))
159  for (size_t I = 0; I < Step; I++, Bundle >>= 8)
160  if (uint8_t V = Bundle & 0xff)
161  Handle8bitCounter(FirstFeature + P - Begin + I, V);
162 
163  // Iterate by 1 byte until the end.
164  for (; P < End; P++)
165  if (uint8_t V = *P)
166  Handle8bitCounter(FirstFeature + P - Begin, V);
167 }
168 
169 template <class Callback> // bool Callback(size_t Feature)
171 __attribute__((noinline))
172 void TracePC::CollectFeatures(Callback HandleFeature) const {
173  uint8_t *Counters = this->Counters();
174  size_t N = GetNumPCs();
175  auto Handle8bitCounter = [&](size_t Idx, uint8_t Counter) {
176  assert(Counter);
177  unsigned Bit = 0;
178  /**/ if (Counter >= 128) Bit = 7;
179  else if (Counter >= 32) Bit = 6;
180  else if (Counter >= 16) Bit = 5;
181  else if (Counter >= 8) Bit = 4;
182  else if (Counter >= 4) Bit = 3;
183  else if (Counter >= 3) Bit = 2;
184  else if (Counter >= 2) Bit = 1;
185  HandleFeature(Idx * 8 + Bit);
186  };
187 
188  size_t FirstFeature = 0;
189  ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter);
190  FirstFeature += N * 8;
191  for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
192  ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop,
193  FirstFeature, Handle8bitCounter);
194  FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start);
195  }
196 
199 
200  if (UseValueProfile)
201  ValueProfileMap.ForEach([&](size_t Idx) {
202  HandleFeature(N * 8 + Idx);
203  });
204 }
205 
206 extern TracePC TPC;
207 
208 } // namespace fuzzer
209 
210 #endif // LLVM_FUZZER_TRACE_PC
ATTRIBUTE_NO_SANITIZE_ALL void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End, size_t FirstFeature, Callback Handle8bitCounter)
size_t FirstFeature
void SetPrintNewPCs(bool P)
Definition: FuzzerTracePC.h:83
const Word & Get(size_t Idx)
Definition: FuzzerTracePC.h:60
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
uint8_t * ExtraCountersBegin()
static int Counter
void Add(const uint8_t *Data, size_t Size)
Definition: FuzzerTracePC.h:54
size_t SimpleFastHash(const uint8_t *Data, size_t Size)
Definition: FuzzerUtil.cpp:218
Word MemMemWords[kSize]
Definition: FuzzerTracePC.h:51
void HandleCmp(uintptr_t PC, T Arg1, T Arg2)
static const size_t kSize
Definition: FuzzerTracePC.h:32
uintptr_t GetPC(size_t Idx)
__attribute__((weak)) int LLVMFuzzerInitialize(int *argc
uint8_t size() const
static const size_t kNumPCs
Definition: FuzzerTracePC.h:72
MemMemTable< 1024 > MMT
void HandleInit(uint32_t *Start, uint32_t *Stop)
TracePC TPC
T Min(T a, T b)
Definition: FuzzerDefs.h:90
#define P(N)
static const size_t kSize
Definition: FuzzerTracePC.h:50
void Set(const uint8_t *B, uint8_t S)
void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize)
static const unsigned End
size_t GetTotalPCCoverage()
TableOfRecentCompares< Word, 32 > TORCW
ATTRIBUTE_NO_SANITIZE_ALL void Insert(size_t Idx, const T &Arg1, const T &Arg2)
Definition: FuzzerTracePC.h:37
void CollectFeatures(Callback CB) const
void SetUseCounters(bool UC)
Definition: FuzzerTracePC.h:81
auto Handle8bitCounter
void PrintFeatureSet()
void ClearExtraCounters()
size_t GetNumPCs() const
ATTRIBUTE_NO_SANITIZE_ALL __attribute__((noinline)) void TracePC size_t N
#define ATTRIBUTE_NO_SANITIZE_ALL
Definition: FuzzerDefs.h:79
static const size_t kTracePcBits
Definition: FuzzerTracePC.h:74
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static uint8_t Counters[N]
void InitializePrintNewPCs()
#define I(x, y, z)
Definition: MD5.cpp:58
void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee)
TableOfRecentCompares< uint64_t, 32 > TORC8
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, size_t n, bool StopAtZero)
void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop)
void SetUseValueProfile(bool VP)
Definition: FuzzerTracePC.h:82
TableOfRecentCompares< uint32_t, 32 > TORC4
uint8_t * ExtraCountersEnd()
static size_t GetMaxSize()