-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphland.c
377 lines (345 loc) · 10.1 KB
/
graphland.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef struct _graph_node graph_node_t;
typedef struct _graph_node {
int num_neighbors;
int label;
void *metadata;
graph_node_t *hash_next;
graph_node_t **neighbors;
} graph_node_t;
typedef enum _opcode {
ADD_NODE,
DEL_NODE,
SET_METADATA,
ADD_EDGE,
DEL_EDGE,
PRINT_GRAPH,
PRINT_NODE,
LAST_OPCODE
} opcode_t;
char *opcode_names[] = {
"ADD_NODE",
"DEL_NODE",
"SET_METADATA",
"ADD_EDGE",
"DEL_EDGE",
"PRINT_GRAPH",
"PRINT_NODE"
};
// Handler prototypes
void add_node(uint8_t *, int);
void del_node(uint8_t *, int);
void set_metadata(uint8_t *, int);
void add_edge(uint8_t *, int);
void del_edge(uint8_t *, int);
void print_graph(uint8_t *, int);
void print_node(uint8_t *, int);
void (*opcode_handlers[])(uint8_t *, int) = {
[ADD_NODE] = add_node,
[DEL_NODE] = del_node,
[SET_METADATA] = set_metadata,
[ADD_EDGE] = add_edge,
[DEL_EDGE] = del_edge,
[PRINT_GRAPH] = print_graph,
[PRINT_NODE] = print_node,
};
typedef struct _command {
uint16_t opcode;
uint16_t size;
uint8_t *data;
} command_t;
#define HASH_SIZE 128
struct graph_hashtable {
graph_node_t *buckets[HASH_SIZE];
} graph = {};
int hash_node(int label) {
if (label < 0) label = -label;
return label % HASH_SIZE;
}
void free_node(graph_node_t *node) {
free(node->metadata);
free(node->neighbors);
free(node);
}
void hash_insert(graph_node_t *node) {
if (node == NULL) return;
int nodehash = hash_node(node->label);
node->hash_next = NULL;
graph_node_t *cursor = graph.buckets[nodehash];
if (!cursor) {
graph.buckets[nodehash] = node;
//printf("DEBUG: inserted label %d node %p\n", node->label, node);
return;
}
// Seek to the last element
graph_node_t *last;
while(cursor) {
// Don't allow duplicate labels
if (cursor->label == node->label) {
//printf("DEBUG: not adding duplicate node %d\n", node->label);
free_node(node);
return;
}
last = cursor;
cursor = cursor->hash_next;
}
// Insert
last->hash_next = node;
//printf("DEBUG: inserted label %d node %p\n", node->label, node);
return;
}
graph_node_t * hash_lookup(int label) {
//printf("DEBUG: trying to look up label %d\n", label);
graph_node_t *cursor = graph.buckets[hash_node(label)];
while (cursor) {
if (cursor->label == label) break;
cursor = cursor->hash_next;
}
//printf("DEBUG: label %d => %p\n", label, cursor);
return cursor;
}
// Remove node from the hash by label
// Does NOT free the node
void hash_remove(int label) {
//printf("DEBUG: trying to remove label %d\n", label);
graph_node_t *cursor = graph.buckets[hash_node(label)];
// Didn't find it, bail
if (!cursor) return;
// Special case for first bucket entry
if (cursor->label == label) {
graph.buckets[hash_node(label)] = cursor->hash_next;
if (!cursor->hash_next) return;
}
while(cursor->hash_next) {
if (cursor->hash_next->label == label) {
cursor->hash_next = cursor->hash_next->hash_next;
return;
}
cursor = cursor->hash_next;
}
return;
}
void add_node(uint8_t *data, int size) {
if (size != sizeof(int)) return;
int label = *(int *)data;
//printf("DEBUG: adding node with label %d\n", label);
graph_node_t *node = calloc(1, sizeof(graph_node_t));
if (!node) return;
node->label = label;
hash_insert(node);
}
void del_node(uint8_t *data, int size) {
if (size != sizeof(int)) return;
int label = *(int *)data;
//printf("DEBUG: deleting node with label %d\n", label);
graph_node_t *node = hash_lookup(label);
if (!node) return;
hash_remove(label);
free_node(node);
}
void set_metadata(uint8_t *data, int size) {
if (size < 2*sizeof(int)) return;
int label = *(int *)data;
int metadata_len = *(int *)(data + sizeof(int));
//printf("DEBUG: setting metadata for node %d mdlen %d\n", label, metadata_len);
if (size != 2*sizeof(int) + metadata_len) return;
graph_node_t *node = hash_lookup(label);
//printf("DEBUG: node lookup returned %p\n", node);
if (!node) return;
if (node->metadata) {
//printf("DEBUG: node has pre-existing metadata, freeing\n");
free(node->metadata);
node->metadata = NULL;
}
uint8_t *metadata = malloc(metadata_len);
if (!metadata) return;
memcpy(metadata, data + 2*sizeof(int), metadata_len);
node->metadata = metadata;
}
void add_edge(uint8_t *data, int size) {
if (size != 2*sizeof(int)) return;
int src_label = *(int *)data;
int dst_label = *(int *)(data + sizeof(int));
graph_node_t *src_node = hash_lookup(src_label);
if (!src_node) return;
graph_node_t *dst_node = hash_lookup(dst_label);
if (!dst_node) return;
src_node->num_neighbors++;
// Increase the neighbor array size
src_node->neighbors = realloc(src_node->neighbors,
src_node->num_neighbors * sizeof(graph_node_t *));
if (!src_node->neighbors) {
src_node->num_neighbors--;
return;
}
src_node->neighbors[src_node->num_neighbors-1] = dst_node;
}
void del_edge(uint8_t *data, int size) {
if (size != 2*sizeof(int)) return;
int src_label = *(int *)data;
int dst_label = *(int *)(data + sizeof(int));
graph_node_t *src_node = hash_lookup(src_label);
if (!src_node) return;
graph_node_t *dst_node = hash_lookup(dst_label);
if (!dst_node) return;
int i;
for (i = 0; i < src_node->num_neighbors; i++) {
if (src_node->neighbors[i] == dst_node) break;
}
// If not found ignore
if (i == src_node->num_neighbors) return;
// Shift array down by one
memmove(src_node->neighbors+i,
src_node->neighbors+i+1,
src_node->num_neighbors-(i+1));
// Resize array
src_node->num_neighbors--;
graph_node_t **tmp = realloc(src_node->neighbors,
src_node->num_neighbors * sizeof(graph_node_t *));
// Need to distinguish between realloc failure and the case
// where we simply resized the array down to 0
if (!tmp && src_node->num_neighbors) {
src_node->num_neighbors++;
return;
}
src_node->neighbors = tmp;
}
static void print_node_internal(graph_node_t *node) {
#ifndef FUZZER
printf("[node @%p with label %d]\n", node, node->label);
#endif
for (int i = 0; i < node->num_neighbors; i++) {
#ifndef FUZZER
printf("[edge %d -> %d]\n", node->label, node->neighbors[i]->label);
#endif
}
}
void print_graph(uint8_t *data, int size) {
graph_node_t *cursor;
for(int i = 0; i < HASH_SIZE; i++) {
cursor = graph.buckets[i];
while(cursor) {
print_node_internal(cursor);
cursor = cursor->hash_next;
}
}
}
void print_node(uint8_t *data, int size) {
if (size != sizeof(int)) return;
int label = *(int *)data;
graph_node_t *node = hash_lookup(label);
if (!node) return;
print_node_internal(node);
}
void free_graph() {
graph_node_t *cursor;
for(int i = 0; i < HASH_SIZE; i++) {
cursor = graph.buckets[i];
while(cursor) {
graph_node_t *tmp = cursor;
cursor = cursor->hash_next;
free_node(tmp);
}
graph.buckets[i] = NULL;
}
}
void test_hashtable() {
for (int i = 0; i < 10000; i++) {
add_node((uint8_t *)&i, sizeof(i));
}
for (int i = 0; i < 10000-1; i++) {
int buf[2] = {i, i+1};
add_edge((uint8_t *)buf, sizeof(buf));
buf[0] = i+1;
buf[1] = i;
add_edge((uint8_t *)buf, sizeof(buf));
}
for (int i = 0; i < 10000-1; i++) {
int buf[2] = {i, i+1};
del_edge((uint8_t *)buf, sizeof(buf));
}
for (int i = 0; i < 10000; i++) {
assert(hash_lookup(i) != NULL);
}
for (int i = 0; i < 10000; i++) {
del_node((uint8_t *)&i, sizeof(i));
}
free_graph();
}
#ifndef FUZZER
int main(int argc, char **argv) {
int err = 0;
if (argc < 2) {
test_hashtable();
fprintf(stderr, "usage: %s <file>\n", argv[0]);
err = 1; goto cleanup;
}
FILE *f = fopen(argv[1], "rb");
if (!f) {
perror("fopen");
err = 1; goto cleanup;
}
while (!feof(f)) {
uint16_t opcode;
uint16_t size;
int n;
n = fread(&opcode, sizeof(uint16_t), 1, f);
if (!n) {
fprintf(stderr, "failed to read from file\n");
err = 1; goto cleanup;
}
n = fread(&size, sizeof(uint16_t), 1, f);
if (!n) {
fprintf(stderr, "failed to read from file\n");
err = 1; goto cleanup;
}
uint8_t *data = malloc(size);
if (!data) {
perror("malloc");
err = 1; goto cleanup;
}
n = fread(data, size, 1, f);
if (!n) {
fprintf(stderr, "failed to read from file\n");
err = 1; goto cleanup;
}
if (opcode >= LAST_OPCODE) {
fprintf(stderr, "note: invalid opcode, skipping\n");
free(data);
continue;
}
//printf("DEBUG: opcode %s size %d\n", opcode_names[opcode], size);
opcode_handlers[opcode](data, size);
free(data);
}
cleanup:
free_graph();
return err;
}
#else
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
int i = 0;
uint8_t *dp = (uint8_t *)Data;
while (i < Size) {
if (Size - i < sizeof(uint16_t)) goto cleanup;
uint16_t opcode = *(uint16_t *)(dp+i); i += sizeof(uint16_t);
if (Size - i < sizeof(uint16_t)) goto cleanup;
uint16_t cmd_size = *(uint16_t *)(dp+i); i += sizeof(uint16_t);
if (Size - i < cmd_size) goto cleanup;
if (opcode >= LAST_OPCODE) {
i += cmd_size;
continue;
}
//printf("DEBUG: opcode %s size %d\n", opcode_names[opcode], cmd_size);
opcode_handlers[opcode](dp+i, cmd_size);
i += cmd_size;
}
cleanup:
free_graph();
return 0;
}
#endif