Merge "Allow asan for glibc tests."
diff --git a/libc/Android.mk b/libc/Android.mk
index 345a1f3..7d438f4 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -213,6 +213,7 @@
     bionic/stubs.cpp \
     bionic/symlink.cpp \
     bionic/sysconf.cpp \
+    bionic/sysinfo.cpp \
     bionic/syslog.cpp \
     bionic/sys_siglist.c \
     bionic/sys_signame.c \
diff --git a/libc/bionic/pathconf.cpp b/libc/bionic/pathconf.cpp
index de9e022..e6f5742 100644
--- a/libc/bionic/pathconf.cpp
+++ b/libc/bionic/pathconf.cpp
@@ -100,12 +100,16 @@
     case _PC_2_SYMLINKS:
       return __2_symlinks(s);
 
+    case _PC_ALLOC_SIZE_MIN:  /* fall through */
+    case _PC_REC_XFER_ALIGN:
+      return s.f_frsize;
+
+    case _PC_REC_MIN_XFER_SIZE:
+      return s.f_bsize;
+
 #if 0
-    case _PC_ALLOC_SIZE_MIN:
     case _PC_REC_INCR_XFER_SIZE:
     case _PC_REC_MAX_XFER_SIZE:
-    case _PC_REC_MIN_XFER_SIZE:
-    case _PC_REC_XFER_ALIGN:
 #endif
 
     case _PC_SYMLINK_MAX:
diff --git a/libc/bionic/sysconf.cpp b/libc/bionic/sysconf.cpp
index 951860d..2df6acc 100644
--- a/libc/bionic/sysconf.cpp
+++ b/libc/bionic/sysconf.cpp
@@ -26,20 +26,17 @@
  * SUCH DAMAGE.
  */
 
-#include <ctype.h>
-#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <pthread.h>
 #include <stdio.h>  // For FOPEN_MAX.
-#include <string.h>
 #include <sys/sysconf.h>
+#include <sys/sysinfo.h>
 #include <time.h>
 #include <unistd.h>
 
 #include "private/bionic_tls.h"
-#include "private/ScopedReaddir.h"
 
 /* seems to be the default on Linux, per the GLibc sources and my own digging */
 
@@ -68,88 +65,13 @@
 #define  SYSTEM_2_UPE        -1       /* No UPE for you ! (User Portability Utilities) */
 #define  SYSTEM_2_VERSION    -1       /* No posix command-line tools */
 
-static bool __matches_cpuN(const char* s) {
-  // The %c trick is to ensure that we have the anchored match "^cpu[0-9]+$".
-  unsigned cpu;
-  char dummy;
-  return (sscanf(s, "cpu%u%c", &cpu, &dummy) == 1);
-}
-
-static int __sysconf_nprocessors_conf() {
-  // On x86 kernels you can use /proc/cpuinfo for this, but on ARM kernels offline CPUs disappear
-  // from there. This method works on both.
-  ScopedReaddir reader("/sys/devices/system/cpu");
-  if (reader.IsBad()) {
-    return 1;
-  }
-
-  int result = 0;
-  dirent* entry;
-  while ((entry = reader.ReadEntry()) != NULL) {
-    if (entry->d_type == DT_DIR && __matches_cpuN(entry->d_name)) {
-      ++result;
-    }
-  }
-  return result;
-}
-
-static int __sysconf_nprocessors_onln() {
-  FILE* fp = fopen("/proc/stat", "re");
-  if (fp == NULL) {
-    return 1;
-  }
-
-  int result = 0;
-  char buf[256];
-  while (fgets(buf, sizeof(buf), fp) != NULL) {
-    // Extract just the first word from the line.
-    // 'cpu0 7976751 1364388 3116842 469770388 8629405 0 49047 0 0 0'
-    char* p = strchr(buf, ' ');
-    if (p != NULL) {
-      *p = 0;
-    }
-    if (__matches_cpuN(buf)) {
-      ++result;
-    }
-  }
-  fclose(fp);
-  return result;
-}
-
-static int __get_meminfo(const char* pattern) {
-  FILE* fp = fopen("/proc/meminfo", "re");
-  if (fp == NULL) {
-    return -1;
-  }
-
-  int result = -1;
-  char buf[256];
-  while (fgets(buf, sizeof(buf), fp) != NULL) {
-    long total;
-    if (sscanf(buf, pattern, &total) == 1) {
-      result = (int) (total / (PAGE_SIZE/1024));
-      break;
-    }
-  }
-  fclose(fp);
-  return result;
-}
-
-static int __sysconf_phys_pages() {
-  return __get_meminfo("MemTotal: %ld kB");
-}
-
-static int __sysconf_avphys_pages() {
-  return __get_meminfo("MemFree: %ld kB");
-}
-
 static int __sysconf_monotonic_clock() {
   timespec t;
   int rc = clock_getres(CLOCK_MONOTONIC, &t);
   return (rc == -1) ? -1 : _POSIX_VERSION;
 }
 
