Merge "Revert^3 "[MTE] remap stacks with PROT_MTE when requested by dlopened library"" into main
diff --git a/libc/Android.bp b/libc/Android.bp
index e4bedba..048cd73 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -860,7 +860,7 @@
         "bionic/initgroups.c",
         "bionic/inotify_init.cpp",
         "bionic/ioctl.cpp",
-        "bionic/isatty.c",
+        "bionic/isatty.cpp",
         "bionic/killpg.cpp",
         "bionic/langinfo.cpp",
         "bionic/lchown.cpp",
@@ -931,8 +931,8 @@
         "bionic/rename.cpp",
         "bionic/rmdir.cpp",
         "bionic/scandir.cpp",
-        "bionic/sched_cpualloc.c",
-        "bionic/sched_cpucount.c",
+        "bionic/sched_cpualloc.cpp",
+        "bionic/sched_cpucount.cpp",
         "bionic/sched_getaffinity.cpp",
         "bionic/sched_getcpu.cpp",
         "bionic/semaphore.cpp",
diff --git a/libc/bionic/isatty.c b/libc/bionic/isatty.cpp
similarity index 92%
rename from libc/bionic/isatty.c
rename to libc/bionic/isatty.cpp
index 93af6c5..8a4aaf1 100644
--- a/libc/bionic/isatty.c
+++ b/libc/bionic/isatty.cpp
@@ -26,14 +26,10 @@
  * SUCH DAMAGE.
  */
 
-#include <unistd.h>
 #include <termios.h>
-#include <errno.h>
+#include <unistd.h>
 
