2018-05-11 03:59:01 +08:00
|
|
|
/*===- DataFlow.cpp - a standalone DataFlow tracer -------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-05-11 03:59:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// An experimental data-flow tracer for fuzz targets.
|
|
|
|
// It is based on DFSan and SanitizerCoverage.
|
|
|
|
// https://clang.llvm.org/docs/DataFlowSanitizer.html
|
|
|
|
// https://clang.llvm.org/docs/SanitizerCoverage.html#tracing-data-flow
|
|
|
|
//
|
|
|
|
// It executes the fuzz target on the given input while monitoring the
|
|
|
|
// data flow for every instrumented comparison instruction.
|
|
|
|
//
|
2019-05-08 08:51:15 +08:00
|
|
|
// The output shows which functions depend on which bytes of the input,
|
|
|
|
// and also provides basic-block coverage for every input.
|
2018-05-11 03:59:01 +08:00
|
|
|
//
|
|
|
|
// Build:
|
|
|
|
// 1. Compile this file with -fsanitize=dataflow
|
|
|
|
// 2. Build the fuzz target with -g -fsanitize=dataflow
|
2019-05-08 08:51:15 +08:00
|
|
|
// -fsanitize-coverage=trace-pc-guard,pc-table,bb,trace-cmp
|
2018-05-11 03:59:01 +08:00
|
|
|
// 3. Link those together with -fsanitize=dataflow
|
|
|
|
//
|
|
|
|
// -fsanitize-coverage=trace-cmp inserts callbacks around every comparison
|
|
|
|
// instruction, DFSan modifies the calls to pass the data flow labels.
|
|
|
|
// The callbacks update the data flow label for the current function.
|
|
|
|
// See e.g. __dfsw___sanitizer_cov_trace_cmp1 below.
|
|
|
|
//
|
2019-05-08 08:51:15 +08:00
|
|
|
// -fsanitize-coverage=trace-pc-guard,pc-table,bb instruments function
|
2018-05-11 03:59:01 +08:00
|
|
|
// entries so that the comparison callback knows that current function.
|
2019-05-08 08:51:15 +08:00
|
|
|
// -fsanitize-coverage=...,bb also allows to collect basic block coverage.
|
2018-05-11 03:59:01 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Run:
|
2019-05-08 08:51:15 +08:00
|
|
|
// # Collect data flow and coverage for INPUT_FILE
|
|
|
|
// # write to OUTPUT_FILE (default: stdout)
|
|
|
|
// ./a.out FIRST_LABEL LAST_LABEL INPUT_FILE [OUTPUT_FILE]
|
2018-05-11 03:59:01 +08:00
|
|
|
//
|
|
|
|
// # Print all instrumented functions. llvm-symbolizer must be present in PATH
|
|
|
|
// ./a.out
|
|
|
|
//
|
|
|
|
// Example output:
|
|
|
|
// ===============
|
2018-05-24 04:57:11 +08:00
|
|
|
// F0 11111111111111
|
|
|
|
// F1 10000000000000
|
2019-05-08 08:51:15 +08:00
|
|
|
// C0 1 2 3 4
|
|
|
|
// C1
|
2018-05-11 03:59:01 +08:00
|
|
|
// ===============
|
2018-05-24 04:57:11 +08:00
|
|
|
// "FN xxxxxxxxxx": tells what bytes of the input does the function N depend on.
|
|
|
|
// The byte string is LEN+1 bytes. The last byte is set if the function
|
|
|
|
// depends on the input length.
|
2019-05-08 08:51:15 +08:00
|
|
|
// "CN X Y Z": tells that a function N has basic blocks X, Y, and Z covered
|
|
|
|
// in addition to the function's entry block.
|
|
|
|
//
|
2018-05-11 03:59:01 +08:00
|
|
|
//===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <execinfo.h> // backtrace_symbols_fd
|
|
|
|
|
|
|
|
#include <sanitizer/dfsan_interface.h>
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
extern int LLVMFuzzerTestOneInput(const unsigned char *Data, size_t Size);
|
|
|
|
__attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
|
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
static size_t InputLen;
|
2019-04-13 05:00:12 +08:00
|
|
|
static size_t InputLabelBeg;
|
|
|
|
static size_t InputLabelEnd;
|
|
|
|
static size_t InputSizeLabel;
|
2019-05-08 08:51:15 +08:00
|
|
|
static size_t NumFuncs, NumGuards;
|
|
|
|
static uint32_t *GuardsBeg, *GuardsEnd;
|
|
|
|
static const uintptr_t *PCsBeg, *PCsEnd;
|
2018-05-11 03:59:01 +08:00
|
|
|
static __thread size_t CurrentFunc;
|
|
|
|
static dfsan_label *FuncLabels; // Array of NumFuncs elements.
|
2019-05-08 08:51:15 +08:00
|
|
|
static bool *BBExecuted; // Array of NumGuards elements.
|
2018-05-24 04:57:11 +08:00
|
|
|
static char *PrintableStringForLabel; // InputLen + 2 bytes.
|
2018-06-06 09:23:29 +08:00
|
|
|
static bool LabelSeen[1 << 8 * sizeof(dfsan_label)];
|
2018-05-11 03:59:01 +08:00
|
|
|
|
2019-05-08 08:51:15 +08:00
|
|
|
enum {
|
|
|
|
PCFLAG_FUNC_ENTRY = 1,
|
|
|
|
};
|
|
|
|
|
2018-05-11 03:59:01 +08:00
|
|
|
// Prints all instrumented functions.
|
2018-05-24 04:57:11 +08:00
|
|
|
static int PrintFunctions() {
|
2018-05-11 03:59:01 +08:00
|
|
|
// We don't have the symbolizer integrated with dfsan yet.
|
|
|
|
// So use backtrace_symbols_fd and pipe it through llvm-symbolizer.
|
|
|
|
// TODO(kcc): this is pretty ugly and may break in lots of ways.
|
|
|
|
// We'll need to make a proper in-process symbolizer work with DFSan.
|
|
|
|
FILE *Pipe = popen("sed 's/(+/ /g; s/).*//g' "
|
|
|
|
"| llvm-symbolizer "
|
|
|
|
"| grep 'dfs\\$' "
|
|
|
|
"| sed 's/dfs\\$//g'", "w");
|
2019-05-08 08:51:15 +08:00
|
|
|
for (size_t I = 0; I < NumGuards; I++) {
|
|
|
|
uintptr_t PC = PCsBeg[I * 2];
|
|
|
|
uintptr_t PCFlags = PCsBeg[I * 2 + 1];
|
|
|
|
if (!(PCFlags & PCFLAG_FUNC_ENTRY)) continue;
|
2018-05-11 03:59:01 +08:00
|
|
|
void *const Buf[1] = {(void*)PC};
|
|
|
|
backtrace_symbols_fd(Buf, 1, fileno(Pipe));
|
|
|
|
}
|
|
|
|
pclose(Pipe);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-06 09:23:29 +08:00
|
|
|
extern "C"
|
|
|
|
void SetBytesForLabel(dfsan_label L, char *Bytes) {
|
|
|
|
if (LabelSeen[L])
|
|
|
|
return;
|
|
|
|
LabelSeen[L] = true;
|
2018-05-24 07:55:54 +08:00
|
|
|
assert(L);
|
2019-04-13 05:00:12 +08:00
|
|
|
if (L < InputSizeLabel) {
|
|
|
|
Bytes[L + InputLabelBeg - 1] = '1';
|
|
|
|
} else if (L == InputSizeLabel) {
|
|
|
|
Bytes[InputLen] = '1';
|
2018-05-24 04:57:11 +08:00
|
|
|
} else {
|
2018-05-11 03:59:01 +08:00
|
|
|
auto *DLI = dfsan_get_label_info(L);
|
2018-05-24 04:57:11 +08:00
|
|
|
SetBytesForLabel(DLI->l1, Bytes);
|
|
|
|
SetBytesForLabel(DLI->l2, Bytes);
|
2018-05-11 03:59:01 +08:00
|
|
|
}
|
2018-05-24 04:57:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *GetPrintableStringForLabel(dfsan_label L) {
|
|
|
|
memset(PrintableStringForLabel, '0', InputLen + 1);
|
|
|
|
PrintableStringForLabel[InputLen + 1] = 0;
|
2018-06-06 09:23:29 +08:00
|
|
|
memset(LabelSeen, 0, sizeof(LabelSeen));
|
2018-05-24 04:57:11 +08:00
|
|
|
SetBytesForLabel(L, PrintableStringForLabel);
|
|
|
|
return PrintableStringForLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintDataFlow(FILE *Out) {
|
2018-05-11 03:59:01 +08:00
|
|
|
for (size_t I = 0; I < NumFuncs; I++)
|
|
|
|
if (FuncLabels[I])
|
2018-05-24 04:57:11 +08:00
|
|
|
fprintf(Out, "F%zd %s\n", I, GetPrintableStringForLabel(FuncLabels[I]));
|
2018-05-11 03:59:01 +08:00
|
|
|
}
|
|
|
|
|
2019-05-08 08:51:15 +08:00
|
|
|
static void PrintCoverage(FILE *Out) {
|
|
|
|
ssize_t CurrentFuncGuard = -1;
|
|
|
|
ssize_t CurrentFuncNum = -1;
|
|
|
|
int NumFuncsCovered = 0;
|
|
|
|
for (size_t I = 0; I < NumGuards; I++) {
|
|
|
|
bool IsEntry = PCsBeg[I * 2 + 1] & PCFLAG_FUNC_ENTRY;
|
|
|
|
if (IsEntry) {
|
|
|
|
CurrentFuncNum++;
|
|
|
|
CurrentFuncGuard = I;
|
|
|
|
}
|
|
|
|
if (!BBExecuted[I]) continue;
|
|
|
|
if (IsEntry) {
|
|
|
|
if (NumFuncsCovered) fprintf(Out, "\n");
|
|
|
|
fprintf(Out, "C%zd ", CurrentFuncNum);
|
|
|
|
NumFuncsCovered++;
|
|
|
|
} else {
|
|
|
|
fprintf(Out, "%zd ", I - CurrentFuncGuard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(Out, "\n");
|
|
|
|
}
|
|
|
|
|
2018-05-11 03:59:01 +08:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (LLVMFuzzerInitialize)
|
|
|
|
LLVMFuzzerInitialize(&argc, &argv);
|
|
|
|
if (argc == 1)
|
|
|
|
return PrintFunctions();
|
2018-05-24 09:43:48 +08:00
|
|
|
assert(argc == 4 || argc == 5);
|
2019-04-13 05:00:12 +08:00
|
|
|
InputLabelBeg = atoi(argv[1]);
|
|
|
|
InputLabelEnd = atoi(argv[2]);
|
|
|
|
assert(InputLabelBeg < InputLabelEnd);
|
2018-05-11 03:59:01 +08:00
|
|
|
|
2018-05-24 09:43:48 +08:00
|
|
|
const char *Input = argv[3];
|
2018-05-11 03:59:01 +08:00
|
|
|
fprintf(stderr, "INFO: reading '%s'\n", Input);
|
|
|
|
FILE *In = fopen(Input, "r");
|
|
|
|
assert(In);
|
|
|
|
fseek(In, 0, SEEK_END);
|
|
|
|
InputLen = ftell(In);
|
|
|
|
fseek(In, 0, SEEK_SET);
|
|
|
|
unsigned char *Buf = (unsigned char*)malloc(InputLen);
|
|
|
|
size_t NumBytesRead = fread(Buf, 1, InputLen, In);
|
|
|
|
assert(NumBytesRead == InputLen);
|
2018-05-24 04:57:11 +08:00
|
|
|
PrintableStringForLabel = (char*)malloc(InputLen + 2);
|
2018-05-11 03:59:01 +08:00
|
|
|
fclose(In);
|
|
|
|
|
|
|
|
fprintf(stderr, "INFO: running '%s'\n", Input);
|
|
|
|
for (size_t I = 1; I <= InputLen; I++) {
|
2018-05-24 09:43:48 +08:00
|
|
|
size_t Idx = I - 1;
|
2019-04-13 05:00:12 +08:00
|
|
|
if (Idx >= InputLabelBeg && Idx < InputLabelEnd) {
|
|
|
|
dfsan_label L = dfsan_create_label("", nullptr);
|
|
|
|
assert(L == I - InputLabelBeg);
|
2018-05-24 09:43:48 +08:00
|
|
|
dfsan_set_label(L, Buf + Idx, 1);
|
2019-04-13 05:00:12 +08:00
|
|
|
}
|
2018-05-11 03:59:01 +08:00
|
|
|
}
|
|
|
|
dfsan_label SizeL = dfsan_create_label("", nullptr);
|
2019-04-13 05:00:12 +08:00
|
|
|
InputSizeLabel = SizeL;
|
|
|
|
assert(InputSizeLabel == InputLabelEnd - InputLabelBeg + 1);
|
2018-05-11 03:59:01 +08:00
|
|
|
dfsan_set_label(SizeL, &InputLen, sizeof(InputLen));
|
|
|
|
|
|
|
|
LLVMFuzzerTestOneInput(Buf, InputLen);
|
|
|
|
free(Buf);
|
|
|
|
|
2018-05-24 09:43:48 +08:00
|
|
|
bool OutIsStdout = argc == 4;
|
2018-05-11 03:59:01 +08:00
|
|
|
fprintf(stderr, "INFO: writing dataflow to %s\n",
|
2018-05-24 09:43:48 +08:00
|
|
|
OutIsStdout ? "<stdout>" : argv[4]);
|
|
|
|
FILE *Out = OutIsStdout ? stdout : fopen(argv[4], "w");
|
2018-05-11 03:59:01 +08:00
|
|
|
PrintDataFlow(Out);
|
2019-05-08 08:51:15 +08:00
|
|
|
PrintCoverage(Out);
|
2018-05-11 03:59:01 +08:00
|
|
|
if (!OutIsStdout) fclose(Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
|
|
|
|
uint32_t *stop) {
|
|
|
|
assert(NumFuncs == 0 && "This tool does not support DSOs");
|
|
|
|
assert(start < stop && "The code is not instrumented for coverage");
|
|
|
|
if (start == stop || *start) return; // Initialize only once.
|
2019-05-08 08:51:15 +08:00
|
|
|
GuardsBeg = start;
|
|
|
|
GuardsEnd = stop;
|
2018-05-11 03:59:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
|
|
|
|
const uintptr_t *pcs_end) {
|
2019-05-08 08:51:15 +08:00
|
|
|
if (NumGuards) return; // Initialize only once.
|
|
|
|
NumGuards = GuardsEnd - GuardsBeg;
|
|
|
|
PCsBeg = pcs_beg;
|
|
|
|
PCsEnd = pcs_end;
|
|
|
|
assert(NumGuards == (PCsEnd - PCsBeg) / 2);
|
|
|
|
for (size_t i = 0; i < NumGuards; i++) {
|
|
|
|
if (PCsBeg[i * 2 + 1] & PCFLAG_FUNC_ENTRY) {
|
|
|
|
NumFuncs++;
|
|
|
|
GuardsBeg[i] = NumFuncs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FuncLabels = (dfsan_label*)calloc(NumFuncs, sizeof(dfsan_label));
|
|
|
|
BBExecuted = (bool*)calloc(NumGuards, sizeof(bool));
|
|
|
|
fprintf(stderr, "INFO: %zd instrumented function(s) observed "
|
|
|
|
"and %zd basic blocks\n", NumFuncs, NumGuards);
|
2018-05-11 03:59:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __sanitizer_cov_trace_pc_indir(uint64_t x){} // unused.
|
|
|
|
|
2019-05-08 08:51:15 +08:00
|
|
|
void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
|
|
|
|
size_t GuardIdx = guard - GuardsBeg;
|
|
|
|
assert(GuardIdx < NumGuards);
|
|
|
|
BBExecuted[GuardIdx] = true;
|
|
|
|
if (!*guard) return; // not a function entry.
|
2018-05-11 03:59:01 +08:00
|
|
|
uint32_t FuncNum = *guard - 1; // Guards start from 1.
|
|
|
|
assert(FuncNum < NumFuncs);
|
|
|
|
CurrentFunc = FuncNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
|
|
|
|
dfsan_label L1, dfsan_label UnusedL) {
|
|
|
|
assert(CurrentFunc < NumFuncs);
|
|
|
|
FuncLabels[CurrentFunc] = dfsan_union(FuncLabels[CurrentFunc], L1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HOOK(Name, Type) \
|
|
|
|
void Name(Type Arg1, Type Arg2, dfsan_label L1, dfsan_label L2) { \
|
|
|
|
assert(CurrentFunc < NumFuncs); \
|
|
|
|
FuncLabels[CurrentFunc] = \
|
|
|
|
dfsan_union(FuncLabels[CurrentFunc], dfsan_union(L1, L2)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_const_cmp1, uint8_t)
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_const_cmp2, uint16_t)
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_const_cmp4, uint32_t)
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_const_cmp8, uint64_t)
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_cmp1, uint8_t)
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_cmp2, uint16_t)
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_cmp4, uint32_t)
|
|
|
|
HOOK(__dfsw___sanitizer_cov_trace_cmp8, uint64_t)
|
|
|
|
|
|
|
|
} // extern "C"
|