blob: 4bd5a761133e9d95b67315960516d35d4ffac80f [file] [log] [blame]
Kostya Serebryany22526252015-05-11 21:16:27 +00001//===- FuzzerTraceState.cpp - Trace-based fuzzer mutator ------------------===//
Kostya Serebryany16d03bd2015-03-30 22:09:51 +00002//
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//===----------------------------------------------------------------------===//
Kostya Serebryany22526252015-05-11 21:16:27 +00009// This file implements a mutation algorithm based on instruction traces and
10// on taint analysis feedback from DFSan.
11//
12// Instruction traces are special hooks inserted by the compiler around
13// interesting instructions. Currently supported traces:
14// * __sanitizer_cov_trace_cmp -- inserted before every ICMP instruction,
15// receives the type, size and arguments of ICMP.
16//
17// Every time a traced event is intercepted we analyse the data involved
18// in the event and suggest a mutation for future executions.
19// For example if 4 bytes of data that derive from input bytes {4,5,6,7}
20// are compared with a constant 12345,
21// we try to insert 12345, 12344, 12346 into bytes
22// {4,5,6,7} of the next fuzzed inputs.
23//
24// The fuzzer can work only with the traces, or with both traces and DFSan.
25//
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000026// DataFlowSanitizer (DFSan) is a tool for
27// generalised dynamic data flow (taint) analysis:
28// http://clang.llvm.org/docs/DataFlowSanitizer.html .
29//
Kostya Serebryany22526252015-05-11 21:16:27 +000030// The approach with DFSan-based fuzzing has some similarity to
31// "Taint-based Directed Whitebox Fuzzing"
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000032// by Vijay Ganesh & Tim Leek & Martin Rinard:
33// http://dspace.mit.edu/openaccess-disseminate/1721.1/59320,
34// but it uses a full blown LLVM IR taint analysis and separate instrumentation
35// to analyze all of the "attack points" at once.
36//
Kostya Serebryany22526252015-05-11 21:16:27 +000037// Workflow with DFSan:
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000038// * lib/Fuzzer/Fuzzer*.cpp is compiled w/o any instrumentation.
Kostya Serebryany22526252015-05-11 21:16:27 +000039// * The code under test is compiled with DFSan *and* with instruction traces.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000040// * Every call to HOOK(a,b) is replaced by DFSan with
41// __dfsw_HOOK(a, b, label(a), label(b)) so that __dfsw_HOOK
42// gets all the taint labels for the arguments.
43// * At the Fuzzer startup we assign a unique DFSan label
44// to every byte of the input string (Fuzzer::CurrentUnit) so that for any
45// chunk of data we know which input bytes it has derived from.
46// * The __dfsw_* functions (implemented in this file) record the
47// parameters (i.e. the application data and the corresponding taint labels)
48// in a global state.
Kostya Serebryany22526252015-05-11 21:16:27 +000049// * Fuzzer::ApplyTraceBasedMutation() tries to use the data recorded
50// by __dfsw_* hooks to guide the fuzzing towards new application states.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000051//
Kostya Serebryany22526252015-05-11 21:16:27 +000052// Parts of this code will not function when DFSan is not linked in.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000053// Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer
54// we redeclare the dfsan_* interface functions as weak and check if they
55// are nullptr before calling.
56// If this approach proves to be useful we may add attribute(weak) to the
57// dfsan declarations in dfsan_interface.h
58//
59// This module is in the "proof of concept" stage.
60// It is capable of solving only the simplest puzzles
61// like test/dfsan/DFSanSimpleCmpTest.cpp.
62//===----------------------------------------------------------------------===//
63
Kostya Serebryany22526252015-05-11 21:16:27 +000064/* Example of manual usage (-fsanitize=dataflow is optional):
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000065(
66 cd $LLVM/lib/Fuzzer/
67 clang -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp
Alexey Samsonov21a33812015-05-07 23:33:24 +000068 clang++ -O0 -std=c++11 -fsanitize-coverage=edge,trace-cmp \
Kostya Serebryany3befe942015-05-06 22:47:24 +000069 -fsanitize=dataflow \
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000070 test/dfsan/DFSanSimpleCmpTest.cpp Fuzzer*.o
71 ./a.out
72)
73*/
74
75#include "FuzzerInternal.h"
76#include <sanitizer/dfsan_interface.h>
77
Kostya Serebryanybeb24c32015-05-07 21:02:11 +000078#include <algorithm>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000079#include <cstring>
80#include <iostream>
81#include <unordered_map>
82
83extern "C" {
84__attribute__((weak))
85dfsan_label dfsan_create_label(const char *desc, void *userdata);
86__attribute__((weak))
87void dfsan_set_label(dfsan_label label, void *addr, size_t size);
88__attribute__((weak))
89void dfsan_add_label(dfsan_label label, void *addr, size_t size);
90__attribute__((weak))
91const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label);
Kostya Serebryanya407dde2015-05-07 00:11:33 +000092__attribute__((weak))
93dfsan_label dfsan_read_label(const void *addr, size_t size);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000094} // extern "C"
95
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000096namespace fuzzer {
97
98static bool ReallyHaveDFSan() {
99 return &dfsan_create_label != nullptr;
100}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000101
102// These values are copied from include/llvm/IR/InstrTypes.h.
103// We do not include the LLVM headers here to remain independent.
104// If these values ever change, an assertion in ComputeCmp will fail.
105enum Predicate {
106 ICMP_EQ = 32, ///< equal
107 ICMP_NE = 33, ///< not equal
108 ICMP_UGT = 34, ///< unsigned greater than
109 ICMP_UGE = 35, ///< unsigned greater or equal
110 ICMP_ULT = 36, ///< unsigned less than
111 ICMP_ULE = 37, ///< unsigned less or equal
112 ICMP_SGT = 38, ///< signed greater than
113 ICMP_SGE = 39, ///< signed greater or equal
114 ICMP_SLT = 40, ///< signed less than
115 ICMP_SLE = 41, ///< signed less or equal
116};
117
118template <class U, class S>
119bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) {
120 switch(CmpType) {
121 case ICMP_EQ : return Arg1 == Arg2;
122 case ICMP_NE : return Arg1 != Arg2;
123 case ICMP_UGT: return Arg1 > Arg2;
124 case ICMP_UGE: return Arg1 >= Arg2;
125 case ICMP_ULT: return Arg1 < Arg2;
126 case ICMP_ULE: return Arg1 <= Arg2;
127 case ICMP_SGT: return (S)Arg1 > (S)Arg2;
128 case ICMP_SGE: return (S)Arg1 >= (S)Arg2;
129 case ICMP_SLT: return (S)Arg1 < (S)Arg2;
130 case ICMP_SLE: return (S)Arg1 <= (S)Arg2;
131 default: assert(0 && "unsupported CmpType");
132 }
133 return false;
134}
135
136static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1,
137 uint64_t Arg2) {
138 if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2);
139 if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2);
140 if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2);
141 if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2);
142 assert(0 && "unsupported type size");
143 return true;
144}
145
146// As a simplification we use the range of input bytes instead of a set of input
147// bytes.
148struct LabelRange {
149 uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range.
150
151 LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {}
152
153 static LabelRange Join(LabelRange LR1, LabelRange LR2) {
154 if (LR1.Beg == LR1.End) return LR2;
155 if (LR2.Beg == LR2.End) return LR1;
156 return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)};
157 }
158 LabelRange &Join(LabelRange LR) {
159 return *this = Join(*this, LR);
160 }
161 static LabelRange Singleton(const dfsan_label_info *LI) {
162 uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata;
163 assert(Idx > 0);
164 return {(uint16_t)(Idx - 1), Idx};
165 }
166};
167
168std::ostream &operator<<(std::ostream &os, const LabelRange &LR) {
169 return os << "[" << LR.Beg << "," << LR.End << ")";
170}
171
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000172// For now, very simple: put Size bytes of Data at position Pos.
173struct TraceBasedMutation {
174 size_t Pos;
175 size_t Size;
176 uint64_t Data;
177};
178
Kostya Serebryany22526252015-05-11 21:16:27 +0000179class TraceState {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000180 public:
Kostya Serebryany22526252015-05-11 21:16:27 +0000181 TraceState(const Fuzzer::FuzzingOptions &Options, const Unit &CurrentUnit)
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000182 : Options(Options), CurrentUnit(CurrentUnit) {}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000183
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000184 LabelRange GetLabelRange(dfsan_label L);
185 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
186 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
187 dfsan_label L2);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000188 void TraceCmpCallback(size_t CmpSize, size_t CmpType, uint64_t Arg1,
189 uint64_t Arg2);
190 int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
191 size_t DataSize);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000192
193 void StartTraceRecording() {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000194 if (!Options.UseTraces) return;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000195 RecordingTraces = true;
196 Mutations.clear();
197 }
198
199 size_t StopTraceRecording() {
200 RecordingTraces = false;
201 std::random_shuffle(Mutations.begin(), Mutations.end());
202 return Mutations.size();
203 }
204
205 void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000206
207 private:
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000208 bool IsTwoByteData(uint64_t Data) {
209 int64_t Signed = static_cast<int64_t>(Data);
210 Signed >>= 16;
211 return Signed == 0 || Signed == -1L;
212 }
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000213 bool RecordingTraces = false;
214 std::vector<TraceBasedMutation> Mutations;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000215 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)] = {};
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000216 const Fuzzer::FuzzingOptions &Options;
217 const Unit &CurrentUnit;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000218};
219
Kostya Serebryany22526252015-05-11 21:16:27 +0000220LabelRange TraceState::GetLabelRange(dfsan_label L) {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000221 LabelRange &LR = LabelRanges[L];
222 if (LR.Beg < LR.End || L == 0)
223 return LR;
224 const dfsan_label_info *LI = dfsan_get_label_info(L);
225 if (LI->l1 || LI->l2)
226 return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
227 return LR = LabelRange::Singleton(LI);
228}
229
Kostya Serebryany22526252015-05-11 21:16:27 +0000230void TraceState::ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000231 assert(Idx < Mutations.size());
232 auto &M = Mutations[Idx];
233 if (Options.Verbosity >= 3)
234 std::cerr << "TBM " << M.Pos << " " << M.Size << " " << M.Data << "\n";
235 if (M.Pos + M.Size > U->size()) return;
236 memcpy(U->data() + M.Pos, &M.Data, M.Size);
237}
238
Kostya Serebryany22526252015-05-11 21:16:27 +0000239void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000240 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
241 dfsan_label L2) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000242 assert(ReallyHaveDFSan());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000243 if (!RecordingTraces) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000244 if (L1 == 0 && L2 == 0)
245 return; // Not actionable.
246 if (L1 != 0 && L2 != 0)
247 return; // Probably still actionable.
248 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000249 uint64_t Data = L1 ? Arg2 : Arg1;
250 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000251
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000252 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
253 Mutations.push_back({Pos, CmpSize, Data});
254 Mutations.push_back({Pos, CmpSize, Data + 1});
255 Mutations.push_back({Pos, CmpSize, Data - 1});
256 }
257
258 if (CmpSize > LR.End - LR.Beg)
259 Mutations.push_back({LR.Beg, (unsigned)(LR.End - LR.Beg), Data});
260
261
262 if (Options.Verbosity >= 3)
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000263 std::cerr << "DFSAN:"
264 << " PC " << std::hex << PC << std::dec
265 << " S " << CmpSize
266 << " T " << CmpType
267 << " A1 " << Arg1 << " A2 " << Arg2 << " R " << Res
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000268 << " L" << L1
269 << " L" << L2
270 << " R" << LR
271 << " MU " << Mutations.size()
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000272 << "\n";
273}
274
Kostya Serebryany22526252015-05-11 21:16:27 +0000275int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000276 size_t DataSize) {
277 int Res = 0;
278 const uint8_t *Beg = CurrentUnit.data();
279 const uint8_t *End = Beg + CurrentUnit.size();
280 for (const uint8_t *Cur = Beg; Cur < End; Cur += DataSize) {
281 Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
282 if (!Cur)
283 break;
284 // std::cerr << "Cur " << (void*)Cur << "\n";
285 size_t Pos = Cur - Beg;
286 assert(Pos < CurrentUnit.size());
287 Mutations.push_back({Pos, DataSize, DesiredData});
288 Mutations.push_back({Pos, DataSize, DesiredData + 1});
289 Mutations.push_back({Pos, DataSize, DesiredData - 1});
290 Cur += DataSize;
291 Res++;
292 }
293 return Res;
294}
295
Kostya Serebryany22526252015-05-11 21:16:27 +0000296void TraceState::TraceCmpCallback(size_t CmpSize, size_t CmpType, uint64_t Arg1,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000297 uint64_t Arg2) {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000298 if (!RecordingTraces) return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000299 int Added = 0;
300 if (Options.Verbosity >= 3)
301 std::cerr << "TraceCmp: " << Arg1 << " " << Arg2 << "\n";
302 Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
303 Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
304 if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
305 Added += TryToAddDesiredData(Arg1, Arg2, 2);
306 Added += TryToAddDesiredData(Arg2, Arg1, 2);
307 }
308}
309
Kostya Serebryany22526252015-05-11 21:16:27 +0000310static TraceState *TS;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000311
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000312void Fuzzer::StartTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000313 if (!TS) return;
314 TS->StartTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000315}
316
317size_t Fuzzer::StopTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000318 if (!TS) return 0;
319 return TS->StopTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000320}
321
322void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000323 assert(TS);
324 TS->ApplyTraceBasedMutation(Idx, U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000325}
326
Kostya Serebryany22526252015-05-11 21:16:27 +0000327void Fuzzer::InitializeTraceState() {
328 if (!Options.UseTraces && !Options.UseDFSan) return;
329 TS = new TraceState(Options, CurrentUnit);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000330 CurrentUnit.resize(Options.MaxLen);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000331 // The rest really requires DFSan.
Kostya Serebryany22526252015-05-11 21:16:27 +0000332 if (!ReallyHaveDFSan() || !Options.UseDFSan) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000333 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) {
334 dfsan_label L = dfsan_create_label("input", (void*)(i + 1));
335 // We assume that no one else has called dfsan_create_label before.
336 assert(L == i + 1);
337 dfsan_set_label(L, &CurrentUnit[i], 1);
338 }
339}
340
341} // namespace fuzzer
342
Kostya Serebryany22526252015-05-11 21:16:27 +0000343using fuzzer::TS;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000344
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000345extern "C" {
346void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
347 uint64_t Arg2, dfsan_label L0,
348 dfsan_label L1, dfsan_label L2) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000349 assert(TS);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000350 assert(L0 == 0);
351 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
352 uint64_t CmpSize = (SizeAndType >> 32) / 8;
353 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany22526252015-05-11 21:16:27 +0000354 TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000355}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000356
357void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
358 size_t n, dfsan_label s1_label,
359 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000360 assert(TS);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000361 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000362 uint64_t S1 = 0, S2 = 0;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000363 // Simplification: handle only first 8 bytes.
364 memcpy(&S1, s1, std::min(n, sizeof(S1)));
365 memcpy(&S2, s2, std::min(n, sizeof(S2)));
366 dfsan_label L1 = dfsan_read_label(s1, n);
367 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryany22526252015-05-11 21:16:27 +0000368 TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000369}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000370
371void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
372 uint64_t Arg2) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000373 if (!TS) return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000374 uint64_t CmpSize = (SizeAndType >> 32) / 8;
375 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany22526252015-05-11 21:16:27 +0000376 TS->TraceCmpCallback(CmpSize, Type, Arg1, Arg2);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000377}
378
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000379} // extern "C"