-int
-isatty (int  fd)
-{
-  struct termios term;
-
-  return tcgetattr (fd, &term) == 0;
+int isatty(int fd) {
+  termios term;
+  return tcgetattr(fd, &term) == 0;
 }
diff --git a/libc/bionic/sched_cpualloc.c b/libc/bionic/sched_cpualloc.c
deleted file mode 100644
index 345de91..0000000
--- a/libc/bionic/sched_cpualloc.c
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-#define _GNU_SOURCE 1
-#include <sched.h>
-#include <stdlib.h>
-
-cpu_set_t* __sched_cpualloc(size_t count)
-{
-    // The static analyzer complains that CPU_ALLOC_SIZE eventually expands to
-    // N * sizeof(unsigned long), which is incompatible with cpu_set_t. This is
-    // on purpose.
-    return (cpu_set_t*) malloc(CPU_ALLOC_SIZE(count)); // NOLINT
-}
-
-void __sched_cpufree(cpu_set_t* set)
-{
-    free(set);
-}
diff --git a/libc/bionic/sched_cpucount.c b/libc/bionic/sched_cpualloc.cpp
similarity index 83%
copy from libc/bionic/sched_cpucount.c
copy to libc/bionic/sched_cpualloc.cpp
index 6f66589..4c8b3c3 100644
--- a/libc/bionic/sched_cpucount.c
+++ b/libc/bionic/sched_cpualloc.cpp
@@ -25,17 +25,14 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#define _GNU_SOURCE 1
+
 #include <sched.h>
+#include <stdlib.h>
 
-int __sched_cpucount(size_t setsize, const cpu_set_t* set) {
-  int nn = 0;
-  int nn_max = setsize / sizeof(__CPU_BITTYPE);
-  int count = 0;
+cpu_set_t* __sched_cpualloc(size_t count) {
+  return static_cast<cpu_set_t*>(malloc(CPU_ALLOC_SIZE(count)));
+}
 
-  for ( ; nn < nn_max; nn++ ) {
-    count += __builtin_popcountl(set->__bits[nn]);
-  }
-
-  return count;
+void __sched_cpufree(cpu_set_t* set) {
+  free(set);
 }
diff --git a/libc/bionic/sched_cpucount.c b/libc/bionic/sched_cpucount.cpp
similarity index 89%
rename from libc/bionic/sched_cpucount.c
rename to libc/bionic/sched_cpucount.cpp
index 6f66589..3ec27bb 100644
--- a/libc/bionic/sched_cpucount.c
+++ b/libc/bionic/sched_cpucount.cpp
@@ -25,17 +25,13 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#define _GNU_SOURCE 1
+
 #include <sched.h>
 
 int __sched_cpucount(size_t setsize, const cpu_set_t* set) {
-  int nn = 0;
-  int nn_max = setsize / sizeof(__CPU_BITTYPE);
   int count = 0;
-
-  for ( ; nn < nn_max; nn++ ) {
-    count += __builtin_popcountl(set->__bits[nn]);
+  for (size_t i = 0; i < setsize / sizeof(__CPU_BITTYPE); i++) {
+    count += __builtin_popcountl(set->__bits[i]);
   }
-
   return count;
 }
diff --git a/libc/bionic/sysconf.cpp b/libc/bionic/sysconf.cpp
index edbdef1..ff72b93 100644
--- a/libc/bionic/sysconf.cpp
+++ b/libc/bionic/sysconf.cpp
@@ -41,6 +41,16 @@
 #include "platform/bionic/page.h"
 #include "private/bionic_tls.h"
 
+static long __sysconf_fread_long(const char* path) {
+  long result = 0;
+  FILE* fp = fopen(path, "re");
+  if (fp != nullptr) {
+    fscanf(fp, "%ld", &result);
+    fclose(fp);
+  }
+  return result;
+}
+
 struct sysconf_cache {
   long size, assoc, linesize;
 
@@ -90,16 +100,6 @@
 
 #else
 
-long __sysconf_fread_long(const char* path) {
-  long result = 0;
-  FILE* fp = fopen(path, "re");
-  if (fp != nullptr) {
-    fscanf(fp, "%ld", &result);
-    fclose(fp);
-  }
-  return result;
-}
-
 static sysconf_caches* __sysconf_caches() {
   static sysconf_caches cached = []{
     sysconf_caches info = {};
@@ -183,6 +183,7 @@
     case _SC_AVPHYS_PAGES:      return get_avphys_pages();
     case _SC_CHILD_MAX:         return __sysconf_rlimit(RLIMIT_NPROC);
     case _SC_CLK_TCK:           return static_cast<long>(getauxval(AT_CLKTCK));
+    case _SC_NGROUPS_MAX:       return __sysconf_fread_long("/proc/sys/kernel/ngroups_max");
     case _SC_NPROCESSORS_CONF:  return get_nprocs_conf();
     case _SC_NPROCESSORS_ONLN:  return get_nprocs();
     case _SC_OPEN_MAX:          return __sysconf_rlimit(RLIMIT_NOFILE);
@@ -204,7 +205,6 @@
     case _SC_COLL_WEIGHTS_MAX:  return _POSIX2_COLL_WEIGHTS_MAX;  // Minimum requirement.
     case _SC_EXPR_NEST_MAX:     return _POSIX2_EXPR_NEST_MAX;     // Minimum requirement.
     case _SC_LINE_MAX:          return _POSIX2_LINE_MAX;          // Minimum requirement.
-    case _SC_NGROUPS_MAX:       return NGROUPS_MAX;
     case _SC_PASS_MAX:          return PASS_MAX;
     case _SC_2_C_BIND:          return _POSIX2_C_BIND;
     case _SC_2_C_DEV:           return _POSIX2_C_DEV;
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index e9a3080..88f5851 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -968,7 +968,7 @@
   VERIFY_SYSCONF_POSIX_VERSION(_SC_CPUTIME);
   VERIFY_SYSCONF_POSITIVE(_SC_EXPR_NEST_MAX);
   VERIFY_SYSCONF_POSITIVE(_SC_LINE_MAX);
-  VERIFY_SYSCONF_POSITIVE(_SC_NGROUPS_MAX);
+  VerifySysconf(_SC_NGROUPS_MAX, "_SC_NGROUPS_MAX", [](long v){return v >= 0 && v <= NGROUPS_MAX;});
   VERIFY_SYSCONF_POSITIVE(_SC_OPEN_MAX);
   VERIFY_SYSCONF_POSITIVE(_SC_PASS_MAX);
   VERIFY_SYSCONF_POSIX_VERSION(_SC_2_C_BIND);