1 /**
2 * License: MIT
3 */
4 module ossfuzz.standaloneengine;
5
6
7 private static import core.memory;
8 private static import core.stdc.stdio;
9 private static import ossfuzz.json_load_dump_fuzzer;
10 private static import std.string;
11
12 /**
13 * Main procedure for standalone fuzzing engine.
14 *
15 * Reads filenames from the argument array. For each filename, read the file
16 * into memory and then call the fuzzing interface with the data.
17 */
18 void main(string[] argv)
19
20 do
21 {
22 for (size_t ii = 1; ii < argv.length; ii++) {
23 core.stdc.stdio.printf("[%s] ", std..string.toStringz(argv[ii]));
24
25 /* Try and open the file. */
26 core.stdc.stdio.FILE* infile = core.stdc.stdio.fopen(std..string.toStringz(argv[ii]), "rb");
27
28 if (infile != null) {
29 ubyte* buffer = null;
30
31 core.stdc.stdio.printf("Opened.. ");
32
33 /* Get the length of the file. */
34 core.stdc.stdio.fseek(infile, 0L, core.stdc.stdio.SEEK_END);
35 size_t buffer_len = core.stdc.stdio.ftell(infile);
36
37 /* Reset the file indicator to the beginning of the file. */
38 core.stdc.stdio.fseek(infile, 0L, core.stdc.stdio.SEEK_SET);
39
40 /* Allocate a buffer for the file contents. */
41 buffer = cast(ubyte*)(core.memory.pureCalloc(buffer_len, ubyte.sizeof));
42
43 if (buffer != null) {
44 /* Read all the text from the file into the buffer. */
45 core.stdc.stdio.fread(buffer, ubyte.sizeof, buffer_len, infile);
46 core.stdc.stdio.printf("Read %zu bytes, fuzzing.. ", buffer_len);
47
48 /* Call the fuzzer with the data. */
49 ossfuzz.json_load_dump_fuzzer.LLVMFuzzerTestOneInput(buffer, buffer_len);
50
51 core.stdc.stdio.printf("complete !!");
52
53 /* Free the buffer as it's no longer needed. */
54 core.memory.pureFree(buffer);
55 } else {
56 core.stdc.stdio.fprintf(core.stdc.stdio.stderr, "[%s] Failed to allocate %zu bytes \n", std..string.toStringz(argv[ii]), buffer_len);
57 }
58
59 /* Close the file as it's no longer needed. */
60 core.stdc.stdio.fclose(infile);
61 infile = null;
62 } else {
63 /* Failed to open the file. Maybe wrong name or wrong permissions? */
64 core.stdc.stdio.fprintf(core.stdc.stdio.stderr, "[%s] Open failed. \n", std..string.toStringz(argv[ii]));
65 }
66
67 core.stdc.stdio.printf("\n");
68 }
69 }