summaryrefslogtreecommitdiff
path: root/src/test/fuzz/fuzzing_common.c
blob: 51d519b861f114c7b10b383f957415a8a7ebcf62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "orconfig.h"
#include "torint.h"
#include "util.h"
#include "torlog.h"
#include "backtrace.h"
#include "fuzzing.h"

extern const char tor_git_revision[];
const char tor_git_revision[] = "";

#define MAX_FUZZ_SIZE (128*1024)

#ifdef LLVM_FUZZ
int
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  static int initialized = 0;
  if (!initialized) {
    if (fuzz_init() < 0)
      abort();
  }

  return fuzz_main(Data, Size);
}

#else /* Not LLVM_FUZZ, so AFL. */

int
main(int argc, char **argv)
{
  size_t size;
  char *input = read_file_to_str_until_eof(0, MAX_FUZZ_SIZE, &size);

  tor_threads_init();
  init_logging(1);

  if (argc > 1 && !strcmp(argv[1], "--info")) {
    log_severity_list_t sev;
    set_log_severity_config(LOG_INFO, LOG_ERR, &sev);
    add_stream_log(&sev, "stdout", 1);
    configure_backtrace_handler(NULL);
  }

  tor_assert(input);
  if (fuzz_init() < 0)
    abort();
  fuzz_main((const uint8_t*)input, size);
  tor_free(input);
  return 0;
}

#endif