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) \ |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -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" |
Torne (Richard Coles) | 12bbb91 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 55 | #define LIBSIZE 1024*1024 // how much address space to reserve for it |
| 56 | |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 57 | #if defined(__LP64__) |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 58 | #define NATIVE_TESTS_PATH "/nativetest64" |
Dmitriy Ivanov | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 59 | #else |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 60 | #define NATIVE_TESTS_PATH "/nativetest" |
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 | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 153 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 154 | // lib_path is relative when $ANDROID_DATA is relative |
| 155 | char lib_realpath_buf[PATH_MAX]; |
| 156 | ASSERT_TRUE(realpath(lib_path.c_str(), lib_realpath_buf) == lib_realpath_buf); |
| 157 | const std::string lib_realpath = std::string(lib_realpath_buf); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 158 | |
| 159 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 160 | 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] | 161 | 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] | 162 | extinfo.library_fd_offset = 17; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 163 | |
| 164 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 165 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 166 | ASSERT_STREQ("dlopen failed: file offset for the library \"libname_placeholder\" is not page-aligned: 17", dlerror()); |
| 167 | |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 168 | // Test an address above 2^44, for http://b/18178121 . |
| 169 | extinfo.library_fd_offset = (5LL<<48) + PAGE_SIZE; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 170 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 171 | ASSERT_TRUE(handle_ == nullptr); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 172 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" >= file size", dlerror()); |
| 173 | |
| 174 | extinfo.library_fd_offset = 0LL - PAGE_SIZE; |
| 175 | handle_ = android_dlopen_ext("libname_placeholder", RTLD_NOW, &extinfo); |
| 176 | ASSERT_TRUE(handle_ == nullptr); |
| 177 | ASSERT_SUBSTR("dlopen failed: file offset for the library \"libname_placeholder\" is negative", dlerror()); |
| 178 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 179 | extinfo.library_fd_offset = 0; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 180 | handle_ = android_dlopen_ext("libname_ignored", RTLD_NOW, &extinfo); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 181 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | ef25592 | 2015-04-08 11:53:08 -0700 | [diff] [blame] | 182 | ASSERT_EQ("dlopen failed: \"" + lib_realpath + "\" has bad ELF magic", dlerror()); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 183 | |
Dmitriy Ivanov | fd7a91e | 2015-11-06 10:44:37 -0800 | [diff] [blame] | 184 | // Check if dlsym works after unsuccessful dlopen(). |
| 185 | // Supply non-exiting one to make linker visit every soinfo. |
| 186 | void* sym = dlsym(RTLD_DEFAULT, "this_symbol_does_not_exist___"); |
| 187 | ASSERT_TRUE(sym == nullptr); |
| 188 | |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 189 | close(extinfo.library_fd); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 192 | TEST_F(DlExtTest, ExtInfoUseOffsetWithoutFd) { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 193 | android_dlextinfo extinfo; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 194 | extinfo.flags = ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET; |
Christopher Ferris | c0ffcec | 2016-01-19 20:32:37 -0800 | [diff] [blame] | 195 | // This offset will not be used, so it doesn't matter. |
| 196 | extinfo.library_fd_offset = 0; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 197 | |
| 198 | handle_ = android_dlopen_ext("/some/lib/that/does_not_exist", RTLD_NOW, &extinfo); |
| 199 | ASSERT_TRUE(handle_ == nullptr); |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 200 | 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] | 201 | } |
| 202 | |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 203 | TEST(dlext, android_dlopen_ext_force_load_smoke) { |
| 204 | // 1. Open actual file |
| 205 | void* handle = dlopen("libdlext_test.so", RTLD_NOW); |
| 206 | ASSERT_DL_NOTNULL(handle); |
| 207 | // 2. Open link with force_load flag set |
| 208 | android_dlextinfo extinfo; |
| 209 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 210 | void* handle2 = android_dlopen_ext("libdlext_test_v2.so", RTLD_NOW, &extinfo); |
| 211 | ASSERT_DL_NOTNULL(handle2); |
| 212 | ASSERT_TRUE(handle != handle2); |
| 213 | |
| 214 | dlclose(handle2); |
| 215 | dlclose(handle); |
| 216 | } |
| 217 | |
| 218 | TEST(dlext, android_dlopen_ext_force_load_soname_exception) { |
| 219 | // Check if soname lookup still returns already loaded library |
| 220 | // when ANDROID_DLEXT_FORCE_LOAD flag is specified. |
| 221 | void* handle = dlopen("libdlext_test_v2.so", RTLD_NOW); |
| 222 | ASSERT_DL_NOTNULL(handle); |
| 223 | |
| 224 | android_dlextinfo extinfo; |
| 225 | extinfo.flags = ANDROID_DLEXT_FORCE_LOAD; |
| 226 | |
| 227 | // Note that 'libdlext_test.so' is dt_soname for libdlext_test_v2.so |
| 228 | void* handle2 = android_dlopen_ext("libdlext_test.so", RTLD_NOW, &extinfo); |
| 229 | |
| 230 | ASSERT_DL_NOTNULL(handle2); |
| 231 | ASSERT_TRUE(handle == handle2); |
| 232 | |
| 233 | dlclose(handle2); |
| 234 | dlclose(handle); |
| 235 | } |
| 236 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 237 | TEST(dlfcn, dlopen_from_zip_absolute_path) { |
| 238 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH; |
| 239 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 240 | 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] | 241 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 242 | |
Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 243 | uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle, "dlopen_testlib_taxicab_number")); |
| 244 | ASSERT_DL_NOTNULL(taxicab_number); |
| 245 | EXPECT_EQ(1729U, *taxicab_number); |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 246 | |
| 247 | dlclose(handle); |
| 248 | } |
| 249 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 250 | TEST(dlfcn, dlopen_from_zip_with_dt_runpath) { |
| 251 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH_WITH_RUNPATH; |
| 252 | |
| 253 | void* handle = dlopen((lib_path + "!/libdir/libtest_dt_runpath_d_zip.so").c_str(), RTLD_NOW); |
| 254 | |
| 255 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 256 | |
| 257 | typedef void *(* dlopen_b_fn)(); |
| 258 | dlopen_b_fn fn = (dlopen_b_fn)dlsym(handle, "dlopen_b"); |
| 259 | ASSERT_TRUE(fn != nullptr) << dlerror(); |
| 260 | |
| 261 | void *p = fn(); |
| 262 | ASSERT_TRUE(p != nullptr) << dlerror(); |
| 263 | |
| 264 | dlclose(p); |
| 265 | dlclose(handle); |
| 266 | } |
| 267 | |
Dmitriy Ivanov | 52393a5 | 2015-03-18 22:50:01 -0700 | [diff] [blame] | 268 | TEST(dlfcn, dlopen_from_zip_ld_library_path) { |
Dmitriy Ivanov | 402a750 | 2015-06-09 13:46:51 -0700 | [diff] [blame] | 269 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + LIBZIPPATH + "!/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 | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 300 | 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] | 301 | ASSERT_TRUE(start != MAP_FAILED); |
| 302 | android_dlextinfo extinfo; |
| 303 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 304 | extinfo.reserved_addr = start; |
| 305 | extinfo.reserved_size = LIBSIZE; |
| 306 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 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), |
| 312 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 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; |
| 330 | handle_ = android_dlopen_ext(LIBNAME, 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 | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 335 | 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] | 336 | ASSERT_TRUE(start != MAP_FAILED); |
| 337 | android_dlextinfo extinfo; |
| 338 | extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS_HINT; |
| 339 | extinfo.reserved_addr = start; |
| 340 | extinfo.reserved_size = LIBSIZE; |
| 341 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 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), |
| 347 | reinterpret_cast<char*>(start) + LIBSIZE); |
| 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; |
| 358 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 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 | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 369 | 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] | 370 | ASSERT_TRUE(start != MAP_FAILED); |
| 371 | munmap(start, LIBSIZE); |
| 372 | |
| 373 | android_dlextinfo extinfo; |
| 374 | extinfo.flags = ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS; |
| 375 | extinfo.reserved_addr = start; |
| 376 | |
| 377 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 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); |
| 382 | EXPECT_LT(reinterpret_cast<void*>(f), reinterpret_cast<char*>(start) + LIBSIZE); |
| 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 |
| 389 | void* addr = mmap(start, LIBSIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 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) { |
| 394 | void* start = mmap(nullptr, LIBSIZE + PAGE_SIZE, PROT_NONE, |
| 395 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 396 | ASSERT_TRUE(start != MAP_FAILED); |
| 397 | munmap(start, LIBSIZE + PAGE_SIZE); |
| 398 | void* new_addr = mmap(reinterpret_cast<uint8_t*>(start) + PAGE_SIZE, LIBSIZE, PROT_NONE, |
| 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 | |
| 406 | handle_ = android_dlopen_ext(LIBNAME, RTLD_NOW, &extinfo); |
| 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 | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 414 | 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] | 415 | ASSERT_TRUE(start != MAP_FAILED); |
| 416 | extinfo_.flags = ANDROID_DLEXT_RESERVED_ADDRESS; |
| 417 | extinfo_.reserved_addr = start; |
| 418 | extinfo_.reserved_size = LIBSIZE; |
| 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 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 468 | void SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, size_t* pss_out); |
| 469 | |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 470 | android_dlextinfo extinfo_; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | TEST_F(DlExtRelroSharingTest, ChildWritesGoodData) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 474 | TemporaryFile tf; // Use tf to get an unique filename. |
| 475 | ASSERT_NOERROR(close(tf.fd)); |
| 476 | |
| 477 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 478 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 479 | |
| 480 | // Use destructor of tf to close and unlink the file. |
| 481 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | TEST_F(DlExtRelroSharingTest, ChildWritesNoRelro) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 485 | TemporaryFile tf; // // Use tf to get an unique filename. |
| 486 | ASSERT_NOERROR(close(tf.fd)); |
| 487 | |
| 488 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME_NORELRO, tf.filename)); |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 489 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME_NORELRO)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 490 | |
| 491 | // Use destructor of tf to close and unlink the file. |
| 492 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | TEST_F(DlExtRelroSharingTest, RelroFileEmpty) { |
Torne (Richard Coles) | 26ec967 | 2014-04-30 15:48:40 +0100 | [diff] [blame] | 496 | ASSERT_NO_FATAL_FAILURE(TryUsingRelro(LIBNAME)); |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 497 | } |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 498 | |
| 499 | TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) { |
Dan Albert | 69fb9f3 | 2014-09-03 11:30:21 -0700 | [diff] [blame] | 500 | if (geteuid() != 0) { |
| 501 | GTEST_LOG_(INFO) << "This test must be run as root.\n"; |
| 502 | return; |
| 503 | } |
| 504 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 505 | TemporaryFile tf; // Use tf to get an unique filename. |
| 506 | ASSERT_NOERROR(close(tf.fd)); |
| 507 | |
| 508 | ASSERT_NO_FATAL_FAILURE(CreateRelroFile(LIBNAME, tf.filename)); |
| 509 | |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 510 | int pipefd[2]; |
| 511 | ASSERT_NOERROR(pipe(pipefd)); |
| 512 | |
| 513 | size_t without_sharing, with_sharing; |
| 514 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, false, &without_sharing)); |
| 515 | ASSERT_NO_FATAL_FAILURE(SpawnChildrenAndMeasurePss(LIBNAME, true, &with_sharing)); |
| 516 | |
| 517 | // We expect the sharing to save at least 10% of the total PSS. In practice |
| 518 | // it saves 40%+ for this test. |
| 519 | size_t expected_size = without_sharing - (without_sharing/10); |
| 520 | EXPECT_LT(with_sharing, expected_size); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 521 | |
| 522 | // Use destructor of tf to close and unlink the file. |
| 523 | tf.fd = extinfo_.relro_fd; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void getPss(pid_t pid, size_t* pss_out) { |
| 527 | pm_kernel_t* kernel; |
| 528 | ASSERT_EQ(0, pm_kernel_create(&kernel)); |
| 529 | |
| 530 | pm_process_t* process; |
| 531 | ASSERT_EQ(0, pm_process_create(kernel, pid, &process)); |
| 532 | |
| 533 | pm_map_t** maps; |
| 534 | size_t num_maps; |
| 535 | ASSERT_EQ(0, pm_process_maps(process, &maps, &num_maps)); |
| 536 | |
| 537 | size_t total_pss = 0; |
| 538 | for (size_t i = 0; i < num_maps; i++) { |
| 539 | pm_memusage_t usage; |
| 540 | ASSERT_EQ(0, pm_map_usage(maps[i], &usage)); |
| 541 | total_pss += usage.pss; |
| 542 | } |
| 543 | *pss_out = total_pss; |
| 544 | |
| 545 | free(maps); |
| 546 | pm_process_destroy(process); |
| 547 | pm_kernel_destroy(kernel); |
| 548 | } |
| 549 | |
| 550 | void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool share_relro, |
| 551 | size_t* pss_out) { |
| 552 | const int CHILDREN = 20; |
| 553 | |
| 554 | // Create children |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 555 | pid_t child_pids[CHILDREN]; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 556 | int childpipe[CHILDREN]; |
| 557 | for (int i=0; i<CHILDREN; ++i) { |
| 558 | char read_buf; |
| 559 | int child_done_pipe[2], parent_done_pipe[2]; |
| 560 | ASSERT_NOERROR(pipe(child_done_pipe)); |
| 561 | ASSERT_NOERROR(pipe(parent_done_pipe)); |
| 562 | |
| 563 | pid_t child = fork(); |
| 564 | if (child == 0) { |
| 565 | // close the 'wrong' ends of the pipes in the child |
| 566 | close(child_done_pipe[0]); |
| 567 | close(parent_done_pipe[1]); |
| 568 | |
| 569 | // open the library |
| 570 | void* handle; |
| 571 | if (share_relro) { |
| 572 | handle = android_dlopen_ext(lib, RTLD_NOW, &extinfo_); |
| 573 | } else { |
| 574 | handle = dlopen(lib, RTLD_NOW); |
| 575 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 576 | if (handle == nullptr) { |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 577 | fprintf(stderr, "in child: %s\n", dlerror()); |
| 578 | exit(1); |
| 579 | } |
| 580 | |
| 581 | // close write end of child_done_pipe to signal the parent that we're done. |
| 582 | close(child_done_pipe[1]); |
| 583 | |
| 584 | // wait for the parent to close parent_done_pipe, then exit |
| 585 | read(parent_done_pipe[0], &read_buf, 1); |
| 586 | exit(0); |
| 587 | } |
| 588 | |
| 589 | ASSERT_NOERROR(child); |
| 590 | |
| 591 | // close the 'wrong' ends of the pipes in the parent |
| 592 | close(child_done_pipe[1]); |
| 593 | close(parent_done_pipe[0]); |
| 594 | |
| 595 | // wait for the child to be done |
| 596 | read(child_done_pipe[0], &read_buf, 1); |
| 597 | close(child_done_pipe[0]); |
| 598 | |
| 599 | // save the child's pid and the parent_done_pipe |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 600 | child_pids[i] = child; |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 601 | childpipe[i] = parent_done_pipe[1]; |
| 602 | } |
| 603 | |
| 604 | // Sum the PSS of all the children |
| 605 | size_t total_pss = 0; |
| 606 | for (int i=0; i<CHILDREN; ++i) { |
| 607 | size_t child_pss; |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 608 | ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss)); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 609 | total_pss += child_pss; |
| 610 | } |
| 611 | *pss_out = total_pss; |
| 612 | |
| 613 | // Close pipes and wait for children to exit |
| 614 | for (int i=0; i<CHILDREN; ++i) { |
| 615 | ASSERT_NOERROR(close(childpipe[i])); |
| 616 | } |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 617 | for (int i = 0; i < CHILDREN; ++i) { |
| 618 | AssertChildExited(child_pids[i], 0); |
Torne (Richard Coles) | 2605261 | 2014-05-02 14:57:42 +0100 | [diff] [blame] | 619 | } |
| 620 | } |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 621 | |
| 622 | // Testing namespaces |
| 623 | static const char* g_public_lib = "libnstest_public.so"; |
| 624 | |
| 625 | TEST(dlext, ns_smoke) { |
| 626 | static const char* root_lib = "libnstest_root.so"; |
| 627 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 628 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 629 | ASSERT_FALSE(android_init_namespaces(path.c_str(), nullptr)); |
| 630 | ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: " |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 631 | "\"libnstest_public.so\" was not found in the default namespace", dlerror()); |
| 632 | |
Dimitry Ivanov | 5480761 | 2016-04-21 14:57:38 -0700 | [diff] [blame] | 633 | ASSERT_FALSE(android_init_namespaces("", nullptr)); |
| 634 | ASSERT_STREQ("android_init_namespaces failed: error initializing public namespace: " |
| 635 | "the list of public libraries is empty.", dlerror()); |
| 636 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 637 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 638 | |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 639 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 640 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 641 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 642 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 643 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 644 | |
| 645 | // Check that libraries added to public namespace are NODELETE |
| 646 | dlclose(handle_public); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 647 | handle_public = dlopen((lib_path + "/public_namespace_libs/" + g_public_lib).c_str(), |
| 648 | RTLD_NOW | RTLD_NOLOAD); |
| 649 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 650 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 651 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 652 | android_namespace_t* ns1 = |
| 653 | android_create_namespace("private", nullptr, |
| 654 | (lib_path + "/private_namespace_libs").c_str(), |
| 655 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 656 | ASSERT_TRUE(ns1 != nullptr) << dlerror(); |
| 657 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 658 | android_namespace_t* ns2 = |
| 659 | android_create_namespace("private_isolated", nullptr, |
| 660 | (lib_path + "/private_namespace_libs").c_str(), |
| 661 | ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 662 | ASSERT_TRUE(ns2 != nullptr) << dlerror(); |
| 663 | |
| 664 | // This should not have affect search path for default namespace: |
| 665 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 666 | void* handle = dlopen(g_public_lib, RTLD_NOW); |
| 667 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 668 | dlclose(handle); |
| 669 | |
| 670 | android_dlextinfo extinfo; |
| 671 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 672 | extinfo.library_namespace = ns1; |
| 673 | |
| 674 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 675 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 676 | |
| 677 | extinfo.library_namespace = ns2; |
| 678 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 679 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 680 | |
| 681 | ASSERT_TRUE(handle1 != handle2); |
| 682 | |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 683 | // dlopen for a public library using an absolute path should work for isolated namespaces |
| 684 | extinfo.library_namespace = ns2; |
| 685 | handle = android_dlopen_ext(lib_public_path.c_str(), RTLD_NOW, &extinfo); |
| 686 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 687 | ASSERT_TRUE(handle == handle_public); |
| 688 | |
| 689 | dlclose(handle); |
| 690 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 691 | typedef const char* (*fn_t)(); |
| 692 | |
| 693 | fn_t ns_get_local_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 694 | ASSERT_TRUE(ns_get_local_string1 != nullptr) << dlerror(); |
| 695 | fn_t ns_get_local_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string")); |
| 696 | ASSERT_TRUE(ns_get_local_string2 != nullptr) << dlerror(); |
| 697 | |
| 698 | EXPECT_STREQ("This string is local to root library", ns_get_local_string1()); |
| 699 | EXPECT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 700 | |
| 701 | ASSERT_TRUE(ns_get_local_string1() != ns_get_local_string2()); |
| 702 | |
| 703 | fn_t ns_get_private_extern_string1 = |
| 704 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 705 | ASSERT_TRUE(ns_get_private_extern_string1 != nullptr) << dlerror(); |
| 706 | fn_t ns_get_private_extern_string2 = |
| 707 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string")); |
| 708 | ASSERT_TRUE(ns_get_private_extern_string2 != nullptr) << dlerror(); |
| 709 | |
| 710 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string1()); |
| 711 | EXPECT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 712 | |
| 713 | ASSERT_TRUE(ns_get_private_extern_string1() != ns_get_private_extern_string2()); |
| 714 | |
| 715 | fn_t ns_get_public_extern_string1 = |
| 716 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 717 | ASSERT_TRUE(ns_get_public_extern_string1 != nullptr) << dlerror(); |
| 718 | fn_t ns_get_public_extern_string2 = |
| 719 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string")); |
| 720 | ASSERT_TRUE(ns_get_public_extern_string2 != nullptr) << dlerror(); |
| 721 | |
| 722 | EXPECT_STREQ("This string is from public namespace", ns_get_public_extern_string1()); |
| 723 | ASSERT_TRUE(ns_get_public_extern_string1() == ns_get_public_extern_string2()); |
| 724 | |
| 725 | // and now check that dlopen() does the right thing in terms of preserving namespace |
| 726 | fn_t ns_get_dlopened_string1 = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 727 | ASSERT_TRUE(ns_get_dlopened_string1 != nullptr) << dlerror(); |
| 728 | fn_t ns_get_dlopened_string2 = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string")); |
| 729 | ASSERT_TRUE(ns_get_dlopened_string2 != nullptr) << dlerror(); |
| 730 | |
| 731 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string1()); |
| 732 | EXPECT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 733 | |
| 734 | ASSERT_TRUE(ns_get_dlopened_string1() != ns_get_dlopened_string2()); |
| 735 | |
| 736 | dlclose(handle1); |
| 737 | |
| 738 | // Check if handle2 is still alive (and well) |
| 739 | ASSERT_STREQ("This string is local to root library", ns_get_local_string2()); |
| 740 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string2()); |
| 741 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string2()); |
| 742 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string2()); |
| 743 | |
| 744 | dlclose(handle2); |
| 745 | } |
| 746 | |
| 747 | TEST(dlext, ns_isolated) { |
| 748 | static const char* root_lib = "libnstest_root_not_isolated.so"; |
| 749 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 750 | |
| 751 | 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] | 752 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 753 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 754 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 755 | |
Dmitriy Ivanov | 3cc35e2 | 2015-11-17 18:36:50 -0800 | [diff] [blame] | 756 | android_set_application_target_sdk_version(42U); // something > 23 |
| 757 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 758 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 759 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 760 | android_namespace_t* ns_not_isolated = |
| 761 | android_create_namespace("private", nullptr, |
| 762 | (lib_path + "/private_namespace_libs").c_str(), |
| 763 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 764 | ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror(); |
| 765 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 766 | android_namespace_t* ns_isolated = |
| 767 | android_create_namespace("private_isolated1", nullptr, |
| 768 | (lib_path + "/private_namespace_libs").c_str(), |
| 769 | ANDROID_NAMESPACE_TYPE_ISOLATED, nullptr); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 770 | ASSERT_TRUE(ns_isolated != nullptr) << dlerror(); |
| 771 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 772 | android_namespace_t* ns_isolated2 = |
| 773 | android_create_namespace("private_isolated2", |
| 774 | (lib_path + "/private_namespace_libs").c_str(), |
| 775 | nullptr, ANDROID_NAMESPACE_TYPE_ISOLATED, lib_path.c_str()); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 776 | ASSERT_TRUE(ns_isolated2 != nullptr) << dlerror(); |
| 777 | |
| 778 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 779 | ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror()); |
| 780 | |
| 781 | std::string lib_private_external_path = |
| 782 | lib_path + "/private_namespace_libs_external/libnstest_private_external.so"; |
| 783 | |
| 784 | // Load lib_private_external_path to default namespace |
| 785 | // (it should remain invisible for the isolated namespaces after this) |
| 786 | void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW); |
| 787 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 788 | |
| 789 | android_dlextinfo extinfo; |
| 790 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 791 | extinfo.library_namespace = ns_not_isolated; |
| 792 | |
| 793 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 794 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 795 | |
| 796 | extinfo.library_namespace = ns_isolated; |
| 797 | |
| 798 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 799 | ASSERT_TRUE(handle2 == nullptr); |
| 800 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 801 | |
| 802 | // Check dlopen by absolute path |
| 803 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 804 | ASSERT_TRUE(handle2 == nullptr); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 805 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed" |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame^] | 806 | " or dlopened by \"" + get_executable_path() + "\" is not accessible" |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 807 | " for the namespace \"private_isolated1\"", dlerror()); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 808 | |
| 809 | extinfo.library_namespace = ns_isolated2; |
| 810 | |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 811 | // this should work because isolation_path for private_isolated2 includes lib_path |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 812 | handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 813 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 814 | dlclose(handle2); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 815 | |
| 816 | // Check dlopen by absolute path |
| 817 | 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] | 818 | ASSERT_TRUE(handle2 != nullptr) << dlerror(); |
| 819 | dlclose(handle2); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 820 | |
| 821 | typedef const char* (*fn_t)(); |
| 822 | fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 823 | ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror(); |
| 824 | |
| 825 | ASSERT_STREQ("This string is local to root library", ns_get_local_string()); |
| 826 | |
| 827 | fn_t ns_get_private_extern_string = |
| 828 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 829 | ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror(); |
| 830 | |
| 831 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string()); |
| 832 | |
| 833 | fn_t ns_get_public_extern_string = |
| 834 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 835 | ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror(); |
| 836 | |
| 837 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string()); |
| 838 | |
| 839 | fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 840 | ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror(); |
| 841 | |
| 842 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string()); |
| 843 | |
| 844 | dlclose(handle1); |
| 845 | } |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 846 | |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 847 | TEST(dlext, ns_shared) { |
| 848 | static const char* root_lib = "libnstest_root_not_isolated.so"; |
| 849 | static const char* root_lib_isolated = "libnstest_root.so"; |
| 850 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 851 | |
| 852 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 853 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 854 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 855 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 856 | |
| 857 | android_set_application_target_sdk_version(42U); // something > 23 |
| 858 | |
| 859 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
| 860 | |
| 861 | // preload this library to the default namespace to check if it |
| 862 | // is shared later on. |
| 863 | void* handle_dlopened = |
| 864 | dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW); |
| 865 | ASSERT_TRUE(handle_dlopened != nullptr) << dlerror(); |
| 866 | |
| 867 | android_namespace_t* ns_not_isolated = |
| 868 | android_create_namespace("private", nullptr, |
| 869 | (lib_path + "/private_namespace_libs").c_str(), |
| 870 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr); |
| 871 | ASSERT_TRUE(ns_not_isolated != nullptr) << dlerror(); |
| 872 | |
| 873 | android_namespace_t* ns_isolated_shared = |
| 874 | android_create_namespace("private_isolated_shared", nullptr, |
| 875 | (lib_path + "/private_namespace_libs").c_str(), |
| 876 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED, |
| 877 | nullptr); |
| 878 | ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror(); |
| 879 | |
| 880 | ASSERT_TRUE(dlopen(root_lib, RTLD_NOW) == nullptr); |
| 881 | ASSERT_STREQ("dlopen failed: library \"libnstest_root_not_isolated.so\" not found", dlerror()); |
| 882 | |
| 883 | std::string lib_private_external_path = |
| 884 | lib_path + "/private_namespace_libs_external/libnstest_private_external.so"; |
| 885 | |
| 886 | // Load lib_private_external_path to default namespace |
| 887 | // (it should remain invisible for the isolated namespaces after this) |
| 888 | void* handle = dlopen(lib_private_external_path.c_str(), RTLD_NOW); |
| 889 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 890 | |
| 891 | android_dlextinfo extinfo; |
| 892 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 893 | extinfo.library_namespace = ns_not_isolated; |
| 894 | |
| 895 | void* handle1 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 896 | ASSERT_TRUE(handle1 != nullptr) << dlerror(); |
| 897 | |
| 898 | extinfo.library_namespace = ns_isolated_shared; |
| 899 | |
| 900 | void* handle2 = android_dlopen_ext(root_lib, RTLD_NOW, &extinfo); |
| 901 | ASSERT_TRUE(handle2 == nullptr); |
| 902 | ASSERT_STREQ("dlopen failed: library \"libnstest_private_external.so\" not found", dlerror()); |
| 903 | |
| 904 | // Check dlopen by absolute path |
| 905 | handle2 = android_dlopen_ext(lib_private_external_path.c_str(), RTLD_NOW, &extinfo); |
| 906 | ASSERT_TRUE(handle2 == nullptr); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 907 | ASSERT_EQ("dlopen failed: library \"" + lib_private_external_path + "\" needed" |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame^] | 908 | " or dlopened by \"" + get_executable_path() + "\" is not accessible" |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 909 | " for the namespace \"private_isolated_shared\"", dlerror()); |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 910 | |
| 911 | // load libnstest_root.so to shared namespace in order to check that everything is different |
| 912 | // except shared libnstest_dlopened.so |
| 913 | |
| 914 | handle2 = android_dlopen_ext(root_lib_isolated, RTLD_NOW, &extinfo); |
| 915 | |
| 916 | typedef const char* (*fn_t)(); |
| 917 | fn_t ns_get_local_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_local_string")); |
| 918 | ASSERT_TRUE(ns_get_local_string != nullptr) << dlerror(); |
| 919 | fn_t ns_get_local_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_local_string")); |
| 920 | ASSERT_TRUE(ns_get_local_string_shared != nullptr) << dlerror(); |
| 921 | |
| 922 | ASSERT_STREQ("This string is local to root library", ns_get_local_string()); |
| 923 | ASSERT_STREQ("This string is local to root library", ns_get_local_string_shared()); |
| 924 | ASSERT_TRUE(ns_get_local_string() != ns_get_local_string_shared()); |
| 925 | |
| 926 | fn_t ns_get_private_extern_string = |
| 927 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_private_extern_string")); |
| 928 | ASSERT_TRUE(ns_get_private_extern_string != nullptr) << dlerror(); |
| 929 | fn_t ns_get_private_extern_string_shared = |
| 930 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_private_extern_string")); |
| 931 | ASSERT_TRUE(ns_get_private_extern_string_shared() != nullptr) << dlerror(); |
| 932 | |
| 933 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string()); |
| 934 | ASSERT_STREQ("This string is from private namespace", ns_get_private_extern_string_shared()); |
| 935 | ASSERT_TRUE(ns_get_private_extern_string() != ns_get_private_extern_string_shared()); |
| 936 | |
| 937 | fn_t ns_get_public_extern_string = |
| 938 | reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_public_extern_string")); |
| 939 | ASSERT_TRUE(ns_get_public_extern_string != nullptr) << dlerror(); |
| 940 | fn_t ns_get_public_extern_string_shared = |
| 941 | reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_public_extern_string")); |
| 942 | ASSERT_TRUE(ns_get_public_extern_string_shared != nullptr) << dlerror(); |
| 943 | |
| 944 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string()); |
| 945 | ASSERT_STREQ("This string is from public namespace", ns_get_public_extern_string_shared()); |
| 946 | ASSERT_TRUE(ns_get_public_extern_string() == ns_get_public_extern_string_shared()); |
| 947 | |
| 948 | fn_t ns_get_dlopened_string = reinterpret_cast<fn_t>(dlsym(handle1, "ns_get_dlopened_string")); |
| 949 | ASSERT_TRUE(ns_get_dlopened_string != nullptr) << dlerror(); |
| 950 | fn_t ns_get_dlopened_string_shared = reinterpret_cast<fn_t>(dlsym(handle2, "ns_get_dlopened_string")); |
| 951 | ASSERT_TRUE(ns_get_dlopened_string_shared != nullptr) << dlerror(); |
| 952 | const char** ns_dlopened_string = static_cast<const char**>(dlsym(handle_dlopened, "g_private_dlopened_string")); |
| 953 | ASSERT_TRUE(ns_dlopened_string != nullptr) << dlerror(); |
| 954 | |
| 955 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string()); |
| 956 | ASSERT_STREQ("This string is from private namespace (dlopened library)", *ns_dlopened_string); |
| 957 | ASSERT_STREQ("This string is from private namespace (dlopened library)", ns_get_dlopened_string_shared()); |
| 958 | ASSERT_TRUE(ns_get_dlopened_string() != ns_get_dlopened_string_shared()); |
| 959 | ASSERT_TRUE(*ns_dlopened_string == ns_get_dlopened_string_shared()); |
| 960 | |
| 961 | dlclose(handle1); |
| 962 | dlclose(handle2); |
| 963 | } |
| 964 | |
Dimitry Ivanov | aca299a | 2016-04-11 12:42:58 -0700 | [diff] [blame] | 965 | TEST(dlext, ns_shared_dlclose) { |
| 966 | std::string path = "libc.so:libc++.so:libdl.so:libm.so"; |
| 967 | |
| 968 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 969 | |
| 970 | android_set_application_target_sdk_version(42U); // something > 23 |
| 971 | |
| 972 | ASSERT_TRUE(android_init_namespaces(path.c_str(), nullptr)) << dlerror(); |
| 973 | |
| 974 | // preload this library to the default namespace to check if it |
| 975 | // is shared later on. |
| 976 | void* handle_dlopened = |
| 977 | dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), RTLD_NOW); |
| 978 | ASSERT_TRUE(handle_dlopened != nullptr) << dlerror(); |
| 979 | |
| 980 | android_namespace_t* ns_isolated_shared = |
| 981 | android_create_namespace("private_isolated_shared", nullptr, |
| 982 | (lib_path + "/private_namespace_libs").c_str(), |
| 983 | ANDROID_NAMESPACE_TYPE_ISOLATED | ANDROID_NAMESPACE_TYPE_SHARED, |
| 984 | nullptr); |
| 985 | ASSERT_TRUE(ns_isolated_shared != nullptr) << dlerror(); |
| 986 | |
| 987 | // Check if "libnstest_dlopened.so" is loaded (and the same) |
| 988 | android_dlextinfo extinfo; |
| 989 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 990 | extinfo.library_namespace = ns_isolated_shared; |
| 991 | |
| 992 | void* handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 993 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 994 | ASSERT_TRUE(handle == handle_dlopened); |
| 995 | dlclose(handle); |
| 996 | dlclose(handle_dlopened); |
| 997 | |
| 998 | // And now check that the library cannot be found by soname (and is no longer loaded) |
| 999 | handle = android_dlopen_ext("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1000 | ASSERT_TRUE(handle == nullptr) |
| 1001 | << "Error: libnstest_dlopened.so is still accessible in shared namespace"; |
| 1002 | |
| 1003 | handle = android_dlopen_ext((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), |
| 1004 | RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1005 | ASSERT_TRUE(handle == nullptr) |
| 1006 | << "Error: libnstest_dlopened.so is still accessible in shared namespace"; |
| 1007 | |
| 1008 | handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_NOLOAD); |
| 1009 | ASSERT_TRUE(handle == nullptr) |
| 1010 | << "Error: libnstest_dlopened.so is still accessible in default namespace"; |
| 1011 | |
| 1012 | handle = dlopen((lib_path + "/private_namespace_libs/libnstest_dlopened.so").c_str(), |
| 1013 | RTLD_NOW | RTLD_NOLOAD); |
| 1014 | ASSERT_TRUE(handle == nullptr) |
| 1015 | << "Error: libnstest_dlopened.so is still accessible in default namespace"; |
| 1016 | |
| 1017 | // Now lets see if the soinfo area gets reused in the wrong way: |
| 1018 | // load a library to default namespace. |
| 1019 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 1020 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 1021 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1022 | |
| 1023 | // try to find it in shared namespace |
| 1024 | handle = android_dlopen_ext(g_public_lib, RTLD_NOW | RTLD_NOLOAD, &extinfo); |
| 1025 | ASSERT_TRUE(handle == nullptr) |
| 1026 | << "Error: " << g_public_lib << " is accessible in shared namespace"; |
| 1027 | } |
| 1028 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1029 | TEST(dlext, ns_anonymous) { |
| 1030 | static const char* root_lib = "libnstest_root.so"; |
| 1031 | std::string path = std::string("libc.so:libc++.so:libdl.so:libm.so:") + g_public_lib; |
| 1032 | |
| 1033 | const std::string lib_path = std::string(getenv("ANDROID_DATA")) + NATIVE_TESTS_PATH; |
| 1034 | |
Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 1035 | const std::string lib_public_path = lib_path + "/public_namespace_libs/" + g_public_lib; |
| 1036 | void* handle_public = dlopen(lib_public_path.c_str(), RTLD_NOW); |
| 1037 | |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1038 | ASSERT_TRUE(handle_public != nullptr) << dlerror(); |
| 1039 | |
| 1040 | ASSERT_TRUE(android_init_namespaces(path.c_str(), (lib_path + "/private_namespace_libs").c_str())) |
| 1041 | << dlerror(); |
| 1042 | |
| 1043 | android_namespace_t* ns = android_create_namespace( |
| 1044 | "private", nullptr, |
| 1045 | (lib_path + "/private_namespace_libs").c_str(), |
Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 1046 | ANDROID_NAMESPACE_TYPE_REGULAR, nullptr); |
Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 1047 | |
| 1048 | ASSERT_TRUE(ns != nullptr) << dlerror(); |
| 1049 | |
| 1050 | std::string private_library_absolute_path = lib_path + "/private_namespace_libs/" + root_lib; |
| 1051 | |
| 1052 | android_dlextinfo extinfo; |
| 1053 | extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 1054 | extinfo.library_namespace = ns; |
| 1055 | |
| 1056 | // we are going to copy this library to anonymous mmap and call the copy of ns_get_dlopened_string |
| 1057 | void* handle = android_dlopen_ext(private_library_absolute_path.c_str(), RTLD_NOW, &extinfo); |
| 1058 | ASSERT_TRUE(handle != nullptr) << dlerror(); |
| 1059 | |
| 1060 | uintptr_t ns_get_dlopened_string_addr = |
| 1061 | reinterpret_cast<uintptr_t>(dlsym(handle, "ns_get_dlopened_string")); |
| 1062 | ASSERT_TRUE(ns_get_dlopened_string_addr != 0) << dlerror(); |
| 1063 | typedef const char* (*fn_t)(); |
| 1064 | fn_t ns_get_dlopened_string_private = reinterpret_cast<fn_t>(ns_get_dlopened_string_addr); |
| 1065 | |
| 1066 | std::vector<map_record> maps; |
| 1067 | Maps::parse_maps(&maps); |
| 1068 | |
| 1069 | uintptr_t addr_start = 0; |
| 1070 | uintptr_t addr_end = 0; |
| 1071 | std::vector<map_record> maps_to_copy; |
| 1072 | |
| 1073 | for (const auto& rec : maps) { |
| 1074 | if (rec.pathname == private_library_absolute_path) { |
| 1075 | if (addr_start == 0) { |
| 1076 | addr_start = rec.addr_start; |
| 1077 | } |
| 1078 | addr_end = rec.addr_end; |
| 1079 | |
| 1080 | maps_to_copy.push_back(rec); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | // some sanity checks.. |
| 1085 | ASSERT_TRUE(addr_start > 0); |
| 1086 | ASSERT_TRUE(addr_end > 0); |
| 1087 | ASSERT_EQ(3U, maps_to_copy.size()); |
| 1088 | ASSERT_TRUE(ns_get_dlopened_string_addr > addr_start); |
| 1089 | ASSERT_TRUE(ns_get_dlopened_string_addr < addr_end); |
| 1090 | |
| 1091 | // copy |
| 1092 | uintptr_t reserved_addr = reinterpret_cast<uintptr_t>(mmap(nullptr, addr_end - addr_start, |
| 1093 | PROT_NONE, MAP_ANON | MAP_PRIVATE, |
| 1094 | -1, 0)); |
| 1095 | ASSERT_TRUE(reinterpret_cast<void*>(reserved_addr) != MAP_FAILED); |
| 1096 | |
| 1097 | for (const auto& rec : maps_to_copy) { |
| 1098 | uintptr_t offset = rec.addr_start - addr_start; |
| 1099 | size_t size = rec.addr_end - rec.addr_start; |
| 1100 | void* addr = reinterpret_cast<void*>(reserved_addr + offset); |
| 1101 | void* map = mmap(addr, size, PROT_READ | PROT_WRITE, |
| 1102 | MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0); |
| 1103 | ASSERT_TRUE(map != MAP_FAILED); |
| 1104 | memcpy(map, reinterpret_cast<void*>(rec.addr_start), size); |
| 1105 | mprotect(map, size, rec.perms); |
| 1106 | } |
| 1107 | |
| 1108 | // call the function copy |
| 1109 | uintptr_t ns_get_dlopened_string_offset = ns_get_dlopened_string_addr - addr_start; |
| 1110 | fn_t ns_get_dlopened_string_anon = reinterpret_cast<fn_t>(reserved_addr + ns_get_dlopened_string_offset); |
| 1111 | ASSERT_STREQ("This string is from private namespace (dlopened library)", |
| 1112 | ns_get_dlopened_string_anon()); |
| 1113 | |
| 1114 | // They should belong to different namespaces (private and anonymous) |
| 1115 | ASSERT_STREQ("This string is from private namespace (dlopened library)", |
| 1116 | ns_get_dlopened_string_private()); |
| 1117 | |
| 1118 | ASSERT_TRUE(ns_get_dlopened_string_anon() != ns_get_dlopened_string_private()); |
| 1119 | } |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 1120 | |
| 1121 | TEST(dlext, dlopen_handle_value_platform) { |
| 1122 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); |
| 1123 | ASSERT_TRUE((reinterpret_cast<uintptr_t>(handle) & 1) != 0) |
| 1124 | << "dlopen should return odd value for the handle"; |
| 1125 | dlclose(handle); |
| 1126 | } |
| 1127 | |
| 1128 | TEST(dlext, dlopen_handle_value_app_compat) { |
| 1129 | android_set_application_target_sdk_version(23); |
| 1130 | void* handle = dlopen("libtest_dlsym_from_this.so", RTLD_NOW | RTLD_LOCAL); |
| 1131 | ASSERT_TRUE(reinterpret_cast<uintptr_t>(handle) % sizeof(uintptr_t) == 0) |
| 1132 | << "dlopen should return valid pointer"; |
| 1133 | dlclose(handle); |
| 1134 | } |
| 1135 | |