Merge "Fix new clang compiler warning"
diff --git a/libc/Android.bp b/libc/Android.bp
index f1cca49..0950ebd 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -71,6 +71,7 @@
         fuzzer: false,
     },
     native_coverage: false,
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
 
@@ -1402,6 +1403,7 @@
         "libc_defaults",
         "libc_native_allocator_defaults",
     ],
+    ramdisk_available: false,
     srcs: libc_common_src_files + [
         "bionic/heap_tagging.cpp",
         "bionic/malloc_common.cpp",
@@ -1823,6 +1825,7 @@
     ],
     host_supported: true,
     vendor_available: true,
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
     export_include_dirs: [
@@ -1839,6 +1842,7 @@
 
     host_supported: true,
     vendor_available: true,
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
 
@@ -1979,6 +1983,7 @@
     name: "crt_defaults",
     defaults: ["linux_bionic_supported"],
     vendor_available: true,
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
 
diff --git a/libc/async_safe/Android.bp b/libc/async_safe/Android.bp
index c28d53a..7df6ab9 100644
--- a/libc/async_safe/Android.bp
+++ b/libc/async_safe/Android.bp
@@ -22,6 +22,7 @@
 
 cc_library_headers {
     name: "libasync_safe_headers",
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
     defaults: ["linux_bionic_supported"],
diff --git a/libc/bionic/heap_tagging.cpp b/libc/bionic/heap_tagging.cpp
index 2522da5..0eaf34e 100644
--- a/libc/bionic/heap_tagging.cpp
+++ b/libc/bionic/heap_tagging.cpp
@@ -31,6 +31,7 @@
 #include "malloc_tagged_pointers.h"
 
 #include <platform/bionic/malloc.h>
+#include <platform/bionic/mte_kernel.h>
 
 static HeapTaggingLevel heap_tagging_level = M_HEAP_TAGGING_LEVEL_NONE;
 
diff --git a/libc/bionic/ifaddrs.cpp b/libc/bionic/ifaddrs.cpp
index f5b080c..954d43b 100644
--- a/libc/bionic/ifaddrs.cpp
+++ b/libc/bionic/ifaddrs.cpp
@@ -28,6 +28,7 @@
 
 #include <ifaddrs.h>
 
+#include <async_safe/log.h>
 #include <errno.h>
 #include <linux/if_packet.h>
 #include <net/if.h>
@@ -193,23 +194,26 @@
   } else if (hdr->nlmsg_type == RTM_NEWADDR) {
     ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
 
-    // We should already know about this from an RTM_NEWLINK message.
-    const ifaddrs_storage* addr = reinterpret_cast<const ifaddrs_storage*>(*out);
-    while (addr != nullptr && addr->interface_index != static_cast<int>(msg->ifa_index)) {
-      addr = reinterpret_cast<const ifaddrs_storage*>(addr->ifa.ifa_next);
+    // We might already know about this interface from an RTM_NEWLINK message.
+    const ifaddrs_storage* known_addr = reinterpret_cast<const ifaddrs_storage*>(*out);
+    while (known_addr != nullptr && known_addr->interface_index != static_cast<int>(msg->ifa_index)) {
+      known_addr = reinterpret_cast<const ifaddrs_storage*>(known_addr->ifa.ifa_next);
     }
-    // If this is an unknown interface, ignore whatever we're being told about it.
-    if (addr == nullptr) return;
 
-    // Create a new ifaddr entry and copy what we already know.
+    // Create a new ifaddr entry, and set the interface index.
     ifaddrs_storage* new_addr = new ifaddrs_storage(out);
-    // We can just copy the name rather than look for IFA_LABEL.
-    strcpy(new_addr->name, addr->name);
-    new_addr->ifa.ifa_name = new_addr->name;
-    new_addr->ifa.ifa_flags = addr->ifa.ifa_flags;
-    new_addr->interface_index = addr->interface_index;
+    new_addr->interface_index = static_cast<int>(msg->ifa_index);
 
-    // Go through the various bits of information and find the address
+    // If this is a known interface, copy what we already know.
+    if (known_addr != nullptr) {
+      strcpy(new_addr->name, known_addr->name);
+      new_addr->ifa.ifa_name = new_addr->name;
+      new_addr->ifa.ifa_flags = known_addr->ifa.ifa_flags;
+    } else {
+      new_addr->ifa.ifa_flags = msg->ifa_flags;
+    }
+
+    // Go through the various bits of information and find the name, address
     // and any broadcast/destination address.
     rtattr* rta = IFA_RTA(msg);
     size_t rta_len = IFA_PAYLOAD(hdr);
@@ -222,32 +226,71 @@
       } else if (rta->rta_type == IFA_BROADCAST) {
         if (msg->ifa_family == AF_INET) {
           new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
+          if (known_addr == nullptr) {
+            // We did not read the broadcast flag from an RTM_NEWLINK message.
+            // Ensure that it is set.
+            new_addr->ifa.ifa_flags |= IFF_BROADCAST;
+          }
         }
       } else if (rta->rta_type == IFA_LOCAL) {
         if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
           new_addr->SetLocalAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
         }
+      } else if (rta->rta_type == IFA_LABEL) {
+        if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {
+          memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));
+          new_addr->ifa.ifa_name = new_addr->name;
+        }
       }
       rta = RTA_NEXT(rta, rta_len);
     }
   }
 }
 
