From ddffae0a8872b211e0a94d10565ce517026c7581 Mon Sep 17 00:00:00 2001 From: Dmitry Shulga Date: Fri, 22 Sep 2023 12:02:47 +0700 Subject: [PATCH] MDEV-31871: maria-install-db fails on MacOS Follow-up to fix issue with access to probably not-initialized mutex/cond_var Constructor of the class st_debug_sync_globals was changed to initialize the data members dsp_hits, dsp_executed, dsp_max_active with zero. Formerly, these data members were filled with zeroes by C-runtime since the variable debug_sync_global was declared as static and according with C rules the static variable initialized with zero bytes. By the same reason, the data members debug_sync_global->ds_mutex debug_sync_global->ds_cond were initialized by zeros before the patch for MDEV-31871. After this patch the memory for the synch primitives debug_sync_global->ds_mutex and debug_sync_global->ds_cond are initialized explicitly by calling the functions mysql_mutex_init/mysql_cond_init so access to these synch primitives should be done only after such initialization be completed. Guarded access to these synch primitives has been added to the function debug_sync_end_thread() that is called on clean up since that was single problem place detected by MSAN. Theoretically problem places located in the function debug_sync_execute were not protected with similar check since it is not obvious that the variables debug_sync_global->ds_mutex and debug_sync_global->ds_cond could be not initilialized for use cases where the function debug_sync_execute() is called. It is required additional study to conclude whether it does need or not. --- sql/debug_sync.cc | 20 +++++++++++++++++--- sql/sql_class.cc | 2 ++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/sql/debug_sync.cc b/sql/debug_sync.cc index 673b6c1d4bc..362463a7f22 100644 --- a/sql/debug_sync.cc +++ b/sql/debug_sync.cc @@ -87,7 +87,9 @@ struct st_debug_sync_globals ulonglong dsp_executed; /* statistics */ ulonglong dsp_max_active; /* statistics */ - st_debug_sync_globals() : ds_signal_set(PSI_NOT_INSTRUMENTED, signal_key) {}; + st_debug_sync_globals() : + ds_signal_set(PSI_NOT_INSTRUMENTED, signal_key), + dsp_hits (0), dsp_executed(0), dsp_max_active(0) {}; ~st_debug_sync_globals() { clear_set(); @@ -422,12 +424,24 @@ void debug_sync_end_thread(THD *thd) } /* Statistics. */ - mysql_mutex_lock(&debug_sync_global->ds_mutex); + /* + Protect access with debug_sync_global->ds_mutex only if + it had been initialized. + */ + if (debug_sync_C_callback_ptr) + mysql_mutex_lock(&debug_sync_global->ds_mutex); + debug_sync_global->dsp_hits+= ds_control->dsp_hits; debug_sync_global->dsp_executed+= ds_control->dsp_executed; if (debug_sync_global->dsp_max_active < ds_control->dsp_max_active) debug_sync_global->dsp_max_active= ds_control->dsp_max_active; - mysql_mutex_unlock(&debug_sync_global->ds_mutex); + + /* + Protect access with debug_sync_global->ds_mutex only if + it had been initialized. + */ + if (debug_sync_C_callback_ptr) + mysql_mutex_unlock(&debug_sync_global->ds_mutex); my_free(ds_control); thd->debug_sync_control= NULL; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index a4aea9775bc..884d6c81511 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -1748,7 +1748,9 @@ THD::~THD() lf_hash_put_pins(tdc_hash_pins); if (xid_hash_pins) lf_hash_put_pins(xid_hash_pins); +#if defined(ENABLED_DEBUG_SYNC) debug_sync_end_thread(this); +#endif /* Ensure everything is freed */ status_var.local_memory_used-= sizeof(THD);