Merge changes from topics "musl_execinfo", "musl_mallinfo", "musl_sysroot" am: b60ce71254

Original change: https://android-review.googlesource.com/c/platform/bionic/+/1971623

Change-Id: I1b8b1bcb4840544c368714155a7473cb1f75a3a4
diff --git a/benchmarks/pthread_benchmark.cpp b/benchmarks/pthread_benchmark.cpp
index 856f150..6790c77 100644
--- a/benchmarks/pthread_benchmark.cpp
+++ b/benchmarks/pthread_benchmark.cpp
@@ -75,6 +75,7 @@
 }
 BIONIC_BENCHMARK(BM_pthread_mutex_lock);
 
+#if !defined(ANDROID_HOST_MUSL)
 static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) {
   pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
 
@@ -84,7 +85,9 @@
   }
 }
 BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
+#endif
 
+#if !defined(ANDROID_HOST_MUSL)
 static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) {
   pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
 
@@ -94,6 +97,7 @@
   }
 }
 BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
+#endif
 
 namespace {
 struct PIMutex {
diff --git a/benchmarks/time_benchmark.cpp b/benchmarks/time_benchmark.cpp
index a765e3e..70cc662 100644
--- a/benchmarks/time_benchmark.cpp
+++ b/benchmarks/time_benchmark.cpp
@@ -22,6 +22,21 @@
 #include <benchmark/benchmark.h>
 #include "util.h"
 
+// Musl doesn't define __NR_gettimeofday, __NR_clock_gettime32, __NR_gettimeofday_time32 or
+// __NR_clock_getres on 32-bit architectures.
+#if !defined(__NR_gettimeofday)
+#define __NR_gettimeofday __NR_gettimeofday_time32
+#endif
+#if !defined(__NR_clock_gettime)
+#define __NR_clock_gettime __NR_clock_gettime32
+#endif
+#if !defined(__NR_gettimeofday)
+#define __NR_gettimeofday __NR_gettimeofday_time32
+#endif
+#if !defined(__NR_clock_getres)
+#define __NR_clock_getres __NR_clock_getres_time32
+#endif
+
 static void BM_time_clock_gettime(benchmark::State& state) {
   // CLOCK_MONOTONIC is required supported in vdso
   timespec t;
diff --git a/libc/Android.bp b/libc/Android.bp
index 2dc10fc..84f5fbd 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -2572,8 +2572,136 @@
             enabled: true,
         },
     },
+    stl: "none",
+}
+
+cc_library_host_static {
+    name: "libexecinfo",
+    visibility: ["//external/musl"],
+    srcs: ["bionic/execinfo.cpp"],
+    export_include_dirs: ["execinfo/include"],
+    local_include_dirs: ["private"],
+    enabled: false,
+    target: {
+        musl: {
+            enabled: true,
+            system_shared_libs: [],
+            header_libs: ["libc_musl_headers"],
+        },
+    },
+    stl: "none",
+}
+
+cc_library_host_static {
+    name: "libb64",
+    visibility: ["//external/musl"],
+    srcs: ["upstream-openbsd/lib/libc/net/base64.c"],
+    export_include_dirs: ["b64/include"],
+    local_include_dirs: [
+        "private",
+        "upstream-openbsd/android/include",
+    ],
+    cflags: [
+        "-include openbsd-compat.h",
+    ],
+    enabled: false,
+    target: {
+        musl: {
+            enabled: true,
+            system_shared_libs: [],
+            header_libs: ["libc_musl_headers"],
+        },
+    },
+    stl: "none",
 }
 
 subdirs = [
     "bionic/scudo",
 ]