+static void 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;
+    if (strlen(addr->name) == 0) {
+      if (prev_addr == nullptr) {
+        *list = next_addr;
+      } else {
+        prev_addr->ifa.ifa_next = next_addr;
+      }
+      free(addr);
+    } else {
+      prev_addr = addr;
+    }
+    addr = reinterpret_cast<ifaddrs_storage*>(next_addr);
+  }
+}
+
 int getifaddrs(ifaddrs** out) {
   // We construct the result directly into `out`, so terminate the list.
   *out = nullptr;
 
   // Open the netlink socket and ask for all the links and addresses.
   NetlinkConnection nc;
-  bool okay = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__getifaddrs_callback, out) &&
-              nc.SendRequest(RTM_GETADDR) && nc.ReadResponses(__getifaddrs_callback, out);
-  if (!okay) {
+  bool getlink_success =
+    nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__getifaddrs_callback, out);
+  bool getaddr_success =
+    nc.SendRequest(RTM_GETADDR) && nc.ReadResponses(__getifaddrs_callback, out);
+
+  if (!getaddr_success) {
     freeifaddrs(*out);
     // Ensure that callers crash if they forget to check for success.
     *out = nullptr;
     return -1;
   }
 
+  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);
+  }
+
   return 0;
 }
 
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index 070c45f..e4106e9 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -43,7 +43,6 @@
 #include <unistd.h>
 
 #include <async_safe/log.h>
-#include <platform/bionic/mte_kernel.h>
 
 #include "private/WriteProtected.h"
 #include "private/bionic_defs.h"
diff --git a/libdl/Android.bp b/libdl/Android.bp
index 9daa9c4..59f1937 100644
--- a/libdl/Android.bp
+++ b/libdl/Android.bp
@@ -4,6 +4,7 @@
 cc_library_static {
     name: "libdl_static",
     defaults: ["linux_bionic_supported"],
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
 
@@ -32,6 +33,7 @@
 
 cc_library {
     name: "libdl",
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
     static_ndk_lib: true,
@@ -122,6 +124,7 @@
     name: "libdl_android",
 
     defaults: ["linux_bionic_supported"],
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,
 
diff --git a/libm/Android.bp b/libm/Android.bp
index 801129a..d4cdd7b 100644
--- a/libm/Android.bp
+++ b/libm/Android.bp
@@ -6,6 +6,7 @@
 cc_library {
     name: "libm",
     defaults: ["linux_bionic_supported"],
+    ramdisk_available: true,
     recovery_available: true,
     static_ndk_lib: true,
 
diff --git a/linker/Android.bp b/linker/Android.bp
index acdf094..e21ee60 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -425,6 +425,7 @@
 
     name: "ld-android",
     defaults: ["linux_bionic_supported", "linker_version_script_overlay"],
+    ramdisk_available: true,
     recovery_available: true,
     native_bridge_supported: true,