-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
270 lines (241 loc) · 6.32 KB
/
compiler.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mman.h>
/* Call `execvp` in child process, return true if execution is successful. */
bool execvp_child(char* const* argv)
{
pid_t pid = fork();
if (pid < 0)
{
perror("fork");
return false;
}
else if (pid == 0)
{
execvp(argv[0], argv);
perror("execvp");
abort();
}
else
{
int status;
if (waitpid(pid, &status, 0) < 0)
return false;
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
}
}
/* This compiler converts the link command that produces a libFuzzer binary to
the command that produces a AFL++ custom mutator shared library,
which uses the `LLVMFuzzerCustomMutator` and `LLVMFuzzerCustomCrossOver`. */
bool is_fsanitize_fuzzer(const char* s)
{
if (strncmp(s, "-fsanitize=", strlen("-fsanitize=")) != 0)
return false;
char* buf = strdup(s + strlen("-fsanitize="));
char* p = buf; char* next;
while ((next = strchr(p, ',')) != NULL)
{
*next = 0;
if (strcmp(p, "fuzzer") == 0)
{
free(buf);
return true;
}
p = next + 1;
}
bool ret = strcmp(p, "fuzzer") == 0;
free(buf);
return ret;
}
/*
The function identifies libfuzzer linkage, returns non-zero if it is.
When flag `-fsanitize=fuzzer` is detected, it returns 2.
Since such flag is going to be removed, we need to link with `libfuzzer-mutator.a` for
symbol `LLVMFuzzerMutate`. When the binary is considered as libfuzzer binary,
but is generated with `-fsanitize=fuzzer-no-link`, we assume `LLVMFuzzerMutate` exists
in the binary so we do not link with `libfuzzer-mutator.a`.
*/
int is_libfuzzer_link(int argc, char const *argv[], const char* clang)
{
for (int i = 0; i < argc; ++i)
{
// If command is not link command.
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "-E") == 0)
return 0;
}
for (int i = 0; i < argc; ++i)
{
// If contains `-fsanitize=fuzzer`, it is considered as extraction target.
if (is_fsanitize_fuzzer(argv[i]))
return 2;
}
// Then we go to the slow path, we try to compile the binary,
// and check if it contains LLVM fuzzing strings.
char const** new_argv = (char const**)malloc((argc + 1) * sizeof(char const*));
int new_argc = 0;
new_argv[new_argc++] = clang;
const char* file_name = NULL;
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-s") != 0) // We don't strip.
new_argv[new_argc++] = argv[i];
if (strcmp(argv[i], "-o") == 0 && i + 1 < argc)
file_name = argv[i + 1];
}
new_argv[new_argc] = NULL;
if (!execvp_child((char**)new_argv))
exit(EXIT_FAILURE);
if (file_name == NULL)
file_name = "a.out";
int fd = open(file_name, O_RDONLY);
if (fd < 0)
{
perror("open");
exit(EXIT_FAILURE);
}
struct stat st;
if (fstat(fd, &st) < 0)
{
perror("fstat");
exit(EXIT_FAILURE);
}
void* file_content = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (file_content == MAP_FAILED)
{
perror("mmap");
exit(EXIT_FAILURE);
}
// The binary is considered as libfuzzer if it contains string
// `LLVMFuzzerTestOneInput` and contains one of
// `LLVMFuzzerCustomMutator` or `LLVMFuzzerCustomCrossOver`.
static const char toi[] = "LLVMFuzzerTestOneInput";
static const char cm[] = "LLVMFuzzerCustomMutator";
static const char cco[] = "LLVMFuzzerCustomCrossOver";
int ret =
memmem(file_content, st.st_size, toi, sizeof(toi)) != NULL &&
(memmem(file_content, st.st_size, cm, sizeof(cm)) != NULL ||
memmem(file_content, st.st_size, cco, sizeof(cco)) != NULL);
if (munmap(file_content, st.st_size) < 0)
{
perror("munmap");
exit(EXIT_FAILURE);
}
close(fd);
return ret;
}
const char* find_repo_path(const char* argv0)
{
const char* lib_path = getenv("LIBFUZZER2AFL_PATH");
if (lib_path)
{
if (access(lib_path, R_OK))
{
fprintf(stderr, "Invalid extractor library path\n");
abort();
}
return lib_path;
}
if (access(LIBFUZZER2AFL_PATH"/afl.o", R_OK) == 0)
{
return LIBFUZZER2AFL_PATH;
}
const char* slash = strrchr(argv0, '/');
if (slash)
{
char* dir = strdup(argv0);
*strrchr(dir, '/') = 0;
char* path;
int r = asprintf(&path, "%s/afl.o", dir);
if (r < 0) abort();
if (!access(path, R_OK)) return dir;
free(path); free(dir);
}
fprintf(stderr, "TODO\n");
abort();
}
int main(int argc, char const *argv[])
{
if (argc <= 1)
{
puts("LibFuzzer2AFL Compiler by Mem2019");
return 0;
}
char const** new_argv = (char const**)malloc((argc + 100) * sizeof(char const*));
int new_argc = 0;
if (strstr(argv[0], "++") == NULL)
{
const char* cc = getenv("EXT_CC");
new_argv[new_argc++] = cc ? cc : "clang";
}
else
{
const char* cxx = getenv("EXT_CXX");
new_argv[new_argc++] = cxx ? cxx : "clang++";
}
int if_libfuzzer = is_libfuzzer_link(argc, argv, new_argv[0]);
new_argv[new_argc++] = "-fPIC"; // always PIC
if (if_libfuzzer)
{
new_argv[new_argc++] = "-shared";
new_argv[new_argc++] = "-Wl,--no-undefined";
}
bool has_fsan = false;
for (int i = 1; i < argc; ++i)
{
if (strncmp(argv[i], "-fsanitize=", strlen("-fsanitize=")) == 0)
{
has_fsan = true;
continue;
}
if (strcmp(argv[i], "-fno-PIC") == 0 || strcmp(argv[i], "-fno-PIE") == 0 ||
strcmp(argv[i], "-fno-pic") == 0 || strcmp(argv[i], "-fno-pie") == 0)
continue;
new_argv[new_argc++] = argv[i];
}
char* path; int r;
const char* repo_path = find_repo_path(argv[0]);
if (has_fsan)
{ // If we have removed any `-fsanitize` flag,
// we link with the dummy interface to prevent link error.
r = asprintf(&path, "%s/common_interface_defs.o", repo_path);
if (r < 0) abort();
new_argv[new_argc++] = path;
}
if (if_libfuzzer)
{
r = asprintf(&path, "%s/afl.o", repo_path);
if (r < 0) abort();
new_argv[new_argc++] = path;
if (if_libfuzzer == 2)
{
r = asprintf(&path, "%s/libfuzzer-mutator.a", repo_path);
if (r < 0) abort();
new_argv[new_argc++] = path;
new_argv[new_argc++] = "-lstdc++";
new_argv[new_argc++] = "-lm";
}
}
new_argv[new_argc++] = "-Wno-unused-command-line-argument";
new_argv[new_argc] = NULL;
if (getenv("SHOW_COMPILER_ARGS"))
{
for (int i = 0; i < argc; ++i)
fprintf(stderr, "%s ", argv[i]);
fprintf(stderr, "\n");
for (const char** i = new_argv; *i; ++i)
fprintf(stderr, "%s ", *i);
fprintf(stderr, "\n");
}
execvp(new_argv[0], (char**)new_argv);
abort();
return 0;
}