Merge "Fix recvfrom prototype."
diff --git a/libc/bionic/netinet_in.cpp b/libc/bionic/netinet_in.cpp
index dfa5d8d..2a7090a 100644
--- a/libc/bionic/netinet_in.cpp
+++ b/libc/bionic/netinet_in.cpp
@@ -29,6 +29,7 @@
 #include <netinet/in.h>
 
 #include <errno.h>
+#include <netdb.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <string.h>
diff --git a/libc/bionic/pthread_barrier.cpp b/libc/bionic/pthread_barrier.cpp
index 1bcd12a..1618222 100644
--- a/libc/bionic/pthread_barrier.cpp
+++ b/libc/bionic/pthread_barrier.cpp
@@ -42,7 +42,7 @@
   return 0;
 }
 
-int pthread_barrierattr_getpshared(pthread_barrierattr_t* attr, int* pshared) {
+int pthread_barrierattr_getpshared(const pthread_barrierattr_t* attr, int* pshared) {
   *pshared = (*attr & 1) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE;
   return 0;
 }
diff --git a/libc/bionic/sysinfo.cpp b/libc/bionic/sysinfo.cpp
index 1cb5c79..304634a 100644
--- a/libc/bionic/sysinfo.cpp
+++ b/libc/bionic/sysinfo.cpp
@@ -76,29 +76,14 @@
   return cpu_count;
 }
 
-static int __get_meminfo_page_count(const char* pattern) {
-  FILE* fp = fopen("/proc/meminfo", "re");
-  if (fp == NULL) {
-    return -1;
-  }
-
-  int page_count = -1;
-  char buf[256];
-  while (fgets(buf, sizeof(buf), fp) != NULL) {
-    long total;
-    if (sscanf(buf, pattern, &total) == 1) {
-      page_count = static_cast<int>(total / (sysconf(_SC_PAGE_SIZE) / 1024));
-      break;
-    }
-  }
-  fclose(fp);
-  return page_count;
-}
-
 long get_phys_pages() {
-  return __get_meminfo_page_count("MemTotal: %ld kB");
+  struct sysinfo si;
+  sysinfo(&si);
+  return (si.totalram * si.mem_unit) / sysconf(_SC_PAGE_SIZE);
 }
 
 long get_avphys_pages() {
-  return __get_meminfo_page_count("MemFree: %ld kB");
+  struct sysinfo si;
+  sysinfo(&si);
+  return ((si.freeram + si.bufferram) * si.mem_unit) / sysconf(_SC_PAGE_SIZE);
 }
diff --git a/libc/include/bits/seek_constants.h b/libc/include/bits/seek_constants.h
new file mode 100644
index 0000000..34c5866
--- /dev/null
+++ b/libc/include/bits/seek_constants.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#ifndef _BITS_SEEK_CONSTANTS_H_
+#define _BITS_SEEK_CONSTANTS_H_
+
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+
+#endif
diff --git a/libc/include/cpio.h b/libc/include/cpio.h
new file mode 100644
index 0000000..ceeeb87
--- /dev/null
+++ b/libc/include/cpio.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#ifndef _CPIO_H_
+#define _CPIO_H_
+
+#include <sys/cdefs.h>
+
+#define C_IRUSR 0000400
+#define C_IWUSR 0000200
+#define C_IXUSR 0000100
+#define C_IRGRP 0000040
+#define C_IWGRP 0000020
+#define C_IXGRP 0000010
+#define C_IROTH 0000004
+#define C_IWOTH 0000002
+#define C_IXOTH 0000001
+#define C_ISUID 0004000
+#define C_ISGID 0002000
+#define C_ISVTX 0001000
+#define C_ISDIR 0040000
+#define C_ISFIFO 0010000
+#define C_ISREG 0100000
+#define C_ISBLK 0060000
+#define C_ISCHR 0020000
+#define C_ISCTG 0110000
+#define C_ISLNK 0120000
+#define C_ISSOCK 0140000
+
+#define MAGIC "070707"
+
+#endif /* _CPIO_H_ */
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index 872a727..c49dbd0 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -37,6 +37,7 @@
 #include <linux/uio.h>
 
 #include <bits/fcntl.h>
