blob: 8bf4e25b8b15b599719a474151ca9fd60b867e42 [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:
kccf45db182019-06-21 01:39:35 +000020// 1. Compile this file (DataFlow.cpp) with -fsanitize=dataflow and -O2.
21// 2. Compile DataFlowCallbacks.cpp with -O2 -fPIC.
22// 3. Build the fuzz target with -g -fsanitize=dataflow
kccce6392a2019-05-08 00:51:15 +000023// -fsanitize-coverage=trace-pc-guard,pc-table,bb,trace-cmp
kccf45db182019-06-21 01:39:35 +000024// 4. Link those together with -fsanitize=dataflow
kccdbf20a02018-05-10 19:59:01 +000025//
26// -fsanitize-coverage=trace-cmp inserts callbacks around every comparison
27// instruction, DFSan modifies the calls to pass the data flow labels.
28// The callbacks update the data flow label for the current function.
29// See e.g. __dfsw___sanitizer_cov_trace_cmp1 below.
30//
kccce6392a2019-05-08 00:51:15 +000031// -fsanitize-coverage=trace-pc-guard,pc-table,bb instruments function
kccdbf20a02018-05-10 19:59:01 +000032// entries so that the comparison callback knows that current function.
kccce6392a2019-05-08 00:51:15 +000033// -fsanitize-coverage=...,bb also allows to collect basic block coverage.
kccdbf20a02018-05-10 19:59:01 +000034//
35//
36// Run:
kccce6392a2019-05-08 00:51:15 +000037// # Collect data flow and coverage for INPUT_FILE
38// # write to OUTPUT_FILE (default: stdout)
kcc0a66b5b2019-06-14 19:54:32 +000039// export DFSAN_OPTIONS=fast16labels=1:warn_unimplemented=0
40// ./a.out INPUT_FILE [OUTPUT_FILE]
kccdbf20a02018-05-10 19:59:01 +000041//
42// # Print all instrumented functions. llvm-symbolizer must be present in PATH
43// ./a.out
44//
45// Example output:
46// ===============
kcc54dce2c2018-05-23 20:57:11 +000047// F0 11111111111111
48// F1 10000000000000
kcc45fa3552019-05-08 17:20:09 +000049// C0 1 2 3 4 5
50// C1 8
kccdbf20a02018-05-10 19:59:01 +000051// ===============
kcc54dce2c2018-05-23 20:57:11 +000052// "FN xxxxxxxxxx": tells what bytes of the input does the function N depend on.
kcc45fa3552019-05-08 17:20:09 +000053// "CN X Y Z T": tells that a function N has basic blocks X, Y, and Z covered
54// in addition to the function's entry block, out of T total instrumented
55// blocks.
kccce6392a2019-05-08 00:51:15 +000056//
kccdbf20a02018-05-10 19:59:01 +000057//===----------------------------------------------------------------------===*/
58
59#include <assert.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <stdint.h>
63#include <string.h>
64
65#include <execinfo.h> // backtrace_symbols_fd
66
kccf45db182019-06-21 01:39:35 +000067#include "DataFlow.h"
kccdbf20a02018-05-10 19:59:01 +000068
69extern "C" {
70extern int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size);
71__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
72} // extern "C"
73
kccf45db182019-06-21 01:39:35 +000074CallbackData __dft;
kccdbf20a02018-05-10 19:59:01 +000075static size_t InputLen;
kcc0a66b5b2019-06-14 19:54:32 +000076static size_t NumIterations;
kccf45db182019-06-21 01:39:35 +000077static dfsan_label **FuncLabelsPerIter; // NumIterations x NumFuncs;
kcc0a66b5b2019-06-14 19:54:32 +000078
kcc45fa3552019-05-08 17:20:09 +000079static inline bool BlockIsEntry(size_t BlockIdx) {
kccf45db182019-06-21 01:39:35 +000080 return __dft.PCsBeg[BlockIdx * 2 + 1] & PCFLAG_FUNC_ENTRY;
kcc45fa3552019-05-08 17:20:09 +000081}
82
kccf45db182019-06-21 01:39:35 +000083const int kNumLabels = 16;
84
kccdbf20a02018-05-10 19:59:01 +000085// Prints all instrumented functions.
kcc54dce2c2018-05-23 20:57:11 +000086static int PrintFunctions() {
kccdbf20a02018-05-10 19:59:01 +000087 // We don't have the symbolizer integrated with dfsan yet.
88 // So use backtrace_symbols_fd and pipe it through llvm-symbolizer.
89 // TODO(kcc): this is pretty ugly and may break in lots of ways.
90 // We'll need to make a proper in-process symbolizer work with DFSan.
91 FILE *Pipe = popen("sed 's/(+/ /g; s/).*//g' "
92 "| llvm-symbolizer "
93 "| grep 'dfs\\$' "
kccb9f51dc2019-06-20 01:48:45 +000094 "| sed 's/dfs\\$//g' "
95 "| c++filt",
96 "w");
kccf45db182019-06-21 01:39:35 +000097 for (size_t I = 0; I < __dft.NumGuards; I++) {
98 uintptr_t PC = __dft.PCsBeg[I * 2];
kcc45fa3552019-05-08 17:20:09 +000099 if (!BlockIsEntry(I)) continue;
kccdbf20a02018-05-10 19:59:01 +0000100 void *const Buf[1] = {(void*)PC};
101 backtrace_symbols_fd(Buf, 1, fileno(Pipe));
102 }
103 pclose(Pipe);
104 return 0;
105}
106
kcc0a66b5b2019-06-14 19:54:32 +0000107static void PrintBinary(FILE *Out, dfsan_label L, size_t Len) {
108 char buf[kNumLabels + 1];
109 assert(Len <= kNumLabels);
110 for (int i = 0; i < kNumLabels; i++)
111 buf[i] = (L & (1 << i)) ? '1' : '0';
112 buf[Len] = 0;
113 fprintf(Out, "%s", buf);
kcc54dce2c2018-05-23 20:57:11 +0000114}
115
116static void PrintDataFlow(FILE *Out) {
kccf45db182019-06-21 01:39:35 +0000117 for (size_t Func = 0; Func < __dft.NumFuncs; Func++) {
kcc0a66b5b2019-06-14 19:54:32 +0000118 bool HasAny = false;
119 for (size_t Iter = 0; Iter < NumIterations; Iter++)
kccf45db182019-06-21 01:39:35 +0000120 if (FuncLabelsPerIter[Iter][Func])
kcc0a66b5b2019-06-14 19:54:32 +0000121 HasAny = true;
122 if (!HasAny)
123 continue;
124 fprintf(Out, "F%zd ", Func);
125 size_t LenOfLastIteration = kNumLabels;
126 if (auto Tail = InputLen % kNumLabels)
127 LenOfLastIteration = Tail;
128 for (size_t Iter = 0; Iter < NumIterations; Iter++)
kccf45db182019-06-21 01:39:35 +0000129 PrintBinary(Out, FuncLabelsPerIter[Iter][Func],
kcc0a66b5b2019-06-14 19:54:32 +0000130 Iter == NumIterations - 1 ? LenOfLastIteration : kNumLabels);
131 fprintf(Out, "\n");
132 }
kccdbf20a02018-05-10 19:59:01 +0000133}
134
kccce6392a2019-05-08 00:51:15 +0000135static void PrintCoverage(FILE *Out) {
136 ssize_t CurrentFuncGuard = -1;
137 ssize_t CurrentFuncNum = -1;
kcc45fa3552019-05-08 17:20:09 +0000138 ssize_t NumBlocksInCurrentFunc = -1;
kccf45db182019-06-21 01:39:35 +0000139 for (size_t FuncBeg = 0; FuncBeg < __dft.NumGuards;) {
kcc45fa3552019-05-08 17:20:09 +0000140 CurrentFuncNum++;
141 assert(BlockIsEntry(FuncBeg));
142 size_t FuncEnd = FuncBeg + 1;
kccf45db182019-06-21 01:39:35 +0000143 for (; FuncEnd < __dft.NumGuards && !BlockIsEntry(FuncEnd); FuncEnd++)
kcc45fa3552019-05-08 17:20:09 +0000144 ;
kccf45db182019-06-21 01:39:35 +0000145 if (__dft.BBExecuted[FuncBeg]) {
kcc45fa3552019-05-08 17:20:09 +0000146 fprintf(Out, "C%zd", CurrentFuncNum);
147 for (size_t I = FuncBeg + 1; I < FuncEnd; I++)
kccf45db182019-06-21 01:39:35 +0000148 if (__dft.BBExecuted[I])
kcc45fa3552019-05-08 17:20:09 +0000149 fprintf(Out, " %zd", I - FuncBeg);
150 fprintf(Out, " %zd\n", FuncEnd - FuncBeg);
kccce6392a2019-05-08 00:51:15 +0000151 }
kcc45fa3552019-05-08 17:20:09 +0000152 FuncBeg = FuncEnd;
kccce6392a2019-05-08 00:51:15 +0000153 }
kccce6392a2019-05-08 00:51:15 +0000154}
155
kccdbf20a02018-05-10 19:59:01 +0000156int main(int argc, char **argv) {
157 if (LLVMFuzzerInitialize)
158 LLVMFuzzerInitialize(&argc, &argv);
159 if (argc == 1)
160 return PrintFunctions();
kcc0a66b5b2019-06-14 19:54:32 +0000161 assert(argc == 2 || argc == 3);
kccdbf20a02018-05-10 19:59:01 +0000162
kcc0a66b5b2019-06-14 19:54:32 +0000163 const char *Input = argv[1];
kccdbf20a02018-05-10 19:59:01 +0000164 fprintf(stderr, "INFO: reading '%s'\n", Input);
165 FILE *In = fopen(Input, "r");
166 assert(In);
167 fseek(In, 0, SEEK_END);
168 InputLen = ftell(In);
169 fseek(In, 0, SEEK_SET);
170 unsigned char *Buf = (unsigned char*)malloc(InputLen);
171 size_t NumBytesRead = fread(Buf, 1, InputLen, In);
172 assert(NumBytesRead == InputLen);
kccdbf20a02018-05-10 19:59:01 +0000173 fclose(In);
174
kcc0a66b5b2019-06-14 19:54:32 +0000175 NumIterations = (NumBytesRead + kNumLabels - 1) / kNumLabels;
kccf45db182019-06-21 01:39:35 +0000176 FuncLabelsPerIter =
177 (dfsan_label **)calloc(NumIterations, sizeof(dfsan_label *));
178 for (size_t Iter = 0; Iter < NumIterations; Iter++)
179 FuncLabelsPerIter[Iter] =
180 (dfsan_label *)calloc(__dft.NumFuncs, sizeof(dfsan_label));
hans39ed0342019-06-14 07:32:22 +0000181
kccf45db182019-06-21 01:39:35 +0000182 for (size_t Iter = 0; Iter < NumIterations; Iter++) {
183 fprintf(stderr, "INFO: running '%s' %zd/%zd\n", Input, Iter, NumIterations);
kcc0a66b5b2019-06-14 19:54:32 +0000184 dfsan_flush();
185 dfsan_set_label(0, Buf, InputLen);
kccf45db182019-06-21 01:39:35 +0000186 __dft.FuncLabels = FuncLabelsPerIter[Iter];
kcc0a66b5b2019-06-14 19:54:32 +0000187
kccf45db182019-06-21 01:39:35 +0000188 size_t BaseIdx = Iter * kNumLabels;
kcc0a66b5b2019-06-14 19:54:32 +0000189 size_t LastIdx = BaseIdx + kNumLabels < NumBytesRead ? BaseIdx + kNumLabels
190 : NumBytesRead;
191 assert(BaseIdx < LastIdx);
192 for (size_t Idx = BaseIdx; Idx < LastIdx; Idx++)
193 dfsan_set_label(1 << (Idx - BaseIdx), Buf + Idx, 1);
194 LLVMFuzzerTestOneInput(Buf, InputLen);
195 }
kccdbf20a02018-05-10 19:59:01 +0000196 free(Buf);
197
kcc0a66b5b2019-06-14 19:54:32 +0000198 bool OutIsStdout = argc == 2;
kccdbf20a02018-05-10 19:59:01 +0000199 fprintf(stderr, "INFO: writing dataflow to %s\n",
kcc0a66b5b2019-06-14 19:54:32 +0000200 OutIsStdout ? "<stdout>" : argv[2]);
201 FILE *Out = OutIsStdout ? stdout : fopen(argv[2], "w");
kccdbf20a02018-05-10 19:59:01 +0000202 PrintDataFlow(Out);
kccce6392a2019-05-08 00:51:15 +0000203 PrintCoverage(Out);
kccdbf20a02018-05-10 19:59:01 +0000204 if (!OutIsStdout) fclose(Out);
205}