Merge "Add support for scudo."
diff --git a/libc/include/bits/fortify/stdio.h b/libc/include/bits/fortify/stdio.h
index 8bb5b68..528d5fb 100644
--- a/libc/include/bits/fortify/stdio.h
+++ b/libc/include/bits/fortify/stdio.h
@@ -50,7 +50,23 @@
 }
 #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
 
+__BIONIC_ERROR_FUNCTION_VISIBILITY
+int sprintf(char* dest, const char* format)
+    __overloadable
+    __enable_if(__bos_unevaluated_lt(__bos(dest), __builtin_strlen(format)),
+                "format string will always overflow destination buffer")
+    __errorattr("format string will always overflow destination buffer");
+
 #if __ANDROID_API__ >= __ANDROID_API_J_MR1__
+__BIONIC_FORTIFY_VARIADIC __printflike(2, 3)
+int sprintf(char* const __pass_object_size dest, const char* format, ...) __overloadable {
+    va_list va;
+    va_start(va, format);
+    int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
+    va_end(va);
+    return result;
+}
+
 /* No diag -- clang diagnoses misuses of this on its own.  */
 __BIONIC_FORTIFY_VARIADIC __printflike(3, 4)
 int snprintf(char* const __pass_object_size dest, size_t size, const char* format, ...)
@@ -61,26 +77,9 @@
     va_end(va);
     return result;
 }
-
-__BIONIC_ERROR_FUNCTION_VISIBILITY
-int sprintf(char* dest, const char* format)
-    __overloadable
-    __enable_if(__bos_unevaluated_lt(__bos(dest), __builtin_strlen(format)),
-                "format string will always overflow destination buffer")
-    __errorattr("format string will always overflow destination buffer");
-
-__BIONIC_FORTIFY_VARIADIC __printflike(2, 3)
-int sprintf(char* const __pass_object_size dest, const char* format, ...) __overloadable {
-    va_list va;
-    va_start(va, format);
-    int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
-    va_end(va);
-    return result;
-}
 #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
 
-#if __ANDROID_API__ >= __ANDROID_API_N__
-#define __bos_trivially_not_lt_mul(bos_val, size, count) \
+#define __bos_trivially_ge_mul(bos_val, size, count) \
   __bos_dynamic_check_impl_and(bos_val, >=, (size) * (count), \
                                !__unsafe_check_mul_overflow(size, count))
 
@@ -91,12 +90,14 @@
                          "in call to 'fread', size * count overflows")
         __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count),
                          "in call to 'fread', size * count is too large for the given buffer") {
+#if __ANDROID_API__ >= __ANDROID_API_N__
     size_t bos = __bos0(buf);
 
-    if (__bos_trivially_not_lt_mul(bos, size, count)) {
-        return __call_bypassing_fortify(fread)(buf, size, count, stream);
+    if (!__bos_trivially_ge_mul(bos, size, count)) {
+        return __fread_chk(buf, size, count, stream, bos);
     }
-    return __fread_chk(buf, size, count, stream, bos);
+#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
+    return __call_bypassing_fortify(fread)(buf, size, count, stream);
 }
 
 __BIONIC_FORTIFY_INLINE
@@ -106,32 +107,31 @@
                          "in call to 'fwrite', size * count overflows")
         __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count),
                          "in call to 'fwrite', size * count is too large for the given buffer") {
+#if __ANDROID_API__ >= __ANDROID_API_N__
     size_t bos = __bos0(buf);
 
-    if (__bos_trivially_not_lt_mul(bos, size, count)) {
-        return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
+    if (!__bos_trivially_ge_mul(bos, size, count)) {
+        return __fwrite_chk(buf, size, count, stream, bos);
     }
-
-    return __fwrite_chk(buf, size, count, stream, bos);
-}
-#undef __bos_trivially_not_lt_mul
 #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
+    return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
+}
+#undef __bos_trivially_ge_mul
 
-#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
 __BIONIC_FORTIFY_INLINE
 char* fgets(char* const __pass_object_size dest, int size, FILE* stream)
         __overloadable
         __clang_error_if(size < 0, "in call to 'fgets', size should not be negative")
         __clang_error_if(__bos_unevaluated_lt(__bos(dest), size),
                          "in call to 'fgets', size is larger than the destination buffer") {
+#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
     size_t bos = __bos(dest);
 
-    if (__bos_dynamic_check_impl_and(bos, >=, (size_t)size, size >= 0)) {
-        return __call_bypassing_fortify(fgets)(dest, size, stream);
+    if (!__bos_dynamic_check_impl_and(bos, >=, (size_t)size, size >= 0)) {
+        return __fgets_chk(dest, size, stream, bos);
     }
-
-    return __fgets_chk(dest, size, stream, bos);
-}
 #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
+    return __call_bypassing_fortify(fgets)(dest, size, stream);
+}
 
 #endif /* defined(__BIONIC_FORTIFY) */
