Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <dlfcn.h> |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 20 | #include <elf.h> |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 23 | #include <inttypes.h> |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 27 | #include <android/dlext.h> |
| 28 | #include <sys/mman.h> |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 29 | #include <sys/types.h> |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 30 | #include <sys/wait.h> |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 31 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 32 | #include <pagemap/pagemap.h> |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 33 | #include <ziparchive/zip_archive.h> |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 34 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 35 | #include "TemporaryFile.h" |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 36 | #include "utils.h" |
Dimitry Ivanov | 41fd295 | 2016-05-09 17:37:39 -0700 | [diff] [blame] | 37 | #include "dlext_private.h" |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame^] | 38 | #include "dlfcn_symlink_support.h" |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 39 | |
| 40 | #define ASSERT_DL_NOTNULL(ptr) \ |
Chih-Hung Hsieh | d61ca37 | 2016-06-03 10:18:07 -0700 | [diff] [blame] | 41 | ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror() |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 42 | |
| 43 | #define ASSERT_DL_ZERO(i) \ |
| 44 | ASSERT_EQ(0, i) << "dlerror: " << dlerror() |
| 45 | |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 46 | #define ASSERT_NOERROR(i) \ |
| 47 | ASSERT_NE(-1, i) << "errno: " << strerror(errno) |
| 48 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 49 | #define ASSERT_SUBSTR(needle, haystack) \ |
| 50 | ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) |
| 51 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 52 | |
| 53 | typedef int (*fn)(void); |
| 54 | #define LIBNAME "libdlext_test.so" |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 55 | #define LIBNAME_NORELRO "libdlext_test_norelro.so" |
Chih-Hung Hsieh | d61ca37 | 2016-06-03 10:18:07 -0700 | [diff] [blame] | 56 | constexpr auto LIBSIZE = 1024 * 1024; // how much address space to reserve for it |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 57 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 58 | #if defined(__LP64__) |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 59 | #define NATIVE_TESTS_PATH "/nativetest64/bionic-loader-test-libs" |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 60 | #else |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 61 | #define NATIVE_TESTS_PATH "/nativetest/bionic-loader-test-libs" |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 62 | #endif |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 63 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 64 | #define LIBPATH NATIVE_TESTS_PATH "/libdlext_test_fd/libdlext_test_fd.so" |
| 65 | #define LIBZIPPATH NATIVE_TESTS_PATH "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip" |
| 66 | #define LIBZIPPATH_WITH_RUNPATH NATIVE_TESTS_PATH "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip" |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 67 | #define LIBZIP_SIMPLE_ZIP "libdir/libatest_simple_zip.so" |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 68 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 69 | class DlExtTest : public ::testing::Test { |
| 70 | protected: |
| 71 | virtual void SetUp() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 72 | handle_ = nullptr; |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 73 | // verify that we don't have the library loaded already |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 74 | void* h = dlopen(LIBNAME, RTLD_NOW | RTLD_NOLOAD); |
| 75 | ASSERT_TRUE(h == nullptr); |
| 76 | h = dlopen(LIBNAME_NORELRO, RTLD_NOW | RTLD_NOLOAD); |
| 77 | ASSERT_TRUE(h == nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 78 | // call dlerror() to swallow the error, and check it was the one we wanted |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 79 | ASSERT_STREQ("dlopen failed: library \"" LIBNAME_NORELRO "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | virtual void TearDown() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 83 | if (handle_ != nullptr) { |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 84 | ASSERT_DL_ZERO(dlclose(handle_)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void* handle_; |
| 89 | }; |
| 90 | |
| 91 | TEST_F(DlExtTest, ExtInfoNull) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 92 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 93 | ASSERT_DL_NOTNULL(handle_); |
| 94 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 95 | ASSERT_DL_NOTNULL(f); |
| 96 | EXPECT_EQ(4, f()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(DlExtTest, ExtInfoNoFlags) { |
| 100 | android_dlextinfo extinfo; |
| 101 | extinfo.flags = 0; |
| 102 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 103 | ASSERT_DL_NOTNULL(handle_); |
| 104 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 105 | ASSERT_DL_NOTNULL(f); |
| 106 | EXPECT_EQ(4, f()); |
| 107 | } |
| 108 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 109 | TEST_F(DlExtTest, ExtInfoUseFd) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 110 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBPATH; |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 111 | |
| 112 | android_dlextinfo extinfo; |
| 113 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 114 | extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 115 | ASSERT_TRUE(extinfo.library_fd != -1); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 116 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 117 | ASSERT_DL_NOTNULL(handle_); |
| 118 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 119 | ASSERT_DL_NOTNULL(f); |
| 120 | EXPECT_EQ(4, f()); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 121 | |
| 122 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 123 | ASSERT_DL_NOTNULL(taxicab_number); |
| 124 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 127 | TEST_F(DlExtTest, ExtInfoUseFdWithOffset) { |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 128 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 129 | |
| 130 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 131 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 132 | extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 133 | |
| 134 | // Find the offset of the shared library in the zip. |
| 135 | ZipArchiveHandle handle; |
| 136 | ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle)); |
| 137 | ZipEntry zip_entry; |
| 138 | ZipString zip_name; |
| 139 | zip_name.name = reinterpret_cast<const uint8_t*>(LIBZIP_SIMPLE_ZIP); |
| 140 | zip_name.name_length = sizeof(LIBZIP_SIMPLE_ZIP) - 1; |
| 141 | ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry)); |
| 142 | extinfo.library_fd_offset = zip_entry.offset; |
| 143 | CloseArchive(handle); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 144 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 145 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 146 | ASSERT_DL_NOTNULL(handle_); |
| 147 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 148 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 149 | ASSERT_DL_NOTNULL(taxicab_number); |
| 150 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) { |
Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 154 | // lib_path is relative when $ANDROID_DATA is relative |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 155 | std::string lib_path; |
| 156 | ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 157 | |
| 158 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 159 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 160 | extinfo.library_fd = TEMP_FAILURE_RETRY(open(lib_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 161 | extinfo.library_fd_offset = 17; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 162 | |
| 163 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 164 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 165 | ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror()); |
| 166 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 167 | // Test an address above 2^44, for http://b/18178121 . |
| 168 | extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 169 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 170 | ASSERT_TRUE(handle_ == nullptr); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 171 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror()); |
| 172 | |
| 173 | extinfo.library_fd_offset = 0LL - PAGE_SIZE; |
| 174 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 175 | ASSERT_TRUE(handle_ == nullptr); |
| 176 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror()); |
| 177 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 178 | extinfo.library_fd_offset = 0; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 179 | handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 180 | ASSERT_TRUE(handle_ == nullptr); |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 181 | ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror()); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 182 | |
Dmitriy Ivanov | fd7a91e | 2015-11-06 10:44:37 -0800 | [diff] [blame] | 183 | // Check if dlsym works after unsuccessful dlopen(). |
| 184 | // Supply non-exiting one to make linker visit every soinfo. |
| 185 | void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___"); |
| 186 | ASSERT_TRUE(sym == nullptr); |
| 187 | |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 188 | close(extinfo.library_fd); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 191 | TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 192 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 193 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 194 | // This offset will not be used, so it doesn't matter. |
| 195 | extinfo.library_fd_offset = 0; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 196 | |
| 197 | handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo); |
| 198 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 199 | ASSERT_STREQ("dlopen failed: invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without ANDROID_DLEXT_USE_LIBRARY_FD): 0x20", dlerror()); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 202 | TEST(dlext, android_dlopen_ext_force_load_smoke) { |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame^] | 203 | DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke"); |
| 204 | const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 205 | // 1. Open actual file |
| 206 | void* handle = dlopen("libdlext_test.so", RTLD_NOW); |
| 207 | ASSERT_DL_NOTNULL(handle); |
| 208 | // 2. Open link with force_load flag set |
| 209 | android_dlextinfo extinfo; |
| 210 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame^] | 211 | void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 212 | ASSERT_DL_NOTNULL(handle2); |
| 213 | ASSERT_TRUE(handle != handle2); |
| 214 | |
| 215 | dlclose(handle2); |
| 216 | dlclose(handle); |
| 217 | } |
| 218 | |
| 219 | TEST(dlext, android_dlopen_ext_force_load_soname_exception) { |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame^] | 220 | DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception"); |
| 221 | const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 222 | // Check if soname lookup still returns already loaded library |
| 223 | // when ANDROID_DLEXT_FORCE_LOAD flag is specified. |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame^] | 224 | void* handle = dlopen(symlink_name.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 225 | ASSERT_DL_NOTNULL(handle); |
| 226 | |
| 227 | android_dlextinfo extinfo; |
| 228 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 229 | |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame^] | 230 | // Note that 'libdlext_test.so' is dt_soname for the symlink_name |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 231 | void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo); |
| 232 | |
| 233 | ASSERT_DL_NOTNULL(handle2); |
| 234 | ASSERT_TRUE(handle == handle2); |
| 235 | |
| 236 | dlclose(handle2); |
| 237 | dlclose(handle); |
| 238 | } |
| 239 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 240 | TEST(dlfcn, dlopen_from_zip_absolute_path) { |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 241 | std::string lib_path; |
| 242 | ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")) + LIBZIPPATH, &lib_path)) << strerror(errno); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 243 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 244 | void* handle = dlopen((lib_path + "!/libdir/libatest_simple_zip.so").c_str(), RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 245 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 246 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 247 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 248 | ASSERT_DL_NOTNULL(taxicab_number); |
| 249 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 250 | |
| 251 | dlclose(handle); |
| 252 | } |
| 253 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 254 | TEST(dlfcn, dlopen_from_zip_with_dt_runpath) { |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 255 | std::string data_path; |
| 256 | ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno); |
| 257 | const std::string lib_path = data_path + LIBZIPPATH_WITH_RUNPATH; |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 258 | |
| 259 | void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW); |
| 260 | |
| 261 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 262 | |
| 263 | typedef void *(* dlopen_b_fn)(); |
| 264 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); |
| 265 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 266 | |
| 267 | void *p = fn(); |
| 268 | ASSERT_TRUE(p != nullptr) << dlerror(); |
| 269 | |
| 270 | dlclose(p); |
| 271 | dlclose(handle); |
| 272 | } |
| 273 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 274 | TEST(dlfcn, dlopen_from_zip_ld_library_path) { |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 275 | std::string data_path; |
| 276 | ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno); |
| 277 | const std::string lib_path = data_path + LIBZIPPATH + "!/libdir"; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 278 | |
| 279 | typedef void (*fn_t)(const char*); |
| 280 | fn_t android_update_LD_LIBRARY_PATH = |
| 281 | reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH")); |
| 282 | |
| 283 | ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror(); |
| 284 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 285 | void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 286 | ASSERT_TRUE(handle == nullptr); |
| 287 | |
| 288 | android_update_LD_LIBRARY_PATH(lib_path.c_str()); |
| 289 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 290 | handle = dlopen("libdlext_test_zip.so", RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 291 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 292 | |
| 293 | int (*fn)(void); |
| 294 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 295 | ASSERT_TRUE(fn != nullptr); |
| 296 | EXPECT_EQ(4, fn()); |
| 297 | |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 298 | uint32_t* taxicab_number = |
| 299 | reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 300 | ASSERT_DL_NOTNULL(taxicab_number); |
| 301 | EXPECT_EQ(1729U, *taxicab_number); |
| 302 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 303 | dlclose(handle); |
| 304 | } |
| 305 | |
| 306 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 307 | TEST_F(DlExtTest, Reserved) { |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 308 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 309 | ASSERT_TRUE(start != MAP_FAILED); |
| 310 | android_dlextinfo extinfo; |
| 311 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 312 | extinfo.reserved_addr = start; |
| 313 | extinfo.reserved_size = LIBSIZE; |
| 314 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 315 | ASSERT_DL_NOTNULL(handle_); |
| 316 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 317 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 318 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 319 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 320 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 321 | EXPECT_EQ(4, f()); |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 322 | |
| 323 | // Check that after dlclose reserved address space is unmapped (and can be reused) |
| 324 | dlclose(handle_); |
| 325 | handle_ = nullptr; |
| 326 | |
| 327 | void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 328 | ASSERT_NE(start, new_start) << "dlclose unmapped reserved space"; |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | TEST_F(DlExtTest, ReservedTooSmall) { |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 332 | void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 333 | ASSERT_TRUE(start != MAP_FAILED); |
| 334 | android_dlextinfo extinfo; |
| 335 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 336 | extinfo.reserved_addr = start; |
| 337 | extinfo.reserved_size = PAGE_SIZE; |
| 338 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 339 | EXPECT_EQ(nullptr, handle_); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST_F(DlExtTest, ReservedHint) { |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 343 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 344 | ASSERT_TRUE(start != MAP_FAILED); |
| 345 | android_dlextinfo extinfo; |
| 346 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 347 | extinfo.reserved_addr = start; |
| 348 | extinfo.reserved_size = LIBSIZE; |
| 349 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 350 | ASSERT_DL_NOTNULL(handle_); |
| 351 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 352 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 353 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 354 | EXPECT_LT(reinterpret_cast<void*>(f), |
| 355 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 356 | EXPECT_EQ(4, f()); |
| 357 | } |
| 358 | |
| 359 | TEST_F(DlExtTest, ReservedHintTooSmall) { |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 360 | void* start = mmap(nullptr, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 361 | ASSERT_TRUE(start != MAP_FAILED); |
| 362 | android_dlextinfo extinfo; |
| 363 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 364 | extinfo.reserved_addr = start; |
| 365 | extinfo.reserved_size = PAGE_SIZE; |
| 366 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 367 | ASSERT_DL_NOTNULL(handle_); |
| 368 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 369 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 370 | EXPECT_TRUE(reinterpret_cast<void*>(f) < start || |
| 371 | (reinterpret_cast<void*>(f) >= |
| 372 | reinterpret_cast<char*>(start) + PAGE_SIZE)); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 373 | EXPECT_EQ(4, f()); |
| 374 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 375 | |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 376 | TEST_F(DlExtTest, LoadAtFixedAddress) { |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 377 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 378 | ASSERT_TRUE(start != MAP_FAILED); |
| 379 | munmap(start, LIBSIZE); |
| 380 | |
| 381 | android_dlextinfo extinfo; |
| 382 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 383 | extinfo.reserved_addr = start; |
| 384 | |
| 385 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 386 | ASSERT_DL_NOTNULL(handle_); |
| 387 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 388 | ASSERT_DL_NOTNULL(f); |
| 389 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
| 390 | EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE); |
| 391 | |
| 392 | EXPECT_EQ(4, f()); |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 393 | dlclose(handle_); |
| 394 | handle_ = nullptr; |
| 395 | |
| 396 | // Check that dlclose unmapped the file |
| 397 | void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 398 | ASSERT_EQ(start, addr) << "dlclose did not unmap the memory"; |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) { |
| 402 | void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE, |
| 403 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 404 | ASSERT_TRUE(start != MAP_FAILED); |
| 405 | munmap(start, LIBSIZE + PAGE_SIZE); |
| 406 | void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE, |
| 407 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 408 | ASSERT_TRUE(new_addr != MAP_FAILED); |
| 409 | |
| 410 | android_dlextinfo extinfo; |
| 411 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 412 | extinfo.reserved_addr = start; |
| 413 | |
| 414 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 415 | ASSERT_TRUE(handle_ == nullptr); |
| 416 | } |
| 417 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 418 | class DlExtRelroSharingTest : public DlExtTest { |
| 419 | protected: |
| 420 | virtual void SetUp() { |
| 421 | DlExtTest::SetUp(); |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 422 | void* start = mmap(nullptr, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 423 | ASSERT_TRUE(start != MAP_FAILED); |
| 424 | extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 425 | extinfo_.reserved_addr = start; |
| 426 | extinfo_.reserved_size = LIBSIZE; |
| 427 | extinfo_.relro_fd = -1; |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 430 | virtual void TearDown() { |
| 431 | DlExtTest::TearDown(); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 432 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 433 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 434 | void CreateRelroFile(const char* lib, const char* relro_file) { |
| 435 | int relro_fd = open(relro_file, O_RDWR | O_TRUNC); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 436 | ASSERT_NOERROR(relro_fd); |
| 437 | |
| 438 | pid_t pid = fork(); |
| 439 | if (pid == 0) { |
| 440 | // child process |
| 441 | extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO; |
| 442 | extinfo_.relro_fd = relro_fd; |
| 443 | void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 444 | if (handle == nullptr) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 445 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 446 | exit(1); |
| 447 | } |
| 448 | exit(0); |
| 449 | } |
| 450 | |
| 451 | // continuing in parent |
| 452 | ASSERT_NOERROR(close(relro_fd)); |
| 453 | ASSERT_NOERROR(pid); |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 454 | AssertChildExited(pid, 0); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 455 | |
| 456 | // reopen file for reading so it can be used |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 457 | relro_fd = open(relro_file, O_RDONLY); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 458 | ASSERT_NOERROR(relro_fd); |
| 459 | extinfo_.flags |= ANDROID_DLEXT_USE_RELRO; |
| 460 | extinfo_.relro_fd = relro_fd; |
| 461 | } |
| 462 | |
| 463 | void TryUsingRelro(const char* lib) { |
| 464 | handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 465 | ASSERT_DL_NOTNULL(handle_); |
| 466 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 467 | ASSERT_DL_NOTNULL(f); |
| 468 | EXPECT_EQ(4, f()); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 469 | |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 470 | uint32_t* taxicab_number = |
| 471 | reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 472 | ASSERT_DL_NOTNULL(taxicab_number); |
| 473 | EXPECT_EQ(1729U, *taxicab_number); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 476 | void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out); |
| 477 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 478 | android_dlextinfo extinfo_; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 479 | }; |
| 480 | |
| 481 | TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 482 | TemporaryFile tf; // Use tf to get an unique filename. |
| 483 | ASSERT_NOERROR(close(tf.fd)); |
| 484 | |
| 485 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 486 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 487 | |
| 488 | // Use destructor of tf to close and unlink the file. |
| 489 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 493 | TemporaryFile tf; // // Use tf to get an unique filename. |
| 494 | ASSERT_NOERROR(close(tf.fd)); |
| 495 | |
| 496 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 497 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 498 | |
| 499 | // Use destructor of tf to close and unlink the file. |
| 500 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | TEST_F(DlExtRelroSharingTest, RelroFileEmpty) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 504 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 505 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 506 | |
| 507 | TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) { |
Dan Albert | 69fb9f3 | 2014-09-03 11:30:21 -0700 | [diff] [blame] | 508 | if (geteuid() != 0) { |
| 509 | GTEST_LOG_(INFO) << "This test must be run as root.\n"; |
| 510 | return; |
| 511 | } |
| 512 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 513 | TemporaryFile tf; // Use tf to get an unique filename. |
| 514 | ASSERT_NOERROR(close(tf.fd)); |
| 515 | |
| 516 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
| 517 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 518 | int pipefd[2]; |
| 519 | ASSERT_NOERROR(pipe(pipefd)); |
| 520 | |
| 521 | size_t without_sharing, with_sharing; |
| 522 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing)); |
| 523 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing)); |
| 524 | |
| 525 | // We expect the sharing to save at least 10% of the total PSS. In practice |
| 526 | // it saves 40%+ for this test. |
| 527 | size_t expected_size = without_sharing - (without_sharing/10); |
| 528 | EXPECT_LT(with_sharing, expected_size); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 529 | |
| 530 | // Use destructor of tf to close and unlink the file. |
| 531 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void getPss(pid_t pid, size_t* pss_out) { |
| 535 | pm_kernel_t* kernel; |
| 536 | ASSERT_EQ(0, pm_kernel_create(&kernel)); |
| 537 | |
| 538 | pm_process_t* process; |
| 539 | ASSERT_EQ(0, pm_process_create(kernel, pid, &process)); |
| 540 | |
| 541 | pm_map_t** maps; |
| 542 | size_t num_maps; |
| 543 | ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps)); |
| 544 | |
| 545 | size_t total_pss = 0; |
| 546 | for (size_t i = 0; i < num_maps; i++) { |
| 547 | pm_memusage_t usage; |
| 548 | ASSERT_EQ(0, pm_map_usage(maps[i], &usage)); |
| 549 | total_pss += usage.pss; |
| 550 | } |
| 551 | *pss_out = total_pss; |
| 552 | |
| 553 | free(maps); |
| 554 | pm_process_destroy(process); |
| 555 | pm_kernel_destroy(kernel); |
| 556 | } |
| 557 | |
| 558 | void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, |
| 559 | size_t* pss_out) { |
| 560 | const int CHILDREN = 20; |
| 561 | |
| 562 | // Create children |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 563 | pid_t child_pids[CHILDREN]; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 564 | int childpipe[CHILDREN]; |
| 565 | for (int i=0; i<CHILDREN; ++i) { |
| 566 | char read_buf; |
| 567 | int child_done_pipe[2], parent_done_pipe[2]; |
| 568 | ASSERT_NOERROR(pipe(child_done_pipe)); |
| 569 | ASSERT_NOERROR(pipe(parent_done_pipe)); |
| 570 | |
| 571 | pid_t child = fork(); |
| 572 | if (child == 0) { |
| 573 | // close the 'wrong' ends of the pipes in the child |
| 574 | close(child_done_pipe[0]); |
| 575 | close(parent_done_pipe[1]); |
| 576 | |
| 577 | // open the library |
| 578 | void* handle; |
| 579 | if (share_relro) { |
| 580 | handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 581 | } else { |
| 582 | handle = dlopen(lib, RTLD_NOW); |
| 583 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 584 | if (handle == nullptr) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 585 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 586 | exit(1); |
| 587 | } |
| 588 | |
| 589 | // close write end of child_done_pipe to signal the parent that we're done. |
| 590 | close(child_done_pipe[1]); |
| 591 | |
| 592 | // wait for the parent to close parent_done_pipe, then exit |
| 593 | read(parent_done_pipe[0], &read_buf, 1); |
| 594 | exit(0); |
| 595 | } |
| 596 | |
| 597 | ASSERT_NOERROR(child); |
| 598 | |
| 599 | // close the 'wrong' ends of the pipes in the parent |
| 600 | close(child_done_pipe[1]); |
| 601 | close(parent_done_pipe[0]); |
| 602 | |
| 603 | // wait for the child to be done |
| 604 | read(child_done_pipe[0], &read_buf, 1); |
| 605 | close(child_done_pipe[0]); |
| 606 | |
| 607 | // save the child's pid and the parent_done_pipe |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 608 | child_pids[i] = child; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 609 | childpipe[i] = parent_done_pipe[1]; |
| 610 | } |
| 611 | |
| 612 | // Sum the PSS of all the children |
| 613 | size_t total_pss = 0; |
| 614 | for (int i=0; i<CHILDREN; ++i) { |
| 615 | size_t child_pss; |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 616 | ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss)); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 617 | total_pss += child_pss; |
| 618 | } |
| 619 | *pss_out = total_pss; |
| 620 | |
| 621 | // Close pipes and wait for children to exit |
| 622 | for (int i=0; i<CHILDREN; ++i) { |
| 623 | ASSERT_NOERROR(close(childpipe[i])); |
| 624 | } |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 625 | for (int i = 0; i < CHILDREN; ++i) { |
| 626 | AssertChildExited(child_pids[i], 0); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 627 | } |
| 628 | } |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 629 | |
| 630 | // Testing namespaces |
| 631 | static const char* g_public_lib = "libnstest_public.so"; |
| 632 | |
| 633 | TEST(dlext, ns_smoke) { |
| 634 | static const char* root_lib = "libnstest_root.so"; |
| 635 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 636 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 637 | ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr)); |
| 638 | ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: " |
Dimitry Ivanov | 3a6c6b3 | 2016-07-13 16:28:20 -0700 | [diff] [blame] | 639 | "a library with soname \"libnstest_public.so\" was not found in the " |
| 640 | "default namespace", |
| 641 | dlerror()); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 642 | |
Dimitry Ivanov | 5480761 | 2016-04-21 14:57:38 -0700 | [diff] [blame] | 643 | ASSERT_FALSE(android_init_namespaces("", nullptr)); |
| 644 | ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: " |
| 645 | "the list of public libraries is empty.", dlerror()); |
| 646 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 647 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 648 | |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 649 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 650 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 651 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 652 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 653 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 654 | |
| 655 | // Check that libraries added to public namespace are NODELETE |
| 656 | dlclose(handle_public); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 657 | handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), |
| 658 | RTLD_NOW | RTLD_NOLOAD); |
| 659 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 660 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 661 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 662 | android_namespace_t* ns1 = |
| 663 | android_create_namespace("private", nullptr, |
| 664 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 665 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 666 | ASSERT_TRUE(ns1 != nullptr) << dlerror(); |
| 667 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 668 | android_namespace_t* ns2 = |
| 669 | android_create_namespace("private_isolated", nullptr, |
| 670 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 671 | ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr, nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 672 | ASSERT_TRUE(ns2 != nullptr) << dlerror(); |
| 673 | |
| 674 | // This should not have affect search path for default namespace: |
| 675 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 676 | void* handle = dlopen(g_public_lib, RTLD_NOW); |
| 677 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 678 | dlclose(handle); |
| 679 | |
| 680 | android_dlextinfo extinfo; |
| 681 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 682 | extinfo.library_namespace = ns1; |
| 683 | |
| 684 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 685 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 686 | |
| 687 | extinfo.library_namespace = ns2; |
| 688 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 689 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 690 | |
| 691 | ASSERT_TRUE(handle1 != handle2); |
| 692 | |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 693 | // dlopen for a public library using an absolute path should work for isolated namespaces |
| 694 | extinfo.library_namespace = ns2; |
| 695 | handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo); |
| 696 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 697 | ASSERT_TRUE(handle == handle_public); |
| 698 | |
| 699 | dlclose(handle); |
| 700 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 701 | typedef const char* (*fn_t)(); |
| 702 | |
| 703 | fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 704 | ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror(); |
| 705 | fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string")); |
| 706 | ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror(); |
| 707 | |
| 708 | EXPECT_STREQ("This string is local to root library", ns_get_local_string1()); |
| 709 | EXPECT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 710 | |
| 711 | ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2()); |
| 712 | |
| 713 | fn_t ns_get_private_extern_string1 = |
| 714 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 715 | ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror(); |
| 716 | fn_t ns_get_private_extern_string2 = |
| 717 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string")); |
| 718 | ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror(); |
| 719 | |
| 720 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1()); |
| 721 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 722 | |
| 723 | ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2()); |
| 724 | |
| 725 | fn_t ns_get_public_extern_string1 = |
| 726 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 727 | ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror(); |
| 728 | fn_t ns_get_public_extern_string2 = |
| 729 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string")); |
| 730 | ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror(); |
| 731 | |
| 732 | EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1()); |
| 733 | ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2()); |
| 734 | |
| 735 | // and now check that dlopen() does the right thing in terms of preserving namespace |
| 736 | fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 737 | ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror(); |
| 738 | fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string")); |
| 739 | ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror(); |
| 740 | |
| 741 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1()); |
| 742 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 743 | |
| 744 | ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2()); |
| 745 | |
| 746 | dlclose(handle1); |
| 747 | |
| 748 | // Check if handle2 is still alive (and well) |
| 749 | ASSERT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 750 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 751 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2()); |
| 752 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 753 | |
| 754 | dlclose(handle2); |
| 755 | } |
| 756 | |
| 757 | TEST(dlext, ns_isolated) { |
| 758 | static const char* root_lib = "libnstest_root_not_isolated.so"; |
| 759 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 760 | |
| 761 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 762 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 763 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 764 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 765 | |
Dmitriy Ivanov | 3cc35e2 | 2015-11-17 18:36:50 -0800 | [diff] [blame] | 766 | android_set_application_target_sdk_version(42U); // something > 23 |
| 767 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 768 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 769 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 770 | android_namespace_t* ns_not_isolated = |
| 771 | android_create_namespace("private", nullptr, |
| 772 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 773 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 774 | ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror(); |
| 775 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 776 | android_namespace_t* ns_isolated = |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 777 | android_create_namespace("private_isolated1", |
| 778 | nullptr, |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 779 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 780 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 781 | nullptr, |
| 782 | nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 783 | ASSERT_TRUE(ns_isolated != nullptr) << dlerror(); |
| 784 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 785 | android_namespace_t* ns_isolated2 = |
| 786 | android_create_namespace("private_isolated2", |
| 787 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 788 | nullptr, |
| 789 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 790 | lib_path.c_str(), |
| 791 | nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 792 | ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror(); |
| 793 | |
| 794 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 795 | ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror()); |
| 796 | |
| 797 | std::string lib_private_external_path = |
| 798 | lib_path + "/private_namespace_libs_external/libnstest_private_external.so"; |
| 799 | |
| 800 | // Load lib_private_external_path to default namespace |
| 801 | // (it should remain invisible for the isolated namespaces after this) |
| 802 | void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW); |
| 803 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 804 | |
| 805 | android_dlextinfo extinfo; |
| 806 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 807 | extinfo.library_namespace = ns_not_isolated; |
| 808 | |
| 809 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 810 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 811 | |
| 812 | extinfo.library_namespace = ns_isolated; |
| 813 | |
| 814 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 815 | ASSERT_TRUE(handle2 == nullptr); |
| 816 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 817 | |
| 818 | // Check dlopen by absolute path |
| 819 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 820 | ASSERT_TRUE(handle2 == nullptr); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 821 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed" |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 822 | " or dlopened by \"" + get_executable_path() + "\" is not accessible" |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 823 | " for the namespace \"private_isolated1\"", dlerror()); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 824 | |
| 825 | extinfo.library_namespace = ns_isolated2; |
| 826 | |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 827 | // this should work because isolation_path for private_isolated2 includes lib_path |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 828 | handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 829 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 830 | dlclose(handle2); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 831 | |
| 832 | // Check dlopen by absolute path |
| 833 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 834 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 835 | dlclose(handle2); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 836 | |
| 837 | typedef const char* (*fn_t)(); |
| 838 | fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 839 | ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror(); |
| 840 | |
| 841 | ASSERT_STREQ("This string is local to root library", ns_get_local_string()); |
| 842 | |
| 843 | fn_t ns_get_private_extern_string = |
| 844 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 845 | ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror(); |
| 846 | |
| 847 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string()); |
| 848 | |
| 849 | fn_t ns_get_public_extern_string = |
| 850 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 851 | ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror(); |
| 852 | |
| 853 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string()); |
| 854 | |
| 855 | fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 856 | ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror(); |
| 857 | |
| 858 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string()); |
| 859 | |
| 860 | dlclose(handle1); |
| 861 | } |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 862 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 863 | TEST(dlext, ns_shared) { |
| 864 | static const char* root_lib = "libnstest_root_not_isolated.so"; |
| 865 | static const char* root_lib_isolated = "libnstest_root.so"; |
| 866 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 867 | |
| 868 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 869 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 870 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 871 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 872 | |
| 873 | android_set_application_target_sdk_version(42U); // something > 23 |
| 874 | |
| 875 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
| 876 | |
| 877 | // preload this library to the default namespace to check if it |
| 878 | // is shared later on. |
| 879 | void* handle_dlopened = |
| 880 | dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW); |
| 881 | ASSERT_TRUE(handle_dlopened != nullptr) << dlerror(); |
| 882 | |
| 883 | android_namespace_t* ns_not_isolated = |
| 884 | android_create_namespace("private", nullptr, |
| 885 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 886 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 887 | ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror(); |
| 888 | |
| 889 | android_namespace_t* ns_isolated_shared = |
| 890 | android_create_namespace("private_isolated_shared", nullptr, |
| 891 | (lib_path + "/private_namespace_libs").c_str(), |
| 892 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED, |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 893 | nullptr, nullptr); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 894 | ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror(); |
| 895 | |
| 896 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 897 | ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror()); |
| 898 | |
| 899 | std::string lib_private_external_path = |
| 900 | lib_path + "/private_namespace_libs_external/libnstest_private_external.so"; |
| 901 | |
| 902 | // Load lib_private_external_path to default namespace |
| 903 | // (it should remain invisible for the isolated namespaces after this) |
| 904 | void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW); |
| 905 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 906 | |
| 907 | android_dlextinfo extinfo; |
| 908 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 909 | extinfo.library_namespace = ns_not_isolated; |
| 910 | |
| 911 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 912 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 913 | |
| 914 | extinfo.library_namespace = ns_isolated_shared; |
| 915 | |
| 916 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 917 | ASSERT_TRUE(handle2 == nullptr); |
| 918 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 919 | |
| 920 | // Check dlopen by absolute path |
| 921 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 922 | ASSERT_TRUE(handle2 == nullptr); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 923 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed" |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 924 | " or dlopened by \"" + get_executable_path() + "\" is not accessible" |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 925 | " for the namespace \"private_isolated_shared\"", dlerror()); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 926 | |
| 927 | // load libnstest_root.so to shared namespace in order to check that everything is different |
| 928 | // except shared libnstest_dlopened.so |
| 929 | |
| 930 | handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo); |
| 931 | |
| 932 | typedef const char* (*fn_t)(); |
| 933 | fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 934 | ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror(); |
| 935 | fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string")); |
| 936 | ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror(); |
| 937 | |
| 938 | ASSERT_STREQ("This string is local to root library", ns_get_local_string()); |
| 939 | ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared()); |
| 940 | ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared()); |
| 941 | |
| 942 | fn_t ns_get_private_extern_string = |
| 943 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 944 | ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror(); |
| 945 | fn_t ns_get_private_extern_string_shared = |
| 946 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string")); |
| 947 | ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror(); |
| 948 | |
| 949 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string()); |
| 950 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared()); |
| 951 | ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared()); |
| 952 | |
| 953 | fn_t ns_get_public_extern_string = |
| 954 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 955 | ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror(); |
| 956 | fn_t ns_get_public_extern_string_shared = |
| 957 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string")); |
| 958 | ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror(); |
| 959 | |
| 960 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string()); |
| 961 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared()); |
| 962 | ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared()); |
| 963 | |
| 964 | fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 965 | ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror(); |
| 966 | fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string")); |
| 967 | ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror(); |
| 968 | const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string")); |
| 969 | ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror(); |
| 970 | |
| 971 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string()); |
| 972 | ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string); |
| 973 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared()); |
| 974 | ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared()); |
| 975 | ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared()); |
| 976 | |
| 977 | dlclose(handle1); |
| 978 | dlclose(handle2); |
| 979 | } |
| 980 | |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 981 | TEST(dlext, ns_shared_dlclose) { |
| 982 | std::string path = "libc.so:libc++.so:libdl.so:libm.so"; |
| 983 | |
| 984 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 985 | |
| 986 | android_set_application_target_sdk_version(42U); // something > 23 |
| 987 | |
| 988 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
| 989 | |
| 990 | // preload this library to the default namespace to check if it |
| 991 | // is shared later on. |
| 992 | void* handle_dlopened = |
| 993 | dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW); |
| 994 | ASSERT_TRUE(handle_dlopened != nullptr) << dlerror(); |
| 995 | |
| 996 | android_namespace_t* ns_isolated_shared = |
| 997 | android_create_namespace("private_isolated_shared", nullptr, |
| 998 | (lib_path + "/private_namespace_libs").c_str(), |
| 999 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED, |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1000 | nullptr, nullptr); |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1001 | ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror(); |
| 1002 | |
| 1003 | // Check if "libnstest_dlopened.so" is loaded (and the same) |
| 1004 | android_dlextinfo extinfo; |
| 1005 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1006 | extinfo.library_namespace = ns_isolated_shared; |
| 1007 | |
| 1008 | void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1009 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1010 | ASSERT_TRUE(handle == handle_dlopened); |
| 1011 | dlclose(handle); |
| 1012 | dlclose(handle_dlopened); |
| 1013 | |
| 1014 | // And now check that the library cannot be found by soname (and is no longer loaded) |
| 1015 | handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1016 | ASSERT_TRUE(handle == nullptr) |
| 1017 | << "Error: libnstest_dlopened.so is still accessible in shared namespace"; |
| 1018 | |
| 1019 | handle = android_dlopen_ext((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), |
| 1020 | RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1021 | ASSERT_TRUE(handle == nullptr) |
| 1022 | << "Error: libnstest_dlopened.so is still accessible in shared namespace"; |
| 1023 | |
| 1024 | handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD); |
| 1025 | ASSERT_TRUE(handle == nullptr) |
| 1026 | << "Error: libnstest_dlopened.so is still accessible in default namespace"; |
| 1027 | |
| 1028 | handle = dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), |
| 1029 | RTLD_NOW | RTLD_NOLOAD); |
| 1030 | ASSERT_TRUE(handle == nullptr) |
| 1031 | << "Error: libnstest_dlopened.so is still accessible in default namespace"; |
| 1032 | |
| 1033 | // Now lets see if the soinfo area gets reused in the wrong way: |
| 1034 | // load a library to default namespace. |
| 1035 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 1036 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 1037 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1038 | |
| 1039 | // try to find it in shared namespace |
| 1040 | handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1041 | ASSERT_TRUE(handle == nullptr) |
| 1042 | << "Error: " << g_public_lib << " is accessible in shared namespace"; |
| 1043 | } |
| 1044 | |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1045 | TEST(dlext, ns_isolated_rtld_global) { |
| 1046 | static const char* root_lib = "libnstest_root.so"; |
| 1047 | std::string path = "libc.so:libc++.so:libdl.so:libm.so"; |
| 1048 | |
| 1049 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)); |
| 1050 | |
| 1051 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 1052 | |
| 1053 | const std::string lib_public_path = lib_path + "/public_namespace_libs"; |
| 1054 | |
| 1055 | android_namespace_t* ns1 = |
| 1056 | android_create_namespace("isolated1", |
| 1057 | nullptr, |
| 1058 | (lib_path + "/private_namespace_libs").c_str(), |
| 1059 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1060 | lib_public_path.c_str(), |
| 1061 | nullptr); |
| 1062 | ASSERT_TRUE(ns1 != nullptr) << dlerror(); |
| 1063 | |
| 1064 | android_namespace_t* ns2 = |
| 1065 | android_create_namespace("isolated2", |
| 1066 | nullptr, |
| 1067 | (lib_path + "/private_namespace_libs").c_str(), |
| 1068 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1069 | lib_public_path.c_str(), |
| 1070 | nullptr); |
| 1071 | ASSERT_TRUE(ns2 != nullptr) << dlerror(); |
| 1072 | |
| 1073 | android_dlextinfo extinfo; |
| 1074 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1075 | extinfo.library_namespace = ns1; |
| 1076 | |
| 1077 | void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(), |
| 1078 | RTLD_GLOBAL, |
| 1079 | &extinfo); |
| 1080 | |
| 1081 | ASSERT_TRUE(handle_global != nullptr) << dlerror(); |
| 1082 | |
| 1083 | android_namespace_t* ns1_child = |
| 1084 | android_create_namespace("isolated1_child", |
| 1085 | nullptr, |
| 1086 | (lib_path + "/private_namespace_libs").c_str(), |
| 1087 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1088 | nullptr, |
| 1089 | ns1); |
| 1090 | |
| 1091 | // Now - only ns1 and ns1 child should be able to dlopen root_lib |
| 1092 | // attempt to use ns2 should result in dlerror() |
| 1093 | |
| 1094 | // Check ns1_child first. |
| 1095 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1096 | extinfo.library_namespace = ns1_child; |
| 1097 | |
| 1098 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1099 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 1100 | |
| 1101 | // now ns1 |
| 1102 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1103 | extinfo.library_namespace = ns1; |
| 1104 | |
| 1105 | handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1106 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 1107 | |
| 1108 | // and ns2 should fail |
| 1109 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1110 | extinfo.library_namespace = ns2; |
| 1111 | |
| 1112 | handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1113 | ASSERT_TRUE(handle1 == nullptr); |
| 1114 | ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror()); |
| 1115 | } |
| 1116 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1117 | TEST(dlext, ns_anonymous) { |
| 1118 | static const char* root_lib = "libnstest_root.so"; |
| 1119 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 1120 | |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 1121 | std::string data_path; |
| 1122 | ASSERT_TRUE(get_realpath(std::string(getenv("ANDROID_DATA")), &data_path)) << strerror(errno); |
| 1123 | const std::string lib_path = data_path + NATIVE_TESTS_PATH; |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1124 | |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 1125 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 1126 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 1127 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1128 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1129 | |
| 1130 | ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str())) |
| 1131 | << dlerror(); |
| 1132 | |
| 1133 | android_namespace_t* ns = android_create_namespace( |
| 1134 | "private", nullptr, |
| 1135 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1136 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, nullptr); |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1137 | |
| 1138 | ASSERT_TRUE(ns != nullptr) << dlerror(); |
| 1139 | |
| 1140 | std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib; |
| 1141 | |
| 1142 | android_dlextinfo extinfo; |
| 1143 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1144 | extinfo.library_namespace = ns; |
| 1145 | |
| 1146 | // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string |
| 1147 | void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo); |
| 1148 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1149 | |
| 1150 | uintptr_t ns_get_dlopened_string_addr = |
| 1151 | reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string")); |
| 1152 | ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror(); |
| 1153 | typedef const char* (*fn_t)(); |
| 1154 | fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr); |
| 1155 | |
| 1156 | std::vector<map_record> maps; |
| 1157 | Maps::parse_maps(&maps); |
| 1158 | |
| 1159 | uintptr_t addr_start = 0; |
| 1160 | uintptr_t addr_end = 0; |
| 1161 | std::vector<map_record> maps_to_copy; |
| 1162 | |
| 1163 | for (const auto& rec : maps) { |
| 1164 | if (rec.pathname == private_library_absolute_path) { |
| 1165 | if (addr_start == 0) { |
| 1166 | addr_start = rec.addr_start; |
| 1167 | } |
| 1168 | addr_end = rec.addr_end; |
| 1169 | |
| 1170 | maps_to_copy.push_back(rec); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | // some sanity checks.. |
| 1175 | ASSERT_TRUE(addr_start > 0); |
| 1176 | ASSERT_TRUE(addr_end > 0); |
| 1177 | ASSERT_EQ(3U, maps_to_copy.size()); |
| 1178 | ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start); |
| 1179 | ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end); |
| 1180 | |
| 1181 | // copy |
| 1182 | uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start, |
| 1183 | PROT_NONE, MAP_ANON | MAP_PRIVATE, |
| 1184 | -1, 0)); |
| 1185 | ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED); |
| 1186 | |
| 1187 | for (const auto& rec : maps_to_copy) { |
| 1188 | uintptr_t offset = rec.addr_start - addr_start; |
| 1189 | size_t size = rec.addr_end - rec.addr_start; |
| 1190 | void* addr = reinterpret_cast<void*>(reserved_addr + offset); |
| 1191 | void* map = mmap(addr, size, PROT_READ | PROT_WRITE, |
| 1192 | MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0); |
| 1193 | ASSERT_TRUE(map != MAP_FAILED); |
| 1194 | memcpy(map, reinterpret_cast<void*>(rec.addr_start), size); |
| 1195 | mprotect(map, size, rec.perms); |
| 1196 | } |
| 1197 | |
| 1198 | // call the function copy |
| 1199 | uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start; |
| 1200 | fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset); |
| 1201 | ASSERT_STREQ("This string is from private namespace (dlopened library)", |
| 1202 | ns_get_dlopened_string_anon()); |
| 1203 | |
| 1204 | // They should belong to different namespaces (private and anonymous) |
| 1205 | ASSERT_STREQ("This string is from private namespace (dlopened library)", |
| 1206 | ns_get_dlopened_string_private()); |
| 1207 | |
| 1208 | ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private()); |
| 1209 | } |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 1210 | |
| 1211 | TEST(dlext, dlopen_handle_value_platform) { |
| 1212 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); |
| 1213 | ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0) |
| 1214 | << "dlopen should return odd value for the handle"; |
| 1215 | dlclose(handle); |
| 1216 | } |
| 1217 | |
| 1218 | TEST(dlext, dlopen_handle_value_app_compat) { |
| 1219 | android_set_application_target_sdk_version(23); |
| 1220 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); |
| 1221 | ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0) |
| 1222 | << "dlopen should return valid pointer"; |
| 1223 | dlclose(handle); |
| 1224 | } |
| 1225 | |