aboutsummaryrefslogtreecommitdiffhomepage
path: root/projects/firefox/target.c
blob: 1c225657d1ac4551ebafd3741a6f503298df9e7f (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define STRINGLIT(S) #S
#define STRINGIFY(S) STRINGLIT(S)

// Required for oss-fuzz to consider the binary a target.
static const char* magic = "LLVMFuzzerTestOneInput";

int main(int argc, char* argv[]) {
  char path[PATH_MAX] = {0};

  if (**argv != '/') {
    if (!getcwd(path, PATH_MAX)) {
      perror("Couldn't get CWD");
      exit(1);
    }
    strcat(path, "/");
  }

  if (strlen(path) + strlen(*argv) + 20 > PATH_MAX) {
    fprintf(stderr, "Path length would exceed PATH_MAX\n");
    exit(1);
  }

  strcat(path, *argv);

  char* solidus = strrchr(path, '/');
  *solidus = 0; // terminate string before last /

  char ld_path[PATH_MAX] = {0};
  strcpy(ld_path, path);
  strcat(ld_path, "/lib");

  char ff_path[PATH_MAX] = {0};
  strcpy(ff_path, path);
  strcat(ff_path, "/firefox/firefox");

  if (getenv("LD_LIBRARY_PATH")) {
    // Shouldn't be set. Code can be changed to append if it ever is.
    fprintf(stderr, "LD_LIBRARY_PATH unexpectedly set\n");
    exit(1);
  }
  if (setenv("LD_LIBRARY_PATH", ld_path, 0)) {
    perror("Error setting LD_LIBRARY_PATH");
    exit(1);
  }

  if (setenv("MOZ_RUN_GTEST", "1", 1) || setenv("LIBFUZZER", "1", 1) ||
      setenv("FUZZER", STRINGIFY(FUZZ_TARGET), 1)) {
    perror("Error setting fuzzing variables");
    exit(1);
  }

  // Temporary (or permanent?) work-around for a bug in the fuzzing interface.
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1466021#c9
  char* options = getenv("ASAN_OPTIONS");
  if (!options) {
    fprintf(stderr, "ASAN_OPTIONS not set ?!\n");
    exit(1);
  }
  char append[] = ":detect_stack_use_after_return=0";
  char* new_options = (char*)malloc(strlen(options) + sizeof(append));
  memcpy(new_options, options, strlen(options));
  memcpy(new_options + strlen(options), append, sizeof(append));
  if (setenv("ASAN_OPTIONS", new_options, 1)) {
    perror("Error setting ASAN_OPTIONS");
    exit(1);
  }
  free(new_options);

  return execv(ff_path, argv);
}