blob: 742e956e78470dadce7161d5eb4ab9c711459367 [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//
16// The output shows which functions depend on which bytes of the input.
17//
18// Build:
19// 1. Compile this file with -fsanitize=dataflow
20// 2. Build the fuzz target with -g -fsanitize=dataflow
21// -fsanitize-coverage=trace-pc-guard,pc-table,func,trace-cmp
22// 3. Link those together with -fsanitize=dataflow
23//
24// -fsanitize-coverage=trace-cmp inserts callbacks around every comparison
25// instruction, DFSan modifies the calls to pass the data flow labels.
26// The callbacks update the data flow label for the current function.
27// See e.g. __dfsw___sanitizer_cov_trace_cmp1 below.
28//
29// -fsanitize-coverage=trace-pc-guard,pc-table,func instruments function
30// entries so that the comparison callback knows that current function.
31//
32//
33// Run:
34// # Collect data flow for INPUT_FILE, write to OUTPUT_FILE (default: stdout)
35// ./a.out INPUT_FILE [OUTPUT_FILE]
36//
37// # Print all instrumented functions. llvm-symbolizer must be present in PATH
38// ./a.out
39//
40// Example output:
41// ===============
kcc54dce2c2018-05-23 20:57:11 +000042// F0 11111111111111
43// F1 10000000000000
kccdbf20a02018-05-10 19:59:01 +000044// ===============
kcc54dce2c2018-05-23 20:57:11 +000045// "FN xxxxxxxxxx": tells what bytes of the input does the function N depend on.
46// The byte string is LEN+1 bytes. The last byte is set if the function
47// depends on the input length.
kccdbf20a02018-05-10 19:59:01 +000048//===----------------------------------------------------------------------===*/
49
50#include <assert.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <stdint.h>
54#include <string.h>
55
56#include <execinfo.h> // backtrace_symbols_fd
57
58#include <sanitizer/dfsan_interface.h>
59
60extern "C" {
61extern int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size);
62__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
63} // extern "C"
64
65static size_t InputLen;
66static size_t NumFuncs;
67static const uintptr_t *FuncsBeg;
68static __thread size_t CurrentFunc;
69static dfsan_label *FuncLabels; // Array of NumFuncs elements.
kcc54dce2c2018-05-23 20:57:11 +000070static char *PrintableStringForLabel; // InputLen + 2 bytes.
kcc86e43882018-06-06 01:23:29 +000071static bool LabelSeen[1 << 8 * sizeof(dfsan_label)];
kccdbf20a02018-05-10 19:59:01 +000072
73// Prints all instrumented functions.
kcc54dce2c2018-05-23 20:57:11 +000074static int PrintFunctions() {
kccdbf20a02018-05-10 19:59:01 +000075 // We don't have the symbolizer integrated with dfsan yet.
76 // So use backtrace_symbols_fd and pipe it through llvm-symbolizer.
77 // TODO(kcc): this is pretty ugly and may break in lots of ways.
78 // We'll need to make a proper in-process symbolizer work with DFSan.
79 FILE *Pipe = popen("sed 's/(+/ /g; s/).*//g' "
80 "| llvm-symbolizer "
81 "| grep 'dfs\\$' "
82 "| sed 's/dfs\\$//g'", "w");
83 for (size_t I = 0; I < NumFuncs; I++) {
84 uintptr_t PC = FuncsBeg[I * 2];
85 void *const Buf[1] = {(void*)PC};
86 backtrace_symbols_fd(Buf, 1, fileno(Pipe));
87 }
88 pclose(Pipe);
89 return 0;
90}
91
kcc86e43882018-06-06 01:23:29 +000092extern "C"
93void SetBytesForLabel(dfsan_label L, char *Bytes) {
94 if (LabelSeen[L])
95 return;
96 LabelSeen[L] = true;
kccb8a127f2018-05-23 23:55:54 +000097 assert(L);
98 if (L <= InputLen + 1) {
99 Bytes[L - 1] = '1';
kcc54dce2c2018-05-23 20:57:11 +0000100 } else {
kccdbf20a02018-05-10 19:59:01 +0000101 auto *DLI = dfsan_get_label_info(L);
kcc54dce2c2018-05-23 20:57:11 +0000102 SetBytesForLabel(DLI->l1, Bytes);
103 SetBytesForLabel(DLI->l2, Bytes);
kccdbf20a02018-05-10 19:59:01 +0000104 }
kcc54dce2c2018-05-23 20:57:11 +0000105}
106
107static char *GetPrintableStringForLabel(dfsan_label L) {
108 memset(PrintableStringForLabel, '0', InputLen + 1);
109 PrintableStringForLabel[InputLen + 1] = 0;
kcc86e43882018-06-06 01:23:29 +0000110 memset(LabelSeen, 0, sizeof(LabelSeen));
kcc54dce2c2018-05-23 20:57:11 +0000111 SetBytesForLabel(L, PrintableStringForLabel);
112 return PrintableStringForLabel;
113}
114
115static void PrintDataFlow(FILE *Out) {
kccdbf20a02018-05-10 19:59:01 +0000116 for (size_t I = 0; I < NumFuncs; I++)
117 if (FuncLabels[I])
kcc54dce2c2018-05-23 20:57:11 +0000118 fprintf(Out, "F%zd %s\n", I, GetPrintableStringForLabel(FuncLabels[I]));
kccdbf20a02018-05-10 19:59:01 +0000119}
120
121int main(int argc, char **argv) {
122 if (LLVMFuzzerInitialize)
123 LLVMFuzzerInitialize(&argc, &argv);
124 if (argc == 1)
125 return PrintFunctions();
kcc0a250e22018-05-24 01:43:48 +0000126 assert(argc == 4 || argc == 5);
127 size_t Beg = atoi(argv[1]);
128 size_t End = atoi(argv[2]);
129 assert(Beg < End);
kccdbf20a02018-05-10 19:59:01 +0000130
kcc0a250e22018-05-24 01:43:48 +0000131 const char *Input = argv[3];
kccdbf20a02018-05-10 19:59:01 +0000132 fprintf(stderr, "INFO: reading '%s'\n", Input);
133 FILE *In = fopen(Input, "r");
134 assert(In);
135 fseek(In, 0, SEEK_END);
136 InputLen = ftell(In);
137 fseek(In, 0, SEEK_SET);
138 unsigned char *Buf = (unsigned char*)malloc(InputLen);
139 size_t NumBytesRead = fread(Buf, 1, InputLen, In);
140 assert(NumBytesRead == InputLen);
kcc54dce2c2018-05-23 20:57:11 +0000141 PrintableStringForLabel = (char*)malloc(InputLen + 2);
kccdbf20a02018-05-10 19:59:01 +0000142 fclose(In);
143
144 fprintf(stderr, "INFO: running '%s'\n", Input);
145 for (size_t I = 1; I <= InputLen; I++) {
146 dfsan_label L = dfsan_create_label("", nullptr);
147 assert(L == I);
kcc0a250e22018-05-24 01:43:48 +0000148 size_t Idx = I - 1;
149 if (Idx >= Beg && Idx < End)
150 dfsan_set_label(L, Buf + Idx, 1);
kccdbf20a02018-05-10 19:59:01 +0000151 }
152 dfsan_label SizeL = dfsan_create_label("", nullptr);
153 assert(SizeL == InputLen + 1);
154 dfsan_set_label(SizeL, &InputLen, sizeof(InputLen));
155
156 LLVMFuzzerTestOneInput(Buf, InputLen);
157 free(Buf);
158
kcc0a250e22018-05-24 01:43:48 +0000159 bool OutIsStdout = argc == 4;
kccdbf20a02018-05-10 19:59:01 +0000160 fprintf(stderr, "INFO: writing dataflow to %s\n",
kcc0a250e22018-05-24 01:43:48 +0000161 OutIsStdout ? "<stdout>" : argv[4]);
162 FILE *Out = OutIsStdout ? stdout : fopen(argv[4], "w");
kccdbf20a02018-05-10 19:59:01 +0000163 PrintDataFlow(Out);
164 if (!OutIsStdout) fclose(Out);
165}
166
167extern "C" {
168
169void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
170 uint32_t *stop) {
171 assert(NumFuncs == 0 && "This tool does not support DSOs");
172 assert(start < stop && "The code is not instrumented for coverage");
173 if (start == stop || *start) return; // Initialize only once.
174 for (uint32_t *x = start; x < stop; x++)
175 *x = ++NumFuncs; // The first index is 1.
176 FuncLabels = (dfsan_label*)calloc(NumFuncs, sizeof(dfsan_label));
177 fprintf(stderr, "INFO: %zd instrumented function(s) observed\n", NumFuncs);
178}
179
180void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
181 const uintptr_t *pcs_end) {
182 assert(NumFuncs == (pcs_end - pcs_beg) / 2);
183 FuncsBeg = pcs_beg;
184}
185
186void __sanitizer_cov_trace_pc_indir(uint64_t x){} // unused.
187
188void __sanitizer_cov_trace_pc_guard(uint32_t *guard){
189 uint32_t FuncNum = *guard - 1; // Guards start from 1.
190 assert(FuncNum < NumFuncs);
191 CurrentFunc = FuncNum;
192}
193
194void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
195 dfsan_label L1, dfsan_label UnusedL) {
196 assert(CurrentFunc < NumFuncs);
197 FuncLabels[CurrentFunc] = dfsan_union(FuncLabels[CurrentFunc], L1);
198}
199
200#define HOOK(Name, Type) \
201 void Name(Type Arg1, Type Arg2, dfsan_label L1, dfsan_label L2) { \
202 assert(CurrentFunc < NumFuncs); \
203 FuncLabels[CurrentFunc] = \
204 dfsan_union(FuncLabels[CurrentFunc], dfsan_union(L1, L2)); \
205 }
206
207HOOK(__dfsw___sanitizer_cov_trace_const_cmp1, uint8_t)
208HOOK(__dfsw___sanitizer_cov_trace_const_cmp2, uint16_t)
209HOOK(__dfsw___sanitizer_cov_trace_const_cmp4, uint32_t)
210HOOK(__dfsw___sanitizer_cov_trace_const_cmp8, uint64_t)
211HOOK(__dfsw___sanitizer_cov_trace_cmp1, uint8_t)
212HOOK(__dfsw___sanitizer_cov_trace_cmp2, uint16_t)
213HOOK(__dfsw___sanitizer_cov_trace_cmp4, uint32_t)
214HOOK(__dfsw___sanitizer_cov_trace_cmp8, uint64_t)
215
216} // extern "C"