Merge "Fix cfi directives for memmove/strlcpy."
diff --git a/libc/bionic/ifaddrs.cpp b/libc/bionic/ifaddrs.cpp
index d03d76f..1fb16d4 100644
--- a/libc/bionic/ifaddrs.cpp
+++ b/libc/bionic/ifaddrs.cpp
@@ -67,20 +67,12 @@
     *list = reinterpret_cast<ifaddrs*>(this);
   }
 
-  // Netlink gives us the address family in the header, and the
-  // sockaddr_in or sockaddr_in6 bytes as the payload. We need to
-  // stitch the two bits together into the sockaddr that's part of
-  // our portable interface.
   void SetAddress(int family, const void* data, size_t byteCount) {
-      addr.ss_family = family;
-      memcpy(SockaddrBytes(family, &addr), data, byteCount);
-      ifa.ifa_addr = reinterpret_cast<sockaddr*>(&addr);
+      ifa.ifa_addr = CopyAddress(family, data, byteCount, &addr);
   }
 
   void SetBroadcastAddress(int family, const void* data, size_t byteCount) {
-      ifa_ifu.ss_family = family;
-      memcpy(SockaddrBytes(family, &ifa_ifu), data, byteCount);
-      ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(&ifa_ifu);
+      ifa.ifa_dstaddr = CopyAddress(family, data, byteCount, &ifa_ifu);
   }
 
   // Netlink gives us the prefix length as a bit count. We need to turn
@@ -104,6 +96,22 @@
   }
 
  private:
+  sockaddr* CopyAddress(int family, const void* data, size_t byteCount, sockaddr_storage* ss) {
+      // Netlink gives us the address family in the header, and the
+      // sockaddr_in or sockaddr_in6 bytes as the payload. We need to
+      // stitch the two bits together into the sockaddr that's part of
+      // our portable interface.
+      ss->ss_family = family;
+      memcpy(SockaddrBytes(family, ss), data, byteCount);
+
+      // For IPv6 we might also have to set the scope id.
+      if (family == AF_INET6 && (IN6_IS_ADDR_LINKLOCAL(data) || IN6_IS_ADDR_MC_LINKLOCAL(data))) {
+          reinterpret_cast<sockaddr_in6*>(ss)->sin6_scope_id = interface_index;
+      }
+
+      return reinterpret_cast<sockaddr*>(ss);
+  }
+
   // Returns a pointer to the first byte in the address data (which is
   // stored in network byte order).
   uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) {
diff --git a/libc/include/netinet/in6.h b/libc/include/netinet/in6.h
index 7f3286a..879ac75 100644
--- a/libc/include/netinet/in6.h
+++ b/libc/include/netinet/in6.h
@@ -25,48 +25,47 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _NETINET_IN6_H
 #define _NETINET_IN6_H
 
 #include <linux/in6.h>
 
-#define IN6_IS_ADDR_UNSPECIFIED(a)	\
-	((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) == 0))
+#define IN6_IS_ADDR_UNSPECIFIED(a) \
+  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[8]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[12]) == 0))
 
-#define IN6_IS_ADDR_LOOPBACK(a)		\
-	((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) == ntohl(1)))
+#define IN6_IS_ADDR_LOOPBACK(a) \
+  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[8]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[12]) == ntohl(1)))
 
-#define IN6_IS_ADDR_V4COMPAT(a)		\
-	((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) != 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[12]) != ntohl(1)))
+#define IN6_IS_ADDR_V4COMPAT(a) \
+  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[8]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[12]) != 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[12]) != ntohl(1)))
 
-#define IN6_IS_ADDR_V4MAPPED(a)		      \
-	((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
-	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)))
+#define IN6_IS_ADDR_V4MAPPED(a) \
+  ((*(const uint32_t*)(&(a)->s6_addr[0]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[4]) == 0) && \
+   (*(const uint32_t*)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)))
 
