blob: 77946ef976316186fe2215dc2310b4037a59639d [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
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000044// to every byte of the input string (Fuzzer::CurrentUnitData) so that
45// for any chunk of data we know which input bytes it has derived from.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000046// * 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 Serebryany16d03bd2015-03-30 22:09:51 +000049//
Kostya Serebryany22526252015-05-11 21:16:27 +000050// Parts of this code will not function when DFSan is not linked in.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000051// Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer
52// we redeclare the dfsan_* interface functions as weak and check if they
53// are nullptr before calling.
54// If this approach proves to be useful we may add attribute(weak) to the
55// dfsan declarations in dfsan_interface.h
56//
57// This module is in the "proof of concept" stage.
58// It is capable of solving only the simplest puzzles
59// like test/dfsan/DFSanSimpleCmpTest.cpp.
60//===----------------------------------------------------------------------===//
61
Kostya Serebryany22526252015-05-11 21:16:27 +000062/* Example of manual usage (-fsanitize=dataflow is optional):
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000063(
64 cd $LLVM/lib/Fuzzer/
65 clang -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp
Alexey Samsonov21a33812015-05-07 23:33:24 +000066 clang++ -O0 -std=c++11 -fsanitize-coverage=edge,trace-cmp \
Kostya Serebryany3befe942015-05-06 22:47:24 +000067 -fsanitize=dataflow \
Kostya Serebryany65f50862015-09-10 18:48:38 +000068 test/SimpleCmpTest.cpp Fuzzer*.o
69 ./a.out -use_traces=1
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000070)
71*/
72
Kostya Serebryany65f50862015-09-10 18:48:38 +000073#include "FuzzerDFSan.h"
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000074#include "FuzzerInternal.h"
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000075
Kostya Serebryanybeb24c32015-05-07 21:02:11 +000076#include <algorithm>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000077#include <cstring>
Kostya Serebryany226b7342016-01-06 00:03:35 +000078#include <thread>
Kostya Serebryany859e86d2016-01-12 02:08:37 +000079#include <map>
Kostya Serebryanyc135b552016-07-15 23:27:19 +000080#include <set>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000081
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 {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000168 uint32_t Pos;
169 Word W;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000170};
171
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000172// Declared as static globals for faster checks inside the hooks.
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000173static bool RecordingMemcmp = false;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000174static bool RecordingMemmem = false;
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000175static bool RecordingValueProfile = false;
Kostya Serebryany6b08be92016-07-19 18:29:06 +0000176static bool DoingMyOwnMemmem = false;
177
178struct ScopedDoingMyOwnMemmem {
179 ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = true; }
180 ~ScopedDoingMyOwnMemmem() { DoingMyOwnMemmem = false; }
181};
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000182
Kostya Serebryany22526252015-05-11 21:16:27 +0000183class TraceState {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000184public:
185 TraceState(MutationDispatcher &MD, const FuzzingOptions &Options,
Richard Smithb62e7e32016-05-27 21:05:35 +0000186 const Fuzzer *F)
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000187 : MD(MD), Options(Options), F(F) {}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000188
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000189 LabelRange GetLabelRange(dfsan_label L);
190 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
191 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
192 dfsan_label L2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000193 void DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
194 const uint8_t *Data2, dfsan_label L1,
195 dfsan_label L2);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000196 void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val,
197 size_t NumCases, uint64_t *Cases, dfsan_label L);
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000198 void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
199 uint64_t Arg1, uint64_t Arg2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000200 void TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
201 const uint8_t *Data2);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000202
203 void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val,
204 size_t NumCases, uint64_t *Cases);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000205 int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000206 size_t DataSize);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000207 int TryToAddDesiredData(const uint8_t *PresentData,
208 const uint8_t *DesiredData, size_t DataSize);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000209
210 void StartTraceRecording() {
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000211 if (!Options.UseMemcmp)
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000212 return;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000213 RecordingMemcmp = Options.UseMemcmp;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000214 RecordingMemmem = Options.UseMemmem;
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000215 NumMutations = 0;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000216 InterestingWords.clear();
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000217 MD.ClearAutoDictionary();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000218 }
219
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000220 void StopTraceRecording() {
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000221 if (!RecordingMemcmp)
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000222 return;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000223 RecordingMemcmp = false;
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000224 for (size_t i = 0; i < NumMutations; i++) {
225 auto &M = Mutations[i];
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000226 if (Options.Verbosity >= 2) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000227 AutoDictUnitCounts[M.W]++;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000228 AutoDictAdds++;
229 if ((AutoDictAdds & (AutoDictAdds - 1)) == 0) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000230 typedef std::pair<size_t, Word> CU;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000231 std::vector<CU> CountedUnits;
232 for (auto &I : AutoDictUnitCounts)
233 CountedUnits.push_back(std::make_pair(I.second, I.first));
234 std::sort(CountedUnits.begin(), CountedUnits.end(),
235 [](const CU &a, const CU &b) { return a.first > b.first; });
236 Printf("AutoDict:\n");
237 for (auto &I : CountedUnits) {
238 Printf(" %zd ", I.first);
Kostya Serebryany41740052016-01-12 02:36:59 +0000239 PrintASCII(I.second);
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000240 Printf("\n");
241 }
242 }
243 }
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000244 MD.AddWordToAutoDictionary({M.W, M.Pos});
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000245 }
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000246 for (auto &W : InterestingWords)
247 MD.AddWordToAutoDictionary({W});
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000248 }
249
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000250 void AddMutation(uint32_t Pos, uint32_t Size, const uint8_t *Data) {
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000251 if (NumMutations >= kMaxMutations) return;
252 auto &M = Mutations[NumMutations++];
253 M.Pos = Pos;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000254 M.W.Set(Data, Size);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000255 }
256
257 void AddMutation(uint32_t Pos, uint32_t Size, uint64_t Data) {
258 assert(Size <= sizeof(Data));
259 AddMutation(Pos, Size, reinterpret_cast<uint8_t*>(&Data));
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000260 }
261
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000262 void AddInterestingWord(const uint8_t *Data, size_t Size) {
263 if (!RecordingMemmem || !F->InFuzzingThread()) return;
264 if (Size <= 1) return;
265 Size = std::min(Size, Word::GetMaxSize());
266 Word W(Data, Size);
267 InterestingWords.insert(W);
268 }
269
Kostya Serebryany64d24572016-03-12 01:57:04 +0000270 void EnsureDfsanLabels(size_t Size) {
271 for (; LastDfsanLabel < Size; LastDfsanLabel++) {
272 dfsan_label L = dfsan_create_label("input", (void *)(LastDfsanLabel + 1));
273 // We assume that no one else has called dfsan_create_label before.
274 if (L != LastDfsanLabel + 1) {
275 Printf("DFSan labels are not starting from 1, exiting\n");
276 exit(1);
277 }
278 }
279 }
280
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000281 private:
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000282 bool IsTwoByteData(uint64_t Data) {
283 int64_t Signed = static_cast<int64_t>(Data);
284 Signed >>= 16;
285 return Signed == 0 || Signed == -1L;
286 }
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000287
288 // We don't want to create too many trace-based mutations as it is both
289 // expensive and useless. So after some number of mutations is collected,
290 // start rejecting some of them. The more there are mutations the more we
291 // reject.
292 bool WantToHandleOneMoreMutation() {
293 const size_t FirstN = 64;
294 // Gladly handle first N mutations.
295 if (NumMutations <= FirstN) return true;
296 size_t Diff = NumMutations - FirstN;
297 size_t DiffLog = sizeof(long) * 8 - __builtin_clzl((long)Diff);
298 assert(DiffLog > 0 && DiffLog < 64);
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000299 bool WantThisOne = MD.GetRand()(1 << DiffLog) == 0; // 1 out of DiffLog.
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000300 return WantThisOne;
301 }
302
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000303 static const size_t kMaxMutations = 1 << 16;
304 size_t NumMutations;
305 TraceBasedMutation Mutations[kMaxMutations];
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000306 // TODO: std::set is too inefficient, need to have a custom DS here.
307 std::set<Word> InterestingWords;
Kostya Serebryany4d623222015-11-18 01:08:30 +0000308 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)];
Kostya Serebryany64d24572016-03-12 01:57:04 +0000309 size_t LastDfsanLabel = 0;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000310 MutationDispatcher &MD;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000311 const FuzzingOptions Options;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000312 const Fuzzer *F;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000313 std::map<Word, size_t> AutoDictUnitCounts;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000314 size_t AutoDictAdds = 0;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000315};
316
Kostya Serebryany226b7342016-01-06 00:03:35 +0000317
Kostya Serebryany22526252015-05-11 21:16:27 +0000318LabelRange TraceState::GetLabelRange(dfsan_label L) {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000319 LabelRange &LR = LabelRanges[L];
320 if (LR.Beg < LR.End || L == 0)
321 return LR;
322 const dfsan_label_info *LI = dfsan_get_label_info(L);
323 if (LI->l1 || LI->l2)
324 return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
325 return LR = LabelRange::Singleton(LI);
326}
327
Kostya Serebryany22526252015-05-11 21:16:27 +0000328void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000329 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
330 dfsan_label L2) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000331 assert(ReallyHaveDFSan());
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000332 if (!F->InFuzzingThread()) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000333 if (L1 == 0 && L2 == 0)
334 return; // Not actionable.
335 if (L1 != 0 && L2 != 0)
336 return; // Probably still actionable.
337 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000338 uint64_t Data = L1 ? Arg2 : Arg1;
339 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000340
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000341 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000342 AddMutation(Pos, CmpSize, Data);
343 AddMutation(Pos, CmpSize, Data + 1);
344 AddMutation(Pos, CmpSize, Data - 1);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000345 }
346
Dmitry Vyukov2eed1212016-03-02 09:54:40 +0000347 if (CmpSize > (size_t)(LR.End - LR.Beg))
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000348 AddMutation(LR.Beg, (unsigned)(LR.End - LR.Beg), Data);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000349
350
351 if (Options.Verbosity >= 3)
Kostya Serebryanyae7df1c2015-07-28 01:25:00 +0000352 Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 "
353 "%d MU %zd\n",
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000354 PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, NumMutations);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000355}
356
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000357void TraceState::DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
358 const uint8_t *Data2, dfsan_label L1,
359 dfsan_label L2) {
360
361 assert(ReallyHaveDFSan());
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000362 if (!RecordingMemcmp || !F->InFuzzingThread()) return;
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000363 if (L1 == 0 && L2 == 0)
364 return; // Not actionable.
365 if (L1 != 0 && L2 != 0)
366 return; // Probably still actionable.
367
368 const uint8_t *Data = L1 ? Data2 : Data1;
369 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
370 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
371 AddMutation(Pos, CmpSize, Data);
372 if (Options.Verbosity >= 3)
373 Printf("DFSanMemcmpCallback: Pos %d Size %d\n", Pos, CmpSize);
374 }
375}
376
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000377void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits,
378 uint64_t Val, size_t NumCases,
379 uint64_t *Cases, dfsan_label L) {
380 assert(ReallyHaveDFSan());
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000381 if (!F->InFuzzingThread()) return;
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000382 if (!L) return; // Not actionable.
383 LabelRange LR = GetLabelRange(L);
384 size_t ValSize = ValSizeInBits / 8;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000385 bool TryShort = IsTwoByteData(Val);
386 for (size_t i = 0; i < NumCases; i++)
387 TryShort &= IsTwoByteData(Cases[i]);
388
389 for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++)
390 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000391 AddMutation(Pos, ValSize, Cases[i]);
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000392
393 if (TryShort)
394 for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++)
395 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000396 AddMutation(Pos, 2, Cases[i]);
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000397
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000398 if (Options.Verbosity >= 3)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000399 Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} "
400 "TryShort %d\n",
401 PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000402}
403
Kostya Serebryany22526252015-05-11 21:16:27 +0000404int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000405 size_t DataSize) {
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000406 if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
Kostya Serebryany6b08be92016-07-19 18:29:06 +0000407 ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000408 const uint8_t *UnitData;
409 auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000410 int Res = 0;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000411 const uint8_t *Beg = UnitData;
412 const uint8_t *End = Beg + UnitSize;
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000413 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000414 Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
415 if (!Cur)
416 break;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000417 size_t Pos = Cur - Beg;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000418 assert(Pos < UnitSize);
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000419 AddMutation(Pos, DataSize, DesiredData);
420 AddMutation(Pos, DataSize, DesiredData + 1);
421 AddMutation(Pos, DataSize, DesiredData - 1);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000422 Res++;
423 }
424 return Res;
425}
426
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000427int TraceState::TryToAddDesiredData(const uint8_t *PresentData,
428 const uint8_t *DesiredData,
429 size_t DataSize) {
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000430 if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
Kostya Serebryany6b08be92016-07-19 18:29:06 +0000431 ScopedDoingMyOwnMemmem scoped_doing_my_own_memmem;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000432 const uint8_t *UnitData;
433 auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000434 int Res = 0;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000435 const uint8_t *Beg = UnitData;
436 const uint8_t *End = Beg + UnitSize;
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000437 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
438 Cur = (uint8_t *)memmem(Cur, End - Cur, PresentData, DataSize);
439 if (!Cur)
440 break;
441 size_t Pos = Cur - Beg;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000442 assert(Pos < UnitSize);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000443 AddMutation(Pos, DataSize, DesiredData);
444 Res++;
445 }
446 return Res;
447}
448
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000449void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
450 uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000451 if (!F->InFuzzingThread()) return;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000452 if ((CmpType == ICMP_EQ || CmpType == ICMP_NE) && Arg1 == Arg2)
453 return; // No reason to mutate.
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000454 int Added = 0;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000455 Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
456 Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
457 if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
458 Added += TryToAddDesiredData(Arg1, Arg2, 2);
459 Added += TryToAddDesiredData(Arg2, Arg1, 2);
460 }
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000461 if (Options.Verbosity >= 3 && Added)
462 Printf("TraceCmp %zd/%zd: %p %zd %zd\n", CmpSize, CmpType, PC, Arg1, Arg2);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000463}
464
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000465void TraceState::TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
466 const uint8_t *Data2) {
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000467 if (!RecordingMemcmp || !F->InFuzzingThread()) return;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000468 CmpSize = std::min(CmpSize, Word::GetMaxSize());
Kostya Serebryany1f9c40d2016-01-09 03:46:08 +0000469 int Added2 = TryToAddDesiredData(Data1, Data2, CmpSize);
470 int Added1 = TryToAddDesiredData(Data2, Data1, CmpSize);
471 if ((Added1 || Added2) && Options.Verbosity >= 3) {
472 Printf("MemCmp Added %d%d: ", Added1, Added2);
Kostya Serebryany41740052016-01-12 02:36:59 +0000473 if (Added1) PrintASCII(Data1, CmpSize);
474 if (Added2) PrintASCII(Data2, CmpSize);
Kostya Serebryany1f9c40d2016-01-09 03:46:08 +0000475 Printf("\n");
476 }
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000477}
478
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000479void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits,
480 uint64_t Val, size_t NumCases,
481 uint64_t *Cases) {
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000482 if (F->InFuzzingThread()) return;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000483 size_t ValSize = ValSizeInBits / 8;
484 bool TryShort = IsTwoByteData(Val);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000485 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000486 TryShort &= IsTwoByteData(Cases[i]);
487
488 if (Options.Verbosity >= 3)
489 Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases,
490 TryShort);
491
492 for (size_t i = 0; i < NumCases; i++) {
493 TryToAddDesiredData(Val, Cases[i], ValSize);
494 if (TryShort)
495 TryToAddDesiredData(Val, Cases[i], 2);
496 }
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000497}
498
Kostya Serebryany22526252015-05-11 21:16:27 +0000499static TraceState *TS;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000500
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000501void Fuzzer::StartTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000502 if (!TS) return;
503 TS->StartTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000504}
505
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000506void Fuzzer::StopTraceRecording() {
507 if (!TS) return;
508 TS->StopTraceRecording();
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000509}
510
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000511void Fuzzer::AssignTaintLabels(uint8_t *Data, size_t Size) {
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000512 if (!Options.UseMemcmp) return;
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000513 if (!ReallyHaveDFSan()) return;
Kostya Serebryany64d24572016-03-12 01:57:04 +0000514 TS->EnsureDfsanLabels(Size);
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000515 for (size_t i = 0; i < Size; i++)
516 dfsan_set_label(i + 1, &Data[i], 1);
517}
518
Kostya Serebryany22526252015-05-11 21:16:27 +0000519void Fuzzer::InitializeTraceState() {
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000520 if (!Options.UseMemcmp) return;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000521 TS = new TraceState(MD, Options, this);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000522}
523
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000524static size_t InternalStrnlen(const char *S, size_t MaxLen) {
525 size_t Len = 0;
526 for (; Len < MaxLen && S[Len]; Len++) {}
527 return Len;
528}
529
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000530// Value profile.
531// We keep track of various values that affect control flow.
532// These values are inserted into a bit-set-based hash map (ValueBitMap VP).
533// Every new bit in the map is treated as a new coverage.
534//
535// For memcmp/strcmp/etc the interesting value is the length of the common
536// prefix of the parameters.
537// For cmp instructions the interesting value is a XOR of the parameters.
538// The interesting value is mixed up with the PC and is then added to the map.
539static ValueBitMap VP;
540
541void EnableValueProfile() { RecordingValueProfile = true; }
542
543size_t VPMapMergeFromCurrent(ValueBitMap &M) {
544 if (!RecordingValueProfile) return 0;
545 return M.MergeFrom(VP);
546}
547
548static void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
549 size_t n) {
550 if (!n) return;
551 size_t Len = std::min(n, (size_t)32);
Kostya Serebryanyd4492f82016-08-30 03:05:50 +0000552 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
553 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
554 size_t I = 0;
555 for (; I < Len; I++)
556 if (A1[I] != A2[I])
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000557 break;
558 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany248d1152016-08-30 14:39:33 +0000559 size_t Idx = I;
560 // if (I < Len)
561 // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
Kostya Serebryanyd4492f82016-08-30 03:05:50 +0000562 VP.AddValue((PC & 4095) | (Idx << 12));
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000563}
564
565static void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
566 size_t n) {
567 if (!n) return;
568 size_t Len = std::min(n, (size_t)32);
Kostya Serebryanyd4492f82016-08-30 03:05:50 +0000569 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
570 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
571 size_t I = 0;
572 for (; I < Len; I++)
573 if (A1[I] != A2[I] || A1[I] == 0)
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000574 break;
575 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany248d1152016-08-30 14:39:33 +0000576 size_t Idx = I;
577 // if (I < Len && A1[I])
578 // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
Kostya Serebryanyd4492f82016-08-30 03:05:50 +0000579 VP.AddValue((PC & 4095) | (Idx << 12));
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000580}
581
Kostya Serebryanybceadcf2016-08-24 01:38:42 +0000582ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000583static void AddValueForCmp(void *PCptr, uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryanyac524cf2016-08-23 23:37:37 +0000584 if (Arg1 == Arg2)
585 return;
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000586 uintptr_t PC = reinterpret_cast<uintptr_t>(PCptr);
Kostya Serebryanyac524cf2016-08-23 23:37:37 +0000587 uint64_t ArgDistance = __builtin_popcountl(Arg1 ^ Arg2) - 1; // [0,63]
588 uintptr_t Idx = (PC & 4095) | (ArgDistance << 12);
589 VP.AddValue(Idx);
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000590}
591
Kostya Serebryany4d22e4f2016-08-30 01:30:14 +0000592static void AddValueForSingleVal(void *PCptr, uintptr_t Val) {
593 if (!Val) return;
594 uintptr_t PC = reinterpret_cast<uintptr_t>(PCptr);
595 uint64_t ArgDistance = __builtin_popcountl(Val) - 1; // [0,63]
596 uintptr_t Idx = (PC & 4095) | (ArgDistance << 12);
597 VP.AddValue(Idx);
598}
599
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000600} // namespace fuzzer
601
Kostya Serebryany22526252015-05-11 21:16:27 +0000602using fuzzer::TS;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000603using fuzzer::RecordingMemcmp;
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000604using fuzzer::RecordingValueProfile;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000605
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000606extern "C" {
607void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
608 uint64_t Arg2, dfsan_label L0,
609 dfsan_label L1, dfsan_label L2) {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000610}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000611
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000612#define DFSAN_CMP_CALLBACK(N) \
613 void __dfsw___sanitizer_cov_trace_cmp##N(uint64_t Arg1, uint64_t Arg2, \
614 dfsan_label L1, dfsan_label L2) { \
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000615 }
616
617DFSAN_CMP_CALLBACK(1)
618DFSAN_CMP_CALLBACK(2)
619DFSAN_CMP_CALLBACK(4)
620DFSAN_CMP_CALLBACK(8)
621#undef DFSAN_CMP_CALLBACK
622
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000623void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
624 dfsan_label L1, dfsan_label L2) {
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000625}
626
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000627void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
628 size_t n, dfsan_label s1_label,
629 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000630 if (!RecordingMemcmp) return;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000631 dfsan_label L1 = dfsan_read_label(s1, n);
632 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000633 TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
634 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000635}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000636
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000637void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
638 size_t n, dfsan_label s1_label,
639 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000640 if (!RecordingMemcmp) return;
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000641 n = std::min(n, fuzzer::InternalStrnlen(s1, n));
642 n = std::min(n, fuzzer::InternalStrnlen(s2, n));
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000643 dfsan_label L1 = dfsan_read_label(s1, n);
644 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000645 TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
646 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000647}
648
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000649void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2,
650 dfsan_label s1_label, dfsan_label s2_label) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000651 if (!RecordingMemcmp) return;
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000652 size_t Len1 = strlen(s1);
653 size_t Len2 = strlen(s2);
654 size_t N = std::min(Len1, Len2);
655 if (N <= 1) return; // Not interesting.
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000656 dfsan_label L1 = dfsan_read_label(s1, Len1);
657 dfsan_label L2 = dfsan_read_label(s2, Len2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000658 TS->DFSanMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
659 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000660}
661
Kostya Serebryany4b83a4f2016-01-12 16:50:18 +0000662// We may need to avoid defining weak hooks to stay compatible with older clang.
663#ifndef LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
664# define LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS 1
665#endif
666
667#if LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000668void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
Kostya Serebryanye3580952016-01-12 00:43:42 +0000669 const void *s2, size_t n, int result) {
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000670 if (RecordingValueProfile)
671 fuzzer::AddValueForMemcmp(caller_pc, s1, s2, n);
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000672 if (!RecordingMemcmp) return;
Kostya Serebryanye3580952016-01-12 00:43:42 +0000673 if (result == 0) return; // No reason to mutate.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000674 if (n <= 1) return; // Not interesting.
675 TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
676 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000677}
678
679void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
Kostya Serebryanye3580952016-01-12 00:43:42 +0000680 const char *s2, size_t n, int result) {
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000681 if (RecordingValueProfile)
682 fuzzer::AddValueForStrcmp(caller_pc, s1, s2, n);
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000683 if (!RecordingMemcmp) return;
Kostya Serebryanye3580952016-01-12 00:43:42 +0000684 if (result == 0) return; // No reason to mutate.
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000685 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
686 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
687 n = std::min(n, Len1);
688 n = std::min(n, Len2);
689 if (n <= 1) return; // Not interesting.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000690 TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
691 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000692}
693
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000694void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
Kostya Serebryanye3580952016-01-12 00:43:42 +0000695 const char *s2, int result) {
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000696 if (RecordingValueProfile)
697 fuzzer::AddValueForStrcmp(caller_pc, s1, s2, 64);
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000698 if (!RecordingMemcmp) return;
Kostya Serebryanye3580952016-01-12 00:43:42 +0000699 if (result == 0) return; // No reason to mutate.
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000700 size_t Len1 = strlen(s1);
701 size_t Len2 = strlen(s2);
702 size_t N = std::min(Len1, Len2);
703 if (N <= 1) return; // Not interesting.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000704 TS->TraceMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
705 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000706}
707
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000708void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
709 const char *s2, size_t n, int result) {
710 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
711}
712void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
713 const char *s2, int result) {
714 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
715}
716void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
717 const char *s2, char *result) {
718 TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
719}
720void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
721 const char *s2, char *result) {
722 TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
723}
724void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
725 const void *s2, size_t len2, void *result) {
Kostya Serebryany6b08be92016-07-19 18:29:06 +0000726 if (fuzzer::DoingMyOwnMemmem) return;
727 TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), len2);
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000728}
729
Kostya Serebryany4b83a4f2016-01-12 16:50:18 +0000730#endif // LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
731
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000732// TODO: this one will not be used with the newest clang. Remove it.
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000733__attribute__((visibility("default")))
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000734void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
735 uint64_t Arg2) {
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +0000736 if (RecordingValueProfile)
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000737 fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
738}
739
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000740__attribute__((visibility("default")))
741void __sanitizer_cov_trace_cmp8(uint64_t Arg1, int64_t Arg2) {
742 fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000743}
744__attribute__((visibility("default")))
745void __sanitizer_cov_trace_cmp4(uint32_t Arg1, int32_t Arg2) {
746 fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000747}
748__attribute__((visibility("default")))
749void __sanitizer_cov_trace_cmp2(uint16_t Arg1, int16_t Arg2) {
750 fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany524c3f32016-08-18 01:25:28 +0000751}
752__attribute__((visibility("default")))
753void __sanitizer_cov_trace_cmp1(uint8_t Arg1, int8_t Arg2) {
754 fuzzer::AddValueForCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000755}
756
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000757__attribute__((visibility("default")))
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000758void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryany5c04bd22016-09-09 01:17:03 +0000759 // TODO(kcc): support value profile here.
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000760}
761
Kostya Serebryany4d22e4f2016-08-30 01:30:14 +0000762__attribute__((visibility("default")))
763void __sanitizer_cov_trace_div4(uint32_t Val) {
764 fuzzer::AddValueForSingleVal(__builtin_return_address(0), Val);
765}
766__attribute__((visibility("default")))
767void __sanitizer_cov_trace_div8(uint64_t Val) {
768 fuzzer::AddValueForSingleVal(__builtin_return_address(0), Val);
769}
770__attribute__((visibility("default")))
771void __sanitizer_cov_trace_gep(uintptr_t Idx) {
772 fuzzer::AddValueForSingleVal(__builtin_return_address(0), Idx);
773}
774
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000775} // extern "C"