Merge "Improve glibc compatibility of gethostby*_r functions."
diff --git a/android-changes-for-ndk-developers.md b/android-changes-for-ndk-developers.md
index c301857..0620d9e 100644
--- a/android-changes-for-ndk-developers.md
+++ b/android-changes-for-ndk-developers.md
@@ -376,3 +376,22 @@
 ```
 
 enables logging of all errors and dlopen calls
+
+## dlclose interacts badly with thread local variables with non-trivial destructors
+
+Android allows `dlclose` to unload a library even if there are still
+thread-local variables with non-trivial destructors. This leads to
+crashes when a thread exits and attempts to call the destructor, the
+code for which has been unloaded (as in [issue 360]).
+
+[issue 360]: https://github.com/android-ndk/ndk/issues/360
+
+Not calling `dlclose` or ensuring that your library has `RTLD_NODELETE`
+set (so that calls to `dlclose` don't actually unload the library)
+are possible workarounds.
+
+|                   | Pre-M                      | M+      |
+| ----------------- | -------------------------- | ------- |
+| No workaround     | Works for static STL       | Broken  |
+| `-Wl,-z,nodelete` | Works for static STL       | Works   |
+| No `dlclose`      | Works                      | Works   |
diff --git a/benchmarks/pthread_benchmark.cpp b/benchmarks/pthread_benchmark.cpp
index 7a967ef..1ad6345 100644
--- a/benchmarks/pthread_benchmark.cpp
+++ b/benchmarks/pthread_benchmark.cpp
@@ -137,9 +137,7 @@
 }
 BIONIC_BENCHMARK(BM_pthread_create);
 
-static void* RunThread(void* arg) {
-  benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg);
-  state.PauseTiming();
+static void* RunThread(void*) {
   return NULL;
 }
 
@@ -148,22 +146,18 @@
     pthread_t thread;
     pthread_create(&thread, NULL, RunThread, &state);
     pthread_join(thread, NULL);
-    state.ResumeTiming();
   }
 }
 BIONIC_BENCHMARK(BM_pthread_create_and_run);
 
-static void* ExitThread(void* arg) {
-  benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg);
-  state.ResumeTiming();
+static void* ExitThread(void*) {
   pthread_exit(NULL);
 }
 
 static void BM_pthread_exit_and_join(benchmark::State& state) {
   while (state.KeepRunning()) {
-    state.PauseTiming();
     pthread_t thread;
-    pthread_create(&thread, NULL, ExitThread, &state);
+    pthread_create(&thread, NULL, ExitThread, nullptr);
     pthread_join(thread, NULL);
   }
 }
diff --git a/docs/status.md b/docs/status.md
index cd40e2c..32de91e 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -15,7 +15,7 @@
   * `glob`/`globfree` (adding <glob.h>)
   * `hcreate`/hcreate_r`/`hdestroy`/`hdestroy_r`/`hsearch`/`hsearch_r` (completing <search.h>)
   * `iconv`/`iconv_close`/`iconv_open` (adding <iconv.h>)
-  * `pthread_setschedprio`
+  * `pthread_attr_getinheritsched`/`pthread_attr_setinheritsched`/`pthread_setschedprio`
   * <spawn.h>
   * `swab`
   * `syncfs`
@@ -74,8 +74,6 @@
 aio_suspend
 aio_write
 lio_listio
-pthread_attr_getinheritsched
-pthread_attr_setinheritsched
 pthread_cancel
 pthread_mutex_consistent
 pthread_mutex_getprioceiling
diff --git a/libc/arch-common/bionic/__dso_handle.h b/libc/arch-common/bionic/__dso_handle.h
index d4bff77..fa40060 100644
--- a/libc/arch-common/bionic/__dso_handle.h
+++ b/libc/arch-common/bionic/__dso_handle.h
@@ -28,11 +28,6 @@
 
 
 #ifndef CRT_LEGACY_WORKAROUND
-__attribute__ ((visibility ("hidden")))
+__attribute__((__visibility__("hidden")))
 #endif
-#ifdef __aarch64__
-__attribute__ ((section (".data")))
-#else
-__attribute__ ((section (".bss")))
-#endif
-void *__dso_handle = (void *) 0;
+void* __dso_handle = (void*) 0;
diff --git a/libc/arch-common/bionic/__dso_handle_so.h b/libc/arch-common/bionic/__dso_handle_so.h
index fab328a..e2c26b6 100644
--- a/libc/arch-common/bionic/__dso_handle_so.h
+++ b/libc/arch-common/bionic/__dso_handle_so.h
@@ -26,11 +26,4 @@
  * SUCH DAMAGE.
  */
 
-
-__attribute__ ((visibility ("hidden")))
-__attribute__ ((section (".data")))
-#ifdef __aarch64__
-void *__dso_handle = (void *) 0;
-#else
-void *__dso_handle = &__dso_handle;
-#endif
+__attribute__((__visibility__("hidden"))) void* __dso_handle = &__dso_handle;
diff --git a/libc/async_safe/async_safe_log.cpp b/libc/async_safe/async_safe_log.cpp
index 78f62bd..d5aad16 100644
--- a/libc/async_safe/async_safe_log.cpp
+++ b/libc/async_safe/async_safe_log.cpp
@@ -47,6 +47,7 @@
 #include <async_safe/log.h>
 
 #include "private/CachedProperty.h"
+#include "private/ErrnoRestorer.h"
 #include "private/ScopedPthreadMutexLocker.h"
 
 // Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java.
@@ -508,6 +509,7 @@
 }
 
 int async_safe_format_log_va_list(int priority, const char* tag, const char* format, va_list args) {
+  ErrnoRestorer errno_restorer;
   char buffer[1024];
   BufferOutputStream os(buffer, sizeof(buffer));
   out_vformat(os, format, args);
diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp
index d7581dd..5004e24 100644
--- a/libc/bionic/__libc_init_main_thread.cpp
+++ b/libc/bionic/__libc_init_main_thread.cpp
@@ -88,7 +88,6 @@
   pthread_attr_init(&main_thread.attr);
   main_thread.attr.guard_size = 0; // The main thread has no guard page.
   main_thread.attr.stack_size = 0; // User code should never see this; we'll compute it when asked.
-  // TODO: the main thread's sched_policy and sched_priority need to be queried.
 
   // The TLS stack guard is set from the global, so ensure that we've initialized the global
   // before we initialize the TLS. Dynamic executables will initialize their copy of the global
diff --git a/libc/bionic/pthread_attr.cpp b/libc/bionic/pthread_attr.cpp
index fc9e74a..0d79374 100644
--- a/libc/bionic/pthread_attr.cpp
+++ b/libc/bionic/pthread_attr.cpp
@@ -54,6 +54,23 @@
   return 0;
 }
 
