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