Avoid a crash if constructors in other libraries are called before br_init.
authorRichard W.M. Jones <rjones@redhat.com>
Wed, 10 Mar 2021 20:45:40 +0000 (20:45 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Wed, 10 Mar 2021 20:45:40 +0000 (20:45 +0000)
auto-buildrequires-preload.c

index b67e20a..c8622a1 100644 (file)
@@ -95,6 +95,13 @@ abr_open (const char *pathname, int flags, mode_t mode)
   int fd;
   char *rp;
 
+  /* Constructors in other libraries can call this function before
+   * br_init has been called.  Detect this and call br_init to avoid a
+   * crash.
+   */
+  if (glibc_open == NULL)
+    br_init ();
+
   rp = br_path (pathname);
   if (rp)
     br_log ("open %s\n", rp);
@@ -114,6 +121,13 @@ abr_execve (const char *filename, char *const argv[], char *const envp[])
   int r;
   char *rp;
 
+  /* Constructors in other libraries can call this function before
+   * br_init has been called.  Detect this and call br_init to avoid a
+   * crash.
+   */
+  if (glibc_open == NULL)
+    br_init ();
+
   rp = br_path (filename);
   if (rp)
     br_log ("execve %s\n", rp);
@@ -190,7 +204,9 @@ br_log (const char *fs, ...)
 static void
 br_init (void)
 {
-  void *dl;
+  _Atomic static void *dl;
+
+  if (dl != NULL) return;
 
   dl = dlopen ("/lib64/" LIBC_SO, RTLD_LAZY|RTLD_LOCAL);
   if (dl == NULL)      // Try '/lib/' also