+
+// Export kernel uapi headers to be used in the musl sysroot.
+// Also include the execinfo headers for the libexecinfo and the
+// b64 headers for libb64 embedded in musl libc.
+cc_genrule {
+    name: "libc_musl_sysroot_bionic_headers",
+    visibility: ["//external/musl"],
+    host_supported: true,
+    device_supported: false,
+    enabled: false,
+    target: {
+        musl: {
+            enabled: true,
+        },
+    },
+    srcs: [
+        "kernel/uapi/**/*.h",
+        "kernel/android/**/*.h",
+
+        "kernel/uapi/linux/capability.h",
+        "kernel/android/scsi/scsi/scsi.h",
+        "kernel/android/uapi/linux/compiler.h",
+
+        "execinfo/include/**/*.h",
+        "execinfo/include/execinfo.h",
+
+        "b64/include/**/*.h",
+        "b64/include/bionic/b64.h",
+
+        ":libc_musl_sysroot_bionic_arch_headers",
+    ],
+    out: ["libc_musl_sysroot_bionic_headers.zip"],
+    tools: [
+        "soong_zip",
+        "merge_zips",
+        "zip2zip",
+    ],
+    cmd: "$(location soong_zip) -o $(genDir)/sysroot.zip -symlinks=false" +
+        // headers
+        " -P include " +
+        "  -C $$(dirname $(location kernel/uapi/linux/capability.h))/.. " +
+        "  -D $$(dirname $(location kernel/uapi/linux/capability.h))/.. " +
+        "  -C $$(dirname $(location kernel/android/scsi/scsi/scsi.h))/.. " +
+        "  -D $$(dirname $(location kernel/android/scsi/scsi/scsi.h))/.. " +
+        "  -C $$(dirname $(location kernel/android/uapi/linux/compiler.h))/.. " +
+        "  -D $$(dirname $(location kernel/android/uapi/linux/compiler.h))/.. " +
+        "  -C $$(dirname $(location execinfo/include/execinfo.h)) " +
+        "  -D $$(dirname $(location execinfo/include/execinfo.h)) " +
+        "  -C $$(dirname $(location b64/include/bionic/b64.h))/.. " +
+        "  -D $$(dirname $(location b64/include/bionic/b64.h))/.. " +
+        " && " +
+        "$(location merge_zips) $(out) $(location :libc_musl_sysroot_bionic_arch_headers) $(genDir)/sysroot.zip",
+}
+
+// The architecture-specific bits have to be handled separately because the label varies based
+// on architecture, which prevents using $(locations) to find them and requires using $(in)
+// instead, which would mix in all the other files if this were part of the main libc_musl_sysroot
+// genrule.
+cc_genrule {
+    name: "libc_musl_sysroot_bionic_arch_headers",
+    visibility: ["//visibility:private"],
+    host_supported: true,
+    device_supported: false,
+    enabled: false,
+    target: {
+        musl: {
+            enabled: true,
+        },
+    },
+    arch: {
+        arm: {
+            srcs: ["kernel/uapi/asm-arm/**/*.h"],
+        },
+        arm64: {
+            srcs: ["kernel/uapi/asm-arm64/**/*.h"],
+        },
+        x86: {
+            srcs: ["kernel/uapi/asm-x86/**/*.h"],
+        },
+        x86_64: {
+            srcs: ["kernel/uapi/asm-x86/**/*.h"],
+        },
+    },
+    out: ["libc_musl_sysroot_bionic_arch_headers.zip"],
+    tools: ["soong_zip"],
+    cmd: "includes=($(in)) && $(location soong_zip) -o $(out) -P include/asm -j -D $$(dirname $${includes[0]})",
+}
diff --git a/libc/NOTICE b/libc/NOTICE
index ff16da7..9cbbde2 100644
--- a/libc/NOTICE
+++ b/libc/NOTICE
@@ -910,6 +910,34 @@
 
 -------------------------------------------------------------------
 
+Copyright (C) 2022 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.
+
+-------------------------------------------------------------------
+
 Copyright (c) 1980, 1983, 1988, 1993
    The Regents of the University of California.  All rights reserved.
 
diff --git a/libc/b64/include/bionic/b64.h b/libc/b64/include/bionic/b64.h
new file mode 100644
index 0000000..f365bae
--- /dev/null
+++ b/libc/b64/include/bionic/b64.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+#pragma once
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+__BEGIN_DECLS
+
+int b64_ntop(unsigned char const* __src, size_t __src_size, char* __dst, size_t __dst_size);
+int b64_pton(char const* __src, u_char* __dst, size_t __dst_size);
+
+__END_DECLS
diff --git a/libc/execinfo/include/bionic/execinfo.h b/libc/execinfo/include/bionic/execinfo.h
new file mode 120000
index 0000000..0068019
--- /dev/null
+++ b/libc/execinfo/include/bionic/execinfo.h
@@ -0,0 +1 @@
+../../../include/execinfo.h
\ No newline at end of file
diff --git a/libc/execinfo/include/execinfo.h b/libc/execinfo/include/execinfo.h
new file mode 100644
index 0000000..e092c00
--- /dev/null
+++ b/libc/execinfo/include/execinfo.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+#pragma once
+
+/*
+ * This file is exported as part of libexecinfo for use with musl, which doesn't
+ * define __INTRODUCED_IN.  Stub it out.
+ */
+#define __INTRODUCED_IN(x)
+#include <bionic/execinfo.h>
+#undef __INTRODUCED_IN
diff --git a/tests/Android.bp b/tests/Android.bp
index 19d18ab..c077847 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -474,9 +474,6 @@
                 // musl doesn't have error.h
                 "error_test.cpp",
 