-#define IN6_IS_ADDR_LINKLOCAL(a)	\
-	(((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0x80))
+#define __bionic_s6_addr(a) ((const uint8_t*)(a))
 
-#define IN6_IS_ADDR_SITELOCAL(a)	\
-	(((a)->s6_addr[0] == 0xfe) && (((a)->s6_addr[1] & 0xc0) == 0xc0))
+#define IN6_IS_ADDR_LINKLOCAL(a) \
+  ((__bionic_s6_addr(a)[0] == 0xfe) && ((__bionic_s6_addr(a)[1] & 0xc0) == 0x80))
 
-/* RFC 4193. */
-#define IN6_IS_ADDR_ULA(a)	\
-	(((a)->s6_addr[0] & 0xfe) == 0xfc)
+#define IN6_IS_ADDR_SITELOCAL(a) \
+  ((__bionic_s6_addr(a)[0] == 0xfe) && ((__bionic_s6_addr(a)[1] & 0xc0) == 0xc0))
 
-#define IN6_IS_ADDR_MULTICAST(a)	\
-	(((__const uint8_t *) (a))[0] == 0xff)
+#define IN6_IS_ADDR_MULTICAST(a) (__bionic_s6_addr(a)[0] == 0xff)
 
+#define IN6_IS_ADDR_ULA(a) ((__bionic_s6_addr(a)[0] & 0xfe) == 0xfc)
 
 #define IPV6_ADDR_SCOPE_NODELOCAL       0x01
 #define IPV6_ADDR_SCOPE_INTFACELOCAL    0x01
@@ -75,27 +74,21 @@
 #define IPV6_ADDR_SCOPE_ORGLOCAL        0x08
 #define IPV6_ADDR_SCOPE_GLOBAL          0x0e
 
-#define IPV6_ADDR_MC_SCOPE(a)	\
-	((a)->s6_addr[1] & 0x0f)
+#define IPV6_ADDR_MC_SCOPE(a) (__bionic_s6_addr(a)[1] & 0x0f)
 
-#define IN6_IS_ADDR_MC_NODELOCAL(a)     \
-	(IN6_IS_ADDR_MULTICAST(a) &&  \
-	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_NODELOCAL))
-#define IN6_IS_ADDR_MC_LINKLOCAL(a)	\
-	(IN6_IS_ADDR_MULTICAST(a) &&  \
-	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_LINKLOCAL))
-#define IN6_IS_ADDR_MC_SITELOCAL(a)     \
-	(IN6_IS_ADDR_MULTICAST(a) &&  \
-	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_SITELOCAL))
-#define IN6_IS_ADDR_MC_ORGLOCAL(a)     \
-	(IN6_IS_ADDR_MULTICAST(a) &&  \
-	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_ORGLOCAL))
-#define IN6_IS_ADDR_MC_GLOBAL(a)       \
-	(IN6_IS_ADDR_MULTICAST(a) &&  \
-	 (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_GLOBAL))
+#define IN6_IS_ADDR_MC_NODELOCAL(a) \
+  (IN6_IS_ADDR_MULTICAST(a) && (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_NODELOCAL))
+#define IN6_IS_ADDR_MC_LINKLOCAL(a) \
+  (IN6_IS_ADDR_MULTICAST(a) && (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_LINKLOCAL))
+#define IN6_IS_ADDR_MC_SITELOCAL(a) \
+  (IN6_IS_ADDR_MULTICAST(a) && (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_SITELOCAL))
+#define IN6_IS_ADDR_MC_ORGLOCAL(a) \
+  (IN6_IS_ADDR_MULTICAST(a) && (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_ORGLOCAL))
+#define IN6_IS_ADDR_MC_GLOBAL(a) \
+  (IN6_IS_ADDR_MULTICAST(a) && (IPV6_ADDR_MC_SCOPE(a) == IPV6_ADDR_SCOPE_GLOBAL))
 
-#define IN6_ARE_ADDR_EQUAL(a, b)			\
-    (memcmp(&(a)->s6_addr[0], &(b)->s6_addr[0], sizeof(struct in6_addr)) == 0)
+#define IN6_ARE_ADDR_EQUAL(a, b) \
+  (memcmp(&(a)->s6_addr[0], &(b)->s6_addr[0], sizeof(struct in6_addr)) == 0)
 
 #define INET6_ADDRSTRLEN 46
 
@@ -107,5 +100,4 @@
 
 #define ipv6mr_interface ipv6mr_ifindex
 
-
 #endif /* _NETINET_IN6_H */
diff --git a/linker/Android.mk b/linker/Android.mk
index c37d6db..b940690 100644
--- a/linker/Android.mk
+++ b/linker/Android.mk
@@ -23,7 +23,6 @@
     dlfcn.cpp \
     linker.cpp \
     linker_block_allocator.cpp \
-    linker_gdb_support.cpp \
     linker_libc_support.c \
     linker_mapped_file_fragment.cpp \
     linker_phdr.cpp \
diff --git a/linker/debugger.cpp b/linker/debugger.cpp
index b7e84ac..3731c99 100644
--- a/linker/debugger.cpp
+++ b/linker/debugger.cpp
@@ -27,7 +27,6 @@
  */
 
 #include "linker.h"
