gnutls: Fix a crash caused by loading and unloading the module
authorDan Winship <danw@gnome.org>
Mon, 29 Nov 2010 15:07:37 +0000 (10:07 -0500)
committerDan Winship <danw@gnome.org>
Mon, 29 Nov 2010 15:15:56 +0000 (10:15 -0500)
If gio-querymodules isn't run to update the cache then gio will need
to load and unload the module at startup time. This was causing
crashes in apps that used gcrypt elsewhere, because gcrypt was being
initialized to use our module's mutex functions, which then no longer
existed after unloading.

Fix this in two different ways: first, force the module to remain
resident after gtls_gnutls_init() has been called, and second, don't
call gtls_gnutls_init() if we're just being called from
g_io_modules_scan_all_in_directory().

https://bugzilla.gnome.org/show_bug.cgi?id=636052

tls/gnutls/gtlsbackend-gnutls.c

index fdf081c..9511ae8 100644 (file)
 #include "gtlsclientconnection-gnutls.h"
 #include "gtlsserverconnection-gnutls.h"
 
-static void gtls_gnutls_init (void);
 static void g_tls_backend_gnutls_interface_init (GTlsBackendInterface *iface);
 
 G_DEFINE_DYNAMIC_TYPE_EXTENDED (GTlsBackendGnutls, g_tls_backend_gnutls, G_TYPE_OBJECT, 0,
                                G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_TLS_BACKEND,
-                                                              g_tls_backend_gnutls_interface_init);
-                               gtls_gnutls_init ();)
+                                                              g_tls_backend_gnutls_interface_init);)
 
 #if defined(GCRY_THREAD_OPTION_PTHREAD_IMPL) && !defined(G_OS_WIN32)
 GCRY_THREAD_OPTION_PTHREAD_IMPL;
@@ -94,8 +92,8 @@ static struct gcry_thread_cbs gtls_gcry_threads_win32 = {              \
 
 #endif
 
-static void
-gtls_gnutls_init (void)
+static gpointer
+gtls_gnutls_init (gpointer data)
 {
 #if defined(GCRY_THREAD_OPTION_PTHREAD_IMPL) && !defined(G_OS_WIN32)
   gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
@@ -103,11 +101,25 @@ gtls_gnutls_init (void)
   gcry_control (GCRYCTL_SET_THREAD_CBS, &gtls_gcry_threads_win32);
 #endif
   gnutls_global_init ();
+
+  /* Leak the module to keep it from being unloaded. */
+  g_type_plugin_use (g_type_get_plugin (G_TYPE_TLS_BACKEND_GNUTLS));
+  return NULL;
 }
 
+static GOnce gnutls_inited = G_ONCE_INIT;
+
 static void
 g_tls_backend_gnutls_init (GTlsBackendGnutls *backend)
 {
+  /* Once we call gtls_gnutls_init(), we can't allow the module to be
+   * unloaded, since that would break the pointers to the mutex
+   * functions we set for gcrypt. So we initialize it from here rather
+   * than at class init time so that it doesn't happen unless the app
+   * is actually using TLS (as opposed to just calling
+   * g_io_modules_scan_all_in_directory()).
+   */
+  g_once (&gnutls_inited, gtls_gnutls_init, NULL);
 }
 
 static void