1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 
7 #define STRINGLIT(S) #S
8 #define STRINGIFY(S) STRINGLIT(S)
9 
10 // Required for oss-fuzz to consider the binary a target.
11 static const char* magic __attribute__((used)) = "LLVMFuzzerTestOneInput";
12 
main(int argc,char * argv[])13 int main(int argc, char* argv[]) {
14   char path[PATH_MAX] = {0};
15 
16   // Handle (currently not used) relative binary path.
17   if (**argv != '/') {
18     if (!getcwd(path, PATH_MAX - 1)) {
19       perror("getcwd");
20       exit(1);
21     }
22     strcat(path, "/");
23   }
24 
25   if (strlen(path) + strlen(*argv) + 40 >= PATH_MAX) {
26     fprintf(stderr, "Path length would exceed PATH_MAX\n");
27     exit(1);
28   }
29 
30   strcat(path, *argv);
31   char* solidus = strrchr(path, '/');
32   *solidus = 0; // terminate path before last /
33 
34   char ld_path[PATH_MAX] = {0};
35   strcpy(ld_path, path);
36   strcat(ld_path, "/lib");
37 
38   // Expects LD_LIBRARY_PATH to not also be set by oss-fuzz.
39   setenv("LD_LIBRARY_PATH", ld_path, 0);
40   setenv("HOME", "/tmp", 0);
41   setenv("FUZZER", STRINGIFY(FUZZ_TARGET), 1);
42 
43   // ContentParentIPC
44   char blacklist_path[PATH_MAX] = {0};
45   strcpy(blacklist_path, path);
46   strcat(blacklist_path, "/firefox/libfuzzer.content.blacklist.txt");
47   setenv("MOZ_IPC_MESSAGE_FUZZ_BLACKLIST", blacklist_path, 1);
48 
49   // Temporary (or permanent?) work-arounds for fuzzing interface bugs.
50   char* options = getenv("ASAN_OPTIONS");
51   if (options) {
52     char* ptr;
53     char* new_options = strdup(options);
54     // https://bugzilla.mozilla.org/1477846
55     ptr = strstr(new_options, "detect_stack_use_after_return=1");
56     if (ptr) ptr[30] = '0';
57     // https://bugzilla.mozilla.org/1477844
58     ptr = strstr(new_options, "detect_leaks=1");
59     if (ptr) ptr[13] = '0';
60     setenv("ASAN_OPTIONS", new_options, 1);
61     free(new_options);
62   }
63 
64   char ff_path[PATH_MAX] = {0};
65   strcpy(ff_path, path);
66   strcat(ff_path, "/firefox/firefox");
67 
68   int ret = execv(ff_path, argv);
69   if (ret)
70     perror("execv");
71   return ret;
72 }
73