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