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> |
Dimitry Ivanov | bf34ba3 | 2017-04-21 13:12:05 -0700 | [diff] [blame] | 27 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 28 | #include <android/dlext.h> |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Dimitry Ivanov | bf34ba3 | 2017-04-21 13:12:05 -0700 | [diff] [blame] | 30 | |
| 31 | #include <linux/memfd.h> |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 32 | #include <sys/mman.h> |
Elliott Hughes | d7c5262 | 2017-06-20 14:26:56 -0700 | [diff] [blame] | 33 | #include <sys/syscall.h> |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 34 | #include <sys/types.h> |
Dimitry Ivanov | bf34ba3 | 2017-04-21 13:12:05 -0700 | [diff] [blame] | 35 | #include <sys/vfs.h> |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 36 | #include <sys/wait.h> |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 37 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 38 | #include <pagemap/pagemap.h> |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 39 | #include <ziparchive/zip_archive.h> |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 40 | |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 41 | #include "gtest_globals.h" |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 42 | #include "TemporaryFile.h" |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 43 | #include "utils.h" |
Dimitry Ivanov | 41fd295 | 2016-05-09 17:37:39 -0700 | [diff] [blame] | 44 | #include "dlext_private.h" |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 45 | #include "dlfcn_symlink_support.h" |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 46 | |
| 47 | #define ASSERT_DL_NOTNULL(ptr) \ |
Chih-Hung Hsieh | d61ca37 | 2016-06-03 10:18:07 -0700 | [diff] [blame] | 48 | ASSERT_TRUE((ptr) != nullptr) << "dlerror: " << dlerror() |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 49 | |
| 50 | #define ASSERT_DL_ZERO(i) \ |
| 51 | ASSERT_EQ(0, i) << "dlerror: " << dlerror() |
| 52 | |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 53 | #define ASSERT_NOERROR(i) \ |
| 54 | ASSERT_NE(-1, i) << "errno: " << strerror(errno) |
| 55 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 56 | #define ASSERT_SUBSTR(needle, haystack) \ |
| 57 | ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack) |
| 58 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 59 | |
| 60 | typedef int (*fn)(void); |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 61 | constexpr const char* kLibName = "libdlext_test.so"; |
| 62 | constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so"; |
| 63 | constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so"; |
| 64 | constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 65 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 66 | class DlExtTest : public ::testing::Test { |
| 67 | protected: |
| 68 | virtual void SetUp() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 69 | handle_ = nullptr; |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 70 | // verify that we don't have the library loaded already |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 71 | void* h = dlopen(kLibName, RTLD_NOW | RTLD_NOLOAD); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 72 | ASSERT_TRUE(h == nullptr); |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 73 | h = dlopen(kLibNameNoRelro, RTLD_NOW | RTLD_NOLOAD); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 74 | ASSERT_TRUE(h == nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 75 | // call dlerror() to swallow the error, and check it was the one we wanted |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 76 | ASSERT_EQ(std::string("dlopen failed: library \"") + kLibNameNoRelro + "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | virtual void TearDown() { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 80 | if (handle_ != nullptr) { |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 81 | ASSERT_DL_ZERO(dlclose(handle_)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void* handle_; |
| 86 | }; |
| 87 | |
| 88 | TEST_F(DlExtTest, ExtInfoNull) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 89 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, nullptr); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 90 | ASSERT_DL_NOTNULL(handle_); |
| 91 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 92 | ASSERT_DL_NOTNULL(f); |
| 93 | EXPECT_EQ(4, f()); |
| 94 | } |
| 95 | |
| 96 | TEST_F(DlExtTest, ExtInfoNoFlags) { |
| 97 | android_dlextinfo extinfo; |
| 98 | extinfo.flags = 0; |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 99 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 100 | ASSERT_DL_NOTNULL(handle_); |
| 101 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 102 | ASSERT_DL_NOTNULL(f); |
| 103 | EXPECT_EQ(4, f()); |
| 104 | } |
| 105 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 106 | TEST_F(DlExtTest, ExtInfoUseFd) { |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 107 | const std::string lib_path = get_testlib_root() + "/libdlext_test_fd/libdlext_test_fd.so"; |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 108 | |
| 109 | android_dlextinfo extinfo; |
| 110 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 111 | 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] | 112 | ASSERT_TRUE(extinfo.library_fd != -1); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 113 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 114 | ASSERT_DL_NOTNULL(handle_); |
| 115 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 116 | ASSERT_DL_NOTNULL(f); |
| 117 | EXPECT_EQ(4, f()); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 118 | |
| 119 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 120 | ASSERT_DL_NOTNULL(taxicab_number); |
| 121 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 124 | TEST_F(DlExtTest, ExtInfoUseFdWithOffset) { |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 125 | const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 126 | |
| 127 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 128 | 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] | 129 | 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] | 130 | |
| 131 | // Find the offset of the shared library in the zip. |
| 132 | ZipArchiveHandle handle; |
| 133 | ASSERT_EQ(0, OpenArchive(lib_path.c_str(), &handle)); |
| 134 | ZipEntry zip_entry; |
| 135 | ZipString zip_name; |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 136 | zip_name.name = reinterpret_cast<const uint8_t*>(kLibZipSimpleZip); |
| 137 | zip_name.name_length = strlen(kLibZipSimpleZip); |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 138 | ASSERT_EQ(0, FindEntry(handle, zip_name, &zip_entry)); |
| 139 | extinfo.library_fd_offset = zip_entry.offset; |
| 140 | CloseArchive(handle); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 141 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 142 | handle_ = android_dlopen_ext(lib_path.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 143 | ASSERT_DL_NOTNULL(handle_); |
| 144 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 145 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
| 146 | ASSERT_DL_NOTNULL(taxicab_number); |
| 147 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TEST_F(DlExtTest, ExtInfoUseFdWithInvalidOffset) { |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 151 | const std::string lib_path = get_testlib_root() + "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 152 | |
| 153 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 154 | 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] | 155 | 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] | 156 | extinfo.library_fd_offset = 17; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 157 | |
| 158 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 159 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 160 | ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror()); |
| 161 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 162 | // Test an address above 2^44, for http://b/18178121 . |
| 163 | extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 164 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 165 | ASSERT_TRUE(handle_ == nullptr); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 166 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror()); |
| 167 | |
| 168 | extinfo.library_fd_offset = 0LL - PAGE_SIZE; |
| 169 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 170 | ASSERT_TRUE(handle_ == nullptr); |
| 171 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror()); |
| 172 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 173 | extinfo.library_fd_offset = 0; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 174 | handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 175 | ASSERT_TRUE(handle_ == nullptr); |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 176 | ASSERT_EQ("dlopen failed: \"" + lib_path + "\" has bad ELF magic", dlerror()); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 177 | |
Dmitriy Ivanov | fd7a91e | 2015-11-06 10:44:37 -0800 | [diff] [blame] | 178 | // Check if dlsym works after unsuccessful dlopen(). |
| 179 | // Supply non-exiting one to make linker visit every soinfo. |
| 180 | void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___"); |
| 181 | ASSERT_TRUE(sym == nullptr); |
| 182 | |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 183 | close(extinfo.library_fd); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 186 | TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 187 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 188 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 189 | // This offset will not be used, so it doesn't matter. |
| 190 | extinfo.library_fd_offset = 0; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 191 | |
| 192 | handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo); |
| 193 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 194 | 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] | 195 | } |
| 196 | |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 197 | TEST(dlext, android_dlopen_ext_force_load_smoke) { |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 198 | DlfcnSymlink symlink("android_dlopen_ext_force_load_smoke"); |
| 199 | const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 200 | // 1. Open actual file |
| 201 | void* handle = dlopen("libdlext_test.so", RTLD_NOW); |
| 202 | ASSERT_DL_NOTNULL(handle); |
| 203 | // 2. Open link with force_load flag set |
| 204 | android_dlextinfo extinfo; |
| 205 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 206 | void* handle2 = android_dlopen_ext(symlink_name.c_str(), RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 207 | ASSERT_DL_NOTNULL(handle2); |
| 208 | ASSERT_TRUE(handle != handle2); |
| 209 | |
| 210 | dlclose(handle2); |
| 211 | dlclose(handle); |
| 212 | } |
| 213 | |
| 214 | TEST(dlext, android_dlopen_ext_force_load_soname_exception) { |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 215 | DlfcnSymlink symlink("android_dlopen_ext_force_load_soname_exception"); |
| 216 | const std::string symlink_name = basename(symlink.get_symlink_path().c_str()); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 217 | // Check if soname lookup still returns already loaded library |
| 218 | // when ANDROID_DLEXT_FORCE_LOAD flag is specified. |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 219 | void* handle = dlopen(symlink_name.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 220 | ASSERT_DL_NOTNULL(handle); |
| 221 | |
| 222 | android_dlextinfo extinfo; |
| 223 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 224 | |
Dimitry Ivanov | 708589f | 2016-09-19 10:50:28 -0700 | [diff] [blame] | 225 | // Note that 'libdlext_test.so' is dt_soname for the symlink_name |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 226 | void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo); |
| 227 | |
| 228 | ASSERT_DL_NOTNULL(handle2); |
| 229 | ASSERT_TRUE(handle == handle2); |
| 230 | |
| 231 | dlclose(handle2); |
| 232 | dlclose(handle); |
| 233 | } |
| 234 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 235 | TEST(dlfcn, dlopen_from_zip_absolute_path) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 236 | const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"; |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 237 | const std::string lib_path = get_testlib_root() + lib_zip_path; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 238 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 239 | 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] | 240 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 241 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 242 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 243 | ASSERT_DL_NOTNULL(taxicab_number); |
| 244 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 245 | |
| 246 | dlclose(handle); |
| 247 | } |
| 248 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 249 | TEST(dlfcn, dlopen_from_zip_with_dt_runpath) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 250 | const std::string lib_zip_path = "/libdlext_test_runpath_zip/libdlext_test_runpath_zip_zipaligned.zip"; |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 251 | const std::string lib_path = get_testlib_root() + lib_zip_path; |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 252 | |
| 253 | void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW); |
| 254 | |
| 255 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 256 | |
| 257 | typedef void *(* dlopen_b_fn)(); |
| 258 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); |
| 259 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 260 | |
| 261 | void *p = fn(); |
| 262 | ASSERT_TRUE(p != nullptr) << dlerror(); |
| 263 | |
| 264 | dlclose(p); |
| 265 | dlclose(handle); |
| 266 | } |
| 267 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 268 | TEST(dlfcn, dlopen_from_zip_ld_library_path) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 269 | const std::string lib_zip_path = "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip"; |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 270 | const std::string lib_path = get_testlib_root() + lib_zip_path + "!/libdir"; |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 271 | |
| 272 | typedef void (*fn_t)(const char*); |
| 273 | fn_t android_update_LD_LIBRARY_PATH = |
| 274 | reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH")); |
| 275 | |
| 276 | ASSERT_TRUE(android_update_LD_LIBRARY_PATH != nullptr) << dlerror(); |
| 277 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 278 | void* handle = dlopen("libdlext_test_zip.so", RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 279 | ASSERT_TRUE(handle == nullptr); |
| 280 | |
| 281 | android_update_LD_LIBRARY_PATH(lib_path.c_str()); |
| 282 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 283 | handle = dlopen("libdlext_test_zip.so", RTLD_NOW); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 284 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 285 | |
| 286 | int (*fn)(void); |
| 287 | fn = reinterpret_cast<int (*)(void)>(dlsym(handle, "getRandomNumber")); |
| 288 | ASSERT_TRUE(fn != nullptr); |
| 289 | EXPECT_EQ(4, fn()); |
| 290 | |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 291 | uint32_t* taxicab_number = |
| 292 | reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 293 | ASSERT_DL_NOTNULL(taxicab_number); |
| 294 | EXPECT_EQ(1729U, *taxicab_number); |
| 295 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 296 | dlclose(handle); |
| 297 | } |
| 298 | |
| 299 | |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 300 | TEST_F(DlExtTest, Reserved) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 301 | void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 302 | ASSERT_TRUE(start != MAP_FAILED); |
| 303 | android_dlextinfo extinfo; |
| 304 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 305 | extinfo.reserved_addr = start; |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 306 | extinfo.reserved_size = kLibSize; |
| 307 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 308 | ASSERT_DL_NOTNULL(handle_); |
| 309 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 310 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 311 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 312 | EXPECT_LT(reinterpret_cast<void*>(f), |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 313 | reinterpret_cast<char*>(start) + kLibSize); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 314 | EXPECT_EQ(4, f()); |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 315 | |
| 316 | // Check that after dlclose reserved address space is unmapped (and can be reused) |
| 317 | dlclose(handle_); |
| 318 | handle_ = nullptr; |
| 319 | |
| 320 | void* new_start = mmap(start, PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 321 | ASSERT_NE(start, new_start) << "dlclose unmapped reserved space"; |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | TEST_F(DlExtTest, ReservedTooSmall) { |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 325 | 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] | 326 | ASSERT_TRUE(start != MAP_FAILED); |
| 327 | android_dlextinfo extinfo; |
| 328 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 329 | extinfo.reserved_addr = start; |
| 330 | extinfo.reserved_size = PAGE_SIZE; |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 331 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 332 | EXPECT_EQ(nullptr, handle_); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | TEST_F(DlExtTest, ReservedHint) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 336 | void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 337 | ASSERT_TRUE(start != MAP_FAILED); |
| 338 | android_dlextinfo extinfo; |
| 339 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 340 | extinfo.reserved_addr = start; |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 341 | extinfo.reserved_size = kLibSize; |
| 342 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 343 | ASSERT_DL_NOTNULL(handle_); |
| 344 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 345 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 346 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 347 | EXPECT_LT(reinterpret_cast<void*>(f), |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 348 | reinterpret_cast<char*>(start) + kLibSize); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 349 | EXPECT_EQ(4, f()); |
| 350 | } |
| 351 | |
| 352 | TEST_F(DlExtTest, ReservedHintTooSmall) { |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 353 | 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] | 354 | ASSERT_TRUE(start != MAP_FAILED); |
| 355 | android_dlextinfo extinfo; |
| 356 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 357 | extinfo.reserved_addr = start; |
| 358 | extinfo.reserved_size = PAGE_SIZE; |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 359 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 360 | ASSERT_DL_NOTNULL(handle_); |
| 361 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 362 | ASSERT_DL_NOTNULL(f); |
Chih-Hung Hsieh | a2c6ae6 | 2014-08-27 13:45:37 -0700 | [diff] [blame] | 363 | EXPECT_TRUE(reinterpret_cast<void*>(f) < start || |
| 364 | (reinterpret_cast<void*>(f) >= |
| 365 | reinterpret_cast<char*>(start) + PAGE_SIZE)); |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 366 | EXPECT_EQ(4, f()); |
| 367 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 368 | |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 369 | TEST_F(DlExtTest, LoadAtFixedAddress) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 370 | void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 371 | ASSERT_TRUE(start != MAP_FAILED); |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 372 | munmap(start, kLibSize); |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 373 | |
| 374 | android_dlextinfo extinfo; |
| 375 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 376 | extinfo.reserved_addr = start; |
| 377 | |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 378 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 379 | ASSERT_DL_NOTNULL(handle_); |
| 380 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 381 | ASSERT_DL_NOTNULL(f); |
| 382 | EXPECT_GE(reinterpret_cast<void*>(f), start); |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 383 | EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + kLibSize); |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 384 | |
| 385 | EXPECT_EQ(4, f()); |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 386 | dlclose(handle_); |
| 387 | handle_ = nullptr; |
| 388 | |
| 389 | // Check that dlclose unmapped the file |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 390 | void* addr = mmap(start, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 391 | ASSERT_EQ(start, addr) << "dlclose did not unmap the memory"; |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | TEST_F(DlExtTest, LoadAtFixedAddressTooSmall) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 395 | void* start = mmap(nullptr, kLibSize + PAGE_SIZE, PROT_NONE, |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 396 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 397 | ASSERT_TRUE(start != MAP_FAILED); |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 398 | munmap(start, kLibSize + PAGE_SIZE); |
| 399 | void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, kLibSize, PROT_NONE, |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 400 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 401 | ASSERT_TRUE(new_addr != MAP_FAILED); |
| 402 | |
| 403 | android_dlextinfo extinfo; |
| 404 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 405 | extinfo.reserved_addr = start; |
| 406 | |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 407 | handle_ = android_dlopen_ext(kLibName, RTLD_NOW, &extinfo); |
Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 408 | ASSERT_TRUE(handle_ == nullptr); |
| 409 | } |
| 410 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 411 | class DlExtRelroSharingTest : public DlExtTest { |
| 412 | protected: |
| 413 | virtual void SetUp() { |
| 414 | DlExtTest::SetUp(); |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 415 | void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 416 | ASSERT_TRUE(start != MAP_FAILED); |
| 417 | extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 418 | extinfo_.reserved_addr = start; |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 419 | extinfo_.reserved_size = kLibSize; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 420 | extinfo_.relro_fd = -1; |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 423 | virtual void TearDown() { |
| 424 | DlExtTest::TearDown(); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 425 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 426 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 427 | void CreateRelroFile(const char* lib, const char* relro_file) { |
| 428 | int relro_fd = open(relro_file, O_RDWR | O_TRUNC); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 429 | ASSERT_NOERROR(relro_fd); |
| 430 | |
| 431 | pid_t pid = fork(); |
| 432 | if (pid == 0) { |
| 433 | // child process |
| 434 | extinfo_.flags |= ANDROID_DLEXT_WRITE_RELRO; |
| 435 | extinfo_.relro_fd = relro_fd; |
| 436 | void* handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 437 | if (handle == nullptr) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 438 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 439 | exit(1); |
| 440 | } |
| 441 | exit(0); |
| 442 | } |
| 443 | |
| 444 | // continuing in parent |
| 445 | ASSERT_NOERROR(close(relro_fd)); |
| 446 | ASSERT_NOERROR(pid); |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 447 | AssertChildExited(pid, 0); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 448 | |
| 449 | // reopen file for reading so it can be used |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 450 | relro_fd = open(relro_file, O_RDONLY); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 451 | ASSERT_NOERROR(relro_fd); |
| 452 | extinfo_.flags |= ANDROID_DLEXT_USE_RELRO; |
| 453 | extinfo_.relro_fd = relro_fd; |
| 454 | } |
| 455 | |
| 456 | void TryUsingRelro(const char* lib) { |
| 457 | handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 458 | ASSERT_DL_NOTNULL(handle_); |
| 459 | fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber")); |
| 460 | ASSERT_DL_NOTNULL(f); |
| 461 | EXPECT_EQ(4, f()); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 462 | |
Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 463 | uint32_t* taxicab_number = |
| 464 | reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number")); |
Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 465 | ASSERT_DL_NOTNULL(taxicab_number); |
| 466 | EXPECT_EQ(1729U, *taxicab_number); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 469 | void SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file, bool share_relro, |
| 470 | size_t* pss_out); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 471 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 472 | android_dlextinfo extinfo_; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 476 | TemporaryFile tf; // Use tf to get an unique filename. |
| 477 | ASSERT_NOERROR(close(tf.fd)); |
| 478 | |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 479 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename)); |
| 480 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 481 | |
| 482 | // Use destructor of tf to close and unlink the file. |
| 483 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 487 | TemporaryFile tf; // // Use tf to get an unique filename. |
| 488 | ASSERT_NOERROR(close(tf.fd)); |
| 489 | |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 490 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.filename)); |
| 491 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 492 | |
| 493 | // Use destructor of tf to close and unlink the file. |
| 494 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | TEST_F(DlExtRelroSharingTest, RelroFileEmpty) { |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 498 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName)); |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 499 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 500 | |
| 501 | TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) { |
Dan Albert | 69fb9f3 | 2014-09-03 11:30:21 -0700 | [diff] [blame] | 502 | if (geteuid() != 0) { |
| 503 | GTEST_LOG_(INFO) << "This test must be run as root.\n"; |
| 504 | return; |
| 505 | } |
| 506 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 507 | TemporaryFile tf; // Use tf to get an unique filename. |
| 508 | ASSERT_NOERROR(close(tf.fd)); |
| 509 | |
Dimitry Ivanov | 927877c | 2016-09-21 11:17:13 -0700 | [diff] [blame] | 510 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.filename)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 511 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 512 | int pipefd[2]; |
| 513 | ASSERT_NOERROR(pipe(pipefd)); |
| 514 | |
| 515 | size_t without_sharing, with_sharing; |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 516 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, false, &without_sharing)); |
| 517 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(kLibName, tf.filename, true, &with_sharing)); |
| 518 | ASSERT_LT(with_sharing, without_sharing); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 519 | |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 520 | // We expect the sharing to save at least 50% of the library's total PSS. |
| 521 | // In practice it saves 80%+ for this library in the test. |
| 522 | size_t pss_saved = without_sharing - with_sharing; |
| 523 | size_t expected_min_saved = without_sharing / 2; |
| 524 | |
| 525 | EXPECT_LT(expected_min_saved, pss_saved); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 526 | |
| 527 | // Use destructor of tf to close and unlink the file. |
| 528 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 531 | void GetPss(bool shared_relro, const char* lib, const char* relro_file, pid_t pid, |
| 532 | size_t* total_pss) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 533 | pm_kernel_t* kernel; |
| 534 | ASSERT_EQ(0, pm_kernel_create(&kernel)); |
| 535 | |
| 536 | pm_process_t* process; |
| 537 | ASSERT_EQ(0, pm_process_create(kernel, pid, &process)); |
| 538 | |
| 539 | pm_map_t** maps; |
| 540 | size_t num_maps; |
| 541 | ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps)); |
| 542 | |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 543 | // Calculate total PSS of the library. |
| 544 | *total_pss = 0; |
| 545 | bool saw_relro_file = false; |
| 546 | for (size_t i = 0; i < num_maps; ++i) { |
| 547 | if (android::base::EndsWith(maps[i]->name, lib) || strcmp(maps[i]->name, relro_file) == 0) { |
| 548 | if (strcmp(maps[i]->name, relro_file) == 0) saw_relro_file = true; |
| 549 | |
| 550 | pm_memusage_t usage; |
| 551 | ASSERT_EQ(0, pm_map_usage(maps[i], &usage)); |
| 552 | *total_pss += usage.pss; |
| 553 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 554 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 555 | |
| 556 | free(maps); |
| 557 | pm_process_destroy(process); |
| 558 | pm_kernel_destroy(kernel); |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 559 | |
| 560 | if (shared_relro) ASSERT_TRUE(saw_relro_file); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 563 | void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file, |
| 564 | bool share_relro, size_t* pss_out) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 565 | const int CHILDREN = 20; |
| 566 | |
| 567 | // Create children |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 568 | pid_t child_pids[CHILDREN]; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 569 | int childpipe[CHILDREN]; |
| 570 | for (int i=0; i<CHILDREN; ++i) { |
| 571 | char read_buf; |
| 572 | int child_done_pipe[2], parent_done_pipe[2]; |
| 573 | ASSERT_NOERROR(pipe(child_done_pipe)); |
| 574 | ASSERT_NOERROR(pipe(parent_done_pipe)); |
| 575 | |
| 576 | pid_t child = fork(); |
| 577 | if (child == 0) { |
| 578 | // close the 'wrong' ends of the pipes in the child |
| 579 | close(child_done_pipe[0]); |
| 580 | close(parent_done_pipe[1]); |
| 581 | |
| 582 | // open the library |
| 583 | void* handle; |
| 584 | if (share_relro) { |
| 585 | handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 586 | } else { |
| 587 | handle = dlopen(lib, RTLD_NOW); |
| 588 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 589 | if (handle == nullptr) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 590 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 591 | exit(1); |
| 592 | } |
| 593 | |
| 594 | // close write end of child_done_pipe to signal the parent that we're done. |
| 595 | close(child_done_pipe[1]); |
| 596 | |
| 597 | // wait for the parent to close parent_done_pipe, then exit |
| 598 | read(parent_done_pipe[0], &read_buf, 1); |
| 599 | exit(0); |
| 600 | } |
| 601 | |
| 602 | ASSERT_NOERROR(child); |
| 603 | |
| 604 | // close the 'wrong' ends of the pipes in the parent |
| 605 | close(child_done_pipe[1]); |
| 606 | close(parent_done_pipe[0]); |
| 607 | |
| 608 | // wait for the child to be done |
| 609 | read(child_done_pipe[0], &read_buf, 1); |
| 610 | close(child_done_pipe[0]); |
| 611 | |
| 612 | // save the child's pid and the parent_done_pipe |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 613 | child_pids[i] = child; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 614 | childpipe[i] = parent_done_pipe[1]; |
| 615 | } |
| 616 | |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 617 | // Sum the PSS of tested library of all the children |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 618 | size_t total_pss = 0; |
| 619 | for (int i=0; i<CHILDREN; ++i) { |
| 620 | size_t child_pss; |
Zhenhua WANG | 81aad00 | 2017-04-25 11:07:19 +0800 | [diff] [blame] | 621 | ASSERT_NO_FATAL_FAILURE(GetPss(share_relro, lib, relro_file, child_pids[i], &child_pss)); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 622 | total_pss += child_pss; |
| 623 | } |
| 624 | *pss_out = total_pss; |
| 625 | |
| 626 | // Close pipes and wait for children to exit |
| 627 | for (int i=0; i<CHILDREN; ++i) { |
| 628 | ASSERT_NOERROR(close(childpipe[i])); |
| 629 | } |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 630 | for (int i = 0; i < CHILDREN; ++i) { |
| 631 | AssertChildExited(child_pids[i], 0); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 632 | } |
| 633 | } |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 634 | |
| 635 | // Testing namespaces |
| 636 | static const char* g_public_lib = "libnstest_public.so"; |
| 637 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 638 | // These are libs shared with default namespace |
| 639 | static const std::string g_core_shared_libs = "libc.so:libc++.so:libdl.so:libm.so"; |
| 640 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 641 | TEST(dlext, ns_smoke) { |
| 642 | static const char* root_lib = "libnstest_root.so"; |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 643 | std::string shared_libs = g_core_shared_libs + ":" + g_public_lib; |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 644 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 645 | ASSERT_FALSE(android_init_anonymous_namespace("", nullptr)); |
| 646 | ASSERT_STREQ("android_init_anonymous_namespace failed: error linking namespaces" |
| 647 | " \"(anonymous)\"->\"(default)\": the list of shared libraries is empty.", |
| 648 | dlerror()); |
Dimitry Ivanov | 5480761 | 2016-04-21 14:57:38 -0700 | [diff] [blame] | 649 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 650 | const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib; |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 651 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 652 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 653 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 654 | ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 655 | |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 656 | // Check that libraries added to public namespace are not NODELETE |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 657 | dlclose(handle_public); |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 658 | handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD); |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 659 | ASSERT_TRUE(handle_public == nullptr); |
| 660 | ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path + |
| 661 | "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 662 | |
| 663 | handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 664 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 665 | // create "public namespace", share limited set of public libraries with |
| 666 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 667 | android_namespace_t* ns1 = |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 668 | android_create_namespace("private", |
| 669 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 670 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 671 | ANDROID_NAMESPACE_TYPE_REGULAR, |
| 672 | nullptr, |
| 673 | nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 674 | ASSERT_TRUE(ns1 != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 675 | ASSERT_TRUE(android_link_namespaces(ns1, nullptr, shared_libs.c_str())) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 676 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 677 | android_namespace_t* ns2 = |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 678 | android_create_namespace("private_isolated", |
| 679 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 680 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 681 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 682 | nullptr, |
| 683 | nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 684 | ASSERT_TRUE(ns2 != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 685 | ASSERT_TRUE(android_link_namespaces(ns2, nullptr, shared_libs.c_str())) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 686 | |
| 687 | // This should not have affect search path for default namespace: |
| 688 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 689 | void* handle = dlopen(g_public_lib, RTLD_NOW); |
| 690 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 691 | dlclose(handle); |
| 692 | |
Dimitry Ivanov | d3e7d08 | 2017-03-27 14:11:02 -0700 | [diff] [blame] | 693 | // dlopen for a public library using an absolute path should work |
| 694 | // 1. For isolated namespaces |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 695 | android_dlextinfo extinfo; |
| 696 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
Dimitry Ivanov | d3e7d08 | 2017-03-27 14:11:02 -0700 | [diff] [blame] | 697 | extinfo.library_namespace = ns2; |
| 698 | handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo); |
| 699 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 700 | ASSERT_TRUE(handle == handle_public); |
| 701 | |
| 702 | dlclose(handle); |
| 703 | |
| 704 | // 1.1 even if it wasn't loaded before |
| 705 | dlclose(handle_public); |
| 706 | |
| 707 | handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD); |
| 708 | ASSERT_TRUE(handle_public == nullptr); |
| 709 | ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path + |
| 710 | "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 711 | |
| 712 | handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo); |
| 713 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 714 | |
| 715 | handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 716 | ASSERT_TRUE(handle == handle_public); |
| 717 | |
| 718 | dlclose(handle); |
| 719 | |
| 720 | // 2. And for regular namespaces (make sure it does not load second copy of the library) |
| 721 | extinfo.library_namespace = ns1; |
| 722 | handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo); |
| 723 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 724 | ASSERT_TRUE(handle == handle_public); |
| 725 | |
| 726 | dlclose(handle); |
| 727 | |
| 728 | // 2.1 Unless it was not loaded before - in which case it will load a duplicate. |
| 729 | // TODO(dimitry): This is broken. Maybe we need to deprecate non-isolated namespaces? |
| 730 | dlclose(handle_public); |
| 731 | |
| 732 | handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW | RTLD_NOLOAD); |
| 733 | ASSERT_TRUE(handle_public == nullptr); |
| 734 | ASSERT_EQ(std::string("dlopen failed: library \"") + lib_public_path + |
| 735 | "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 736 | |
| 737 | handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo); |
| 738 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 739 | |
| 740 | handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 741 | |
| 742 | ASSERT_TRUE(handle != handle_public); |
| 743 | |
| 744 | dlclose(handle); |
| 745 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 746 | extinfo.library_namespace = ns1; |
| 747 | |
| 748 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 749 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 750 | |
| 751 | extinfo.library_namespace = ns2; |
| 752 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 753 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 754 | |
| 755 | ASSERT_TRUE(handle1 != handle2); |
| 756 | |
| 757 | typedef const char* (*fn_t)(); |
| 758 | |
| 759 | fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 760 | ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror(); |
| 761 | fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string")); |
| 762 | ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror(); |
| 763 | |
| 764 | EXPECT_STREQ("This string is local to root library", ns_get_local_string1()); |
| 765 | EXPECT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 766 | |
| 767 | ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2()); |
| 768 | |
| 769 | fn_t ns_get_private_extern_string1 = |
| 770 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 771 | ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror(); |
| 772 | fn_t ns_get_private_extern_string2 = |
| 773 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string")); |
| 774 | ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror(); |
| 775 | |
| 776 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1()); |
| 777 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 778 | |
| 779 | ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2()); |
| 780 | |
| 781 | fn_t ns_get_public_extern_string1 = |
| 782 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 783 | ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror(); |
| 784 | fn_t ns_get_public_extern_string2 = |
| 785 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string")); |
| 786 | ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror(); |
| 787 | |
| 788 | EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1()); |
| 789 | ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2()); |
| 790 | |
| 791 | // and now check that dlopen() does the right thing in terms of preserving namespace |
| 792 | fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 793 | ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror(); |
| 794 | fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string")); |
| 795 | ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror(); |
| 796 | |
| 797 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1()); |
| 798 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 799 | |
| 800 | ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2()); |
| 801 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 802 | // Check that symbols from non-shared libraries a shared library depends on are not visible |
| 803 | // from original namespace. |
| 804 | |
| 805 | fn_t ns_get_internal_extern_string = |
| 806 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_internal_extern_string")); |
| 807 | ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror(); |
| 808 | ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) << |
| 809 | "ns_get_internal_extern_string() expected to return null but returns \"" << |
| 810 | ns_get_internal_extern_string() << "\""; |
| 811 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 812 | dlclose(handle1); |
| 813 | |
| 814 | // Check if handle2 is still alive (and well) |
| 815 | ASSERT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 816 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 817 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2()); |
| 818 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 819 | |
| 820 | dlclose(handle2); |
| 821 | } |
| 822 | |
Dimitry Ivanov | bf34ba3 | 2017-04-21 13:12:05 -0700 | [diff] [blame] | 823 | TEST(dlext, dlopen_ext_use_o_tmpfile_fd) { |
| 824 | const std::string lib_path = get_testlib_root() + "/libtest_simple.so"; |
| 825 | |
| 826 | int tmpfd = TEMP_FAILURE_RETRY( |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 827 | open(get_testlib_root().c_str(), O_TMPFILE | O_CLOEXEC | O_RDWR | O_EXCL, 0)); |
Dimitry Ivanov | bf34ba3 | 2017-04-21 13:12:05 -0700 | [diff] [blame] | 828 | |
| 829 | // Ignore kernels without O_TMPFILE flag support |
| 830 | if (tmpfd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) { |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | ASSERT_TRUE(tmpfd != -1) << strerror(errno); |
| 835 | |
| 836 | android_namespace_t* ns = |
| 837 | android_create_namespace("testing-o_tmpfile", |
| 838 | nullptr, |
| 839 | get_testlib_root().c_str(), |
| 840 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 841 | nullptr, |
| 842 | nullptr); |
| 843 | |
| 844 | ASSERT_DL_NOTNULL(ns); |
| 845 | |
| 846 | ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 847 | |
| 848 | std::string content; |
| 849 | ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno); |
| 850 | ASSERT_TRUE(android::base::WriteStringToFd(content, tmpfd)) << strerror(errno); |
| 851 | |
| 852 | android_dlextinfo extinfo; |
| 853 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE; |
| 854 | extinfo.library_fd = tmpfd; |
| 855 | extinfo.library_namespace = ns; |
| 856 | |
| 857 | void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo); |
| 858 | |
| 859 | ASSERT_DL_NOTNULL(handle); |
| 860 | |
| 861 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 862 | ASSERT_DL_NOTNULL(taxicab_number); |
| 863 | EXPECT_EQ(1729U, *taxicab_number); |
| 864 | dlclose(handle); |
| 865 | } |
| 866 | |
| 867 | TEST(dlext, dlopen_ext_use_memfd) { |
| 868 | const std::string lib_path = get_testlib_root() + "/libtest_simple.so"; |
| 869 | |
| 870 | // create memfd |
| 871 | int memfd = syscall(__NR_memfd_create, "foobar", MFD_CLOEXEC); |
| 872 | if (memfd == -1 && errno == ENOSYS) { |
| 873 | return; |
| 874 | } |
| 875 | |
| 876 | ASSERT_TRUE(memfd != -1) << strerror(errno); |
| 877 | |
| 878 | // Check st.f_type is TMPFS_MAGIC for memfd |
| 879 | struct statfs st; |
| 880 | ASSERT_TRUE(TEMP_FAILURE_RETRY(fstatfs(memfd, &st)) == 0) << strerror(errno); |
| 881 | ASSERT_EQ(static_cast<decltype(st.f_type)>(TMPFS_MAGIC), st.f_type); |
| 882 | |
| 883 | android_namespace_t* ns = |
| 884 | android_create_namespace("testing-memfd", |
| 885 | nullptr, |
| 886 | get_testlib_root().c_str(), |
| 887 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 888 | nullptr, |
| 889 | nullptr); |
| 890 | |
| 891 | ASSERT_DL_NOTNULL(ns); |
| 892 | |
| 893 | ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 894 | |
| 895 | // read file into memfd backed one. |
| 896 | std::string content; |
| 897 | ASSERT_TRUE(android::base::ReadFileToString(lib_path, &content)) << strerror(errno); |
| 898 | ASSERT_TRUE(android::base::WriteStringToFd(content, memfd)) << strerror(errno); |
| 899 | |
| 900 | android_dlextinfo extinfo; |
| 901 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD | ANDROID_DLEXT_USE_NAMESPACE; |
| 902 | extinfo.library_fd = memfd; |
| 903 | extinfo.library_namespace = ns; |
| 904 | |
| 905 | void* handle = android_dlopen_ext("foobar", RTLD_NOW, &extinfo); |
| 906 | |
| 907 | ASSERT_DL_NOTNULL(handle); |
| 908 | |
| 909 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 910 | ASSERT_DL_NOTNULL(taxicab_number); |
| 911 | EXPECT_EQ(1729U, *taxicab_number); |
| 912 | dlclose(handle); |
| 913 | } |
| 914 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 915 | TEST(dlext, ns_symbol_visibilty_one_namespace) { |
| 916 | static const char* root_lib = "libnstest_root.so"; |
| 917 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)); |
| 918 | |
| 919 | const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs:" + |
| 920 | get_testlib_root() + "/private_namespace_libs"; |
| 921 | |
| 922 | android_namespace_t* ns = |
| 923 | android_create_namespace("one", |
| 924 | nullptr, |
| 925 | ns_search_path.c_str(), |
| 926 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 927 | nullptr, |
| 928 | nullptr); |
| 929 | |
| 930 | ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 931 | |
| 932 | android_dlextinfo extinfo; |
| 933 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 934 | extinfo.library_namespace = ns; |
| 935 | |
| 936 | void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 937 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 938 | |
| 939 | typedef const char* (*fn_t)(); |
| 940 | |
| 941 | // Check that relocation worked correctly |
| 942 | fn_t ns_get_internal_extern_string = |
| 943 | reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string")); |
| 944 | ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror(); |
| 945 | ASSERT_STREQ("This string is from a library a shared library depends on", ns_get_internal_extern_string()); |
| 946 | |
| 947 | fn_t internal_extern_string_fn = |
| 948 | reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string")); |
| 949 | ASSERT_TRUE(internal_extern_string_fn != nullptr) << dlerror(); |
| 950 | ASSERT_STREQ("This string is from a library a shared library depends on", internal_extern_string_fn()); |
| 951 | } |
| 952 | |
| 953 | TEST(dlext, ns_symbol_visibilty_between_namespaces) { |
| 954 | static const char* root_lib = "libnstest_root.so"; |
| 955 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)); |
| 956 | |
| 957 | const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs"; |
| 958 | const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs"; |
| 959 | |
| 960 | android_namespace_t* ns_public = |
| 961 | android_create_namespace("public", |
| 962 | nullptr, |
| 963 | public_ns_search_path.c_str(), |
| 964 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 965 | nullptr, |
| 966 | nullptr); |
| 967 | |
| 968 | ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 969 | |
| 970 | android_namespace_t* ns_private = |
| 971 | android_create_namespace("private", |
| 972 | nullptr, |
| 973 | private_ns_search_path.c_str(), |
| 974 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 975 | nullptr, |
| 976 | nullptr); |
| 977 | |
| 978 | ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror(); |
| 979 | ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 980 | |
| 981 | android_dlextinfo extinfo; |
| 982 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 983 | extinfo.library_namespace = ns_private; |
| 984 | |
| 985 | void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 986 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 987 | |
| 988 | typedef const char* (*fn_t)(); |
| 989 | |
| 990 | // Check that relocation worked correctly |
| 991 | fn_t ns_get_internal_extern_string = |
| 992 | reinterpret_cast<fn_t>(dlsym(handle, "ns_get_internal_extern_string")); |
| 993 | ASSERT_TRUE(ns_get_internal_extern_string != nullptr) << dlerror(); |
| 994 | ASSERT_TRUE(ns_get_internal_extern_string() == nullptr) << |
| 995 | "ns_get_internal_extern_string() expected to return null but returns \"" << |
| 996 | ns_get_internal_extern_string() << "\""; |
| 997 | |
| 998 | fn_t internal_extern_string_fn = |
| 999 | reinterpret_cast<fn_t>(dlsym(handle, "internal_extern_string")); |
| 1000 | ASSERT_TRUE(internal_extern_string_fn == nullptr); |
| 1001 | ASSERT_STREQ("undefined symbol: internal_extern_string", dlerror()); |
| 1002 | } |
| 1003 | |
| 1004 | TEST(dlext, ns_unload_between_namespaces) { |
| 1005 | static const char* root_lib = "libnstest_root.so"; |
| 1006 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)); |
| 1007 | |
| 1008 | const std::string public_ns_search_path = get_testlib_root() + "/public_namespace_libs"; |
| 1009 | const std::string private_ns_search_path = get_testlib_root() + "/private_namespace_libs"; |
| 1010 | |
| 1011 | android_namespace_t* ns_public = |
| 1012 | android_create_namespace("public", |
| 1013 | nullptr, |
| 1014 | public_ns_search_path.c_str(), |
| 1015 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1016 | nullptr, |
| 1017 | nullptr); |
| 1018 | |
| 1019 | ASSERT_TRUE(android_link_namespaces(ns_public, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 1020 | |
| 1021 | android_namespace_t* ns_private = |
| 1022 | android_create_namespace("private", |
| 1023 | nullptr, |
| 1024 | private_ns_search_path.c_str(), |
| 1025 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1026 | nullptr, |
| 1027 | nullptr); |
| 1028 | |
| 1029 | ASSERT_TRUE(android_link_namespaces(ns_private, ns_public, g_public_lib)) << dlerror(); |
| 1030 | ASSERT_TRUE(android_link_namespaces(ns_private, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 1031 | |
| 1032 | android_dlextinfo extinfo; |
| 1033 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1034 | extinfo.library_namespace = ns_private; |
| 1035 | |
| 1036 | void* handle = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1037 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1038 | |
| 1039 | dlclose(handle); |
| 1040 | // Check that root_lib was unloaded |
| 1041 | handle = android_dlopen_ext(root_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1042 | ASSERT_TRUE(handle == nullptr); |
| 1043 | ASSERT_EQ(std::string("dlopen failed: library \"") + root_lib + |
| 1044 | "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 1045 | |
| 1046 | // Check that shared library was unloaded in public ns |
| 1047 | extinfo.library_namespace = ns_public; |
| 1048 | handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1049 | ASSERT_TRUE(handle == nullptr); |
| 1050 | ASSERT_EQ(std::string("dlopen failed: library \"") + g_public_lib + |
| 1051 | "\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror()); |
| 1052 | } |
| 1053 | |
Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 1054 | TEST(dlext, ns_greylist_enabled) { |
Dimitry Ivanov | 1862314 | 2017-02-21 13:41:08 -0800 | [diff] [blame] | 1055 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)); |
| 1056 | |
| 1057 | const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs"; |
| 1058 | |
| 1059 | android_namespace_t* ns = |
| 1060 | android_create_namespace("namespace", |
| 1061 | nullptr, |
| 1062 | ns_search_path.c_str(), |
Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 1063 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED, |
Dimitry Ivanov | 1862314 | 2017-02-21 13:41:08 -0800 | [diff] [blame] | 1064 | nullptr, |
| 1065 | nullptr); |
| 1066 | |
| 1067 | ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 1068 | |
| 1069 | android_dlextinfo extinfo; |
| 1070 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1071 | extinfo.library_namespace = ns; |
| 1072 | |
Dimitry Ivanov | 35c8e3b | 2017-02-27 12:17:47 -0800 | [diff] [blame] | 1073 | // An app targeting M can open libnativehelper.so because it's on the greylist. |
Dimitry Ivanov | 1862314 | 2017-02-21 13:41:08 -0800 | [diff] [blame] | 1074 | android_set_application_target_sdk_version(__ANDROID_API_M__); |
| 1075 | void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo); |
| 1076 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1077 | |
Dimitry Ivanov | 35c8e3b | 2017-02-27 12:17:47 -0800 | [diff] [blame] | 1078 | // Check that loader did not load another copy of libdl.so while loading greylisted library. |
| 1079 | void* dlsym_ptr = dlsym(handle, "dlsym"); |
| 1080 | ASSERT_TRUE(dlsym_ptr != nullptr) << dlerror(); |
| 1081 | ASSERT_EQ(&dlsym, dlsym_ptr); |
| 1082 | |
Dimitry Ivanov | 1862314 | 2017-02-21 13:41:08 -0800 | [diff] [blame] | 1083 | dlclose(handle); |
| 1084 | |
Dimitry Ivanov | 35c8e3b | 2017-02-27 12:17:47 -0800 | [diff] [blame] | 1085 | // An app targeting N no longer has the greylist. |
Dimitry Ivanov | 1862314 | 2017-02-21 13:41:08 -0800 | [diff] [blame] | 1086 | android_set_application_target_sdk_version(__ANDROID_API_N__); |
| 1087 | handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo); |
| 1088 | ASSERT_TRUE(handle == nullptr); |
| 1089 | ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror()); |
| 1090 | } |
| 1091 | |
Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 1092 | TEST(dlext, ns_greylist_disabled_by_default) { |
| 1093 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)); |
| 1094 | |
| 1095 | const std::string ns_search_path = get_testlib_root() + "/private_namespace_libs"; |
| 1096 | |
| 1097 | android_namespace_t* ns = |
| 1098 | android_create_namespace("namespace", |
| 1099 | nullptr, |
| 1100 | ns_search_path.c_str(), |
| 1101 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1102 | nullptr, |
| 1103 | nullptr); |
| 1104 | |
| 1105 | ASSERT_TRUE(android_link_namespaces(ns, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 1106 | |
| 1107 | android_dlextinfo extinfo; |
| 1108 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1109 | extinfo.library_namespace = ns; |
| 1110 | |
| 1111 | android_set_application_target_sdk_version(__ANDROID_API_M__); |
| 1112 | void* handle = android_dlopen_ext("libnativehelper.so", RTLD_NOW, &extinfo); |
| 1113 | ASSERT_TRUE(handle == nullptr); |
| 1114 | ASSERT_STREQ("dlopen failed: library \"libnativehelper.so\" not found", dlerror()); |
| 1115 | } |
| 1116 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1117 | TEST(dlext, ns_cyclic_namespaces) { |
| 1118 | // Test that ns1->ns2->ns1 link does not break the loader |
| 1119 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)); |
| 1120 | std::string shared_libs = g_core_shared_libs + ":libthatdoesnotexist.so"; |
| 1121 | |
| 1122 | const std::string ns_search_path = get_testlib_root() + "/public_namespace_libs"; |
| 1123 | |
| 1124 | android_namespace_t* ns1 = |
| 1125 | android_create_namespace("ns1", |
| 1126 | nullptr, |
| 1127 | ns_search_path.c_str(), |
| 1128 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1129 | nullptr, |
| 1130 | nullptr); |
| 1131 | |
| 1132 | ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 1133 | |
| 1134 | android_namespace_t* ns2 = |
| 1135 | android_create_namespace("ns1", |
| 1136 | nullptr, |
| 1137 | ns_search_path.c_str(), |
| 1138 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1139 | nullptr, |
| 1140 | nullptr); |
| 1141 | |
| 1142 | ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 1143 | |
| 1144 | ASSERT_TRUE(android_link_namespaces(ns2, ns1, shared_libs.c_str())) << dlerror(); |
| 1145 | ASSERT_TRUE(android_link_namespaces(ns1, ns2, shared_libs.c_str())) << dlerror(); |
| 1146 | |
| 1147 | android_dlextinfo extinfo; |
| 1148 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1149 | extinfo.library_namespace = ns1; |
| 1150 | |
| 1151 | void* handle = android_dlopen_ext("libthatdoesnotexist.so", RTLD_NOW, &extinfo); |
| 1152 | ASSERT_TRUE(handle == nullptr); |
| 1153 | ASSERT_STREQ("dlopen failed: library \"libthatdoesnotexist.so\" not found", dlerror()); |
| 1154 | } |
| 1155 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1156 | TEST(dlext, ns_isolated) { |
| 1157 | static const char* root_lib = "libnstest_root_not_isolated.so"; |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1158 | std::string shared_libs = g_core_shared_libs + ":" + g_public_lib; |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1159 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1160 | const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib; |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 1161 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1162 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1163 | |
Dmitriy Ivanov | 3cc35e2 | 2015-11-17 18:36:50 -0800 | [diff] [blame] | 1164 | android_set_application_target_sdk_version(42U); // something > 23 |
| 1165 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1166 | ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1167 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1168 | android_namespace_t* ns_not_isolated = |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1169 | android_create_namespace("private", |
| 1170 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1171 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1172 | ANDROID_NAMESPACE_TYPE_REGULAR, |
| 1173 | nullptr, |
| 1174 | nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1175 | ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1176 | ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1177 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1178 | android_namespace_t* ns_isolated = |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1179 | android_create_namespace("private_isolated1", |
| 1180 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1181 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1182 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1183 | nullptr, |
| 1184 | nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1185 | ASSERT_TRUE(ns_isolated != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1186 | ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, shared_libs.c_str())) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1187 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1188 | android_namespace_t* ns_isolated2 = |
| 1189 | android_create_namespace("private_isolated2", |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1190 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1191 | nullptr, |
| 1192 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1193 | get_testlib_root().c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1194 | nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1195 | ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1196 | ASSERT_TRUE(android_link_namespaces(ns_isolated2, nullptr, shared_libs.c_str())) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1197 | |
| 1198 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 1199 | ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror()); |
| 1200 | |
| 1201 | std::string lib_private_external_path = |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1202 | get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so"; |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1203 | |
| 1204 | // Load lib_private_external_path to default namespace |
| 1205 | // (it should remain invisible for the isolated namespaces after this) |
| 1206 | void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW); |
| 1207 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1208 | |
| 1209 | android_dlextinfo extinfo; |
| 1210 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1211 | extinfo.library_namespace = ns_not_isolated; |
| 1212 | |
| 1213 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1214 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 1215 | |
| 1216 | extinfo.library_namespace = ns_isolated; |
| 1217 | |
| 1218 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1219 | ASSERT_TRUE(handle2 == nullptr); |
| 1220 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 1221 | |
| 1222 | // Check dlopen by absolute path |
| 1223 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 1224 | ASSERT_TRUE(handle2 == nullptr); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 1225 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed" |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 1226 | " or dlopened by \"" + get_executable_path() + "\" is not accessible" |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 1227 | " for the namespace \"private_isolated1\"", dlerror()); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1228 | |
| 1229 | extinfo.library_namespace = ns_isolated2; |
| 1230 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1231 | // this should work because isolation_path for private_isolated2 includes get_testlib_root() |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1232 | handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 1233 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 1234 | dlclose(handle2); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1235 | |
| 1236 | // Check dlopen by absolute path |
| 1237 | 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] | 1238 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 1239 | dlclose(handle2); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1240 | |
| 1241 | typedef const char* (*fn_t)(); |
| 1242 | fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 1243 | ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror(); |
| 1244 | |
| 1245 | ASSERT_STREQ("This string is local to root library", ns_get_local_string()); |
| 1246 | |
| 1247 | fn_t ns_get_private_extern_string = |
| 1248 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 1249 | ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror(); |
| 1250 | |
| 1251 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string()); |
| 1252 | |
| 1253 | fn_t ns_get_public_extern_string = |
| 1254 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 1255 | ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror(); |
| 1256 | |
| 1257 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string()); |
| 1258 | |
| 1259 | fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 1260 | ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror(); |
| 1261 | |
| 1262 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string()); |
| 1263 | |
| 1264 | dlclose(handle1); |
| 1265 | } |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1266 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1267 | TEST(dlext, ns_shared) { |
| 1268 | static const char* root_lib = "libnstest_root_not_isolated.so"; |
| 1269 | static const char* root_lib_isolated = "libnstest_root.so"; |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1270 | |
| 1271 | std::string shared_libs = g_core_shared_libs + ":" + g_public_lib; |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1272 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1273 | const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib; |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1274 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 1275 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1276 | |
| 1277 | android_set_application_target_sdk_version(42U); // something > 23 |
| 1278 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1279 | ASSERT_TRUE(android_init_anonymous_namespace(shared_libs.c_str(), nullptr)) << dlerror(); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1280 | |
| 1281 | // preload this library to the default namespace to check if it |
| 1282 | // is shared later on. |
| 1283 | void* handle_dlopened = |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1284 | dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1285 | ASSERT_TRUE(handle_dlopened != nullptr) << dlerror(); |
| 1286 | |
| 1287 | android_namespace_t* ns_not_isolated = |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1288 | android_create_namespace("private", |
| 1289 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1290 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1291 | ANDROID_NAMESPACE_TYPE_REGULAR, |
| 1292 | nullptr, |
| 1293 | nullptr); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1294 | ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1295 | ASSERT_TRUE(android_link_namespaces(ns_not_isolated, nullptr, shared_libs.c_str())) << dlerror(); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1296 | |
| 1297 | android_namespace_t* ns_isolated_shared = |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1298 | android_create_namespace("private_isolated_shared", |
| 1299 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1300 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1301 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED, |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1302 | nullptr, |
| 1303 | nullptr); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1304 | ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1305 | ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, shared_libs.c_str())) << dlerror(); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1306 | |
| 1307 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 1308 | ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror()); |
| 1309 | |
| 1310 | std::string lib_private_external_path = |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1311 | get_testlib_root() + "/private_namespace_libs_external/libnstest_private_external.so"; |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1312 | |
| 1313 | // Load lib_private_external_path to default namespace |
| 1314 | // (it should remain invisible for the isolated namespaces after this) |
| 1315 | void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW); |
| 1316 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1317 | |
| 1318 | android_dlextinfo extinfo; |
| 1319 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1320 | extinfo.library_namespace = ns_not_isolated; |
| 1321 | |
| 1322 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1323 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 1324 | |
| 1325 | extinfo.library_namespace = ns_isolated_shared; |
| 1326 | |
| 1327 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1328 | ASSERT_TRUE(handle2 == nullptr); |
| 1329 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 1330 | |
| 1331 | // Check dlopen by absolute path |
| 1332 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 1333 | ASSERT_TRUE(handle2 == nullptr); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 1334 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed" |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 1335 | " or dlopened by \"" + get_executable_path() + "\" is not accessible" |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 1336 | " for the namespace \"private_isolated_shared\"", dlerror()); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1337 | |
| 1338 | // load libnstest_root.so to shared namespace in order to check that everything is different |
| 1339 | // except shared libnstest_dlopened.so |
| 1340 | |
| 1341 | handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo); |
| 1342 | |
| 1343 | typedef const char* (*fn_t)(); |
| 1344 | fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 1345 | ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror(); |
| 1346 | fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string")); |
| 1347 | ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror(); |
| 1348 | |
| 1349 | ASSERT_STREQ("This string is local to root library", ns_get_local_string()); |
| 1350 | ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared()); |
| 1351 | ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared()); |
| 1352 | |
| 1353 | fn_t ns_get_private_extern_string = |
| 1354 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 1355 | ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror(); |
| 1356 | fn_t ns_get_private_extern_string_shared = |
| 1357 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string")); |
| 1358 | ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror(); |
| 1359 | |
| 1360 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string()); |
| 1361 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared()); |
| 1362 | ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared()); |
| 1363 | |
| 1364 | fn_t ns_get_public_extern_string = |
| 1365 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 1366 | ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror(); |
| 1367 | fn_t ns_get_public_extern_string_shared = |
| 1368 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string")); |
| 1369 | ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror(); |
| 1370 | |
| 1371 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string()); |
| 1372 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared()); |
| 1373 | ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared()); |
| 1374 | |
| 1375 | fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 1376 | ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror(); |
| 1377 | fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string")); |
| 1378 | ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror(); |
| 1379 | const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string")); |
| 1380 | ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror(); |
| 1381 | |
| 1382 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string()); |
| 1383 | ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string); |
| 1384 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared()); |
| 1385 | ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared()); |
| 1386 | ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared()); |
| 1387 | |
| 1388 | dlclose(handle1); |
| 1389 | dlclose(handle2); |
| 1390 | } |
| 1391 | |
Dimitry Ivanov | f1cb669 | 2017-05-01 17:45:38 -0700 | [diff] [blame] | 1392 | TEST(dlext, ns_shared_links_and_paths) { |
| 1393 | // Create parent namespace (isolated, not shared) |
| 1394 | android_namespace_t* ns_isolated = |
| 1395 | android_create_namespace("private_isolated", |
| 1396 | nullptr, |
| 1397 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
| 1398 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1399 | (get_testlib_root() + "/public_namespace_libs").c_str(), |
| 1400 | nullptr); |
| 1401 | ASSERT_TRUE(ns_isolated != nullptr) << dlerror(); |
| 1402 | ASSERT_TRUE(android_link_namespaces(ns_isolated, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
| 1403 | |
| 1404 | // Create shared namespace with ns_isolated parent |
| 1405 | android_namespace_t* ns_shared = |
| 1406 | android_create_namespace("private_shared", |
| 1407 | nullptr, |
| 1408 | nullptr, |
| 1409 | ANDROID_NAMESPACE_TYPE_SHARED | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1410 | nullptr, |
| 1411 | ns_isolated); |
| 1412 | ASSERT_TRUE(ns_shared != nullptr) << dlerror(); |
| 1413 | |
| 1414 | // 1. Load a library in ns_shared to check that it has inherited |
| 1415 | // search path and the link to the default namespace. |
| 1416 | android_dlextinfo extinfo; |
| 1417 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1418 | extinfo.library_namespace = ns_shared; |
| 1419 | |
| 1420 | { |
| 1421 | void* handle = android_dlopen_ext("libnstest_private.so", RTLD_NOW, &extinfo); |
| 1422 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1423 | const char** ns_private_extern_string = static_cast<const char**>(dlsym(handle, "g_private_extern_string")); |
| 1424 | ASSERT_TRUE(ns_private_extern_string != nullptr) << dlerror(); |
| 1425 | ASSERT_STREQ("This string is from private namespace", *ns_private_extern_string); |
| 1426 | |
| 1427 | dlclose(handle); |
| 1428 | } |
| 1429 | // 2. Load another test library by absolute path to check that |
| 1430 | // it has inherited permitted_when_isolated_path |
| 1431 | { |
| 1432 | void* handle = android_dlopen_ext( |
| 1433 | (get_testlib_root() + "/public_namespace_libs/libnstest_public.so").c_str(), |
| 1434 | RTLD_NOW, |
| 1435 | &extinfo); |
| 1436 | |
| 1437 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1438 | const char** ns_public_extern_string = static_cast<const char**>(dlsym(handle, "g_public_extern_string")); |
| 1439 | ASSERT_TRUE(ns_public_extern_string != nullptr) << dlerror(); |
| 1440 | ASSERT_STREQ("This string is from public namespace", *ns_public_extern_string); |
| 1441 | |
| 1442 | dlclose(handle); |
| 1443 | } |
| 1444 | |
| 1445 | // 3. Check that it is still isolated. |
| 1446 | { |
| 1447 | void* handle = android_dlopen_ext( |
| 1448 | (get_testlib_root() + "/libtest_empty.so").c_str(), |
| 1449 | RTLD_NOW, |
| 1450 | &extinfo); |
| 1451 | |
| 1452 | ASSERT_TRUE(handle == nullptr); |
| 1453 | } |
| 1454 | } |
| 1455 | |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1456 | TEST(dlext, ns_shared_dlclose) { |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1457 | android_set_application_target_sdk_version(42U); // something > 23 |
| 1458 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1459 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)) << dlerror(); |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1460 | |
| 1461 | // preload this library to the default namespace to check if it |
| 1462 | // is shared later on. |
| 1463 | void* handle_dlopened = |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1464 | dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW); |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1465 | ASSERT_TRUE(handle_dlopened != nullptr) << dlerror(); |
| 1466 | |
| 1467 | android_namespace_t* ns_isolated_shared = |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1468 | android_create_namespace("private_isolated_shared", |
| 1469 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1470 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1471 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED, |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1472 | nullptr, |
| 1473 | nullptr); |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1474 | ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1475 | ASSERT_TRUE(android_link_namespaces(ns_isolated_shared, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1476 | |
| 1477 | // Check if "libnstest_dlopened.so" is loaded (and the same) |
| 1478 | android_dlextinfo extinfo; |
| 1479 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1480 | extinfo.library_namespace = ns_isolated_shared; |
| 1481 | |
| 1482 | void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1483 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1484 | ASSERT_TRUE(handle == handle_dlopened); |
| 1485 | dlclose(handle); |
| 1486 | dlclose(handle_dlopened); |
| 1487 | |
| 1488 | // And now check that the library cannot be found by soname (and is no longer loaded) |
| 1489 | handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1490 | ASSERT_TRUE(handle == nullptr) |
| 1491 | << "Error: libnstest_dlopened.so is still accessible in shared namespace"; |
| 1492 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1493 | handle = android_dlopen_ext((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1494 | RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1495 | ASSERT_TRUE(handle == nullptr) |
| 1496 | << "Error: libnstest_dlopened.so is still accessible in shared namespace"; |
| 1497 | |
| 1498 | handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD); |
| 1499 | ASSERT_TRUE(handle == nullptr) |
| 1500 | << "Error: libnstest_dlopened.so is still accessible in default namespace"; |
| 1501 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1502 | handle = dlopen((get_testlib_root() + "/private_namespace_libs/libnstest_dlopened.so").c_str(), |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1503 | RTLD_NOW | RTLD_NOLOAD); |
| 1504 | ASSERT_TRUE(handle == nullptr) |
| 1505 | << "Error: libnstest_dlopened.so is still accessible in default namespace"; |
| 1506 | |
| 1507 | // Now lets see if the soinfo area gets reused in the wrong way: |
| 1508 | // load a library to default namespace. |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1509 | const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib; |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 1510 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 1511 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1512 | |
| 1513 | // try to find it in shared namespace |
| 1514 | handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1515 | ASSERT_TRUE(handle == nullptr) |
| 1516 | << "Error: " << g_public_lib << " is accessible in shared namespace"; |
| 1517 | } |
| 1518 | |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1519 | TEST(dlext, ns_isolated_rtld_global) { |
| 1520 | static const char* root_lib = "libnstest_root.so"; |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1521 | ASSERT_TRUE(android_init_anonymous_namespace(g_core_shared_libs.c_str(), nullptr)); |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1522 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1523 | const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs"; |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1524 | |
| 1525 | android_namespace_t* ns1 = |
| 1526 | android_create_namespace("isolated1", |
| 1527 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1528 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1529 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1530 | lib_public_path.c_str(), |
| 1531 | nullptr); |
| 1532 | ASSERT_TRUE(ns1 != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1533 | ASSERT_TRUE(android_link_namespaces(ns1, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1534 | |
| 1535 | android_namespace_t* ns2 = |
| 1536 | android_create_namespace("isolated2", |
| 1537 | nullptr, |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1538 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1539 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1540 | lib_public_path.c_str(), |
| 1541 | nullptr); |
| 1542 | ASSERT_TRUE(ns2 != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1543 | ASSERT_TRUE(android_link_namespaces(ns2, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1544 | |
| 1545 | android_dlextinfo extinfo; |
| 1546 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1547 | extinfo.library_namespace = ns1; |
| 1548 | |
| 1549 | void* handle_global = android_dlopen_ext((lib_public_path + "/" + g_public_lib).c_str(), |
| 1550 | RTLD_GLOBAL, |
| 1551 | &extinfo); |
| 1552 | |
| 1553 | ASSERT_TRUE(handle_global != nullptr) << dlerror(); |
| 1554 | |
| 1555 | android_namespace_t* ns1_child = |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1556 | android_create_namespace("isolated1_child", |
| 1557 | nullptr, |
| 1558 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
| 1559 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 1560 | nullptr, |
| 1561 | ns1); |
| 1562 | |
| 1563 | ASSERT_TRUE(ns1_child != nullptr) << dlerror(); |
| 1564 | ASSERT_TRUE(android_link_namespaces(ns1_child, nullptr, g_core_shared_libs.c_str())) << dlerror(); |
Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 1565 | |
| 1566 | // Now - only ns1 and ns1 child should be able to dlopen root_lib |
| 1567 | // attempt to use ns2 should result in dlerror() |
| 1568 | |
| 1569 | // Check ns1_child first. |
| 1570 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1571 | extinfo.library_namespace = ns1_child; |
| 1572 | |
| 1573 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1574 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 1575 | |
| 1576 | // now ns1 |
| 1577 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1578 | extinfo.library_namespace = ns1; |
| 1579 | |
| 1580 | handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1581 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 1582 | |
| 1583 | // and ns2 should fail |
| 1584 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1585 | extinfo.library_namespace = ns2; |
| 1586 | |
| 1587 | handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 1588 | ASSERT_TRUE(handle1 == nullptr); |
| 1589 | ASSERT_STREQ("dlopen failed: library \"libnstest_public.so\" not found", dlerror()); |
| 1590 | } |
| 1591 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1592 | TEST(dlext, ns_anonymous) { |
| 1593 | static const char* root_lib = "libnstest_root.so"; |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1594 | std::string shared_libs = g_core_shared_libs + ":" + g_public_lib; |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1595 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1596 | const std::string lib_public_path = get_testlib_root() + "/public_namespace_libs/" + g_public_lib; |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 1597 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 1598 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1599 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1600 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1601 | ASSERT_TRUE( |
| 1602 | android_init_anonymous_namespace(shared_libs.c_str(), |
| 1603 | (get_testlib_root() + "/private_namespace_libs").c_str()) |
| 1604 | ) << dlerror(); |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1605 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1606 | android_namespace_t* ns = |
| 1607 | android_create_namespace("private", |
| 1608 | nullptr, |
| 1609 | (get_testlib_root() + "/private_namespace_libs").c_str(), |
| 1610 | ANDROID_NAMESPACE_TYPE_REGULAR, |
| 1611 | nullptr, |
| 1612 | nullptr); |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1613 | |
| 1614 | ASSERT_TRUE(ns != nullptr) << dlerror(); |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 1615 | ASSERT_TRUE(android_link_namespaces(ns, nullptr, shared_libs.c_str())) << dlerror(); |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1616 | |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 1617 | std::string private_library_absolute_path = get_testlib_root() + "/private_namespace_libs/" + root_lib; |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1618 | |
| 1619 | android_dlextinfo extinfo; |
| 1620 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1621 | extinfo.library_namespace = ns; |
| 1622 | |
| 1623 | // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string |
| 1624 | void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo); |
| 1625 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1626 | |
| 1627 | uintptr_t ns_get_dlopened_string_addr = |
| 1628 | reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string")); |
| 1629 | ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror(); |
| 1630 | typedef const char* (*fn_t)(); |
| 1631 | fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr); |
| 1632 | |
| 1633 | std::vector<map_record> maps; |
| 1634 | Maps::parse_maps(&maps); |
| 1635 | |
| 1636 | uintptr_t addr_start = 0; |
| 1637 | uintptr_t addr_end = 0; |
| 1638 | std::vector<map_record> maps_to_copy; |
| 1639 | |
| 1640 | for (const auto& rec : maps) { |
| 1641 | if (rec.pathname == private_library_absolute_path) { |
| 1642 | if (addr_start == 0) { |
| 1643 | addr_start = rec.addr_start; |
| 1644 | } |
| 1645 | addr_end = rec.addr_end; |
| 1646 | |
| 1647 | maps_to_copy.push_back(rec); |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | // some sanity checks.. |
| 1652 | ASSERT_TRUE(addr_start > 0); |
| 1653 | ASSERT_TRUE(addr_end > 0); |
| 1654 | ASSERT_EQ(3U, maps_to_copy.size()); |
| 1655 | ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start); |
| 1656 | ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end); |
| 1657 | |
| 1658 | // copy |
| 1659 | uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start, |
| 1660 | PROT_NONE, MAP_ANON | MAP_PRIVATE, |
| 1661 | -1, 0)); |
| 1662 | ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED); |
| 1663 | |
| 1664 | for (const auto& rec : maps_to_copy) { |
| 1665 | uintptr_t offset = rec.addr_start - addr_start; |
| 1666 | size_t size = rec.addr_end - rec.addr_start; |
| 1667 | void* addr = reinterpret_cast<void*>(reserved_addr + offset); |
| 1668 | void* map = mmap(addr, size, PROT_READ | PROT_WRITE, |
| 1669 | MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0); |
| 1670 | ASSERT_TRUE(map != MAP_FAILED); |
| 1671 | memcpy(map, reinterpret_cast<void*>(rec.addr_start), size); |
| 1672 | mprotect(map, size, rec.perms); |
| 1673 | } |
| 1674 | |
| 1675 | // call the function copy |
| 1676 | uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start; |
| 1677 | fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset); |
| 1678 | ASSERT_STREQ("This string is from private namespace (dlopened library)", |
| 1679 | ns_get_dlopened_string_anon()); |
| 1680 | |
| 1681 | // They should belong to different namespaces (private and anonymous) |
| 1682 | ASSERT_STREQ("This string is from private namespace (dlopened library)", |
| 1683 | ns_get_dlopened_string_private()); |
| 1684 | |
| 1685 | ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private()); |
| 1686 | } |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 1687 | |
| 1688 | TEST(dlext, dlopen_handle_value_platform) { |
| 1689 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); |
| 1690 | ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0) |
| 1691 | << "dlopen should return odd value for the handle"; |
| 1692 | dlclose(handle); |
| 1693 | } |
| 1694 | |
| 1695 | TEST(dlext, dlopen_handle_value_app_compat) { |
Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 1696 | android_set_application_target_sdk_version(__ANDROID_API_M__); |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 1697 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); |
| 1698 | ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0) |
| 1699 | << "dlopen should return valid pointer"; |
| 1700 | dlclose(handle); |
| 1701 | } |