Blame SOURCES/ibus-1385349-segv-bus-proxy.patch

da1ca3
From 41c325dfb32269c9aadfeedb4df44656aac4d883 Mon Sep 17 00:00:00 2001
da1ca3
From: fujiwarat <takao.fujiwara1@gmail.com>
da1ca3
Date: Fri, 20 Nov 2020 09:53:54 +0900
da1ca3
Subject: [PATCH] Fix SEGV in bus_panel_proxy_focus_in()
da1ca3
da1ca3
rhbz#1350291 SEGV in BUS_IS_CONNECTION(skip_connection) in
da1ca3
bus_dbus_impl_dispatch_message_by_rule()
da1ca3
check if dbus_connection is closed in bus_dbus_impl_connection_filter_cb().
da1ca3
da1ca3
rhbz#1767976 SEGV in assert(connection != NULL) in
da1ca3
bus_dbus_impl_connection_filter_cb()
da1ca3
call bus_connection_set_filter() in bus_dbus_impl_destroy().
da1ca3
da1ca3
rhbz#1601577 rhbz#1797726 SEGV in ibus_engine_desc_get_layout() in
da1ca3
bus_engine_proxy_new_internal()
da1ca3
WIP: Added a GError to get the error message to check why the SEGV happened.
da1ca3
da1ca3
rhbz#1663528 SEGV in g_mutex_clear() in bus_dbus_impl_destroy()
da1ca3
If the mutex is not unlocked, g_mutex_clear() causes assert.
da1ca3
da1ca3
rhbz#1767691 SEGV in client/x11/main.c:_sighandler().
da1ca3
Do not call atexit functions in _sighandler().
da1ca3
da1ca3
rhbz#1795499 SEGV in ibus_bus_get_bus_address() because of no _bus->priv.
da1ca3
_changed_cb() should not be called after ibus_bus_destroy() is called.
da1ca3
da1ca3
rhbz#1771238 SEGV in assert(m_loop == null) in switcher.vala.
da1ca3
Grabbing keyboard could be failed and switcher received the keyboard
da1ca3
events and m_loop was not released.
da1ca3
da1ca3
rhbz#1797120 SEGV in assert(bus.is_connected()) in panel_binding_construct()
da1ca3
Check m_ibus in extension.vala:bus_name_acquired_cb()
da1ca3
da1ca3
BUG=rhbz#1350291
da1ca3
BUG=rhbz#1601577
da1ca3
BUG=rhbz#1663528
da1ca3
BUG=rhbz#1767691
da1ca3
BUG=rhbz#1795499
da1ca3
BUG=rhbz#1771238
da1ca3
BUG=rhbz#1767976
da1ca3
BUG=rhbz#1797120
da1ca3
---
da1ca3
 bus/dbusimpl.c         | 47 ++++++++++++++++++++++++---
da1ca3
 bus/engineproxy.c      | 51 ++++++++++++++++++++++-------
da1ca3
 client/x11/main.c      |  8 ++++-
da1ca3
 src/ibusbus.c          |  5 +++
da1ca3
 ui/gtk3/extension.vala |  4 +++
da1ca3
 ui/gtk3/switcher.vala  | 73 +++++++++++++++++++++++++-----------------
da1ca3
 6 files changed, 141 insertions(+), 47 deletions(-)
da1ca3
da1ca3
diff --git a/bus/dbusimpl.c b/bus/dbusimpl.c
da1ca3
index 59787a80..af2fbde2 100644
da1ca3
--- a/bus/dbusimpl.c
da1ca3
+++ b/bus/dbusimpl.c
da1ca3
@@ -610,6 +610,7 @@ static void
da1ca3
 bus_dbus_impl_destroy (BusDBusImpl *dbus)
