Merge "Remove #if cruft."
diff --git a/libc/Android.bp b/libc/Android.bp
index 1e65fea..e5a7454 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1088,7 +1088,6 @@
         "bionic/ffs.cpp",
         "bionic/fgetxattr.cpp",
         "bionic/flistxattr.cpp",
-        "bionic/flockfile.cpp",
         "bionic/fpclassify.cpp",
         "bionic/fsetxattr.cpp",
         "bionic/ftruncate.cpp",
diff --git a/libc/bionic/flockfile.cpp b/libc/bionic/flockfile.cpp
deleted file mode 100644
index db53828..0000000
--- a/libc/bionic/flockfile.cpp
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <errno.h>
-#include <stdio.h>
-
-#include "local.h"
-
-// We can't use the OpenBSD implementation which uses kernel-specific
-// APIs not available on Linux. Instead we use a pthread_mutex_t within
-// struct __sfileext (see fileext.h).
-
-void flockfile(FILE* fp) {
-  if (fp != nullptr) {
-    pthread_mutex_lock(&_FLOCK(fp));
-  }
-}
-
-int ftrylockfile(FILE* fp) {
-  // The specification for ftrylockfile() says it returns 0 on success,
-  // or non-zero on error. So return an errno code directly on error.
-  if (fp == nullptr) {
-    return EINVAL;
-  }
-
-  return pthread_mutex_trylock(&_FLOCK(fp));
-}
-
-void funlockfile(FILE* fp) {
-  if (fp != nullptr) {
-    pthread_mutex_unlock(&_FLOCK(fp));
-  }
-}
diff --git a/libc/bionic/pthread_cond.cpp b/libc/bionic/pthread_cond.cpp
index 793dcd9..f444676 100644
--- a/libc/bionic/pthread_cond.cpp
+++ b/libc/bionic/pthread_cond.cpp
@@ -249,15 +249,18 @@
 }
 
 #if !defined(__LP64__)
-// TODO: this exists only for backward binary compatibility on 32 bit platforms.
+// This exists only for backward binary compatibility on 32 bit platforms.
+// (This is actually a _new_ function in API 28 that we could only implement for LP64.)
 extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond_interface,
                                                 pthread_mutex_t* mutex,
                                                 const timespec* abs_timeout) {
   return pthread_cond_timedwait_monotonic_np(cond_interface, mutex, abs_timeout);
 }
+#endif
 
-// Force this function using CLOCK_MONOTONIC because it was always using
-// CLOCK_MONOTONIC in history.
+#if !defined(__LP64__)
+// This exists only for backward binary compatibility on 32 bit platforms.
+// (This function never existed for LP64.)
 extern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond_interface,
                                                   pthread_mutex_t* mutex,
                                                   const timespec* rel_timeout) {
@@ -269,11 +272,15 @@
   }
   return __pthread_cond_timedwait(__get_internal_cond(cond_interface), mutex, false, abs_timeout);
 }
+#endif
 
+#if !defined(__LP64__)
+// This exists only for backward binary compatibility on 32 bit platforms.
+// (This function never existed for LP64.)
 extern "C" int pthread_cond_timeout_np(pthread_cond_t* cond_interface,
                                        pthread_mutex_t* mutex, unsigned ms) {
   timespec ts;
   timespec_from_ms(ts, ms);
   return pthread_cond_timedwait_relative_np(cond_interface, mutex, &ts);
 }
-#endif // !defined(__LP64__)
+#endif
diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp
index a15e981..9b37225 100644
--- a/libc/bionic/pthread_mutex.cpp
+++ b/libc/bionic/pthread_mutex.cpp
@@ -945,6 +945,8 @@
 }
 
 #if !defined(__LP64__)