+#include <bits/seek_constants.h>
 
 #if defined(__USE_GNU) || defined(__USE_BSD)
 #include <bits/lockf.h>
diff --git a/libc/include/netdb.h b/libc/include/netdb.h
index 7fcb11f..95f0986 100644
--- a/libc/include/netdb.h
+++ b/libc/include/netdb.h
@@ -192,6 +192,8 @@
  */
 #define	SCOPE_DELIMITER	'%'
 
+#define IPPORT_RESERVED 1024
+
 __BEGIN_DECLS
 
 /* BIONIC-BEGIN */
diff --git a/libc/include/netinet/in.h b/libc/include/netinet/in.h
index b524b05..00b5cf9 100644
--- a/libc/include/netinet/in.h
+++ b/libc/include/netinet/in.h
@@ -40,8 +40,6 @@
 
 __BEGIN_DECLS
 
-#define IPPORT_RESERVED  1024
-
 #define INET_ADDRSTRLEN 16
 
 typedef uint16_t in_port_t;
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 1153695..47dc153 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -232,8 +232,8 @@
 
 int pthread_barrierattr_init(pthread_barrierattr_t* _Nonnull attr) __INTRODUCED_IN(24);
 int pthread_barrierattr_destroy(pthread_barrierattr_t* _Nonnull attr) __INTRODUCED_IN(24);
-int pthread_barrierattr_getpshared(pthread_barrierattr_t* _Nonnull attr, int* _Nonnull pshared)
-  __INTRODUCED_IN(24);
+int pthread_barrierattr_getpshared(const pthread_barrierattr_t* _Nonnull attr,
+                                   int* _Nonnull pshared) __INTRODUCED_IN(24);
 int pthread_barrierattr_setpshared(pthread_barrierattr_t* _Nonnull attr, int pshared)
   __INTRODUCED_IN(24);
 
diff --git a/libc/include/semaphore.h b/libc/include/semaphore.h
index 4ef13af..218f22a 100644
--- a/libc/include/semaphore.h
+++ b/libc/include/semaphore.h
@@ -42,7 +42,7 @@
 #endif
 } sem_t;
 
-#define SEM_FAILED NULL
+#define SEM_FAILED __BIONIC_CAST(reinterpret_cast, sem_t*, 0)
 
 int sem_destroy(sem_t*);
 int sem_getvalue(sem_t*, int*);
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 097be3a..38021ef 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -47,6 +47,8 @@
 #define __need_NULL
 #include <stddef.h>
 
+#include <bits/seek_constants.h>
+
 __BEGIN_DECLS
 
 #if defined(__clang__)
@@ -106,10 +108,6 @@
 #define	L_tmpnam	1024	/* XXX must be == PATH_MAX */
 #define	TMP_MAX		308915776
 
-#define SEEK_SET 0
-#define SEEK_CUR 1
-#define SEEK_END 2
-
 /*
  * Functions defined in ANSI C standard.
  */
diff --git a/libc/include/tar.h b/libc/include/tar.h
new file mode 100644
index 0000000..a5d7a36
--- /dev/null
+++ b/libc/include/tar.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#ifndef _TAR_H_
+#define _TAR_H_
+
+#include <sys/cdefs.h>
+
+#define TMAGIC "ustar"
+#define TMAGLEN 6
+#define TVERSION "00"
+#define TVERSLEN 2
+
+#define REGTYPE '0'
+#define AREGTYPE '\0'
+#define LNKTYPE '1'
+#define SYMTYPE '2'
+#define CHRTYPE '3'
+#define BLKTYPE '4'
+#define DIRTYPE '5'
+#define FIFOTYPE '6'
+#define CONTTYPE '7'
+
+#define TSUID 04000
+#define TSGID 02000
+#define TSVTX 01000
+#define TUREAD 00400
+#define TUWRITE 00200
+#define TUEXEC 00100
+#define TGREAD 00040
+#define TGWRITE 00020
+#define TGEXEC 00010
+#define TOREAD 00004
+#define TOWRITE 00002
+#define TOEXEC 00001
+
+#endif /* _TAR_H_ */
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 86949de..ac7d4c2 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -39,6 +39,7 @@
 #include <bits/ioctl.h>
 #include <bits/lockf.h>
 #include <bits/posix_limits.h>
