blob: 4f9c117081298e8b1387a59165f4b78bdf3fb5e2 [file] [log] [blame]
kccdbf20a02018-05-10 19:59:01 +00001/*===- DataFlow.cpp - a standalone DataFlow tracer -------===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
kccdbf20a02018-05-10 19:59:01 +00006//
7//===----------------------------------------------------------------------===//
8// An experimental data-flow tracer for fuzz targets.
9// It is based on DFSan and SanitizerCoverage.
10// https://clang.llvm.org/docs/DataFlowSanitizer.html
11// https://clang.llvm.org/docs/SanitizerCoverage.html#tracing-data-flow
12//
13// It executes the fuzz target on the given input while monitoring the
14// data flow for every instrumented comparison instruction.
15//
kccce6392a2019-05-08 00:51:15 +000016// The output shows which functions depend on which bytes of the input,
17// and also provides basic-block coverage for every input.
kccdbf20a02018-05-10 19:59:01 +000018//
19// Build:
20// 1. Compile this file with -fsanitize=dataflow
21// 2. Build the fuzz target with -g -fsanitize=dataflow
kccce6392a2019-05-08 00:51:15 +000022// -fsanitize-coverage=trace-pc-guard,pc-table,bb,trace-cmp
kccdbf20a02018-05-10 19:59:01 +000023// 3. Link those together with -fsanitize=dataflow
24//
25// -fsanitize-coverage=trace-cmp inserts callbacks around every comparison
26// instruction, DFSan modifies the calls to pass the data flow labels.
27// The callbacks update the data flow label for the current function.
28// See e.g. __dfsw___sanitizer_cov_trace_cmp1 below.
29//
kccce6392a2019-05-08 00:51:15 +000030// -fsanitize-coverage=trace-pc-guard,pc-table,bb instruments function
kccdbf20a02018-05-10 19:59:01 +000031// entries so that the comparison callback knows that current function.
kccce6392a2019-05-08 00:51:15 +000032// -fsanitize-coverage=...,bb also allows to collect basic block coverage.
kccdbf20a02018-05-10 19:59:01 +000033//
34//
35// Run:
kccce6392a2019-05-08 00:51:15 +000036// # Collect data flow and coverage for INPUT_FILE
37// # write to OUTPUT_FILE (default: stdout)
kcc0a66b5b2019-06-14 19:54:32 +000038// export DFSAN_OPTIONS=fast16labels=1:warn_unimplemented=0
39// ./a.out INPUT_FILE [OUTPUT_FILE]
kccdbf20a02018-05-10 19:59:01 +000040//
41// # Print all instrumented functions. llvm-symbolizer must be present in PATH
42// ./a.out
43//
44// Example output:
45// ===============
kcc54dce2c2018-05-23 20:57:11 +000046// F0 11111111111111
47// F1 10000000000000
kcc45fa3552019-05-08 17:20:09 +000048// C0 1 2 3 4 5
49// C1 8
kccdbf20a02018-05-10 19:59:01 +000050// ===============
kcc54dce2c2018-05-23 20:57:11 +000051// "FN xxxxxxxxxx": tells what bytes of the input does the function N depend on.
kcc45fa3552019-05-08 17:20:09 +000052// "CN X Y Z T": tells that a function N has basic blocks X, Y, and Z covered
53// in addition to the function's entry block, out of T total instrumented
54// blocks.
kccce6392a2019-05-08 00:51:15 +000055//
kccdbf20a02018-05-10 19:59:01 +000056//===----------------------------------------------------------------------===*/
57
58#include <assert.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <stdint.h>
62#include <string.h>
63
64#include <execinfo.h> // backtrace_symbols_fd
65
66#include <sanitizer/dfsan_interface.h>
67
68extern "C" {
69extern int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size);
70__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
71} // extern "C"
72
73static size_t InputLen;
kcc0a66b5b2019-06-14 19:54:32 +000074static size_t NumIterations;
kccce6392a2019-05-08 00:51:15 +000075static size_t NumFuncs, NumGuards;
76static uint32_t *GuardsBeg, *GuardsEnd;
77static const uintptr_t *PCsBeg, *PCsEnd;
kcc0a66b5b2019-06-14 19:54:32 +000078static __thread size_t CurrentFunc, CurrentIteration;
79static dfsan_label **FuncLabels; // NumFuncs x NumIterations.
kccce6392a2019-05-08 00:51:15 +000080static bool *BBExecuted; // Array of NumGuards elements.
kccdbf20a02018-05-10 19:59:01 +000081
kccce6392a2019-05-08 00:51:15 +000082enum {
83 PCFLAG_FUNC_ENTRY = 1,
84};
85
kcc0a66b5b2019-06-14 19:54:32 +000086const int kNumLabels = 16;
87
kcc45fa3552019-05-08 17:20:09 +000088static inline bool BlockIsEntry(size_t BlockIdx) {
89 return PCsBeg[BlockIdx * 2 + 1] & PCFLAG_FUNC_ENTRY;
90}
91
kccdbf20a02018-05-10 19:59:01 +000092// Prints all instrumented functions.
kcc54dce2c2018-05-23 20:57:11 +000093static int PrintFunctions() {
kccdbf20a02018-05-10 19:59:01 +000094 // We don't have the symbolizer integrated with dfsan yet.
95 // So use backtrace_symbols_fd and pipe it through llvm-symbolizer.
96 // TODO(kcc): this is pretty ugly and may break in lots of ways.
97 // We'll need to make a proper in-process symbolizer work with DFSan.
98 FILE *Pipe = popen("sed 's/(+/ /g; s/).*//g' "
99 "| llvm-symbolizer "
100 "| grep 'dfs\\$' "
kccb9f51dc2019-06-20 01:48:45 +0000101 "| sed 's/dfs\\$//g' "
102 "| c++filt",
103 "w");
kccce6392a2019-05-08 00:51:15 +0000104 for (size_t I = 0; I < NumGuards; I++) {
105 uintptr_t PC = PCsBeg[I * 2];
kcc45fa3552019-05-08 17:20:09 +0000106 if (!BlockIsEntry(I)) continue;
kccdbf20a02018-05-10 19:59:01 +0000107 void *const Buf[1] = {(void*)PC};
108 backtrace_symbols_fd(Buf, 1, fileno(Pipe));
109 }
110 pclose(Pipe);
111 return 0;
112}
113
kcc0a66b5b2019-06-14 19:54:32 +0000114static void PrintBinary(FILE *Out, dfsan_label L, size_t Len) {
115 char buf[kNumLabels + 1];
116 assert(Len <= kNumLabels);
117 for (int i = 0; i < kNumLabels; i++)
118 buf[i] = (L & (1 << i)) ? '1' : '0';
119 buf[Len] = 0;
120 fprintf(Out, "%s", buf);
kcc54dce2c2018-05-23 20:57:11 +0000121}
122
123static void PrintDataFlow(FILE *Out) {
kcc0a66b5b2019-06-14 19:54:32 +0000124 for (size_t Func = 0; Func < NumFuncs; Func++) {
125 bool HasAny = false;
126 for (size_t Iter = 0; Iter < NumIterations; Iter++)
127 if (FuncLabels[Func][Iter])
128 HasAny = true;
129 if (!HasAny)
130 continue;
131 fprintf(Out, "F%zd ", Func);
132 size_t LenOfLastIteration = kNumLabels;
133 if (auto Tail = InputLen % kNumLabels)
134 LenOfLastIteration = Tail;
135 for (size_t Iter = 0; Iter < NumIterations; Iter++)
136 PrintBinary(Out, FuncLabels[Func][Iter],
137 Iter == NumIterations - 1 ? LenOfLastIteration : kNumLabels);
138 fprintf(Out, "\n");
139 }
kccdbf20a02018-05-10 19:59:01 +0000140}
141
kccce6392a2019-05-08 00:51:15 +0000142static void PrintCoverage(FILE *Out) {
143 ssize_t CurrentFuncGuard = -1;
144 ssize_t CurrentFuncNum = -1;
kcc45fa3552019-05-08 17:20:09 +0000145 ssize_t NumBlocksInCurrentFunc = -1;
146 for (size_t FuncBeg = 0; FuncBeg < NumGuards;) {
147 CurrentFuncNum++;
148 assert(BlockIsEntry(FuncBeg));
149 size_t FuncEnd = FuncBeg + 1;
150 for (; FuncEnd < NumGuards && !BlockIsEntry(FuncEnd); FuncEnd++)
151 ;
152 if (BBExecuted[FuncBeg]) {
153 fprintf(Out, "C%zd", CurrentFuncNum);
154 for (size_t I = FuncBeg + 1; I < FuncEnd; I++)
155 if (BBExecuted[I])
156 fprintf(Out, " %zd", I - FuncBeg);
157 fprintf(Out, " %zd\n", FuncEnd - FuncBeg);
kccce6392a2019-05-08 00:51:15 +0000158 }
kcc45fa3552019-05-08 17:20:09 +0000159 FuncBeg = FuncEnd;
kccce6392a2019-05-08 00:51:15 +0000160 }
kccce6392a2019-05-08 00:51:15 +0000161}
162
kccdbf20a02018-05-10 19:59:01 +0000163int main(int argc, char **argv) {
164 if (LLVMFuzzerInitialize)
165 LLVMFuzzerInitialize(&argc, &argv);
166 if (argc == 1)
167 return PrintFunctions();
kcc0a66b5b2019-06-14 19:54:32 +0000168 assert(argc == 2 || argc == 3);
kccdbf20a02018-05-10 19:59:01 +0000169
kcc0a66b5b2019-06-14 19:54:32 +0000170 const char *Input = argv[1];
kccdbf20a02018-05-10 19:59:01 +0000171 fprintf(stderr, "INFO: reading '%s'\n", Input);
172 FILE *In = fopen(Input, "r");
173 assert(In);
174 fseek(In, 0, SEEK_END);
175 InputLen = ftell(In);
176 fseek(In, 0, SEEK_SET);
177 unsigned char *Buf = (unsigned char*)malloc(InputLen);
178 size_t NumBytesRead = fread(Buf, 1, InputLen, In);
179 assert(NumBytesRead == InputLen);
kccdbf20a02018-05-10 19:59:01 +0000180 fclose(In);
181
kcc0a66b5b2019-06-14 19:54:32 +0000182 NumIterations = (NumBytesRead + kNumLabels - 1) / kNumLabels;
183 FuncLabels = (dfsan_label**)calloc(NumFuncs, sizeof(dfsan_label*));
184 for (size_t Func = 0; Func < NumFuncs; Func++)
185 FuncLabels[Func] =
186 (dfsan_label *)calloc(NumIterations, sizeof(dfsan_label));
hans39ed0342019-06-14 07:32:22 +0000187
kcc0a66b5b2019-06-14 19:54:32 +0000188 for (CurrentIteration = 0; CurrentIteration < NumIterations;
189 CurrentIteration++) {
190 fprintf(stderr, "INFO: running '%s' %zd/%zd\n", Input, CurrentIteration,
191 NumIterations);
192 dfsan_flush();
193 dfsan_set_label(0, Buf, InputLen);
194
195 size_t BaseIdx = CurrentIteration * kNumLabels;
196 size_t LastIdx = BaseIdx + kNumLabels < NumBytesRead ? BaseIdx + kNumLabels
197 : NumBytesRead;
198 assert(BaseIdx < LastIdx);
199 for (size_t Idx = BaseIdx; Idx < LastIdx; Idx++)
200 dfsan_set_label(1 << (Idx - BaseIdx), Buf + Idx, 1);
201 LLVMFuzzerTestOneInput(Buf, InputLen);
202 }
kccdbf20a02018-05-10 19:59:01 +0000203 free(Buf);
204
kcc0a66b5b2019-06-14 19:54:32 +0000205 bool OutIsStdout = argc == 2;
kccdbf20a02018-05-10 19:59:01 +0000206 fprintf(stderr, "INFO: writing dataflow to %s\n",
kcc0a66b5b2019-06-14 19:54:32 +0000207 OutIsStdout ? "<stdout>" : argv[2]);
208 FILE *Out = OutIsStdout ? stdout : fopen(argv[2], "w");
kccdbf20a02018-05-10 19:59:01 +0000209 PrintDataFlow(Out);
kccce6392a2019-05-08 00:51:15 +0000210 PrintCoverage(Out);
kccdbf20a02018-05-10 19:59:01 +0000211 if (!OutIsStdout) fclose(Out);
212}
213
214extern "C" {
215
216void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
217 uint32_t *stop) {
218 assert(NumFuncs == 0 && "This tool does not support DSOs");
219 assert(start < stop && "The code is not instrumented for coverage");
220 if (start == stop || *start) return; // Initialize only once.
kccce6392a2019-05-08 00:51:15 +0000221 GuardsBeg = start;
222 GuardsEnd = stop;
kccdbf20a02018-05-10 19:59:01 +0000223}
224
225void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
226 const uintptr_t *pcs_end) {
kccce6392a2019-05-08 00:51:15 +0000227 if (NumGuards) return; // Initialize only once.
228 NumGuards = GuardsEnd - GuardsBeg;
229 PCsBeg = pcs_beg;
230 PCsEnd = pcs_end;
231 assert(NumGuards == (PCsEnd - PCsBeg) / 2);
232 for (size_t i = 0; i < NumGuards; i++) {
kcc45fa3552019-05-08 17:20:09 +0000233 if (BlockIsEntry(i)) {
kccce6392a2019-05-08 00:51:15 +0000234 NumFuncs++;
235 GuardsBeg[i] = NumFuncs;
236 }
237 }
kccce6392a2019-05-08 00:51:15 +0000238 BBExecuted = (bool*)calloc(NumGuards, sizeof(bool));
239 fprintf(stderr, "INFO: %zd instrumented function(s) observed "
240 "and %zd basic blocks\n", NumFuncs, NumGuards);
kccdbf20a02018-05-10 19:59:01 +0000241}
242
243void __sanitizer_cov_trace_pc_indir(uint64_t x){} // unused.
244
kccce6392a2019-05-08 00:51:15 +0000245void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
246 size_t GuardIdx = guard - GuardsBeg;
247 assert(GuardIdx < NumGuards);
248 BBExecuted[GuardIdx] = true;
249 if (!*guard) return; // not a function entry.
kccdbf20a02018-05-10 19:59:01 +0000250 uint32_t FuncNum = *guard - 1; // Guards start from 1.
251 assert(FuncNum < NumFuncs);
252 CurrentFunc = FuncNum;
253}
254
255void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
256 dfsan_label L1, dfsan_label UnusedL) {
257 assert(CurrentFunc < NumFuncs);
kcc0a66b5b2019-06-14 19:54:32 +0000258 FuncLabels[CurrentFunc][CurrentIteration] |= L1;
kccdbf20a02018-05-10 19:59:01 +0000259}
260
261#define HOOK(Name, Type) \
262 void Name(Type Arg1, Type Arg2, dfsan_label L1, dfsan_label L2) { \
263 assert(CurrentFunc < NumFuncs); \
kcc0a66b5b2019-06-14 19:54:32 +0000264 FuncLabels[CurrentFunc][CurrentIteration] |= L1 | L2; \
kccdbf20a02018-05-10 19:59:01 +0000265 }
266
267HOOK(__dfsw___sanitizer_cov_trace_const_cmp1, uint8_t)
268HOOK(__dfsw___sanitizer_cov_trace_const_cmp2, uint16_t)
269HOOK(__dfsw___sanitizer_cov_trace_const_cmp4, uint32_t)
270HOOK(__dfsw___sanitizer_cov_trace_const_cmp8, uint64_t)
271HOOK(__dfsw___sanitizer_cov_trace_cmp1, uint8_t)
272HOOK(__dfsw___sanitizer_cov_trace_cmp2, uint16_t)
273HOOK(__dfsw___sanitizer_cov_trace_cmp4, uint32_t)
274HOOK(__dfsw___sanitizer_cov_trace_cmp8, uint64_t)
275
276} // extern "C"