-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz_test_helper.c
90 lines (82 loc) · 1.84 KB
/
fuzz_test_helper.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
#include "fuzz_test_helper.h"
/**
* fuzz file related
*/
unsigned int fuzz_file_index = 0;
unsigned long fuzz_file_length;
char *fuzz_file_string;
char *fuzz_file_name = "fuzz_test_input";
/**
* fuzz loop related
*/
int fuzz_loop_index = 0;
u8 *push_data(u8 **data, u8 value, int *size, int *capacity) {
if (*size >= *capacity) {
(*data) = (u8 *) realloc(*data, (*capacity) * 2 * sizeof(u8));
if ((*data) == NULL) {
fprintf(stderr, "realloc failed!\n");
exit(0);
}
(*capacity) *= 2;
}
(*data)[*size] = value;
*size = *size + 1;
return *data;
}
u8 read_byte() {
if (fuzz_file_index >= fuzz_file_length) {
return 0;
}
u8 t = fuzz_file_string[fuzz_file_index++];
return t;
}
u32 read_int() {
u32 result = read_byte();
result <<= 8u;
result |= read_byte();
result <<= 8u;
result |= read_byte();
result <<= 8u;
result |= read_byte();
return result;
}
void file_to_string(const char *filename) {
FILE *f = fopen(filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
fuzz_file_length = ftell(f);
fseek(f, 0, SEEK_SET);
fuzz_file_string = malloc(fuzz_file_length + 10);
if (fuzz_file_string) {
fread(fuzz_file_string, 1, fuzz_file_length, f);
}
fclose(f);
} else {
printf("fuzz file open fail.\n");
exit(0);
}
}
/**
* @return index of the first different character, -1 for equal string
*/
int fuzz_test_result_compare(char **a, char **b) {
for (int i = 0; i < 1000; i++) {
if (a[i] != NULL && b[i] != NULL) {
if (strcmp(a[i], b[i]) != 0) {
return i;
}
}
// if ((a[i] == NULL && b[i] != NULL) ||
// (a[i] != NULL && b[i] == NULL)) { // Not both NULL
// return i;
// }
}
return -1;
}
struct event *new_event(enum event_t type) {
struct event *e = (struct event *) calloc(1, sizeof(struct event));
e->type = type;
e->time_usecs_end = NO_TIME_RANGE;
e->offset_usecs = NO_TIME_RANGE;
return e;
}