blob: 1dc93df1f358cd30e5927727de0a08aab107e39e [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 Serebryany65f50862015-09-10 18:48:38 +000070 test/SimpleCmpTest.cpp Fuzzer*.o
71 ./a.out -use_traces=1
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000072)
73*/
74
Kostya Serebryany65f50862015-09-10 18:48:38 +000075#include "FuzzerDFSan.h"
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000076#include "FuzzerInternal.h"
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000077
Kostya Serebryanybeb24c32015-05-07 21:02:11 +000078#include <algorithm>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000079#include <cstring>
Kostya Serebryany226b7342016-01-06 00:03:35 +000080#include <thread>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000081#include <unordered_map>
82
Kostya Serebryany65f50862015-09-10 18:48:38 +000083#if !LLVM_FUZZER_SUPPORTS_DFSAN
84// Stubs for dfsan for platforms where dfsan does not exist and weak
85// functions don't work.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000086extern "C" {
Kostya Serebryany65f50862015-09-10 18:48:38 +000087dfsan_label dfsan_create_label(const char *desc, void *userdata) { return 0; }
88void dfsan_set_label(dfsan_label label, void *addr, size_t size) {}
89void dfsan_add_label(dfsan_label label, void *addr, size_t size) {}
90const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) {
91 return nullptr;
92}
93dfsan_label dfsan_read_label(const void *addr, size_t size) { return 0; }
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000094} // extern "C"
Kostya Serebryany65f50862015-09-10 18:48:38 +000095#endif // !LLVM_FUZZER_SUPPORTS_DFSAN
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000096
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000097namespace fuzzer {
98
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000099// These values are copied from include/llvm/IR/InstrTypes.h.
100// We do not include the LLVM headers here to remain independent.
101// If these values ever change, an assertion in ComputeCmp will fail.
102enum Predicate {
103 ICMP_EQ = 32, ///< equal
104 ICMP_NE = 33, ///< not equal
105 ICMP_UGT = 34, ///< unsigned greater than
106 ICMP_UGE = 35, ///< unsigned greater or equal
107 ICMP_ULT = 36, ///< unsigned less than
108 ICMP_ULE = 37, ///< unsigned less or equal
109 ICMP_SGT = 38, ///< signed greater than
110 ICMP_SGE = 39, ///< signed greater or equal
111 ICMP_SLT = 40, ///< signed less than
112 ICMP_SLE = 41, ///< signed less or equal
113};
114
115template <class U, class S>
116bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) {
117 switch(CmpType) {
118 case ICMP_EQ : return Arg1 == Arg2;
119 case ICMP_NE : return Arg1 != Arg2;
120 case ICMP_UGT: return Arg1 > Arg2;
121 case ICMP_UGE: return Arg1 >= Arg2;
122 case ICMP_ULT: return Arg1 < Arg2;
123 case ICMP_ULE: return Arg1 <= Arg2;
124 case ICMP_SGT: return (S)Arg1 > (S)Arg2;
125 case ICMP_SGE: return (S)Arg1 >= (S)Arg2;
126 case ICMP_SLT: return (S)Arg1 < (S)Arg2;
127 case ICMP_SLE: return (S)Arg1 <= (S)Arg2;
128 default: assert(0 && "unsupported CmpType");
129 }
130 return false;
131}
132
133static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1,
134 uint64_t Arg2) {
135 if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2);
136 if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2);
137 if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2);
138 if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000139 // Other size, ==
140 if (CmpType == ICMP_EQ) return Arg1 == Arg2;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000141 // assert(0 && "unsupported cmp and type size combination");
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000142 return true;
143}
144
145// As a simplification we use the range of input bytes instead of a set of input
146// bytes.
147struct LabelRange {
148 uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range.
149
150 LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {}
151
152 static LabelRange Join(LabelRange LR1, LabelRange LR2) {
153 if (LR1.Beg == LR1.End) return LR2;
154 if (LR2.Beg == LR2.End) return LR1;
155 return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)};
156 }
157 LabelRange &Join(LabelRange LR) {
158 return *this = Join(*this, LR);
159 }
160 static LabelRange Singleton(const dfsan_label_info *LI) {
161 uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata;
162 assert(Idx > 0);
163 return {(uint16_t)(Idx - 1), Idx};
164 }
165};
166
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000167// For now, very simple: put Size bytes of Data at position Pos.
168struct TraceBasedMutation {
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000169 static const size_t kMaxSize = 28;
170 uint32_t Pos : 24;
171 uint32_t Size : 8;
172 uint8_t Data[kMaxSize];
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000173};
174
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000175const size_t TraceBasedMutation::kMaxSize;
176
Kostya Serebryany22526252015-05-11 21:16:27 +0000177class TraceState {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000178 public:
Kostya Serebryany226b7342016-01-06 00:03:35 +0000179 TraceState(const Fuzzer::FuzzingOptions &Options, const Unit &CurrentUnit)
180 : Options(Options), CurrentUnit(CurrentUnit) {
181 // Current trace collection is not thread-friendly and it probably
182 // does not have to be such, but at least we should not crash in presence
183 // of threads. So, just ignore all traces coming from all threads but one.
184 IsMyThread = true;
185 }
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000186
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000187 LabelRange GetLabelRange(dfsan_label L);
188 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
189 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
190 dfsan_label L2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000191 void DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
192 const uint8_t *Data2, dfsan_label L1,
193 dfsan_label L2);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000194 void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val,
195 size_t NumCases, uint64_t *Cases, dfsan_label L);
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000196 void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
197 uint64_t Arg1, uint64_t Arg2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000198 void TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
199 const uint8_t *Data2);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000200
201 void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val,
202 size_t NumCases, uint64_t *Cases);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000203 int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
204 size_t DataSize);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000205 int TryToAddDesiredData(const uint8_t *PresentData,
206 const uint8_t *DesiredData, size_t DataSize);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000207
208 void StartTraceRecording() {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000209 if (!Options.UseTraces) return;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000210 RecordingTraces = true;
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000211 NumMutations = 0;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000212 }
213
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000214 size_t StopTraceRecording(FuzzerRandomBase &Rand) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000215 RecordingTraces = false;
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000216 return NumMutations;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000217 }
218
219 void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000220
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000221 void AddMutation(uint32_t Pos, uint32_t Size, const uint8_t *Data) {
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000222 if (NumMutations >= kMaxMutations) return;
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000223 assert(Size <= TraceBasedMutation::kMaxSize);
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000224 auto &M = Mutations[NumMutations++];
225 M.Pos = Pos;
226 M.Size = Size;
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000227 memcpy(M.Data, Data, Size);
228 }
229
230 void AddMutation(uint32_t Pos, uint32_t Size, uint64_t Data) {
231 assert(Size <= sizeof(Data));
232 AddMutation(Pos, Size, reinterpret_cast<uint8_t*>(&Data));
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000233 }
234
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000235 private:
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000236 bool IsTwoByteData(uint64_t Data) {
237 int64_t Signed = static_cast<int64_t>(Data);
238 Signed >>= 16;
239 return Signed == 0 || Signed == -1L;
240 }
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000241 bool RecordingTraces = false;
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000242 static const size_t kMaxMutations = 1 << 16;
243 size_t NumMutations;
244 TraceBasedMutation Mutations[kMaxMutations];
Kostya Serebryany4d623222015-11-18 01:08:30 +0000245 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)];
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000246 const Fuzzer::FuzzingOptions &Options;
247 const Unit &CurrentUnit;
Kostya Serebryany226b7342016-01-06 00:03:35 +0000248 static thread_local bool IsMyThread;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000249};
250
Kostya Serebryany226b7342016-01-06 00:03:35 +0000251thread_local bool TraceState::IsMyThread;
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 Serebryanye7583d22016-01-09 00:38:40 +0000264 assert(Idx < NumMutations);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000265 auto &M = Mutations[Idx];
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000266 if (Options.Verbosity >= 3) {
267 Printf("TBM Pos %u Size %u ", M.Pos, M.Size);
268 for (uint32_t i = 0; i < M.Size; i++)
269 Printf("%02x", M.Data[i]);
270 Printf("\n");
271 }
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000272 if (M.Pos + M.Size > U->size()) return;
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000273 memcpy(U->data() + M.Pos, &M.Data[0], M.Size);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000274}
275
Kostya Serebryany22526252015-05-11 21:16:27 +0000276void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000277 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
278 dfsan_label L2) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000279 assert(ReallyHaveDFSan());
Kostya Serebryany226b7342016-01-06 00:03:35 +0000280 if (!RecordingTraces || !IsMyThread) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000281 if (L1 == 0 && L2 == 0)
282 return; // Not actionable.
283 if (L1 != 0 && L2 != 0)
284 return; // Probably still actionable.
285 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000286 uint64_t Data = L1 ? Arg2 : Arg1;
287 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000288
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000289 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000290 AddMutation(Pos, CmpSize, Data);
291 AddMutation(Pos, CmpSize, Data + 1);
292 AddMutation(Pos, CmpSize, Data - 1);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000293 }
294
295 if (CmpSize > LR.End - LR.Beg)
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000296 AddMutation(LR.Beg, (unsigned)(LR.End - LR.Beg), Data);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000297
298
299 if (Options.Verbosity >= 3)
Kostya Serebryanyae7df1c2015-07-28 01:25:00 +0000300 Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 "
301 "%d MU %zd\n",
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000302 PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, NumMutations);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000303}
304
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000305void TraceState::DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
306 const uint8_t *Data2, dfsan_label L1,
307 dfsan_label L2) {
308
309 assert(ReallyHaveDFSan());
310 if (!RecordingTraces || !IsMyThread) return;
311 if (L1 == 0 && L2 == 0)
312 return; // Not actionable.
313 if (L1 != 0 && L2 != 0)
314 return; // Probably still actionable.
315
316 const uint8_t *Data = L1 ? Data2 : Data1;
317 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
318 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
319 AddMutation(Pos, CmpSize, Data);
320 if (Options.Verbosity >= 3)
321 Printf("DFSanMemcmpCallback: Pos %d Size %d\n", Pos, CmpSize);
322 }
323}
324
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000325void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits,
326 uint64_t Val, size_t NumCases,
327 uint64_t *Cases, dfsan_label L) {
328 assert(ReallyHaveDFSan());
Kostya Serebryany226b7342016-01-06 00:03:35 +0000329 if (!RecordingTraces || !IsMyThread) return;
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000330 if (!L) return; // Not actionable.
331 LabelRange LR = GetLabelRange(L);
332 size_t ValSize = ValSizeInBits / 8;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000333 bool TryShort = IsTwoByteData(Val);
334 for (size_t i = 0; i < NumCases; i++)
335 TryShort &= IsTwoByteData(Cases[i]);
336
337 for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++)
338 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000339 AddMutation(Pos, ValSize, Cases[i]);
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000340
341 if (TryShort)
342 for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++)
343 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000344 AddMutation(Pos, 2, Cases[i]);
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000345
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000346 if (Options.Verbosity >= 3)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000347 Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} "
348 "TryShort %d\n",
349 PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000350}
351
Kostya Serebryany22526252015-05-11 21:16:27 +0000352int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000353 size_t DataSize) {
354 int Res = 0;
355 const uint8_t *Beg = CurrentUnit.data();
356 const uint8_t *End = Beg + CurrentUnit.size();
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000357 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000358 Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
359 if (!Cur)
360 break;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000361 size_t Pos = Cur - Beg;
362 assert(Pos < CurrentUnit.size());
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000363 AddMutation(Pos, DataSize, DesiredData);
364 AddMutation(Pos, DataSize, DesiredData + 1);
365 AddMutation(Pos, DataSize, DesiredData - 1);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000366 Res++;
367 }
368 return Res;
369}
370
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000371int TraceState::TryToAddDesiredData(const uint8_t *PresentData,
372 const uint8_t *DesiredData,
373 size_t DataSize) {
374 int Res = 0;
375 const uint8_t *Beg = CurrentUnit.data();
376 const uint8_t *End = Beg + CurrentUnit.size();
377 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
378 Cur = (uint8_t *)memmem(Cur, End - Cur, PresentData, DataSize);
379 if (!Cur)
380 break;
381 size_t Pos = Cur - Beg;
382 assert(Pos < CurrentUnit.size());
383 AddMutation(Pos, DataSize, DesiredData);
384 Res++;
385 }
386 return Res;
387}
388
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000389void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
390 uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany226b7342016-01-06 00:03:35 +0000391 if (!RecordingTraces || !IsMyThread) return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000392 int Added = 0;
393 if (Options.Verbosity >= 3)
Kostya Serebryany4b82de22015-09-08 20:40:10 +0000394 Printf("TraceCmp %zd/%zd: %p %zd %zd\n", CmpSize, CmpType, PC, Arg1, Arg2);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000395 Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
396 Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
397 if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
398 Added += TryToAddDesiredData(Arg1, Arg2, 2);
399 Added += TryToAddDesiredData(Arg2, Arg1, 2);
400 }
401}
402
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000403void TraceState::TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
404 const uint8_t *Data2) {
405 if (!RecordingTraces || !IsMyThread) return;
406 CmpSize = std::min(CmpSize, TraceBasedMutation::kMaxSize);
407 TryToAddDesiredData(Data1, Data2, CmpSize);
408 TryToAddDesiredData(Data2, Data1, CmpSize);
409}
410
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000411void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits,
412 uint64_t Val, size_t NumCases,
413 uint64_t *Cases) {
Kostya Serebryany226b7342016-01-06 00:03:35 +0000414 if (!RecordingTraces || !IsMyThread) return;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000415 size_t ValSize = ValSizeInBits / 8;
416 bool TryShort = IsTwoByteData(Val);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000417 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000418 TryShort &= IsTwoByteData(Cases[i]);
419
420 if (Options.Verbosity >= 3)
421 Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases,
422 TryShort);
423
424 for (size_t i = 0; i < NumCases; i++) {
425 TryToAddDesiredData(Val, Cases[i], ValSize);
426 if (TryShort)
427 TryToAddDesiredData(Val, Cases[i], 2);
428 }
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000429}
430
Kostya Serebryany22526252015-05-11 21:16:27 +0000431static TraceState *TS;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000432
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000433void Fuzzer::StartTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000434 if (!TS) return;
Kostya Serebryany4cc10d42015-08-05 23:02:57 +0000435 if (ReallyHaveDFSan())
436 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++)
437 dfsan_set_label(i + 1, &CurrentUnit[i], 1);
Kostya Serebryany22526252015-05-11 21:16:27 +0000438 TS->StartTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000439}
440
441size_t Fuzzer::StopTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000442 if (!TS) return 0;
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000443 return TS->StopTraceRecording(USF.GetRand());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000444}
445
446void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000447 assert(TS);
448 TS->ApplyTraceBasedMutation(Idx, U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000449}
450
Kostya Serebryany22526252015-05-11 21:16:27 +0000451void Fuzzer::InitializeTraceState() {
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000452 if (!Options.UseTraces) return;
Kostya Serebryany22526252015-05-11 21:16:27 +0000453 TS = new TraceState(Options, CurrentUnit);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000454 CurrentUnit.resize(Options.MaxLen);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000455 // The rest really requires DFSan.
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000456 if (!ReallyHaveDFSan()) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000457 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) {
458 dfsan_label L = dfsan_create_label("input", (void*)(i + 1));
459 // We assume that no one else has called dfsan_create_label before.
Kostya Serebryanyd46369d2015-08-05 23:44:42 +0000460 if (L != i + 1) {
461 Printf("DFSan labels are not starting from 1, exiting\n");
462 exit(1);
463 }
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000464 }
465}
466
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000467static size_t InternalStrnlen(const char *S, size_t MaxLen) {
468 size_t Len = 0;
469 for (; Len < MaxLen && S[Len]; Len++) {}
470 return Len;
471}
472
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000473} // namespace fuzzer
474
Kostya Serebryany22526252015-05-11 21:16:27 +0000475using fuzzer::TS;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000476
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000477extern "C" {
478void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
479 uint64_t Arg2, dfsan_label L0,
480 dfsan_label L1, dfsan_label L2) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000481 if (!TS) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000482 assert(L0 == 0);
483 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
484 uint64_t CmpSize = (SizeAndType >> 32) / 8;
485 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany22526252015-05-11 21:16:27 +0000486 TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000487}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000488
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000489void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
490 dfsan_label L1, dfsan_label L2) {
491 if (!TS) return;
492 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
493 TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1);
494}
495
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000496void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
497 size_t n, dfsan_label s1_label,
498 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000499 if (!TS) return;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000500 dfsan_label L1 = dfsan_read_label(s1, n);
501 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000502 TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
503 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000504}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000505
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000506void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
507 size_t n, dfsan_label s1_label,
508 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000509 if (!TS) return;
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000510 n = std::min(n, fuzzer::InternalStrnlen(s1, n));
511 n = std::min(n, fuzzer::InternalStrnlen(s2, n));
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000512 dfsan_label L1 = dfsan_read_label(s1, n);
513 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000514 TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
515 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000516}
517
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000518void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2,
519 dfsan_label s1_label, dfsan_label s2_label) {
520 if (!TS) return;
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000521 size_t Len1 = strlen(s1);
522 size_t Len2 = strlen(s2);
523 size_t N = std::min(Len1, Len2);
524 if (N <= 1) return; // Not interesting.
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000525 dfsan_label L1 = dfsan_read_label(s1, Len1);
526 dfsan_label L2 = dfsan_read_label(s2, Len2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000527 TS->DFSanMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
528 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000529}
530
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000531void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
532 const void *s2, size_t n) {
533 if (!TS) return;
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000534 if (n <= 1) return; // Not interesting.
535 TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
536 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000537}
538
539void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
540 const char *s2, size_t n) {
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000541 if (!TS) return;
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000542 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
543 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
544 n = std::min(n, Len1);
545 n = std::min(n, Len2);
546 if (n <= 1) return; // Not interesting.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000547 TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
548 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000549}
550
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000551void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
552 const char *s2) {
553 if (!TS) return;
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000554 size_t Len1 = strlen(s1);
555 size_t Len2 = strlen(s2);
556 size_t N = std::min(Len1, Len2);
557 if (N <= 1) return; // Not interesting.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000558 TS->TraceMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
559 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000560}
561
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000562__attribute__((visibility("default")))
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000563void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
564 uint64_t Arg2) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000565 if (!TS) return;
Kostya Serebryany35959592015-07-28 00:59:53 +0000566 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000567 uint64_t CmpSize = (SizeAndType >> 32) / 8;
568 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany35959592015-07-28 00:59:53 +0000569 TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000570}
571
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000572__attribute__((visibility("default")))
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000573void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
574 if (!TS) return;
575 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
576 TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2);
577}
578
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000579} // extern "C"