From 708589f5e8244a17d690848eed5f0fbfcce48a26 Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Mon, 19 Sep 2016 10:50:28 -0700 Subject: 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 --- tests/dlfcn_symlink_support.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/dlfcn_symlink_support.cpp (limited to 'tests/dlfcn_symlink_support.cpp') 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); +} -- cgit v1.2.3