MDEV-30811: Build issues on macOS 11.0

Building of MariaDB server version 11.0 and 11.1 fails
on MacOS 12.x (Monterey). Build failure happened on generating
header files from the error messages contained in the file
errmsg-utf8.txt. This process is performed by the utility comp_err
that crashes when it is run on MacOS Monterey.

comp_err invokes my_init at the very beginning of start before
initialization of thread environment done. While executing my_init
the function my_readlink is called. my_readlink is wrapper around
the system call readlink with extra errors handling. In case the
system call readlink returns error the following block of code is run
  if (my_thread_var)
    my_errno= errno;

my_thred_var is macros that expanded to invocation of
my_pthread_getspecific() against supplied thread specific key
THR_KEY_mysys. Unfortunately, the tsd key THR_KEY_mysys is initialized
right after the call of my_init() so return value of pthread_getspecific
is platform dependent. On Linux pthread_getspecific returns NULL
if key is not a valid TSD key. On MacOS, the effect of calling
pthread_getspecific() with a key value not obtained from pthread_key_create()
is undefined. So, on MacOS pthread_getspecific() returns some invalid address
where the errno value is written. It leads to a crash latter when the library
API function pthread_self() is called.

To fix the issue, initialization of thread environment is moved at very
beginning of the main() function in order to run it as the first step
to have full initiliazed environment at the moment when my_init()
is ivoked.
This commit is contained in:
Dmitry Shulga 2023-03-16 16:25:57 +07:00
parent d77aaa6994
commit 1310b3a02f
2 changed files with 7 additions and 9 deletions

View file

@ -176,6 +176,12 @@ my_bool my_init(void)
mysql_stdin= & instrumented_stdin;
my_progname_short= "unknown";
/* Initialize our mutex handling */
my_mutex_init();
if (my_thread_global_init())
return 1;
if (my_progname)
{
char link_name[FN_REFLEN];
@ -198,12 +204,6 @@ my_bool my_init(void)
}
}
/* Initialize our mutex handling */
my_mutex_init();
if (my_thread_global_init())
return 1;
#if defined(SAFEMALLOC) && !defined(DBUG_OFF)
dbug_sanity= sf_sanity;
#endif

View file

@ -54,10 +54,8 @@ int my_readlink(char *to, const char *filename, myf MyFlags)
if ((length=readlink(filename, to, FN_REFLEN-1)) < 0)
{
if (my_thread_var)
my_errno= errno;
/* Don't give an error if this wasn't a symlink */
if (errno == EINVAL)
if ((my_errno=errno) == EINVAL)
{
result= 1;
strnmov(to, filename, FN_REFLEN);