Merge "Add a libc wrapper for statx(2)."
diff --git a/benchmarks/Android.bp b/benchmarks/Android.bp
index b4176de..5f8c113 100644
--- a/benchmarks/Android.bp
+++ b/benchmarks/Android.bp
@@ -34,6 +34,7 @@
         "inttypes_benchmark.cpp",
         "malloc_benchmark.cpp",
         "malloc_sql_benchmark.cpp",
+        "malloc_map_benchmark.cpp",
         "math_benchmark.cpp",
         "property_benchmark.cpp",
         "pthread_benchmark.cpp",
@@ -52,6 +53,15 @@
         "libtinyxml2",
     ],
     stl: "libc++_static",
+
+    target: {
+        android: {
+            static_libs: [
+                "libmeminfo",
+                "libprocinfo",
+            ],
+        },
+    },
 }
 
 cc_defaults {
diff --git a/benchmarks/malloc_map_benchmark.cpp b/benchmarks/malloc_map_benchmark.cpp
new file mode 100644
index 0000000..ba4d62c
--- /dev/null
+++ b/benchmarks/malloc_map_benchmark.cpp
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2020 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 <err.h>
+#include <malloc.h>
+#include <stdint.h>
+
+#include <map>
+#include <unordered_map>
+#include <vector>
+
+#include <benchmark/benchmark.h>
+#include "util.h"
+
+#include <android-base/strings.h>
+
+#if defined(__BIONIC__)
+
+#include <meminfo/procmeminfo.h>
+#include <procinfo/process_map.h>
+
+static void Gather(uint64_t* rss_bytes) {
+  android::meminfo::ProcMemInfo proc_mem(getpid());
+  const std::vector<android::meminfo::Vma>& maps = proc_mem.MapsWithoutUsageStats();
+  for (auto& vma : maps) {
+    if (vma.name == "[anon:libc_malloc]" || android::base::StartsWith(vma.name, "[anon:scudo:") ||
+        android::base::StartsWith(vma.name, "[anon:GWP-ASan")) {
+      android::meminfo::Vma update_vma(vma);
+      if (!proc_mem.FillInVmaStats(update_vma)) {
+        err(1, "FillInVmaStats failed\n");
+      }
+      *rss_bytes += update_vma.usage.rss;
+    }
+  }
+}
+#endif
+
+template <typename MapType>
+static void MapBenchmark(benchmark::State& state, size_t num_elements) {
+#if defined(__BIONIC__)
+  uint64_t rss_bytes = 0;
+#endif
+
+  for (auto _ : state) {
+#if defined(__BIONIC__)
+    state.PauseTiming();
+    mallopt(M_PURGE, 0);
+    uint64_t rss_bytes_before = 0;
+    Gather(&rss_bytes_before);
+    state.ResumeTiming();
+#endif
+    MapType map;
+    for (size_t i = 0; i < num_elements; i++) {
+      map[i][0] = 0;
+    }
+#if defined(__BIONIC__)
+    state.PauseTiming();
+    mallopt(M_PURGE, 0);
+    Gather(&rss_bytes);
+    // Try and record only the memory used in the map.
+    rss_bytes -= rss_bytes_before;
+    state.ResumeTiming();
+#endif
+  }
+
+#if defined(__BIONIC__)
+  double rss_mb = (rss_bytes / static_cast<double>(state.iterations())) / 1024.0 / 1024.0;
+  state.counters["RSS_MB"] = rss_mb;
+#endif
+}
+
+static void BM_std_map_8(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[8]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_map_8);
+
+static void BM_std_map_16(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[16]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_map_16);
+
+static void BM_std_map_32(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[32]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_map_32);
+
+static void BM_std_map_64(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[64]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_map_64);
+
+static void BM_std_map_96(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[96]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_map_96);
+
+static void BM_std_map_128(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[128]>>(state, 500000);
+}
+BIONIC_BENCHMARK(BM_std_map_128);
+
+static void BM_std_map_256(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[256]>>(state, 500000);
+}
+BIONIC_BENCHMARK(BM_std_map_256);
+
+static void BM_std_map_512(benchmark::State& state) {
+  MapBenchmark<std::map<uint64_t, char[512]>>(state, 500000);
+}
+BIONIC_BENCHMARK(BM_std_map_512);
+
+static void BM_std_unordered_map_8(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[8]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_8);
+
+static void BM_std_unordered_map_16(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[16]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_16);
+
+static void BM_std_unordered_map_32(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[32]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_32);
+
+static void BM_std_unordered_map_64(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[64]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_64);
+
+static void BM_std_unordered_map_96(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[96]>>(state, 1000000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_96);
+
+static void BM_std_unordered_map_128(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[128]>>(state, 500000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_128);
+
+static void BM_std_unordered_map_256(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[256]>>(state, 500000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_256);
+
+static void BM_std_unordered_map_512(benchmark::State& state) {
+  MapBenchmark<std::unordered_map<uint64_t, char[512]>>(state, 500000);
+}
+BIONIC_BENCHMARK(BM_std_unordered_map_512);
diff --git a/libc/bionic/ifaddrs.cpp b/libc/bionic/ifaddrs.cpp
index e21ec40..0e9b544 100644
--- a/libc/bionic/ifaddrs.cpp
+++ b/libc/bionic/ifaddrs.cpp
@@ -248,11 +248,20 @@
   }
 }
 
-static void remove_nameless_interfaces(ifaddrs** list) {
+static void resolve_or_remove_nameless_interfaces(ifaddrs** list) {
   ifaddrs_storage* addr = reinterpret_cast<ifaddrs_storage*>(*list);
   ifaddrs_storage* prev_addr = nullptr;
   while (addr != nullptr) {
     ifaddrs* next_addr = addr->ifa.ifa_next;
+
+    // Try resolving interfaces without a name first.
+    if (strlen(addr->name) == 0) {
+      if (if_indextoname(addr->interface_index, addr->name) != nullptr) {
+        addr->ifa.ifa_name = addr->name;
+      }
+    }
+
+    // If the interface could not be resolved, remove it.
     if (strlen(addr->name) == 0) {
       if (prev_addr == nullptr) {
         *list = next_addr;
@@ -296,8 +305,8 @@
   if (!getlink_success) {
     async_safe_format_log(ANDROID_LOG_INFO, "ifaddrs", "Failed to send RTM_GETLINK request");
     // If we weren't able to depend on GETLINK messages, it's possible some
-    // interfaces never got their name set. Remove those.
-    remove_nameless_interfaces(out);
+    // interfaces never got their name set. Resolve them using if_indextoname or remove them.
+    resolve_or_remove_nameless_interfaces(out);
   }
 
   return 0;
diff --git a/libc/include/bits/auxvec.h b/libc/include/bits/auxvec.h
index 4d39477..2d6522a 100644
--- a/libc/include/bits/auxvec.h
+++ b/libc/include/bits/auxvec.h
@@ -37,6 +37,14 @@
 
 #include <linux/auxvec.h>
 
+// AT_HWCAP isn't useful without these constants.
+#if __has_include(<asm/hwcap.h>)
+#include <asm/hwcap.h>
+#endif
+#if __has_include(<asm/hwcap2.h>)
+#include <asm/hwcap2.h>
+#endif
+
 /** Historical SuperH cruft. Irrelevant on Android. */
 #define AT_FPUCW 18
 /** Historical PowerPC cruft. Irrelevant on Android. */
diff --git a/libc/include/sys/auxv.h b/libc/include/sys/auxv.h
index c651940..bf70dda 100644
--- a/libc/include/sys/auxv.h
+++ b/libc/include/sys/auxv.h
@@ -40,7 +40,7 @@
 __BEGIN_DECLS
 
 /**
- * [getauxval(3)](http://man7.org/linux/man-pages/man2/personality.2.html) returns values from
+ * [getauxval(3)](http://man7.org/linux/man-pages/man3/getauxval.3.html) returns values from
  * the ELF auxiliary vector passed by the kernel.
  *
  * Returns the corresponding value on success,
diff --git a/tests/Android.bp b/tests/Android.bp
index 4bd96ad..d1afd51 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -166,6 +166,7 @@
         "strings_nofortify_test.cpp",
         "strings_test.cpp",
         "sstream_test.cpp",
+        "sys_auxv_test.cpp",
         "sys_epoll_test.cpp",
         "sys_mman_test.cpp",
         "sys_msg_test.cpp",
diff --git a/tests/sys_auxv_test.cpp b/tests/sys_auxv_test.cpp
new file mode 100644
index 0000000..afd62ea
--- /dev/null
+++ b/tests/sys_auxv_test.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2020 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 <gtest/gtest.h>
+
+#include <sys/auxv.h>
+
+TEST(sys_auxv, getauxval_HWCAP) {
+  __attribute__((__unused__)) unsigned long hwcap = getauxval(AT_HWCAP);
+
+  // Check that the constants for *using* AT_HWCAP are also available.
+#if defined(__arm__)
+  ASSERT_NE(0, HWCAP_THUMB);
+#elif defined(__aarch64__)
+  ASSERT_NE(0, HWCAP_FP);
+#endif
+}
+
+TEST(sys_auxv, getauxval_HWCAP2) {
+#if defined(AT_HWCAP2)
+  __attribute__((__unused__)) unsigned long hwcap = getauxval(AT_HWCAP2);
+
+  // Check that the constants for *using* AT_HWCAP2 are also available.
+#if defined(__arm__)
+  ASSERT_NE(0, HWCAP2_AES);
+#elif defined(__aarch64__)
+  ASSERT_NE(0, HWCAP2_SVE2);
+#endif
+#else
+  GTEST_SKIP() << "No AT_HWCAP2 for this architecture.";
+#endif
+}