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: |
| 20 | // 1. Compile this file with -fsanitize=dataflow |
| 21 | // 2. Build the fuzz target with -g -fsanitize=dataflow |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 22 | // -fsanitize-coverage=trace-pc-guard,pc-table,bb,trace-cmp |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 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 | // |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 30 | // -fsanitize-coverage=trace-pc-guard,pc-table,bb instruments function |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 31 | // entries so that the comparison callback knows that current function. |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 32 | // -fsanitize-coverage=...,bb also allows to collect basic block coverage. |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 33 | // |
| 34 | // |
| 35 | // Run: |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 36 | // # Collect data flow and coverage for INPUT_FILE |
| 37 | // # write to OUTPUT_FILE (default: stdout) |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 38 | // export DFSAN_OPTIONS=fast16labels=1:warn_unimplemented=0 |
| 39 | // ./a.out INPUT_FILE [OUTPUT_FILE] |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 40 | // |
| 41 | // # Print all instrumented functions. llvm-symbolizer must be present in PATH |
| 42 | // ./a.out |
| 43 | // |
| 44 | // Example output: |
| 45 | // =============== |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 46 | // F0 11111111111111 |
| 47 | // F1 10000000000000 |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 48 | // C0 1 2 3 4 5 |
| 49 | // C1 8 |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 50 | // =============== |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 51 | // "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] | 52 | // "CN X Y Z T": tells that a function N has basic blocks X, Y, and Z covered |
| 53 | // in addition to the function's entry block, out of T total instrumented |
| 54 | // blocks. |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 55 | // |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===*/ |
| 57 | |
| 58 | #include <assert.h> |
| 59 | #include <stdio.h> |
| 60 | #include <stdlib.h> |
| 61 | #include <stdint.h> |
| 62 | #include <string.h> |
| 63 | |
| 64 | #include <execinfo.h> // backtrace_symbols_fd |
| 65 | |
| 66 | #include <sanitizer/dfsan_interface.h> |
| 67 | |
| 68 | extern "C" { |
| 69 | extern int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size); |
| 70 | __attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv); |
| 71 | } // extern "C" |
| 72 | |
| 73 | static size_t InputLen; |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 74 | static size_t NumIterations; |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 75 | static size_t NumFuncs, NumGuards; |
| 76 | static uint32_t *GuardsBeg, *GuardsEnd; |
| 77 | static const uintptr_t *PCsBeg, *PCsEnd; |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 78 | static __thread size_t CurrentFunc, CurrentIteration; |
| 79 | static dfsan_label **FuncLabels; // NumFuncs x NumIterations. |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 80 | static bool *BBExecuted; // Array of NumGuards elements. |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 81 | |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 82 | enum { |
| 83 | PCFLAG_FUNC_ENTRY = 1, |
| 84 | }; |
| 85 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 86 | const int kNumLabels = 16; |
| 87 | |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 88 | static inline bool BlockIsEntry(size_t BlockIdx) { |
| 89 | return PCsBeg[BlockIdx * 2 + 1] & PCFLAG_FUNC_ENTRY; |
| 90 | } |
| 91 | |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 92 | // Prints all instrumented functions. |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 93 | static int PrintFunctions() { |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 94 | // We don't have the symbolizer integrated with dfsan yet. |
| 95 | // So use backtrace_symbols_fd and pipe it through llvm-symbolizer. |
| 96 | // TODO(kcc): this is pretty ugly and may break in lots of ways. |
| 97 | // We'll need to make a proper in-process symbolizer work with DFSan. |
| 98 | FILE *Pipe = popen("sed 's/(+/ /g; s/).*//g' " |
| 99 | "| llvm-symbolizer " |
| 100 | "| grep 'dfs\\$' " |
| 101 | "| sed 's/dfs\\$//g'", "w"); |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 102 | for (size_t I = 0; I < NumGuards; I++) { |
| 103 | uintptr_t PC = PCsBeg[I * 2]; |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 104 | if (!BlockIsEntry(I)) continue; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 105 | void *const Buf[1] = {(void*)PC}; |
| 106 | backtrace_symbols_fd(Buf, 1, fileno(Pipe)); |
| 107 | } |
| 108 | pclose(Pipe); |
| 109 | return 0; |
| 110 | } |
| 111 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 112 | static void PrintBinary(FILE *Out, dfsan_label L, size_t Len) { |
| 113 | char buf[kNumLabels + 1]; |
| 114 | assert(Len <= kNumLabels); |
| 115 | for (int i = 0; i < kNumLabels; i++) |
| 116 | buf[i] = (L & (1 << i)) ? '1' : '0'; |
| 117 | buf[Len] = 0; |
| 118 | fprintf(Out, "%s", buf); |
kcc | 54dce2c | 2018-05-23 20:57:11 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void PrintDataFlow(FILE *Out) { |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 122 | for (size_t Func = 0; Func < NumFuncs; Func++) { |
| 123 | bool HasAny = false; |
| 124 | for (size_t Iter = 0; Iter < NumIterations; Iter++) |
| 125 | if (FuncLabels[Func][Iter]) |
| 126 | HasAny = true; |
| 127 | if (!HasAny) |
| 128 | continue; |
| 129 | fprintf(Out, "F%zd ", Func); |
| 130 | size_t LenOfLastIteration = kNumLabels; |
| 131 | if (auto Tail = InputLen % kNumLabels) |
| 132 | LenOfLastIteration = Tail; |
| 133 | for (size_t Iter = 0; Iter < NumIterations; Iter++) |
| 134 | PrintBinary(Out, FuncLabels[Func][Iter], |
| 135 | Iter == NumIterations - 1 ? LenOfLastIteration : kNumLabels); |
| 136 | fprintf(Out, "\n"); |
| 137 | } |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 138 | } |
| 139 | |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 140 | static void PrintCoverage(FILE *Out) { |
| 141 | ssize_t CurrentFuncGuard = -1; |
| 142 | ssize_t CurrentFuncNum = -1; |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 143 | ssize_t NumBlocksInCurrentFunc = -1; |
| 144 | for (size_t FuncBeg = 0; FuncBeg < NumGuards;) { |
| 145 | CurrentFuncNum++; |
| 146 | assert(BlockIsEntry(FuncBeg)); |
| 147 | size_t FuncEnd = FuncBeg + 1; |
| 148 | for (; FuncEnd < NumGuards && !BlockIsEntry(FuncEnd); FuncEnd++) |
| 149 | ; |
| 150 | if (BBExecuted[FuncBeg]) { |
| 151 | fprintf(Out, "C%zd", CurrentFuncNum); |
| 152 | for (size_t I = FuncBeg + 1; I < FuncEnd; I++) |
| 153 | if (BBExecuted[I]) |
| 154 | fprintf(Out, " %zd", I - FuncBeg); |
| 155 | fprintf(Out, " %zd\n", FuncEnd - FuncBeg); |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 156 | } |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 157 | FuncBeg = FuncEnd; |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 158 | } |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 159 | } |
| 160 | |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 161 | int main(int argc, char **argv) { |
| 162 | if (LLVMFuzzerInitialize) |
| 163 | LLVMFuzzerInitialize(&argc, &argv); |
| 164 | if (argc == 1) |
| 165 | return PrintFunctions(); |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 166 | assert(argc == 2 || argc == 3); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 167 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 168 | const char *Input = argv[1]; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 169 | fprintf(stderr, "INFO: reading '%s'\n", Input); |
| 170 | FILE *In = fopen(Input, "r"); |
| 171 | assert(In); |
| 172 | fseek(In, 0, SEEK_END); |
| 173 | InputLen = ftell(In); |
| 174 | fseek(In, 0, SEEK_SET); |
| 175 | unsigned char *Buf = (unsigned char*)malloc(InputLen); |
| 176 | size_t NumBytesRead = fread(Buf, 1, InputLen, In); |
| 177 | assert(NumBytesRead == InputLen); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 178 | fclose(In); |
| 179 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 180 | NumIterations = (NumBytesRead + kNumLabels - 1) / kNumLabels; |
| 181 | FuncLabels = (dfsan_label**)calloc(NumFuncs, sizeof(dfsan_label*)); |
| 182 | for (size_t Func = 0; Func < NumFuncs; Func++) |
| 183 | FuncLabels[Func] = |
| 184 | (dfsan_label *)calloc(NumIterations, sizeof(dfsan_label)); |
hans | 39ed034 | 2019-06-14 07:32:22 +0000 | [diff] [blame] | 185 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 186 | for (CurrentIteration = 0; CurrentIteration < NumIterations; |
| 187 | CurrentIteration++) { |
| 188 | fprintf(stderr, "INFO: running '%s' %zd/%zd\n", Input, CurrentIteration, |
| 189 | NumIterations); |
| 190 | dfsan_flush(); |
| 191 | dfsan_set_label(0, Buf, InputLen); |
| 192 | |
| 193 | size_t BaseIdx = CurrentIteration * kNumLabels; |
| 194 | size_t LastIdx = BaseIdx + kNumLabels < NumBytesRead ? BaseIdx + kNumLabels |
| 195 | : NumBytesRead; |
| 196 | assert(BaseIdx < LastIdx); |
| 197 | for (size_t Idx = BaseIdx; Idx < LastIdx; Idx++) |
| 198 | dfsan_set_label(1 << (Idx - BaseIdx), Buf + Idx, 1); |
| 199 | LLVMFuzzerTestOneInput(Buf, InputLen); |
| 200 | } |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 201 | free(Buf); |
| 202 | |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 203 | bool OutIsStdout = argc == 2; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 204 | fprintf(stderr, "INFO: writing dataflow to %s\n", |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 205 | OutIsStdout ? "<stdout>" : argv[2]); |
| 206 | FILE *Out = OutIsStdout ? stdout : fopen(argv[2], "w"); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 207 | PrintDataFlow(Out); |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 208 | PrintCoverage(Out); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 209 | if (!OutIsStdout) fclose(Out); |
| 210 | } |
| 211 | |
| 212 | extern "C" { |
| 213 | |
| 214 | void __sanitizer_cov_trace_pc_guard_init(uint32_t *start, |
| 215 | uint32_t *stop) { |
| 216 | assert(NumFuncs == 0 && "This tool does not support DSOs"); |
| 217 | assert(start < stop && "The code is not instrumented for coverage"); |
| 218 | if (start == stop || *start) return; // Initialize only once. |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 219 | GuardsBeg = start; |
| 220 | GuardsEnd = stop; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg, |
| 224 | const uintptr_t *pcs_end) { |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 225 | if (NumGuards) return; // Initialize only once. |
| 226 | NumGuards = GuardsEnd - GuardsBeg; |
| 227 | PCsBeg = pcs_beg; |
| 228 | PCsEnd = pcs_end; |
| 229 | assert(NumGuards == (PCsEnd - PCsBeg) / 2); |
| 230 | for (size_t i = 0; i < NumGuards; i++) { |
kcc | 45fa355 | 2019-05-08 17:20:09 +0000 | [diff] [blame] | 231 | if (BlockIsEntry(i)) { |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 232 | NumFuncs++; |
| 233 | GuardsBeg[i] = NumFuncs; |
| 234 | } |
| 235 | } |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 236 | BBExecuted = (bool*)calloc(NumGuards, sizeof(bool)); |
| 237 | fprintf(stderr, "INFO: %zd instrumented function(s) observed " |
| 238 | "and %zd basic blocks\n", NumFuncs, NumGuards); |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void __sanitizer_cov_trace_pc_indir(uint64_t x){} // unused. |
| 242 | |
kcc | ce6392a | 2019-05-08 00:51:15 +0000 | [diff] [blame] | 243 | void __sanitizer_cov_trace_pc_guard(uint32_t *guard) { |
| 244 | size_t GuardIdx = guard - GuardsBeg; |
| 245 | assert(GuardIdx < NumGuards); |
| 246 | BBExecuted[GuardIdx] = true; |
| 247 | if (!*guard) return; // not a function entry. |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 248 | uint32_t FuncNum = *guard - 1; // Guards start from 1. |
| 249 | assert(FuncNum < NumFuncs); |
| 250 | CurrentFunc = FuncNum; |
| 251 | } |
| 252 | |
| 253 | void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases, |
| 254 | dfsan_label L1, dfsan_label UnusedL) { |
| 255 | assert(CurrentFunc < NumFuncs); |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 256 | FuncLabels[CurrentFunc][CurrentIteration] |= L1; |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | #define HOOK(Name, Type) \ |
| 260 | void Name(Type Arg1, Type Arg2, dfsan_label L1, dfsan_label L2) { \ |
| 261 | assert(CurrentFunc < NumFuncs); \ |
kcc | 0a66b5b | 2019-06-14 19:54:32 +0000 | [diff] [blame] | 262 | FuncLabels[CurrentFunc][CurrentIteration] |= L1 | L2; \ |
kcc | dbf20a0 | 2018-05-10 19:59:01 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | HOOK(__dfsw___sanitizer_cov_trace_const_cmp1, uint8_t) |
| 266 | HOOK(__dfsw___sanitizer_cov_trace_const_cmp2, uint16_t) |
| 267 | HOOK(__dfsw___sanitizer_cov_trace_const_cmp4, uint32_t) |
| 268 | HOOK(__dfsw___sanitizer_cov_trace_const_cmp8, uint64_t) |
| 269 | HOOK(__dfsw___sanitizer_cov_trace_cmp1, uint8_t) |
| 270 | HOOK(__dfsw___sanitizer_cov_trace_cmp2, uint16_t) |
| 271 | HOOK(__dfsw___sanitizer_cov_trace_cmp4, uint32_t) |
| 272 | HOOK(__dfsw___sanitizer_cov_trace_cmp8, uint64_t) |
| 273 | |
| 274 | } // extern "C" |