+// This exists only for backward binary compatibility on 32 bit platforms.
+// (This function never existed for LP64.)
 extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex_interface, unsigned ms) {
     timespec ts;
     timespec_from_ms(ts, ms);
diff --git a/libc/include/dlfcn.h b/libc/include/dlfcn.h
index 68d8bc9..a8066a9 100644
--- a/libc/include/dlfcn.h
+++ b/libc/include/dlfcn.h
@@ -36,21 +36,23 @@
 
 typedef struct {
   /* Pathname of shared object that contains address. */
-  const char* dli_fname;
+  const char* _Nullable dli_fname;
   /* Address at which shared object is loaded. */
-  void* dli_fbase;
+  void* _Nullable dli_fbase;
   /* Name of nearest symbol with address lower than addr. */
-  const char* dli_sname;
+  const char* _Nullable dli_sname;
   /* Exact address of symbol named in dli_sname. */
-  void* dli_saddr;
+  void* _Nullable dli_saddr;
 } Dl_info;
 
-void* dlopen(const char* __filename, int __flag);
-int dlclose(void* __handle);
-char* dlerror(void);
-void* dlsym(void* __handle, const char* __symbol);
-void* dlvsym(void* __handle, const char* __symbol, const char* __version) __INTRODUCED_IN(24);
-int dladdr(const void* __addr, Dl_info* __info);
+void* _Nullable dlopen(const char* _Nullable __filename, int __flag);
+int dlclose(void* _Nonnull __handle);
+char* _Nullable dlerror(void);
+/* (RTLD_DEFAULT is null for LP64, but -1 for LP32) */
+void* _Nullable dlsym(void* __BIONIC_COMPLICATED_NULLNESS __handle, const char* _Nullable __symbol);
+/* (RTLD_DEFAULT is null for LP64, but -1 for LP32) */
+void* _Nullable dlvsym(void* __BIONIC_COMPLICATED_NULLNESS __handle, const char* _Nullable __symbol, const char* _Nullable __version) __INTRODUCED_IN(24);
+int dladdr(const void* _Nonnull __addr, Dl_info* _Nonnull __info);
 
 #define RTLD_LOCAL    0
 #define RTLD_LAZY     0x00001
diff --git a/libc/include/ifaddrs.h b/libc/include/ifaddrs.h
index 9eaabbd..7c0dcbf 100644
--- a/libc/include/ifaddrs.h
+++ b/libc/include/ifaddrs.h
@@ -44,26 +44,26 @@
  */
 struct ifaddrs {
   /** Pointer to the next element in the linked list. */
-  struct ifaddrs* ifa_next;
+  struct ifaddrs* _Nullable ifa_next;
 
   /** Interface name. */
-  char* ifa_name;
+  char* _Nullable ifa_name;
   /** Interface flags (like `SIOCGIFFLAGS`). */
   unsigned int ifa_flags;
   /** Interface address. */
-  struct sockaddr* ifa_addr;
+  struct sockaddr* _Nullable ifa_addr;
   /** Interface netmask. */
-  struct sockaddr* ifa_netmask;
+  struct sockaddr* _Nullable ifa_netmask;
 
   union {
     /** Interface broadcast address (if IFF_BROADCAST is set). */
-    struct sockaddr* ifu_broadaddr;
+    struct sockaddr* _Nullable ifu_broadaddr;
     /** Interface destination address (if IFF_POINTOPOINT is set). */
-    struct sockaddr* ifu_dstaddr;
+    struct sockaddr* _Nullable ifu_dstaddr;
   } ifa_ifu;
 
   /** Unused. */
-  void* ifa_data;
+  void* _Nullable ifa_data;
 };
 
 /** Synonym for `ifa_ifu.ifu_broadaddr` in `struct ifaddrs`. */
@@ -80,7 +80,7 @@
  *
  * Available since API level 24.
  */
-int getifaddrs(struct ifaddrs** __list_ptr) __INTRODUCED_IN(24);
+int getifaddrs(struct ifaddrs* _Nullable * _Nonnull __list_ptr) __INTRODUCED_IN(24);
 
 /**
  * [freeifaddrs(3)](http://man7.org/linux/man-pages/man3/freeifaddrs.3.html) frees a linked list
@@ -88,6 +88,6 @@
  *
  * Available since API level 24.
  */
-void freeifaddrs(struct ifaddrs* __ptr) __INTRODUCED_IN(24);
+void freeifaddrs(struct ifaddrs* _Nullable __ptr) __INTRODUCED_IN(24);
 
 __END_DECLS
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 130a72f..fd1ebc3 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -207,23 +207,6 @@
 int pthread_mutex_trylock(pthread_mutex_t* __mutex);
 int pthread_mutex_unlock(pthread_mutex_t* __mutex);
 
-#if __ANDROID_API__ < 21
-/*
- * Cruft for supporting old API levels. Pre-L we didn't have the proper POSIX
- * APIs for things, but instead had some locally grown, artisan equivalents.
- * Keep exposing the old prototypes on old API levels so we don't regress
- * functionality.
- *
- * See the following bugs:
- *  * https://github.com/android-ndk/ndk/issues/420
- *  * https://github.com/android-ndk/ndk/issues/423
- *  * https://stackoverflow.com/q/44580542/632035
- */
-int pthread_mutex_lock_timeout_np(pthread_mutex_t* __mutex, unsigned __timeout_ms);
-int pthread_cond_timeout_np(pthread_cond_t* __cond, pthread_mutex_t* __mutex, unsigned __timeout_ms);
-int pthread_cond_timedwait_relative_np(pthread_cond_t* __cond, pthread_mutex_t* __mutex, const struct timespec* __relative_timeout);
-#endif
-
 int pthread_once(pthread_once_t* __once, void (*__init_routine)(void));
 
 int pthread_rwlockattr_init(pthread_rwlockattr_t* __attr);
diff --git a/libc/include/stdio_ext.h b/libc/include/stdio_ext.h
index eda5919..8b106a6 100644
--- a/libc/include/stdio_ext.h
+++ b/libc/include/stdio_ext.h
@@ -44,7 +44,7 @@
  *
  * Available since API level 23.
  */
-size_t __fbufsize(FILE* __fp) __INTRODUCED_IN(23);
+size_t __fbufsize(FILE* _Nonnull __fp) __INTRODUCED_IN(23);
 
 /**
  * [__freadable(3)](http://man7.org/linux/man-pages/man3/__freadable.3.html) returns non-zero if
@@ -52,7 +52,7 @@
  *
  * Available since API level 23.
  */
-int __freadable(FILE* __fp) __INTRODUCED_IN(23);
+int __freadable(FILE* _Nonnull __fp) __INTRODUCED_IN(23);
 
 /**
  * [__freading(3)](http://man7.org/linux/man-pages/man3/__freading.3.html) returns non-zero if
@@ -60,7 +60,7 @@
  *
  * Available since API level 28.
  */
-int __freading(FILE* __fp) __INTRODUCED_IN(28);
+int __freading(FILE* _Nonnull __fp) __INTRODUCED_IN(28);
 
 /**
  * [__fwritable(3)](http://man7.org/linux/man-pages/man3/__fwritable.3.html) returns non-zero if
@@ -68,7 +68,7 @@
  *
  * Available since API level 23.
  */
-int __fwritable(FILE* __fp) __INTRODUCED_IN(23);
+int __fwritable(FILE* _Nonnull __fp) __INTRODUCED_IN(23);
 
 /**
  * [__fwriting(3)](http://man7.org/linux/man-pages/man3/__fwriting.3.html) returns non-zero if
@@ -76,7 +76,7 @@
  *
  * Available since API level 28.
  */
-int __fwriting(FILE* __fp) __INTRODUCED_IN(28);
+int __fwriting(FILE* _Nonnull __fp) __INTRODUCED_IN(28);
 
 /**
  * [__flbf(3)](http://man7.org/linux/man-pages/man3/__flbf.3.html) returns non-zero if
@@ -84,7 +84,7 @@
  *
  * Available since API level 23.
  */
-int __flbf(FILE* __fp) __INTRODUCED_IN(23);
+int __flbf(FILE* _Nonnull __fp) __INTRODUCED_IN(23);
 
 /**
  * [__fpurge(3)](http://man7.org/linux/man-pages/man3/__fpurge.3.html) discards the contents of
@@ -92,7 +92,7 @@
  *
  * Available since API level 23.
  */
-void __fpurge(FILE* __fp) __INTRODUCED_IN(23);
+void __fpurge(FILE* _Nonnull __fp) __INTRODUCED_IN(23);
 
 /**
  * [__fpending(3)](http://man7.org/linux/man-pages/man3/__fpending.3.html) returns the number of
@@ -100,7 +100,7 @@
  *
  * Available since API level 23.
  */
-size_t __fpending(FILE* __fp) __INTRODUCED_IN(23);
+size_t __fpending(FILE* _Nonnull __fp) __INTRODUCED_IN(23);
 
 /**
  * __freadahead(3) returns the number of bytes in the input buffer.
@@ -108,7 +108,7 @@
  *
  * Available since API level 34.
  */
-size_t __freadahead(FILE* __fp) __INTRODUCED_IN(34);
+size_t __freadahead(FILE* _Nonnull __fp) __INTRODUCED_IN(34);
 
 /**
  * [_flushlbf(3)](http://man7.org/linux/man-pages/man3/_flushlbf.3.html) flushes all
@@ -124,7 +124,7 @@
  *
  * Available since API level 28.
  */
-void __fseterr(FILE* __fp) __INTRODUCED_IN(28);
+void __fseterr(FILE* _Nonnull __fp) __INTRODUCED_IN(28);
 
 /** __fsetlocking() constant to query locking type. */
 #define FSETLOCKING_QUERY 0
@@ -141,6 +141,6 @@
  *
  * Available since API level 23.
  */
-int __fsetlocking(FILE* __fp, int __type) __INTRODUCED_IN(23);
+int __fsetlocking(FILE* _Nonnull __fp, int __type) __INTRODUCED_IN(23);
 
 __END_DECLS
diff --git a/libc/include/string.h b/libc/include/string.h
index 74dfbda..d6b2967 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -136,12 +136,8 @@
 int strcoll(const char* _Nonnull __lhs, const char* _Nonnull __rhs) __attribute_pure__;
 size_t strxfrm(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n);
 
-#if __ANDROID_API__ >= 21
 int strcoll_l(const char* _Nonnull __lhs, const char* _Nonnull __rhs, locale_t _Nonnull __l) __attribute_pure__ __INTRODUCED_IN(21);
 size_t strxfrm_l(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n, locale_t _Nonnull __l) __INTRODUCED_IN(21);
-#else
-// Implemented as static inlines before 21.
-#endif
 
 #if defined(__USE_GNU) && !defined(basename)
 /*
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index f0966de..add3606 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -121,7 +121,6 @@
 int wprintf(const wchar_t* __fmt, ...);
 int wscanf(const wchar_t* __fmt, ...);
 
-#if __ANDROID_API__ >= 21
 long long wcstoll_l(const wchar_t* __s, wchar_t** __end_ptr, int __base, locale_t __l) __INTRODUCED_IN(21);
 unsigned long long wcstoull_l(const wchar_t* __s, wchar_t** __end_ptr, int __base, locale_t __l) __INTRODUCED_IN(21);
 long double wcstold_l(const wchar_t* __s, wchar_t** __end_ptr, locale_t __l) __INTRODUCED_IN(21);
@@ -129,9 +128,6 @@
 int wcscoll_l(const wchar_t* __lhs, const wchar_t* __rhs, locale_t __l) __attribute_pure__
     __INTRODUCED_IN(21);
 size_t wcsxfrm_l(wchar_t* __dst, const wchar_t* __src, size_t __n, locale_t __l) __INTRODUCED_IN(21);
-#else
-// Implemented as static inlines before 21.
-#endif
 
 size_t wcslcat(wchar_t* __dst, const wchar_t* __src, size_t __n);
 size_t wcslcpy(wchar_t* __dst, const wchar_t* __src, size_t __n);
diff --git a/libc/include/wctype.h b/libc/include/wctype.h
index 7da2bb4..344343f 100644
--- a/libc/include/wctype.h
+++ b/libc/include/wctype.h
@@ -35,7 +35,6 @@
 
 __BEGIN_DECLS
 
-#if __ANDROID_API__ >= 21
 int iswalnum_l(wint_t __wc, locale_t _Nonnull __l) __INTRODUCED_IN(21);
 int iswalpha_l(wint_t __wc, locale_t _Nonnull __l) __INTRODUCED_IN(21);
 int iswblank_l(wint_t __wc, locale_t _Nonnull __l) __INTRODUCED_IN(21);
@@ -51,9 +50,6 @@
 
 wint_t towlower_l(wint_t __wc, locale_t _Nonnull __l) __INTRODUCED_IN(21);
 wint_t towupper_l(wint_t __wc, locale_t _Nonnull __l) __INTRODUCED_IN(21);
-#else
-// Implemented as static inlines before 21.
-#endif
 
 wint_t towctrans_l(wint_t __wc, wctrans_t _Nonnull __transform, locale_t _Nonnull __l) __INTRODUCED_IN(26);
 wctrans_t _Nonnull wctrans_l(const char* _Nonnull __name, locale_t _Nonnull __l) __INTRODUCED_IN(26);
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index 6ffda49..2fc12a0 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -170,7 +170,6 @@
 #define _EXT(fp) __BIONIC_CAST(reinterpret_cast, struct __sfileext*, (fp)->_ext._base)
 
 #define _UB(fp) _EXT(fp)->_ub
-#define _FLOCK(fp) _EXT(fp)->_lock
 
 #define _FILEEXT_SETUP(fp, fext)                                              \
   do {                                                                        \
diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp
index 27813a6..4f12fd0 100644
--- a/libc/stdio/stdio.cpp
+++ b/libc/stdio/stdio.cpp
@@ -197,7 +197,7 @@
 	fp->_lb._size = 0;
 
 	memset(_EXT(fp), 0, sizeof(struct __sfileext));
-	_FLOCK(fp) = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+	_EXT(fp)->_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 	_EXT(fp)->_caller_handles_locking = false;
 
 	// Caller sets cookie, _read/_write etc.
@@ -1239,6 +1239,23 @@
   return __FILE_close(fp);
 }
 
+void flockfile(FILE* fp) {
+  CHECK_FP(fp);
+  pthread_mutex_lock(&_EXT(fp)->_lock);
+}
+
+int ftrylockfile(FILE* fp) {
+  CHECK_FP(fp);
+  // The specification for ftrylockfile() says it returns 0 on success,
+  // or non-zero on error. We don't bother canonicalizing to 0/-1...
+  return pthread_mutex_trylock(&_EXT(fp)->_lock);
+}
+
+void funlockfile(FILE* fp) {
+  CHECK_FP(fp);
+  pthread_mutex_unlock(&_EXT(fp)->_lock);
+}
+
 namespace {
 
 namespace phony {
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 939c092..3f70279 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -889,11 +889,14 @@
   void* sym;
 
 #if defined(__BIONIC__) && !defined(__LP64__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnonnull"
   // RTLD_DEFAULT in lp32 bionic is not (void*)0
   // so it can be distinguished from the NULL handle.
   sym = dlsym(nullptr, "test");
   ASSERT_TRUE(sym == nullptr);
   ASSERT_STREQ("dlsym failed: library handle is null", dlerror());
+#pragma clang diagnostic pop
 #endif
 
   // Symbol that doesn't exist.
@@ -1008,9 +1011,12 @@
 
   dlerror(); // Clear any pending errors.
 
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnonnull"
   // No symbol corresponding to NULL.
   ASSERT_EQ(dladdr(nullptr, &info), 0); // Zero on error, non-zero on success.
   ASSERT_TRUE(dlerror() == nullptr); // dladdr(3) doesn't set dlerror(3).
+#pragma clang diagnostic pop
 
   // No symbol corresponding to a stack address.
   ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
diff --git a/tests/elftls_dl_test.cpp b/tests/elftls_dl_test.cpp
index 82ccf82..56736e7 100644
--- a/tests/elftls_dl_test.cpp
+++ b/tests/elftls_dl_test.cpp
@@ -30,6 +30,7 @@
 #include <link.h>
 
 #include <android-base/file.h>
+#include <android-base/test_utils.h>
 #include <gtest/gtest.h>
 
 #include <thread>
@@ -153,6 +154,7 @@
 }
 
 TEST(elftls_dl, dtv_resize) {
+  SKIP_WITH_HWASAN; // TODO(b/271243811): Fix for new toolchain
 #if defined(__BIONIC__)
 #define LOAD_LIB(soname) ({                           \
     auto lib = dlopen(soname, RTLD_LOCAL | RTLD_NOW); \