3 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) 2002 David A. Schleef <ds@schleef.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
35 #define MAP_ANONYMOUS MAP_ANON
37 /* assume we don't need it */
38 #define MAP_ANONYMOUS 0
42 GST_DEBUG_CATEGORY_STATIC (gst_efence_debug);
43 #define GST_CAT_DEFAULT gst_efence_debug
45 static const GstElementDetails plugin_details =
46 GST_ELEMENT_DETAILS ("Electric Fence",
48 "This element converts a stream of normal GStreamer buffers into a "
49 "stream of buffers that are allocated in such a way that out-of-bounds "
50 "access to data in the buffer is more likely to cause segmentation "
51 "faults. This allocation method is very similar to the debugging tool "
52 "\"Electric Fence\".",
53 "David A. Schleef <ds@schleef.org>");
55 /* Filter signals and args */
68 static GstStaticPadTemplate gst_efence_sink_factory =
69 GST_STATIC_PAD_TEMPLATE ("sink",
74 static GstStaticPadTemplate gst_efence_src_factory =
75 GST_STATIC_PAD_TEMPLATE ("src",
80 static void gst_efence_base_init (gpointer g_class);
81 static void gst_efence_class_init (GstEFenceClass * klass);
82 static void gst_efence_init (GstEFence * filter);
84 static void gst_efence_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_efence_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
89 static GstFlowReturn gst_efence_chain (GstPad * pad, GstBuffer * buf);
90 static GstFlowReturn gst_efence_getrange (GstPad * pad, guint64 offset,
91 guint length, GstBuffer ** buffer);
92 static gboolean gst_efence_checkgetrange (GstPad * pad);
93 static gboolean gst_efence_activate_src_pull (GstPad * pad, gboolean active);
95 static GstElementClass *parent_class = NULL;
97 typedef struct _GstFencedBuffer GstFencedBuffer;
98 struct _GstFencedBuffer
105 GType gst_fenced_buffer_get_type (void);
106 static void gst_fenced_buffer_finalize (GstFencedBuffer * buf);
107 static GstFencedBuffer *gst_fenced_buffer_copy (const GstBuffer * buffer);
108 static void *gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
110 static GstFlowReturn gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
111 guint size, GstCaps * caps, GstBuffer ** buf);
113 #define GST_TYPE_FENCED_BUFFER (gst_fenced_buffer_get_type())
115 #define GST_IS_FENCED_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FENCED_BUFFER))
116 #define GST_FENCED_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_FENCED_BUFFER, GstFencedBuffer))
119 gst_gst_efence_get_type (void)
121 static GType plugin_type = 0;
124 static const GTypeInfo plugin_info = {
125 sizeof (GstEFenceClass),
126 gst_efence_base_init,
128 (GClassInitFunc) gst_efence_class_init,
133 (GInstanceInitFunc) gst_efence_init,
136 plugin_type = g_type_register_static (GST_TYPE_ELEMENT,
137 "GstEFence", &plugin_info, 0);
143 gst_efence_base_init (gpointer g_class)
145 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
147 gst_element_class_add_pad_template (element_class,
148 gst_static_pad_template_get (&gst_efence_sink_factory));
149 gst_element_class_add_pad_template (element_class,
150 gst_static_pad_template_get (&gst_efence_src_factory));
151 gst_element_class_set_details (element_class, &plugin_details);
154 /* initialize the plugin's class */
156 gst_efence_class_init (GstEFenceClass * klass)
158 GObjectClass *gobject_class;
159 GstElementClass *gstelement_class;
161 gobject_class = (GObjectClass *) klass;
162 gstelement_class = (GstElementClass *) klass;
164 parent_class = g_type_class_peek_parent (klass);
166 gobject_class->set_property = gst_efence_set_property;
167 gobject_class->get_property = gst_efence_get_property;
169 g_object_class_install_property (gobject_class, ARG_FENCE_TOP,
170 g_param_spec_boolean ("fence_top", "Fence Top",
171 "Align buffers with top of fenced region", TRUE, G_PARAM_READWRITE));
174 /* initialize the new element
175 * instantiate pads and add them to element
177 * initialize structure
180 gst_efence_init (GstEFence * filter)
183 gst_pad_new_from_static_template (&gst_efence_sink_factory, "sink");
184 gst_pad_set_getcaps_function (filter->sinkpad,
185 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
186 gst_pad_set_setcaps_function (filter->sinkpad,
187 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
188 gst_pad_set_chain_function (filter->sinkpad,
189 GST_DEBUG_FUNCPTR (gst_efence_chain));
190 gst_pad_set_bufferalloc_function (filter->sinkpad,
191 GST_DEBUG_FUNCPTR (gst_efence_buffer_alloc));
192 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
195 gst_pad_new_from_static_template (&gst_efence_src_factory, "src");
196 gst_pad_set_getcaps_function (filter->srcpad,
197 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
198 gst_pad_set_setcaps_function (filter->srcpad,
199 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
200 gst_pad_set_checkgetrange_function (filter->srcpad,
201 GST_DEBUG_FUNCPTR (gst_efence_checkgetrange));
202 gst_pad_set_getrange_function (filter->srcpad,
203 GST_DEBUG_FUNCPTR (gst_efence_getrange));
204 gst_pad_set_activatepull_function (filter->srcpad,
205 GST_DEBUG_FUNCPTR (gst_efence_activate_src_pull));
207 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
209 filter->fence_top = TRUE;
213 * this function does the actual processing
217 gst_efence_chain (GstPad * pad, GstBuffer * buffer)
222 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
223 g_return_val_if_fail (GST_IS_EFENCE (efence), GST_FLOW_ERROR);
225 if (GST_IS_FENCED_BUFFER (buffer)) {
226 GST_DEBUG_OBJECT (efence, "Passing on existing fenced buffer with caps %"
227 GST_PTR_FORMAT, GST_BUFFER_CAPS (buffer));
228 return gst_pad_push (efence->srcpad, buffer);
231 copy = (GstBuffer *) gst_fenced_buffer_copy (buffer);
233 GST_DEBUG_OBJECT (efence, "Pushing newly fenced buffer with caps %"
234 GST_PTR_FORMAT ", data=%p, size=%u", GST_BUFFER_CAPS (copy),
235 GST_BUFFER_DATA (copy), GST_BUFFER_SIZE (copy));
237 gst_buffer_unref (buffer);
239 return gst_pad_push (efence->srcpad, copy);
243 gst_efence_getrange (GstPad * pad, guint64 offset,
244 guint length, GstBuffer ** buffer)
251 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
253 peer = gst_pad_get_peer (efence->sinkpad);
255 return GST_FLOW_NOT_LINKED;
257 if ((ret = gst_pad_get_range (peer, offset, length, buffer)) != GST_FLOW_OK)
260 ownbuf = (GstBuffer *) gst_fenced_buffer_copy (*buffer);
261 gst_buffer_unref ((GstBuffer *) * buffer);
265 gst_object_unref (peer);
270 gst_efence_checkgetrange (GstPad * pad)
272 GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
274 return gst_pad_check_pull_range (efence->sinkpad);
278 gst_efence_activate_src_pull (GstPad * pad, gboolean active)
280 GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
282 return gst_pad_activate_pull (efence->sinkpad, active);
286 gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
287 guint size, GstCaps * caps, GstBuffer ** buf)
292 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
293 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
295 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
297 buffer = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
299 GST_BUFFER_DATA (buffer) = gst_fenced_buffer_alloc (buffer, size,
301 GST_BUFFER_SIZE (buffer) = size;
302 GST_BUFFER_OFFSET (buffer) = offset;
305 gst_buffer_set_caps (buffer, caps);
309 GST_DEBUG_OBJECT (efence, "Allocated buffer of size %u, caps: %"
310 GST_PTR_FORMAT, GST_BUFFER_SIZE (buffer), GST_BUFFER_CAPS (buffer));
316 gst_efence_set_property (GObject * object, guint prop_id,
317 const GValue * value, GParamSpec * pspec)
321 g_return_if_fail (GST_IS_EFENCE (object));
322 filter = GST_EFENCE (object);
326 filter->fence_top = g_value_get_boolean (value);
329 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
335 gst_efence_get_property (GObject * object, guint prop_id,
336 GValue * value, GParamSpec * pspec)
340 g_return_if_fail (GST_IS_EFENCE (object));
341 filter = GST_EFENCE (object);
345 g_value_set_boolean (value, filter->fence_top);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
353 /* entry point to initialize the plug-in
354 * initialize the plug-in itself
355 * register the element factories and pad templates
356 * register the features
359 plugin_init (GstPlugin * plugin)
361 if (!gst_element_register (plugin, "efence", GST_RANK_NONE, GST_TYPE_EFENCE))
364 GST_DEBUG_CATEGORY_INIT (gst_efence_debug, "efence", 0,
365 "Debug output from the efence element");
367 /* plugin initialisation succeeded */
371 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
374 "This element converts a stream of normal GStreamer buffers into a "
375 "stream of buffers that are allocated in such a way that out-of-bounds "
376 "access to data in the buffer is more likely to cause segmentation "
377 "faults. This allocation method is very similar to the debugging tool "
378 "\"Electric Fence\".",
379 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
382 static GstBufferClass *fenced_buffer_parent_class = NULL;
385 gst_fenced_buffer_finalize (GstFencedBuffer * buffer)
387 GstFencedBuffer *fenced_buffer;
389 GST_DEBUG ("free buffer=%p", buffer);
391 fenced_buffer = GST_FENCED_BUFFER (buffer);
394 if (GST_BUFFER_DATA (buffer)) {
395 GST_DEBUG ("free region %p %d", fenced_buffer->region,
396 fenced_buffer->length);
397 munmap (fenced_buffer->region, fenced_buffer->length);
400 GST_MINI_OBJECT_CLASS (fenced_buffer_parent_class)->
401 finalize (GST_MINI_OBJECT (buffer));
404 static GstFencedBuffer *
405 gst_fenced_buffer_copy (const GstBuffer * buffer)
411 g_return_val_if_fail (buffer != NULL, NULL);
413 /* create a fresh new buffer */
414 copy = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
416 /* we simply copy everything from our parent */
417 ptr = gst_fenced_buffer_alloc (GST_BUFFER (copy),
418 GST_BUFFER_SIZE (buffer), TRUE);
419 memcpy (ptr, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
421 /* copy relevant flags */
422 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
423 GST_BUFFER_FLAG_DELTA_UNIT;
424 GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
426 GST_BUFFER_DATA (copy) = ptr;
427 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
428 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
429 GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
430 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
431 GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
433 if (GST_BUFFER_CAPS (buffer))
434 GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
436 GST_BUFFER_CAPS (copy) = NULL;
438 GST_DEBUG ("Copied buffer %p with ts %" GST_TIME_FORMAT
439 ", caps: %" GST_PTR_FORMAT, buffer,
440 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (copy)), GST_BUFFER_CAPS (copy));
442 return GST_FENCED_BUFFER (copy);
446 gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
451 GstFencedBuffer *fenced_buffer = (GstFencedBuffer *) buffer;
454 GST_DEBUG ("buffer=%p length=%d fence_top=%d", buffer, length, fence_top);
460 page_size = sysconf (_SC_PAGESIZE);
462 page_size = getpagesize ();
465 /* Allocate a complete page, and one on either side */
466 alloc_size = ((length - 1) & ~(page_size - 1)) + page_size;
467 alloc_size += 2 * page_size;
469 region = mmap (NULL, alloc_size, PROT_READ | PROT_WRITE,
470 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
471 if (region == MAP_FAILED) {
472 g_warning ("mmap failed");
476 munmap (region, page_size);
477 munmap (region + alloc_size - page_size, page_size);
479 fenced_buffer->region = region + page_size;
480 fenced_buffer->length = alloc_size - page_size;
482 mprotect (region, page_size, PROT_NONE);
483 mprotect ((char *) region + alloc_size - page_size, page_size, PROT_NONE);
485 fenced_buffer->region = region;
486 fenced_buffer->length = alloc_size;
489 GST_DEBUG ("new region %p %d", fenced_buffer->region, fenced_buffer->length);
494 /* Align to top of region, but force alignment to 4 bytes */
495 offset = alloc_size - page_size - length;
497 return (void *) ((char *) region + offset);
499 return (void *) ((char *) region + page_size);
504 gst_fenced_buffer_class_init (gpointer g_class, gpointer class_data)
506 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
508 fenced_buffer_parent_class = g_type_class_peek_parent (g_class);
510 mini_object_class->finalize =
511 (GstMiniObjectFinalizeFunction) gst_fenced_buffer_finalize;
512 mini_object_class->copy = (GstMiniObjectCopyFunction) gst_fenced_buffer_copy;
516 gst_fenced_buffer_init (GTypeInstance * instance, gpointer g_class)
521 gst_fenced_buffer_get_type (void)
523 static GType fenced_buf_type = 0;
525 if (G_UNLIKELY (!fenced_buf_type)) {
526 static const GTypeInfo fenced_buf_info = {
527 sizeof (GstBufferClass),
530 (GClassInitFunc) gst_fenced_buffer_class_init,
533 sizeof (GstFencedBuffer),
535 (GInstanceInitFunc) gst_fenced_buffer_init,
538 fenced_buf_type = g_type_register_static (GST_TYPE_BUFFER,
539 "GstFencedBuffer", &fenced_buf_info, 0);
541 return fenced_buf_type;