diff --git a/libc/include/sys/select.h b/libc/include/sys/select.h
index 9be444c..1ee8d2a 100644
--- a/libc/include/sys/select.h
+++ b/libc/include/sys/select.h
@@ -26,8 +26,12 @@
  * SUCH DAMAGE.
  */
 
-#ifndef _SYS_SELECT_H_
-#define _SYS_SELECT_H_
+#pragma once
+
+/**
+ * @file sys/select.h
+ * @brief Wait for events on a set of file descriptors (but use <poll.h> instead).
+ */
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
@@ -39,9 +43,17 @@
 
 typedef unsigned long fd_mask;
 
+/**
+ * The limit on the largest fd that can be used with this API.
+ * Use <poll.h> instead.
+ */
 #define FD_SETSIZE 1024
 #define NFDBITS (8 * sizeof(fd_mask))
 
+/**
+ * The type of a file descriptor set. Limited to 1024 fds.
+ * Use <poll.h> instead.
+ */
 typedef struct {
   fd_mask fds_bits[FD_SETSIZE/NFDBITS];
 } fd_set;
@@ -67,21 +79,53 @@
 #define __FD_SET(fd, set) (__FDS_BITS(fd_set*,set)[__FDELT(fd)] |= __FDMASK(fd))
 #define __FD_ISSET(fd, set) ((__FDS_BITS(const fd_set*,set)[__FDELT(fd)] & __FDMASK(fd)) != 0)
 
-
 #if __ANDROID_API__ >= __ANDROID_API_L__
+
+/** Removes `fd` from the given set. Use <poll.h> instead. */
 #define FD_CLR(fd, set) __FD_CLR_chk(fd, set, __bos(set))
+/** Adds `fd` to the given set. Use <poll.h> instead. */
 #define FD_SET(fd, set) __FD_SET_chk(fd, set, __bos(set))
+/** Tests whether `fd` is in the given set. Use <poll.h> instead. */
 #define FD_ISSET(fd, set) __FD_ISSET_chk(fd, set, __bos(set))
+
 #else
+
+/** Removes `fd` from the given set. Use <poll.h> instead. */
 #define FD_CLR(fd, set) __FD_CLR(fd, set)
+/** Adds `fd` to the given set. Use <poll.h> instead. */
 #define FD_SET(fd, set) __FD_SET(fd, set)
+/** Tests whether `fd` is in the given set. Use <poll.h> instead. */
 #define FD_ISSET(fd, set) __FD_ISSET(fd, set)
+
 #endif /* __ANDROID_API >= 21 */
 
+/**
+ * [select(2)](http://man7.org/linux/man-pages/man2/select.2.html) waits on a
+ * set of file descriptors. Use poll() instead.
+ *
+ * Returns the number of ready file descriptors on success, 0 for timeout,
+ * and returns -1 and sets `errno` on failure.
+ */
 int select(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, struct timeval* __timeout);
+
+/**
+ * [pselect(2)](http://man7.org/linux/man-pages/man2/select.2.html) waits on a
+ * set of file descriptors. Use ppoll() instead.
+ *
+ * Returns the number of ready file descriptors on success, 0 for timeout,
+ * and returns -1 and sets `errno` on failure.
+ */
 int pselect(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, const struct timespec* __timeout, const sigset_t* __mask);
+
+/**
+ * [pselect64(2)](http://man7.org/linux/man-pages/man2/select.2.html) waits on a
+ * set of file descriptors. Use ppoll64() instead.
+ *
+ * Returns the number of ready file descriptors on success, 0 for timeout,
+ * and returns -1 and sets `errno` on failure.
+ *
+ * Available since API level 28.
+ */
 int pselect64(int __fd_count, fd_set* __read_fds, fd_set* __write_fds, fd_set* __exception_fds, const struct timespec* __timeout, const sigset64_t* __mask) __INTRODUCED_IN(28);
 
 __END_DECLS
-
-#endif