From: Dimitry Ivanov Date: Mon, 19 Sep 2016 17:50:28 +0000 (-0700) Subject: bionic-unit-tests: Create symlinks in runtime X-Git-Tag: android-x86-8.1-r1~224^2^2~85^2~74^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=708589f5e8244a17d690848eed5f0fbfcce48a26;p=android-x86%2Fbionic.git bionic-unit-tests: Create symlinks in runtime Instead of creating symlinks in build time and then relying on usage of adb sync (adb push follows symlinks) - create them in test run time. Test: mm && run bionic-unit-tests --gtest_filter=dl*:Dl* Bug: http://b/22182538 Change-Id: I9d2a873e3ba303533c14ba5e3b779e4a67925462 --- diff --git a/tests/Android.bp b/tests/Android.bp index 4caaa5495..673bfd082 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -267,6 +267,7 @@ cc_test_library { srcs: [ "atexit_test.cpp", "dl_test.cpp", + "dlfcn_symlink_support.cpp", "dlfcn_test.cpp", "pthread_dlfcn_test.cpp", ], @@ -426,6 +427,7 @@ cc_test_host { srcs: [ "atexit_test.cpp", + "dlfcn_symlink_support.cpp", "dlfcn_test.cpp", "dl_test.cpp", "pthread_dlfcn_test.cpp", diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp index f597e6188..c4e0ac6b8 100644 --- a/tests/dlext_test.cpp +++ b/tests/dlext_test.cpp @@ -35,6 +35,7 @@ #include "TemporaryFile.h" #include "utils.h" #include "dlext_private.h" +#include "dlfcn_symlink_support.h" #define ASSERT_DL_NOTNULL(ptr) \ ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror() @@ -199,13 +200,15 @@ TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) { } TEST(dlext, android_dlopen_ext_force_load_smoke) { + DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke"); + const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); // 1. Open actual file void* handle = dlopen("libdlext_test.so", RTLD_NOW); ASSERT_DL_NOTNULL(handle); // 2. Open link with force_load flag set android_dlextinfo extinfo; extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; - void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo); + void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo); ASSERT_DL_NOTNULL(handle2); ASSERT_TRUE(handle != handle2); @@ -214,15 +217,17 @@ TEST(dlext, android_dlopen_ext_force_load_smoke) { } TEST(dlext, android_dlopen_ext_force_load_soname_exception) { + DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception"); + const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); // Check if soname lookup still returns already loaded library // when ANDROID_DLEXT_FORCE_LOAD flag is specified. - void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW); + void* handle = dlopen(symlink_name.c_str(), RTLD_NOW); ASSERT_DL_NOTNULL(handle); android_dlextinfo extinfo; extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; - // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so + // Note that 'libdlext_test.so' is dt_soname for the symlink_name void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo); ASSERT_DL_NOTNULL(handle2); diff --git a/tests/dlfcn_symlink_support.cpp b/tests/dlfcn_symlink_support.cpp new file mode 100644 index 000000000..be1839ecf --- /dev/null +++ b/tests/dlfcn_symlink_support.cpp @@ -0,0 +1,78 @@ +/* + * 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 "dlfcn_symlink_support.h" + +#include + +#include +#include +#include +#include + +#include + +#include +#include +#include + +static const constexpr char* source_file_name = "libdlext_test.so"; +static const constexpr char* symlink_name_prefix = "libdlext_test_"; + +static int dl_callback(struct dl_phdr_info *info, size_t /* size */, void *data) { + // The case when path is not absolute and is equal to source_file_name + // is disregarded intentionally since in bionic dlpi_name should always + // be realpath to a shared object. + const std::string suffix = std::string("/") + source_file_name; + + // TODO (dimitry): remove this check once fake libdl.so is gone + if (info->dlpi_name == nullptr) { + // This is linker imposing as libdl.so - skip it + return 0; + } + + if (android::base::EndsWith(info->dlpi_name, suffix.c_str())) { + std::string* path = reinterpret_cast(data); + *path = info->dlpi_name; + return 1; // found + } + + return 0; +} + +void create_dlfcn_test_symlink(const char* suffix, std::string* result) { + void* handle = dlopen(source_file_name, RTLD_NOW); + std::string source_file_path; + + ASSERT_TRUE(handle != nullptr) << dlerror(); + ASSERT_TRUE(dl_iterate_phdr(dl_callback, &source_file_path) == 1) + << "dl_phdr_info for \"" << source_file_name << "\" was not found."; + + dlclose(handle); + std::vector buf; + std::copy(source_file_path.begin(), source_file_path.end(), std::back_inserter(buf)); + buf.push_back('\0'); + + std::string path_dir = dirname(&buf[0]); + std::string link_path = path_dir + "/" + symlink_name_prefix + suffix + ".so"; + + ASSERT_TRUE(symlink(source_file_path.c_str(), link_path.c_str()) == 0) << strerror(errno); + *result = link_path; +} + +void remove_dlfcn_test_symlink(const std::string& path) { + ASSERT_TRUE(unlink(path.c_str()) == 0) << strerror(errno); +} diff --git a/tests/dlfcn_symlink_support.h b/tests/dlfcn_symlink_support.h new file mode 100644 index 000000000..8a8d3a2b0 --- /dev/null +++ b/tests/dlfcn_symlink_support.h @@ -0,0 +1,43 @@ +/* + * 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 __DLFCN_SYMLINK_SUPPORT_H__ +#define __DLFCN_SYMLINK_SUPPORT_H__ + +#include + +void create_dlfcn_test_symlink(const char* suffix, std::string* result); +void remove_dlfcn_test_symlink(const std::string& path); + +class DlfcnSymlink { + public: + explicit DlfcnSymlink(const char* test_name) { + create_dlfcn_test_symlink(test_name, &symlink_path_); + } + + ~DlfcnSymlink() { + remove_dlfcn_test_symlink(symlink_path_); + } + + const std::string& get_symlink_path() const { + return symlink_path_; + } + + private: + std::string symlink_path_; +}; + +#endif /* __DLFCN_SYMLINK_SUPPORT_H__ */ diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp index 34c410899..23b133bbe 100644 --- a/tests/dlfcn_test.cpp +++ b/tests/dlfcn_test.cpp @@ -17,15 +17,16 @@ #include #include -#include #include #include #include +#include #include "private/ScopeGuard.h" #include +#include "dlfcn_symlink_support.h" #include "utils.h" #define ASSERT_SUBSTR(needle, haystack) \ @@ -1012,8 +1013,10 @@ TEST(dlfcn, dlopen_undefined_weak_func) { } TEST(dlfcn, dlopen_symlink) { + DlfcnSymlink symlink("dlopen_symlink"); + const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); void* handle1 = dlopen("libdlext_test.so", RTLD_NOW); - void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW); + void* handle2 = dlopen(symlink_name.c_str(), RTLD_NOW); ASSERT_TRUE(handle1 != nullptr); ASSERT_TRUE(handle2 != nullptr); ASSERT_EQ(handle1, handle2); diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk index 94cc51632..a54318dd6 100644 --- a/tests/libs/Android.mk +++ b/tests/libs/Android.mk @@ -49,26 +49,6 @@ module_tag := optional include $(LOCAL_PATH)/Android.build.testlib.mk # ----------------------------------------------------------------------------- -# create symlink to libdlext_test.so for symlink test -# ----------------------------------------------------------------------------- -# Use = instead of := to defer the evaluation of $@ -$(TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \ - $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so - -ifneq ($(TARGET_2ND_ARCH),) -# link 64 bit .so -$($(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_DATA_NATIVE_TESTS)/bionic-loader-test-libs/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \ - $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so -endif - -# host symlinks -$(HOST_OUT)/lib64/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \ - $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so - -$(HOST_OUT)/lib/libdlext_test.so: PRIVATE_POST_INSTALL_CMD = \ - $(hide) cd $(dir $@) && ln -sf $(notdir $@) libdlext_test_v2.so - -# ----------------------------------------------------------------------------- # Library used by dlext tests - different name non-default location # ----------------------------------------------------------------------------- module := libdlext_test_fd