After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 623589 - Fix races/refcounting bugs with slave clocks
Fix races/refcounting bugs with slave clocks
Status: RESOLVED FIXED
Product: GStreamer
Classification: Platform
Component: gstreamer (core)
git master
Other Mac OS
: Normal normal
: 0.10.30
Assigned To: GStreamer Maintainers
GStreamer Maintainers
Depends on:
Blocks:
 
 
Reported: 2010-07-05 11:26 UTC by Alessandro Decina
Modified: 2010-07-06 09:24 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
add gst_clock_id_wait_async_full (3.91 KB, patch)
2010-07-05 11:26 UTC, Alessandro Decina
needs-work Details | Review
fix refcounting bug (1.47 KB, patch)
2010-07-05 11:26 UTC, Alessandro Decina
none Details | Review
use gst_clock_id_wait_async_full + tests (7.93 KB, patch)
2010-07-05 11:27 UTC, Alessandro Decina
none Details | Review

Description Alessandro Decina 2010-07-05 11:26:09 UTC
Created attachment 165271 [details] [review]
add gst_clock_id_wait_async_full

I found a race and a refcounting bug using slaved clocks. I'm attacching patches here for review.

I added a new gst_clock_id_wait_async_full function which is now used by gst_clock_set_master. The function is needed to avoid a race between the master's slave_callback and the slave being deallocated.

Also in gst_clock_set_master we were possibly deallocating a clock (the master) before unreffing one of its entries. I fixed that.
Comment 1 Alessandro Decina 2010-07-05 11:26:44 UTC
Created attachment 165272 [details] [review]
fix refcounting bug
Comment 2 Alessandro Decina 2010-07-05 11:27:41 UTC
Created attachment 165273 [details] [review]
use gst_clock_id_wait_async_full + tests
Comment 3 Tim-Philipp Müller 2010-07-05 11:41:18 UTC
Comment on attachment 165271 [details] [review]
add gst_clock_id_wait_async_full

>--- a/gst/gstclock.h
>+++ b/gst/gstclock.h
>@@ -346,6 +346,7 @@ struct _GstClockEntry {
>   GstClockReturn	 status;
>   GstClockCallback	 func;
>   gpointer		 user_data;
>+  GDestroyNotify	 destroy_data;
> };

This patch looks harmless and ok in general, but here we're extending a public struct that has no padding, not sure how I feel about that, wonder what the chances are that someone outthere has subclassed this..
Comment 4 Sebastian Dröge (slomo) 2010-07-05 11:41:38 UTC
Review of attachment 165271 [details] [review]:

::: gst/gstclock.h
@@ +347,3 @@
   GstClockCallback	 func;
   gpointer		 user_data;
+  GDestroyNotify	 destroy_data;

We can't do that, GstClockEntry is public and this changes its size.
Comment 5 Wim Taymans 2010-07-05 11:53:59 UTC
> +  GDestroyNotify     destroy_data;
> 
> We can't do that, GstClockEntry is public and this changes its size.

This is going to fail when someone were to allocate a GstClockID themselves with the slice allocator and so, instead of using the _new_*_id() functions.

If someone would do that, it would have failed previously as well because you need to give the size to the slice allocator to free the entry (and that size would be wrong when someone extended the structure and used the default unref function).

The only way it could have worked previously is when the custom GstClockEntry allocator would keep an extra ref to the entry and free them when cleaning up. 

None of the existing elements that perform sync would ever manage to allocate such an entry..

I'm pretty confident nobody does this and that making the struct bigger is not going to cause trouble.
Comment 6 Tim-Philipp Müller 2010-07-05 13:40:45 UTC
Ok then, let's do it. Maybe also add a sentence to the GstClockEntry docs that makes it clear that the structure should be treated as an opaque structure and must not be subclassed or allocated outside of GStreamer.

Wim, were the other patches also fine with you?
Comment 7 Wim Taymans 2010-07-05 13:49:11 UTC
> Wim, were the other patches also fine with you?

I don't quite understand the reason for this second patch.

the other patch is fine.
Comment 8 Alessandro Decina 2010-07-06 09:22:32 UTC
(just for reference since the discussion carried on on IRC and this patch set was committed)

The second patch is needed to avoid unreffing clock->clockid after unreffing clock->master. clock->clockid belongs to clock->master here so if clock->master is unreffed and deallocated, unreffing clock->clockid could access deallocated memory.
Comment 9 Alessandro Decina 2010-07-06 09:24:20 UTC
commit 7acde37fc10486908e3b685ecd895543beaf8d96
Author: Alessandro Decina <alessandro.decina@collabora.co.uk>
Date:   Tue Jul 6 10:35:09 2010 +0200

    win32: export gst_clock_id_wait_async_full

commit e6889791698f01ae237f41355df96a8547a21f6c
Author: Alessandro Decina <alessandro.decina@collabora.co.uk>
Date:   Tue Jul 6 10:31:25 2010 +0200

    tests: remove ABI checks for GstClockEntry.

commit a02fcb0478fb196271425527b3e070b4ae215fcd
Author: Alessandro Decina <alessandro.decina@collabora.co.uk>
Date:   Mon Jul 5 18:45:55 2010 +0200

    clock: document that GstClockEntry should be treated as ana opaque structure.

commit 819780acf79217c8a9b40c486f33668c0e2ae8d7
Author: Alessandro Decina <alessandro.decina@collabora.co.uk>
Date:   Mon Jul 5 13:10:09 2010 +0200

    clock: use the new gst_clock_id_wait_async_full.
    
    Use the new gst_clock_id_wait_async_full in gst_clock_set_master.
    Also add some tests.

commit 538e82c6f7ff8f2652651d6201c32f27b0ae3a62
Author: Alessandro Decina <alessandro.decina@collabora.co.uk>
Date:   Mon Jul 5 13:01:53 2010 +0200

    clock: fix refcounting bug in gst_clock_set_master.
    
    Make sure clock->clockid is unreffed before clock->master.
    gst_clock_id_unschedule (clock->clockid) tries to access clock->master. If
    clock->master is unreffed before and it's deallocated, _unschedule could access
    free'd memory.

commit dda86638110a97518c0d76aff405ffc0781c3573
Author: Alessandro Decina <alessandro.decina@collabora.co.uk>
Date:   Mon Jul 5 12:56:40 2010 +0200

    clock: add gst_clock_id_wait_async_full.
    
    Add gst_clock_id_wait_async_full. It's the same as gst_clock_id_wait_async but
    allows passing a GDestroyNotify to destroy user_data.