-                // musl doesn't have execinfo.h
-                "execinfo_test.cpp",
-
                 // musl doesn't define noreturn for C++
                 "stdnoreturn_test.cpp",
 
@@ -602,6 +599,10 @@
         host: {
             clang_cflags: ["-D__clang__"],
         },
+        musl: {
+            // Musl doesn't have fortify
+            enabled: false,
+        },
     },
 }
 
@@ -723,6 +724,7 @@
 cc_test_library {
     name: "libBionicTests",
     defaults: ["bionic_tests_defaults"],
+    host_supported: false,
     whole_static_libs: [
         "libBionicStandardTests",
         "libBionicElfTlsTests",
diff --git a/tests/headers/posix/Android.bp b/tests/headers/posix/Android.bp
index 0809cdb..4a20d45 100644
--- a/tests/headers/posix/Android.bp
+++ b/tests/headers/posix/Android.bp
@@ -33,5 +33,8 @@
         darwin: {
             enabled: false,
         },
+        musl: {
+            enabled: false,
+        },
     },
 }
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp
index 8272d39..f157ec4 100644
--- a/tests/malloc_test.cpp
+++ b/tests/malloc_test.cpp
@@ -765,7 +765,7 @@
 }
 
 TEST(malloc, mallinfo) {
-#if defined(__BIONIC__)
+#if defined(__BIONIC__) || defined(ANDROID_HOST_MUSL)
   SKIP_WITH_HWASAN << "hwasan does not implement mallinfo";
   static size_t sizes[] = {
     8, 32, 128, 4096, 32768, 131072, 1024000, 10240000, 20480000, 300000000
@@ -810,7 +810,7 @@
 }
 
 TEST(malloc, mallinfo2) {
-#if defined(__BIONIC__)
+#if defined(__BIONIC__) || defined(ANDROID_HOST_MUSL)
   SKIP_WITH_HWASAN << "hwasan does not implement mallinfo2";
   static size_t sizes[] = {8, 32, 128, 4096, 32768, 131072, 1024000, 10240000, 20480000, 300000000};
 
@@ -828,16 +828,16 @@
       struct mallinfo info = mallinfo();
       struct mallinfo2 info2 = mallinfo2();
       // Verify that mallinfo and mallinfo2 are exactly the same.
-      ASSERT_EQ(info.arena, info2.arena);
-      ASSERT_EQ(info.ordblks, info2.ordblks);
-      ASSERT_EQ(info.smblks, info2.smblks);
-      ASSERT_EQ(info.hblks, info2.hblks);
-      ASSERT_EQ(info.hblkhd, info2.hblkhd);
-      ASSERT_EQ(info.usmblks, info2.usmblks);
-      ASSERT_EQ(info.fsmblks, info2.fsmblks);
-      ASSERT_EQ(info.uordblks, info2.uordblks);
-      ASSERT_EQ(info.fordblks, info2.fordblks);
-      ASSERT_EQ(info.keepcost, info2.keepcost);
+      ASSERT_EQ(static_cast<size_t>(info.arena), info2.arena);
+      ASSERT_EQ(static_cast<size_t>(info.ordblks), info2.ordblks);
+      ASSERT_EQ(static_cast<size_t>(info.smblks), info2.smblks);
+      ASSERT_EQ(static_cast<size_t>(info.hblks), info2.hblks);
+      ASSERT_EQ(static_cast<size_t>(info.hblkhd), info2.hblkhd);
+      ASSERT_EQ(static_cast<size_t>(info.usmblks), info2.usmblks);
+      ASSERT_EQ(static_cast<size_t>(info.fsmblks), info2.fsmblks);
+      ASSERT_EQ(static_cast<size_t>(info.uordblks), info2.uordblks);
+      ASSERT_EQ(static_cast<size_t>(info.fordblks), info2.fordblks);
+      ASSERT_EQ(static_cast<size_t>(info.keepcost), info2.keepcost);
 
       size_t allocated = info2.uordblks;
       ptrs[i] = malloc(size);
@@ -846,16 +846,16 @@
       info = mallinfo();
       info2 = mallinfo2();
       // Verify that mallinfo and mallinfo2 are exactly the same.
-      ASSERT_EQ(info.arena, info2.arena);
-      ASSERT_EQ(info.ordblks, info2.ordblks);
-      ASSERT_EQ(info.smblks, info2.smblks);
-      ASSERT_EQ(info.hblks, info2.hblks);
-      ASSERT_EQ(info.hblkhd, info2.hblkhd);
-      ASSERT_EQ(info.usmblks, info2.usmblks);
-      ASSERT_EQ(info.fsmblks, info2.fsmblks);
-      ASSERT_EQ(info.uordblks, info2.uordblks);
-      ASSERT_EQ(info.fordblks, info2.fordblks);
-      ASSERT_EQ(info.keepcost, info2.keepcost);
+      ASSERT_EQ(static_cast<size_t>(info.arena), info2.arena);
+      ASSERT_EQ(static_cast<size_t>(info.ordblks), info2.ordblks);
+      ASSERT_EQ(static_cast<size_t>(info.smblks), info2.smblks);
+      ASSERT_EQ(static_cast<size_t>(info.hblks), info2.hblks);
+      ASSERT_EQ(static_cast<size_t>(info.hblkhd), info2.hblkhd);
+      ASSERT_EQ(static_cast<size_t>(info.usmblks), info2.usmblks);
+      ASSERT_EQ(static_cast<size_t>(info.fsmblks), info2.fsmblks);
+      ASSERT_EQ(static_cast<size_t>(info.uordblks), info2.uordblks);
+      ASSERT_EQ(static_cast<size_t>(info.fordblks), info2.fordblks);
+      ASSERT_EQ(static_cast<size_t>(info.keepcost), info2.keepcost);
 
       size_t new_allocated = info2.uordblks;
       if (allocated != new_allocated) {
diff --git a/tests/resolv_test.cpp b/tests/resolv_test.cpp
index 4425503..0cd8e63 100644
--- a/tests/resolv_test.cpp
+++ b/tests/resolv_test.cpp
@@ -33,7 +33,6 @@
 #include <gtest/gtest.h>
 
 TEST(resolv, b64_pton_28035006) {
-#if !defined(ANDROID_HOST_MUSL)
   // Test data from https://groups.google.com/forum/#!topic/mailing.openbsd.tech/w3ACIlklJkI.
   const char* data =
       "p1v3+nehH3N3n+/OokzXpsyGF2VVpxIxkjSn3Mv/Sq74OE1iFuVU+K4bQImuVj"
@@ -44,33 +43,22 @@
   // incorrectly required an extra byte. http://b/28035006.
   uint8_t buf[128];
   ASSERT_EQ(128, b64_pton(data, buf, sizeof(buf)));
-#else
-  GTEST_SKIP() << "musl doesn't have b64_pton";
-#endif
 }
 
 TEST(resolv, b64_ntop) {
-#if !defined(ANDROID_HOST_MUSL)
   char buf[128];
   memset(buf, 'x', sizeof(buf));
   ASSERT_EQ(static_cast<int>(strlen("aGVsbG8=")),
             b64_ntop(reinterpret_cast<u_char const*>("hello"), strlen("hello"),
                      buf, sizeof(buf)));
   ASSERT_STREQ(buf, "aGVsbG8=");
-#else
-  GTEST_SKIP() << "musl doesn't have b64_ntop";
-#endif
 }
 
 TEST(resolv, b64_pton) {
-#if !defined(ANDROID_HOST_MUSL)
   u_char buf[128];
   memset(buf, 'x', sizeof(buf));
   ASSERT_EQ(static_cast<int>(strlen("hello")), b64_pton("aGVsbG8=", buf, sizeof(buf)));
   ASSERT_STREQ(reinterpret_cast<char*>(buf), "hello");
-#else
-  GTEST_SKIP() << "musl doesn't have b64_pton";
-#endif
 }
 
 TEST(resolv, p_class) {