ci: bump image tags so subprojects get updated
[platform/upstream/gstreamer.git] / ci / fuzzing / gst-discoverer.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 #include <gst/pbutils/pbutils.h>
29
30 /* push-based discoverer fuzzing target
31  *
32  * This application can be compiled with libFuzzer to simulate
33  * a push-based discoverer execution.
34  *
35  * To reproduce the failing behaviour, use:
36  * $ gst-discoverer-1.0 pushfile:///...
37  *
38  * The goal is to cover basic usage of demuxers, parsers and
39  * base decoder elements.
40  *
41  * When compiling, only link the required demuxer/parser/decoder
42  * plugins and keep it to a limited range (ex: ogg/theora/vorbis)
43  *
44  **/
45
46 const guint8 *fuzztesting_data;
47 size_t fuzztesting_size;
48
49 static void
50 appsrc_configuration (GstDiscoverer * dc, GstElement * source, gpointer data)
51 {
52   GstBuffer *buf;
53   GstFlowReturn ret;
54
55   /* Create buffer from fuzztesting_data which shouldn't be freed */
56   buf =
57       gst_buffer_new_wrapped_full (0, (gpointer) fuzztesting_data,
58       fuzztesting_size, 0, fuzztesting_size, NULL, NULL);
59   g_object_set (G_OBJECT (source), "size", fuzztesting_size, NULL);
60   g_signal_emit_by_name (G_OBJECT (source), "push-buffer", buf, &ret);
61   gst_buffer_unref (buf);
62 }
63
64 static void
65 custom_logger (const gchar * log_domain,
66     GLogLevelFlags log_level, const gchar * message, gpointer unused_data)
67 {
68   if (log_level & G_LOG_LEVEL_CRITICAL) {
69     g_printerr ("CRITICAL ERROR : %s\n", message);
70     abort ();
71   } else if (log_level & G_LOG_LEVEL_WARNING) {
72     g_printerr ("WARNING : %s\n", message);
73   }
74 }
75
76 int
77 LLVMFuzzerTestOneInput (const guint8 * data, size_t size)
78 {
79   GError *err = NULL;
80   GstDiscoverer *dc;
81   gint timeout = 10;
82   GstDiscovererInfo *info;
83   static gboolean initialized = FALSE;
84
85   if (!initialized) {
86     /* We want critical warnings to assert so we can fix them */
87     g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
88     g_log_set_default_handler (custom_logger, NULL);
89
90     /* Only initialize and register plugins once */
91     gst_init (NULL, NULL);
92     initialized = TRUE;
93   }
94
95   dc = gst_discoverer_new (timeout * GST_SECOND, &err);
96   if (G_UNLIKELY (dc == NULL)) {
97     g_print ("Error initializing: %s\n", err->message);
98     g_clear_error (&err);
99     exit (1);
100   }
101
102   fuzztesting_data = data;
103   fuzztesting_size = size;
104
105   /* Connect to source-setup signal to give the data */
106   g_signal_connect (dc, "source-setup", (GCallback) appsrc_configuration, NULL);
107
108   info = gst_discoverer_discover_uri (dc, "appsrc://", &err);
109   g_clear_error (&err);
110   if (info)
111     gst_discoverer_info_unref (info);
112
113   g_object_unref (dc);
114
115   return 0;
116 }