blob: d6e1f79791f5e2d7a196e6affd98cd0b3ebb0338 [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.
173static bool RecordingTraces = false;
174static bool RecordingMemcmp = false;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000175static bool RecordingMemmem = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000176
Kostya Serebryany22526252015-05-11 21:16:27 +0000177class TraceState {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000178public:
179 TraceState(MutationDispatcher &MD, const FuzzingOptions &Options,
Richard Smithb62e7e32016-05-27 21:05:35 +0000180 const Fuzzer *F)
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000181 : MD(MD), Options(Options), F(F) {}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000182
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000183 LabelRange GetLabelRange(dfsan_label L);
184 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
185 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
186 dfsan_label L2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000187 void DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
188 const uint8_t *Data2, dfsan_label L1,
189 dfsan_label L2);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000190 void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val,
191 size_t NumCases, uint64_t *Cases, dfsan_label L);
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000192 void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
193 uint64_t Arg1, uint64_t Arg2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000194 void TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
195 const uint8_t *Data2);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000196
197 void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val,
198 size_t NumCases, uint64_t *Cases);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000199 int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000200 size_t DataSize);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000201 int TryToAddDesiredData(const uint8_t *PresentData,
202 const uint8_t *DesiredData, size_t DataSize);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000203
204 void StartTraceRecording() {
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000205 if (!Options.UseTraces && !Options.UseMemcmp)
206 return;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000207 RecordingTraces = Options.UseTraces;
208 RecordingMemcmp = Options.UseMemcmp;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000209 RecordingMemmem = Options.UseMemmem;
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000210 NumMutations = 0;
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000211 InterestingWords.clear();
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000212 MD.ClearAutoDictionary();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000213 }
214
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000215 void StopTraceRecording() {
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000216 if (!RecordingTraces && !RecordingMemcmp)
217 return;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000218 RecordingTraces = false;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000219 RecordingMemcmp = false;
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000220 for (size_t i = 0; i < NumMutations; i++) {
221 auto &M = Mutations[i];
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000222 if (Options.Verbosity >= 2) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000223 AutoDictUnitCounts[M.W]++;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000224 AutoDictAdds++;
225 if ((AutoDictAdds & (AutoDictAdds - 1)) == 0) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000226 typedef std::pair<size_t, Word> CU;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000227 std::vector<CU> CountedUnits;
228 for (auto &I : AutoDictUnitCounts)
229 CountedUnits.push_back(std::make_pair(I.second, I.first));
230 std::sort(CountedUnits.begin(), CountedUnits.end(),
231 [](const CU &a, const CU &b) { return a.first > b.first; });
232 Printf("AutoDict:\n");
233 for (auto &I : CountedUnits) {
234 Printf(" %zd ", I.first);
Kostya Serebryany41740052016-01-12 02:36:59 +0000235 PrintASCII(I.second);
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000236 Printf("\n");
237 }
238 }
239 }
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000240 MD.AddWordToAutoDictionary({M.W, M.Pos});
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000241 }
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000242 for (auto &W : InterestingWords)
243 MD.AddWordToAutoDictionary({W});
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000244 }
245
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000246 void AddMutation(uint32_t Pos, uint32_t Size, const uint8_t *Data) {
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000247 if (NumMutations >= kMaxMutations) return;
248 auto &M = Mutations[NumMutations++];
249 M.Pos = Pos;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000250 M.W.Set(Data, Size);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000251 }
252
253 void AddMutation(uint32_t Pos, uint32_t Size, uint64_t Data) {
254 assert(Size <= sizeof(Data));
255 AddMutation(Pos, Size, reinterpret_cast<uint8_t*>(&Data));
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000256 }
257
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000258 void AddInterestingWord(const uint8_t *Data, size_t Size) {
259 if (!RecordingMemmem || !F->InFuzzingThread()) return;
260 if (Size <= 1) return;
261 Size = std::min(Size, Word::GetMaxSize());
262 Word W(Data, Size);
263 InterestingWords.insert(W);
264 }
265
Kostya Serebryany64d24572016-03-12 01:57:04 +0000266 void EnsureDfsanLabels(size_t Size) {
267 for (; LastDfsanLabel < Size; LastDfsanLabel++) {
268 dfsan_label L = dfsan_create_label("input", (void *)(LastDfsanLabel + 1));
269 // We assume that no one else has called dfsan_create_label before.
270 if (L != LastDfsanLabel + 1) {
271 Printf("DFSan labels are not starting from 1, exiting\n");
272 exit(1);
273 }
274 }
275 }
276
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000277 private:
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000278 bool IsTwoByteData(uint64_t Data) {
279 int64_t Signed = static_cast<int64_t>(Data);
280 Signed >>= 16;
281 return Signed == 0 || Signed == -1L;
282 }
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000283
284 // We don't want to create too many trace-based mutations as it is both
285 // expensive and useless. So after some number of mutations is collected,
286 // start rejecting some of them. The more there are mutations the more we
287 // reject.
288 bool WantToHandleOneMoreMutation() {
289 const size_t FirstN = 64;
290 // Gladly handle first N mutations.
291 if (NumMutations <= FirstN) return true;
292 size_t Diff = NumMutations - FirstN;
293 size_t DiffLog = sizeof(long) * 8 - __builtin_clzl((long)Diff);
294 assert(DiffLog > 0 && DiffLog < 64);
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000295 bool WantThisOne = MD.GetRand()(1 << DiffLog) == 0; // 1 out of DiffLog.
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000296 return WantThisOne;
297 }
298
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000299 static const size_t kMaxMutations = 1 << 16;
300 size_t NumMutations;
301 TraceBasedMutation Mutations[kMaxMutations];
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000302 // TODO: std::set is too inefficient, need to have a custom DS here.
303 std::set<Word> InterestingWords;
Kostya Serebryany4d623222015-11-18 01:08:30 +0000304 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)];
Kostya Serebryany64d24572016-03-12 01:57:04 +0000305 size_t LastDfsanLabel = 0;
Kostya Serebryany7ec0c562016-02-13 03:25:16 +0000306 MutationDispatcher &MD;
Mike Aizatskyf0b3e852016-06-23 20:44:48 +0000307 const FuzzingOptions Options;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000308 const Fuzzer *F;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000309 std::map<Word, size_t> AutoDictUnitCounts;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000310 size_t AutoDictAdds = 0;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000311};
312
Kostya Serebryany226b7342016-01-06 00:03:35 +0000313
Kostya Serebryany22526252015-05-11 21:16:27 +0000314LabelRange TraceState::GetLabelRange(dfsan_label L) {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000315 LabelRange &LR = LabelRanges[L];
316 if (LR.Beg < LR.End || L == 0)
317 return LR;
318 const dfsan_label_info *LI = dfsan_get_label_info(L);
319 if (LI->l1 || LI->l2)
320 return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
321 return LR = LabelRange::Singleton(LI);
322}
323
Kostya Serebryany22526252015-05-11 21:16:27 +0000324void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000325 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
326 dfsan_label L2) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000327 assert(ReallyHaveDFSan());
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000328 if (!RecordingTraces || !F->InFuzzingThread()) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000329 if (L1 == 0 && L2 == 0)
330 return; // Not actionable.
331 if (L1 != 0 && L2 != 0)
332 return; // Probably still actionable.
333 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000334 uint64_t Data = L1 ? Arg2 : Arg1;
335 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000336
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000337 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000338 AddMutation(Pos, CmpSize, Data);
339 AddMutation(Pos, CmpSize, Data + 1);
340 AddMutation(Pos, CmpSize, Data - 1);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000341 }
342
Dmitry Vyukov2eed1212016-03-02 09:54:40 +0000343 if (CmpSize > (size_t)(LR.End - LR.Beg))
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000344 AddMutation(LR.Beg, (unsigned)(LR.End - LR.Beg), Data);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000345
346
347 if (Options.Verbosity >= 3)
Kostya Serebryanyae7df1c2015-07-28 01:25:00 +0000348 Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 "
349 "%d MU %zd\n",
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000350 PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, NumMutations);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000351}
352
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000353void TraceState::DFSanMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
354 const uint8_t *Data2, dfsan_label L1,
355 dfsan_label L2) {
356
357 assert(ReallyHaveDFSan());
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000358 if (!RecordingMemcmp || !F->InFuzzingThread()) return;
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000359 if (L1 == 0 && L2 == 0)
360 return; // Not actionable.
361 if (L1 != 0 && L2 != 0)
362 return; // Probably still actionable.
363
364 const uint8_t *Data = L1 ? Data2 : Data1;
365 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
366 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
367 AddMutation(Pos, CmpSize, Data);
368 if (Options.Verbosity >= 3)
369 Printf("DFSanMemcmpCallback: Pos %d Size %d\n", Pos, CmpSize);
370 }
371}
372
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000373void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits,
374 uint64_t Val, size_t NumCases,
375 uint64_t *Cases, dfsan_label L) {
376 assert(ReallyHaveDFSan());
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000377 if (!RecordingTraces || !F->InFuzzingThread()) return;
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000378 if (!L) return; // Not actionable.
379 LabelRange LR = GetLabelRange(L);
380 size_t ValSize = ValSizeInBits / 8;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000381 bool TryShort = IsTwoByteData(Val);
382 for (size_t i = 0; i < NumCases; i++)
383 TryShort &= IsTwoByteData(Cases[i]);
384
385 for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++)
386 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000387 AddMutation(Pos, ValSize, Cases[i]);
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000388
389 if (TryShort)
390 for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++)
391 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000392 AddMutation(Pos, 2, Cases[i]);
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000393
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000394 if (Options.Verbosity >= 3)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000395 Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} "
396 "TryShort %d\n",
397 PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort);
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000398}
399
Kostya Serebryany22526252015-05-11 21:16:27 +0000400int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000401 size_t DataSize) {
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000402 if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000403 const uint8_t *UnitData;
404 auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000405 int Res = 0;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000406 const uint8_t *Beg = UnitData;
407 const uint8_t *End = Beg + UnitSize;
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000408 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000409 Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
410 if (!Cur)
411 break;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000412 size_t Pos = Cur - Beg;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000413 assert(Pos < UnitSize);
Kostya Serebryanye7583d22016-01-09 00:38:40 +0000414 AddMutation(Pos, DataSize, DesiredData);
415 AddMutation(Pos, DataSize, DesiredData + 1);
416 AddMutation(Pos, DataSize, DesiredData - 1);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000417 Res++;
418 }
419 return Res;
420}
421
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000422int TraceState::TryToAddDesiredData(const uint8_t *PresentData,
423 const uint8_t *DesiredData,
424 size_t DataSize) {
Kostya Serebryanyd88d1302016-02-02 23:17:45 +0000425 if (NumMutations >= kMaxMutations || !WantToHandleOneMoreMutation()) return 0;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000426 const uint8_t *UnitData;
427 auto UnitSize = F->GetCurrentUnitInFuzzingThead(&UnitData);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000428 int Res = 0;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000429 const uint8_t *Beg = UnitData;
430 const uint8_t *End = Beg + UnitSize;
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000431 for (const uint8_t *Cur = Beg; Cur < End; Cur++) {
432 Cur = (uint8_t *)memmem(Cur, End - Cur, PresentData, DataSize);
433 if (!Cur)
434 break;
435 size_t Pos = Cur - Beg;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000436 assert(Pos < UnitSize);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000437 AddMutation(Pos, DataSize, DesiredData);
438 Res++;
439 }
440 return Res;
441}
442
Kostya Serebryanye641dd62015-09-04 22:32:25 +0000443void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
444 uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000445 if (!RecordingTraces || !F->InFuzzingThread()) return;
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000446 if ((CmpType == ICMP_EQ || CmpType == ICMP_NE) && Arg1 == Arg2)
447 return; // No reason to mutate.
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000448 int Added = 0;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000449 Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
450 Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
451 if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
452 Added += TryToAddDesiredData(Arg1, Arg2, 2);
453 Added += TryToAddDesiredData(Arg2, Arg1, 2);
454 }
Kostya Serebryany859e86d2016-01-12 02:08:37 +0000455 if (Options.Verbosity >= 3 && Added)
456 Printf("TraceCmp %zd/%zd: %p %zd %zd\n", CmpSize, CmpType, PC, Arg1, Arg2);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000457}
458
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000459void TraceState::TraceMemcmpCallback(size_t CmpSize, const uint8_t *Data1,
460 const uint8_t *Data2) {
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000461 if (!RecordingMemcmp || !F->InFuzzingThread()) return;
Kostya Serebryany476f0ce2016-01-16 03:53:32 +0000462 CmpSize = std::min(CmpSize, Word::GetMaxSize());
Kostya Serebryany1f9c40d2016-01-09 03:46:08 +0000463 int Added2 = TryToAddDesiredData(Data1, Data2, CmpSize);
464 int Added1 = TryToAddDesiredData(Data2, Data1, CmpSize);
465 if ((Added1 || Added2) && Options.Verbosity >= 3) {
466 Printf("MemCmp Added %d%d: ", Added1, Added2);
Kostya Serebryany41740052016-01-12 02:36:59 +0000467 if (Added1) PrintASCII(Data1, CmpSize);
468 if (Added2) PrintASCII(Data2, CmpSize);
Kostya Serebryany1f9c40d2016-01-09 03:46:08 +0000469 Printf("\n");
470 }
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000471}
472
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000473void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits,
474 uint64_t Val, size_t NumCases,
475 uint64_t *Cases) {
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000476 if (!RecordingTraces || !F->InFuzzingThread()) return;
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000477 size_t ValSize = ValSizeInBits / 8;
478 bool TryShort = IsTwoByteData(Val);
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000479 for (size_t i = 0; i < NumCases; i++)
Kostya Serebryanyfe7e41e2015-07-31 20:58:55 +0000480 TryShort &= IsTwoByteData(Cases[i]);
481
482 if (Options.Verbosity >= 3)
483 Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases,
484 TryShort);
485
486 for (size_t i = 0; i < NumCases; i++) {
487 TryToAddDesiredData(Val, Cases[i], ValSize);
488 if (TryShort)
489 TryToAddDesiredData(Val, Cases[i], 2);
490 }
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000491}
492
Kostya Serebryany22526252015-05-11 21:16:27 +0000493static TraceState *TS;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000494
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000495void Fuzzer::StartTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000496 if (!TS) return;
497 TS->StartTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000498}
499
Kostya Serebryanyb65805a2016-01-09 03:08:58 +0000500void Fuzzer::StopTraceRecording() {
501 if (!TS) return;
502 TS->StopTraceRecording();
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000503}
504
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000505void Fuzzer::AssignTaintLabels(uint8_t *Data, size_t Size) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000506 if (!Options.UseTraces && !Options.UseMemcmp) return;
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000507 if (!ReallyHaveDFSan()) return;
Kostya Serebryany64d24572016-03-12 01:57:04 +0000508 TS->EnsureDfsanLabels(Size);
Kostya Serebryanyd50a3ee2016-01-13 23:02:30 +0000509 for (size_t i = 0; i < Size; i++)
510 dfsan_set_label(i + 1, &Data[i], 1);
511}
512
Kostya Serebryany22526252015-05-11 21:16:27 +0000513void Fuzzer::InitializeTraceState() {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000514 if (!Options.UseTraces && !Options.UseMemcmp) return;
Kostya Serebryanyf26017b2016-05-26 21:32:30 +0000515 TS = new TraceState(MD, Options, this);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000516}
517
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000518static size_t InternalStrnlen(const char *S, size_t MaxLen) {
519 size_t Len = 0;
520 for (; Len < MaxLen && S[Len]; Len++) {}
521 return Len;
522}
523
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000524} // namespace fuzzer
525
Kostya Serebryany22526252015-05-11 21:16:27 +0000526using fuzzer::TS;
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000527using fuzzer::RecordingTraces;
528using fuzzer::RecordingMemcmp;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000529
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000530extern "C" {
531void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
532 uint64_t Arg2, dfsan_label L0,
533 dfsan_label L1, dfsan_label L2) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000534 if (!RecordingTraces) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000535 assert(L0 == 0);
536 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
537 uint64_t CmpSize = (SizeAndType >> 32) / 8;
538 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany22526252015-05-11 21:16:27 +0000539 TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000540}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000541
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000542void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
543 dfsan_label L1, dfsan_label L2) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000544 if (!RecordingTraces) return;
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000545 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
546 TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1);
547}
548
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000549void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
550 size_t n, dfsan_label s1_label,
551 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000552 if (!RecordingMemcmp) return;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000553 dfsan_label L1 = dfsan_read_label(s1, n);
554 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000555 TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
556 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000557}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000558
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000559void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2,
560 size_t n, dfsan_label s1_label,
561 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000562 if (!RecordingMemcmp) return;
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000563 n = std::min(n, fuzzer::InternalStrnlen(s1, n));
564 n = std::min(n, fuzzer::InternalStrnlen(s2, n));
Kostya Serebryanyc9dc96b2015-07-30 21:22:22 +0000565 dfsan_label L1 = dfsan_read_label(s1, n);
566 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000567 TS->DFSanMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
568 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000569}
570
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000571void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2,
572 dfsan_label s1_label, dfsan_label s2_label) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000573 if (!RecordingMemcmp) return;
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000574 size_t Len1 = strlen(s1);
575 size_t Len2 = strlen(s2);
576 size_t N = std::min(Len1, Len2);
577 if (N <= 1) return; // Not interesting.
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000578 dfsan_label L1 = dfsan_read_label(s1, Len1);
579 dfsan_label L2 = dfsan_read_label(s2, Len2);
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000580 TS->DFSanMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
581 reinterpret_cast<const uint8_t *>(s2), L1, L2);
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000582}
583
Kostya Serebryany4b83a4f2016-01-12 16:50:18 +0000584// We may need to avoid defining weak hooks to stay compatible with older clang.
585#ifndef LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
586# define LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS 1
587#endif
588
589#if LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000590void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
Kostya Serebryanye3580952016-01-12 00:43:42 +0000591 const void *s2, size_t n, int result) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000592 if (!RecordingMemcmp) return;
Kostya Serebryanye3580952016-01-12 00:43:42 +0000593 if (result == 0) return; // No reason to mutate.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000594 if (n <= 1) return; // Not interesting.
595 TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
596 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryanyb74ba422015-07-30 02:33:45 +0000597}
598
599void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
Kostya Serebryanye3580952016-01-12 00:43:42 +0000600 const char *s2, size_t n, int result) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000601 if (!RecordingMemcmp) return;
Kostya Serebryanye3580952016-01-12 00:43:42 +0000602 if (result == 0) return; // No reason to mutate.
Kostya Serebryanycd6a4662015-07-31 17:05:05 +0000603 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
604 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
605 n = std::min(n, Len1);
606 n = std::min(n, Len2);
607 if (n <= 1) return; // Not interesting.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000608 TS->TraceMemcmpCallback(n, reinterpret_cast<const uint8_t *>(s1),
609 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryany0e776a22015-07-30 01:34:58 +0000610}
611
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000612void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
Kostya Serebryanye3580952016-01-12 00:43:42 +0000613 const char *s2, int result) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000614 if (!RecordingMemcmp) return;
Kostya Serebryanye3580952016-01-12 00:43:42 +0000615 if (result == 0) return; // No reason to mutate.
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000616 size_t Len1 = strlen(s1);
617 size_t Len2 = strlen(s2);
618 size_t N = std::min(Len1, Len2);
619 if (N <= 1) return; // Not interesting.
Kostya Serebryanyc5733162016-01-09 01:39:55 +0000620 TS->TraceMemcmpCallback(N, reinterpret_cast<const uint8_t *>(s1),
621 reinterpret_cast<const uint8_t *>(s2));
Kostya Serebryany7f4227d2015-08-05 18:23:01 +0000622}
623
Kostya Serebryanyc135b552016-07-15 23:27:19 +0000624void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
625 const char *s2, size_t n, int result) {
626 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
627}
628void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
629 const char *s2, int result) {
630 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
631}
632void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
633 const char *s2, char *result) {
634 TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
635}
636void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
637 const char *s2, char *result) {
638 TS->AddInterestingWord(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
639}
640void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
641 const void *s2, size_t len2, void *result) {
642 // TODO: can't hook memmem since memmem is used by libFuzzer.
643}
644
Kostya Serebryany4b83a4f2016-01-12 16:50:18 +0000645#endif // LLVM_FUZZER_DEFINES_SANITIZER_WEAK_HOOOKS
646
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000647__attribute__((visibility("default")))
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000648void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
649 uint64_t Arg2) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000650 if (!RecordingTraces) return;
Kostya Serebryany35959592015-07-28 00:59:53 +0000651 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000652 uint64_t CmpSize = (SizeAndType >> 32) / 8;
653 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany35959592015-07-28 00:59:53 +0000654 TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000655}
656
Kostya Serebryany3287d7a2015-09-30 22:22:37 +0000657__attribute__((visibility("default")))
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000658void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyae5b9562016-01-15 06:24:05 +0000659 if (!RecordingTraces) return;
Kostya Serebryanyfb7d8d92015-07-31 01:33:06 +0000660 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
661 TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2);
662}
663
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000664} // extern "C"