ocaml: Fix locking in event callbacks.
authorRichard W.M. Jones <rjones@redhat.com>
Tue, 26 Jul 2011 17:57:19 +0000 (18:57 +0100)
committerRichard W.M. Jones <rjones@redhat.com>
Wed, 27 Jul 2011 14:06:34 +0000 (15:06 +0100)
We weren't acquiring the GC lock around some allocations, resulting in
segfaults when an event callback ran at the same time as a main thread
allocation or garbage collection.

In particular this fixes a noticable crash in guestfs-browser.

(Cherry picked from commit 2b8b3f9794ceb43eabd3083e225c669896d8b186).

ocaml/guestfs_c.c

index a1386ec..2270289 100644 (file)
@@ -325,13 +325,13 @@ event_bitmask_to_event (uint64_t event)
 }
 
 static void
-event_callback_wrapper (guestfs_h *g,
-                        void *data,
-                        uint64_t event,
-                        int event_handle,
-                        int flags,
-                        const char *buf, size_t buf_len,
-                        const uint64_t *array, size_t array_len)
+event_callback_wrapper_locked (guestfs_h *g,
+                               void *data,
+                               uint64_t event,
+                               int event_handle,
+                               int flags,
+                               const char *buf, size_t buf_len,
+                               const uint64_t *array, size_t array_len)
 {
   CAMLparam0 ();
   CAMLlocal5 (gv, evv, ehv, bufv, arrayv);
@@ -358,9 +358,7 @@ event_callback_wrapper (guestfs_h *g,
 
   value args[5] = { gv, evv, ehv, bufv, arrayv };
 
-  caml_leave_blocking_section ();
   rv = caml_callbackN_exn (*(value*)data, 5, args);
-  caml_enter_blocking_section ();
 
   /* Callbacks shouldn't throw exceptions.  There's not much we can do
    * except to print it.
@@ -372,3 +370,23 @@ event_callback_wrapper (guestfs_h *g,
 
   CAMLreturn0;
 }
+
+static void
+event_callback_wrapper (guestfs_h *g,
+                        void *data,
+                        uint64_t event,
+                        int event_handle,
+                        int flags,
+                        const char *buf, size_t buf_len,
+                        const uint64_t *array, size_t array_len)
+{
+  /* Ensure we are holding the GC lock before any GC operations are
+   * possible. (RHBZ#725824)
+   */
+  caml_leave_blocking_section ();
+
+  event_callback_wrapper_locked (g, data, event, event_handle, flags,
+                                 buf, buf_len, array, array_len);
+
+  caml_enter_blocking_section ();
+}