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;
160 gobject_class = (GObjectClass *) klass;
162 parent_class = g_type_class_peek_parent (klass);
164 gobject_class->set_property = gst_efence_set_property;
165 gobject_class->get_property = gst_efence_get_property;
167 g_object_class_install_property (gobject_class, ARG_FENCE_TOP,
168 g_param_spec_boolean ("fence_top", "Fence Top",
169 "Align buffers with top of fenced region", TRUE, G_PARAM_READWRITE));
172 /* initialize the new element
173 * instantiate pads and add them to element
175 * initialize structure
178 gst_efence_init (GstEFence * filter)
181 gst_pad_new_from_static_template (&gst_efence_sink_factory, "sink");
182 gst_pad_set_getcaps_function (filter->sinkpad,
183 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
184 gst_pad_set_setcaps_function (filter->sinkpad,
185 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
186 gst_pad_set_chain_function (filter->sinkpad,
187 GST_DEBUG_FUNCPTR (gst_efence_chain));
188 gst_pad_set_bufferalloc_function (filter->sinkpad,
189 GST_DEBUG_FUNCPTR (gst_efence_buffer_alloc));
190 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
193 gst_pad_new_from_static_template (&gst_efence_src_factory, "src");
194 gst_pad_set_getcaps_function (filter->srcpad,
195 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
196 gst_pad_set_setcaps_function (filter->srcpad,
197 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
198 gst_pad_set_checkgetrange_function (filter->srcpad,
199 GST_DEBUG_FUNCPTR (gst_efence_checkgetrange));
200 gst_pad_set_getrange_function (filter->srcpad,
201 GST_DEBUG_FUNCPTR (gst_efence_getrange));
202 gst_pad_set_activatepull_function (filter->srcpad,
203 GST_DEBUG_FUNCPTR (gst_efence_activate_src_pull));
205 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
207 filter->fence_top = TRUE;
211 * this function does the actual processing
215 gst_efence_chain (GstPad * pad, GstBuffer * buffer)
220 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
221 g_return_val_if_fail (GST_IS_EFENCE (efence), GST_FLOW_ERROR);
223 if (GST_IS_FENCED_BUFFER (buffer)) {
224 GST_DEBUG_OBJECT (efence, "Passing on existing fenced buffer with caps %"
225 GST_PTR_FORMAT, GST_BUFFER_CAPS (buffer));
226 return gst_pad_push (efence->srcpad, buffer);
229 copy = (GstBuffer *) gst_fenced_buffer_copy (buffer);
231 GST_DEBUG_OBJECT (efence, "Pushing newly fenced buffer with caps %"
232 GST_PTR_FORMAT ", data=%p, size=%u", GST_BUFFER_CAPS (copy),
233 GST_BUFFER_DATA (copy), GST_BUFFER_SIZE (copy));
235 gst_buffer_unref (buffer);
237 return gst_pad_push (efence->srcpad, copy);
241 gst_efence_getrange (GstPad * pad, guint64 offset,
242 guint length, GstBuffer ** buffer)
249 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
251 peer = gst_pad_get_peer (efence->sinkpad);
253 return GST_FLOW_NOT_LINKED;
255 if ((ret = gst_pad_get_range (peer, offset, length, buffer)) != GST_FLOW_OK)
258 ownbuf = (GstBuffer *) gst_fenced_buffer_copy (*buffer);
259 gst_buffer_unref ((GstBuffer *) * buffer);
263 gst_object_unref (peer);
268 gst_efence_checkgetrange (GstPad * pad)
270 GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
272 return gst_pad_check_pull_range (efence->sinkpad);
276 gst_efence_activate_src_pull (GstPad * pad, gboolean active)
278 GstEFence *efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
280 return gst_pad_activate_pull (efence->sinkpad, active);
284 gst_efence_buffer_alloc (GstPad * pad, guint64 offset,
285 guint size, GstCaps * caps, GstBuffer ** buf)
290 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
291 g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
293 efence = GST_EFENCE (GST_OBJECT_PARENT (pad));
295 buffer = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
297 GST_BUFFER_DATA (buffer) = gst_fenced_buffer_alloc (buffer, size,
299 GST_BUFFER_SIZE (buffer) = size;
300 GST_BUFFER_OFFSET (buffer) = offset;
303 gst_buffer_set_caps (buffer, caps);
307 GST_DEBUG_OBJECT (efence, "Allocated buffer of size %u, caps: %"
308 GST_PTR_FORMAT, GST_BUFFER_SIZE (buffer), GST_BUFFER_CAPS (buffer));
314 gst_efence_set_property (GObject * object, guint prop_id,
315 const GValue * value, GParamSpec * pspec)
319 g_return_if_fail (GST_IS_EFENCE (object));
320 filter = GST_EFENCE (object);
324 filter->fence_top = g_value_get_boolean (value);
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333 gst_efence_get_property (GObject * object, guint prop_id,
334 GValue * value, GParamSpec * pspec)
338 g_return_if_fail (GST_IS_EFENCE (object));
339 filter = GST_EFENCE (object);
343 g_value_set_boolean (value, filter->fence_top);
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 /* entry point to initialize the plug-in
352 * initialize the plug-in itself
353 * register the element factories and pad templates
354 * register the features
357 plugin_init (GstPlugin * plugin)
359 if (!gst_element_register (plugin, "efence", GST_RANK_NONE, GST_TYPE_EFENCE))
362 GST_DEBUG_CATEGORY_INIT (gst_efence_debug, "efence", 0,
363 "Debug output from the efence element");
365 /* plugin initialisation succeeded */
369 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
372 "This element converts a stream of normal GStreamer buffers into a "
373 "stream of buffers that are allocated in such a way that out-of-bounds "
374 "access to data in the buffer is more likely to cause segmentation "
375 "faults. This allocation method is very similar to the debugging tool "
376 "\"Electric Fence\".",
377 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
380 static GstBufferClass *fenced_buffer_parent_class = NULL;
383 gst_fenced_buffer_finalize (GstFencedBuffer * buffer)
385 GstFencedBuffer *fenced_buffer;
387 GST_DEBUG ("free buffer=%p", buffer);
389 fenced_buffer = GST_FENCED_BUFFER (buffer);
392 if (GST_BUFFER_DATA (buffer)) {
393 GST_DEBUG ("free region %p %d", fenced_buffer->region,
394 fenced_buffer->length);
395 munmap (fenced_buffer->region, fenced_buffer->length);
398 GST_MINI_OBJECT_CLASS (fenced_buffer_parent_class)->finalize (GST_MINI_OBJECT
402 static GstFencedBuffer *
403 gst_fenced_buffer_copy (const GstBuffer * buffer)
409 g_return_val_if_fail (buffer != NULL, NULL);
411 /* create a fresh new buffer */
412 copy = (GstBuffer *) gst_mini_object_new (GST_TYPE_FENCED_BUFFER);
414 /* we simply copy everything from our parent */
415 ptr = gst_fenced_buffer_alloc (GST_BUFFER (copy),
416 GST_BUFFER_SIZE (buffer), TRUE);
417 memcpy (ptr, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
419 /* copy relevant flags */
420 mask = GST_BUFFER_FLAG_PREROLL | GST_BUFFER_FLAG_IN_CAPS |
421 GST_BUFFER_FLAG_DELTA_UNIT;
422 GST_MINI_OBJECT (copy)->flags |= GST_MINI_OBJECT (buffer)->flags & mask;
424 GST_BUFFER_DATA (copy) = ptr;
425 GST_BUFFER_SIZE (copy) = GST_BUFFER_SIZE (buffer);
426 GST_BUFFER_TIMESTAMP (copy) = GST_BUFFER_TIMESTAMP (buffer);
427 GST_BUFFER_DURATION (copy) = GST_BUFFER_DURATION (buffer);
428 GST_BUFFER_OFFSET (copy) = GST_BUFFER_OFFSET (buffer);
429 GST_BUFFER_OFFSET_END (copy) = GST_BUFFER_OFFSET_END (buffer);
431 if (GST_BUFFER_CAPS (buffer))
432 GST_BUFFER_CAPS (copy) = gst_caps_ref (GST_BUFFER_CAPS (buffer));
434 GST_BUFFER_CAPS (copy) = NULL;
436 GST_DEBUG ("Copied buffer %p with ts %" GST_TIME_FORMAT
437 ", caps: %" GST_PTR_FORMAT, buffer,
438 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (copy)), GST_BUFFER_CAPS (copy));
440 return GST_FENCED_BUFFER (copy);
444 gst_fenced_buffer_alloc (GstBuffer * buffer, unsigned int length,
449 GstFencedBuffer *fenced_buffer = (GstFencedBuffer *) buffer;
452 GST_DEBUG ("buffer=%p length=%d fence_top=%d", buffer, length, fence_top);
458 page_size = sysconf (_SC_PAGESIZE);
460 page_size = getpagesize ();
463 /* Allocate a complete page, and one on either side */
464 alloc_size = ((length - 1) & ~(page_size - 1)) + page_size;
465 alloc_size += 2 * page_size;
467 region = mmap (NULL, alloc_size, PROT_READ | PROT_WRITE,
468 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
469 if (region == MAP_FAILED) {
470 g_warning ("mmap failed");
474 munmap (region, page_size);
475 munmap (region + alloc_size - page_size, page_size);
477 fenced_buffer->region = region + page_size;
478 fenced_buffer->length = alloc_size - page_size;
480 mprotect (region, page_size, PROT_NONE);
481 mprotect ((char *) region + alloc_size - page_size, page_size, PROT_NONE);
483 fenced_buffer->region = region;
484 fenced_buffer->length = alloc_size;
487 GST_DEBUG ("new region %p %d", fenced_buffer->region, fenced_buffer->length);
492 /* Align to top of region, but force alignment to 4 bytes */
493 offset = alloc_size - page_size - length;
495 return (void *) ((char *) region + offset);
497 return (void *) ((char *) region + page_size);
502 gst_fenced_buffer_class_init (gpointer g_class, gpointer class_data)
504 GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (g_class);
506 fenced_buffer_parent_class = g_type_class_peek_parent (g_class);
508 mini_object_class->finalize =
509 (GstMiniObjectFinalizeFunction) gst_fenced_buffer_finalize;
510 mini_object_class->copy = (GstMiniObjectCopyFunction) gst_fenced_buffer_copy;
514 gst_fenced_buffer_get_type (void)
516 static GType fenced_buf_type = 0;
518 if (G_UNLIKELY (!fenced_buf_type)) {
519 static const GTypeInfo fenced_buf_info = {
520 sizeof (GstBufferClass),
523 (GClassInitFunc) gst_fenced_buffer_class_init,
526 sizeof (GstFencedBuffer),
531 fenced_buf_type = g_type_register_static (GST_TYPE_BUFFER,
532 "GstFencedBuffer", &fenced_buf_info, 0);
534 return fenced_buf_type;