fuzzing: Add local fuzz runner
authorEdward Hervey <edward@centricular.com>
Wed, 6 Dec 2017 07:41:59 +0000 (08:41 +0100)
committerEdward Hervey <bilboed@bilboed.com>
Wed, 6 Dec 2017 07:45:27 +0000 (08:45 +0100)
Allows testing without full oss-fuzz infrastructure

fuzzing/gst-discoverer.c
fuzzing/localfuzzer.c [new file with mode: 0644]
fuzzing/typefind.c

index 519537a..17c9159 100644 (file)
@@ -27,6 +27,7 @@
 #include <gst/gst.h>
 #include <gst/pbutils/pbutils.h>
 
+#ifndef LOCAL_FUZZ_BUILD
 GST_PLUGIN_STATIC_DECLARE(coreelements);
 GST_PLUGIN_STATIC_DECLARE(playback);
 GST_PLUGIN_STATIC_DECLARE(typefindfunctions);
@@ -34,6 +35,7 @@ GST_PLUGIN_STATIC_DECLARE(app);
 GST_PLUGIN_STATIC_DECLARE(ogg);
 GST_PLUGIN_STATIC_DECLARE(theora);
 GST_PLUGIN_STATIC_DECLARE(vorbis);
+#endif
 
 /* push-based discoverer fuzzing target
  *
@@ -97,7 +99,8 @@ int LLVMFuzzerTestOneInput(const guint8 *data, size_t size)
 
     /* Only initialize and register plugins once */
     gst_init (NULL, NULL);
-    
+
+#ifndef LOCAL_FUZZ_BUILD
     GST_PLUGIN_STATIC_REGISTER(coreelements);
     GST_PLUGIN_STATIC_REGISTER(playback);
     GST_PLUGIN_STATIC_REGISTER(typefindfunctions);
@@ -105,6 +108,7 @@ int LLVMFuzzerTestOneInput(const guint8 *data, size_t size)
     GST_PLUGIN_STATIC_REGISTER(ogg);
     GST_PLUGIN_STATIC_REGISTER(theora);
     GST_PLUGIN_STATIC_REGISTER(vorbis);
+#endif
 
     initialized = TRUE;
   }
diff --git a/fuzzing/localfuzzer.c b/fuzzing/localfuzzer.c
new file mode 100644 (file)
index 0000000..188d246
--- /dev/null
@@ -0,0 +1,78 @@
+/* GStreamer
+ * Copyright (C) 2017 Edward Hervey <bilboed@bilboed.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+/* Local fuzzer runner */
+#include <glib.h>
+
+extern int LLVMFuzzerTestOneInput (const guint8 * data, size_t size);
+
+static void
+test_file (gchar * filename)
+{
+  GDir *dir;
+  gchar *path;
+  gchar *contents;
+  gsize length;
+
+  /* if filename is a directory, process the contents */
+  if ((dir = g_dir_open (filename, 0, NULL))) {
+    const gchar *entry;
+
+    while ((entry = g_dir_read_name (dir))) {
+      gchar *spath;
+
+      spath = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
+      test_file (spath);
+      g_free (spath);
+    }
+
+    g_dir_close (dir);
+    return;
+  }
+
+  /* Make sure path is absolute */
+  if (!g_path_is_absolute (filename)) {
+    gchar *curdir;
+
+    curdir = g_get_current_dir ();
+    path = g_build_filename (curdir, filename, NULL);
+    g_free (curdir);
+  } else
+    path = g_strdup (filename);
+
+  /* Check if path exists */
+  if (g_file_get_contents (path, &contents, &length, NULL)) {
+    g_print (">>> %s (%" G_GSIZE_FORMAT " bytes)\n", path, length);
+    LLVMFuzzerTestOneInput ((const guint8 *) contents, length);
+    g_free (contents);
+  }
+
+  g_free (path);
+}
+
+int
+main (int argc, gchar ** argv)
+{
+  gint i;
+
+  for (i = 1; i < argc; i++)
+    test_file (argv[i]);
+
+  return 0;
+}
index 16615ef..3d5d14c 100644 (file)
 #include <glib.h>
 #include <gst/gst.h>
 
+#ifndef LOCAL_FUZZ_BUILD
 GST_PLUGIN_STATIC_DECLARE (coreelements);
 GST_PLUGIN_STATIC_DECLARE (typefindfunctions);
 GST_PLUGIN_STATIC_DECLARE (app);
+#endif
 
 /* push-based typefind fuzzing target
  *
@@ -73,9 +75,11 @@ LLVMFuzzerTestOneInput (const guint8 * data, size_t size)
     /* Only initialize and register plugins once */
     gst_init (NULL, NULL);
 
+#ifndef LOCAL_FUZZ_BUILD
     GST_PLUGIN_STATIC_REGISTER (coreelements);
     GST_PLUGIN_STATIC_REGISTER (typefindfunctions);
     GST_PLUGIN_STATIC_REGISTER (app);
+#endif
 
     initialized = TRUE;
   }