+#include <bits/seek_constants.h>
 #include <bits/sysconf.h>
 
 __BEGIN_DECLS
@@ -52,10 +53,6 @@
 #define W_OK 2
 #define R_OK 4
 
-#define SEEK_SET 0
-#define SEEK_CUR 1
-#define SEEK_END 2
-
 #define _PC_FILESIZEBITS 0
 #define _PC_LINK_MAX 1
 #define _PC_MAX_CANON 2
diff --git a/linker/linker_phdr.cpp b/linker/linker_phdr.cpp
index c41f3a0..bced722 100644
--- a/linker/linker_phdr.cpp
+++ b/linker/linker_phdr.cpp
@@ -604,9 +604,16 @@
     }
 
     if (file_length != 0) {
+      int prot = PFLAGS_TO_PROT(phdr->p_flags);
+      // W + E PT_LOAD segments are not allowed.
+      if ((prot & (PROT_EXEC | PROT_WRITE)) == (PROT_EXEC | PROT_WRITE)) {
+        DL_ERR_AND_LOG("\"%s\": W + E load segments are not allowed", name_.c_str());
+        return false;
+      }
+
       void* seg_addr = mmap64(reinterpret_cast<void*>(seg_page_start),
                             file_length,
-                            PFLAGS_TO_PROT(phdr->p_flags),
+                            prot,
                             MAP_FIXED|MAP_PRIVATE,
                             fd_,
                             file_offset_ + file_page_start);
diff --git a/tests/Android.mk b/tests/Android.mk
index 0da3b88..b329831 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -16,6 +16,20 @@
 
 LOCAL_PATH := $(call my-dir)
 
+# Move prebuilt test elf-files to $(TARGET_OUT_NATIVE_TESTS)
+include $(CLEAR_VARS)
+LOCAL_MULTILIB := both
+LOCAL_MODULE := libtest_invalid-rw_load_segment.so
+LOCAL_MODULE_PATH_32 := $($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_DATA_NATIVE_TESTS)/prebuilt-elf-files
+LOCAL_MODULE_PATH_64 := $(TARGET_OUT_DATA_NATIVE_TESTS)/prebuilt-elf-files
+LOCAL_MODULE_CLASS := SHARED_LIBRARY
+
+LOCAL_SRC_FILES_arm := prebuilt-elf-files/arm/$(LOCAL_MODULE)
+LOCAL_SRC_FILES_arm64 := prebuilt-elf-files/arm64/$(LOCAL_MODULE)
+LOCAL_SRC_FILES_x86 := prebuilt-elf-files/x86/$(LOCAL_MODULE)
+LOCAL_SRC_FILES_x86_64 := prebuilt-elf-files/x86_64/$(LOCAL_MODULE)
+include $(BUILD_PREBUILT)
+
 ifeq ($(HOST_OS)-$(HOST_ARCH),$(filter $(HOST_OS)-$(HOST_ARCH),linux-x86 linux-x86_64))
 build_host := true
 else
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 5bf5861..a1c5801 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -1142,3 +1142,23 @@
 
   dlclose(handle);
 }
