Merge "Verify that allocated memory is always zeroed."
diff --git a/libc/arch-arm64/dynamic_function_dispatch.cpp b/libc/arch-arm64/dynamic_function_dispatch.cpp
index 0edc7f7..cd55311 100644
--- a/libc/arch-arm64/dynamic_function_dispatch.cpp
+++ b/libc/arch-arm64/dynamic_function_dispatch.cpp
@@ -41,6 +41,12 @@
     }
 }
 
+typedef void* memcmp_func(void*, const void*, size_t);
+DEFINE_IFUNC_FOR(memcmp) {
+    // TODO: enable the SVE version.
+    RETURN_FUNC(memcmp_func, __memcmp_aarch64);
+}
+
 typedef void* memcpy_func(void*, const void*, size_t);
 DEFINE_IFUNC_FOR(memcpy) {
     if (arg->_hwcap & HWCAP_ASIMD) {
@@ -110,6 +116,12 @@
     RETURN_FUNC(strncmp_func, __strncmp_aarch64);
 }
 
+typedef size_t strnlen_func(const char*);
+DEFINE_IFUNC_FOR(strnlen) {
+    // TODO: enable the SVE version.
+    RETURN_FUNC(strnlen_func, __strnlen_aarch64);
+}
+
 typedef char* strrchr_func(const char*, int);
 DEFINE_IFUNC_FOR(strrchr) {
     if (arg->_hwcap2 & HWCAP2_MTE) {
diff --git a/libc/arch-arm64/static_function_dispatch.S b/libc/arch-arm64/static_function_dispatch.S
index 5c36de6..c7557f8 100644
--- a/libc/arch-arm64/static_function_dispatch.S
+++ b/libc/arch-arm64/static_function_dispatch.S
@@ -34,6 +34,7 @@
 END(name)
 
 FUNCTION_DELEGATE(memchr, __memchr_aarch64_mte)
+FUNCTION_DELEGATE(memcmp, __memcmp_aarch64)
 FUNCTION_DELEGATE(memcpy, __memcpy_aarch64)
 FUNCTION_DELEGATE(memmove, __memmove_aarch64)
 FUNCTION_DELEGATE(stpcpy, __stpcpy_aarch64)
@@ -44,5 +45,6 @@
 FUNCTION_DELEGATE(strlen, __strlen_aarch64_mte)
 FUNCTION_DELEGATE(strrchr, __strrchr_aarch64_mte)
 FUNCTION_DELEGATE(strncmp, __strncmp_aarch64)
+FUNCTION_DELEGATE(strnlen, __strnlen_aarch64)
 
 NOTE_GNU_PROPERTY()
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index 1ea94e6..4ef894b 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -111,9 +111,9 @@
  * Returns a new file descriptor on success and returns -1 and sets `errno` on
  * failure.
  */
-int creat(const char* __path, mode_t __mode);
+int creat(const char* _Nonnull __path, mode_t __mode);
 /** See creat(). */
-int creat64(const char* __path, mode_t __mode) __INTRODUCED_IN(21);
+int creat64(const char* _Nonnull __path, mode_t __mode) __INTRODUCED_IN(21);
 
 /**
  * [openat(2)](http://man7.org/linux/man-pages/man2/openat.2.html)
@@ -122,9 +122,9 @@
  * Returns a new file descriptor on success and returns -1 and sets `errno` on
  * failure.
  */
-int openat(int __dir_fd, const char* __path, int __flags, ...);
+int openat(int __dir_fd, const char* _Nonnull __path, int __flags, ...);
 /** See openat(). */
-int openat64(int __dir_fd, const char* __path, int __flags, ...) __INTRODUCED_IN(21);
+int openat64(int __dir_fd, const char* _Nonnull __path, int __flags, ...) __INTRODUCED_IN(21);
 
 /**
  * [open(2)](http://man7.org/linux/man-pages/man2/open.2.html)
@@ -133,9 +133,9 @@
  * Returns a new file descriptor on success and returns -1 and sets `errno` on
  * failure.
  */
-int open(const char* __path, int __flags, ...);
+int open(const char* _Nonnull __path, int __flags, ...);
 /** See open(). */
-int open64(const char* __path, int __flags, ...) __INTRODUCED_IN(21);
+int open64(const char* _Nonnull __path, int __flags, ...) __INTRODUCED_IN(21);
 
 /**
  * [splice(2)](http://man7.org/linux/man-pages/man2/splice.2.html)
@@ -149,7 +149,7 @@
  *
  * Available since API level 21.
  */
-ssize_t splice(int __in_fd, off64_t* __in_offset, int __out_fd, off64_t* __out_offset, size_t __length, unsigned int __flags) __INTRODUCED_IN(21);
+ssize_t splice(int __in_fd, off64_t* _Null_unspecified __in_offset, int __out_fd, off64_t* _Null_unspecified __out_offset, size_t __length, unsigned int __flags) __INTRODUCED_IN(21);
 
 /**
  * [tee(2)](http://man7.org/linux/man-pages/man2/tee.2.html)
@@ -177,7 +177,7 @@
  *
  * Available since API level 21.
  */
-ssize_t vmsplice(int __fd, const struct iovec* __iov, size_t __count, unsigned int __flags) __INTRODUCED_IN(21);
+ssize_t vmsplice(int __fd, const struct iovec* _Nonnull __iov, size_t __count, unsigned int __flags) __INTRODUCED_IN(21);
 
 /**
  * [fallocate(2)](http://man7.org/linux/man-pages/man2/fallocate.2.html)
diff --git a/libc/include/nl_types.h b/libc/include/nl_types.h
index 1c80e4e..f4d7f43 100644
--- a/libc/include/nl_types.h
+++ b/libc/include/nl_types.h
@@ -62,7 +62,7 @@
  *
  * Available since API level 28.
  */
-nl_catd catopen(const char* __name, int __flag) __INTRODUCED_IN(26);
+nl_catd _Nonnull catopen(const char* _Nonnull __name, int __flag) __INTRODUCED_IN(26);
 
 /**
  * [catgets(3)](http://man7.org/linux/man-pages/man3/catgets.3.html) translates the given message
@@ -72,13 +72,13 @@
  *
  * Available since API level 28.
  */
-char* catgets(nl_catd __catalog, int __set_number, int __msg_number, const char* __msg) __INTRODUCED_IN(26);
+char* _Nonnull catgets(nl_catd _Nonnull __catalog, int __set_number, int __msg_number, const char* _Nonnull __msg) __INTRODUCED_IN(26);
 
 /**
  * [catclose(3)](http://man7.org/linux/man-pages/man3/catclose.3.html) closes a message catalog.
  *
  * On Android, this always returns -1 with `errno` set to `EBADF`.
  */
-int catclose(nl_catd __catalog) __INTRODUCED_IN(26);
+int catclose(nl_catd _Nonnull __catalog) __INTRODUCED_IN(26);
 
 __END_DECLS
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index f0e7b1b..833157e 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -203,35 +203,29 @@
   ElfW(Addr) entry_point;
 };
 
-static ExecutableInfo get_executable_info() {
+static ExecutableInfo get_executable_info(const char* arg_path) {
   ExecutableInfo result = {};
+  char const* exe_path = "/proc/self/exe";
 
-  if (is_first_stage_init()) {
-    // /proc fs is not mounted when first stage init starts. Therefore we can't
-    // use /proc/self/exe for init.
-    stat("/init", &result.file_stat);
-
-    // /init may be a symlink, so try to read it as such.
-    char path[PATH_MAX];
-    ssize_t path_len = readlink("/init", path, sizeof(path));
-    if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
-      result.path = "/init";
-    } else {
-      result.path = std::string(path, path_len);
+  // Stat "/proc/self/exe" instead of executable_path because
+  // the executable could be unlinked by this point and it should
+  // not cause a crash (see http://b/31084669)
+  if (TEMP_FAILURE_RETRY(stat(exe_path, &result.file_stat) == -1)) {
+    // Fallback to argv[0] for the case where /proc isn't available
+    if (TEMP_FAILURE_RETRY(stat(arg_path, &result.file_stat) == -1)) {
+      async_safe_fatal("unable to stat either \"/proc/self/exe\" or \"%s\": %s",
+          arg_path, strerror(errno));
     }
+    exe_path = arg_path;
+  }
+
+  // Path might be a symlink
+  char sym_path[PATH_MAX];
+  ssize_t sym_path_len = readlink(exe_path, sym_path, sizeof(sym_path));
+  if (sym_path_len > 0 && sym_path_len < static_cast<ssize_t>(sizeof(sym_path))) {
+    result.path = std::string(sym_path, sym_path_len);
   } else {
-    // Stat "/proc/self/exe" instead of executable_path because
-    // the executable could be unlinked by this point and it should
-    // not cause a crash (see http://b/31084669)
-    if (TEMP_FAILURE_RETRY(stat("/proc/self/exe", &result.file_stat)) != 0) {
-      async_safe_fatal("unable to stat \"/proc/self/exe\": %s", strerror(errno));
-    }
-    char path[PATH_MAX];
-    ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
-    if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
-      async_safe_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
-    }
-    result.path = std::string(path, path_len);
+    result.path = std::string(exe_path, strlen(exe_path));
   }
 
   result.phdr = reinterpret_cast<const ElfW(Phdr)*>(getauxval(AT_PHDR));
@@ -359,7 +353,7 @@
   }
 
   const ExecutableInfo exe_info = exe_to_load ? load_executable(exe_to_load) :
-                                                get_executable_info();
+                                                get_executable_info(args.argv[0]);
 
   INFO("[ Linking executable \"%s\" ]", exe_info.path.c_str());