-#include "linker_gdb_support.h"
 
 #include <errno.h>
 #include <inttypes.h>
diff --git a/linker/linker.cpp b/linker/linker.cpp
index bad4c74..872d81d 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -51,7 +51,6 @@
 
 #include "linker.h"
 #include "linker_block_allocator.h"
-#include "linker_gdb_support.h"
 #include "linker_debug.h"
 #include "linker_sleb128.h"
 #include "linker_phdr.h"
@@ -206,13 +205,75 @@
   return sizeof(__linker_dl_err_buf);
 }
 
+// This function is an empty stub where GDB locates a breakpoint to get notified
+// about linker activity.
+extern "C"
+void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
+
+static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
+static r_debug _r_debug =
+    {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
+
+static link_map* r_debug_tail = nullptr;
+
+static void insert_link_map_into_debug_map(link_map* map) {
+  // Stick the new library at the end of the list.
+  // gdb tends to care more about libc than it does
+  // about leaf libraries, and ordering it this way
+  // reduces the back-and-forth over the wire.
+  if (r_debug_tail != nullptr) {
+    r_debug_tail->l_next = map;
+    map->l_prev = r_debug_tail;
+    map->l_next = nullptr;
+  } else {
+    _r_debug.r_map = map;
+    map->l_prev = nullptr;
+    map->l_next = nullptr;
+  }
+  r_debug_tail = map;
+}
+
+static void insert_soinfo_into_debug_map(soinfo* info) {
+  // Copy the necessary fields into the debug structure.
+  link_map* map = &(info->link_map_head);
+  map->l_addr = info->load_bias;
+  // link_map l_name field is not const.
+  map->l_name = const_cast<char*>(info->get_realpath());
+  map->l_ld = info->dynamic;
+
+  insert_link_map_into_debug_map(map);
+}
+
+static void remove_soinfo_from_debug_map(soinfo* info) {
+  link_map* map = &(info->link_map_head);
+
+  if (r_debug_tail == map) {
+    r_debug_tail = map->l_prev;
+  }
+
+  if (map->l_prev) {
+    map->l_prev->l_next = map->l_next;
+  }
+  if (map->l_next) {
+    map->l_next->l_prev = map->l_prev;
+  }
+}
+
 static void notify_gdb_of_load(soinfo* info) {
   if (info->is_main_executable()) {
     // GDB already knows about the main executable
     return;
   }
 
-  notify_gdb_of_load(&(info->link_map_head));
+  ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
+
+  _r_debug.r_state = r_debug::RT_ADD;
+  rtld_db_dlactivity();
+
+  insert_soinfo_into_debug_map(info);
+
+  _r_debug.r_state = r_debug::RT_CONSISTENT;
+  rtld_db_dlactivity();
 }
 
 static void notify_gdb_of_unload(soinfo* info) {
@@ -221,7 +282,22 @@
     return;
   }
 
-  notify_gdb_of_unload(&(info->link_map_head));
+  ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
+
+  _r_debug.r_state = r_debug::RT_DELETE;
+  rtld_db_dlactivity();
+
+  remove_soinfo_from_debug_map(info);
+
+  _r_debug.r_state = r_debug::RT_CONSISTENT;
+  rtld_db_dlactivity();
+}
+
+void notify_gdb_of_libraries() {
+  _r_debug.r_state = r_debug::RT_ADD;
+  rtld_db_dlactivity();
+  _r_debug.r_state = r_debug::RT_CONSISTENT;
+  rtld_db_dlactivity();
 }
 
 bool android_namespace_t::is_accessible(const std::string& file) {
diff --git a/linker/linker.h b/linker/linker.h
index c793b6e..c669c46 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -442,6 +442,7 @@
 
 void debuggerd_init();
 extern "C" abort_msg_t* g_abort_message;
+extern "C" void notify_gdb_of_libraries();
 
 char* linker_get_error_buffer();
 size_t linker_get_error_buffer_size();
diff --git a/linker/linker_gdb_support.cpp b/linker/linker_gdb_support.cpp
deleted file mode 100644
index de74087..0000000
--- a/linker/linker_gdb_support.cpp
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright (C) 2016 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 "linker_gdb_support.h"
-
-#include <pthread.h>
-
-#include "private/ScopedPthreadMutexLocker.h"
-
-// This function is an empty stub where GDB locates a breakpoint to get notified
-// about linker activity.
-extern "C"
-void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
-
-r_debug _r_debug =
-    {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
-
-static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
-static link_map* r_debug_tail = nullptr;
-
-void insert_link_map_into_debug_map(link_map* map) {
-  // Stick the new library at the end of the list.
-  // gdb tends to care more about libc than it does
-  // about leaf libraries, and ordering it this way
-  // reduces the back-and-forth over the wire.
-  if (r_debug_tail != nullptr) {
-    r_debug_tail->l_next = map;
-    map->l_prev = r_debug_tail;
-    map->l_next = nullptr;
-  } else {
-    _r_debug.r_map = map;
-    map->l_prev = nullptr;
-    map->l_next = nullptr;
-  }
-  r_debug_tail = map;
-}
-
-void remove_link_map_from_debug_map(link_map* map) {
-  if (r_debug_tail == map) {
-    r_debug_tail = map->l_prev;
-  }
-
-  if (map->l_prev) {
-    map->l_prev->l_next = map->l_next;
-  }
-  if (map->l_next) {
-    map->l_next->l_prev = map->l_prev;
-  }
-}
-
-void notify_gdb_of_load(link_map* map) {
-  ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
-
-  _r_debug.r_state = r_debug::RT_ADD;
-  rtld_db_dlactivity();
-
-  insert_link_map_into_debug_map(map);
-
-  _r_debug.r_state = r_debug::RT_CONSISTENT;
-  rtld_db_dlactivity();
-}
-
-void notify_gdb_of_unload(link_map* map) {
-  ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
-
-  _r_debug.r_state = r_debug::RT_DELETE;
-  rtld_db_dlactivity();
-
-  remove_link_map_from_debug_map(map);
-
-  _r_debug.r_state = r_debug::RT_CONSISTENT;
-  rtld_db_dlactivity();
-}
-
-void notify_gdb_of_libraries() {
-  _r_debug.r_state = r_debug::RT_ADD;
-  rtld_db_dlactivity();
-  _r_debug.r_state = r_debug::RT_CONSISTENT;
-  rtld_db_dlactivity();
-}
-
diff --git a/linker/linker_gdb_support.h b/linker/linker_gdb_support.h
deleted file mode 100644
index 2a590ba..0000000
--- a/linker/linker_gdb_support.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2016 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.
- */
-#ifndef __LINKER_GDB_SUPPORT_H
-#define __LINKER_GDB_SUPPORT_H
-
-#include <link.h>
-#include <sys/cdefs.h>
-
-__BEGIN_DECLS
-
-void insert_link_map_into_debug_map(link_map* map);
-void remove_link_map_from_debug_map(link_map* map);
-void notify_gdb_of_load(link_map* map);
-void notify_gdb_of_unload(link_map* map);
-void notify_gdb_of_libraries();
-
-extern struct r_debug _r_debug;
-
-__END_DECLS
-
-#endif
diff --git a/tests/ifaddrs_test.cpp b/tests/ifaddrs_test.cpp
index 2c34633..c3f0273 100644
--- a/tests/ifaddrs_test.cpp
+++ b/tests/ifaddrs_test.cpp
@@ -93,8 +93,7 @@
 
   std::vector<std::string> sys_class_net;
   {
-    auto dir_deleter = [](DIR* handle) { if (handle) closedir(handle); };
-    std::unique_ptr<DIR, decltype(dir_deleter)> d(opendir("/sys/class/net"), dir_deleter);
+    std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/sys/class/net"), closedir);
     ASSERT_TRUE(d != nullptr);
     dirent* dir;
     while ((dir = readdir(d.get())) != nullptr) {
@@ -150,6 +149,7 @@
       for (auto ub = addrs.upper_bound(it->first); it != ub; ++it) {
         if (it->second == addr) {
           found = true;
+          break;
         }
       }
       EXPECT_TRUE(found) << if_name;
@@ -220,3 +220,21 @@
 
   freeifaddrs(addrs);
 }
+
+TEST(ifaddrs, inet6_scope_ids) {
+  ifaddrs* addrs;
+  ASSERT_EQ(0, getifaddrs(&addrs));
+
+  for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
+    if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
+      sockaddr_in6* sa6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
+      // Any link-local IPv6 address should have a scope id. (http://b/27219454.)
+      // 0 isn't a valid interface index, so that would mean the scope id wasn't set.
+      if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
+        ASSERT_NE(sa6->sin6_scope_id, 0U);
+      }
+    }
+  }
+
+  freeifaddrs(addrs);
+}