+
+// Bionic specific tests
+#if defined(__BIONIC__)
+
+#if defined(__LP64__)
+#define NATIVE_TESTS_PATH "/nativetest64"
+#else
+#define NATIVE_TESTS_PATH "/nativetest"
+#endif
+
+#define PREBUILT_ELF_PATH NATIVE_TESTS_PATH "/prebuilt-elf-files"
+
+TEST(dlfcn, dlopen_invalid_rw_load_segment) {
+  std::string libpath = std::string(getenv("ANDROID_DATA")) + PREBUILT_ELF_PATH + "/libtest_invalid-rw_load_segment.so";
+  void* handle = dlopen(libpath.c_str(), RTLD_NOW);
+  ASSERT_TRUE(handle == nullptr);
+  std::string expected_dlerror = std::string("dlopen failed: \"") + libpath + "\": W + E load segments are not allowed";
+  ASSERT_STREQ(expected_dlerror.c_str(), dlerror());
+}
+#endif
diff --git a/tests/prebuilt-elf-files/arm/libtest_invalid-rw_load_segment.so b/tests/prebuilt-elf-files/arm/libtest_invalid-rw_load_segment.so
new file mode 100755
index 0000000..00d91ff
--- /dev/null
+++ b/tests/prebuilt-elf-files/arm/libtest_invalid-rw_load_segment.so
Binary files differ
diff --git a/tests/prebuilt-elf-files/arm64/libtest_invalid-rw_load_segment.so b/tests/prebuilt-elf-files/arm64/libtest_invalid-rw_load_segment.so
new file mode 100755
index 0000000..f61d2ed
--- /dev/null
+++ b/tests/prebuilt-elf-files/arm64/libtest_invalid-rw_load_segment.so
Binary files differ
diff --git a/tests/prebuilt-elf-files/x86/libtest_invalid-rw_load_segment.so b/tests/prebuilt-elf-files/x86/libtest_invalid-rw_load_segment.so
new file mode 100755
index 0000000..7a343c9
--- /dev/null
+++ b/tests/prebuilt-elf-files/x86/libtest_invalid-rw_load_segment.so
Binary files differ
diff --git a/tests/prebuilt-elf-files/x86_64/libtest_invalid-rw_load_segment.so b/tests/prebuilt-elf-files/x86_64/libtest_invalid-rw_load_segment.so
new file mode 100755
index 0000000..b39d0ca
--- /dev/null
+++ b/tests/prebuilt-elf-files/x86_64/libtest_invalid-rw_load_segment.so
Binary files differ
diff --git a/tests/sys_sysinfo_test.cpp b/tests/sys_sysinfo_test.cpp
index b00e13f..d7d0f6e 100644
--- a/tests/sys_sysinfo_test.cpp
+++ b/tests/sys_sysinfo_test.cpp
@@ -17,17 +17,28 @@
 #include <gtest/gtest.h>
 
 #include <sys/sysinfo.h>
+#include <unistd.h>
 
 TEST(sys_sysinfo, smoke) {
-  int nprocessor = get_nprocs();
-  ASSERT_GT(nprocessor, 0);
+  int nprocs = get_nprocs();
+  ASSERT_GT(nprocs, 0);
+  ASSERT_EQ(sysconf(_SC_NPROCESSORS_ONLN), nprocs);
 
-  int nprocessor_conf = get_nprocs_conf();
-  ASSERT_GE(nprocessor_conf, nprocessor);
+  int nprocs_conf = get_nprocs_conf();
+  ASSERT_GE(nprocs_conf, nprocs);
+  ASSERT_EQ(sysconf(_SC_NPROCESSORS_CONF), nprocs_conf);
 
   long avail_phys_pages = get_avphys_pages();
   ASSERT_GT(avail_phys_pages, 0);
+  ASSERT_EQ(sysconf(_SC_AVPHYS_PAGES), avail_phys_pages);
 
   long phys_pages = get_phys_pages();
   ASSERT_GE(phys_pages, avail_phys_pages);
+  ASSERT_EQ(sysconf(_SC_PHYS_PAGES), phys_pages);
+}
+
+TEST(sys_sysinfo, sysinfo) {
+  struct sysinfo si;
+  memset(&si, 0, sizeof(si));
+  ASSERT_EQ(0, sysinfo(&si));
 }