blob: a79c796ac456c9f0ca06315fd517dec65233a052 [file] [log] [blame]
kccdbf20a02018-05-10 19:59:01 +00001/*===- DataFlow.cpp - a standalone DataFlow tracer -------===//
2//
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//===----------------------------------------------------------------------===//
9// An experimental data-flow tracer for fuzz targets.
10// It is based on DFSan and SanitizerCoverage.
11// https://clang.llvm.org/docs/DataFlowSanitizer.html
12// https://clang.llvm.org/docs/SanitizerCoverage.html#tracing-data-flow
13//
14// It executes the fuzz target on the given input while monitoring the
15// data flow for every instrumented comparison instruction.
16//
17// The output shows which functions depend on which bytes of the input.
18//
19// Build:
20// 1. Compile this file with -fsanitize=dataflow
21// 2. Build the fuzz target with -g -fsanitize=dataflow
22// -fsanitize-coverage=trace-pc-guard,pc-table,func,trace-cmp
23// 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//
30// -fsanitize-coverage=trace-pc-guard,pc-table,func instruments function
31// entries so that the comparison callback knows that current function.
32//
33//
34// Run:
35// # Collect data flow for INPUT_FILE, write to OUTPUT_FILE (default: stdout)
36// ./a.out INPUT_FILE [OUTPUT_FILE]
37//
38// # Print all instrumented functions. llvm-symbolizer must be present in PATH
39// ./a.out
40//
41// Example output:
42// ===============
kcc54dce2c2018-05-23 20:57:11 +000043// F0 11111111111111
44// F1 10000000000000
kccdbf20a02018-05-10 19:59:01 +000045// ===============
kcc54dce2c2018-05-23 20:57:11 +000046// "FN xxxxxxxxxx": tells what bytes of the input does the function N depend on.
47// The byte string is LEN+1 bytes. The last byte is set if the function
48// depends on the input length.
kccdbf20a02018-05-10 19:59:01 +000049//===----------------------------------------------------------------------===*/
50
51#include <assert.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <stdint.h>
55#include <string.h>
56
57#include <execinfo.h> // backtrace_symbols_fd
58
59#include <sanitizer/dfsan_interface.h>
60
61extern "C" {
62extern int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size);
63__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
64} // extern "C"
65
66static size_t InputLen;
67static size_t NumFuncs;
68static const uintptr_t *FuncsBeg;
69static __thread size_t CurrentFunc;
70static dfsan_label *FuncLabels; // Array of NumFuncs elements.
kcc54dce2c2018-05-23 20:57:11 +000071static char *PrintableStringForLabel; // InputLen + 2 bytes.
kcc86e43882018-06-06 01:23:29 +000072static bool LabelSeen[1 << 8 * sizeof(dfsan_label)];
kccdbf20a02018-05-10 19:59:01 +000073
74// Prints all instrumented functions.
kcc54dce2c2018-05-23 20:57:11 +000075static int PrintFunctions() {
kccdbf20a02018-05-10 19:59:01 +000076 // We don't have the symbolizer integrated with dfsan yet.
77 // So use backtrace_symbols_fd and pipe it through llvm-symbolizer.
78 // TODO(kcc): this is pretty ugly and may break in lots of ways.
79 // We'll need to make a proper in-process symbolizer work with DFSan.
80 FILE *Pipe = popen("sed 's/(+/ /g; s/).*//g' "
81 "| llvm-symbolizer "
82 "| grep 'dfs\\$' "
83 "| sed 's/dfs\\$//g'", "w");
84 for (size_t I = 0; I < NumFuncs; I++) {
85 uintptr_t PC = FuncsBeg[I * 2];
86 void *const Buf[1] = {(void*)PC};
87 backtrace_symbols_fd(Buf, 1, fileno(Pipe));
88 }
89 pclose(Pipe);
90 return 0;
91}
92
kcc86e43882018-06-06 01:23:29 +000093extern "C"
94void SetBytesForLabel(dfsan_label L, char *Bytes) {
95 if (LabelSeen[L])
96 return;
97 LabelSeen[L] = true;
kccb8a127f2018-05-23 23:55:54 +000098 assert(L);
99 if (L <= InputLen + 1) {
100 Bytes[L - 1] = '1';
kcc54dce2c2018-05-23 20:57:11 +0000101 } else {
kccdbf20a02018-05-10 19:59:01 +0000102 auto *DLI = dfsan_get_label_info(L);
kcc54dce2c2018-05-23 20:57:11 +0000103 SetBytesForLabel(DLI->l1, Bytes);
104 SetBytesForLabel(DLI->l2, Bytes);
kccdbf20a02018-05-10 19:59:01 +0000105 }
kcc54dce2c2018-05-23 20:57:11 +0000106}
107
108static char *GetPrintableStringForLabel(dfsan_label L) {
109 memset(PrintableStringForLabel, '0', InputLen + 1);
110 PrintableStringForLabel[InputLen + 1] = 0;
kcc86e43882018-06-06 01:23:29 +0000111 memset(LabelSeen, 0, sizeof(LabelSeen));
kcc54dce2c2018-05-23 20:57:11 +0000112 SetBytesForLabel(L, PrintableStringForLabel);
113 return PrintableStringForLabel;
114}
115
116static void PrintDataFlow(FILE *Out) {
kccdbf20a02018-05-10 19:59:01 +0000117 for (size_t I = 0; I < NumFuncs; I++)
118 if (FuncLabels[I])
kcc54dce2c2018-05-23 20:57:11 +0000119 fprintf(Out, "F%zd %s\n", I, GetPrintableStringForLabel(FuncLabels[I]));
kccdbf20a02018-05-10 19:59:01 +0000120}
121
122int main(int argc, char **argv) {
123 if (LLVMFuzzerInitialize)
124 LLVMFuzzerInitialize(&argc, &argv);
125 if (argc == 1)
126 return PrintFunctions();
kcc0a250e22018-05-24 01:43:48 +0000127 assert(argc == 4 || argc == 5);
128 size_t Beg = atoi(argv[1]);
129 size_t End = atoi(argv[2]);
130 assert(Beg < End);
kccdbf20a02018-05-10 19:59:01 +0000131
kcc0a250e22018-05-24 01:43:48 +0000132 const char *Input = argv[3];
kccdbf20a02018-05-10 19:59:01 +0000133 fprintf(stderr, "INFO: reading '%s'\n", Input);
134 FILE *In = fopen(Input, "r");
135 assert(In);
136 fseek(In, 0, SEEK_END);
137 InputLen = ftell(In);
138 fseek(In, 0, SEEK_SET);
139 unsigned char *Buf = (unsigned char*)malloc(InputLen);
140 size_t NumBytesRead = fread(Buf, 1, InputLen, In);
141 assert(NumBytesRead == InputLen);
kcc54dce2c2018-05-23 20:57:11 +0000142 PrintableStringForLabel = (char*)malloc(InputLen + 2);
kccdbf20a02018-05-10 19:59:01 +0000143 fclose(In);
144
145 fprintf(stderr, "INFO: running '%s'\n", Input);
146 for (size_t I = 1; I <= InputLen; I++) {
147 dfsan_label L = dfsan_create_label("", nullptr);
148 assert(L == I);
kcc0a250e22018-05-24 01:43:48 +0000149 size_t Idx = I - 1;
150 if (Idx >= Beg && Idx < End)
151 dfsan_set_label(L, Buf + Idx, 1);
kccdbf20a02018-05-10 19:59:01 +0000152 }
153 dfsan_label SizeL = dfsan_create_label("", nullptr);
154 assert(SizeL == InputLen + 1);
155 dfsan_set_label(SizeL, &InputLen, sizeof(InputLen));
156
157 LLVMFuzzerTestOneInput(Buf, InputLen);
158 free(Buf);
159
kcc0a250e22018-05-24 01:43:48 +0000160 bool OutIsStdout = argc == 4;
kccdbf20a02018-05-10 19:59:01 +0000161 fprintf(stderr, "INFO: writing dataflow to %s\n",
kcc0a250e22018-05-24 01:43:48 +0000162 OutIsStdout ? "<stdout>" : argv[4]);
163 FILE *Out = OutIsStdout ? stdout : fopen(argv[4], "w");
kccdbf20a02018-05-10 19:59:01 +0000164 PrintDataFlow(Out);
165 if (!OutIsStdout) fclose(Out);
166}
167
168extern "C" {
169
170void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
171 uint32_t *stop) {
172 assert(NumFuncs == 0 && "This tool does not support DSOs");
173 assert(start < stop && "The code is not instrumented for coverage");
174 if (start == stop || *start) return; // Initialize only once.
175 for (uint32_t *x = start; x < stop; x++)
176 *x = ++NumFuncs; // The first index is 1.
177 FuncLabels = (dfsan_label*)calloc(NumFuncs, sizeof(dfsan_label));
178 fprintf(stderr, "INFO: %zd instrumented function(s) observed\n", NumFuncs);
179}
180
181void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
182 const uintptr_t *pcs_end) {
183 assert(NumFuncs == (pcs_end - pcs_beg) / 2);
184 FuncsBeg = pcs_beg;
185}
186
187void __sanitizer_cov_trace_pc_indir(uint64_t x){} // unused.
188
189void __sanitizer_cov_trace_pc_guard(uint32_t *guard){
190 uint32_t FuncNum = *guard - 1; // Guards start from 1.
191 assert(FuncNum < NumFuncs);
192 CurrentFunc = FuncNum;
193}
194
195void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
196 dfsan_label L1, dfsan_label UnusedL) {
197 assert(CurrentFunc < NumFuncs);
198 FuncLabels[CurrentFunc] = dfsan_union(FuncLabels[CurrentFunc], L1);
199}
200
201#define HOOK(Name, Type) \
202 void Name(Type Arg1, Type Arg2, dfsan_label L1, dfsan_label L2) { \
203 assert(CurrentFunc < NumFuncs); \
204 FuncLabels[CurrentFunc] = \
205 dfsan_union(FuncLabels[CurrentFunc], dfsan_union(L1, L2)); \
206 }
207
208HOOK(__dfsw___sanitizer_cov_trace_const_cmp1, uint8_t)
209HOOK(__dfsw___sanitizer_cov_trace_const_cmp2, uint16_t)
210HOOK(__dfsw___sanitizer_cov_trace_const_cmp4, uint32_t)
211HOOK(__dfsw___sanitizer_cov_trace_const_cmp8, uint64_t)
212HOOK(__dfsw___sanitizer_cov_trace_cmp1, uint8_t)
213HOOK(__dfsw___sanitizer_cov_trace_cmp2, uint16_t)
214HOOK(__dfsw___sanitizer_cov_trace_cmp4, uint32_t)
215HOOK(__dfsw___sanitizer_cov_trace_cmp8, uint64_t)
216
217} // extern "C"