+int pthread_attr_setinheritsched(pthread_attr_t* attr, int flag) {
+  if (flag == PTHREAD_EXPLICIT_SCHED) {
+    attr->flags &= ~PTHREAD_ATTR_FLAG_INHERIT;
+  } else if (flag == PTHREAD_INHERIT_SCHED) {
+    attr->flags |= PTHREAD_ATTR_FLAG_INHERIT;
+  } else {
+    return EINVAL;
+  }
+  return 0;
+}
+
+int pthread_attr_getinheritsched(const pthread_attr_t* attr, int* flag) {
+  *flag = (attr->flags & PTHREAD_ATTR_FLAG_INHERIT) ? PTHREAD_INHERIT_SCHED
+                                                    : PTHREAD_EXPLICIT_SCHED;
+  return 0;
+}
+
 int pthread_attr_setdetachstate(pthread_attr_t* attr, int state) {
   if (state == PTHREAD_CREATE_DETACHED) {
     attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
diff --git a/libc/bionic/pthread_create.cpp b/libc/bionic/pthread_create.cpp
index be0fd1b..11e148c 100644
--- a/libc/bionic/pthread_create.cpp
+++ b/libc/bionic/pthread_create.cpp
@@ -109,7 +109,7 @@
 }
 
 int __init_thread(pthread_internal_t* thread) {
-  int error = 0;
+  thread->cleanup_stack = nullptr;
 
   if (__predict_true((thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) == 0)) {
     atomic_init(&thread->join_state, THREAD_NOT_JOINED);
@@ -117,23 +117,43 @@
     atomic_init(&thread->join_state, THREAD_DETACHED);
   }
 
-  // Set the scheduling policy/priority of the thread.
-  if (thread->attr.sched_policy != SCHED_NORMAL) {
-    sched_param param;
+  // Set the scheduling policy/priority of the thread if necessary.
+  bool need_set = true;
+  int policy;
+  sched_param param;
+  if (thread->attr.flags & PTHREAD_ATTR_FLAG_INHERIT) {
+    // Unless the parent has SCHED_RESET_ON_FORK set, we've already inherited from the parent.
+    policy = sched_getscheduler(0);
+    need_set = ((policy & SCHED_RESET_ON_FORK) != 0);
+    if (need_set) {
+      if (policy == -1) {
+        async_safe_format_log(ANDROID_LOG_WARN, "libc",
+                              "pthread_create sched_getscheduler failed: %s", strerror(errno));
+        return errno;
+      }
+      if (sched_getparam(0, &param) == -1) {
+        async_safe_format_log(ANDROID_LOG_WARN, "libc",
+                              "pthread_create sched_getparam failed: %s", strerror(errno));
+        return errno;
+      }
+    }
+  } else {
+    policy = thread->attr.sched_policy;
     param.sched_priority = thread->attr.sched_priority;
-    if (sched_setscheduler(thread->tid, thread->attr.sched_policy, &param) == -1) {
+  }
+  if (need_set) {
+    if (sched_setscheduler(thread->tid, policy, &param) == -1) {
+      async_safe_format_log(ANDROID_LOG_WARN, "libc",
+                            "pthread_create sched_setscheduler(%d, {%d}) call failed: %s", policy,
+                            param.sched_priority, strerror(errno));
 #if defined(__LP64__)
       // For backwards compatibility reasons, we only report failures on 64-bit devices.
-      error = errno;
+      return errno;
 #endif
-      async_safe_format_log(ANDROID_LOG_WARN, "libc",
-                            "pthread_create sched_setscheduler call failed: %s", strerror(errno));
     }
   }
 
-  thread->cleanup_stack = NULL;
-
-  return error;
+  return 0;
 }
 
 static void* __create_thread_mapped_space(size_t mmap_size, size_t stack_guard_size) {
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index ad8be66..2dd0cff 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -34,12 +34,15 @@
 #include "private/bionic_lock.h"
 #include "private/bionic_tls.h"
 
-/* Has the thread been detached by a pthread_join or pthread_detach call? */
+// Has the thread been detached by a pthread_join or pthread_detach call?
 #define PTHREAD_ATTR_FLAG_DETACHED 0x00000001
 
-/* Has the thread been joined by another thread? */
+// Has the thread been joined by another thread?
 #define PTHREAD_ATTR_FLAG_JOINED 0x00000002
 
+// Should we inherit scheduling attributes from the parent on pthread_create?
+#define PTHREAD_ATTR_FLAG_INHERIT 0x00000004
+
 class pthread_key_data_t {
  public:
   uintptr_t seq; // Use uintptr_t just for alignment, as we use pointer below.
diff --git a/libc/include/bits/posix_limits.h b/libc/include/bits/posix_limits.h
index 2688cc5..8dfea52 100644
--- a/libc/include/bits/posix_limits.h
+++ b/libc/include/bits/posix_limits.h
@@ -77,7 +77,7 @@
 #define _POSIX_THREAD_CPUTIME _POSIX_VERSION /* CLOCK_THREAD_CPUTIME_ID. */
 #define _POSIX_THREAD_PRIO_INHERIT __BIONIC_POSIX_FEATURE_MISSING
 #define _POSIX_THREAD_PRIO_PROTECT __BIONIC_POSIX_FEATURE_MISSING
-#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION /* Strictly, pthread_attr_getinheritsched/pthread_attr_setinheritsched are missing. */
+#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION /* Strictly, pthread_attr_getinheritsched/pthread_attr_setinheritsched arrived in 28. */
 #define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
 #define _POSIX_THREAD_ROBUST_PRIO_INHERIT __BIONIC_POSIX_FEATURE_MISSING
 #define _POSIX_THREAD_ROBUST_PRIO_PROTECT __BIONIC_POSIX_FEATURE_MISSING
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 2253e0d..99515da 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -74,20 +74,24 @@
 #define PTHREAD_STACK_MIN (2 * PAGE_SIZE)
 #endif
 
-#define PTHREAD_CREATE_DETACHED  0x00000001
-#define PTHREAD_CREATE_JOINABLE  0x00000000
+#define PTHREAD_CREATE_DETACHED 1
+#define PTHREAD_CREATE_JOINABLE 0
 
-#define PTHREAD_PROCESS_PRIVATE  0
-#define PTHREAD_PROCESS_SHARED   1
+#define PTHREAD_EXPLICIT_SCHED 0
+#define PTHREAD_INHERIT_SCHED 1
 
-#define PTHREAD_SCOPE_SYSTEM     0
-#define PTHREAD_SCOPE_PROCESS    1
+#define PTHREAD_PROCESS_PRIVATE 0
+#define PTHREAD_PROCESS_SHARED 1
+
+#define PTHREAD_SCOPE_SYSTEM 0
+#define PTHREAD_SCOPE_PROCESS 1
 
 int pthread_atfork(void (*__prepare)(void), void (*__parent)(void), void (*__child)(void)) __INTRODUCED_IN(12);
 
 int pthread_attr_destroy(pthread_attr_t* __attr);
 int pthread_attr_getdetachstate(const pthread_attr_t* __attr, int* __state);
 int pthread_attr_getguardsize(const pthread_attr_t* __attr, size_t* __size);
+int pthread_attr_getinheritsched(const pthread_attr_t* __attr, int* __flag);
 int pthread_attr_getschedparam(const pthread_attr_t* __attr, struct sched_param* __param);
 int pthread_attr_getschedpolicy(const pthread_attr_t* __attr, int* __policy);
 int pthread_attr_getscope(const pthread_attr_t* __attr, int* __scope);
@@ -96,6 +100,7 @@
 int pthread_attr_init(pthread_attr_t* __attr);
 int pthread_attr_setdetachstate(pthread_attr_t* __attr, int __state);
 int pthread_attr_setguardsize(pthread_attr_t* __attr, size_t __size);
+int pthread_attr_setinheritsched(pthread_attr_t* __attr, int __flag);
 int pthread_attr_setschedparam(pthread_attr_t* __attr, const struct sched_param* __param);
 int pthread_attr_setschedpolicy(pthread_attr_t* __attr, int __policy);
 int pthread_attr_setscope(pthread_attr_t* __attr, int __scope);
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index 6e1015a..981cea6 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1363,6 +1363,8 @@
     posix_spawn_file_actions_destroy;
     posix_spawn_file_actions_init;
     posix_spawnp;
+    pthread_attr_getinheritsched;
+    pthread_attr_setinheritsched;
     pthread_setschedprio;
     sethostent;
     setnetent;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index b88ecd0..67fac4e 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1283,6 +1283,8 @@
     posix_spawn_file_actions_destroy;
     posix_spawn_file_actions_init;
     posix_spawnp;
+    pthread_attr_getinheritsched;
+    pthread_attr_setinheritsched;
     pthread_setschedprio;
     sethostent;
     setnetent;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 4fc6535..443d18c 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1388,6 +1388,8 @@
     posix_spawn_file_actions_destroy;
     posix_spawn_file_actions_init;
     posix_spawnp;
+    pthread_attr_getinheritsched;
+    pthread_attr_setinheritsched;
     pthread_setschedprio;
     sethostent;
     setnetent;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 360af09..0f5efff 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1347,6 +1347,8 @@
     posix_spawn_file_actions_destroy;
     posix_spawn_file_actions_init;
     posix_spawnp;
+    pthread_attr_getinheritsched;
+    pthread_attr_setinheritsched;
     pthread_setschedprio;
     sethostent;
     setnetent;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index b88ecd0..67fac4e 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1283,6 +1283,8 @@
     posix_spawn_file_actions_destroy;
     posix_spawn_file_actions_init;
     posix_spawnp;
+    pthread_attr_getinheritsched;
+    pthread_attr_setinheritsched;
     pthread_setschedprio;
     sethostent;
     setnetent;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 04ff514..a802aa1 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1345,6 +1345,8 @@
     posix_spawn_file_actions_destroy;
     posix_spawn_file_actions_init;
     posix_spawnp;
+    pthread_attr_getinheritsched;
+    pthread_attr_setinheritsched;
     pthread_setschedprio;
     sethostent;
     setnetent;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index b88ecd0..67fac4e 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1283,6 +1283,8 @@
     posix_spawn_file_actions_destroy;
     posix_spawn_file_actions_init;
     posix_spawnp;
+    pthread_attr_getinheritsched;
+    pthread_attr_setinheritsched;
     pthread_setschedprio;
     sethostent;
     setnetent;
diff --git a/linker/linker.cpp b/linker/linker.cpp
index ec92c92..5f906c8 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -1433,6 +1433,8 @@
 
   if (search_linked_namespaces) {
     // if a library was not found - look into linked namespaces
+    // preserve current dlerror in the case it fails.
+    DlErrorRestorer dlerror_restorer;
     for (auto& linked_namespace : ns->linked_namespaces()) {
       if (find_library_in_linked_namespace(linked_namespace,
                                            task)) {
diff --git a/linker/linker_config.cpp b/linker/linker_config.cpp
index e036c05..f7d2c53 100644
--- a/linker/linker_config.cpp
+++ b/linker/linker_config.cpp
@@ -43,6 +43,9 @@
 #include <string>
 #include <unordered_map>
 
+#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
+#include <sys/_system_properties.h>
+
 class ConfigParser {
  public:
   enum {
@@ -275,6 +278,15 @@
   return true;
 }
 
+static std::string getVndkVersionString() {
+  char vndk_version_str[1 + PROP_VALUE_MAX] = {};
+  __system_property_get("ro.vndk.version", vndk_version_str + 1);
+  if (strlen(vndk_version_str + 1) != 0 && strcmp(vndk_version_str + 1, "current") != 0) {
+    vndk_version_str[0] = '-';
+  }
+  return vndk_version_str;
+}
+
 static Config g_config;
 
 static constexpr const char* kDefaultConfigName = "default";
@@ -334,6 +346,9 @@
       params.push_back({ "SDK_VER", buf });
     }
 
+    static std::string vndk = getVndkVersionString();
+    params.push_back({ "VNDK_VER", vndk });
+
     for (auto&& path : paths) {
       format_string(&path, params);
     }
diff --git a/linker/linker_globals.h b/linker/linker_globals.h
index d8134af..11ccbd5 100644
--- a/linker/linker_globals.h
+++ b/linker/linker_globals.h
@@ -32,6 +32,7 @@
 #include <link.h>
 #include <stddef.h>
 
+#include <string>
 #include <unordered_map>
 
 #include <async_safe/log.h>
@@ -39,7 +40,6 @@
 #define DL_ERR(fmt, x...) \
     do { \
       async_safe_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
-      /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
     } while (false)
 
 #define DL_WARN(fmt, x...) \
@@ -75,4 +75,16 @@
 char* linker_get_error_buffer();
 size_t linker_get_error_buffer_size();
 
+class DlErrorRestorer {
+ public:
+  DlErrorRestorer() {
+    saved_error_msg_ = linker_get_error_buffer();
+  }
+  ~DlErrorRestorer() {
+    strlcpy(linker_get_error_buffer(), saved_error_msg_.c_str(), linker_get_error_buffer_size());
+  }
+ private:
+  std::string saved_error_msg_;
+};
+
 #endif  /* __LINKER_GLOBALS_H */
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index 7028ca7..3f6da59 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -1589,6 +1589,54 @@
   ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror());
 }
 
+TEST(dlext, ns_inaccessible_error_message) {
+  // We set up 2 namespaces (a and b) and link a->b with a shared library
+  // libtestshared.so. Then try to dlopen different library with the same
+  // name from in namespace a. Note that library should not be accessible
+  // in either namespace but since it's soname is in the list of shared libs
+  // the linker will attempt to find it in linked namespace.
+  //
+  // Check the error message and make sure it mentions correct namespace name.
+  ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
+
+  android_namespace_t* ns_a =
+          android_create_namespace("ns_a",
+                                   nullptr,
+                                   (get_testlib_root() + "/private_namespace_libs").c_str(),
+                                   ANDROID_NAMESPACE_TYPE_ISOLATED,
+                                   nullptr,
+                                   nullptr);
+  ASSERT_TRUE(ns_a != nullptr) << dlerror();
+  ASSERT_TRUE(android_link_namespaces(ns_a, nullptr, g_core_shared_libs.c_str())) << dlerror();
+
+  android_namespace_t* ns_b =
+          android_create_namespace("ns_b",
+                                   nullptr,
+                                   get_testlib_root().c_str(),
+                                   ANDROID_NAMESPACE_TYPE_ISOLATED,
+                                   nullptr,
+                                   nullptr);
+  ASSERT_TRUE(ns_b != nullptr) << dlerror();
+  ASSERT_TRUE(android_link_namespaces(ns_b, nullptr, g_core_shared_libs.c_str())) << dlerror();
+
+  ASSERT_TRUE(android_link_namespaces(ns_a, ns_b, "libtestshared.so")) << dlerror();
+
+  android_dlextinfo extinfo;
+  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
+  extinfo.library_namespace = ns_a;
+
+  std::string library_path = get_testlib_root() + "/inaccessible_libs/libtestshared.so";
+
+  void* handle = android_dlopen_ext(library_path.c_str(), RTLD_NOW, &extinfo);
+  ASSERT_TRUE(handle == nullptr);
+  std::string expected_dlerror =
+      android::base::StringPrintf("dlopen failed: library \"%s\" needed or dlopened by \"%s\""
+                                  " is not accessible for the namespace \"ns_a\"",
+                                  library_path.c_str(),
+                                  get_executable_path().c_str());
+  ASSERT_EQ(expected_dlerror, dlerror());
+}
+
 TEST(dlext, ns_anonymous) {
   static const char* root_lib = "libnstest_root.so";
   std::string shared_libs = g_core_shared_libs + ":" + g_public_lib;
diff --git a/tests/fcntl_test.cpp b/tests/fcntl_test.cpp
index cb00c3a..0a83dff 100644
--- a/tests/fcntl_test.cpp
+++ b/tests/fcntl_test.cpp
@@ -319,6 +319,9 @@
   ASSERT_EQ(0, fstat(fd, &sb));
   ASSERT_EQ(perms, (sb.st_mode & ~S_IFMT));
 
+  // On Android if we're not root, we won't be able to create links anyway...
+  if (getuid() != 0) return;
+
   std::string final_path = android::base::StringPrintf("%s/named_now", dir.dirname);
   ASSERT_EQ(0, linkat(AT_FDCWD, android::base::StringPrintf("/proc/self/fd/%d", fd).c_str(),
                       AT_FDCWD, final_path.c_str(),
diff --git a/tests/libs/Android.bp b/tests/libs/Android.bp
index ba0b1aa..5f27387 100644
--- a/tests/libs/Android.bp
+++ b/tests/libs/Android.bp
@@ -440,6 +440,16 @@
 }
 
 // -----------------------------------------------------------------------------
+// Library for inaccessible shared library test
+// -----------------------------------------------------------------------------
+cc_test_library {
+    name: "libtestshared",
+    defaults: ["bionic_testlib_defaults"],
+    srcs: ["empty.cpp"],
+    relative_install_path: "/inaccessible_libs",
+}
+
+// -----------------------------------------------------------------------------
 // Library with weak undefined function
 // -----------------------------------------------------------------------------
 cc_test_library {
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 85978bd..fb2a679 100755
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -1527,7 +1527,7 @@
   ASSERT_EQ(0, pthread_create(&t, NULL,
             reinterpret_cast<void* (*)(void*)>(pthread_attr_getstack_18908062_helper),
             NULL));
-  pthread_join(t, NULL);
+  ASSERT_EQ(0, pthread_join(t, NULL));
 }
 
 #if defined(__BIONIC__)
@@ -1559,7 +1559,7 @@
 
   // Release the other thread and wait for it to exit.
   pthread_mutex_unlock(&pthread_gettid_np_mutex);
-  pthread_join(t, NULL);
+  ASSERT_EQ(0, pthread_join(t, NULL));
 
   ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
 #else
@@ -1600,7 +1600,7 @@
 TEST(pthread, pthread_cleanup_push__pthread_cleanup_pop) {
   pthread_t t;
   ASSERT_EQ(0, pthread_create(&t, NULL, PthreadCleanupStartRoutine, NULL));
-  pthread_join(t, NULL);
+  ASSERT_EQ(0, pthread_join(t, NULL));
   ASSERT_EQ(2U, cleanup_counter);
 }
 
@@ -2113,13 +2113,22 @@
   ASSERT_EQ(0, pthread_spin_destroy(&lock));
 }
 
-TEST(pthread, pthread_attr_setdetachstate) {
+TEST(pthread, pthread_attr_getdetachstate__pthread_attr_setdetachstate) {
   pthread_attr_t attr;
   ASSERT_EQ(0, pthread_attr_init(&attr));
 
+  int state;
   ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
+  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
+  ASSERT_EQ(PTHREAD_CREATE_DETACHED, state);
+
   ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
+  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
+  ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state);
+
   ASSERT_EQ(EINVAL, pthread_attr_setdetachstate(&attr, 123));
+  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &state));
+  ASSERT_EQ(PTHREAD_CREATE_JOINABLE, state);
 }
 
 TEST(pthread, pthread_create__mmap_failures) {
@@ -2168,3 +2177,107 @@
 TEST(pthread, pthread_setschedprio) {
   ASSERT_EQ(EINVAL, pthread_setschedprio(pthread_self(), INT_MIN));
 }
+
+TEST(pthread, pthread_attr_getinheritsched__pthread_attr_setinheritsched) {
+  pthread_attr_t attr;
+  ASSERT_EQ(0, pthread_attr_init(&attr));
+
+  int state;
+  ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
+  ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
+  ASSERT_EQ(PTHREAD_INHERIT_SCHED, state);
+
+  ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
+  ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
+  ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state);
+
+  ASSERT_EQ(EINVAL, pthread_attr_setinheritsched(&attr, 123));
+  ASSERT_EQ(0, pthread_attr_getinheritsched(&attr, &state));
+  ASSERT_EQ(PTHREAD_EXPLICIT_SCHED, state);
+}
+
+TEST(pthread, pthread_attr_setinheritsched__PTHREAD_INHERIT_SCHED__PTHREAD_EXPLICIT_SCHED) {
+  pthread_attr_t attr;
+  ASSERT_EQ(0, pthread_attr_init(&attr));
+
+  // If we set invalid scheduling attributes but choose to inherit, everything's fine...
+  sched_param param = { .sched_priority = sched_get_priority_max(SCHED_FIFO) + 1 };
+  ASSERT_EQ(0, pthread_attr_setschedparam(&attr, &param));
+  ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_FIFO));
+  ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
+
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr));
+  ASSERT_EQ(0, pthread_join(t, nullptr));
+
+  // If we ask to use them, though...
+  ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
+  ASSERT_EQ(EINVAL, pthread_create(&t, &attr, IdFn, nullptr));
+}
+
+TEST(pthread, pthread_attr_setinheritsched_PTHREAD_INHERIT_SCHED_takes_effect) {
+  sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
+  int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
+  if (rc == EPERM) {
+    GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
+    return;
+  }
+  ASSERT_EQ(0, rc);
+
+  pthread_attr_t attr;
+  ASSERT_EQ(0, pthread_attr_init(&attr));
+  ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
+
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr));
+  int actual_policy;
+  sched_param actual_param;
+  ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
+  ASSERT_EQ(SCHED_FIFO, actual_policy);
+  ASSERT_EQ(0, pthread_join(t, nullptr));
+}
+
+TEST(pthread, pthread_attr_setinheritsched_PTHREAD_EXPLICIT_SCHED_takes_effect) {
+  sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
+  int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
+  if (rc == EPERM) {
+    GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
+    return;
+  }
+  ASSERT_EQ(0, rc);
+
+  pthread_attr_t attr;
+  ASSERT_EQ(0, pthread_attr_init(&attr));
+  ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED));
+  ASSERT_EQ(0, pthread_attr_setschedpolicy(&attr, SCHED_OTHER));
+
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr));
+  int actual_policy;
+  sched_param actual_param;
+  ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
+  ASSERT_EQ(SCHED_OTHER, actual_policy);
+  ASSERT_EQ(0, pthread_join(t, nullptr));
+}
+
+TEST(pthread, pthread_attr_setinheritsched__takes_effect_despite_SCHED_RESET_ON_FORK) {
+  sched_param param = { .sched_priority = sched_get_priority_min(SCHED_FIFO) };
+  int rc = pthread_setschedparam(pthread_self(), SCHED_FIFO | SCHED_RESET_ON_FORK, &param);
+  if (rc == EPERM) {
+    GTEST_LOG_(INFO) << "pthread_setschedparam failed with EPERM, skipping test\n";
+    return;
+  }
+  ASSERT_EQ(0, rc);
+
+  pthread_attr_t attr;
+  ASSERT_EQ(0, pthread_attr_init(&attr));
+  ASSERT_EQ(0, pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED));
+
+  pthread_t t;
+  ASSERT_EQ(0, pthread_create(&t, &attr, IdFn, nullptr));
+  int actual_policy;
+  sched_param actual_param;
+  ASSERT_EQ(0, pthread_getschedparam(t, &actual_policy, &actual_param));
+  ASSERT_EQ(SCHED_FIFO  | SCHED_RESET_ON_FORK, actual_policy);
+  ASSERT_EQ(0, pthread_join(t, nullptr));
+}
diff --git a/tools/versioner/src/DeclarationDatabase.cpp b/tools/versioner/src/DeclarationDatabase.cpp
index 247e58b..aef4f55 100644
--- a/tools/versioner/src/DeclarationDatabase.cpp
+++ b/tools/versioner/src/DeclarationDatabase.cpp
@@ -54,17 +54,19 @@
       }
     }
 
