aboutsummaryrefslogtreecommitdiff
path: root/test/ossfuzz/standaloneengine.cc
diff options
context:
space:
mode:
authorMax Dymond <cmeister2@gmail.com>2019-07-18 14:49:07 +0100
committerMax Dymond <cmeister2@gmail.com>2019-07-19 09:52:37 +0100
commit24cc9dd0789d368861d750e0ebdda8747a0597d0 (patch)
treedf016ba189735631e8901292f60d7d8a556892e8 /test/ossfuzz/standaloneengine.cc
parent100e5549b6e27bc48d6133be0941ea6bfd4b75ac (diff)
downloadjansson-24cc9dd0789d368861d750e0ebdda8747a0597d0.zip
jansson-24cc9dd0789d368861d750e0ebdda8747a0597d0.tar.gz
jansson-24cc9dd0789d368861d750e0ebdda8747a0597d0.tar.bz2
Move ossfuzz directory and use Makefile.am
Diffstat (limited to 'test/ossfuzz/standaloneengine.cc')
-rw-r--r--test/ossfuzz/standaloneengine.cc74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/ossfuzz/standaloneengine.cc b/test/ossfuzz/standaloneengine.cc
new file mode 100644
index 0000000..175360e
--- /dev/null
+++ b/test/ossfuzz/standaloneengine.cc
@@ -0,0 +1,74 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "testinput.h"
+
+/**
+ * Main procedure for standalone fuzzing engine.
+ *
+ * Reads filenames from the argument array. For each filename, read the file
+ * into memory and then call the fuzzing interface with the data.
+ */
+int main(int argc, char **argv)
+{
+ int ii;
+ for(ii = 1; ii < argc; ii++)
+ {
+ FILE *infile;
+ printf("[%s] ", argv[ii]);
+
+ /* Try and open the file. */
+ infile = fopen(argv[ii], "rb");
+ if(infile)
+ {
+ uint8_t *buffer = NULL;
+ size_t buffer_len;
+
+ printf("Opened.. ");
+
+ /* Get the length of the file. */
+ fseek(infile, 0L, SEEK_END);
+ buffer_len = ftell(infile);
+
+ /* Reset the file indicator to the beginning of the file. */
+ fseek(infile, 0L, SEEK_SET);
+
+ /* Allocate a buffer for the file contents. */
+ buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
+ if(buffer)
+ {
+ /* Read all the text from the file into the buffer. */
+ fread(buffer, sizeof(uint8_t), buffer_len, infile);
+ printf("Read %zu bytes, fuzzing.. ", buffer_len);
+
+ /* Call the fuzzer with the data. */
+ LLVMFuzzerTestOneInput(buffer, buffer_len);
+
+ printf("complete !!");
+
+ /* Free the buffer as it's no longer needed. */
+ free(buffer);
+ buffer = NULL;
+ }
+ else
+ {
+ fprintf(stderr,
+ "[%s] Failed to allocate %zu bytes \n",
+ argv[ii],
+ buffer_len);
+ }
+
+ /* Close the file as it's no longer needed. */
+ fclose(infile);
+ infile = NULL;
+ }
+ else
+ {
+ /* Failed to open the file. Maybe wrong name or wrong permissions? */
+ fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
+ }
+
+ printf("\n");
+ }
+}