forked from aflsmart/aflsmart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart-utils.c
314 lines (241 loc) · 5.87 KB
/
smart-utils.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
AFLSmart - Utility functions
----------------------------
Copyright 2018 National University of Singapore
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:
http://www.apache.org/licenses/LICENSE-2.0
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
struct log_file {
pid_t pid;
FILE *file;
struct log_file *left;
struct log_file *right;
};
static struct log_file *log_file_set = NULL;
static char *dirname = NULL;
FILE *get_new_log_file(pid_t pid) {
char filespec[100];
FILE *file;
if (sprintf(filespec, "%s/%u.log", dirname, pid) < 0)
return NULL;
if ((file = fopen(filespec, "w")) == NULL)
return NULL;
return file;
}
struct log_file *get_node(FILE *file, pid_t pid) {
struct log_file *node;
if (file == NULL)
return NULL;
if ((node = (struct log_file *)malloc(sizeof(struct log_file))) == NULL)
return NULL;
node->pid = pid;
node->file = file;
node->left = NULL;
node->right = NULL;
return node;
}
FILE *get_log_file_recursively(struct log_file *node, pid_t pid) {
if (node == NULL)
return NULL;
if (pid == node->pid)
return node->file;
if (pid < node->pid) {
if (node->left == NULL) {
FILE *file = get_new_log_file(pid);
struct log_file *new_node = get_node(file, pid);
node->left = new_node;
return file;
} else {
return get_log_file_recursively(node->left, pid);
}
} else if (pid > node->pid) {
if (node->right == NULL) {
FILE *file = get_new_log_file(pid);
struct log_file *new_node = get_node(file, pid);
node->right = new_node;
return file;
} else {
return get_log_file_recursively(node->right, pid);
}
}
return NULL;
}
/*
Finds a log file to write to, if not found, then add a new one for
the current process id.
*/
FILE *get_log_file() {
pid_t pid = getpid();
/* Initialization is not done */
if (log_file_set == NULL)
return NULL;
return get_log_file_recursively(log_file_set, pid);
}
char *read_file(const char *filespec) {
FILE *file;
long file_size;
char *file_content;
if ((file = fopen(filespec, "r")) == NULL) {
/* Error opening file for reading. */
return NULL;
}
fseek(file, 0L, SEEK_END);
file_size = ftell(file);
fseek(file, 0L, SEEK_SET);
file_content = (char *)malloc(file_size + 1);
if (file_content == NULL) {
fclose(file);
/* Memory allocation error */
return NULL;
}
if (fread(file_content, 1, file_size, file) != file_size) {
free(file_content);
fclose(file);
/* Error reading file content */
return NULL;
}
file_content[file_size] = '\0';
fclose(file);
return file_content;
}
void print_segment(const char *s, int start_byte, int end_byte) {
int current = 1;
while (*s) {
if (current >= start_byte && current <= end_byte) {
printf("%c", *s);
}
s++;
current++;
}
}
void smart_log_init(const char *out_dir) {
pid_t pid = getpid();
char *tmp_dirname;
const char *s;
FILE *file;
int len;
/* Already initialized */
if (log_file_set != NULL)
return;
if (out_dir == NULL)
return;
s = out_dir;
len = 0;
while (*s) {
if (len == 1000)
break;
s++;
len++;
}
/* out_dir is too long */
if (*s)
return;
tmp_dirname = (char *)malloc(len + strlen("/log") + 1);
sprintf(tmp_dirname, "%s/log", out_dir);
if (mkdir(tmp_dirname, S_IRWXU) < 0 && errno != EEXIST)
return;
dirname = tmp_dirname;
file = get_new_log_file(pid);
log_file_set = get_node(file, pid);
}
/* Function to log a string in <out_dir>/log/<process id>.log. */
void smart_log(const char *format, ...) {
if (format == NULL)
return;
/* Initialization is not done */
if (log_file_set == NULL)
return;
va_list args;
va_start(args, format);
FILE *log_file = get_log_file();
if (log_file == NULL)
return;
vfprintf(log_file, format, args);
}
/* Function to log a bounded string in <out_dir>/log/<process id>.log. */
void smart_log_n(size_t size, const char *format, ...) {
if (size <= 0 || format == NULL)
return;
/* Initialization is not done */
if (log_file_set == NULL)
return;
va_list args;
va_start(args, format);
FILE *log_file = get_log_file();
if (log_file == NULL)
return;
char *buf = (char *)malloc(size + 1);
vsnprintf(buf, size, format, args);
fprintf(log_file, "%s", buf);
free(buf);
}
void smart_log_hex(const char *s) {
if (s == NULL)
return;
/* Initialization is not done */
if (log_file_set == NULL)
return;
FILE *log_file = get_log_file();
if (log_file == NULL)
return;
unsigned i = 0;
while (*s) {
if (i % 16 == 0) {
if (i > 0) {
fprintf(log_file, "\n");
}
fprintf(log_file, "%p: ", (void *)s);
}
fprintf(log_file, "%02x ", ((*s) & 0xff));
s++;
i++;
}
fprintf(log_file, "\n");
fflush(log_file);
}
void smart_log_n_hex(size_t size, const char *s) {
unsigned i;
if (s == NULL || size == 0)
return;
/* Initialization is not done */
if (log_file_set == NULL)
return;
FILE *log_file = get_log_file();
if (log_file == NULL)
return;
for (i = 0; i < size; ++i) {
if (i % 16 == 0) {
if (i > 0) {
fprintf(log_file, "\n");
}
fprintf(log_file, "%p: ", (void *)s);
}
fprintf(log_file, "%02x ", ((*s) & 0xff));
s++;
}
fprintf(log_file, "\n");
fflush(log_file);
}
void smart_log_int_hex(long number) {
char *s = (char *)&number;
unsigned i;
FILE *log_file = get_log_file();
if (log_file == NULL)
return;
for (i = 0; i < 4; ++i) {
fprintf(log_file, "%02x ", ((*s) & 0xff));
s++;
}
fprintf(log_file, "\n");
fflush(log_file);
}