blob: c55c68ea9dae21a32bb4bc39e7119d6b0c951d83 [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.
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
kcc54dce2c2018-05-23 20:57:11 +000092static void SetBytesForLabel(dfsan_label L, char *Bytes) {
kccb8a127f2018-05-23 23:55:54 +000093 assert(L);
94 if (L <= InputLen + 1) {
95 Bytes[L - 1] = '1';
kcc54dce2c2018-05-23 20:57:11 +000096 } else {
kccdbf20a02018-05-10 19:59:01 +000097 auto *DLI = dfsan_get_label_info(L);
kcc54dce2c2018-05-23 20:57:11 +000098 SetBytesForLabel(DLI->l1, Bytes);
99 SetBytesForLabel(DLI->l2, Bytes);
kccdbf20a02018-05-10 19:59:01 +0000100 }
kcc54dce2c2018-05-23 20:57:11 +0000101}
102
103static char *GetPrintableStringForLabel(dfsan_label L) {
104 memset(PrintableStringForLabel, '0', InputLen + 1);
105 PrintableStringForLabel[InputLen + 1] = 0;
106 SetBytesForLabel(L, PrintableStringForLabel);
107 return PrintableStringForLabel;
108}
109
110static void PrintDataFlow(FILE *Out) {
kccdbf20a02018-05-10 19:59:01 +0000111 for (size_t I = 0; I < NumFuncs; I++)
112 if (FuncLabels[I])
kcc54dce2c2018-05-23 20:57:11 +0000113 fprintf(Out, "F%zd %s\n", I, GetPrintableStringForLabel(FuncLabels[I]));
kccdbf20a02018-05-10 19:59:01 +0000114}
115
116int main(int argc, char **argv) {
117 if (LLVMFuzzerInitialize)
118 LLVMFuzzerInitialize(&argc, &argv);
119 if (argc == 1)
120 return PrintFunctions();
121 assert(argc == 2 || argc == 3);
122
123 const char *Input = argv[1];
124 fprintf(stderr, "INFO: reading '%s'\n", Input);
125 FILE *In = fopen(Input, "r");
126 assert(In);
127 fseek(In, 0, SEEK_END);
128 InputLen = ftell(In);
129 fseek(In, 0, SEEK_SET);
130 unsigned char *Buf = (unsigned char*)malloc(InputLen);
131 size_t NumBytesRead = fread(Buf, 1, InputLen, In);
132 assert(NumBytesRead == InputLen);
kcc54dce2c2018-05-23 20:57:11 +0000133 PrintableStringForLabel = (char*)malloc(InputLen + 2);
kccdbf20a02018-05-10 19:59:01 +0000134 fclose(In);
135
136 fprintf(stderr, "INFO: running '%s'\n", Input);
137 for (size_t I = 1; I <= InputLen; I++) {
138 dfsan_label L = dfsan_create_label("", nullptr);
139 assert(L == I);
140 dfsan_set_label(L, Buf + I - 1, 1);
141 }
142 dfsan_label SizeL = dfsan_create_label("", nullptr);
143 assert(SizeL == InputLen + 1);
144 dfsan_set_label(SizeL, &InputLen, sizeof(InputLen));
145
146 LLVMFuzzerTestOneInput(Buf, InputLen);
147 free(Buf);
148
149 bool OutIsStdout = argc == 2;
150 fprintf(stderr, "INFO: writing dataflow to %s\n",
151 OutIsStdout ? "<stdout>" : argv[2]);
152 FILE *Out = OutIsStdout ? stdout : fopen(argv[2], "w");
153 PrintDataFlow(Out);
154 if (!OutIsStdout) fclose(Out);
155}
156
157extern "C" {
158
159void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
160 uint32_t *stop) {
161 assert(NumFuncs == 0 && "This tool does not support DSOs");
162 assert(start < stop && "The code is not instrumented for coverage");
163 if (start == stop || *start) return; // Initialize only once.
164 for (uint32_t *x = start; x < stop; x++)
165 *x = ++NumFuncs; // The first index is 1.
166 FuncLabels = (dfsan_label*)calloc(NumFuncs, sizeof(dfsan_label));
167 fprintf(stderr, "INFO: %zd instrumented function(s) observed\n", NumFuncs);
168}
169
170void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
171 const uintptr_t *pcs_end) {
172 assert(NumFuncs == (pcs_end - pcs_beg) / 2);
173 FuncsBeg = pcs_beg;
174}
175
176void __sanitizer_cov_trace_pc_indir(uint64_t x){} // unused.
177
178void __sanitizer_cov_trace_pc_guard(uint32_t *guard){
179 uint32_t FuncNum = *guard - 1; // Guards start from 1.
180 assert(FuncNum < NumFuncs);
181 CurrentFunc = FuncNum;
182}
183
184void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
185 dfsan_label L1, dfsan_label UnusedL) {
186 assert(CurrentFunc < NumFuncs);
187 FuncLabels[CurrentFunc] = dfsan_union(FuncLabels[CurrentFunc], L1);
188}
189
190#define HOOK(Name, Type) \
191 void Name(Type Arg1, Type Arg2, dfsan_label L1, dfsan_label L2) { \
192 assert(CurrentFunc < NumFuncs); \
193 FuncLabels[CurrentFunc] = \
194 dfsan_union(FuncLabels[CurrentFunc], dfsan_union(L1, L2)); \
195 }
196
197HOOK(__dfsw___sanitizer_cov_trace_const_cmp1, uint8_t)
198HOOK(__dfsw___sanitizer_cov_trace_const_cmp2, uint16_t)
199HOOK(__dfsw___sanitizer_cov_trace_const_cmp4, uint32_t)
200HOOK(__dfsw___sanitizer_cov_trace_const_cmp8, uint64_t)
201HOOK(__dfsw___sanitizer_cov_trace_cmp1, uint8_t)
202HOOK(__dfsw___sanitizer_cov_trace_cmp2, uint16_t)
203HOOK(__dfsw___sanitizer_cov_trace_cmp4, uint32_t)
204HOOK(__dfsw___sanitizer_cov_trace_cmp8, uint64_t)
205
206} // extern "C"