-int sysconf(int name) {
+long sysconf(int name) {
   switch (name) {
     case _SC_ARG_MAX:           return _POSIX_ARG_MAX;
     case _SC_CHILD_MAX:         return CHILD_MAX;
@@ -244,10 +166,10 @@
     //case _SC_THREAD_SAFE_FUNCTIONS:  return _POSIX_THREAD_SAFE_FUNCTIONS
 
     case _SC_MONOTONIC_CLOCK:   return __sysconf_monotonic_clock();
-    case _SC_NPROCESSORS_CONF:  return __sysconf_nprocessors_conf();
-    case _SC_NPROCESSORS_ONLN:  return __sysconf_nprocessors_onln();
-    case _SC_PHYS_PAGES:        return __sysconf_phys_pages();
-    case _SC_AVPHYS_PAGES:      return __sysconf_avphys_pages();
+    case _SC_NPROCESSORS_CONF:  return get_nprocs_conf();
+    case _SC_NPROCESSORS_ONLN:  return get_nprocs();
+    case _SC_PHYS_PAGES:        return get_phys_pages();
+    case _SC_AVPHYS_PAGES:      return get_avphys_pages();
 
     default:
       // Posix says EINVAL is the only error that shall be returned,
diff --git a/libc/bionic/sysinfo.cpp b/libc/bionic/sysinfo.cpp
new file mode 100644
index 0000000..6f0afb8
--- /dev/null
+++ b/libc/bionic/sysinfo.cpp
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2014 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 <sys/sysinfo.h>
+
+#include <dirent.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "private/ScopedReaddir.h"
+
+static bool __matches_cpuN(const char* s) {
+  // The %c trick is to ensure that we have the anchored match "^cpu[0-9]+$".
+  unsigned cpu;
+  char dummy;
+  return (sscanf(s, "cpu%u%c", &cpu, &dummy) == 1);
+}
+
+int get_nprocs_conf() {
+  // On x86 kernels you can use /proc/cpuinfo for this, but on ARM kernels offline CPUs disappear
+  // from there. This method works on both.
+  ScopedReaddir reader("/sys/devices/system/cpu");
+  if (reader.IsBad()) {
+    return 1;
+  }
+
+  int result = 0;
+  dirent* entry;
+  while ((entry = reader.ReadEntry()) != NULL) {
+    if (entry->d_type == DT_DIR && __matches_cpuN(entry->d_name)) {
+      ++result;
+    }
+  }
+  return result;
+}
+
+int get_nprocs() {
+  FILE* fp = fopen("/proc/stat", "re");
+  if (fp == NULL) {
+    return 1;
+  }
+
+  int result = 0;
+  char buf[256];
+  while (fgets(buf, sizeof(buf), fp) != NULL) {
+    // Extract just the first word from the line.
+    // 'cpu0 7976751 1364388 3116842 469770388 8629405 0 49047 0 0 0'
+    char* p = strchr(buf, ' ');
+    if (p != NULL) {
+      *p = 0;
+    }
+    if (__matches_cpuN(buf)) {
+      ++result;
+    }
+  }
+  fclose(fp);
+  return result;
+}
+
+static int __get_meminfo(const char* pattern) {
+  FILE* fp = fopen("/proc/meminfo", "re");
+  if (fp == NULL) {
+    return -1;
+  }
+
+  int result = -1;
+  char buf[256];
+  while (fgets(buf, sizeof(buf), fp) != NULL) {
+    long total;
+    if (sscanf(buf, pattern, &total) == 1) {
+      result = (int) (total / (PAGE_SIZE/1024));
+      break;
+    }
+  }
+  fclose(fp);
+  return result;
+}
+
+long get_phys_pages() {
+  return __get_meminfo("MemTotal: %ld kB");
+}
+
+long get_avphys_pages() {
+  return __get_meminfo("MemFree: %ld kB");
+}
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index a7ec615..c0dac1a 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -146,7 +146,16 @@
 	fpos_t	_offset;	/* current lseek offset */
 } FILE;
 
+/* Legacy BSD implementation of stdin/stdout/stderr. */
 extern FILE __sF[];
+/* More obvious implementation. */
+extern FILE* stdin;
+extern FILE* stdout;
+extern FILE* stderr;
+/* C99 and earlier plus current C++ standards say these must be macros. */
+#define stdin stdin
+#define stdout stdout
+#define stderr stderr
 
 #define	__SLBF	0x0001		/* line buffered */
 #define	__SNBF	0x0002		/* unbuffered */
@@ -209,10 +218,6 @@
 #define	SEEK_END	2	/* set file offset to EOF plus offset */
 #endif
 
-#define	stdin	(&__sF[0])
-#define	stdout	(&__sF[1])
-#define	stderr	(&__sF[2])
-
 /*
  * Functions defined in ANSI C standard.
  */
diff --git a/libc/include/sys/sysconf.h b/libc/include/sys/sysconf.h
index 0a46e7a..3d058d7 100644
--- a/libc/include/sys/sysconf.h
+++ b/libc/include/sys/sysconf.h
@@ -129,7 +129,7 @@
 #define _SC_AVPHYS_PAGES                0x0063
 #define _SC_MONOTONIC_CLOCK             0x0064
 
-extern int sysconf(int name);
+long sysconf(int);
 
 __END_DECLS
 
diff --git a/libc/include/sys/sysinfo.h b/libc/include/sys/sysinfo.h
index c7e46e6..b66bc8e 100644
--- a/libc/include/sys/sysinfo.h
+++ b/libc/include/sys/sysinfo.h
@@ -33,7 +33,15 @@
 
 __BEGIN_DECLS
 
-extern int sysinfo (struct sysinfo *info);
+int sysinfo(struct sysinfo* info);
+
+int get_nprocs_conf(void);
+
+int get_nprocs(void);
+
+long get_phys_pages(void);
+
+long get_avphys_pages(void);
 
 __END_DECLS
 
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index dca9b3d..e94ee66 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -202,7 +202,7 @@
 
 int getpagesize(void);
 
-extern int sysconf(int  name);
+long sysconf(int);
 
 extern int daemon(int, int);
 
diff --git a/libc/stdio/fileext.h b/libc/stdio/fileext.h
index 7efff8f..dc89fff 100644
--- a/libc/stdio/fileext.h
+++ b/libc/stdio/fileext.h
@@ -43,10 +43,6 @@
 	pthread_mutex_t _lock; /* file lock */
 };
 
-__LIBC_HIDDEN__ extern struct __sfileext __sFext[3];
-
-#define _FILEEXT_INITIALIZER  {{NULL,0},{0},PTHREAD_RECURSIVE_MUTEX_INITIALIZER}
-
 #define _EXT(fp) ((struct __sfileext *)((fp)->_ext._base))
 #define _UB(fp) _EXT(fp)->_ub
 #define _FLOCK(fp)  _EXT(fp)->_lock
diff --git a/libc/stdio/findfp.c b/libc/stdio/findfp.c
index ef8e9e6..5e51198 100644
--- a/libc/stdio/findfp.c
+++ b/libc/stdio/findfp.c
@@ -59,12 +59,15 @@
 static struct glue *lastglue = &uglue;
 _THREAD_PRIVATE_MUTEX(__sfp_mutex);
 
-struct __sfileext __sFext[3];
+static struct __sfileext __sFext[3];
 FILE __sF[3] = {
 	std(__SRD, STDIN_FILENO),		/* stdin */
 	std(__SWR, STDOUT_FILENO),		/* stdout */
 	std(__SWR|__SNBF, STDERR_FILENO)	/* stderr */
 };
+FILE* stdin = &__sF[0];
+FILE* stdout = &__sF[1];
+FILE* stderr = &__sF[2];
 struct glue __sglue = { &uglue, 3, __sF };
 
 static struct glue *
diff --git a/tests/Android.mk b/tests/Android.mk
index 2a34ea9..92d7976 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -98,6 +98,7 @@
     sys_stat_test.cpp \
     sys_statvfs_test.cpp \
     sys_syscall_test.cpp \
+    sys_sysinfo_test.cpp \
     sys_time_test.cpp \
     sys_types_test.cpp \
     sys_vfs_test.cpp \
diff --git a/tests/sys_sysinfo_test.cpp b/tests/sys_sysinfo_test.cpp
new file mode 100644
index 0000000..b00e13f
--- /dev/null
+++ b/tests/sys_sysinfo_test.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <sys/sysinfo.h>
+
+TEST(sys_sysinfo, smoke) {
+  int nprocessor = get_nprocs();
+  ASSERT_GT(nprocessor, 0);
+
+  int nprocessor_conf = get_nprocs_conf();
+  ASSERT_GE(nprocessor_conf, nprocessor);
+
+  long avail_phys_pages = get_avphys_pages();
+  ASSERT_GT(avail_phys_pages, 0);
+
+  long phys_pages = get_phys_pages();
+  ASSERT_GE(phys_pages, avail_phys_pages);
+}
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index c9d9943..b5fcf26 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -23,6 +23,7 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <stdint.h>
+#include <sys/param.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
 #include <sys/utsname.h>
@@ -499,3 +500,23 @@
   ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
   ASSERT_EQ(ENAMETOOLONG, errno);
 }
+
+TEST(unistd, pathconf_fpathconf) {
+  TemporaryFile tf;
+  long rc = 0L;
+  // As a file system's block size is always power of 2, the configure values
+  // for ALLOC and XFER should be power of 2 as well.
+  rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN);
+  ASSERT_TRUE(rc > 0 && powerof2(rc));
+  rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE);
+  ASSERT_TRUE(rc > 0 && powerof2(rc));
+  rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN);
+  ASSERT_TRUE(rc > 0 && powerof2(rc));
+
+  rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN);
+  ASSERT_TRUE(rc > 0 && powerof2(rc));
+  rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE);
+  ASSERT_TRUE(rc > 0 && powerof2(rc));
+  rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN);
+  ASSERT_TRUE(rc > 0 && powerof2(rc));
+}