Merge "versioner: kill some obsolete versions."
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/dns/net/gethnamaddr.c b/libc/dns/net/gethnamaddr.c
index 4e416fd..7118a29 100644
--- a/libc/dns/net/gethnamaddr.c
+++ b/libc/dns/net/gethnamaddr.c
@@ -170,6 +170,15 @@
 	{ 0, 0 }
 };
 
+static int h_errno_to_result(int* herrno_p) {
+  // glibc considers ERANGE a special case (and BSD uses ENOSPC instead).
+  if (*herrno_p == NETDB_INTERNAL && errno == ENOSPC) {
+    errno = ERANGE;
+    return errno;
+  }
+  // glibc considers HOST_NOT_FOUND not an error for the _r functions' return value.
+  return (*herrno_p != HOST_NOT_FOUND) ? *herrno_p : 0;
+}
 
 #ifdef DEBUG
 static void
@@ -519,9 +528,8 @@
     struct hostent **result, int *errorp)
 {
 	res_state res = __res_get_state();
-
 	if (res == NULL) {
-	  *result = NULL;
+		*result = NULL;
 		*errorp = NETDB_INTERNAL;
 		return -1;
 	}
@@ -538,12 +546,7 @@
 	}
 	*result = gethostbyname_internal(name, AF_INET, res, hp, buf, buflen, errorp,
 	                                 &NETCONTEXT_UNSET);
-	__res_put_state(res);
-	if (!*result && errno == ENOSPC) {
-	  errno = ERANGE;
-	  return ERANGE; /* Return error as in linux manual page. */
-	}
-	return (*result) ? 0 : -1;
+	return h_errno_to_result(errorp);
 }
 
 /* The prototype of gethostbyname2_r is from glibc, not that in netbsd. */
@@ -552,7 +555,6 @@
     size_t buflen, struct hostent **result, int *errorp)
 {
 	res_state res = __res_get_state();
-
 	if (res == NULL) {
 		*result = NULL;
 		*errorp = NETDB_INTERNAL;
@@ -560,12 +562,7 @@
 	}
 	*result = gethostbyname_internal(name, af, res, hp, buf, buflen, errorp,
 	                                 &NETCONTEXT_UNSET);
-	__res_put_state(res);
-	if (!*result && errno == ENOSPC) {
-		errno = ERANGE;
-		return ERANGE;
-	}
-	return (*result) ? 0 : -1;
+	return h_errno_to_result(errorp);
 }
 
 __LIBC_HIDDEN__ FILE* android_open_proxy() {
@@ -859,11 +856,7 @@
 {
 	*result = android_gethostbyaddrfornetcontext_proxy_internal(
 		addr, len, af, hp, buf, buflen, h_errnop, &NETCONTEXT_UNSET);
-	if (!*result && errno == ENOSPC) {
-		errno = ERANGE;
-		return ERANGE;
-	}
-	return (*result) ? 0 : -1;
+	return h_errno_to_result(h_errnop);
 }
 
 static struct hostent *
diff --git a/libc/dns/net/sethostent.c b/libc/dns/net/sethostent.c
index 66c305f..a743a54 100644
--- a/libc/dns/net/sethostent.c
+++ b/libc/dns/net/sethostent.c
@@ -129,6 +129,9 @@
 	hp = _hf_gethtbyname2(name, af, info);
 #endif
 	if (hp == NULL) {
+		if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
+			return NS_UNAVAIL; // glibc compatibility.
+		}
 		*info->he = HOST_NOT_FOUND;
 		return NS_NOTFOUND;
 	}
@@ -171,8 +174,12 @@
 
 		hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen,
 		    info->he);
-		if (hp == NULL)
+		if (hp == NULL) {
+			if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
+				goto nospc; // glibc compatibility.
+			}
 			break;
+		}
 
 		if (strcasecmp(hp->h_name, name) != 0) {
 			char **cp;
@@ -270,6 +277,7 @@
 	endhostent_r(&hf);
 
 	if (hp == NULL) {
+		if (errno == ENOSPC) return NS_UNAVAIL; // glibc compatibility.
 		*info->he = HOST_NOT_FOUND;
 		return NS_NOTFOUND;
 	}
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/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/netdb_test.cpp b/tests/netdb_test.cpp
index e699701..a624138 100644
--- a/tests/netdb_test.cpp
+++ b/tests/netdb_test.cpp
@@ -271,8 +271,9 @@
   char buf[4]; // Use too small buffer.
   int err;
   int result = gethostbyname_r("localhost", &hent, buf, sizeof(buf), &hp, &err);
-  ASSERT_EQ(ERANGE, result);
-  ASSERT_EQ(NULL, hp);
+  EXPECT_EQ(NETDB_INTERNAL, err);
+  EXPECT_EQ(ERANGE, result);
+  EXPECT_EQ(NULL, hp);
 }
 
 TEST(netdb, gethostbyname2_r_ERANGE) {
@@ -281,8 +282,9 @@
   char buf[4]; // Use too small buffer.
   int err;
   int result = gethostbyname2_r("localhost", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
-  ASSERT_EQ(ERANGE, result);
-  ASSERT_EQ(NULL, hp);
+  EXPECT_EQ(NETDB_INTERNAL, err);
+  EXPECT_EQ(ERANGE, result);
+  EXPECT_EQ(NULL, hp);
 }
 
 TEST(netdb, gethostbyaddr_r_ERANGE) {
@@ -292,8 +294,43 @@
   char buf[4]; // Use too small buffer.
   int err;
   int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
-  ASSERT_EQ(ERANGE, result);
-  ASSERT_EQ(NULL, hp);
+  EXPECT_EQ(NETDB_INTERNAL, err);
+  EXPECT_EQ(ERANGE, result);
+  EXPECT_EQ(NULL, hp);
+}
+
+TEST(netdb, gethostbyname_r_HOST_NOT_FOUND) {
+  hostent hent;
+  hostent *hp;
+  char buf[BUFSIZ];
+  int err;
+  int result = gethostbyname_r("does.not.exist.google.com", &hent, buf, sizeof(buf), &hp, &err);
+  EXPECT_EQ(HOST_NOT_FOUND, err);
+  EXPECT_EQ(0, result);
+  EXPECT_EQ(NULL, hp);
+}
+
+TEST(netdb, gethostbyname2_r_HOST_NOT_FOUND) {
+  hostent hent;
+  hostent *hp;
+  char buf[BUFSIZ];
+  int err;
+  int result = gethostbyname2_r("does.not.exist.google.com", AF_INET, &hent, buf, sizeof(buf), &hp, &err);
+  EXPECT_EQ(HOST_NOT_FOUND, err);
+  EXPECT_EQ(0, result);
+  EXPECT_EQ(NULL, hp);
+}
+
+TEST(netdb, gethostbyaddr_r_HOST_NOT_FOUND) {
+  in_addr addr = { htonl(0xffffffff) };
+  hostent hent;
+  hostent *hp;
+  char buf[BUFSIZ];
+  int err;
+  int result = gethostbyaddr_r(&addr, sizeof(addr), AF_INET, &hent, buf, sizeof(buf), &hp, &err);
+  EXPECT_EQ(HOST_NOT_FOUND, err);
+  EXPECT_EQ(0, result);
+  EXPECT_EQ(NULL, hp);
 }
 
 TEST(netdb, getservbyname) {
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));
+}