Make dlerror(3) thread-safe.

I gave up trying to use the usual thread-local buffer idiom; calls to
calloc(3) and free(3) from any of the "dl" functions -- which live in
the dynamic linker -- end up resolving to the dynamic linker's stubs.
I tried to work around that, but was just making things more complicated.
This alternative costs us a well-known TLS slot (instead of the
dynamically-allocated TLS slot we'd have used otherwise, so no difference
there), plus an extra buffer inside every pthread_internal_t.

Bug: 5404023
Change-Id: Ie9614edd05b6d1eeaf7bf9172792d616c6361767
diff --git a/tests/dlopen_test.cpp b/tests/dlopen_test.cpp
index 5ef32ad..5b5c7f6 100644
--- a/tests/dlopen_test.cpp
+++ b/tests/dlopen_test.cpp
@@ -58,6 +58,27 @@
 #endif
 }
 
+static void* ConcurrentDlErrorFn(void* arg) {
+  dlopen("/child/thread", RTLD_NOW);
+  return reinterpret_cast<void*>(strdup(dlerror()));
+}
+
+TEST(dlopen, dlerror_concurrent) {
+  dlopen("/main/thread", RTLD_NOW);
+  const char* main_thread_error = dlerror();
+  ASSERT_SUBSTR("/main/thread", main_thread_error);
+
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
+  void* result;
+  ASSERT_EQ(0, pthread_join(t, &result));
+  char* child_thread_error = static_cast<char*>(result);
+  ASSERT_SUBSTR("/child/thread", child_thread_error);
+  free(child_thread_error);
+
+  ASSERT_SUBSTR("/main/thread", main_thread_error);
+}
+
 TEST(dlopen, dlsym_failures) {
   dlerror(); // Clear any pending errors.
   void* self = dlopen(NULL, RTLD_NOW);