Cleanup root directory from misc files
[platform/upstream/gstreamer.git] / ci / fuzzing / typefind.c
1 /*
2  * Copyright 2016 Google Inc.
3  * author: Edward Hervey <bilboed@bilboed.com>
4  * 
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <locale.h>
24
25 #include <stdlib.h>
26 #include <glib.h>
27 #include <gst/gst.h>
28
29 #ifndef LOCAL_FUZZ_BUILD
30 GST_PLUGIN_STATIC_DECLARE (coreelements);
31 GST_PLUGIN_STATIC_DECLARE (typefindfunctions);
32 GST_PLUGIN_STATIC_DECLARE (app);
33 #endif
34
35 /* push-based typefind fuzzing target
36  *
37  * This application can be compiled with libFuzzer to simulate
38  * a push-based typefind execution.
39  *
40  * To reproduce the failing behaviour, use:
41  * $ gst-launch-1.0 pushfile:///.. ! typefind ! fakesink
42  *
43  * The goal is to cover typefind code and implementation.
44  *
45  **/
46 static void
47 custom_logger (const gchar * log_domain,
48     GLogLevelFlags log_level, const gchar * message, gpointer unused_data)
49 {
50   if (log_level & G_LOG_LEVEL_CRITICAL) {
51     g_printerr ("CRITICAL ERROR : %s\n", message);
52     abort ();
53   } else if (log_level & G_LOG_LEVEL_WARNING) {
54     g_printerr ("WARNING : %s\n", message);
55   }
56 }
57
58 int
59 LLVMFuzzerTestOneInput (const guint8 * data, size_t size)
60 {
61   GError *err = NULL;
62   static gboolean initialized = FALSE;
63   GstElement *pipeline, *source, *typefind, *fakesink;
64   GstBuffer *buf;
65   GstFlowReturn flowret;
66   GstState state;
67
68   if (!initialized) {
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);
72
73     /* Only initialize and register plugins once */
74     gst_init (NULL, NULL);
75
76 #ifndef LOCAL_FUZZ_BUILD
77     GST_PLUGIN_STATIC_REGISTER (coreelements);
78     GST_PLUGIN_STATIC_REGISTER (typefindfunctions);
79     GST_PLUGIN_STATIC_REGISTER (app);
80 #endif
81
82     initialized = TRUE;
83   }
84
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");
90
91   gst_bin_add_many (GST_BIN (pipeline), source, typefind, fakesink, NULL);
92   gst_element_link_many (source, typefind, fakesink, NULL);
93
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,
97       0, size, NULL, NULL);
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);
101
102   /* Set pipeline to PAUSED and wait (typefind will either fail or succeed) */
103   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
104
105   /* wait until state change either completes or fails */
106   gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL, -1);
107
108   /* Go back to NULL */
109   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
110
111   /* And release the pipeline */
112   gst_object_unref (pipeline);
113
114   return 0;
115 }