Merge "Add basic tests for <link.h>."
diff --git a/libc/kernel/tools/defaults.py b/libc/kernel/tools/defaults.py
index 620bb31..c22f684 100644
--- a/libc/kernel/tools/defaults.py
+++ b/libc/kernel/tools/defaults.py
@@ -26,6 +26,9 @@
"CONFIG_64BIT": "__LP64__",
"CONFIG_X86_32": "__i386__",
"__EXPORTED_HEADERS__": "1",
+ "__HAVE_BUILTIN_BSWAP16__": "1",
+ "__HAVE_BUILTIN_BSWAP32__": "1",
+ "__HAVE_BUILTIN_BSWAP64__": "1",
}
# define to true if you want to remove all defined(CONFIG_FOO) tests
@@ -77,14 +80,14 @@
"udphdr": "__kernel_udphdr",
# The kernel's struct epoll_event just has __u64 for the data.
"epoll_event": "__kernel_uapi_epoll_event",
+ # This causes problems when trying to export the headers for the ndk.
+ "__attribute_const__": "__attribute__((__const__))",
}
-# this is the set of known static inline functions that we want to keep
-# in the final ARM headers. this is only used to keep optimized byteswapping
-# static functions and stuff like that.
-# TODO: this isn't working!
+# This is the set of known static inline functions that we want to keep
+# in the final kernel headers.
kernel_known_arm_statics = set(
- [ "___arch__swab32", # asm-arm/byteorder.h
+ [
]
)
@@ -99,8 +102,7 @@
)
kernel_known_x86_statics = set(
- [ "___arch__swab32", # asm-x86/byteorder.h
- "___arch__swab64", # asm-x86/byteorder.h
+ [
]
)
@@ -108,6 +110,26 @@
[
"ipt_get_target", # uapi/linux/netfilter_ipv4/ip_tables.h
"ip6t_get_target", # uapi/linux/netfilter_ipv6/ip6_tables.h
+ # Byte swapping inlines from uapi/linux/swab.h
+ # The below functions are the ones we are guaranting we export.
+ "__swab16",
+ "__swab32",
+ "__swab64",
+ "__swab16p",
+ "__swab32p",
+ "__swab64p",
+ "__swab16s",
+ "__swab32s",
+ "__swab64s",
+ "__swahw32",
+ "__swahb32",
+ "__swahw32p",
+ "__swahb32p",
+ "__swahw32s",
+ "__swahb32s",
+ # These are required to support the above functions.
+ "__fswahw32",
+ "__fswahb32",
]
)
diff --git a/libc/kernel/uapi/linux/swab.h b/libc/kernel/uapi/linux/swab.h
index a3387de..71aac11 100644
--- a/libc/kernel/uapi/linux/swab.h
+++ b/libc/kernel/uapi/linux/swab.h
@@ -41,70 +41,115 @@
#else
#endif
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+static inline __attribute__((__const__)) __u32 __fswahw32(__u32 val) {
#ifdef __arch_swahw32
+ return __arch_swahw32(val);
#else
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ return ___constant_swahw32(val);
#endif
+}
+static inline __attribute__((__const__)) __u32 __fswahb32(__u32 val) {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#ifdef __arch_swahb32
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ return __arch_swahb32(val);
#else
+ return ___constant_swahb32(val);
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#endif
-#ifdef __HAVE_BUILTIN_BSWAP16__
+}
#define __swab16(x) (__u16) __builtin_bswap16((__u16) (x))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#else
-#define __swab16(x) (__builtin_constant_p((__u16) (x)) ? ___constant_swab16(x) : __fswab16(x))
-#endif
-#ifdef __HAVE_BUILTIN_BSWAP32__
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define __swab32(x) (__u32) __builtin_bswap32((__u32) (x))
-#else
-#define __swab32(x) (__builtin_constant_p((__u32) (x)) ? ___constant_swab32(x) : __fswab32(x))
-#endif
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#ifdef __HAVE_BUILTIN_BSWAP64__
#define __swab64(x) (__u64) __builtin_bswap64((__u64) (x))
-#else
-#define __swab64(x) (__builtin_constant_p((__u64) (x)) ? ___constant_swab64(x) : __fswab64(x))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#endif
#define __swahw32(x) (__builtin_constant_p((__u32) (x)) ? ___constant_swahw32(x) : __fswahw32(x))
#define __swahb32(x) (__builtin_constant_p((__u32) (x)) ? ___constant_swahb32(x) : __fswahb32(x))
+static __always_inline __u16 __swab16p(const __u16 * p) {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#ifdef __arch_swab16p
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ return __arch_swab16p(p);
#else
+ return __swab16(* p);
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#endif
+}
+static __always_inline __u32 __swab32p(const __u32 * p) {
#ifdef __arch_swab32p
-#else
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ return __arch_swab32p(p);
+#else
+ return __swab32(* p);
#endif
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+}
+static __always_inline __u64 __swab64p(const __u64 * p) {
#ifdef __arch_swab64p
-#else
-#endif
+ return __arch_swab64p(p);
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#else
+ return __swab64(* p);
+#endif
+}
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+static inline __u32 __swahw32p(const __u32 * p) {
#ifdef __arch_swahw32p
+ return __arch_swahw32p(p);
#else
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ return __swahw32(* p);
#endif
+}
+static inline __u32 __swahb32p(const __u32 * p) {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#ifdef __arch_swahb32p
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ return __arch_swahb32p(p);
#else
+ return __swahb32(* p);
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#endif
+}
+static inline void __swab16s(__u16 * p) {
#ifdef __arch_swab16s
-#else
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ __arch_swab16s(p);
+#else
+ * p = __swab16p(p);
#endif
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+}
+static __always_inline void __swab32s(__u32 * p) {
#ifdef __arch_swab32s
-#else
-#endif
+ __arch_swab32s(p);
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+#else
+ * p = __swab32p(p);
+#endif
+}
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+static __always_inline void __swab64s(__u64 * p) {
#ifdef __arch_swab64s
+ __arch_swab64s(p);
#else
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ * p = __swab64p(p);
#endif
+}
+static inline void __swahw32s(__u32 * p) {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#ifdef __arch_swahw32s
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ __arch_swahw32s(p);
#else
+ * p = __swahw32p(p);
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#endif
+}
+static inline void __swahb32s(__u32 * p) {
#ifdef __arch_swahb32s
-#else
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ __arch_swahb32s(p);
+#else
+ * p = __swahb32p(p);
#endif
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+}
#endif
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 39062e6..11a00f7 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -36,6 +36,7 @@
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
+#include <sys/vfs.h>
#include <unistd.h>
#include <new>
@@ -1189,7 +1190,15 @@
return false;
}
- if (!ns->is_accessible(realpath)) {
+ struct statfs fs_stat;
+ if (TEMP_FAILURE_RETRY(fstatfs(task->get_fd(), &fs_stat)) != 0) {
+ DL_ERR("unable to fstatfs file for the library \"%s\": %s", name, strerror(errno));
+ return false;
+ }
+
+ // do not check accessibility using realpath if fd is located on tmpfs
+ // this enables use of memfd_create() for apps
+ if ((fs_stat.f_type != TMPFS_MAGIC) && (!ns->is_accessible(realpath))) {
// TODO(dimitry): workaround for http://b/26394120 - the grey-list
// TODO(dimitry) before O release: add a namespace attribute to have this enabled
diff --git a/tests/Android.bp b/tests/Android.bp
index c936c5c..27cc954 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -74,6 +74,7 @@
"libc_logging_test.cpp",
"libgen_basename_test.cpp",
"libgen_test.cpp",
+ "linux_swab_test.cpp",
"locale_test.cpp",
"malloc_test.cpp",
"math_test.cpp",
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index a0226a6..35dff2a 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -24,9 +24,13 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+
#include <android/dlext.h>
+
+#include <linux/memfd.h>
#include <sys/mman.h>
#include <sys/types.h>
+#include <sys/vfs.h>
#include <sys/wait.h>
#include <pagemap/pagemap.h>
@@ -802,6 +806,98 @@
dlclose(handle2);
}
+TEST(dlext, dlopen_ext_use_o_tmpfile_fd) {
+ const std::string lib_path = get_testlib_root() + "/libtest_simple.so";
+
+ int tmpfd = TEMP_FAILURE_RETRY(
+ open(get_testlib_root().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR | O_EXCL));
+
+ // Ignore kernels without O_TMPFILE flag support
+ if (tmpfd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) {
+ return;
+ }
+
+ ASSERT_TRUE(tmpfd != -1) << strerror(errno);
+
+ android_namespace_t* ns =
+ android_create_namespace("testing-o_tmpfile",
+ nullptr,
+ get_testlib_root().c_str(),
+ ANDROID_NAMESPACE_TYPE_ISOLATED,
+ nullptr,
+ nullptr);
+
+ ASSERT_DL_NOTNULL(ns);
+
+ ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
+
+ std::string content;
+ ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
+ ASSERT_TRUE(android::base::WriteStringToFd(content, tmpfd)) << strerror(errno);
+
+ android_dlextinfo extinfo;
+ extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
+ extinfo.library_fd = tmpfd;
+ extinfo.library_namespace = ns;
+
+ void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
+
+ ASSERT_DL_NOTNULL(handle);
+
+ uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
+ ASSERT_DL_NOTNULL(taxicab_number);
+ EXPECT_EQ(1729U, *taxicab_number);
+ dlclose(handle);
+}
+
+TEST(dlext, dlopen_ext_use_memfd) {
+ const std::string lib_path = get_testlib_root() + "/libtest_simple.so";
+
+ // create memfd
+ int memfd = syscall(__NR_memfd_create, "foobar", MFD_CLOEXEC);
+ if (memfd == -1 && errno == ENOSYS) {
+ return;
+ }
+
+ ASSERT_TRUE(memfd != -1) << strerror(errno);
+
+ // Check st.f_type is TMPFS_MAGIC for memfd
+ struct statfs st;
+ ASSERT_TRUE(TEMP_FAILURE_RETRY(fstatfs(memfd, &st)) == 0) << strerror(errno);
+ ASSERT_EQ(static_cast<decltype(st.f_type)>(TMPFS_MAGIC), st.f_type);
+
+ android_namespace_t* ns =
+ android_create_namespace("testing-memfd",
+ nullptr,
+ get_testlib_root().c_str(),
+ ANDROID_NAMESPACE_TYPE_ISOLATED,
+ nullptr,
+ nullptr);
+
+ ASSERT_DL_NOTNULL(ns);
+
+ ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror();
+
+ // read file into memfd backed one.
+ std::string content;
+ ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno);
+ ASSERT_TRUE(android::base::WriteStringToFd(content, memfd)) << strerror(errno);
+
+ android_dlextinfo extinfo;
+ extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE;
+ extinfo.library_fd = memfd;
+ extinfo.library_namespace = ns;
+
+ void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo);
+
+ ASSERT_DL_NOTNULL(handle);
+
+ uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number"));
+ ASSERT_DL_NOTNULL(taxicab_number);
+ EXPECT_EQ(1729U, *taxicab_number);
+ dlclose(handle);
+}
+
TEST(dlext, ns_symbol_visibilty_one_namespace) {
static const char* root_lib = "libnstest_root.so";
ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr));
diff --git a/tests/linux_swab_test.cpp b/tests/linux_swab_test.cpp
new file mode 100644
index 0000000..6b964dc
--- /dev/null
+++ b/tests/linux_swab_test.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2014 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 <gtest/gtest.h>
+
+#include <linux/swab.h>
+
+// This test makes sure that references to all of the kernel swab
+// macros/inline functions that are exported work properly.
+// Verifies that any kernel header updates do not break these macros.
+TEST(swab, fswa) {
+ EXPECT_EQ(0x3412U, __swab16(0x1234));
+ EXPECT_EQ(0x78563412U, __swab32(0x12345678U));
+ EXPECT_EQ(0xbaefcdab78563412ULL, __swab64(0x12345678abcdefbaULL));
+
+ __u16 bval16 = 0x1234;
+ EXPECT_EQ(0x3412U, __swab16p(&bval16));
+ __u32 bval32 = 0x12345678U;
+ EXPECT_EQ(0x78563412U, __swab32p(&bval32));
+ __u64 bval64 = 0x12345678abcdefbaULL;
+ EXPECT_EQ(0xbaefcdab78563412ULL, __swab64p(&bval64));
+
+ __u16 sval16 = 0x1234;
+ __swab16s(&sval16);
+ EXPECT_EQ(0x3412U, sval16);
+ __u32 sval32 = 0x12345678U;
+ __swab32s(&sval32);
+ EXPECT_EQ(0x78563412U, sval32);
+ __u64 sval64 = 0x12345678abcdefbaULL;
+ __swab64s(&sval64);
+ EXPECT_EQ(0xbaefcdab78563412ULL, sval64);
+
+ EXPECT_EQ(0x56781234U, __swahw32(0x12345678U));
+ EXPECT_EQ(0x34127856U, __swahb32(0x12345678U));
+
+ __u32 hval32 = 0x12345678U;
+ EXPECT_EQ(0x56781234U, __swahw32p(&hval32));
+ hval32 = 0x12345678U;
+ EXPECT_EQ(0x34127856U, __swahb32p(&hval32));
+
+ __u32 hsval32 = 0x12345678U;
+ __swahw32s(&hsval32);
+ EXPECT_EQ(0x56781234U, hsval32);
+ hsval32 = 0x12345678U;
+ __swahb32s(&hsval32);
+ EXPECT_EQ(0x34127856U, hsval32);
+}