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 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 char* options = getenv("ASAN_OPTIONS");
44 if (options) {
45 char* ptr;
46 char* new_options = strdup(options);
47
48 // https://bugzilla.mozilla.org/1477846
49 ptr = strstr(new_options, "detect_stack_use_after_return=1");
50 if (ptr) ptr[30] = '0';
51
52 // https://bugzilla.mozilla.org/1477844
53 ptr = strstr(new_options, "detect_leaks=1");
54 if (ptr) ptr[13] = '0';
55
56 setenv("ASAN_OPTIONS", new_options, 1);
57 free(new_options);
58 }
59
60
61 char js_path[PATH_MAX] = {0};
62 strcpy(js_path, path);
63 strcat(js_path, "/fuzz-tests");
64
65 int ret = execv(js_path, argv);
66 if (ret)
67 perror("execv");
68 return ret;
69 }
70
71