[lib/Fuzzer] minor refactoring/simplification, NFC
[oota-llvm.git] / lib / Fuzzer / FuzzerDFSan.cpp
1 //===- FuzzerDFSan.cpp - DFSan-based fuzzer mutator -----------------------===//
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 // DataFlowSanitizer (DFSan) is a tool for
10 // generalised dynamic data flow (taint) analysis:
11 // http://clang.llvm.org/docs/DataFlowSanitizer.html .
12 //
13 // This file implements a mutation algorithm based on taint
14 // analysis feedback from DFSan.
15 //
16 // The approach has some similarity to "Taint-based Directed Whitebox Fuzzing"
17 // by Vijay Ganesh & Tim Leek & Martin Rinard:
18 // http://dspace.mit.edu/openaccess-disseminate/1721.1/59320,
19 // but it uses a full blown LLVM IR taint analysis and separate instrumentation
20 // to analyze all of the "attack points" at once.
21 //
22 // Workflow:
23 //   * lib/Fuzzer/Fuzzer*.cpp is compiled w/o any instrumentation.
24 //   * The code under test is compiled with DFSan *and* with special extra hooks
25 //     that are inserted before dfsan. Currently supported hooks:
26 //     - __sanitizer_cov_trace_cmp: inserted before every ICMP instruction,
27 //       receives the type, size and arguments of ICMP.
28 //   * Every call to HOOK(a,b) is replaced by DFSan with
29 //     __dfsw_HOOK(a, b, label(a), label(b)) so that __dfsw_HOOK
30 //     gets all the taint labels for the arguments.
31 //   * At the Fuzzer startup we assign a unique DFSan label
32 //     to every byte of the input string (Fuzzer::CurrentUnit) so that for any
33 //     chunk of data we know which input bytes it has derived from.
34 //   * The __dfsw_* functions (implemented in this file) record the
35 //     parameters (i.e. the application data and the corresponding taint labels)
36 //     in a global state.
37 //   * Fuzzer::MutateWithDFSan() tries to use the data recorded by __dfsw_*
38 //     hooks to guide the fuzzing towards new application states.
39 //     For example if 4 bytes of data that derive from input bytes {4,5,6,7}
40 //     are compared with a constant 12345 and the comparison always yields
41 //     the same result, we try to insert 12345, 12344, 12346 into bytes
42 //     {4,5,6,7} of the next fuzzed inputs.
43 //
44 // This code does not function when DFSan is not linked in.
45 // Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer
46 // we redeclare the dfsan_* interface functions as weak and check if they
47 // are nullptr before calling.
48 // If this approach proves to be useful we may add attribute(weak) to the
49 // dfsan declarations in dfsan_interface.h
50 //
51 // This module is in the "proof of concept" stage.
52 // It is capable of solving only the simplest puzzles
53 // like test/dfsan/DFSanSimpleCmpTest.cpp.
54 //===----------------------------------------------------------------------===//
55
56 /* Example of manual usage:
57 (
58   cd $LLVM/lib/Fuzzer/
59   clang  -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp
60   clang++ -O0 -std=c++11 -fsanitize-coverage=3  \
61     -mllvm -sanitizer-coverage-experimental-trace-compares=1 \
62     -fsanitize=dataflow \
63     test/dfsan/DFSanSimpleCmpTest.cpp Fuzzer*.o
64   ./a.out
65 )
66 */
67
68 #include "FuzzerInternal.h"
69 #include <sanitizer/dfsan_interface.h>
70
71 #include <cstring>
72 #include <iostream>
73 #include <unordered_map>
74
75 extern "C" {
76 __attribute__((weak))
77 dfsan_label dfsan_create_label(const char *desc, void *userdata);
78 __attribute__((weak))
79 void dfsan_set_label(dfsan_label label, void *addr, size_t size);
80 __attribute__((weak))
81 void dfsan_add_label(dfsan_label label, void *addr, size_t size);
82 __attribute__((weak))
83 const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label);
84 __attribute__((weak))
85 dfsan_label dfsan_read_label(const void *addr, size_t size);
86 }  // extern "C"
87
88 namespace {
89
90 // These values are copied from include/llvm/IR/InstrTypes.h.
91 // We do not include the LLVM headers here to remain independent.
92 // If these values ever change, an assertion in ComputeCmp will fail.
93 enum Predicate {
94   ICMP_EQ = 32,  ///< equal
95   ICMP_NE = 33,  ///< not equal
96   ICMP_UGT = 34, ///< unsigned greater than
97   ICMP_UGE = 35, ///< unsigned greater or equal
98   ICMP_ULT = 36, ///< unsigned less than
99   ICMP_ULE = 37, ///< unsigned less or equal
100   ICMP_SGT = 38, ///< signed greater than
101   ICMP_SGE = 39, ///< signed greater or equal
102   ICMP_SLT = 40, ///< signed less than
103   ICMP_SLE = 41, ///< signed less or equal
104 };
105
106 template <class U, class S>
107 bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) {
108   switch(CmpType) {
109     case ICMP_EQ : return Arg1 == Arg2;
110     case ICMP_NE : return Arg1 != Arg2;
111     case ICMP_UGT: return Arg1 > Arg2;
112     case ICMP_UGE: return Arg1 >= Arg2;
113     case ICMP_ULT: return Arg1 < Arg2;
114     case ICMP_ULE: return Arg1 <= Arg2;
115     case ICMP_SGT: return (S)Arg1 > (S)Arg2;
116     case ICMP_SGE: return (S)Arg1 >= (S)Arg2;
117     case ICMP_SLT: return (S)Arg1 < (S)Arg2;
118     case ICMP_SLE: return (S)Arg1 <= (S)Arg2;
119     default: assert(0 && "unsupported CmpType");
120   }
121   return false;
122 }
123
124 static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1,
125                        uint64_t Arg2) {
126   if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2);
127   if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2);
128   if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2);
129   if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2);
130   assert(0 && "unsupported type size");
131   return true;
132 }
133
134 // As a simplification we use the range of input bytes instead of a set of input
135 // bytes.
136 struct LabelRange {
137   uint16_t Beg, End;  // Range is [Beg, End), thus Beg==End is an empty range.
138
139   LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {}
140
141   static LabelRange Join(LabelRange LR1, LabelRange LR2) {
142     if (LR1.Beg == LR1.End) return LR2;
143     if (LR2.Beg == LR2.End) return LR1;
144     return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)};
145   }
146   LabelRange &Join(LabelRange LR) {
147     return *this = Join(*this, LR);
148   }
149   static LabelRange Singleton(const dfsan_label_info *LI) {
150     uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata;
151     assert(Idx > 0);
152     return {(uint16_t)(Idx - 1), Idx};
153   }
154 };
155
156 std::ostream &operator<<(std::ostream &os, const LabelRange &LR) {
157   return os << "[" << LR.Beg << "," << LR.End << ")";
158 }
159
160 class DFSanState {
161  public:
162    DFSanState(const fuzzer::Fuzzer::FuzzingOptions &Options)
163        : Options(Options) {}
164
165   struct CmpSiteInfo {
166     size_t ResCounters[2] = {0, 0};
167     size_t CmpSize = 0;
168     LabelRange LR;
169     std::unordered_map<uint64_t, size_t> CountedConstants;
170   };
171
172   LabelRange GetLabelRange(dfsan_label L);
173   void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
174                         uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
175                         dfsan_label L2);
176   bool Mutate(fuzzer::Unit *U);
177
178  private:
179   std::unordered_map<uintptr_t, CmpSiteInfo> PcToCmpSiteInfoMap;
180   LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)] = {};
181   const fuzzer::Fuzzer::FuzzingOptions &Options;
182 };
183
184 LabelRange DFSanState::GetLabelRange(dfsan_label L) {
185   LabelRange &LR = LabelRanges[L];
186   if (LR.Beg < LR.End || L == 0)
187     return LR;
188   const dfsan_label_info *LI = dfsan_get_label_info(L);
189   if (LI->l1 || LI->l2)
190     return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
191   return LR = LabelRange::Singleton(LI);
192 }
193
194 void DFSanState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
195                                   uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
196                                   dfsan_label L2) {
197   if (L1 == 0 && L2 == 0)
198     return;  // Not actionable.
199   if (L1 != 0 && L2 != 0)
200     return;  // Probably still actionable.
201   bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
202   CmpSiteInfo &CSI = PcToCmpSiteInfoMap[PC];
203   CSI.CmpSize = CmpSize;
204   CSI.LR.Join(GetLabelRange(L1)).Join(GetLabelRange(L2));
205   if (!L1) CSI.CountedConstants[Arg1]++;
206   if (!L2) CSI.CountedConstants[Arg2]++;
207   size_t Counter = CSI.ResCounters[Res]++;
208
209   if (Options.Verbosity >= 2  &&
210       (Counter & (Counter - 1)) == 0 &&
211       CSI.ResCounters[!Res] == 0)
212     std::cerr << "DFSAN:"
213               << " PC " << std::hex << PC << std::dec
214               << " S " << CmpSize
215               << " T " << CmpType
216               << " A1 " << Arg1 << " A2 " << Arg2 << " R " << Res
217               << " L" << L1 << GetLabelRange(L1)
218               << " L" << L2 << GetLabelRange(L2)
219               << " LR " << CSI.LR
220               << "\n";
221 }
222
223 bool DFSanState::Mutate(fuzzer::Unit *U) {
224   for (auto &PCToCmp : PcToCmpSiteInfoMap) {
225     auto &CSI = PCToCmp.second;
226     if (CSI.ResCounters[0] * CSI.ResCounters[1] != 0) continue;
227     if (CSI.ResCounters[0] + CSI.ResCounters[1] < 1000) continue;
228     if (CSI.CountedConstants.size() != 1) continue;
229     uintptr_t C = CSI.CountedConstants.begin()->first;
230     if (U->size() >= CSI.CmpSize) {
231       size_t RangeSize = CSI.LR.End - CSI.LR.Beg;
232       size_t Idx = CSI.LR.Beg + rand() % RangeSize;
233       if (Idx + CSI.CmpSize > U->size()) continue;
234       C += rand() % 5 - 2;
235       memcpy(U->data() + Idx, &C, CSI.CmpSize);
236       return true;
237     }
238   }
239   return false;
240 }
241
242 static DFSanState *DFSan;
243
244 }  // namespace
245
246 namespace fuzzer {
247
248 bool Fuzzer::MutateWithDFSan(Unit *U) {
249   if (!&dfsan_create_label || !DFSan) return false;
250   return DFSan->Mutate(U);
251 }
252
253 void Fuzzer::InitializeDFSan() {
254   if (!&dfsan_create_label || !Options.UseDFSan) return;
255   DFSan = new DFSanState(Options);
256   CurrentUnit.resize(Options.MaxLen);
257   for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) {
258     dfsan_label L = dfsan_create_label("input", (void*)(i + 1));
259     // We assume that no one else has called dfsan_create_label before.
260     assert(L == i + 1);
261     dfsan_set_label(L, &CurrentUnit[i], 1);
262   }
263 }
264
265 }  // namespace fuzzer
266
267 extern "C" {
268 void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
269                                       uint64_t Arg2, dfsan_label L0,
270                                       dfsan_label L1, dfsan_label L2) {
271   assert(L0 == 0);
272   uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
273   uint64_t CmpSize = (SizeAndType >> 32) / 8;
274   uint64_t Type = (SizeAndType << 32) >> 32;
275   DFSan->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
276 }
277
278 void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
279                             size_t n, dfsan_label s1_label,
280                             dfsan_label s2_label, dfsan_label n_label) {
281   uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
282   uint64_t S1, S2;
283   // Simplification: handle only first 8 bytes.
284   memcpy(&S1, s1, std::min(n, sizeof(S1)));
285   memcpy(&S2, s2, std::min(n, sizeof(S2)));
286   dfsan_label L1 = dfsan_read_label(s1, n);
287   dfsan_label L2 = dfsan_read_label(s2, n);
288   DFSan->DFSanCmpCallback(PC, n, ICMP_EQ, S1, S2, L1, L2);
289 }
290
291 void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
292                                uint64_t Arg2) {
293   // This symbol will be present if dfsan is disabled on the given function.
294   // FIXME: implement poor man's taint analysis here (w/o dfsan).
295 }
296
297 }  // extern "C"