-    if (mangler->shouldMangleDeclName(decl)) {
-      std::string mangled;
-      llvm::raw_string_ostream ss(mangled);
-      mangler->mangleName(decl, ss);
-      return mangled;
-    }
-
+    // The decl might not have a name (e.g. bitfields).
     if (auto identifier = decl->getIdentifier()) {
+      if (mangler->shouldMangleDeclName(decl)) {
+        std::string mangled;
+        llvm::raw_string_ostream ss(mangled);
+        mangler->mangleName(decl, ss);
+        return mangled;
+      }
+
       return identifier->getName();
     }
-    return "<error>";
+
+    return "<unnamed>";
   }
 
   bool VisitDecl(Decl* decl) {
diff --git a/tools/versioner/tests/arch_specific/headers/foo.h b/tools/versioner/tests/arch_specific/headers/foo.h
index 34035b4..4830a68 100644
--- a/tools/versioner/tests/arch_specific/headers/foo.h
+++ b/tools/versioner/tests/arch_specific/headers/foo.h
@@ -1,5 +1,13 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 int foo();
 
 #if defined(__i386__)
 int bar();
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/compilation_error/headers/foo.h b/tools/versioner/tests/compilation_error/headers/foo.h
index c8c1473..51c087a 100644
--- a/tools/versioner/tests/compilation_error/headers/foo.h
+++ b/tools/versioner/tests/compilation_error/headers/foo.h
@@ -1 +1,9 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #error foo
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/dependencies/dependencies/arm/archdep/archdep.h b/tools/versioner/tests/dependencies/dependencies/arm/archdep/archdep.h
index 2005972..b2d357a 100644
--- a/tools/versioner/tests/dependencies/dependencies/arm/archdep/archdep.h
+++ b/tools/versioner/tests/dependencies/dependencies/arm/archdep/archdep.h
@@ -1 +1,9 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 typedef int arm_t;
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/dependencies/dependencies/common/foo/foodep.h b/tools/versioner/tests/dependencies/dependencies/common/foo/foodep.h
index 9feeb6c..baa5857 100644
--- a/tools/versioner/tests/dependencies/dependencies/common/foo/foodep.h
+++ b/tools/versioner/tests/dependencies/dependencies/common/foo/foodep.h
@@ -1 +1,9 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 typedef int foo_t;
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/dependencies/dependencies/x86/archdep/archdep.h b/tools/versioner/tests/dependencies/dependencies/x86/archdep/archdep.h
index 5cc7de2..b73f7cc 100644
--- a/tools/versioner/tests/dependencies/dependencies/x86/archdep/archdep.h
+++ b/tools/versioner/tests/dependencies/dependencies/x86/archdep/archdep.h
@@ -1 +1,9 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 typedef int x86_t;
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/dependencies/headers/foo.h b/tools/versioner/tests/dependencies/headers/foo.h
index 4491f1c..875de1b 100644
--- a/tools/versioner/tests/dependencies/headers/foo.h
+++ b/tools/versioner/tests/dependencies/headers/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #include <archdep.h>
 #include <foodep.h>
 
@@ -6,3 +10,7 @@
 #elif defined(__arm__)
 arm_t foo(foo_t);
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/future/headers/foo.h b/tools/versioner/tests/future/headers/foo.h
index 54e8f0c..51a3a1c 100644
--- a/tools/versioner/tests/future/headers/foo.h
+++ b/tools/versioner/tests/future/headers/foo.h
@@ -1 +1,9 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 int foo() __INTRODUCED_IN_FUTURE;
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/future_arch/headers/foo.h b/tools/versioner/tests/future_arch/headers/foo.h
index 9dd976e..a3258e7 100644
--- a/tools/versioner/tests/future_arch/headers/foo.h
+++ b/tools/versioner/tests/future_arch/headers/foo.h
@@ -1,5 +1,13 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #if defined(__arm__)
 int foo() __INTRODUCED_IN(9);
 #else
 int foo() __INTRODUCED_IN_FUTURE;
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/inline/headers/foo.h b/tools/versioner/tests/inline/headers/foo.h
index a61b386..a337f9c 100644
--- a/tools/versioner/tests/inline/headers/foo.h
+++ b/tools/versioner/tests/inline/headers/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #if __ANDROID_API__ < 12
 static int foo() {
   return 0;
@@ -5,3 +9,7 @@
 #else
 int foo() __INTRODUCED_IN(12);
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/missing_api/expected_fail b/tools/versioner/tests/missing_api/expected_fail
index 18e7845..9f097f7 100644
--- a/tools/versioner/tests/missing_api/expected_fail
+++ b/tools/versioner/tests/missing_api/expected_fail
@@ -1,4 +1,4 @@
   foo: introduced = 9
-    extern declaration @ headers/foo.h:1:1
+    extern declaration @ headers/foo.h:5:1
       introduced = 9
 versioner: version check failed
diff --git a/tools/versioner/tests/missing_api/headers/foo.h b/tools/versioner/tests/missing_api/headers/foo.h
index 3ff3ff7..c201dbb 100644
--- a/tools/versioner/tests/missing_api/headers/foo.h
+++ b/tools/versioner/tests/missing_api/headers/foo.h
@@ -1 +1,9 @@
-int foo() __INTRODUCED_IN(9);
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo() __INTRODUCED_IN(9);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/missing_arch/expected_fail b/tools/versioner/tests/missing_arch/expected_fail
index 82c2b28..7b33e19 100644
--- a/tools/versioner/tests/missing_arch/expected_fail
+++ b/tools/versioner/tests/missing_arch/expected_fail
@@ -1,4 +1,4 @@
   foo: no availability
-    extern declaration @ headers/foo.h:1:1
+    extern declaration @ headers/foo.h:5:1
       no availability
 versioner: version check failed
diff --git a/tools/versioner/tests/missing_arch/headers/foo.h b/tools/versioner/tests/missing_arch/headers/foo.h
index 176e7a3..5ba4794 100644
--- a/tools/versioner/tests/missing_arch/headers/foo.h
+++ b/tools/versioner/tests/missing_arch/headers/foo.h
@@ -1 +1,9 @@
-int foo();
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo();
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_decl/headers/bar.h b/tools/versioner/tests/multiple_decl/headers/bar.h
index 1d3a28c..b16617b 100644
--- a/tools/versioner/tests/multiple_decl/headers/bar.h
+++ b/tools/versioner/tests/multiple_decl/headers/bar.h
@@ -1 +1,9 @@
-int foo() __REMOVED_IN(12);
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo() __REMOVED_IN(12);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_decl/headers/foo.h b/tools/versioner/tests/multiple_decl/headers/foo.h
index 1d3a28c..b16617b 100644
--- a/tools/versioner/tests/multiple_decl/headers/foo.h
+++ b/tools/versioner/tests/multiple_decl/headers/foo.h
@@ -1 +1,9 @@
-int foo() __REMOVED_IN(12);
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo() __REMOVED_IN(12);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_decl_mismatch/expected_fail b/tools/versioner/tests/multiple_decl_mismatch/expected_fail
index 8e8c846..1d1f266 100644
--- a/tools/versioner/tests/multiple_decl_mismatch/expected_fail
+++ b/tools/versioner/tests/multiple_decl_mismatch/expected_fail
@@ -1,8 +1,8 @@
 versioner: inconsistent availability for symbol 'foo'
 versioner: failed to calculate symbol availability
   foo: invalid
-    extern declaration @ headers/bar.h:1:1
+    extern declaration @ headers/bar.h:5:1
       obsoleted = 12
-    extern declaration @ headers/foo.h:1:1
+    extern declaration @ headers/foo.h:5:1
       obsoleted = 9
 versioner: sanity check failed
diff --git a/tools/versioner/tests/multiple_decl_mismatch/headers/bar.h b/tools/versioner/tests/multiple_decl_mismatch/headers/bar.h
index 1d3a28c..b16617b 100644
--- a/tools/versioner/tests/multiple_decl_mismatch/headers/bar.h
+++ b/tools/versioner/tests/multiple_decl_mismatch/headers/bar.h
@@ -1 +1,9 @@
-int foo() __REMOVED_IN(12);
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo() __REMOVED_IN(12);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_decl_mismatch/headers/foo.h b/tools/versioner/tests/multiple_decl_mismatch/headers/foo.h
index 49a73ec..8e8f98c 100644
--- a/tools/versioner/tests/multiple_decl_mismatch/headers/foo.h
+++ b/tools/versioner/tests/multiple_decl_mismatch/headers/foo.h
@@ -1 +1,9 @@
-int foo() __REMOVED_IN(9);
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo() __REMOVED_IN(9);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_definition/expected_fail b/tools/versioner/tests/multiple_definition/expected_fail
index 6c531bd..7070390 100644
--- a/tools/versioner/tests/multiple_definition/expected_fail
+++ b/tools/versioner/tests/multiple_definition/expected_fail
@@ -1,7 +1,7 @@
 versioner: conflicting inline definitions:
   declarations visible in: arm-9 [fob = 32], arm-9 [fob = 64], arm-12 [fob = 32], arm-12 [fob = 64]
-    static definition @ headers/foo.h:1:1
+    static definition @ headers/foo.h:5:1
       no availability
-    static definition @ headers/bar.h:1:1
+    static definition @ headers/bar.h:5:1
       no availability
 versioner: sanity check failed
diff --git a/tools/versioner/tests/multiple_definition/headers/bar.h b/tools/versioner/tests/multiple_definition/headers/bar.h
index a9d0197..29592c6 100644
--- a/tools/versioner/tests/multiple_definition/headers/bar.h
+++ b/tools/versioner/tests/multiple_definition/headers/bar.h
@@ -1,3 +1,11 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 static int foo() {
   return 0;
 }
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_definition/headers/foo.h b/tools/versioner/tests/multiple_definition/headers/foo.h
index a9d0197..29592c6 100644
--- a/tools/versioner/tests/multiple_definition/headers/foo.h
+++ b/tools/versioner/tests/multiple_definition/headers/foo.h
@@ -1,3 +1,11 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 static int foo() {
   return 0;
 }
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_definition_ok/headers/bar.h b/tools/versioner/tests/multiple_definition_ok/headers/bar.h
index c3c87bb..6eced51 100644
--- a/tools/versioner/tests/multiple_definition_ok/headers/bar.h
+++ b/tools/versioner/tests/multiple_definition_ok/headers/bar.h
@@ -1,5 +1,13 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #if __ANDROID_API__ == 12
 static int foo() {
   return 0;
 }
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/multiple_definition_ok/headers/foo.h b/tools/versioner/tests/multiple_definition_ok/headers/foo.h
index 9da9b2a..773d274 100644
--- a/tools/versioner/tests/multiple_definition_ok/headers/foo.h
+++ b/tools/versioner/tests/multiple_definition_ok/headers/foo.h
@@ -1,5 +1,13 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #if __ANDROID_API__ == 9
 static int foo() {
   return 0;
 }
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/obsoleted/headers/foo.h b/tools/versioner/tests/obsoleted/headers/foo.h
index 68f3d43..e9630e5 100644
--- a/tools/versioner/tests/obsoleted/headers/foo.h
+++ b/tools/versioner/tests/obsoleted/headers/foo.h
@@ -1 +1,9 @@
-int foo() __INTRODUCED_IN(9) __REMOVED_IN(11);
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo() __INTRODUCED_IN(9) __REMOVED_IN(11);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor/expected/foo.h b/tools/versioner/tests/preprocessor/expected/foo.h
index 73947b2..cb719f0 100644
--- a/tools/versioner/tests/preprocessor/expected/foo.h
+++ b/tools/versioner/tests/preprocessor/expected/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 int always_available();
 
 int also_always_available() __INTRODUCED_IN(9);
@@ -74,3 +78,7 @@
 int future() __INTRODUCED_IN_FUTURE;
 #endif /* __ANDROID_API__ >= __ANDROID_API_FUTURE__ */
 
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor/headers/foo.h b/tools/versioner/tests/preprocessor/headers/foo.h
index 81c8b4b..2429334 100644
--- a/tools/versioner/tests/preprocessor/headers/foo.h
+++ b/tools/versioner/tests/preprocessor/headers/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 int always_available();
 
 int also_always_available() __INTRODUCED_IN(9);
@@ -42,3 +46,7 @@
 int group_lp32() __INTRODUCED_IN_ARM(12) __INTRODUCED_IN_X86(12) __INTRODUCED_IN_MIPS(12);
 
 int future() __INTRODUCED_IN_FUTURE;
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_file_offset_bits/expected/foo.h b/tools/versioner/tests/preprocessor_file_offset_bits/expected/foo.h
index 7d31ec3..dcfaaee 100644
--- a/tools/versioner/tests/preprocessor_file_offset_bits/expected/foo.h
+++ b/tools/versioner/tests/preprocessor_file_offset_bits/expected/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 typedef int off_t;
 typedef int ssize_t;
 typedef unsigned size_t;
@@ -32,3 +36,7 @@
 #else
 off_t lseek(int __fd, off_t __offset, int __whence);
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_file_offset_bits/headers/foo.h b/tools/versioner/tests/preprocessor_file_offset_bits/headers/foo.h
index 868d1fc..5ffffa8 100644
--- a/tools/versioner/tests/preprocessor_file_offset_bits/headers/foo.h
+++ b/tools/versioner/tests/preprocessor_file_offset_bits/headers/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 typedef int off_t;
 typedef int ssize_t;
 typedef unsigned size_t;
@@ -28,3 +32,7 @@
 #else
 off_t lseek(int __fd, off_t __offset, int __whence);
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_idempotence/expected/foo.h b/tools/versioner/tests/preprocessor_idempotence/expected/foo.h
index ed31e8b..bc442e5 100644
--- a/tools/versioner/tests/preprocessor_idempotence/expected/foo.h
+++ b/tools/versioner/tests/preprocessor_idempotence/expected/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #if __ANDROID_API__ >= 10
 int foo() __INTRODUCED_IN(10);
 #endif
@@ -10,3 +14,7 @@
 int multiple_1() __INTRODUCED_IN(10);
 int multiple_2() __INTRODUCED_IN(10);
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_idempotence/headers/foo.h b/tools/versioner/tests/preprocessor_idempotence/headers/foo.h
index ed31e8b..bc442e5 100644
--- a/tools/versioner/tests/preprocessor_idempotence/headers/foo.h
+++ b/tools/versioner/tests/preprocessor_idempotence/headers/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #if __ANDROID_API__ >= 10
 int foo() __INTRODUCED_IN(10);
 #endif
@@ -10,3 +14,7 @@
 int multiple_1() __INTRODUCED_IN(10);
 int multiple_2() __INTRODUCED_IN(10);
 #endif
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_merging/expected/foo.h b/tools/versioner/tests/preprocessor_merging/expected/foo.h
index 45eb32d..ecd7f71 100644
--- a/tools/versioner/tests/preprocessor_merging/expected/foo.h
+++ b/tools/versioner/tests/preprocessor_merging/expected/foo.h
@@ -1,3 +1,7 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 
 #if __ANDROID_API__ >= 10
 int block_merging_1() __INTRODUCED_IN(10); // foo
@@ -7,3 +11,7 @@
 int block_merging_4() __INTRODUCED_IN(10);
 #endif /* __ANDROID_API__ >= 10 */
 
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_merging/headers/foo.h b/tools/versioner/tests/preprocessor_merging/headers/foo.h
index ac9564b..7aaa471 100644
--- a/tools/versioner/tests/preprocessor_merging/headers/foo.h
+++ b/tools/versioner/tests/preprocessor_merging/headers/foo.h
@@ -1,5 +1,13 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 int block_merging_1() __INTRODUCED_IN(10); // foo
 int block_merging_2() __INTRODUCED_IN(10); /* bar */
 int block_merging_3() __INTRODUCED_IN(10); /* baz
 //*/
 int block_merging_4() __INTRODUCED_IN(10);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_no_guard/expected/foo.h b/tools/versioner/tests/preprocessor_no_guard/expected/foo.h
index 2bf1dbf..3ef0c30 100644
--- a/tools/versioner/tests/preprocessor_no_guard/expected/foo.h
+++ b/tools/versioner/tests/preprocessor_no_guard/expected/foo.h
@@ -1 +1,9 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 int foo() __VERSIONER_NO_GUARD __INTRODUCED_IN(14);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/preprocessor_no_guard/headers/foo.h b/tools/versioner/tests/preprocessor_no_guard/headers/foo.h
index 2bf1dbf..3ef0c30 100644
--- a/tools/versioner/tests/preprocessor_no_guard/headers/foo.h
+++ b/tools/versioner/tests/preprocessor_no_guard/headers/foo.h
@@ -1 +1,9 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 int foo() __VERSIONER_NO_GUARD __INTRODUCED_IN(14);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/smoke/headers/foo.h b/tools/versioner/tests/smoke/headers/foo.h
index 3ff3ff7..c201dbb 100644
--- a/tools/versioner/tests/smoke/headers/foo.h
+++ b/tools/versioner/tests/smoke/headers/foo.h
@@ -1 +1,9 @@
-int foo() __INTRODUCED_IN(9);
\ No newline at end of file
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+int foo() __INTRODUCED_IN(9);
+
+#if defined(__cplusplus)
+}
+#endif
diff --git a/tools/versioner/tests/version_mismatch/expected_fail b/tools/versioner/tests/version_mismatch/expected_fail
index f83f71c..f2143a3 100644
--- a/tools/versioner/tests/version_mismatch/expected_fail
+++ b/tools/versioner/tests/version_mismatch/expected_fail
@@ -1,8 +1,8 @@
 versioner: inconsistent availability for symbol 'foo'
 versioner: failed to calculate symbol availability
   foo: invalid
-    extern declaration @ headers/foo.h:2:1
+    extern declaration @ headers/foo.h:6:1
       introduced = 9
-    extern declaration @ headers/foo.h:4:1
+    extern declaration @ headers/foo.h:8:1
       introduced = 10
 versioner: sanity check failed
diff --git a/tools/versioner/tests/version_mismatch/headers/foo.h b/tools/versioner/tests/version_mismatch/headers/foo.h
index 4604092..ea35f36 100644
--- a/tools/versioner/tests/version_mismatch/headers/foo.h
+++ b/tools/versioner/tests/version_mismatch/headers/foo.h
@@ -1,5 +1,13 @@
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 #if __ANDROID_API__ <= 9
 int foo() __INTRODUCED_IN(9);
 #else
 int foo() __INTRODUCED_IN(10);
 #endif
+
+#if defined(__cplusplus)
+}
+#endif