2 * Copyright 2016 Google Inc.
3 * author: Edward Hervey <bilboed@bilboed.com>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
29 #ifndef LOCAL_FUZZ_BUILD
30 GST_PLUGIN_STATIC_DECLARE (coreelements);
31 GST_PLUGIN_STATIC_DECLARE (typefindfunctions);
32 GST_PLUGIN_STATIC_DECLARE (app);
35 /* push-based typefind fuzzing target
37 * This application can be compiled with libFuzzer to simulate
38 * a push-based typefind execution.
40 * To reproduce the failing behaviour, use:
41 * $ gst-launch-1.0 pushfile:///.. ! typefind ! fakesink
43 * The goal is to cover typefind code and implementation.
47 custom_logger (const gchar * log_domain,
48 GLogLevelFlags log_level, const gchar * message, gpointer unused_data)
50 if (log_level & G_LOG_LEVEL_CRITICAL) {
51 g_printerr ("CRITICAL ERROR : %s\n", message);
53 } else if (log_level & G_LOG_LEVEL_WARNING) {
54 g_printerr ("WARNING : %s\n", message);
59 LLVMFuzzerTestOneInput (const guint8 * data, size_t size)
62 static gboolean initialized = FALSE;
63 GstElement *pipeline, *source, *typefind, *fakesink;
65 GstFlowReturn flowret;
69 /* We want critical warnings to assert so we can fix them */
70 g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
71 g_log_set_default_handler (custom_logger, NULL);
73 /* Only initialize and register plugins once */
74 gst_init (NULL, NULL);
76 #ifndef LOCAL_FUZZ_BUILD
77 GST_PLUGIN_STATIC_REGISTER (coreelements);
78 GST_PLUGIN_STATIC_REGISTER (typefindfunctions);
79 GST_PLUGIN_STATIC_REGISTER (app);
85 /* Create the pipeline */
86 pipeline = gst_pipeline_new ("pipeline");
87 source = gst_element_factory_make ("appsrc", "source");
88 typefind = gst_element_factory_make ("typefind", "typefind");
89 fakesink = gst_element_factory_make ("fakesink", "fakesink");
91 gst_bin_add_many (GST_BIN (pipeline), source, typefind, fakesink, NULL);
92 gst_element_link_many (source, typefind, fakesink, NULL);
94 /* Set pipeline to READY so we can provide data to appsrc */
95 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
96 buf = gst_buffer_new_wrapped_full (0, (gpointer) data, size,
98 g_object_set (G_OBJECT (source), "size", size, NULL);
99 g_signal_emit_by_name (G_OBJECT (source), "push-buffer", buf, &flowret);
100 gst_buffer_unref (buf);
102 /* Set pipeline to PAUSED and wait (typefind will either fail or succeed) */
103 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
105 /* wait until state change either completes or fails */
106 gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL, -1);
108 /* Go back to NULL */
109 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
111 /* And release the pipeline */
112 gst_object_unref (pipeline);