blob: 60524a91320554567aa21af1e9d4ca5ab0328b80 [file] [log] [blame]
Kostya Serebryany22526252015-05-11 21:16:27 +00001//===- FuzzerTraceState.cpp - Trace-based fuzzer mutator ------------------===//
Kostya Serebryany16d03bd2015-03-30 22:09:51 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Kostya Serebryany22526252015-05-11 21:16:27 +00009// This file implements a mutation algorithm based on instruction traces and
10// on taint analysis feedback from DFSan.
11//
12// Instruction traces are special hooks inserted by the compiler around
13// interesting instructions. Currently supported traces:
14// * __sanitizer_cov_trace_cmp -- inserted before every ICMP instruction,
15// receives the type, size and arguments of ICMP.
16//
17// Every time a traced event is intercepted we analyse the data involved
18// in the event and suggest a mutation for future executions.
19// For example if 4 bytes of data that derive from input bytes {4,5,6,7}
20// are compared with a constant 12345,
21// we try to insert 12345, 12344, 12346 into bytes
22// {4,5,6,7} of the next fuzzed inputs.
23//
24// The fuzzer can work only with the traces, or with both traces and DFSan.
25//
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000026// DataFlowSanitizer (DFSan) is a tool for
27// generalised dynamic data flow (taint) analysis:
28// http://clang.llvm.org/docs/DataFlowSanitizer.html .
29//
Kostya Serebryany22526252015-05-11 21:16:27 +000030// The approach with DFSan-based fuzzing has some similarity to
31// "Taint-based Directed Whitebox Fuzzing"
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000032// by Vijay Ganesh & Tim Leek & Martin Rinard:
33// http://dspace.mit.edu/openaccess-disseminate/1721.1/59320,
34// but it uses a full blown LLVM IR taint analysis and separate instrumentation
35// to analyze all of the "attack points" at once.
36//
Kostya Serebryany22526252015-05-11 21:16:27 +000037// Workflow with DFSan:
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000038// * lib/Fuzzer/Fuzzer*.cpp is compiled w/o any instrumentation.
Kostya Serebryany22526252015-05-11 21:16:27 +000039// * The code under test is compiled with DFSan *and* with instruction traces.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000040// * Every call to HOOK(a,b) is replaced by DFSan with
41// __dfsw_HOOK(a, b, label(a), label(b)) so that __dfsw_HOOK
42// gets all the taint labels for the arguments.
43// * At the Fuzzer startup we assign a unique DFSan label
44// to every byte of the input string (Fuzzer::CurrentUnit) so that for any
45// chunk of data we know which input bytes it has derived from.
46// * The __dfsw_* functions (implemented in this file) record the
47// parameters (i.e. the application data and the corresponding taint labels)
48// in a global state.
Kostya Serebryany22526252015-05-11 21:16:27 +000049// * Fuzzer::ApplyTraceBasedMutation() tries to use the data recorded
50// by __dfsw_* hooks to guide the fuzzing towards new application states.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000051//
Kostya Serebryany22526252015-05-11 21:16:27 +000052// Parts of this code will not function when DFSan is not linked in.
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000053// Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer
54// we redeclare the dfsan_* interface functions as weak and check if they
55// are nullptr before calling.
56// If this approach proves to be useful we may add attribute(weak) to the
57// dfsan declarations in dfsan_interface.h
58//
59// This module is in the "proof of concept" stage.
60// It is capable of solving only the simplest puzzles
61// like test/dfsan/DFSanSimpleCmpTest.cpp.
62//===----------------------------------------------------------------------===//
63
Kostya Serebryany22526252015-05-11 21:16:27 +000064/* Example of manual usage (-fsanitize=dataflow is optional):
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000065(
66 cd $LLVM/lib/Fuzzer/
67 clang -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp
Alexey Samsonov21a33812015-05-07 23:33:24 +000068 clang++ -O0 -std=c++11 -fsanitize-coverage=edge,trace-cmp \
Kostya Serebryany3befe942015-05-06 22:47:24 +000069 -fsanitize=dataflow \
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000070 test/dfsan/DFSanSimpleCmpTest.cpp Fuzzer*.o
71 ./a.out
72)
73*/
74
75#include "FuzzerInternal.h"
76#include <sanitizer/dfsan_interface.h>
77
Kostya Serebryanybeb24c32015-05-07 21:02:11 +000078#include <algorithm>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000079#include <cstring>
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000080#include <unordered_map>
81
82extern "C" {
83__attribute__((weak))
84dfsan_label dfsan_create_label(const char *desc, void *userdata);
85__attribute__((weak))
86void dfsan_set_label(dfsan_label label, void *addr, size_t size);
87__attribute__((weak))
88void dfsan_add_label(dfsan_label label, void *addr, size_t size);
89__attribute__((weak))
90const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label);
Kostya Serebryanya407dde2015-05-07 00:11:33 +000091__attribute__((weak))
92dfsan_label dfsan_read_label(const void *addr, size_t size);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +000093} // extern "C"
94
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +000095namespace fuzzer {
96
97static bool ReallyHaveDFSan() {
98 return &dfsan_create_label != nullptr;
99}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000100
101// These values are copied from include/llvm/IR/InstrTypes.h.
102// We do not include the LLVM headers here to remain independent.
103// If these values ever change, an assertion in ComputeCmp will fail.
104enum Predicate {
105 ICMP_EQ = 32, ///< equal
106 ICMP_NE = 33, ///< not equal
107 ICMP_UGT = 34, ///< unsigned greater than
108 ICMP_UGE = 35, ///< unsigned greater or equal
109 ICMP_ULT = 36, ///< unsigned less than
110 ICMP_ULE = 37, ///< unsigned less or equal
111 ICMP_SGT = 38, ///< signed greater than
112 ICMP_SGE = 39, ///< signed greater or equal
113 ICMP_SLT = 40, ///< signed less than
114 ICMP_SLE = 41, ///< signed less or equal
115};
116
117template <class U, class S>
118bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) {
119 switch(CmpType) {
120 case ICMP_EQ : return Arg1 == Arg2;
121 case ICMP_NE : return Arg1 != Arg2;
122 case ICMP_UGT: return Arg1 > Arg2;
123 case ICMP_UGE: return Arg1 >= Arg2;
124 case ICMP_ULT: return Arg1 < Arg2;
125 case ICMP_ULE: return Arg1 <= Arg2;
126 case ICMP_SGT: return (S)Arg1 > (S)Arg2;
127 case ICMP_SGE: return (S)Arg1 >= (S)Arg2;
128 case ICMP_SLT: return (S)Arg1 < (S)Arg2;
129 case ICMP_SLE: return (S)Arg1 <= (S)Arg2;
130 default: assert(0 && "unsupported CmpType");
131 }
132 return false;
133}
134
135static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1,
136 uint64_t Arg2) {
137 if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2);
138 if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2);
139 if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2);
140 if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2);
141 assert(0 && "unsupported type size");
142 return true;
143}
144
145// As a simplification we use the range of input bytes instead of a set of input
146// bytes.
147struct LabelRange {
148 uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range.
149
150 LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {}
151
152 static LabelRange Join(LabelRange LR1, LabelRange LR2) {
153 if (LR1.Beg == LR1.End) return LR2;
154 if (LR2.Beg == LR2.End) return LR1;
155 return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)};
156 }
157 LabelRange &Join(LabelRange LR) {
158 return *this = Join(*this, LR);
159 }
160 static LabelRange Singleton(const dfsan_label_info *LI) {
161 uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata;
162 assert(Idx > 0);
163 return {(uint16_t)(Idx - 1), Idx};
164 }
165};
166
Kostya Serebryany35959592015-07-28 00:59:53 +0000167// A passport for a CMP site. We want to keep track of where the given CMP is
168// and how many times it is evaluated to true or false.
169struct CmpSitePassport {
170 uintptr_t PC;
171 size_t Counter[2];
172
173 bool IsInterestingCmpTarget() {
174 static const size_t kRareEnough = 50;
175 size_t C0 = Counter[0];
176 size_t C1 = Counter[1];
177 return C0 > kRareEnough * (C1 + 1) || C1 > kRareEnough * (C0 + 1);
178 }
179};
180
181// For now, just keep a simple imprecise hash table PC => CmpSitePassport.
182// Potentially, will need to have a compiler support to have a precise mapping
183// and also thread-safety.
184struct CmpSitePassportTable {
185 static const size_t kSize = 99991; // Prime.
186 CmpSitePassport Passports[kSize];
187
188 CmpSitePassport *GetPassport(uintptr_t PC) {
189 uintptr_t Idx = PC & kSize;
190 CmpSitePassport *Res = &Passports[Idx];
191 if (Res->PC == 0) // Not thread safe.
192 Res->PC = PC;
193 return Res->PC == PC ? Res : nullptr;
194 }
195};
196
197static CmpSitePassportTable CSPTable; // Zero initialized.
198
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000199// For now, very simple: put Size bytes of Data at position Pos.
200struct TraceBasedMutation {
201 size_t Pos;
202 size_t Size;
203 uint64_t Data;
204};
205
Kostya Serebryany22526252015-05-11 21:16:27 +0000206class TraceState {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000207 public:
Kostya Serebryany22526252015-05-11 21:16:27 +0000208 TraceState(const Fuzzer::FuzzingOptions &Options, const Unit &CurrentUnit)
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000209 : Options(Options), CurrentUnit(CurrentUnit) {}
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000210
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000211 LabelRange GetLabelRange(dfsan_label L);
212 void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
213 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
214 dfsan_label L2);
Kostya Serebryany35959592015-07-28 00:59:53 +0000215 void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, uint64_t Arg1,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000216 uint64_t Arg2);
217 int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
218 size_t DataSize);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000219
220 void StartTraceRecording() {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000221 if (!Options.UseTraces) return;
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000222 RecordingTraces = true;
223 Mutations.clear();
224 }
225
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000226 size_t StopTraceRecording(FuzzerRandomBase &Rand) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000227 RecordingTraces = false;
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000228 std::random_shuffle(Mutations.begin(), Mutations.end(), Rand);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000229 return Mutations.size();
230 }
231
232 void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000233
234 private:
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000235 bool IsTwoByteData(uint64_t Data) {
236 int64_t Signed = static_cast<int64_t>(Data);
237 Signed >>= 16;
238 return Signed == 0 || Signed == -1L;
239 }
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000240 bool RecordingTraces = false;
241 std::vector<TraceBasedMutation> Mutations;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000242 LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)] = {};
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000243 const Fuzzer::FuzzingOptions &Options;
244 const Unit &CurrentUnit;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000245};
246
Kostya Serebryany22526252015-05-11 21:16:27 +0000247LabelRange TraceState::GetLabelRange(dfsan_label L) {
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000248 LabelRange &LR = LabelRanges[L];
249 if (LR.Beg < LR.End || L == 0)
250 return LR;
251 const dfsan_label_info *LI = dfsan_get_label_info(L);
252 if (LI->l1 || LI->l2)
253 return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2));
254 return LR = LabelRange::Singleton(LI);
255}
256
Kostya Serebryany22526252015-05-11 21:16:27 +0000257void TraceState::ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U) {
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000258 assert(Idx < Mutations.size());
259 auto &M = Mutations[Idx];
260 if (Options.Verbosity >= 3)
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000261 Printf("TBM %zd %zd %zd\n", M.Pos, M.Size, M.Data);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000262 if (M.Pos + M.Size > U->size()) return;
263 memcpy(U->data() + M.Pos, &M.Data, M.Size);
264}
265
Kostya Serebryany22526252015-05-11 21:16:27 +0000266void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType,
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000267 uint64_t Arg1, uint64_t Arg2, dfsan_label L1,
268 dfsan_label L2) {
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000269 assert(ReallyHaveDFSan());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000270 if (!RecordingTraces) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000271 if (L1 == 0 && L2 == 0)
272 return; // Not actionable.
273 if (L1 != 0 && L2 != 0)
274 return; // Probably still actionable.
275 bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000276 uint64_t Data = L1 ? Arg2 : Arg1;
277 LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000278
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000279 for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) {
280 Mutations.push_back({Pos, CmpSize, Data});
281 Mutations.push_back({Pos, CmpSize, Data + 1});
282 Mutations.push_back({Pos, CmpSize, Data - 1});
283 }
284
285 if (CmpSize > LR.End - LR.Beg)
286 Mutations.push_back({LR.Beg, (unsigned)(LR.End - LR.Beg), Data});
287
288
289 if (Options.Verbosity >= 3)
Kostya Serebryanyae7df1c2015-07-28 01:25:00 +0000290 Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 "
291 "%d MU %zd\n",
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +0000292 PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, Mutations.size());
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000293}
294
Kostya Serebryany22526252015-05-11 21:16:27 +0000295int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000296 size_t DataSize) {
297 int Res = 0;
298 const uint8_t *Beg = CurrentUnit.data();
299 const uint8_t *End = Beg + CurrentUnit.size();
300 for (const uint8_t *Cur = Beg; Cur < End; Cur += DataSize) {
301 Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize);
302 if (!Cur)
303 break;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000304 size_t Pos = Cur - Beg;
305 assert(Pos < CurrentUnit.size());
306 Mutations.push_back({Pos, DataSize, DesiredData});
307 Mutations.push_back({Pos, DataSize, DesiredData + 1});
308 Mutations.push_back({Pos, DataSize, DesiredData - 1});
309 Cur += DataSize;
310 Res++;
311 }
312 return Res;
313}
314
Kostya Serebryany35959592015-07-28 00:59:53 +0000315void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, uint64_t Arg1,
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000316 uint64_t Arg2) {
Kostya Serebryany8817e862015-05-11 23:25:28 +0000317 if (!RecordingTraces) return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000318 int Added = 0;
Kostya Serebryany35959592015-07-28 00:59:53 +0000319 CmpSitePassport *CSP = CSPTable.GetPassport(PC);
320 if (!CSP) return;
321 CSP->Counter[ComputeCmp(CmpSize, CmpType, Arg1, Arg2)]++;
322 size_t C0 = CSP->Counter[0];
323 size_t C1 = CSP->Counter[1];
324 if (!CSP->IsInterestingCmpTarget())
325 return;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000326 if (Options.Verbosity >= 3)
Kostya Serebryany35959592015-07-28 00:59:53 +0000327 Printf("TraceCmp: %p %zd/%zd; %zd %zd\n", CSP->PC, C0, C1, Arg1, Arg2);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000328 Added += TryToAddDesiredData(Arg1, Arg2, CmpSize);
329 Added += TryToAddDesiredData(Arg2, Arg1, CmpSize);
330 if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) {
331 Added += TryToAddDesiredData(Arg1, Arg2, 2);
332 Added += TryToAddDesiredData(Arg2, Arg1, 2);
333 }
334}
335
Kostya Serebryany22526252015-05-11 21:16:27 +0000336static TraceState *TS;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000337
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000338void Fuzzer::StartTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000339 if (!TS) return;
340 TS->StartTraceRecording();
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000341}
342
343size_t Fuzzer::StopTraceRecording() {
Kostya Serebryany22526252015-05-11 21:16:27 +0000344 if (!TS) return 0;
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000345 return TS->StopTraceRecording(USF.GetRand());
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000346}
347
348void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000349 assert(TS);
350 TS->ApplyTraceBasedMutation(Idx, U);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000351}
352
Kostya Serebryany22526252015-05-11 21:16:27 +0000353void Fuzzer::InitializeTraceState() {
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000354 if (!Options.UseTraces) return;
Kostya Serebryany22526252015-05-11 21:16:27 +0000355 TS = new TraceState(Options, CurrentUnit);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000356 CurrentUnit.resize(Options.MaxLen);
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000357 // The rest really requires DFSan.
Kostya Serebryanyd8c54722015-05-12 01:58:34 +0000358 if (!ReallyHaveDFSan()) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000359 for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) {
360 dfsan_label L = dfsan_create_label("input", (void*)(i + 1));
361 // We assume that no one else has called dfsan_create_label before.
362 assert(L == i + 1);
363 dfsan_set_label(L, &CurrentUnit[i], 1);
364 }
365}
366
367} // namespace fuzzer
368
Kostya Serebryany22526252015-05-11 21:16:27 +0000369using fuzzer::TS;
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000370
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000371extern "C" {
372void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
373 uint64_t Arg2, dfsan_label L0,
374 dfsan_label L1, dfsan_label L2) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000375 if (!TS) return;
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000376 assert(L0 == 0);
377 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
378 uint64_t CmpSize = (SizeAndType >> 32) / 8;
379 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany22526252015-05-11 21:16:27 +0000380 TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2);
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000381}
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000382
383void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2,
384 size_t n, dfsan_label s1_label,
385 dfsan_label s2_label, dfsan_label n_label) {
Kostya Serebryany3fe76822015-05-29 20:31:17 +0000386 if (!TS) return;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000387 uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc);
Kostya Serebryanybeb24c32015-05-07 21:02:11 +0000388 uint64_t S1 = 0, S2 = 0;
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000389 // Simplification: handle only first 8 bytes.
390 memcpy(&S1, s1, std::min(n, sizeof(S1)));
391 memcpy(&S2, s2, std::min(n, sizeof(S2)));
392 dfsan_label L1 = dfsan_read_label(s1, n);
393 dfsan_label L2 = dfsan_read_label(s2, n);
Kostya Serebryany22526252015-05-11 21:16:27 +0000394 TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2);
Kostya Serebryanya407dde2015-05-07 00:11:33 +0000395}
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000396
397void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1,
398 uint64_t Arg2) {
Kostya Serebryany22526252015-05-11 21:16:27 +0000399 if (!TS) return;
Kostya Serebryany35959592015-07-28 00:59:53 +0000400 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany5a99ecb2015-05-11 20:51:19 +0000401 uint64_t CmpSize = (SizeAndType >> 32) / 8;
402 uint64_t Type = (SizeAndType << 32) >> 32;
Kostya Serebryany35959592015-07-28 00:59:53 +0000403 TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2);
Kostya Serebryany7d470cf2015-05-07 18:32:29 +0000404}
405
Kostya Serebryany16d03bd2015-03-30 22:09:51 +0000406} // extern "C"