From ae2990c259abec198879c362dc13f7047f26c2cf Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Tue, 8 Oct 2013 11:58:31 +0200 Subject: [PATCH] osdep: initialize glib threads in all QEMU tools glib versions prior to 2.31.0 require an explicit g_thread_init() call to enable multi-threading. Failure to initialize threading causes glib to take single-threaded code paths without synchronization. For example, the g_slice allocator will crash due to race conditions. Fix this for all QEMU tool programs (qemu-nbd, qemu-io, qemu-img) by moving the g_thread_init() call from vl.c:main() into a new osdep.c:thread_init() constructor function. thread_init() has __attribute__((constructor)) and is automatically invoked by the runtime during startup. We can now drop the "simple" trace backend's g_thread_init() call since thread_init() already called it. Note that we must keep coroutine-gthread.c's g_thread_init() call which is located in a constructor function. There is no guarantee for constructor function ordering so thread_init() may only be called later. Reported-by: Mario de Chenno Signed-off-by: Stefan Hajnoczi --- trace/simple.c | 9 --------- util/osdep.c | 18 ++++++++++++++++++ vl.c | 8 -------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/trace/simple.c b/trace/simple.c index 57572c4905..aaa010ee2b 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -414,15 +414,6 @@ bool trace_backend_init(const char *events, const char *file) { GThread *thread; - if (!g_thread_supported()) { -#if !GLIB_CHECK_VERSION(2, 31, 0) - g_thread_init(NULL); -#else - fprintf(stderr, "glib threading failed to initialize.\n"); - exit(1); -#endif - } - #if !GLIB_CHECK_VERSION(2, 31, 0) trace_available_cond = g_cond_new(); trace_empty_cond = g_cond_new(); diff --git a/util/osdep.c b/util/osdep.c index bd4f530ad1..a9029f8894 100644 --- a/util/osdep.c +++ b/util/osdep.c @@ -436,6 +436,24 @@ int socket_init(void) return 0; } +/* Ensure that glib is running in multi-threaded mode */ +static void __attribute__((constructor)) thread_init(void) +{ + if (!g_thread_supported()) { +#if !GLIB_CHECK_VERSION(2, 31, 0) + /* Old versions of glib require explicit initialization. Failure to do + * this results in the single-threaded code paths being taken inside + * glib. For example, the g_slice allocator will not be thread-safe + * and cause crashes. + */ + g_thread_init(NULL); +#else + fprintf(stderr, "glib threading failed to initialize.\n"); + exit(1); +#endif + } +} + #ifndef CONFIG_IOVEC /* helper function for iov_send_recv() */ static ssize_t diff --git a/vl.c b/vl.c index acd97a841c..2355227bcb 100644 --- a/vl.c +++ b/vl.c @@ -2970,14 +2970,6 @@ int main(int argc, char **argv, char **envp) qemu_init_exec_dir(argv[0]); g_mem_set_vtable(&mem_trace); - if (!g_thread_supported()) { -#if !GLIB_CHECK_VERSION(2, 31, 0) - g_thread_init(NULL); -#else - fprintf(stderr, "glib threading failed to initialize.\n"); - exit(1); -#endif - } module_call_init(MODULE_INIT_QOM); -- GitLab