blob: 8204a2ddc7c8f2a117ec616b416e4eb645fea19f [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 Serebryany16d03bd2015-03-30 22:09:51 +000080#include <unordered_map>
81
Kostya Serebryany65f50862015-09-10 18:48:38 +000082#if !LLVM_FUZZER_SUPPORTS_DFSAN
83// Stubs for dfsan for platforms where dfsan does not exist and weak
84// functions don't work.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000085extern "C" {
Kostya Serebryany65f50862015-09-10 18:48:38 +000086dfsan_label dfsan_create_label(const char *desc, void *userdata) { return 0; }
87void dfsan_set_label(dfsan_label label, void *addr, size_t size) {}
88void dfsan_add_label(dfsan_label label, void *addr, size_t size) {}
89const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) {
90 return nullptr;
91}
92dfsan_label dfsan_read_label(const void *addr, size_t size) { return 0; }
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000093} // extern "C"
Kostya Serebryany65f50862015-09-10 18:48:38 +000094#endif // !LLVM_FUZZER_SUPPORTS_DFSAN
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000095
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000096namespace fuzzer {
97
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000098// These values are copied from include/llvm/IR/InstrTypes.h.
99// We do not include the LLVM headers here to remain independent.
100// If these values ever change, an assertion in ComputeCmp will fail.
101enum Predicate {
102 ICMP_EQ = 32, ///< equal
103 ICMP_NE = 33, ///< not equal
104 ICMP_UGT = 34, ///< unsigned greater than
105 ICMP_UGE = 35, ///< unsigned greater or equal
106 ICMP_ULT = 36, ///< unsigned less than
107 ICMP_ULE = 37, ///< unsigned less or equal
108 ICMP_SGT = 38, ///< signed greater than
109 ICMP_SGE = 39, ///< signed greater or equal
110 ICMP_SLT = 40, ///< signed less than
111 ICMP_SLE = 41, ///< signed less or equal
112};
113
114template <class U, class S>
115bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) {
116 switch(CmpType) {
117 case ICMP_EQ : return Arg1 == Arg2;
118 case ICMP_NE : return Arg1 != Arg2;
119 case ICMP_UGT: return Arg1 > Arg2;
120 case ICMP_UGE: return Arg1 >= Arg2;
121 case ICMP_ULT: return Arg1 < Arg2;
122 case ICMP_ULE: return Arg1 <= Arg2;
123 case ICMP_SGT: return (S)Arg1 > (S)Arg2;
124 case ICMP_SGE: return (S)Arg1 >= (S)Arg2;
125 case ICMP_SLT: return (S)Arg1 < (S)Arg2;
126 case ICMP_SLE: return (S)Arg1 <= (S)Arg2;
127 default: assert(0 && "unsupported CmpType");
128 }
129 return false;
130}
131
132static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1,
133 uint64_t Arg2) {
134 if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2);
135 if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2);
136 if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2);
137 if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000138 // Other size, ==
139 if (CmpType == ICMP_EQ) return Arg1 == Arg2;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000140 // assert(0 && "unsupported cmp and type size combination");
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000141 return true;
142}
143
144// As a simplification we use the range of input bytes instead of a set of input
145// bytes.
146struct LabelRange {
147 uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range.
148
149 LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {}
150
151 static LabelRange Join(LabelRange LR1, LabelRange LR2) {
152 if (LR1.Beg == LR1.End) return LR2;
153 if (LR2.Beg == LR2.End) return LR1;
154 return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)};
155 }
156 LabelRange &Join(LabelRange LR) {
157 return *this = Join(*this, LR);
158 }
159 static LabelRange Singleton(const dfsan_label_info *LI) {
160 uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata;
161 assert(Idx > 0);
162 return {(uint16_t)(Idx - 1), Idx};
163 }
164};
165
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000166// For now, very simple: put Size bytes of Data at position Pos.
167struct TraceBasedMutation {
168 size_t Pos;
169 size_t Size;
170 uint64_t Data;
171};
172
Kostya Serebryany22526252015-05-11 21:16:27 +0000173class TraceState {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000174 public:
Kostya Serebryany22526252015-05-11 21:16:27 +0000175 TraceState(const Fuzzer::FuzzingOptions &Options, const Unit &CurrentUnit)
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000176 : Options(Options), CurrentUnit(CurrentUnit) {}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000177
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000178 LabelRange GetLabelRange(dfsan_label L);
179 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
180 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
181 dfsan_label L2);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000182 void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val,
183 size_t NumCases, uint64_t *Cases, dfsan_label L);
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000184 void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
185 uint64_t Arg1, uint64_t Arg2);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000186
187 void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val,
188 size_t NumCases, uint64_t *Cases);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000189 int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
190 size_t DataSize);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000191
192 void StartTraceRecording() {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000193 if (!Options.UseTraces) return;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000194 RecordingTraces = true;
195 Mutations.clear();
196 }
197
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000198 size_t StopTraceRecording(FuzzerRandomBase &Rand) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000199 RecordingTraces = false;
Kostya Serebryany12c78372015-08-12 01:55:37 +0000200 return Mutations.size();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000201 }
202
203 void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000204
205 private:
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000206 bool IsTwoByteData(uint64_t Data) {
207 int64_t Signed = static_cast<int64_t>(Data);
208 Signed >>= 16;
209 return Signed == 0 || Signed == -1L;
210 }
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000211 bool RecordingTraces = false;
212 std::vector<TraceBasedMutation> Mutations;
Kostya Serebryany4d623222015-11-18 01:08:30 +0000213 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)];
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000214 const Fuzzer::FuzzingOptions &Options;
215 const Unit &CurrentUnit;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000216};
217
Kostya Serebryany22526252015-05-11 21:16:27 +0000218LabelRange TraceState::GetLabelRange(dfsan_label L) {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000219 LabelRange &LR = LabelRanges[L];
220 if (LR.Beg < LR.End || L == 0)
221 return LR;
222 const dfsan_label_info *LI = dfsan_get_label_info(L);
223 if (LI->l1 || LI->l2)
224 return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
225 return LR = LabelRange::Singleton(LI);
226}
227
Kostya Serebryany22526252015-05-11 21:16:27 +0000228void TraceState::ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000229 assert(Idx < Mutations.size());
230 auto &M = Mutations[Idx];
231 if (Options.Verbosity >= 3)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000232 Printf("TBM %zd %zd %zd\n", M.Pos, M.Size, M.Data);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000233 if (M.Pos + M.Size > U->size()) return;
234 memcpy(U->data() + M.Pos, &M.Data, M.Size);
235}
236
Kostya Serebryany22526252015-05-11 21:16:27 +0000237void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000238 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
239 dfsan_label L2) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000240 assert(ReallyHaveDFSan());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000241 if (!RecordingTraces) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000242 if (L1 == 0 && L2 == 0)
243 return; // Not actionable.
244 if (L1 != 0 && L2 != 0)
245 return; // Probably still actionable.
246 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000247 uint64_t Data = L1 ? Arg2 : Arg1;
248 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000249
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000250 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
251 Mutations.push_back({Pos, CmpSize, Data});
252 Mutations.push_back({Pos, CmpSize, Data + 1});
253 Mutations.push_back({Pos, CmpSize, Data - 1});
254 }
255
256 if (CmpSize > LR.End - LR.Beg)
257 Mutations.push_back({LR.Beg, (unsigned)(LR.End - LR.Beg), Data});
258
259
260 if (Options.Verbosity >= 3)
Kostya Serebryanyae7df1c2015-07-28 01:25:00 +0000261 Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 "
262 "%d MU %zd\n",
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000263 PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, Mutations.size());
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000264}
265
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000266void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits,
267 uint64_t Val, size_t NumCases,
268 uint64_t *Cases, dfsan_label L) {
269 assert(ReallyHaveDFSan());
270 if (!RecordingTraces) return;
271 if (!L) return; // Not actionable.
272 LabelRange LR = GetLabelRange(L);
273 size_t ValSize = ValSizeInBits / 8;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000274 bool TryShort = IsTwoByteData(Val);
275 for (size_t i = 0; i < NumCases; i++)
276 TryShort &= IsTwoByteData(Cases[i]);
277
278 for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++)
279 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000280 Mutations.push_back({Pos, ValSize, Cases[i]});
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000281
282 if (TryShort)
283 for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++)
284 for (size_t i = 0; i < NumCases; i++)
285 Mutations.push_back({Pos, 2, Cases[i]});
286
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000287 if (Options.Verbosity >= 3)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000288 Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} "
289 "TryShort %d\n",
290 PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000291}
292
Kostya Serebryany22526252015-05-11 21:16:27 +0000293int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000294 size_t DataSize) {
295 int Res = 0;
296 const uint8_t *Beg = CurrentUnit.data();
297 const uint8_t *End = Beg + CurrentUnit.size();
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000298 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000299 Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
300 if (!Cur)
301 break;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000302 size_t Pos = Cur - Beg;
303 assert(Pos < CurrentUnit.size());
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000304 if (Mutations.size() > 100000U) return Res; // Just in case.
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000305 Mutations.push_back({Pos, DataSize, DesiredData});
306 Mutations.push_back({Pos, DataSize, DesiredData + 1});
307 Mutations.push_back({Pos, DataSize, DesiredData - 1});
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000308 Res++;
309 }
310 return Res;
311}
312
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000313void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
314 uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000315 if (!RecordingTraces) return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000316 int Added = 0;
317 if (Options.Verbosity >= 3)
Kostya Serebryany4b82de22015-09-08 20:40:10 +0000318 Printf("TraceCmp %zd/%zd: %p %zd %zd\n", CmpSize, CmpType, PC, Arg1, Arg2);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000319 Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
320 Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
321 if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
322 Added += TryToAddDesiredData(Arg1, Arg2, 2);
323 Added += TryToAddDesiredData(Arg2, Arg1, 2);
324 }
325}
326
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000327void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits,
328 uint64_t Val, size_t NumCases,
329 uint64_t *Cases) {
Kostya Serebryany73932e52015-07-31 18:09:08 +0000330 if (!RecordingTraces) return;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000331 size_t ValSize = ValSizeInBits / 8;
332 bool TryShort = IsTwoByteData(Val);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000333 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000334 TryShort &= IsTwoByteData(Cases[i]);
335
336 if (Options.Verbosity >= 3)
337 Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases,
338 TryShort);
339
340 for (size_t i = 0; i < NumCases; i++) {
341 TryToAddDesiredData(Val, Cases[i], ValSize);
342 if (TryShort)
343 TryToAddDesiredData(Val, Cases[i], 2);
344 }
345
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000346}
347
Kostya Serebryany22526252015-05-11 21:16:27 +0000348static TraceState *TS;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000349
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000350void Fuzzer::StartTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000351 if (!TS) return;
Kostya Serebryany4cc10d42015-08-05 23:02:57 +0000352 if (ReallyHaveDFSan())
353 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++)
354 dfsan_set_label(i + 1, &CurrentUnit[i], 1);
Kostya Serebryany22526252015-05-11 21:16:27 +0000355 TS->StartTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000356}
357
358size_t Fuzzer::StopTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000359 if (!TS) return 0;
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000360 return TS->StopTraceRecording(USF.GetRand());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000361}
362
363void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000364 assert(TS);
365 TS->ApplyTraceBasedMutation(Idx, U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000366}
367
Kostya Serebryany22526252015-05-11 21:16:27 +0000368void Fuzzer::InitializeTraceState() {
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000369 if (!Options.UseTraces) return;
Kostya Serebryany22526252015-05-11 21:16:27 +0000370 TS = new TraceState(Options, CurrentUnit);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000371 CurrentUnit.resize(Options.MaxLen);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000372 // The rest really requires DFSan.
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000373 if (!ReallyHaveDFSan()) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000374 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) {
375 dfsan_label L = dfsan_create_label("input", (void*)(i + 1));
376 // We assume that no one else has called dfsan_create_label before.
Kostya Serebryanyd46369d2015-08-05 23:44:42 +0000377 if (L != i + 1) {
378 Printf("DFSan labels are not starting from 1, exiting\n");
379 exit(1);
380 }
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000381 }
382}
383
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000384static size_t InternalStrnlen(const char *S, size_t MaxLen) {
385 size_t Len = 0;
386 for (; Len < MaxLen && S[Len]; Len++) {}
387 return Len;
388}
389
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000390} // namespace fuzzer
391
Kostya Serebryany22526252015-05-11 21:16:27 +0000392using fuzzer::TS;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000393
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000394extern "C" {
395void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
396 uint64_t Arg2, dfsan_label L0,
397 dfsan_label L1, dfsan_label L2) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000398 if (!TS) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000399 assert(L0 == 0);
400 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
401 uint64_t CmpSize = (SizeAndType >> 32) / 8;
402 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany22526252015-05-11 21:16:27 +0000403 TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000404}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000405
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000406void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
407 dfsan_label L1, dfsan_label L2) {
408 if (!TS) return;
409 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
410 TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1);
411}
412
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000413void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
414 size_t n, dfsan_label s1_label,
415 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000416 if (!TS) return;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000417 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000418 uint64_t S1 = 0, S2 = 0;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000419 // Simplification: handle only first 8 bytes.
420 memcpy(&S1, s1, std::min(n, sizeof(S1)));
421 memcpy(&S2, s2, std::min(n, sizeof(S2)));
422 dfsan_label L1 = dfsan_read_label(s1, n);
423 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryany22526252015-05-11 21:16:27 +0000424 TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000425}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000426
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000427void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
428 size_t n, dfsan_label s1_label,
429 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000430 if (!TS) return;
431 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
432 uint64_t S1 = 0, S2 = 0;
433 n = std::min(n, fuzzer::InternalStrnlen(s1, n));
434 n = std::min(n, fuzzer::InternalStrnlen(s2, n));
435 // Simplification: handle only first 8 bytes.
436 memcpy(&S1, s1, std::min(n, sizeof(S1)));
437 memcpy(&S2, s2, std::min(n, sizeof(S2)));
438 dfsan_label L1 = dfsan_read_label(s1, n);
439 dfsan_label L2 = dfsan_read_label(s2, n);
440 TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000441}
442
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000443void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2,
444 dfsan_label s1_label, dfsan_label s2_label) {
445 if (!TS) return;
446 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
447 uint64_t S1 = 0, S2 = 0;
448 size_t Len1 = strlen(s1);
449 size_t Len2 = strlen(s2);
450 size_t N = std::min(Len1, Len2);
451 if (N <= 1) return; // Not interesting.
452 // Simplification: handle only first 8 bytes.
453 memcpy(&S1, s1, std::min(N, sizeof(S1)));
454 memcpy(&S2, s2, std::min(N, sizeof(S2)));
455 dfsan_label L1 = dfsan_read_label(s1, Len1);
456 dfsan_label L2 = dfsan_read_label(s2, Len2);
457 TS->DFSanCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2, L1, L2);
458}
459
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000460void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
461 const void *s2, size_t n) {
462 if (!TS) return;
463 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
464 uint64_t S1 = 0, S2 = 0;
465 // Simplification: handle only first 8 bytes.
466 memcpy(&S1, s1, std::min(n, sizeof(S1)));
467 memcpy(&S2, s2, std::min(n, sizeof(S2)));
468 TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000469}
470
471void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
472 const char *s2, size_t n) {
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;
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000476 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
477 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
478 n = std::min(n, Len1);
479 n = std::min(n, Len2);
480 if (n <= 1) return; // Not interesting.
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000481 // Simplification: handle only first 8 bytes.
482 memcpy(&S1, s1, std::min(n, sizeof(S1)));
483 memcpy(&S2, s2, std::min(n, sizeof(S2)));
484 TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2);
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000485}
486
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000487void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
488 const char *s2) {
489 if (!TS) return;
490 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
491 uint64_t S1 = 0, S2 = 0;
492 size_t Len1 = strlen(s1);
493 size_t Len2 = strlen(s2);
494 size_t N = std::min(Len1, Len2);
495 if (N <= 1) return; // Not interesting.
496 // Simplification: handle only first 8 bytes.
497 memcpy(&S1, s1, std::min(N, sizeof(S1)));
498 memcpy(&S2, s2, std::min(N, sizeof(S2)));
499 TS->TraceCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2);
500}
501
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000502__attribute__((visibility("default")))
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000503void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
504 uint64_t Arg2) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000505 if (!TS) return;
Kostya Serebryany35959592015-07-28 00:59:53 +0000506 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000507 uint64_t CmpSize = (SizeAndType >> 32) / 8;
508 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany35959592015-07-28 00:59:53 +0000509 TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000510}
511
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000512__attribute__((visibility("default")))
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000513void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
514 if (!TS) return;
515 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
516 TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2);
517}
518
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000519} // extern "C"