da1ca3
 {
da1ca3
     GList *p;
da1ca3
+    int i;
da1ca3
 
da1ca3
     for (p = dbus->objects; p != NULL; p = p->next) {
da1ca3
         IBusService *object = (IBusService *) p->data;
da1ca3
@@ -633,6 +634,10 @@ bus_dbus_impl_destroy (BusDBusImpl *dbus)
da1ca3
 
da1ca3
     for (p = dbus->connections; p != NULL; p = p->next) {
da1ca3
         BusConnection *connection = BUS_CONNECTION (p->data);
da1ca3
+        /* rhbz#1767976 Fix connection == NULL in
da1ca3
+         * bus_dbus_impl_connection_filter_cb()
da1ca3
+         */
da1ca3
+        bus_connection_set_filter (connection, NULL, NULL, NULL);
da1ca3
         g_signal_handlers_disconnect_by_func (connection,
da1ca3
                 bus_dbus_impl_connection_destroy_cb, dbus);
da1ca3
         ibus_object_destroy (IBUS_OBJECT (connection));
da1ca3
@@ -647,12 +652,39 @@ bus_dbus_impl_destroy (BusDBusImpl *dbus)
da1ca3
     dbus->unique_names = NULL;
da1ca3
     dbus->names = NULL;
da1ca3
 
da1ca3
+    for (i = 0; g_idle_remove_by_data (dbus); i++) {
da1ca3
+        if (i > 1000) {
da1ca3
+            g_warning ("Too many idle threads were generated by " \
da1ca3
+                       "bus_dbus_impl_forward_message_idle_cb and " \
da1ca3
+                       "bus_dbus_impl_dispatch_message_by_rule_idle_cb");
da1ca3
+            break;
da1ca3
+        }
da1ca3
+    }
da1ca3
     g_list_free_full (dbus->start_service_calls,
da1ca3
                       (GDestroyNotify) bus_method_call_free);
da1ca3
     dbus->start_service_calls = NULL;
da1ca3
 
da1ca3
-    g_mutex_clear (&dbus->dispatch_lock);
da1ca3
-    g_mutex_clear (&dbus->forward_lock);
da1ca3
+   /* rhbz#1663528 Call g_mutex_trylock() before g_mutex_clear()
da1ca3
+    * because if the mutex is not unlocked, g_mutex_clear() causes assert.
da1ca3
+    */
da1ca3
+#define BUS_DBUS_MUTEX_SAFE_CLEAR(mtex) {                               \
da1ca3
+    int count = 0;                                                      \
da1ca3
+    while (!g_mutex_trylock ((mtex))) {                                 \
da1ca3
+        g_usleep (1);                                                   \
da1ca3
+        if (count > 60) {                                               \
da1ca3
+            g_warning (#mtex " is dead lock");                          \
da1ca3
+            break;                                                      \
da1ca3
+        }                                                               \
da1ca3
+        ++count;                                                        \
da1ca3
+    }                                                                   \
da1ca3
+    g_mutex_unlock ((mtex));                                            \
da1ca3
+    g_mutex_clear ((mtex));                                             \
da1ca3
+}
da1ca3
+
da1ca3
+    BUS_DBUS_MUTEX_SAFE_CLEAR (&dbus->dispatch_lock);
da1ca3
+    BUS_DBUS_MUTEX_SAFE_CLEAR (&dbus->forward_lock);
da1ca3
+
da1ca3
+#undef BUS_DBUS_MUTEX_SAFE_CLEAR
da1ca3
 
da1ca3
     /* FIXME destruct _lock and _queue members. */
da1ca3
     IBUS_OBJECT_CLASS(bus_dbus_impl_parent_class)->destroy ((IBusObject *) dbus);
da1ca3
@@ -1483,13 +1515,20 @@ bus_dbus_impl_connection_filter_cb (GDBusConnection *dbus_connection,
da1ca3
                                     gboolean         incoming,
da1ca3
                                     gpointer         user_data)
da1ca3
 {
da1ca3
+    BusDBusImpl *dbus;
da1ca3
+    BusConnection *connection;
da1ca3
+
da1ca3
     g_assert (G_IS_DBUS_CONNECTION (dbus_connection));
da1ca3
     g_assert (G_IS_DBUS_MESSAGE (message));
da1ca3
     g_assert (BUS_IS_DBUS_IMPL (user_data));
da1ca3
 
da1ca3
-    BusDBusImpl *dbus = (BusDBusImpl *) user_data;
da1ca3
-    BusConnection *connection = bus_connection_lookup (dbus_connection);
da1ca3
+    if (g_dbus_connection_is_closed (dbus_connection))
da1ca3
+        return NULL;
da1ca3
+
da1ca3
+    dbus = (BusDBusImpl *) user_data;
da1ca3
+    connection = bus_connection_lookup (dbus_connection);
da1ca3
     g_assert (connection != NULL);
da1ca3
+    g_assert (BUS_IS_CONNECTION (connection));
da1ca3
 
da1ca3
     if (incoming) {
da1ca3
         /* is incoming message */
da1ca3
diff --git a/bus/engineproxy.c b/bus/engineproxy.c
da1ca3
index 2d98995c..bbbe5532 100644
da1ca3
--- a/bus/engineproxy.c
da1ca3
+++ b/bus/engineproxy.c
da1ca3
@@ -660,20 +660,33 @@ bus_engine_proxy_g_signal (GDBusProxy  *proxy,
da1ca3
     g_return_if_reached ();
da1ca3
 }
da1ca3
 
da1ca3
+#pragma GCC optimize ("O0")
da1ca3
 static BusEngineProxy *
da1ca3
 bus_engine_proxy_new_internal (const gchar     *path,
da1ca3
                                IBusEngineDesc  *desc,
da1ca3
-                               GDBusConnection *connection)
da1ca3
+                               GDBusConnection *connection,
da1ca3
+                               GError         **error)
da1ca3
 {
da1ca3
+    GDBusProxyFlags flags;
da1ca3
+    BusEngineProxy *engine;
da1ca3
+
da1ca3
     g_assert (path);
da1ca3
     g_assert (IBUS_IS_ENGINE_DESC (desc));
da1ca3
     g_assert (G_IS_DBUS_CONNECTION (connection));
da1ca3
+    g_assert (error && *error == NULL);
da1ca3
 
da1ca3
-    GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
da1ca3
-    BusEngineProxy *engine =
da1ca3
+    /* rhbz#1601577 engine == NULL if connection is closed. */
da1ca3
+    if (g_dbus_connection_is_closed (connection)) {
da1ca3
+        *error = g_error_new (G_DBUS_ERROR,
da1ca3
+                              G_DBUS_ERROR_FAILED,
da1ca3
+                              "Connection is closed.");
da1ca3
+        return NULL;
da1ca3
+    }
da1ca3
+    flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
da1ca3
+    engine =
da1ca3
         (BusEngineProxy *) g_initable_new (BUS_TYPE_ENGINE_PROXY,
da1ca3
                                            NULL,
da1ca3
-                                           NULL,
da1ca3
+                                           error,
da1ca3
                                            "desc",              desc,
da1ca3
                                            "g-connection",      connection,
da1ca3
                                            "g-interface-name",  IBUS_INTERFACE_ENGINE,
da1ca3
@@ -681,12 +694,19 @@ bus_engine_proxy_new_internal (const gchar     *path,
da1ca3
                                            "g-default-timeout", g_gdbus_timeout,
da1ca3
                                            "g-flags",           flags,
da1ca3
                                            NULL);
da1ca3
+    /* FIXME: rhbz#1601577 */
da1ca3
+    if (!engine) {
da1ca3
+        /* show abrt local variable */
da1ca3
+        gchar *message = g_strdup ((*error)->message);
da1ca3
+        g_error ("%s", message);
da1ca3
+    }
da1ca3
     const gchar *layout = ibus_engine_desc_get_layout (desc);
da1ca3
     if (layout != NULL && layout[0] != '\0') {
da1ca3
         engine->keymap = ibus_keymap_get (layout);
da1ca3
     }
da1ca3
     return engine;
da1ca3
 }
da1ca3
+#pragma GCC reset_options
da1ca3
 
da1ca3
 typedef struct {
da1ca3
     GTask           *task;
da1ca3
@@ -748,23 +768,30 @@ create_engine_ready_cb (BusFactoryProxy    *factory,
da1ca3
                         GAsyncResult       *res,
da1ca3
                         EngineProxyNewData *data)
da1ca3
 {
da1ca3
+    GError *error = NULL;
da1ca3
+    gchar *path;
da1ca3
+    BusEngineProxy *engine;
da1ca3
+
da1ca3
     g_return_if_fail (data->task != NULL);
da1ca3
 
da1ca3
-    GError *error = NULL;
da1ca3
-    gchar *path = bus_factory_proxy_create_engine_finish (factory,
da1ca3
-                                                          res,
da1ca3
-                                                          &error);
da1ca3
+    path = bus_factory_proxy_create_engine_finish (factory, res, &error);
da1ca3
     if (path == NULL) {
da1ca3
         g_task_return_error (data->task, error);
da1ca3
         engine_proxy_new_data_free (data);
da1ca3
         return;
da1ca3
     }
da1ca3
 
da1ca3
-    BusEngineProxy *engine =
da1ca3
-            bus_engine_proxy_new_internal (path,
da1ca3
-                                           data->desc,
da1ca3
-                                           g_dbus_proxy_get_connection ((GDBusProxy *)data->factory));
da1ca3
+    engine = bus_engine_proxy_new_internal (
da1ca3
+            path,
da1ca3
+            data->desc,
da1ca3
+            g_dbus_proxy_get_connection ((GDBusProxy *)data->factory),
da1ca3
+            &error);
da1ca3
     g_free (path);
da1ca3
+    if (!engine) {
da1ca3
+        g_task_return_error (data->task, error);
da1ca3
+        engine_proxy_new_data_free (data);
da1ca3
+        return;
da1ca3
+    }
da1ca3
 
da1ca3
     /* FIXME: set destroy callback ? */
da1ca3
     g_task_return_pointer (data->task, engine, NULL);
da1ca3
diff --git a/client/x11/main.c b/client/x11/main.c
da1ca3
index c9ee174d..768b91f0 100644
da1ca3
--- a/client/x11/main.c
da1ca3
+++ b/client/x11/main.c
da1ca3
@@ -40,6 +40,7 @@
da1ca3
 #include <iconv.h>
da1ca3
 #include <signal.h>
da1ca3
 #include <stdlib.h>
da1ca3
+#include <unistd.h>
da1ca3
 
da1ca3
 #include <getopt.h>
da1ca3
 
da1ca3
@@ -1104,7 +1105,12 @@ _atexit_cb ()
da1ca3
 static void
da1ca3
 _sighandler (int sig)
da1ca3
 {
da1ca3
-    exit(EXIT_FAILURE);
da1ca3
+    /* rhbz#1767691 _sighandler() is called with SIGTERM
da1ca3
+     * and exit() causes SEGV during calling atexit functions.
da1ca3
+     * _atexit_cb() might be broken. _exit() does not call
da1ca3
+     * atexit functions.
da1ca3
+     */
da1ca3
+    _exit(EXIT_FAILURE);
da1ca3
 }
da1ca3
 
da1ca3
 static void
da1ca3
diff --git a/src/ibusbus.c b/src/ibusbus.c
da1ca3
index b7ffbb47..668c8a26 100644
da1ca3
--- a/src/ibusbus.c
da1ca3
+++ b/src/ibusbus.c
da1ca3
@@ -689,6 +689,11 @@ ibus_bus_destroy (IBusObject *object)
da1ca3
     _bus = NULL;
da1ca3
 
da1ca3
     if (bus->priv->monitor) {
da1ca3
+        /* rhbz#1795499 _changed_cb() causes SEGV because of no bus->priv
da1ca3
+         * after ibus_bus_destroy() is called.
da1ca3
+         */
da1ca3
+        g_signal_handlers_disconnect_by_func (bus->priv->monitor,
da1ca3
+                                              (GCallback) _changed_cb, bus);
da1ca3
         g_object_unref (bus->priv->monitor);
da1ca3
         bus->priv->monitor = NULL;
da1ca3
     }
da1ca3
diff --git a/ui/gtk3/extension.vala b/ui/gtk3/extension.vala
da1ca3
index a6f2e8e6..b7a04081 100644
da1ca3
--- a/ui/gtk3/extension.vala
da1ca3
+++ b/ui/gtk3/extension.vala
da1ca3
@@ -73,6 +73,10 @@ class ExtensionGtk : Gtk.Application {
da1ca3
                                       string         signal_name,
da1ca3
                                       Variant        parameters) {
da1ca3
         debug("signal_name = %s", signal_name);
da1ca3
+        /* rhbz#1797120 Fix assert(bus.is_connected()) in
da1ca3
+         * panel_binding_construct()
da1ca3
+         */
da1ca3
+        return_if_fail(m_bus.is_connected());
da1ca3
         m_panel = new PanelBinding(m_bus, this);
da1ca3
         m_panel.load_settings();
da1ca3
     }
da1ca3
diff --git a/ui/gtk3/switcher.vala b/ui/gtk3/switcher.vala
da1ca3
index a4529c88..29a70dd5 100644
da1ca3
--- a/ui/gtk3/switcher.vala
da1ca3
+++ b/ui/gtk3/switcher.vala
da1ca3
@@ -140,8 +140,8 @@ class Switcher : Gtk.Window {
da1ca3
                    IBus.EngineDesc[] engines,
da1ca3
                    int               index,
da1ca3
                    string            input_context_path) {
da1ca3
-        assert (m_loop == null);
da1ca3
-        assert (index < engines.length);
da1ca3
+        assert(m_loop == null);
da1ca3
+        assert(index < engines.length);
da1ca3
 
da1ca3
         m_is_running = true;
da1ca3
         m_keyval = keyval;
da1ca3
@@ -198,16 +198,18 @@ class Switcher : Gtk.Window {
da1ca3
                            null,
da1ca3
                            event,
da1ca3
                            null);
da1ca3
-        if (status != Gdk.GrabStatus.SUCCESS)
da1ca3
+        if (status != Gdk.GrabStatus.SUCCESS) {
da1ca3
             warning("Grab keyboard failed! status = %d", status);
da1ca3
-        status = seat.grab(get_window(),
da1ca3
-                           Gdk.SeatCapabilities.POINTER,
da1ca3
-                           true,
da1ca3
-                           null,
da1ca3
-                           event,
da1ca3
-                           null);
da1ca3
-        if (status != Gdk.GrabStatus.SUCCESS)
da1ca3
-            warning("Grab pointer failed! status = %d", status);
da1ca3
+        } else {
da1ca3
+            status = seat.grab(get_window(),
da1ca3
+                               Gdk.SeatCapabilities.POINTER,
da1ca3
+                               true,
da1ca3
+                               null,
da1ca3
+                               event,
da1ca3
+                               null);
da1ca3
+            if (status != Gdk.GrabStatus.SUCCESS)
da1ca3
+                warning("Grab pointer failed! status = %d", status);
da1ca3
+        }
da1ca3
 #else
da1ca3
         Gdk.Device device = event.get_device();
da1ca3
         if (device == null) {
da1ca3
@@ -243,30 +245,41 @@ class Switcher : Gtk.Window {
da1ca3
                                Gdk.EventMask.KEY_RELEASE_MASK,
da1ca3
                                null,
da1ca3
                                Gdk.CURRENT_TIME);
da1ca3
-        if (status != Gdk.GrabStatus.SUCCESS)
da1ca3
+        if (status != Gdk.GrabStatus.SUCCESS) {
da1ca3
             warning("Grab keyboard failed! status = %d", status);
da1ca3
-        // Grab all pointer events
da1ca3
-        status = pointer.grab(get_window(),
da1ca3
-                              Gdk.GrabOwnership.NONE,
da1ca3
-                              true,
da1ca3
-                              Gdk.EventMask.BUTTON_PRESS_MASK |
da1ca3
-                              Gdk.EventMask.BUTTON_RELEASE_MASK,
da1ca3
-                              null,
da1ca3
-                              Gdk.CURRENT_TIME);
da1ca3
-        if (status != Gdk.GrabStatus.SUCCESS)
da1ca3
-            warning("Grab pointer failed! status = %d", status);
da1ca3
+        } else {
da1ca3
+            // Grab all pointer events
da1ca3
+            status = pointer.grab(get_window(),
da1ca3
+                                  Gdk.GrabOwnership.NONE,
da1ca3
+                                  true,
da1ca3
+                                  Gdk.EventMask.BUTTON_PRESS_MASK |
da1ca3
+                                  Gdk.EventMask.BUTTON_RELEASE_MASK,
da1ca3
+                                  null,
da1ca3
+                                  Gdk.CURRENT_TIME);
da1ca3
+            if (status != Gdk.GrabStatus.SUCCESS)
da1ca3
+                warning("Grab pointer failed! status = %d", status);
da1ca3
+        }
da1ca3
 #endif
da1ca3
 
da1ca3
-        // Probably we can delete m_popup_delay_time in 1.6
da1ca3
-        pointer.get_position_double(null,
da1ca3
-                                    out m_mouse_init_x,
da1ca3
-                                    out m_mouse_init_y);
da1ca3
-        m_mouse_moved = false;
da1ca3
+        /* Fix RHBZ #1771238 assert(m_loop == null)
da1ca3
+         * Grabbing keyboard can be failed when the second Super-e is typed
da1ca3
+         * before Switcher dialog is focused. And m_loop could not be released
da1ca3
+         * if the failed Super-e would call m_loop.run() below and could not
da1ca3
+         * call key_release_event(). And m_loop == null would be false in the
da1ca3
+         * third Super-e.
da1ca3
+         */
da1ca3
+        if (status == Gdk.GrabStatus.SUCCESS) {
da1ca3
+            // Probably we can delete m_popup_delay_time in 1.6
da1ca3
+            pointer.get_position_double(null,
da1ca3
+                                        out m_mouse_init_x,
da1ca3
+                                        out m_mouse_init_y);
da1ca3
+            m_mouse_moved = false;
da1ca3
 
da1ca3
 
da1ca3
-        m_loop = new GLib.MainLoop();
da1ca3
-        m_loop.run();
da1ca3
-        m_loop = null;
da1ca3
+            m_loop = new GLib.MainLoop();
da1ca3
+            m_loop.run();
da1ca3
+            m_loop = null;
da1ca3
+        }
da1ca3
 
da1ca3
 #if VALA_0_34
da1ca3
         seat.ungrab();
da1ca3
-- 
da1ca3
2.24.1
da1ca3