blob: b7ed66fdfc676de23d1b31632331e34b3417818d [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>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000080#include <unordered_map>
81
82extern "C" {
83__attribute__((weak))
84dfsan_label dfsan_create_label(const char *desc, void *userdata);
85__attribute__((weak))
86void dfsan_set_label(dfsan_label label, void *addr, size_t size);
87__attribute__((weak))
88void dfsan_add_label(dfsan_label label, void *addr, size_t size);
89__attribute__((weak))
90const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label);
Kostya Serebryanya407dde2015-05-07 00:11:33 +000091__attribute__((weak))
92dfsan_label dfsan_read_label(const void *addr, size_t size);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000093} // extern "C"
94
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000095namespace fuzzer {
96
97static bool ReallyHaveDFSan() {
98 return &dfsan_create_label != nullptr;
99}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000100
101// These values are copied from include/llvm/IR/InstrTypes.h.
102// We do not include the LLVM headers here to remain independent.
103// If these values ever change, an assertion in ComputeCmp will fail.
104enum Predicate {
105 ICMP_EQ = 32, ///< equal
106 ICMP_NE = 33, ///< not equal
107 ICMP_UGT = 34, ///< unsigned greater than
108 ICMP_UGE = 35, ///< unsigned greater or equal
109 ICMP_ULT = 36, ///< unsigned less than
110 ICMP_ULE = 37, ///< unsigned less or equal
111 ICMP_SGT = 38, ///< signed greater than
112 ICMP_SGE = 39, ///< signed greater or equal
113 ICMP_SLT = 40, ///< signed less than
114 ICMP_SLE = 41, ///< signed less or equal
115};
116
117template <class U, class S>
118bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) {
119 switch(CmpType) {
120 case ICMP_EQ : return Arg1 == Arg2;
121 case ICMP_NE : return Arg1 != Arg2;
122 case ICMP_UGT: return Arg1 > Arg2;
123 case ICMP_UGE: return Arg1 >= Arg2;
124 case ICMP_ULT: return Arg1 < Arg2;
125 case ICMP_ULE: return Arg1 <= Arg2;
126 case ICMP_SGT: return (S)Arg1 > (S)Arg2;
127 case ICMP_SGE: return (S)Arg1 >= (S)Arg2;
128 case ICMP_SLT: return (S)Arg1 < (S)Arg2;
129 case ICMP_SLE: return (S)Arg1 <= (S)Arg2;
130 default: assert(0 && "unsupported CmpType");
131 }
132 return false;
133}
134
135static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1,
136 uint64_t Arg2) {
137 if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2);
138 if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2);
139 if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2);
140 if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000141 // Other size, ==
142 if (CmpType == ICMP_EQ) return Arg1 == Arg2;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000143 // assert(0 && "unsupported cmp and type size combination");
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000144 return true;
145}
146
147// As a simplification we use the range of input bytes instead of a set of input
148// bytes.
149struct LabelRange {
150 uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range.
151
152 LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {}
153
154 static LabelRange Join(LabelRange LR1, LabelRange LR2) {
155 if (LR1.Beg == LR1.End) return LR2;
156 if (LR2.Beg == LR2.End) return LR1;
157 return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)};
158 }
159 LabelRange &Join(LabelRange LR) {
160 return *this = Join(*this, LR);
161 }
162 static LabelRange Singleton(const dfsan_label_info *LI) {
163 uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata;
164 assert(Idx > 0);
165 return {(uint16_t)(Idx - 1), Idx};
166 }
167};
168
Kostya Serebryany35959592015-07-28 00:59:53 +0000169// A passport for a CMP site. We want to keep track of where the given CMP is
170// and how many times it is evaluated to true or false.
171struct CmpSitePassport {
172 uintptr_t PC;
173 size_t Counter[2];
174
175 bool IsInterestingCmpTarget() {
176 static const size_t kRareEnough = 50;
177 size_t C0 = Counter[0];
178 size_t C1 = Counter[1];
179 return C0 > kRareEnough * (C1 + 1) || C1 > kRareEnough * (C0 + 1);
180 }
181};
182
183// For now, just keep a simple imprecise hash table PC => CmpSitePassport.
184// Potentially, will need to have a compiler support to have a precise mapping
185// and also thread-safety.
186struct CmpSitePassportTable {
187 static const size_t kSize = 99991; // Prime.
188 CmpSitePassport Passports[kSize];
189
190 CmpSitePassport *GetPassport(uintptr_t PC) {
191 uintptr_t Idx = PC & kSize;
192 CmpSitePassport *Res = &Passports[Idx];
193 if (Res->PC == 0) // Not thread safe.
194 Res->PC = PC;
195 return Res->PC == PC ? Res : nullptr;
196 }
197};
198
199static CmpSitePassportTable CSPTable; // Zero initialized.
200
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000201// For now, very simple: put Size bytes of Data at position Pos.
202struct TraceBasedMutation {
203 size_t Pos;
204 size_t Size;
205 uint64_t Data;
206};
207
Kostya Serebryany22526252015-05-11 21:16:27 +0000208class TraceState {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000209 public:
Kostya Serebryany22526252015-05-11 21:16:27 +0000210 TraceState(const Fuzzer::FuzzingOptions &Options, const Unit &CurrentUnit)
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000211 : Options(Options), CurrentUnit(CurrentUnit) {}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000212
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000213 LabelRange GetLabelRange(dfsan_label L);
214 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
215 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
216 dfsan_label L2);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000217 void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val,
218 size_t NumCases, uint64_t *Cases, dfsan_label L);
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000219 void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
220 uint64_t Arg1, uint64_t Arg2);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000221
222 void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val,
223 size_t NumCases, uint64_t *Cases);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000224 int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
225 size_t DataSize);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000226
227 void StartTraceRecording() {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000228 if (!Options.UseTraces) return;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000229 RecordingTraces = true;
230 Mutations.clear();
231 }
232
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000233 size_t StopTraceRecording(FuzzerRandomBase &Rand) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000234 RecordingTraces = false;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000235 return Mutations.size();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000236 }
237
238 void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000239
240 private:
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000241 bool IsTwoByteData(uint64_t Data) {
242 int64_t Signed = static_cast<int64_t>(Data);
243 Signed >>= 16;
244 return Signed == 0 || Signed == -1L;
245 }
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000246 bool RecordingTraces = false;
247 std::vector<TraceBasedMutation> Mutations;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000248 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)] = {};
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000249 const Fuzzer::FuzzingOptions &Options;
250 const Unit &CurrentUnit;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000251};
252
Kostya Serebryany22526252015-05-11 21:16:27 +0000253LabelRange TraceState::GetLabelRange(dfsan_label L) {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000254 LabelRange &LR = LabelRanges[L];
255 if (LR.Beg < LR.End || L == 0)
256 return LR;
257 const dfsan_label_info *LI = dfsan_get_label_info(L);
258 if (LI->l1 || LI->l2)
259 return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
260 return LR = LabelRange::Singleton(LI);
261}
262
Kostya Serebryany22526252015-05-11 21:16:27 +0000263void TraceState::ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000264 assert(Idx < Mutations.size());
265 auto &M = Mutations[Idx];
266 if (Options.Verbosity >= 3)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000267 Printf("TBM %zd %zd %zd\n", M.Pos, M.Size, M.Data);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000268 if (M.Pos + M.Size > U->size()) return;
269 memcpy(U->data() + M.Pos, &M.Data, M.Size);
270}
271
Kostya Serebryany22526252015-05-11 21:16:27 +0000272void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000273 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
274 dfsan_label L2) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000275 assert(ReallyHaveDFSan());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000276 if (!RecordingTraces) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000277 if (L1 == 0 && L2 == 0)
278 return; // Not actionable.
279 if (L1 != 0 && L2 != 0)
280 return; // Probably still actionable.
281 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000282 uint64_t Data = L1 ? Arg2 : Arg1;
283 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000284
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000285 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
286 Mutations.push_back({Pos, CmpSize, Data});
287 Mutations.push_back({Pos, CmpSize, Data + 1});
288 Mutations.push_back({Pos, CmpSize, Data - 1});
289 }
290
291 if (CmpSize > LR.End - LR.Beg)
292 Mutations.push_back({LR.Beg, (unsigned)(LR.End - LR.Beg), Data});
293
294
295 if (Options.Verbosity >= 3)
Kostya Serebryanyae7df1c2015-07-28 01:25:00 +0000296 Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 "
297 "%d MU %zd\n",
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000298 PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, Mutations.size());
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000299}
300
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000301void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits,
302 uint64_t Val, size_t NumCases,
303 uint64_t *Cases, dfsan_label L) {
304 assert(ReallyHaveDFSan());
305 if (!RecordingTraces) return;
306 if (!L) return; // Not actionable.
307 LabelRange LR = GetLabelRange(L);
308 size_t ValSize = ValSizeInBits / 8;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000309 bool TryShort = IsTwoByteData(Val);
310 for (size_t i = 0; i < NumCases; i++)
311 TryShort &= IsTwoByteData(Cases[i]);
312
313 for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++)
314 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000315 Mutations.push_back({Pos, ValSize, Cases[i]});
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000316
317 if (TryShort)
318 for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++)
319 for (size_t i = 0; i < NumCases; i++)
320 Mutations.push_back({Pos, 2, Cases[i]});
321
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000322 if (Options.Verbosity >= 3)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000323 Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} "
324 "TryShort %d\n",
325 PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000326}
327
Kostya Serebryany22526252015-05-11 21:16:27 +0000328int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000329 size_t DataSize) {
330 int Res = 0;
331 const uint8_t *Beg = CurrentUnit.data();
332 const uint8_t *End = Beg + CurrentUnit.size();
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000333 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000334 Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
335 if (!Cur)
336 break;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000337 size_t Pos = Cur - Beg;
338 assert(Pos < CurrentUnit.size());
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000339 if (Mutations.size() > 100000U) return Res; // Just in case.
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000340 Mutations.push_back({Pos, DataSize, DesiredData});
341 Mutations.push_back({Pos, DataSize, DesiredData + 1});
342 Mutations.push_back({Pos, DataSize, DesiredData - 1});
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000343 Res++;
344 }
345 return Res;
346}
347
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000348void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
349 uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000350 if (!RecordingTraces) return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000351 int Added = 0;
Kostya Serebryany35959592015-07-28 00:59:53 +0000352 CmpSitePassport *CSP = CSPTable.GetPassport(PC);
353 if (!CSP) return;
354 CSP->Counter[ComputeCmp(CmpSize, CmpType, Arg1, Arg2)]++;
355 size_t C0 = CSP->Counter[0];
356 size_t C1 = CSP->Counter[1];
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000357 // FIXME: is this a good idea or a bad?
358 // if (!CSP->IsInterestingCmpTarget())
359 // return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000360 if (Options.Verbosity >= 3)
Kostya Serebryany35959592015-07-28 00:59:53 +0000361 Printf("TraceCmp: %p %zd/%zd; %zd %zd\n", CSP->PC, C0, C1, Arg1, Arg2);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000362 Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
363 Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
364 if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
365 Added += TryToAddDesiredData(Arg1, Arg2, 2);
366 Added += TryToAddDesiredData(Arg2, Arg1, 2);
367 }
368}
369
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000370void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits,
371 uint64_t Val, size_t NumCases,
372 uint64_t *Cases) {
Kostya Serebryany73932e52015-07-31 18:09:08 +0000373 if (!RecordingTraces) return;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000374 size_t ValSize = ValSizeInBits / 8;
375 bool TryShort = IsTwoByteData(Val);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000376 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000377 TryShort &= IsTwoByteData(Cases[i]);
378
379 if (Options.Verbosity >= 3)
380 Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases,
381 TryShort);
382
383 for (size_t i = 0; i < NumCases; i++) {
384 TryToAddDesiredData(Val, Cases[i], ValSize);
385 if (TryShort)
386 TryToAddDesiredData(Val, Cases[i], 2);
387 }
388
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000389}
390
Kostya Serebryany22526252015-05-11 21:16:27 +0000391static TraceState *TS;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000392
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000393void Fuzzer::StartTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000394 if (!TS) return;
Kostya Serebryany4cc10d42015-08-05 23:02:57 +0000395 if (ReallyHaveDFSan())
396 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++)
397 dfsan_set_label(i + 1, &CurrentUnit[i], 1);
Kostya Serebryany22526252015-05-11 21:16:27 +0000398 TS->StartTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000399}
400
401size_t Fuzzer::StopTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000402 if (!TS) return 0;
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000403 return TS->StopTraceRecording(USF.GetRand());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000404}
405
406void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000407 assert(TS);
408 TS->ApplyTraceBasedMutation(Idx, U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000409}
410
Kostya Serebryany22526252015-05-11 21:16:27 +0000411void Fuzzer::InitializeTraceState() {
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000412 if (!Options.UseTraces) return;
Kostya Serebryany22526252015-05-11 21:16:27 +0000413 TS = new TraceState(Options, CurrentUnit);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000414 CurrentUnit.resize(Options.MaxLen);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000415 // The rest really requires DFSan.
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000416 if (!ReallyHaveDFSan()) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000417 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) {
418 dfsan_label L = dfsan_create_label("input", (void*)(i + 1));
419 // We assume that no one else has called dfsan_create_label before.
Kostya Serebryanyd46369d2015-08-05 23:44:42 +0000420 if (L != i + 1) {
421 Printf("DFSan labels are not starting from 1, exiting\n");
422 exit(1);
423 }
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000424 }
425}
426
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000427static size_t InternalStrnlen(const char *S, size_t MaxLen) {
428 size_t Len = 0;
429 for (; Len < MaxLen && S[Len]; Len++) {}
430 return Len;
431}
432
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000433} // namespace fuzzer
434
Kostya Serebryany22526252015-05-11 21:16:27 +0000435using fuzzer::TS;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000436
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000437extern "C" {
438void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
439 uint64_t Arg2, dfsan_label L0,
440 dfsan_label L1, dfsan_label L2) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000441 if (!TS) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000442 assert(L0 == 0);
443 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
444 uint64_t CmpSize = (SizeAndType >> 32) / 8;
445 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany22526252015-05-11 21:16:27 +0000446 TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000447}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000448
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000449void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
450 dfsan_label L1, dfsan_label L2) {
451 if (!TS) return;
452 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
453 TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1);
454}
455
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000456void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
457 size_t n, dfsan_label s1_label,
458 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000459 if (!TS) return;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000460 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000461 uint64_t S1 = 0, S2 = 0;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000462 // Simplification: handle only first 8 bytes.
463 memcpy(&S1, s1, std::min(n, sizeof(S1)));
464 memcpy(&S2, s2, std::min(n, sizeof(S2)));
465 dfsan_label L1 = dfsan_read_label(s1, n);
466 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryany22526252015-05-11 21:16:27 +0000467 TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000468}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000469
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000470void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
471 size_t n, dfsan_label s1_label,
472 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000473 if (!TS) return;
474 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
475 uint64_t S1 = 0, S2 = 0;
476 n = std::min(n, fuzzer::InternalStrnlen(s1, n));
477 n = std::min(n, fuzzer::InternalStrnlen(s2, n));
478 // Simplification: handle only first 8 bytes.
479 memcpy(&S1, s1, std::min(n, sizeof(S1)));
480 memcpy(&S2, s2, std::min(n, sizeof(S2)));
481 dfsan_label L1 = dfsan_read_label(s1, n);
482 dfsan_label L2 = dfsan_read_label(s2, n);
483 TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000484}
485
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000486void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2,
487 dfsan_label s1_label, dfsan_label s2_label) {
488 if (!TS) return;
489 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
490 uint64_t S1 = 0, S2 = 0;
491 size_t Len1 = strlen(s1);
492 size_t Len2 = strlen(s2);
493 size_t N = std::min(Len1, Len2);
494 if (N <= 1) return; // Not interesting.
495 // Simplification: handle only first 8 bytes.
496 memcpy(&S1, s1, std::min(N, sizeof(S1)));
497 memcpy(&S2, s2, std::min(N, sizeof(S2)));
498 dfsan_label L1 = dfsan_read_label(s1, Len1);
499 dfsan_label L2 = dfsan_read_label(s2, Len2);
500 TS->DFSanCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2, L1, L2);
501}
502
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000503void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
504 const void *s2, size_t n) {
505 if (!TS) return;
506 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
507 uint64_t S1 = 0, S2 = 0;
508 // Simplification: handle only first 8 bytes.
509 memcpy(&S1, s1, std::min(n, sizeof(S1)));
510 memcpy(&S2, s2, std::min(n, sizeof(S2)));
511 TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000512}
513
514void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
515 const char *s2, size_t n) {
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000516 if (!TS) return;
517 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
518 uint64_t S1 = 0, S2 = 0;
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000519 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
520 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
521 n = std::min(n, Len1);
522 n = std::min(n, Len2);
523 if (n <= 1) return; // Not interesting.
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000524 // Simplification: handle only first 8 bytes.
525 memcpy(&S1, s1, std::min(n, sizeof(S1)));
526 memcpy(&S2, s2, std::min(n, sizeof(S2)));
527 TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2);
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000528}
529
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000530void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
531 const char *s2) {
532 if (!TS) return;
533 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
534 uint64_t S1 = 0, S2 = 0;
535 size_t Len1 = strlen(s1);
536 size_t Len2 = strlen(s2);
537 size_t N = std::min(Len1, Len2);
538 if (N <= 1) return; // Not interesting.
539 // Simplification: handle only first 8 bytes.
540 memcpy(&S1, s1, std::min(N, sizeof(S1)));
541 memcpy(&S2, s2, std::min(N, sizeof(S2)));
542 TS->TraceCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2);
543}
544
545
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000546void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
547 uint64_t Arg2) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000548 if (!TS) return;
Kostya Serebryany35959592015-07-28 00:59:53 +0000549 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000550 uint64_t CmpSize = (SizeAndType >> 32) / 8;
551 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany35959592015-07-28 00:59:53 +0000552 TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000553}
554
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000555void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
556 if (!TS) return;
557 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
558 TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2);
559}
560
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000561} // extern "C"