kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 1 | /*===- DataFlow.cpp - a standalone DataFlow tracer -------===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 6 | // |
| 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 | // |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 16 | // The output shows which functions depend on which bytes of the input, |
| 17 | // and also provides basic-block coverage for every input. |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 18 | // |
| 19 | // Build: |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 20 | // 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 |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 23 | // -fsanitize-coverage=trace-pc-guard,pc-table,bb,trace-cmp |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 24 | // 4. Link those together with -fsanitize=dataflow |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 25 | // |
| 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 | // |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 31 | // -fsanitize-coverage=trace-pc-guard,pc-table,bb instruments function |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 32 | // entries so that the comparison callback knows that current function. |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 33 | // -fsanitize-coverage=...,bb also allows to collect basic block coverage. |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 34 | // |
| 35 | // |
| 36 | // Run: |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 37 | // # Collect data flow and coverage for INPUT_FILE |
| 38 | // # write to OUTPUT_FILE (default: stdout) |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 39 | // export DFSAN_OPTIONS=fast16labels=1:warn_unimplemented=0 |
| 40 | // ./a.out INPUT_FILE [OUTPUT_FILE] |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 41 | // |
| 42 | // # Print all instrumented functions. llvm-symbolizer must be present in PATH |
| 43 | // ./a.out |
| 44 | // |
| 45 | // Example output: |
| 46 | // =============== |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 47 | // F0 11111111111111 |
| 48 | // F1 10000000000000 |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 49 | // C0 1 2 3 4 5 |
| 50 | // C1 8 |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 51 | // =============== |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 52 | // "FN xxxxxxxxxx": tells what bytes of the input does the function N depend on. |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 53 | // "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. |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 56 | // |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===*/ |
| 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 | |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 67 | #include "DataFlow.h" |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 68 | |
| 69 | extern "C" { |
| 70 | extern int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size); |
| 71 | __attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv); |
| 72 | } // extern "C" |
| 73 | |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 74 | CallbackData __dft; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 75 | static size_t InputLen; |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 76 | static size_t NumIterations; |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 77 | static dfsan_label **FuncLabelsPerIter; // NumIterations x NumFuncs; |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 78 | |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 79 | static inline bool BlockIsEntry(size_t BlockIdx) { |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 80 | return __dft.PCsBeg[BlockIdx * 2 + 1] & PCFLAG_FUNC_ENTRY; |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 81 | } |
| 82 | |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 83 | const int kNumLabels = 16; |
| 84 | |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 85 | // Prints all instrumented functions. |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 86 | static int PrintFunctions() { |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 87 | // 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\\$' " |
kcc | b9f51dc | 2019-06-20 01:48:45 +0000 | [diff] [blame] | 94 | "| sed 's/dfs\\$//g' " |
| 95 | "| c++filt", |
| 96 | "w"); |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 97 | for (size_t I = 0; I < __dft.NumGuards; I++) { |
| 98 | uintptr_t PC = __dft.PCsBeg[I * 2]; |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 99 | if (!BlockIsEntry(I)) continue; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 100 | void *const Buf[1] = {(void*)PC}; |
| 101 | backtrace_symbols_fd(Buf, 1, fileno(Pipe)); |
| 102 | } |
| 103 | pclose(Pipe); |
| 104 | return 0; |
| 105 | } |
| 106 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 107 | static 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); |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void PrintDataFlow(FILE *Out) { |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 117 | for (size_t Func = 0; Func < __dft.NumFuncs; Func++) { |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 118 | bool HasAny = false; |
| 119 | for (size_t Iter = 0; Iter < NumIterations; Iter++) |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 120 | if (FuncLabelsPerIter[Iter][Func]) |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 121 | 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++) |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 129 | PrintBinary(Out, FuncLabelsPerIter[Iter][Func], |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 130 | Iter == NumIterations - 1 ? LenOfLastIteration : kNumLabels); |
| 131 | fprintf(Out, "\n"); |
| 132 | } |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 133 | } |
| 134 | |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 135 | static void PrintCoverage(FILE *Out) { |
| 136 | ssize_t CurrentFuncGuard = -1; |
| 137 | ssize_t CurrentFuncNum = -1; |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 138 | ssize_t NumBlocksInCurrentFunc = -1; |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 139 | for (size_t FuncBeg = 0; FuncBeg < __dft.NumGuards;) { |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 140 | CurrentFuncNum++; |
| 141 | assert(BlockIsEntry(FuncBeg)); |
| 142 | size_t FuncEnd = FuncBeg + 1; |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 143 | for (; FuncEnd < __dft.NumGuards && !BlockIsEntry(FuncEnd); FuncEnd++) |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 144 | ; |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 145 | if (__dft.BBExecuted[FuncBeg]) { |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 146 | fprintf(Out, "C%zd", CurrentFuncNum); |
| 147 | for (size_t I = FuncBeg + 1; I < FuncEnd; I++) |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 148 | if (__dft.BBExecuted[I]) |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 149 | fprintf(Out, " %zd", I - FuncBeg); |
| 150 | fprintf(Out, " %zd\n", FuncEnd - FuncBeg); |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 151 | } |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 152 | FuncBeg = FuncEnd; |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 153 | } |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 154 | } |
| 155 | |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 156 | int main(int argc, char **argv) { |
| 157 | if (LLVMFuzzerInitialize) |
| 158 | LLVMFuzzerInitialize(&argc, &argv); |
| 159 | if (argc == 1) |
| 160 | return PrintFunctions(); |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 161 | assert(argc == 2 || argc == 3); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 162 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 163 | const char *Input = argv[1]; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 164 | 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); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 173 | fclose(In); |
| 174 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 175 | NumIterations = (NumBytesRead + kNumLabels - 1) / kNumLabels; |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 176 | 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)); |
hans | 39ed034 | 2019-06-14 07:32:22 +0000 | [diff] [blame] | 181 | |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 182 | for (size_t Iter = 0; Iter < NumIterations; Iter++) { |
| 183 | fprintf(stderr, "INFO: running '%s' %zd/%zd\n", Input, Iter, NumIterations); |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 184 | dfsan_flush(); |
| 185 | dfsan_set_label(0, Buf, InputLen); |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 186 | __dft.FuncLabels = FuncLabelsPerIter[Iter]; |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 187 | |
kcc | f45db18 | 2019-06-21 01:39:35 +0000 | [diff] [blame] | 188 | size_t BaseIdx = Iter * kNumLabels; |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 189 | 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 | } |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 196 | free(Buf); |
| 197 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 198 | bool OutIsStdout = argc == 2; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 199 | fprintf(stderr, "INFO: writing dataflow to %s\n", |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 200 | OutIsStdout ? "<stdout>" : argv[2]); |
| 201 | FILE *Out = OutIsStdout ? stdout : fopen(argv[2], "w"); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 202 | PrintDataFlow(Out); |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 203 | PrintCoverage(Out); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 204 | if (!OutIsStdout) fclose(Out); |
| 205 | } |